Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
make example easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
coderedart committed Jun 15, 2023
1 parent e6ecd48 commit dab7a2b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 107 deletions.
114 changes: 58 additions & 56 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@ fn main() {
fake_main();
}

fn fake_main() {
let mut window_backend = GlfwBackend::new(
Default::default(),
BackendConfig {
is_opengl: true,
opengl_config: Default::default(),
transparent: None,
},
);
let gfx_backend = GlowBackend::new(&mut window_backend, Default::default());
let lua = mlua::Lua::new();
luaegui::register_egui_bindings(&lua).unwrap();
let app = AppData {
egui_context: Default::default(),
gfx_backend,
window_backend,
lua,
code: LUA_CODE.to_string(),
script_time: std::time::Duration::ZERO,
};
GlfwBackend::run_event_loop(app);
const LUA_CODE: &str = r#"
my_data = {
text = "my text"
}
function show_fn(ui)
ui:label(my_data.text);
ui:text_edit_singleline(my_data);
if ui:button("cute button"):clicked() then
print("cute button pressed. printing my_data.text");
print(my_data.text);
end
end
function gui_run(ctx)
new_window = egui.window.new("my lua window");
new_window:show(ctx, show_fn);
end
"#;

struct AppData<W: WindowBackend, G: GfxBackend> {
pub script_time: std::time::Duration,
Expand All @@ -40,28 +35,10 @@ struct AppData<W: WindowBackend, G: GfxBackend> {
}

impl<W: WindowBackend, G: GfxBackend> UserApp for AppData<W, G> {
type UserGfxBackend = G;

type UserWindowBackend = W;

fn get_all(
&mut self,
) -> (
&mut Self::UserWindowBackend,
&mut Self::UserGfxBackend,
&egui::Context,
) {
(
&mut self.window_backend,
&mut self.gfx_backend,
&mut self.egui_context,
)
}

fn gui_run(&mut self) {
use egui::*;
let ctx = self.egui_context.clone();
Window::new("My Window").show(&ctx, |ui| {
Window::new("My Window").min_width(400.0).show(&ctx, |ui| {
if ui.button("run").clicked() {
if let Err(e) = self.lua.load(&self.code).exec() {
eprintln!("lua load error: {e:?}");
Expand All @@ -70,7 +47,11 @@ impl<W: WindowBackend, G: GfxBackend> UserApp for AppData<W, G> {
if !self.lua.globals().contains_key("gui_run").unwrap() {
ui.colored_label(Color32::RED, "gui_run fn is not defined");
}
ui.code_editor(&mut self.code);
ui.add(
egui::TextEdit::multiline(&mut self.code)
.code_editor()
.desired_width(400.0),
);
ui.horizontal(|ui| {
ui.label("script execution time (micros): ");
ui.label(format!("{}", self.script_time.as_micros()));
Expand All @@ -83,20 +64,41 @@ impl<W: WindowBackend, G: GfxBackend> UserApp for AppData<W, G> {
}
self.script_time = start.elapsed();
}
type UserGfxBackend = G;
type UserWindowBackend = W;
fn get_all(
&mut self,
) -> (
&mut Self::UserWindowBackend,
&mut Self::UserGfxBackend,
&egui::Context,
) {
(
&mut self.window_backend,
&mut self.gfx_backend,
&mut self.egui_context,
)
}
}

const LUA_CODE: &str = r#"
function show_fn(ui)
ui:label("hello");
if ui:
button("cute button"):
clicked() then
print("cute button tapped");
end
end
function gui_run(ctx)
new_window = egui.window.new("my lua window");
new_window:show(ctx, show_fn);
end
"#;
fn fake_main() {
let mut window_backend = GlfwBackend::new(
Default::default(),
BackendConfig {
is_opengl: true,
..Default::default()
},
);
let gfx_backend = GlowBackend::new(&mut window_backend, Default::default());
let lua = mlua::Lua::new();
luaegui::register_egui_bindings(&lua).unwrap();
let app = AppData {
egui_context: Default::default(),
gfx_backend,
window_backend,
lua,
code: LUA_CODE.to_string(),
script_time: std::time::Duration::ZERO,
};
GlfwBackend::run_event_loop(app);
}
102 changes: 51 additions & 51 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,6 @@ fn add_context(lua: &Lua, _: &Table) -> mlua::Result<()> {
this.request_repaint_after(std::time::Duration::from_secs_f64(duration));
Ok(())
});
// reg.add_method(
// "new_window",
// |lua, this, (window_options, add_contents): (Table, Function)| {
// let title: Value = window_options.get("title")?;
// let mut open = true;
// let mut window_option_open_exists = false;
// let mut window = egui::Window::new(WidgetText::from_lua(title)?);
// if let Ok(o) = window_options.get("open") {
// open = o;
// window_option_open_exists = true;
// window = window.open(&mut open)
// }

// let ir = window.show(this, |ui| {
// lua.scope(|scope| {
// let ui = scope.create_any_userdata_ref_mut(ui)?;
// let result: Result<MultiValue> = add_contents.call(ui);
// result
// })
// });
// if window_option_open_exists {
// window_options.set("open", open)?;
// }
// let mut result = MultiValue::new();
// if let Some(ir) = ir {
// let response = lua.create_any_userdata(ir.response)?;
// result.push_front(Value::UserData(response));
// if let Some(inner) = ir.inner {
// let inner = inner?;
// for v in inner {
// result.push_front(v);
// }
// }
// }
// Ok(result)
// },
// );
})?;
Ok(())
}
Expand Down Expand Up @@ -2018,6 +1981,22 @@ fn add_window(lua: &Lua, egui_table: &Table) -> Result<()> {
);
Ok(())
});
window.add_method_mut("default_width", |_, this, default_width: f32| {
*this = Some(
this.take()
.ok_or_else(|| mlua::Error::RuntimeError("window is null".to_owned()))?
.default_width(default_width),
);
Ok(())
});
window.add_method_mut("min_width", |_, this, width: f32| {
*this = Some(
this.take()
.ok_or_else(|| mlua::Error::RuntimeError("window is null".to_owned()))?
.min_width(width),
);
Ok(())
});
window.add_method_mut("drag_bounds", |_, this, bounds: Value| {
*this = Some(
this.take()
Expand Down Expand Up @@ -2068,22 +2047,43 @@ fn add_window(lua: &Lua, egui_table: &Table) -> Result<()> {
});
window.add_method_mut(
"show",
|lua, this, (ctx, add_contents): (UserDataRef<Context>, Function)| {
|lua, this, (ctx, add_contents, open_table): (UserDataRef<Context>, Function, Option<Table>)| {
let ctx = ctx.clone();
let mut window = this.take()
.ok_or_else(|| mlua::Error::RuntimeError("window is null".to_owned()))?;
let mut open = true;
let mut window_option_open_exists = false;
if let Some(open_table) = open_table.as_ref() {
if let Ok(o) = open_table.get::<_, bool>("open") {
open = o;
window_option_open_exists = true;
window = window.open(&mut open)
}
}
let ir = window.show(&ctx, |ui| {
lua.scope(|scope| {
let ui = scope.create_any_userdata_ref_mut(ui)?;
let result: Result<MultiValue> = add_contents.call(ui);
result
})
});
if window_option_open_exists {
open_table.unwrap().set("open", open)?;
}
let mut result = MultiValue::new();
if let Some(ir) = ir {
let response = lua.create_any_userdata(ir.response)?;
result.push_front(Value::UserData(response));
if let Some(inner) = ir.inner {
let inner = inner?;
for v in inner {
result.push_front(v);
}
}
}
Ok(result)
});

this.take()
.ok_or_else(|| mlua::Error::RuntimeError("window is null".to_owned()))?
.show(&ctx, |ui| {
lua.scope(|scope| {
let ui = scope.create_any_userdata_ref_mut(ui)?;
let _: () = add_contents.call(ui)?;
Ok(())
})
.unwrap();
});
Ok(())
},
)
})?;
let window = lua.create_table()?;
window.set(
Expand Down

0 comments on commit dab7a2b

Please sign in to comment.