generated from ElnuDev/rust-project
Compare commits
2 commits
a9724a4636
...
0e5bd5ad94
Author | SHA1 | Date | |
---|---|---|---|
0e5bd5ad94 | |||
203a87ae2f |
5 changed files with 141 additions and 40 deletions
60
demo/Cargo.lock
generated
60
demo/Cargo.lock
generated
|
@ -682,6 +682,17 @@ dependencies = [
|
||||||
"tracing",
|
"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]]
|
[[package]]
|
||||||
name = "egui-winit"
|
name = "egui-winit"
|
||||||
version = "0.21.1"
|
version = "0.21.1"
|
||||||
|
@ -971,6 +982,15 @@ dependencies = [
|
||||||
"web-sys",
|
"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]]
|
[[package]]
|
||||||
name = "glutin"
|
name = "glutin"
|
||||||
version = "0.30.8"
|
version = "0.30.8"
|
||||||
|
@ -1191,6 +1211,15 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "link-cplusplus"
|
||||||
|
version = "1.0.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.3.8"
|
version = "0.3.8"
|
||||||
|
@ -1369,6 +1398,15 @@ dependencies = [
|
||||||
"minimal-lexical",
|
"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]]
|
[[package]]
|
||||||
name = "num_enum"
|
name = "num_enum"
|
||||||
version = "0.5.11"
|
version = "0.5.11"
|
||||||
|
@ -1736,8 +1774,10 @@ name = "renrs-gui"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"eframe",
|
"eframe",
|
||||||
|
"egui-sfml",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"renrs",
|
"renrs",
|
||||||
|
"sfml",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1831,6 +1871,20 @@ dependencies = [
|
||||||
"syn 2.0.16",
|
"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]]
|
[[package]]
|
||||||
name = "sha1"
|
name = "sha1"
|
||||||
version = "0.10.5"
|
version = "0.10.5"
|
||||||
|
@ -2384,6 +2438,12 @@ dependencies = [
|
||||||
"web-sys",
|
"web-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "widestring"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "653f141f39ec16bba3c5abe400a0c60da7468261cc2cbf36805022876bc721a8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
renrs_gui::run("demo.rpy".into()).unwrap();
|
renrs_gui::run("demo.rpy".into());
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,5 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
eframe = "0.21.3"
|
eframe = "0.21.3"
|
||||||
|
egui-sfml = "0.4.0"
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
renrs = { path = "../renrs" }
|
renrs = { path = "../renrs" }
|
||||||
|
sfml = "0.20.0"
|
||||||
|
|
|
@ -3,23 +3,22 @@ use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
use egui::*;
|
|
||||||
use renrs::{State, Command};
|
use renrs::{State, Command};
|
||||||
|
|
||||||
pub fn run(file: PathBuf) -> Result<(), eframe::Error> {
|
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) {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let options = eframe::NativeOptions {
|
let mut app = App::from_state(State::from_file(file));
|
||||||
initial_window_size: Some(egui::vec2(640.0, 480.0)),
|
app.run();
|
||||||
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 {
|
struct App {
|
||||||
|
@ -41,13 +40,13 @@ impl App {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next(&mut self, ctx: &Context) {
|
fn next(&mut self) {
|
||||||
if let Some(Command::Say { name, text }) = self.state.next_command() {
|
if let Some(Command::Say { name, text }) = self.state.next_command() {
|
||||||
self.text = match name {
|
self.text = match name {
|
||||||
Some(name) => format!("{name}: {text}"),
|
Some(name) => format!("{name}: {text}"),
|
||||||
None => text,
|
None => text,
|
||||||
};
|
};
|
||||||
self.start_typing(ctx);
|
self.start_typing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +62,7 @@ impl App {
|
||||||
*self.typing_done.lock().unwrap() = true;
|
*self.typing_done.lock().unwrap() = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_typing(&mut self, ctx: &Context) {
|
fn start_typing(&mut self) {
|
||||||
// Kill previous typing thread if exists
|
// Kill previous typing thread if exists
|
||||||
self.kill_typing();
|
self.kill_typing();
|
||||||
|
|
||||||
|
@ -71,7 +70,6 @@ impl App {
|
||||||
*self.typing_done.lock().unwrap() = false;
|
*self.typing_done.lock().unwrap() = false;
|
||||||
|
|
||||||
// Set up references to be passed into thread
|
// Set up references to be passed into thread
|
||||||
let ctx = ctx.clone();
|
|
||||||
let kill = {
|
let kill = {
|
||||||
let kill = Arc::new(Mutex::new(false));
|
let kill = Arc::new(Mutex::new(false));
|
||||||
self.typing_kill = Some(kill.clone());
|
self.typing_kill = Some(kill.clone());
|
||||||
|
@ -88,7 +86,6 @@ impl App {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
*chars.lock().unwrap() = i;
|
*chars.lock().unwrap() = i;
|
||||||
ctx.request_repaint();
|
|
||||||
thread::sleep(Duration::from_millis(50));
|
thread::sleep(Duration::from_millis(50));
|
||||||
complete = true;
|
complete = true;
|
||||||
}
|
}
|
||||||
|
@ -98,31 +95,72 @@ impl App {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_interaction(&mut self, ctx: &Context) {
|
fn handle_interaction(&mut self) {
|
||||||
if *self.typing_done.lock().unwrap() {
|
if *self.typing_done.lock().unwrap() {
|
||||||
self.next(ctx);
|
self.next();
|
||||||
} else {
|
} else {
|
||||||
self.interrupt_typing();
|
self.interrupt_typing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl eframe::App for App {
|
fn run(&mut self) {
|
||||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
let mut window = RenderWindow::new(
|
||||||
egui::CentralPanel::default()
|
(WIDTH as u32, HEIGHT as u32),
|
||||||
.frame(Frame {
|
"renrs-gui",
|
||||||
fill: Color32::BLACK,
|
Style::CLOSE,
|
||||||
inner_margin: Margin::same(32.0),
|
&Default::default(),
|
||||||
..Default::default()
|
);
|
||||||
})
|
let mut sfegui = SfEgui::new(&window);
|
||||||
.show(ctx, |ui| {
|
window.set_vertical_sync_enabled(true);
|
||||||
if ctx.input(|i|
|
|
||||||
i.key_pressed(Key::Space)
|
const SIZE: f32 = 64.0;
|
||||||
|| i.pointer.button_clicked(PointerButton::Primary)
|
let mut velocity = Vector2f::new(4.0, 4.0);
|
||||||
) {
|
let mut shape = RectangleShape::with_size(Vector2f::new(SIZE, SIZE));
|
||||||
self.handle_interaction(ctx);
|
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(),
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
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 {
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ mkShell {
|
||||||
libXrandr
|
libXrandr
|
||||||
libXi
|
libXi
|
||||||
pkg-config
|
pkg-config
|
||||||
|
sfml
|
||||||
] ++ [
|
] ++ [
|
||||||
#python3
|
#python3
|
||||||
libGL
|
libGL
|
||||||
|
|
Loading…
Add table
Reference in a new issue