From 0272624b305c1cf6057b80a0dd6b501367dd269f Mon Sep 17 00:00:00 2001 From: QueenOfSquiggles <8940604+QueenOfSquiggles@users.noreply.github.com> Date: Thu, 28 Dec 2023 20:11:54 -0600 Subject: [PATCH] Improvement? IDK can't remember lol --- src/editor_utils.rs | 79 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/src/editor_utils.rs b/src/editor_utils.rs index 68b85aa..70f34f9 100644 --- a/src/editor_utils.rs +++ b/src/editor_utils.rs @@ -1,23 +1,68 @@ +use godot::engine::{EditorCommandPalette, EditorPlugin, IEditorPlugin, PopupMenu}; +use godot::prelude::*; +use crate::game_globals::CoreGlobals; +use crate::serialization::SquigglesSerialized; +pub fn register_editor_elements() {} +pub fn unregister_editor_elements() {} -pub fn register_editor_elements() { - // let Some(mut cmd) = EditorInterface::singleton().get_command_palette() else { - // return; - // }; - // cmd.add_command( - // "Force Core Globals to Serialize".to_godot(), - // "squiggles_core_serialize".to_godot(), - // Callable::from_fn("name", |_args: &[&Variant]| { - // CoreGlobals::singleton() - // .bind() - // .get_config() - // .bind_mut() - // .serialize(); - // Ok(Variant::nil()) - // }), - // ); +#[derive(GodotClass)] +#[class(tool, editor_plugin, init, base=EditorPlugin)] +struct SquigglesCoreEditorUtils { + tool_items: Option>, + #[base] + base: Base, } -pub fn unregister_editor_elements() {} +#[godot_api] +impl IEditorPlugin for SquigglesCoreEditorUtils { + fn enter_tree(&mut self) { + let menu = PopupMenu::new_alloc(); + self.base + .add_tool_submenu_item("Squiggles Core".to_godot(), menu.clone()); + self.tool_items = Some(menu); + let Some(editor) = self.base.get_editor_interface() else { + return; + }; + let Some(mut cmd) = editor.get_command_palette() else { + return; + }; + self.register_tool_item( + "force_globals_serialize", + "Forces the core globals to serialize their data to the user dir. Clears the missing file warnings.", + Callable::from_fn("name", |_args: &[&Variant]| { + CoreGlobals::singleton() + .bind() + .get_config() + .bind_mut() + .serialize(); + Ok(Variant::nil()) + }), + &mut cmd, + ); + } + + fn exit_tree(&mut self) {} +} + +impl SquigglesCoreEditorUtils { + /// registers a callable command in both the tools dropdown pane of the editor and the command palette for quick access + fn register_tool_item( + &mut self, + name: &str, + description: &str, + func: Callable, + command_palette: &mut Gd, + ) { + let fname = ("squiggles_core/".to_string() + name).to_godot(); + if let Some(mut menu_items) = self.tool_items.clone() { + menu_items.add_item(name.to_godot()); + let index = menu_items.get_item_count() - 1; + menu_items.set_item_tooltip(index, description.to_godot()) + } + self.base.add_tool_menu_item(fname.clone(), func.clone()); + command_palette.add_command(name.to_godot(), fname, func); + } +}