main
Elnu 2 years ago
parent 4666368b0a
commit cd01bb6567

@ -1,16 +1,19 @@
use std::{path::PathBuf, sync::{Arc, Mutex}};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};
use eframe::egui; use eframe::egui;
use renrs::State; use renrs::State;
use egui_sfml::SfEgui;
use sfml::{ use sfml::{
graphics::{Color, RenderTarget, RenderWindow, RectangleShape, Transformable}, graphics::{Color, RectangleShape, RenderTarget, RenderWindow, Transformable},
system::Vector2f, system::Vector2f,
window::{Event, mouse, Key, Style}, window::{mouse, Event, Key, Style},
}; };
use egui_sfml::SfEgui;
const WIDTH: f32 = 800.0; const WIDTH: f32 = 800.0;
const HEIGHT: f32 = 600.0; const HEIGHT: f32 = 600.0;
@ -121,8 +124,17 @@ impl App {
while window.is_open() { while window.is_open() {
while let Some(event) = window.poll_event() { while let Some(event) = window.poll_event() {
match event { match event {
Event::Closed | Event::KeyPressed { code: Key::Escape, .. } => return, Event::Closed
Event::MouseButtonPressed { button: mouse::Button::Left, .. } | Event::KeyPressed { code: Key::Space, .. } => self.handle_interaction(), | Event::KeyPressed {
code: Key::Escape, ..
} => return,
Event::MouseButtonPressed {
button: mouse::Button::Left,
..
}
| Event::KeyPressed {
code: Key::Space, ..
} => self.handle_interaction(),
_ => {} _ => {}
} }
} }
@ -137,15 +149,18 @@ impl App {
..Default::default() ..Default::default()
}) })
.show(ctx, |ui| { .show(ctx, |ui| {
if ctx.input(|i| if ctx.input(|i| {
i.key_pressed(Key::Space) i.key_pressed(Key::Space)
|| i.pointer.button_clicked(PointerButton::Primary) || i.pointer.button_clicked(PointerButton::Primary)
) { }) {
self.handle_interaction(); self.handle_interaction();
} }
ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| ui.heading(&self.text[0..*self.chars.lock().unwrap()])); ui.with_layout(egui::Layout::left_to_right(Align::Max), |ui| {
ui.heading(&self.text[0..*self.chars.lock().unwrap()])
}); });
}).unwrap(); });
})
.unwrap();
} }
let Vector2f { x, y } = shape.position(); let Vector2f { x, y } = shape.position();

@ -1,4 +1,4 @@
use std::{path::PathBuf, rc::Rc, cell::RefCell}; use std::{cell::RefCell, path::PathBuf, rc::Rc};
pub mod parser; pub mod parser;
use parser::block::CommandBlock; use parser::block::CommandBlock;

