summaryrefslogtreecommitdiff
path: root/src/state/editor/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/editor/mod.rs')
-rw-r--r--src/state/editor/mod.rs57
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(())
+ })
+ }
+}