This commit is contained in:
Sarah Jamie Lewis 2020-07-04 15:06:15 -07:00
parent 8c5afb7b49
commit 84d15cee9a
1 changed files with 43 additions and 48 deletions

View File

@ -13,7 +13,6 @@ use std::borrow::Borrow;
#[grammar = "../pest/qml.pest"] #[grammar = "../pest/qml.pest"]
struct QmlParser; struct QmlParser;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Value { enum Value {
QmlString(String), QmlString(String),
@ -76,15 +75,16 @@ fn parse_qml(qml: Pairs<Rule>) -> QML {
} }
_ => {} _ => {}
} }
}, }
Rule::function => { Rule::function => {
let mut tokens = pair.into_inner(); let mut tokens = pair.into_inner();
let ident = tokens.next().unwrap().as_str(); let ident = tokens.next().unwrap().as_str();
let value = tokens.concat(); let value = tokens.concat();
qmldoc qmldoc.properties.insert(
.properties String::from(ident),
.insert(String::from(ident), QmlString(String::from(value.clone().trim()))); QmlString(String::from(value.clone().trim())),
} );
}
_ => return parse_qml(pair.into_inner()), _ => return parse_qml(pair.into_inner()),
} }
} }
@ -99,17 +99,11 @@ fn main() {
let qml_doc = parse_qml(qml_tokens); let qml_doc = parse_qml(qml_tokens);
let top_level = qml_doc.children.clone(); let top_level = qml_doc.children.clone();
let width = match top_level[0].1 let width = match top_level[0].1.properties.get("width") {
.properties
.get("width")
{
Some(QmlNumber(num)) => num.clone(), Some(QmlNumber(num)) => num.clone(),
_ => 600.0, _ => 600.0,
}; };
let height = match top_level[0].1 let height = match top_level[0].1.properties.get("height") {
.properties
.get("height")
{
Some(QmlNumber(num)) => num.clone(), Some(QmlNumber(num)) => num.clone(),
_ => 600.0, _ => 600.0,
}; };
@ -119,7 +113,7 @@ fn main() {
.position((100.0, 100.0)) .position((100.0, 100.0))
.resizeable(true) .resizeable(true)
.size(width, height) .size(width, height)
.child(render_ctx(ctx, &top_level, 0,0,&HashMap::new()).unwrap()) .child(render_ctx(ctx, &top_level, 0, 0, &HashMap::new()).unwrap())
.build(ctx) .build(ctx)
}) })
.run(); .run();
@ -128,17 +122,21 @@ fn main() {
fn parse_number(val: Option<&Value>, env: &HashMap<String, Value>) -> f64 { fn parse_number(val: Option<&Value>, env: &HashMap<String, Value>) -> f64 {
match val { match val {
Some(QmlNumber(num)) => num.clone(), Some(QmlNumber(num)) => num.clone(),
Some(QmlIdent(ident)) => { Some(QmlIdent(ident)) => match env.get(ident) {
match env.get(ident) { Some(QmlNumber(num)) => num.clone(),
Some(QmlNumber(num)) => num.clone(), _ => 0.0,
_ => 0.0 },
}
}
_ => 0.0, _ => 0.0,
} }
} }
fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u32, env: &HashMap<String, Value>) -> Option<Entity> { fn render_ctx(
ctx: &mut BuildContext,
qml: &Vec<(String, QML)>,
row: u32,
col: u32,
env: &HashMap<String, Value>,
) -> Option<Entity> {
for (ident, child) in qml.iter() { for (ident, child) in qml.iter() {
match ident.as_str() { match ident.as_str() {
"Grid" => { "Grid" => {
@ -160,27 +158,25 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u
} }
grid = grid.columns(grid_cols.build()); grid = grid.columns(grid_cols.build());
let mut grow = 0u32; let mut grow = 0u32;
let mut gcol = 0u32; let mut gcol = 0u32;
//rect = rect.attach(Grid::column(gcol as usize)); //rect = rect.attach(Grid::column(gcol as usize));
for (i,s) in child.children.iter() { for (i, s) in child.children.iter() {
match render_ctx(ctx, &vec![(i.clone(), s.clone())], grow, gcol, &env) {
match render_ctx(ctx, &vec![(i.clone(),s.clone())],grow,gcol, &env) {
Some(entity) => { Some(entity) => {
grid = grid.child(entity); grid = grid.child(entity);
grow+=1; grow += 1;
if grow as u32 == rows { if grow as u32 == rows {
grow = 0; grow = 0;
gcol+=1; gcol += 1;
} }
}, }
_ => {} _ => {}
} }
} }
return Some(grid.build(ctx)); return Some(grid.build(ctx));
}, }
"Rectangle" => { "Rectangle" => {
let width = parse_number(child.properties.get("width"), &env); let width = parse_number(child.properties.get("width"), &env);
let height = parse_number(child.properties.get("height"), &env); let height = parse_number(child.properties.get("height"), &env);
@ -194,21 +190,22 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u
_ => Color::rgb(0xff, 0xff, 0xff), _ => Color::rgb(0xff, 0xff, 0xff),
}; };
let mut rect = Container::create() let mut rect = Container::create()
// .width(width) // .width(width)
// .height(height) // .height(height)
.background(color); .background(color);
rect = rect.attach(Grid::row(row as usize)); rect = rect.attach(Grid::row(row as usize));
rect = rect.attach(Grid::column(col as usize)); rect = rect.attach(Grid::column(col as usize));
let mut env = env.clone(); let mut env = env.clone();
env.insert(String::from("parent.width"), QmlNumber(width)); env.insert(String::from("parent.width"), QmlNumber(width));
env.insert(String::from("parent.height"), QmlNumber(height)); env.insert(String::from("parent.height"), QmlNumber(height));
for (i,s) in child.children.iter() { for (i, s) in child.children.iter() {
match render_ctx(ctx, &vec![(i.clone(),s.clone())],0,0, &env) { match render_ctx(ctx, &vec![(i.clone(), s.clone())], 0, 0, &env) {
Some(entity) => { rect = rect.child(entity); }, Some(entity) => {
rect = rect.child(entity);
}
_ => {} _ => {}
} }
} }
@ -222,14 +219,12 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u
let code = match child.properties.get("onclick").unwrap() { let code = match child.properties.get("onclick").unwrap() {
QmlString(code) => code.clone(), QmlString(code) => code.clone(),
_ => String::new() _ => String::new(),
}; };
button = button.on_click(move button = button.on_click(move |x, y| -> bool {
|x,y| -> bool {
rhai_script(code.clone()); rhai_script(code.clone());
return true; return true;
} });
);
let text = match child.properties.get("text").unwrap() { let text = match child.properties.get("text").unwrap() {
QmlString(text) => text.clone(), QmlString(text) => text.clone(),
@ -261,7 +256,7 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u
tt = tt.attach(Grid::column(col as usize)); tt = tt.attach(Grid::column(col as usize));
match child.properties.get("anchors.centerIn") { match child.properties.get("anchors.centerIn") {
Some(QmlIdent(str)) => { Some(QmlIdent(str)) => {
if str.eq("parent") { if str.eq("parent") {
tt = tt.vertical_alignment(Alignment::Center); tt = tt.vertical_alignment(Alignment::Center);
tt = tt.horizontal_alignment(Alignment::Center); tt = tt.horizontal_alignment(Alignment::Center);
@ -281,4 +276,4 @@ fn render_ctx(ctx: &mut BuildContext, qml: &Vec<(String, QML)>, row: u32, col: u
fn rhai_script(run: String) { fn rhai_script(run: String) {
let mut engine = Engine::new(); let mut engine = Engine::new();
engine.eval::<()>(run.as_str()); engine.eval::<()>(run.as_str());
} }