diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 11:38:19 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-08-02 11:38:19 +0200 |
| commit | 04a5a938994ddb95cfaa9a74b180e457d3a2b5d0 (patch) | |
| tree | 31ce9525ed3f3423678397323b65c910d63eadb1 /src/state/editor/mod.rs | |
| parent | fe0938b1de0c46fc2afcaa3dcd6a0f4ec870d21a (diff) | |
implement new lua interface
Diffstat (limited to 'src/state/editor/mod.rs')
| -rw-r--r-- | src/state/editor/mod.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/state/editor/mod.rs b/src/state/editor/mod.rs new file mode 100644 index 0000000..6fbd2b9 --- /dev/null +++ b/src/state/editor/mod.rs @@ -0,0 +1,57 @@ +use self::buffer::Buffer; + +use super::GlobalState; +use mlua::{Lua, UserData}; + +pub mod buffer; + +#[derive(Default, Debug)] +pub struct EditorState { + pub visible: bool, + pub buffer: Buffer, +} + +macro_rules! cfg { + () => { + GlobalState::instance().editor + }; +} + +macro_rules! cfg_mut { + () => { + GlobalState::instance_mut().editor + }; +} + +impl EditorState { + pub const fn new() -> Self { + Self { + visible: false, + buffer: Buffer::new(), + } + } + + pub fn run(lua: &Lua) { + let script = { cfg!().buffer.as_string() }; + if let Err(_error) = lua.load(script).exec() { + // TODO: add error handling + } + } +} + +impl UserData for EditorState { + fn add_fields<'lua, F: mlua::prelude::LuaUserDataFields<'lua, Self>>(fields: &mut F) { + fields.add_field_function_get("visible", |_, _| Ok(cfg!().visible)); + fields.add_field_function_set("visible", |_, _, visible: bool| { + cfg_mut!().visible = visible; + Ok(()) + }) + } + + fn add_methods<'lua, M: mlua::prelude::LuaUserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_function("run", |lua, _: ()| { + EditorState::run(lua); + Ok(()) + }) + } +} |