diff --git a/demo/Cargo.lock b/demo/Cargo.lock index ef0bc41..9f18387 100644 --- a/demo/Cargo.lock +++ b/demo/Cargo.lock @@ -682,17 +682,6 @@ 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" @@ -982,15 +971,6 @@ 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" @@ -1211,15 +1191,6 @@ 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" @@ -1398,15 +1369,6 @@ 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" @@ -1774,10 +1736,8 @@ name = "renrs-gui" version = "0.1.0" dependencies = [ "eframe", - "egui-sfml", "env_logger", "renrs", - "sfml", ] [[package]] @@ -1871,20 +1831,6 @@ 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" @@ -2438,12 +2384,6 @@ 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 adcdf16..8539dfa 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -1,3 +1,3 @@ fn main() { - renrs_gui::run("demo.rpy".into()); + renrs_gui::run("demo.rpy".into()).unwrap(); } diff --git a/renrs-gui/Cargo.toml b/renrs-gui/Cargo.toml index 7b5eab9..2c04c02 100644 --- a/renrs-gui/Cargo.toml +++ b/renrs-gui/Cargo.toml @@ -7,7 +7,5 @@ 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 19c370a..f7d8f03 100644 --- a/renrs-gui/src/lib.rs +++ b/renrs-gui/src/lib.rs @@ -3,22 +3,23 @@ use std::thread; use std::time::Duration; use eframe::egui; +use egui::*; use renrs::{State, Command}; -use sfml::{ - graphics::{Color, RenderTarget, RenderWindow, RectangleShape, Transformable}, - system::Vector2f, - window::{Event, mouse, Key, Style}, -}; -use egui_sfml::SfEgui; - -const WIDTH: f32 = 800.0; -const HEIGHT: f32 = 600.0; - -pub fn run(file: PathBuf) { +pub fn run(file: PathBuf) -> Result<(), eframe::Error> { env_logger::init(); - let mut app = App::from_state(State::from_file(file)); - app.run(); + 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)) + )), + ) } struct App { @@ -40,13 +41,13 @@ impl App { } } - fn next(&mut self) { + fn next(&mut self, ctx: &Context) { 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(); + self.start_typing(ctx); } } @@ -62,7 +63,7 @@ impl App { *self.typing_done.lock().unwrap() = true; } - fn start_typing(&mut self) { + fn start_typing(&mut self, ctx: &Context) { // Kill previous typing thread if exists self.kill_typing(); @@ -70,6 +71,7 @@ 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()); @@ -86,6 +88,7 @@ impl App { break; } *chars.lock().unwrap() = i; + ctx.request_repaint(); thread::sleep(Duration::from_millis(50)); complete = true; } @@ -95,72 +98,31 @@ impl App { }); } - fn handle_interaction(&mut self) { + fn handle_interaction(&mut self, ctx: &Context) { if *self.typing_done.lock().unwrap() { - self.next(); + self.next(ctx); } else { self.interrupt_typing(); } } - - fn run(&mut self) { - let mut window = RenderWindow::new( - (WIDTH as u32, HEIGHT as u32), - "renrs-gui", - Style::CLOSE, - &Default::default(), - ); - let mut sfegui = SfEgui::new(&window); - window.set_vertical_sync_enabled(true); - - const SIZE: f32 = 64.0; - let mut velocity = Vector2f::new(4.0, 4.0); - let mut shape = RectangleShape::with_size(Vector2f::new(SIZE, SIZE)); - shape.set_position(Vector2f::new(SIZE, SIZE)); - - 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(), - _ => {} - } - } - - { - use egui::*; - sfegui - .do_frame(|ctx| { - egui::CentralPanel::default() - .frame(Frame { - 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(); - } - - let Vector2f { x, y } = shape.position(); - if x >= WIDTH - SIZE || x < 0.0 { - velocity.x *= -1.0; - } - if y >= HEIGHT - SIZE || y < 0.0 { - velocity.y *= -1.0; - } - shape.set_position(Vector2f::new(x + velocity.x, y + velocity.y)); - - window.clear(Color::BLACK); - window.draw(&shape); - sfegui.draw(&mut window, None); - window.display(); - } - } } + +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); + } + ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| ui.heading(&self.text[0..*self.chars.lock().unwrap()])); + }); + } +} \ No newline at end of file diff --git a/shell.nix b/shell.nix index 7656bbd..58960d2 100644 --- a/shell.nix +++ b/shell.nix @@ -29,7 +29,6 @@ mkShell { libXrandr libXi pkg-config - sfml ] ++ [ #python3 libGL