From a0c00a810a8505f938086fdde1d7b285369489a7 Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Sat, 2 Dec 2023 14:22:05 +0000 Subject: [PATCH] feat: initial porting to egui Former-commit-id: 9d3753be324466c78bf14fcc14e89fc414812f4a --- "\\" | 117 +++++++++++++++++++++++++++++++ src/helpers.rs | 2 - src/main.rs | 185 +++++++++++++++++++------------------------------ 3 files changed, 189 insertions(+), 115 deletions(-) create mode 100644 "\\" diff --git "a/\\" "b/\\" new file mode 100644 index 0000000..d6253a8 --- /dev/null +++ "b/\\" @@ -0,0 +1,117 @@ +use bevy::{ + core_pipeline::{ + tonemapping::Tonemapping, + }, + window::*, + prelude::*, +}; + +use bevy_egui::{egui, EguiContexts, EguiPlugin}; + +use std::collections::HashMap; +use lazy_static::lazy_static; + +mod helpers; +use crate::helpers::*; + +const VERSION: &str = env!("CARGO_PKG_VERSION"); +const PKGNAME: &str = env!("CARGO_PKG_NAME"); + +// Create a map of the Hypernova colorscheme +lazy_static!{ + static ref HYPERNOVA: HashMap<&'static str, (u8, u8, u8)> = vec![ + ("BLACK", (13, 13, 15)), + ("DARK_GRAY", (21, 21, 23)), + ("GRAY", (39, 39, 43)), + ("LIGHT_GRAY", (69, 68, 73)), + ("SUBTEXT", (217, 208, 215)), + ("WHITE", (236, 229, 234)), + ("RED", (240, 105, 105)), + ("MAGENTA", (232, 135, 187)), + ("PURPLE", (162, 146, 232)), + ("BLUE", (120, 175, 196)), + ("CYAN", (126, 230, 174)), + ("GREEN", (145, 214, 92)), + ("YELLOW", (217, 213, 100)), + ].iter().copied().collect(); +} + +fn main() { + App::new() + .add_plugins( + ( + DefaultPlugins.set(WindowPlugin { + primary_window: Some(Window { + title: format!("{} {}", titlecase(PKGNAME), VERSION).into(), + mode: WindowMode::Fullscreen, + ..default() + }), + ..default() + }), + EguiPlugin + ) + ) + .add_systems(Startup, + (setup, setup_ui)) + .add_systems(Update, render_ui) + .run(); +} + +// Bevy engine setup +fn setup(mut commands: Commands) { + commands.spawn( + Camera2dBundle { + camera: Camera { + hdr: true, + ..default() + }, + tonemapping: Tonemapping::TonyMcMapface, + ..default() + } + ); +} + +// 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); + + + let mut username_text: String = "Username".to_string(); +} + +// On update: render the UI +fn render_ui( + mut contexts: EguiContexts, + mut windows: Query<&mut Window>, +) { + let window = windows.single_mut(); + let window_width = window.resolution.width(); + let window_height = window.resolution.height(); + + egui::Window::new("Login") + .anchor(egui::Align2::CENTER_CENTER, egui::Vec2::new(0., 0.)) + .resizable(false) + .title_bar(false) + .show(contexts.ctx_mut(), |ui| { + ui.set_width(window_width / 2.5); + 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); + + ui.heading(egui::RichText::new("Login").size(30.).color(purple)); + + let username = egui::TextEdit::singleline(&mut username_text); + let output = username.show(ui); + }); + }); +} diff --git a/src/helpers.rs b/src/helpers.rs index 1354ef1..fe25221 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,5 +1,3 @@ -use crate::helpers; - pub fn titlecase(s: &str) -> String { let mut c = s.chars(); match c.next() { diff --git a/src/main.rs b/src/main.rs index 2ec1759..6d53e79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,9 @@ -use bevy::{ - core_pipeline::{ - tonemapping::Tonemapping, - }, - window::*, - prelude::*, - winit::WinitSettings, -}; -use std::collections::HashMap; +use bevy::{core_pipeline::tonemapping::Tonemapping, prelude::*, window::*}; + +use bevy_egui::{egui, EguiContexts, EguiPlugin}; + use lazy_static::lazy_static; +use std::collections::HashMap; mod helpers; use crate::helpers::*; @@ -16,29 +12,30 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); const PKGNAME: &str = env!("CARGO_PKG_NAME"); // Create a map of the Hypernova colorscheme -lazy_static!{ - static ref HYPERNOVA: HashMap<&'static str, Color> = vec![ - ("BLACK", Color::hex("0d0d0f").unwrap()), - ("DARK_GRAY", Color::hex("151517").unwrap()), - ("GRAY", Color::hex("27272b").unwrap()), - ("LIGHT_GRAY", Color::hex("454449").unwrap()), - ("SUBTEXT", Color::hex("d9d0d7").unwrap()), - ("WHITE", Color::hex("ece5ea").unwrap()), - ("RED", Color::hex("f06969").unwrap()), - ("MAGENTA", Color::hex("e887bb").unwrap()), - ("PURPLE", Color::hex("a292e8").unwrap()), - ("BLUE", Color::hex("78b9c4").unwrap()), - ("CYAN", Color::hex("7ee6ae").unwrap()), - ("GREEN", Color::hex("91d65c").unwrap()), - ("YELLOW", Color::hex("d9d564").unwrap()), - ].iter().copied().collect(); +lazy_static! { + static ref HYPERNOVA: HashMap<&'static str, (u8, u8, u8)> = vec![ + ("BLACK", (13, 13, 15)), + ("DARK_GRAY", (21, 21, 23)), + ("GRAY", (39, 39, 43)), + ("LIGHT_GRAY", (69, 68, 73)), + ("SUBTEXT", (217, 208, 215)), + ("WHITE", (236, 229, 234)), + ("RED", (240, 105, 105)), + ("MAGENTA", (232, 135, 187)), + ("PURPLE", (162, 146, 232)), + ("BLUE", (120, 175, 196)), + ("CYAN", (126, 230, 174)), + ("GREEN", (145, 214, 92)), + ("YELLOW", (217, 213, 100)), + ] + .iter() + .copied() + .collect(); } fn main() { App::new() - // Only run the app when there is user input, significantly reducing CPU/GPU usage - .insert_resource(WinitSettings::desktop_app()) - .add_plugins( + .add_plugins(( DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { title: format!("{} {}", titlecase(PKGNAME), VERSION).into(), @@ -46,99 +43,61 @@ fn main() { ..default() }), ..default() - }) - ) - .add_systems( - Startup, - (setup, setup_ui) - ) + }), + EguiPlugin, + )) + .add_systems(Startup, (setup, setup_ui)) + .add_systems(Update, render_ui) .run(); } +// Bevy engine setup fn setup(mut commands: Commands) { - commands.spawn( - Camera2dBundle { - camera: Camera { - hdr: true, - ..default() - }, - tonemapping: Tonemapping::TonyMcMapface, + commands.spawn(Camera2dBundle { + camera: Camera { + hdr: true, ..default() - } - ); + }, + tonemapping: Tonemapping::TonyMcMapface, + ..default() + }); } -fn setup_ui( - server: Res, - mut commands: Commands -) { - let bold_font: Handle = server.load("fonts/VictorMono-Bold.otf"); - let regular_font: Handle = server.load("fonts/VictorMono-Regular.otf"); +// 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); +} - let title_style = TextStyle { - font: bold_font.clone(), - font_size: 70., - color: HYPERNOVA.get("WHITE").copied().unwrap(), - }; - let login_style = TextStyle { - font: bold_font.clone(), - font_size: 30., - color: HYPERNOVA.get("PURPLE").copied().unwrap(), - }; +// On update: render the UI +fn render_ui(mut contexts: EguiContexts, mut windows: Query<&mut Window>) { + let window = windows.single_mut(); + let window_width = window.resolution.width(); + let window_height = window.resolution.height(); - commands - .spawn(NodeBundle { - style: Style { - width: Val::Percent(100.), - height: Val::Percent(100.0), - flex_direction: FlexDirection::Column, - align_items: AlignItems::Center, - ..default() - }, - background_color: BackgroundColor(HYPERNOVA.get("DARK_GRAY").copied().unwrap()), - ..default() - }) - .with_children(|parent| { - parent - .spawn(TextBundle::from_section("Ambition", title_style.clone()) - .with_text_alignment(TextAlignment::Center) - .with_style(Style { - position_type: PositionType::Absolute, - margin: UiRect { - top: Val::Vh(10.), - ..default() - }, - ..default() - }) - ); - parent - .spawn(NodeBundle { - style: Style { - flex_direction: FlexDirection::Column, - align_items: AlignItems::Center, - width: Val::Percent(50.), - height: Val::Percent(40.), - margin: UiRect { - top: Val::Vh(30.), - ..default() - }, - padding: UiRect { - top: Val::Percent(2.), - ..default() - }, - ..default() - }, - background_color: BackgroundColor(HYPERNOVA.get("GRAY").copied().unwrap()), - ..default() - }) - .with_children(|parent| { - parent - .spawn(TextBundle::from_section("Login", login_style.clone()) - .with_text_alignment(TextAlignment::Center) - .with_style(Style { - position_type: PositionType::Absolute, - ..default() - })); - }); + egui::Window::new("Login") + .anchor(egui::Align2::CENTER_CENTER, egui::Vec2::new(0., 0.)) + .resizable(false) + .title_bar(false) + .show(contexts.ctx_mut(), |ui| { + ui.set_width(window_width / 2.5); + 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); + + ui.heading(egui::RichText::new("Login").size(30.).color(purple)); + }); }); }