From 97af97ab2e55f08ef4d4282861537ab2f96ef17b Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Mon, 4 Dec 2023 21:23:27 +0000 Subject: [PATCH] refactor: move UI code to separate file Former-commit-id: 7091f9d14e330308714e0bcd495624f2e8f9f95d --- src/main.rs | 101 +++----------------------------------------------- src/player.rs | 2 +- src/ui.rs | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 97 deletions(-) create mode 100644 src/ui.rs diff --git a/src/main.rs b/src/main.rs index 9073573..62c936f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,19 @@ use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*, window::*, winit::WinitSettings}; -use bevy_egui::{egui, EguiContexts, EguiPlugin}; +use bevy_egui::EguiPlugin; use lazy_static::lazy_static; use std::collections::HashMap; +// Load modules from other files mod helpers; use crate::helpers::*; - mod player; use crate::player::*; +mod ui; +use crate::ui::*; +// Version information const VERSION: &str = env!("CARGO_PKG_VERSION"); const PKGNAME: &str = env!("CARGO_PKG_NAME"); @@ -58,18 +61,6 @@ fn main() { .run(); } -// Define UI resources -#[derive(Default, Resource)] -struct UiState { - username: String, - password: String, -} - -#[derive(Default, Resource)] -struct OpenWindows { - login_open: bool, -} - // Bevy engine setup fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2dBundle { @@ -91,85 +82,3 @@ fn setup(mut commands: Commands, asset_server: Res) { }, )); } - -// On startup: setup some UI components -fn setup_ui(mut contexts: EguiContexts) { - // Set Victor Mono as the default custom font - let mut fonts = egui::FontDefinitions::default(); - fonts.font_data.insert( - "victor_mono".to_owned(), - egui::FontData::from_static(include_bytes!("../assets/fonts/VictorMono-Regular.otf")), - ); - fonts - .families - .get_mut(&egui::FontFamily::Proportional) - .unwrap() - .insert(0, "victor_mono".to_owned()); - contexts.ctx_mut().set_fonts(fonts); -} - -// On update: render the UI -fn render_ui( - mut contexts: EguiContexts, - mut windows: Query<&mut Window>, - mut ui_state: ResMut, - mut open_windows: ResMut, -) { - let window = windows.single_mut(); - let window_width = window.resolution.width(); - let window_height = window.resolution.height(); - - let ctx = contexts.ctx_mut(); - - egui::Window::new("Login") - .anchor(egui::Align2::CENTER_CENTER, egui::Vec2::new(0., 0.)) - .resizable(false) - .title_bar(false) - .open(&mut open_windows.login_open) - .show(ctx, |ui| { - ui.set_width(window_width / 3.); - ui.set_height(window_height / 3.); - - ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| { - let purple = HYPERNOVA.get("PURPLE").unwrap(); - let purple = egui::Color32::from_rgb(purple.0, purple.1, purple.2); - - let black = HYPERNOVA.get("BLACK").unwrap(); - let black = egui::Color32::from_rgb(black.0, black.1, black.2); - - // Define spacing between items (widgets) and add manually add some space - // between the window border and the heading - ui.spacing_mut().item_spacing = egui::vec2(0., 10.); - ui.spacing_mut().button_padding = egui::vec2(20., 10.); - ui.add_space(window_height / 22.); - - ui.heading(egui::RichText::new("Login").size(30.).color(purple)); // The window 'title' - - // Manually add some space between the heading and the text inputs - ui.add_space(window_height / 28.); - - // The text inputs - egui::TextEdit::singleline(&mut ui_state.username) - .hint_text("Username") - .margin(egui::vec2(10., 10.)) - .desired_width(window_width / 4.) - .show(ui); - - egui::TextEdit::singleline(&mut ui_state.password) - .password(true) - .margin(egui::vec2(10., 10.)) - .hint_text("Password") - .desired_width(window_width / 4.) - .show(ui); - - // Manually add some space between the text inputs and the 'confirm' button - ui.add_space(window_height / 26.); - - ui.add(egui::Button::new("Confirm").fill(black)); - - // Manually add some space between the button and the bottom border of the - // window...for scaling purposes - ui.add_space(window_height / 22.); - }); - }); -} diff --git a/src/player.rs b/src/player.rs index 8001e41..5d95358 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,4 @@ -use bevy::{prelude::*, window::*}; +use bevy::{prelude::*}; // Define the player component #[derive(Component)] diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..732a97c --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,99 @@ +use bevy::{prelude::*}; + +use bevy_egui::{egui, EguiContexts}; + +use crate::HYPERNOVA; + +// Define UI resources +#[derive(Default, Resource)] +pub struct UiState { + pub username: String, + pub password: String, +} + +#[derive(Default, Resource)] +pub struct OpenWindows { + pub login_open: bool, +} + +// On startup: setup some UI components +pub fn setup_ui(mut contexts: EguiContexts) { + // Set Victor Mono as the default custom font + let mut fonts = egui::FontDefinitions::default(); + fonts.font_data.insert( + "victor_mono".to_owned(), + egui::FontData::from_static(include_bytes!("../assets/fonts/VictorMono-Regular.otf")), + ); + fonts + .families + .get_mut(&egui::FontFamily::Proportional) + .unwrap() + .insert(0, "victor_mono".to_owned()); + contexts.ctx_mut().set_fonts(fonts); +} + +// On update: render the UI +pub fn render_ui( + mut contexts: EguiContexts, + mut windows: Query<&mut Window>, + mut ui_state: ResMut, + mut open_windows: ResMut, +) { + let window = windows.single_mut(); + let window_width = window.resolution.width(); + let window_height = window.resolution.height(); + + let ctx = contexts.ctx_mut(); + + egui::Window::new("Login") + .anchor(egui::Align2::CENTER_CENTER, egui::Vec2::new(0., 0.)) + .resizable(false) + .title_bar(false) + .open(&mut open_windows.login_open) + .show(ctx, |ui| { + ui.set_width(window_width / 3.); + ui.set_height(window_height / 3.); + + ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| { + let purple = HYPERNOVA.get("PURPLE").unwrap(); + let purple = egui::Color32::from_rgb(purple.0, purple.1, purple.2); + + let black = HYPERNOVA.get("BLACK").unwrap(); + let black = egui::Color32::from_rgb(black.0, black.1, black.2); + + // Define spacing between items (widgets) and add manually add some space + // between the window border and the heading + ui.spacing_mut().item_spacing = egui::vec2(0., 10.); + ui.spacing_mut().button_padding = egui::vec2(20., 10.); + ui.add_space(window_height / 22.); + + ui.heading(egui::RichText::new("Login").size(30.).color(purple)); // The window 'title' + + // Manually add some space between the heading and the text inputs + ui.add_space(window_height / 28.); + + // The text inputs + egui::TextEdit::singleline(&mut ui_state.username) + .hint_text("Username") + .margin(egui::vec2(10., 10.)) + .desired_width(window_width / 4.) + .show(ui); + + egui::TextEdit::singleline(&mut ui_state.password) + .password(true) + .margin(egui::vec2(10., 10.)) + .hint_text("Password") + .desired_width(window_width / 4.) + .show(ui); + + // Manually add some space between the text inputs and the 'confirm' button + ui.add_space(window_height / 26.); + + ui.add(egui::Button::new("Confirm").fill(black)); + + // Manually add some space between the button and the bottom border of the + // window...for scaling purposes + ui.add_space(window_height / 22.); + }); + }); +}