generated from ElnuDev/rust-project
Basic renrs-gui implementation
This commit is contained in:
parent
badd78db79
commit
20a8bf41b7
10 changed files with 2913 additions and 13 deletions
2
renrs-gui/.gitignore
vendored
Normal file
2
renrs-gui/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
11
renrs-gui/Cargo.toml
Normal file
11
renrs-gui/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "renrs-gui"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
eframe = "0.21.3"
|
||||
env_logger = "0.10.0"
|
||||
renrs = { path = "../renrs" }
|
60
renrs-gui/src/lib.rs
Normal file
60
renrs-gui/src/lib.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use eframe::egui;
|
||||
use egui::*;
|
||||
use renrs::{State, Command};
|
||||
|
||||
pub fn run(file: PathBuf) -> Result<(), eframe::Error> {
|
||||
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))
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
struct App {
|
||||
state: State,
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl App {
|
||||
fn from_state(state: State) -> Self {
|
||||
let mut app = Self {
|
||||
state,
|
||||
text: "".to_owned(),
|
||||
};
|
||||
app.next();
|
||||
app
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for App {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
if ctx.input(|i|
|
||||
i.key_pressed(Key::Space)
|
||||
|| i.pointer.button_clicked(PointerButton::Primary)
|
||||
) {
|
||||
self.next();
|
||||
}
|
||||
ui.label(&self.text);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue