Basic renrs-gui implementation

main
Elnu 1 year ago
parent badd78db79
commit 20a8bf41b7

@ -1,6 +1,7 @@
{
"rust-analyzer.linkedProjects": [
"./renrs/Cargo.toml",
"./renrs-gui/Cargo.toml",
"./demo/Cargo.toml"
],
"files.exclude": {

205
Cargo.lock generated

@ -0,0 +1,205 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cpufeatures"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "pest"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e68e84bfb01f0507134eac1e9b410a12ba379d064eab48c50ba4ce329a527b70"
dependencies = [
"thiserror",
"ucd-trie",
]
[[package]]
name = "pest_derive"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b79d4c71c865a25a4322296122e3924d30bc8ee0834c8bfc8b95f7f054afbfb"
dependencies = [
"pest",
"pest_generator",
]
[[package]]
name = "pest_generator"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c435bf1076437b851ebc8edc3a18442796b30f1728ffea6262d59bbe28b077e"
dependencies = [
"pest",
"pest_meta",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pest_meta"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "745a452f8eb71e39ffd8ee32b3c5f51d03845f99786fa9b68db6ff509c505411"
dependencies = [
"once_cell",
"pest",
"sha2",
]
[[package]]
name = "proc-macro2"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500"
dependencies = [
"proc-macro2",
]
[[package]]
name = "renrs"
version = "0.1.0"
dependencies = [
"pest",
"pest_derive",
]
[[package]]
name = "sha2"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "syn"
version = "2.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "typenum"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "ucd-trie"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81"
[[package]]
name = "unicode-ident"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

2775
demo/Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
renrs = { path = "../renrs" }
renrs-gui = { path = "../renrs-gui" }

@ -1,6 +1,3 @@
fn main() {
let commands = renrs::parse_file("demo.rpy");
for command in commands {
println!("{:?}", command);
}
renrs_gui::run("demo.rpy".into()).unwrap();
}

@ -0,0 +1,2 @@
/target
Cargo.lock

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

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

@ -1,4 +1,4 @@
use std::fs;
use std::{fs, path::PathBuf};
use pest::Parser;
use pest_derive::Parser;
@ -46,6 +46,7 @@ fn tokenize(script: &str) -> Vec<Vec<Token>> {
let file = RpyParser::parse(Rule::file, script)
.expect("unsuccessful parse")
.next().unwrap();
// TODO: Init with capacity
let mut lines = Vec::new();
for line in file.into_inner() {
let mut tokens = Vec::new();
@ -97,7 +98,7 @@ fn parse_pair(pair: pest::iterators::Pair<Rule>) -> Token {
}
// Tokenize file
fn tokenize_file(file_path: &str) -> Vec<Vec<Token>> {
fn tokenize_file(file_path: PathBuf) -> Vec<Vec<Token>> {
let unparsed_file = fs::read_to_string(file_path).expect("cannot find file");
tokenize(&unparsed_file)
}
@ -114,7 +115,7 @@ fn describe_line(line: &Vec<Token>) -> String {
}
// Parse file into commands
pub fn parse_file(file_path: &str) -> Vec<Command> {
fn parse_file(file_path: PathBuf) -> Vec<Command> {
let token_lines = tokenize_file(file_path);
let mut commands = Vec::new();
for line in token_lines {
@ -135,3 +136,27 @@ pub fn parse_file(file_path: &str) -> Vec<Command> {
}
commands
}
pub struct State {
command_queue: Vec<Command>,
}
impl State {
pub fn from_file(file: PathBuf) -> State {
State {
command_queue: {
let mut q = parse_file(file);
q.reverse();
q
},
}
}
pub fn next_command(&mut self) -> Option<Command> {
if self.command_queue.len() == 0 {
None
} else {
Some(self.command_queue.remove(self.command_queue.len() - 1))
}
}
}

@ -9,13 +9,33 @@ let
"rust-src"
];
});
in
pkgs.mkShell {
buildInputs = with pkgs; [
in with pkgs;
mkShell {
buildInputs = [
ruststable
rust-analyzer
bacon
#pkg-config
#openssl
];
# Shell configuration for egui/eframe
# Based on https://gist.github.com/TheSirC/8fc34ba9143c8fe4f0b1962f4d7225df
# Will fail to find library otherwise
nativeBuildInputs = with xorg; [
libxcb
libXcursor
libXrandr
libXi
pkg-config
] ++ [
#python3
libGL
libGLU
];
shellHook = ''
export LD_LIBRARY_PATH=/run/opengl-driver/lib/:${lib.makeLibraryPath ([libGL libGLU])}
'';
}

Loading…
Cancel
Save