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

Update godot-rust and egui #32

Merged
merged 6 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions example_project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ workspace = ".."
crate-type = ["cdylib"]

[dependencies]
#gdnative = { git = "https://github.com/godot-rust/godot-rust.git", branch = "master" }
gdnative = "0.10.0-rc.0"
egui = "0.15"
gdnative = { git = "https://github.com/godot-rust/godot-rust.git", branch = "master" }
egui = "0.18"
godot_egui = { path = "../godot_egui" }
53 changes: 15 additions & 38 deletions example_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct GodotEguiExample {
checkbox: bool,
icon_1: Ref<Texture>,
icon_2: Ref<Texture>,
use_custom_fonts: bool,
show_font_settings: bool,
}

Expand All @@ -29,13 +28,12 @@ impl GodotEguiExample {
elapsed_time: 0.0,
icon_1: load_texture("res://icon.png"),
icon_2: load_texture("res://icon_ferris.png"),
use_custom_fonts: false,
show_font_settings: false,
}
}

#[export]
pub fn _ready(&mut self, owner: TRef<Control>) {
#[godot]
pub fn _ready(&mut self, #[base] owner: TRef<Control>) {
godot_print!("Initializing godot egui");
let gui = owner
.get_node("GodotEgui")
Expand All @@ -58,17 +56,15 @@ impl GodotEguiExample {
.name("wave")
}

#[export]
pub fn _process(&mut self, _owner: TRef<Control>, delta: f64) {
#[godot]
pub fn _process(&mut self, delta: f64) {
let gui = unsafe { self.gui.as_ref().expect("GUI initialized").assume_safe() };

self.elapsed_time += delta;

// A frame can be passed to `update` specifying background color, margin and other properties
// You may also want to pass in `None` and draw a background using a regular Panel node instead.
let frame = egui::Frame { margin: egui::vec2(20.0, 20.0), ..Default::default() };

let mut should_reverse_font_priorities = false;
let frame = egui::Frame { inner_margin: egui::style::Margin::symmetric(20.0, 20.0), ..Default::default() };

gui.map_mut(|gui, instance| {
// We use the `update` method here to just draw a simple UI on the central panel. If you need more
Expand Down Expand Up @@ -102,12 +98,12 @@ impl GodotEguiExample {

ui.heading("You can even plot graphs");
ui.add_space(5.0);

let plot = egui::plot::Plot::new("plot_example")
.line(self.sin_plot())

egui::plot::Plot::new("plot_example")
.width(400.0)
.view_aspect(4.0 / 3.0);
ui.add(plot);
.view_aspect(4.0 / 3.0)
.show(ui, |ui| ui.line(self.sin_plot()));
// ui.add(plot);

ui.heading("Or use your custom images");
ui.add_space(5.0);
Expand All @@ -132,15 +128,8 @@ impl GodotEguiExample {
ui.label(
"This example registers two custom fonts. Custom fonts can be registered from the \
Godot Editor by setting font paths. For more control, you can also use \
egui::CtxRef's set_fonts method to register fonts manually.
\nEgui does not currently support locally overriding a font, but you can switch the \
global font priorities for an egui::CtxRef so that different fonts take precedence. \
The checkbox below will reverse the vector of fonts so that the last one, our \
Custom Font 2, becomes the main font.",
egui::Context's set_fonts method to register fonts manually.",
);
if ui.checkbox(&mut self.use_custom_fonts, "Reverse font priorities").clicked() {
should_reverse_font_priorities = true;
}

ui.add_space(5.0);

Expand All @@ -155,32 +144,20 @@ impl GodotEguiExample {
ui.add_space(5.0);

ui.horizontal(|ui| {
ui.label("You can also configure font settings, check it out:");
ui.label("You can also configure style settings, check it out:");
if ui.button("Font settings").clicked() {
self.show_font_settings = true;
}
});
});
});

// TODO: How fonts are stored has completely changed so this will need to be redone if it is desired in the sample project.
if self.show_font_settings {
let mut font_definitions = ctx.fonts().definitions().clone();
egui::Window::new("Settings").open(&mut self.show_font_settings).show(ctx, |ui| {
use egui::Widget;
font_definitions.ui(ui);
ui.fonts().texture().ui(ui);
egui::Window::new("Style Settings").open(&mut self.show_font_settings).show(ctx, |ui| {
ctx.style_ui(ui)
});
ctx.set_fonts(font_definitions);
}
});

if should_reverse_font_priorities {
gui.update_ctx(&instance, |ctx| {
let mut font_defs = ctx.fonts().definitions().clone();
font_defs.fonts_for_family.get_mut(&egui::FontFamily::Proportional).unwrap().reverse();
ctx.set_fonts(font_defs);
})
}
})
.expect("Map mut should succeed");
}
Expand Down
5 changes: 2 additions & 3 deletions godot_egui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ readme = "../README.md"
workspace = ".."

[dependencies]
#gdnative = { git = "https://github.com/godot-rust/godot-rust.git", branch = "master"}
gdnative = "0.10.0-rc.0"
egui = "0.15"
gdnative = { git = "https://github.com/godot-rust/godot-rust.git", branch = "master"}
egui = "0.18"
4 changes: 2 additions & 2 deletions godot_egui/src/egui_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn progress_bar(ui: &mut egui::Ui, progress: f32) -> egui::Response {
pub fn progress_bar(ui: &mut egui::Ui, progress: f32, font_size: f32) -> egui::Response {
let desired_size = ui.spacing().interact_size.y * egui::vec2(8.0, 0.8);
let (rect, response) = ui.allocate_exact_size(desired_size, egui::Sense::hover());

Expand All @@ -17,7 +17,7 @@ pub fn progress_bar(ui: &mut egui::Ui, progress: f32) -> egui::Response {
rect.center(),
egui::Align2::CENTER_CENTER,
format!("{:.2}", progress),
egui::TextStyle::Body,
egui::FontId::proportional(font_size),
ui.style().visuals.text_color(),
);

Expand Down
Loading