Use sfml, egui-sfml instead of eframe

This commit is contained in:
Elnu 2023-05-23 13:53:00 -07:00
parent a9724a4636
commit 203a87ae2f
5 changed files with 130 additions and 40 deletions

View file

@ -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"

View file

@ -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();
}
}
}
}