@ -1,7 +1,11 @@
use std::{rc::Rc, cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap, rc::Rc};
use super::{command::{Command, parse_command}, Pair, token::Token, Rule, control::{parse_control, Control}};
use super::{
command::{parse_command, Command},
control::{parse_control, Control},
token::Token,
Pair, Rule,
};
#[derive(Debug)] #[derive(Debug)]
pub struct CommandBlock { pub struct CommandBlock {
@ -29,25 +33,19 @@ impl CommandBlock {
result = Some(command.clone()); result = Some(command.clone());
next += 1; next += 1;
break; break;
}, }
BlockElement::Block(block) => { BlockElement::Block(block) => match block.borrow_mut().next() {
match block.borrow_mut().next() {
Some(command) => { Some(command) => {
result = Some(command.clone()); result = Some(command.clone());
break; break;
}, }
None => { None => {
next += 1; next += 1;
} }
} },
}
}; };
} }
self.next = if count >= next { self.next = if count >= next { Some(next) } else { None };
Some(next)
} else {
None
};
result result
} }
@ -69,7 +67,7 @@ impl CommandBlock {
} else { } else {
variables variables
} }
}, }
None => HashMap::new(), None => HashMap::new(),
} }
} }
@ -93,12 +91,19 @@ enum BlockElement {
Block(Rc<RefCell<CommandBlock>>), Block(Rc<RefCell<CommandBlock>>),
} }
pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, Token>>>>) -> Rc<RefCell<CommandBlock>> { pub fn parse_block(
pair: Pair,
definitions: Option<Rc<RefCell<HashMap<String, Token>>>>,
) -> Rc<RefCell<CommandBlock>> {
//let variables: HashMap<String, Token> = HashMap::new(); //let variables: HashMap<String, Token> = HashMap::new();
let is_root = definitions.is_none(); let is_root = definitions.is_none();
let definitions = definitions.unwrap_or(Rc::new(RefCell::new(HashMap::new()))); let definitions = definitions.unwrap_or(Rc::new(RefCell::new(HashMap::new())));
let block_rc = Rc::new(RefCell::new(CommandBlock { let block_rc = Rc::new(RefCell::new(CommandBlock {
variables: if is_root { Some(definitions.clone()) } else { None }, variables: if is_root {
Some(definitions.clone())
} else {
None
},
..Default::default() ..Default::default()
})); }));
{ {
@ -114,7 +119,7 @@ pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, To
} }
control = Some(parse_control(pair)); control = Some(parse_control(pair));
continue; continue;
}, }
Rule::Line => BlockElement::Command(match parse_command(pair) { Rule::Line => BlockElement::Command(match parse_command(pair) {
Command::Define { variable, value } => { Command::Define { variable, value } => {
let mut value = value; let mut value = value;
@ -137,7 +142,8 @@ pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, To
if let Some(control) = control.as_ref() { if let Some(control) = control.as_ref() {
if control.has_variable_scope() { if control.has_variable_scope() {
// TODO: Sublock-scoped variables // TODO: Sublock-scoped variables
subblock.variables = Some(Rc::new(RefCell::new(HashMap::new()))); subblock.variables =
Some(Rc::new(RefCell::new(HashMap::new())));
} }
} else { } else {
panic!("block should have control"); panic!("block should have control");

@ -1,6 +1,12 @@
use std::{rc::Rc, cell::RefCell}; use std::{cell::RefCell, rc::Rc};
use super::{token::Token, block::CommandBlock, event::Event, Pair, utils::{describe_line, parse_line}}; use super::{
block::CommandBlock,
event::Event,
token::Token,
utils::{describe_line, parse_line},
Pair,
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[allow(dead_code)] #[allow(dead_code)]
@ -13,14 +19,17 @@ pub enum Command {
impl Command { impl Command {
pub fn execute(&self, context: &Rc<RefCell<CommandBlock>>) -> Option<Event> { pub fn execute(&self, context: &Rc<RefCell<CommandBlock>>) -> Option<Event> {
use Command::*; use Command::*;
Some(match self { Some(
match self {
Say { name, text } => Event::Say { Say { name, text } => Event::Say {
name: name.clone(), name: name.clone(),
text: text.clone(), text: text.clone(),
}, },
Define { .. } => panic!("define command should not be executed at runtime!"), Define { .. } => panic!("define command should not be executed at runtime!"),
Eat { .. } => return None, Eat { .. } => return None,
}.process(context)) }
.process(context),
)
} }
} }
@ -42,10 +51,14 @@ pub fn parse_command(pair: Pair) -> Command {
name: Some(name.to_owned()), name: Some(name.to_owned()),
text: text.to_owned(), text: text.to_owned(),
}, },
[Keyword(keyword), Keyword(variable), Keyword(equals), value] if keyword.eq("define") && equals.eq("=") => Define { [Keyword(keyword), Keyword(variable), Keyword(equals), value]
if keyword.eq("define") && equals.eq("=") =>
{
Define {
variable: variable.to_owned(), variable: variable.to_owned(),
value: value.clone(), value: value.clone(),
}, }
}
[Keyword(keyword), Str(food), tail @ ..] if keyword.eq("eat") => Eat { [Keyword(keyword), Str(food), tail @ ..] if keyword.eq("eat") => Eat {
food: food.to_owned(), food: food.to_owned(),
politely: match tail { politely: match tail {

@ -1,4 +1,8 @@
use super::{token::Token, Pair, utils::{describe_line, parse_line}}; use super::{
token::Token,
utils::{describe_line, parse_line},
Pair,
};
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)] #[allow(dead_code)]

@ -1,4 +1,4 @@
use std::{rc::Rc, cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap, rc::Rc};
use regex::Regex; use regex::Regex;
@ -23,7 +23,9 @@ impl Event {
Say { name, text } => { Say { name, text } => {
let context = context.borrow(); let context = context.borrow();
let variables = context.get_variables(); let variables = context.get_variables();
*name = name.as_deref().map(|name| interpolate_string(&name, &variables)); *name = name
.as_deref()
.map(|name| interpolate_string(&name, &variables));
*text = interpolate_string(&text, &variables); *text = interpolate_string(&text, &variables);
} }
} }

@ -1,18 +1,18 @@
pub mod token; pub mod block;
pub mod command; pub mod command;
pub mod control; pub mod control;
pub mod event; pub mod event;
pub mod block; pub mod token;
mod utils; mod utils;
pub type Pair<'a> = pest::iterators::Pair<'a, Rule>; pub type Pair<'a> = pest::iterators::Pair<'a, Rule>;
use std::{path::PathBuf, rc::Rc, cell::RefCell, fs}; use std::{cell::RefCell, fs, path::PathBuf, rc::Rc};
pub use pest::Parser; pub use pest::Parser;
use pest_derive::Parser; use pest_derive::Parser;
use block::{CommandBlock, parse_block}; use block::{parse_block, CommandBlock};
#[derive(Parser)] #[derive(Parser)]
#[grammar = "rpy.pest"] #[grammar = "rpy.pest"]

@ -1,4 +1,4 @@
use super::{Rule, Pair}; use super::{Pair, Rule};
// Raw script tokens // Raw script tokens
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -40,7 +40,8 @@ impl ToString for Token {
Boolean(boolean) => match boolean { Boolean(boolean) => match boolean {
true => "True", true => "True",
false => "False", false => "False",
}.to_owned(), }
.to_owned(),
Number(number) => number.to_string(), Number(number) => number.to_string(),
} }
} }

@ -1,10 +1,10 @@
use super::{Pair, token::{Token, parse_token}}; use super::{
token::{parse_token, Token},
Pair,
};
pub fn parse_line(pair: Pair) -> Vec<Token> { pub fn parse_line(pair: Pair) -> Vec<Token> {
pair pair.into_inner().map(|pair| parse_token(pair)).collect()
.into_inner()
.map(|pair| parse_token(pair))
.collect()
} }
// Line description e.g. [String, Keyword, Array] // Line description e.g. [String, Keyword, Array]

Loading…
Cancel
Save