generated from ElnuDev/rust-project
cargo fmt
This commit is contained in:
parent
4666368b0a
commit
cd01bb6567
9 changed files with 116 additions and 75 deletions
|
@ -1,16 +1,19 @@
|
|||
use std::{path::PathBuf, sync::{Arc, Mutex}};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use eframe::egui;
|
||||
use renrs::State;
|
||||
|
||||
use sfml::{
|
||||
graphics::{Color, RenderTarget, RenderWindow, RectangleShape, Transformable},
|
||||
system::Vector2f,
|
||||
window::{Event, mouse, Key, Style},
|
||||
};
|
||||
use egui_sfml::SfEgui;
|
||||
use sfml::{
|
||||
graphics::{Color, RectangleShape, RenderTarget, RenderWindow, Transformable},
|
||||
system::Vector2f,
|
||||
window::{mouse, Event, Key, Style},
|
||||
};
|
||||
|
||||
const WIDTH: f32 = 800.0;
|
||||
const HEIGHT: f32 = 600.0;
|
||||
|
@ -121,8 +124,17 @@ impl App {
|
|||
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(),
|
||||
Event::Closed
|
||||
| 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()
|
||||
})
|
||||
.show(ctx, |ui| {
|
||||
if ctx.input(|i|
|
||||
if ctx.input(|i| {
|
||||
i.key_pressed(Key::Space)
|
||||
|| i.pointer.button_clicked(PointerButton::Primary)
|
||||
) {
|
||||
|| 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()]));
|
||||
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();
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{path::PathBuf, rc::Rc, cell::RefCell};
|
||||
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
||||
|
||||
pub mod parser;
|
||||
use parser::block::CommandBlock;
|
||||
|
@ -30,4 +30,4 @@ impl State {
|
|||
fn next_command(&mut self) -> Option<Command> {
|
||||
self.script.borrow_mut().next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
use std::{rc::Rc, cell::RefCell, collections::HashMap};
|
||||
|
||||
use super::{command::{Command, parse_command}, Pair, token::Token, Rule, control::{parse_control, Control}};
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
use super::{
|
||||
command::{parse_command, Command},
|
||||
control::{parse_control, Control},
|
||||
token::Token,
|
||||
Pair, Rule,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CommandBlock {
|
||||
|
@ -29,28 +33,22 @@ impl CommandBlock {
|
|||
result = Some(command.clone());
|
||||
next += 1;
|
||||
break;
|
||||
},
|
||||
BlockElement::Block(block) => {
|
||||
match block.borrow_mut().next() {
|
||||
Some(command) => {
|
||||
result = Some(command.clone());
|
||||
break;
|
||||
},
|
||||
None => {
|
||||
next += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
BlockElement::Block(block) => match block.borrow_mut().next() {
|
||||
Some(command) => {
|
||||
result = Some(command.clone());
|
||||
break;
|
||||
}
|
||||
None => {
|
||||
next += 1;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
self.next = if count >= next {
|
||||
Some(next)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.next = if count >= next { Some(next) } else { None };
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
fn evaluate_control(&self) -> bool {
|
||||
use Control::*;
|
||||
self.control.as_ref().map_or(true, |control| match control {
|
||||
|
@ -69,7 +67,7 @@ impl CommandBlock {
|
|||
} else {
|
||||
variables
|
||||
}
|
||||
},
|
||||
}
|
||||
None => HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
@ -93,12 +91,19 @@ enum BlockElement {
|
|||
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 is_root = definitions.is_none();
|
||||
let definitions = definitions.unwrap_or(Rc::new(RefCell::new(HashMap::new())));
|
||||
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()
|
||||
}));
|
||||
{
|
||||
|
@ -114,7 +119,7 @@ pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, To
|
|||
}
|
||||
control = Some(parse_control(pair));
|
||||
continue;
|
||||
},
|
||||
}
|
||||
Rule::Line => BlockElement::Command(match parse_command(pair) {
|
||||
Command::Define { variable, 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 control.has_variable_scope() {
|
||||
// TODO: Sublock-scoped variables
|
||||
subblock.variables = Some(Rc::new(RefCell::new(HashMap::new())));
|
||||
subblock.variables =
|
||||
Some(Rc::new(RefCell::new(HashMap::new())));
|
||||
}
|
||||
} else {
|
||||
panic!("block should have control");
|
||||
|
@ -159,4 +165,4 @@ pub fn parse_block(pair: Pair, definitions: Option<Rc<RefCell<HashMap<String, To
|
|||
}
|
||||
}
|
||||
block_rc
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
#[allow(dead_code)]
|
||||
|
@ -13,14 +19,17 @@ pub enum Command {
|
|||
impl Command {
|
||||
pub fn execute(&self, context: &Rc<RefCell<CommandBlock>>) -> Option<Event> {
|
||||
use Command::*;
|
||||
Some(match self {
|
||||
Say { name, text } => Event::Say {
|
||||
name: name.clone(),
|
||||
text: text.clone(),
|
||||
},
|
||||
Define { .. } => panic!("define command should not be executed at runtime!"),
|
||||
Eat { .. } => return None,
|
||||
}.process(context))
|
||||
Some(
|
||||
match self {
|
||||
Say { name, text } => Event::Say {
|
||||
name: name.clone(),
|
||||
text: text.clone(),
|
||||
},
|
||||
Define { .. } => panic!("define command should not be executed at runtime!"),
|
||||
Eat { .. } => return None,
|
||||
}
|
||||
.process(context),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,10 +51,14 @@ pub fn parse_command(pair: Pair) -> Command {
|
|||
name: Some(name.to_owned()),
|
||||
text: text.to_owned(),
|
||||
},
|
||||
[Keyword(keyword), Keyword(variable), Keyword(equals), value] if keyword.eq("define") && equals.eq("=") => Define {
|
||||
variable: variable.to_owned(),
|
||||
value: value.clone(),
|
||||
},
|
||||
[Keyword(keyword), Keyword(variable), Keyword(equals), value]
|
||||
if keyword.eq("define") && equals.eq("=") =>
|
||||
{
|
||||
Define {
|
||||
variable: variable.to_owned(),
|
||||
value: value.clone(),
|
||||
}
|
||||
}
|
||||
[Keyword(keyword), Str(food), tail @ ..] if keyword.eq("eat") => Eat {
|
||||
food: food.to_owned(),
|
||||
politely: match tail {
|
||||
|
@ -55,4 +68,4 @@ pub fn parse_command(pair: Pair) -> Command {
|
|||
},
|
||||
_ => unknown!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
#[allow(dead_code)]
|
||||
|
@ -29,4 +33,4 @@ pub fn parse_control(pair: Pair) -> Control {
|
|||
},
|
||||
_ => unknown!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{rc::Rc, cell::RefCell, collections::HashMap};
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
|
@ -23,7 +23,9 @@ impl Event {
|
|||
Say { name, text } => {
|
||||
let context = context.borrow();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -43,4 +45,4 @@ fn interpolate_string(input: &str, variables: &HashMap<String, Token>) -> String
|
|||
});
|
||||
|
||||
interpolated_string.into_owned()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
pub mod token;
|
||||
pub mod block;
|
||||
pub mod command;
|
||||
pub mod control;
|
||||
pub mod event;
|
||||
pub mod block;
|
||||
pub mod token;
|
||||
mod utils;
|
||||
|
||||
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;
|
||||
use pest_derive::Parser;
|
||||
|
||||
use block::{CommandBlock, parse_block};
|
||||
use block::{parse_block, CommandBlock};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "rpy.pest"]
|
||||
|
@ -30,4 +30,4 @@ pub fn parse(script: &str) -> Rc<RefCell<CommandBlock>> {
|
|||
.next()
|
||||
.unwrap();
|
||||
parse_block(file, None)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::{Rule, Pair};
|
||||
use super::{Pair, Rule};
|
||||
|
||||
// Raw script tokens
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -40,7 +40,8 @@ impl ToString for Token {
|
|||
Boolean(boolean) => match boolean {
|
||||
true => "True",
|
||||
false => "False",
|
||||
}.to_owned(),
|
||||
}
|
||||
.to_owned(),
|
||||
Number(number) => number.to_string(),
|
||||
}
|
||||
}
|
||||
|
@ -79,4 +80,4 @@ pub fn parse_token(pair: Pair) -> Token {
|
|||
Rule::Keyword => Token::Keyword(pair.as_str().to_owned()),
|
||||
__ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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> {
|
||||
pair
|
||||
.into_inner()
|
||||
.map(|pair| parse_token(pair))
|
||||
.collect()
|
||||
pair.into_inner().map(|pair| parse_token(pair)).collect()
|
||||
}
|
||||
|
||||
// Line description e.g. [String, Keyword, Array]
|
||||
|
@ -18,4 +18,4 @@ pub fn describe_line(line: &[Token]) -> String {
|
|||
}
|
||||
description.push_str("]");
|
||||
description
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue