From 203a87ae2fa9e84239c12ad103c8a8476adfc28e Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Tue, 23 May 2023 13:53:00 -0700 Subject: [PATCH] Use sfml, egui-sfml instead of eframe --- demo/Cargo.lock | 60 +++++++++++++++++++++++++ demo/src/main.rs | 2 +- renrs-gui/Cargo.toml | 2 + renrs-gui/src/lib.rs | 105 +++++++++++++++++++++++++++---------------- shell.nix | 1 + 5 files changed, 130 insertions(+), 40 deletions(-) diff --git a/demo/Cargo.lock b/demo/Cargo.lock index 9f18387..ef0bc41 100644 --- a/demo/Cargo.lock +++ b/demo/Cargo.lock @@ -682,6 +682,17 @@ dependencies = [ "tracing", ] +[[package]] +name = "egui-sfml" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c29f2eb88bc1c2043f5cb807173b643d4241502d97c955880ffc86ca9fb21dc" +dependencies = [ + "egui", + "glu-sys", + "sfml", +] + [[package]] name = "egui-winit" version = "0.21.1" @@ -971,6 +982,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "glu-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b96e6aab9cc22fe28d706c0efb380f1825d660a27ccf9a26591fb327905cc3f3" +dependencies = [ + "libc", +] + [[package]] name = "glutin" version = "0.30.8" @@ -1191,6 +1211,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.3.8" @@ -1369,6 +1398,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_enum" version = "0.5.11" @@ -1736,8 +1774,10 @@ name = "renrs-gui" version = "0.1.0" dependencies = [ "eframe", + "egui-sfml", "env_logger", "renrs", + "sfml", ] [[package]] @@ -1831,6 +1871,20 @@ dependencies = [ "syn 2.0.16", ] +[[package]] +name = "sfml" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc90f712d9a93d8db54e53281dbb6dbb29e443abcc83e185df4bbe60ba6e8311" +dependencies = [ + "bitflags", + "cc", + "link-cplusplus", + "num-traits", + "once_cell", + "widestring", +] + [[package]] name = "sha1" version = "0.10.5" @@ -2384,6 +2438,12 @@ dependencies = [ "web-sys", ] +[[package]] +name = "widestring" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "653f141f39ec16bba3c5abe400a0c60da7468261cc2cbf36805022876bc721a8" + [[package]] name = "winapi" version = "0.3.9" diff --git a/demo/src/main.rs b/demo/src/main.rs index 8539dfa..adcdf16 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -1,3 +1,3 @@ fn main() { - renrs_gui::run("demo.rpy".into()).unwrap(); + renrs_gui::run("demo.rpy".into()); } diff --git a/renrs-gui/Cargo.toml b/renrs-gui/Cargo.toml index 2c04c02..7b5eab9 100644 --- a/renrs-gui/Cargo.toml +++ b/renrs-gui/Cargo.toml @@ -7,5 +7,7 @@ edition = "2021" [dependencies] eframe = "0.21.3" +egui-sfml = "0.4.0" env_logger = "0.10.0" renrs = { path = "../renrs" } +sfml = "0.20.0" diff --git a/renrs-gui/src/lib.rs b/renrs-gui/src/lib.rs index f7d8f03..49bf052 100644 --- a/renrs-gui/src/lib.rs +++ b/renrs-gui/src/lib.rs @@ -3,23 +3,19 @@ use std::thread; use std::time::Duration; use eframe::egui; -use egui::*; use renrs::{State, Command}; -pub fn run(file: PathBuf) -> Result<(), eframe::Error> { +use sfml::{ + graphics::{Color, RenderTarget, RenderWindow, Shape, RectangleShape}, + system::Vector2f, + window::{Event, mouse, Key, Style}, +}; +use egui_sfml::SfEgui; + +pub fn run(file: PathBuf) { env_logger::init(); - let options = eframe::NativeOptions { - initial_window_size: Some(egui::vec2(640.0, 480.0)), - resizable: false, // prevent i3 from automatically resizing - ..Default::default() - }; - eframe::run_native( - "renrs-gui", - options, - Box::new(|_cc| Box::new( - App::from_state(State::from_file(file)) - )), - ) + let mut app = App::from_state(State::from_file(file)); + app.run(); } struct App { @@ -41,13 +37,13 @@ impl App { } } - fn next(&mut self, ctx: &Context) { + fn next(&mut self) { if let Some(Command::Say { name, text }) = self.state.next_command() { self.text = match name { Some(name) => format!("{name}: {text}"), None => text, }; - self.start_typing(ctx); + self.start_typing(); } } @@ -63,7 +59,7 @@ impl App { *self.typing_done.lock().unwrap() = true; } - fn start_typing(&mut self, ctx: &Context) { + fn start_typing(&mut self) { // Kill previous typing thread if exists self.kill_typing(); @@ -71,7 +67,6 @@ impl App { *self.typing_done.lock().unwrap() = false; // Set up references to be passed into thread - let ctx = ctx.clone(); let kill = { let kill = Arc::new(Mutex::new(false)); self.typing_kill = Some(kill.clone()); @@ -88,7 +83,6 @@ impl App { break; } *chars.lock().unwrap() = i; - ctx.request_repaint(); thread::sleep(Duration::from_millis(50)); complete = true; } @@ -98,31 +92,64 @@ impl App { }); } - fn handle_interaction(&mut self, ctx: &Context) { + fn handle_interaction(&mut self) { if *self.typing_done.lock().unwrap() { - self.next(ctx); + self.next(); } else { self.interrupt_typing(); } } -} -impl eframe::App for App { - fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - egui::CentralPanel::default() - .frame(Frame { - fill: Color32::BLACK, - inner_margin: Margin::same(32.0), - ..Default::default() - }) - .show(ctx, |ui| { - if ctx.input(|i| - i.key_pressed(Key::Space) - || i.pointer.button_clicked(PointerButton::Primary) - ) { - self.handle_interaction(ctx); + fn run(&mut self) { + let mut window = RenderWindow::new( + (800, 600), + "renrs-gui", + Style::CLOSE, + &Default::default(), + ); + let mut sfegui = SfEgui::new(&window); + window.set_vertical_sync_enabled(true); + + let mut shape = RectangleShape::with_size(Vector2f::new(64.0, 64.0)); + shape.set_fill_color(Color::RED); + shape.set_outline_color(Color::GREEN); + shape.set_outline_thickness(3.); + + while window.is_open() { + while let Some(event) = window.poll_event() { + match event { + Event::Closed | Event::KeyPressed { code: Key::Escape, .. } => return, + Event::MouseButtonPressed { button: mouse::Button::Left, .. } | Event::KeyPressed { code: Key::Space, .. } => self.handle_interaction(), + _ => {} } - ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| ui.heading(&self.text[0..*self.chars.lock().unwrap()])); - }); + } + + { + use egui::*; + sfegui + .do_frame(|ctx| { + egui::CentralPanel::default() + .frame(Frame { + fill: Color32::BLACK, + inner_margin: Margin::same(32.0), + ..Default::default() + }) + .show(ctx, |ui| { + if ctx.input(|i| + i.key_pressed(Key::Space) + || i.pointer.button_clicked(PointerButton::Primary) + ) { + self.handle_interaction(); + } + ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| ui.heading(&self.text[0..*self.chars.lock().unwrap()])); + }); + }).unwrap(); + } + + window.clear(Color::BLACK); + window.draw(&shape); + sfegui.draw(&mut window, None); + window.display(); + } } -} \ No newline at end of file +} diff --git a/shell.nix b/shell.nix index 58960d2..7656bbd 100644 --- a/shell.nix +++ b/shell.nix @@ -29,6 +29,7 @@ mkShell { libXrandr libXi pkg-config + sfml ] ++ [ #python3 libGL