This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored egui's input handling.
GodotEgui
now no longer receives …
…godot input callbacks. Instead `GodotEgui` requires the parent node to pass the relevant `InputEvent` received from any of Godot's callbacks into `handle_godot_input` with a flag for whether it is gui input as only gui handles mouse positions differently. This also includes a fix to allow for free rotation of the egui nodes via the the parent's rotation.
- Loading branch information
Showing
6 changed files
with
145 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use gdnative::prelude::*; | ||
use godot_egui::*; | ||
|
||
#[derive(NativeClass)] | ||
#[inherit(Control)] | ||
pub struct GodotEguiWindowExample { | ||
gui: Option<Instance<GodotEgui, Shared>>, | ||
} | ||
|
||
#[methods] | ||
impl GodotEguiWindowExample { | ||
fn new(_: &Control) -> Self { | ||
Self { gui: None } | ||
} | ||
#[export] | ||
#[gdnative::profiled] | ||
pub fn _ready(&mut self, owner: TRef<Control>) { | ||
godot_print!("Initializing godot egui"); | ||
let gui = owner | ||
.get_node("GodotEgui") | ||
.and_then(|godot_egui| unsafe { godot_egui.assume_safe() }.cast::<Control>()) | ||
.and_then(|godot_egui| godot_egui.cast_instance::<GodotEgui>()) | ||
.expect("Expected a `GodotEgui` child with the GodotEgui nativescript class."); | ||
|
||
self.gui = Some(gui.claim()); | ||
} | ||
|
||
/// Updates egui from the `_gui_input` callback | ||
#[export] | ||
#[gdnative::profiled] | ||
pub fn _gui_input(&mut self, owner: TRef<Control>, event: Ref<InputEvent>) { | ||
let gui = unsafe { self.gui.as_ref().expect("GUI initialized").assume_safe() }; | ||
gui.map_mut(|gui, instance| { | ||
gui.handle_godot_input(instance, event, true); | ||
if gui.mouse_was_captured(instance) { | ||
owner.accept_event(); | ||
} | ||
}) | ||
.expect("map_mut should succeed"); | ||
} | ||
#[export] | ||
fn _process(&mut self, _owner: TRef<Control>, _: f64) { | ||
if let Some(gui) = &self.gui { | ||
let gui = unsafe { gui.assume_safe() }; | ||
gui.map_mut(|egui, o| { | ||
egui.update_ctx(o, |ctx| { | ||
egui::Window::new("Test Window") | ||
.frame(egui::Frame::default()) | ||
.min_height(100.0) | ||
.min_width(50.0) | ||
.resizable(true) | ||
.show(ctx, |ui| { | ||
ui.label("This is a window!"); | ||
}); | ||
}) | ||
}) | ||
.expect("this should work"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters