parser refactoring

This commit is contained in:
Aleksander
2025-06-28 17:39:53 +02:00
parent 247c51c3b7
commit 2420e8007c
12 changed files with 252 additions and 219 deletions

View File

@@ -1,5 +1,5 @@
[profile.dev] [profile.dev]
opt-level = 0 opt-level = 1
debug = true debug = true
strip = "none" strip = "none"
debug-assertions = true debug-assertions = true

View File

@@ -0,0 +1,14 @@
<layout>
<elements>
<rectangle
color="#AAAAAA"
width="500" height="500" min_width="500" min_height="500"
gap="4" flex_direction="column">
<label text="aaa" color="#FFFFFF" />
<button text="Red button" width="128" height="32" color="#FF0000" />
<button text="Aqua button" width="128" height="32" color="#00FFFF" />
<button text="Yellow button" width="128" height="32" color="#FFFF00" />
</rectangle>
</elements>
</layout>

View File

@@ -1,93 +1,24 @@
use std::{cell::RefCell, rc::Rc};
use glam::{Mat4, Vec2};
use wgui::{
drawing::{self},
event::{EventListenerCollection, EventListenerKind},
layout::{Layout, WidgetID},
renderer_vk::text::TextStyle,
};
use crate::{assets, testbed::Testbed}; use crate::{assets, testbed::Testbed};
use glam::Vec2;
use wgui::{event::EventListenerCollection, layout::Layout};
pub struct TestbedGeneric { pub struct TestbedGeneric {
pub layout: Layout, pub layout: Layout,
rot: f32,
widget_id: Rc<RefCell<Option<WidgetID>>>,
} }
impl TestbedGeneric { impl TestbedGeneric {
pub fn new(listeners: &mut EventListenerCollection<(), ()>) -> anyhow::Result<Self> { pub fn new(listeners: &mut EventListenerCollection<(), ()>) -> anyhow::Result<Self> {
const XML_PATH: &str = "gui/testbed.xml"; const XML_PATH: &str = "gui/testbed.xml";
let (mut layout, res) = let (layout, _res) =
wgui::parser::new_layout_from_assets(Box::new(assets::Asset {}), XML_PATH)?; wgui::parser::new_layout_from_assets(Box::new(assets::Asset {}), XML_PATH)?;
use wgui::components::button; Ok(Self { layout })
let my_div_parent = res.require_by_id("my_div_parent")?;
// create some buttons for testing
for i in 0..4 {
let n = i as f32 / 4.0;
button::construct(
&mut layout,
my_div_parent,
button::Params {
text: "I'm a button!",
color: drawing::Color::new(1.0 - n, n * n, n, 1.0),
..Default::default()
},
)?;
}
let button = button::construct(
&mut layout,
my_div_parent,
button::Params {
text: "Click me!!",
color: drawing::Color::new(0.2, 0.2, 0.2, 1.0),
size: Vec2::new(256.0, 64.0),
text_style: TextStyle {
size: Some(30.0),
..Default::default()
},
},
)?;
let widget_id = Rc::new(RefCell::new(None));
let wid = widget_id.clone();
listeners.add(
button.body,
EventListenerKind::MouseRelease,
Box::new(move |data, (), ()| {
button.set_text(data, "Congratulations!");
*wid.borrow_mut() = Some(data.widget_id);
}),
);
Ok(Self {
layout,
rot: 0.0,
widget_id,
})
} }
} }
impl Testbed for TestbedGeneric { impl Testbed for TestbedGeneric {
fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()> { fn update(&mut self, width: f32, height: f32, timestep_alpha: f32) -> anyhow::Result<()> {
if let Some(widget_id) = *self.widget_id.borrow() {
self.rot += 0.01;
let a = self.layout.widget_map.get(widget_id).unwrap();
let mut widget = a.lock().unwrap();
widget.data.transform = Mat4::IDENTITY
* Mat4::from_rotation_y(-self.rot)
* Mat4::from_rotation_x(self.rot * 0.25)
* Mat4::from_rotation_z(-self.rot * 0.1);
self.layout.needs_redraw = true;
}
self.layout self.layout
.update(Vec2::new(width, height), timestep_alpha)?; .update(Vec2::new(width, height), timestep_alpha)?;
Ok(()) Ok(())

View File

@@ -1,10 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use glam::Vec2;
use taffy::{AlignItems, JustifyContent, prelude::length}; use taffy::{AlignItems, JustifyContent, prelude::length};
use crate::{ use crate::{
animation::{Animation, AnimationEasing}, animation::{Animation, AnimationEasing},
components::Component,
drawing::{self, Color}, drawing::{self, Color},
event::WidgetCallback, event::WidgetCallback,
layout::{Layout, WidgetID}, layout::{Layout, WidgetID},
@@ -19,7 +18,9 @@ use crate::{
pub struct Params<'a> { pub struct Params<'a> {
pub text: &'a str, pub text: &'a str,
pub color: drawing::Color, pub color: drawing::Color,
pub size: Vec2, pub border_color: drawing::Color,
pub round: WLength,
pub style: taffy::Style,
pub text_style: TextStyle, pub text_style: TextStyle,
} }
@@ -28,19 +29,23 @@ impl Default for Params<'_> {
Self { Self {
text: "Text", text: "Text",
color: drawing::Color::new(1.0, 1.0, 1.0, 1.0), color: drawing::Color::new(1.0, 1.0, 1.0, 1.0),
size: Vec2::new(128.0, 32.0), border_color: drawing::Color::new(0.0, 0.0, 0.0, 1.0),
round: WLength::Units(4.0),
style: Default::default(),
text_style: TextStyle::default(), text_style: TextStyle::default(),
} }
} }
} }
pub struct Button { pub struct Button {
color: drawing::Color, pub color: drawing::Color,
pub body: WidgetID, // Rectangle pub body: WidgetID, // Rectangle
pub text_id: WidgetID, // Text pub text_id: WidgetID, // Text
text_node: taffy::NodeId, text_node: taffy::NodeId,
} }
impl Component for Button {}
impl Button { impl Button {
pub fn set_text<'a, C>(&self, callback_data: &mut C, text: &str) pub fn set_text<'a, C>(&self, callback_data: &mut C, text: &str)
where where
@@ -95,23 +100,23 @@ pub fn construct(
parent: WidgetID, parent: WidgetID,
params: Params, params: Params,
) -> anyhow::Result<Arc<Button>> { ) -> anyhow::Result<Arc<Button>> {
let mut style = params.style;
// force-override style
style.align_items = Some(AlignItems::Center);
style.justify_content = Some(JustifyContent::Center);
style.padding = length(1.0);
let (rect_id, _) = layout.add_child( let (rect_id, _) = layout.add_child(
parent, parent,
Rectangle::create(RectangleParams { Rectangle::create(RectangleParams {
color: params.color, color: params.color,
round: WLength::Units(4.0), round: params.round,
border_color: params.border_color,
border: 2.0,
..Default::default() ..Default::default()
})?, })?,
taffy::Style { style,
size: taffy::Size {
width: length(params.size.x),
height: length(params.size.y),
},
align_items: Some(AlignItems::Center),
justify_content: Some(JustifyContent::Center),
padding: length(1.0),
..Default::default()
},
)?; )?;
let light_text = (params.color.r + params.color.g + params.color.b) < 1.5; let light_text = (params.color.r + params.color.g + params.color.b) < 1.5;

View File

@@ -1 +1,5 @@
use crate::any::AnyTrait;
pub mod button; pub mod button;
pub trait Component: AnyTrait {}

View File

@@ -0,0 +1,60 @@
use crate::{
components::button,
drawing::Color,
layout::WidgetID,
parser::{
ParserContext, ParserFile, iter_attribs,
style::{parse_color, parse_round, parse_style, parse_text_style},
},
widget::util::WLength,
};
pub fn parse_component_button<'a>(
file: &'a ParserFile,
ctx: &mut ParserContext,
node: roxmltree::Node<'a, 'a>,
parent_id: WidgetID,
) -> anyhow::Result<()> {
let mut color = Color::new(1.0, 1.0, 1.0, 1.0);
let mut border_color = Color::new(0.0, 0.0, 0.0, 1.0);
let mut round = WLength::Units(4.0);
let mut text = String::default();
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
let text_style = parse_text_style(&attribs);
let style = parse_style(&attribs);
for (key, value) in attribs {
match key.as_ref() {
"text" => {
text = String::from(value.as_ref());
}
"round" => {
parse_round(&value, &mut round);
}
"color" => {
parse_color(&value, &mut color);
}
"border_color" => {
parse_color(&value, &mut border_color);
}
_ => {}
}
}
let button = button::construct(
ctx.layout,
parent_id,
button::Params {
color,
border_color,
text: &text,
style,
text_style,
round,
},
)?;
Ok(())
}

View File

@@ -1,3 +1,4 @@
mod component_button;
mod style; mod style;
mod widget_div; mod widget_div;
mod widget_label; mod widget_label;
@@ -9,8 +10,9 @@ use crate::{
drawing::{self}, drawing::{self},
layout::{Layout, WidgetID}, layout::{Layout, WidgetID},
parser::{ parser::{
widget_div::parse_widget_div, widget_label::parse_widget_label, component_button::parse_component_button, widget_div::parse_widget_div,
widget_rectangle::parse_widget_rectangle, widget_sprite::parse_widget_sprite, widget_label::parse_widget_label, widget_rectangle::parse_widget_rectangle,
widget_sprite::parse_widget_sprite,
}, },
}; };
use ouroboros::self_referencing; use ouroboros::self_referencing;
@@ -543,6 +545,9 @@ fn parse_children<'a>(
"sprite" => { "sprite" => {
parse_widget_sprite(file, ctx, child_node, parent_id)?; parse_widget_sprite(file, ctx, child_node, parent_id)?;
} }
"button" => {
parse_component_button(file, ctx, child_node, parent_id)?;
}
"" => { /* ignore */ } "" => { /* ignore */ }
other_tag_name => { other_tag_name => {
parse_widget_other(other_tag_name, file, ctx, child_node, parent_id)?; parse_widget_other(other_tag_name, file, ctx, child_node, parent_id)?;

View File

@@ -1,75 +1,140 @@
use std::rc::Rc;
use taffy::{ use taffy::{
AlignContent, AlignItems, AlignSelf, BoxSizing, Display, FlexDirection, FlexWrap, JustifyContent, AlignContent, AlignItems, AlignSelf, BoxSizing, Display, FlexDirection, FlexWrap, JustifyContent,
JustifySelf, Overflow, JustifySelf, Overflow,
}; };
use crate::parser::{ use crate::{
ParserContext, ParserFile, iter_attribs, parse_size_unit, parse_val, print_invalid_attrib, drawing,
parser::{
is_percent, parse_color_hex, parse_f32, parse_percent, parse_size_unit, parse_val,
print_invalid_attrib, print_invalid_value,
},
renderer_vk::text::{FontWeight, HorizontalAlign, TextStyle},
widget::util::WLength,
}; };
pub fn style_from_node<'a>( pub fn parse_round(value: &str, round: &mut WLength) {
file: &'a ParserFile, if is_percent(value) {
ctx: &ParserContext, if let Some(val) = parse_percent(value) {
node: roxmltree::Node<'a, 'a>, *round = WLength::Percent(val);
) -> taffy::Style { } else {
print_invalid_value(value);
}
} else if let Some(val) = parse_f32(value) {
*round = WLength::Units(val);
} else {
print_invalid_value(value);
}
}
pub fn parse_color(value: &str, color: &mut drawing::Color) {
if let Some(res_color) = parse_color_hex(value) {
*color = res_color;
} else {
print_invalid_value(value);
}
}
pub fn parse_text_style(attribs: &[(Rc<str>, Rc<str>)]) -> TextStyle {
let mut style = TextStyle::default();
for (key, value) in attribs {
match key.as_ref() {
"color" => {
if let Some(color) = parse_color_hex(value) {
style.color = Some(color);
}
}
"align" => match value.as_ref() {
"left" => style.align = Some(HorizontalAlign::Left),
"right" => style.align = Some(HorizontalAlign::Right),
"center" => style.align = Some(HorizontalAlign::Center),
"justified" => style.align = Some(HorizontalAlign::Justified),
"end" => style.align = Some(HorizontalAlign::End),
_ => {
print_invalid_attrib(key, value);
}
},
"weight" => match value.as_ref() {
"normal" => style.weight = Some(FontWeight::Normal),
"bold" => style.weight = Some(FontWeight::Bold),
_ => {
print_invalid_attrib(key, value);
}
},
"size" => {
if let Ok(size) = value.parse::<f32>() {
style.size = Some(size);
} else {
print_invalid_attrib(key, value);
}
}
_ => {}
}
}
style
}
pub fn parse_style(attribs: &[(Rc<str>, Rc<str>)]) -> taffy::Style {
let mut style = taffy::Style { let mut style = taffy::Style {
..Default::default() ..Default::default()
}; };
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
for (key, value) in attribs { for (key, value) in attribs {
match &*key { match key.as_ref() {
"display" => match &*value { "display" => match value.as_ref() {
"flex" => style.display = Display::Flex, "flex" => style.display = Display::Flex,
"block" => style.display = Display::Block, "block" => style.display = Display::Block,
"grid" => style.display = Display::Grid, "grid" => style.display = Display::Grid,
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"margin_left" => { "margin_left" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.margin.left = dim; style.margin.left = dim;
} }
} }
"margin_right" => { "margin_right" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.margin.right = dim; style.margin.right = dim;
} }
} }
"margin_top" => { "margin_top" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.margin.top = dim; style.margin.top = dim;
} }
} }
"margin_bottom" => { "margin_bottom" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.margin.bottom = dim; style.margin.bottom = dim;
} }
} }
"padding_left" => { "padding_left" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.padding.left = dim; style.padding.left = dim;
} }
} }
"padding_right" => { "padding_right" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.padding.right = dim; style.padding.right = dim;
} }
} }
"padding_top" => { "padding_top" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.padding.top = dim; style.padding.top = dim;
} }
} }
"padding_bottom" => { "padding_bottom" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.padding.bottom = dim; style.padding.bottom = dim;
} }
} }
"margin" => { "margin" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.margin.left = dim; style.margin.left = dim;
style.margin.right = dim; style.margin.right = dim;
style.margin.top = dim; style.margin.top = dim;
@@ -77,14 +142,14 @@ pub fn style_from_node<'a>(
} }
} }
"padding" => { "padding" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.padding.left = dim; style.padding.left = dim;
style.padding.right = dim; style.padding.right = dim;
style.padding.top = dim; style.padding.top = dim;
style.padding.bottom = dim; style.padding.bottom = dim;
} }
} }
"overflow" => match &*value { "overflow" => match value.as_ref() {
"hidden" => { "hidden" => {
style.overflow.x = Overflow::Hidden; style.overflow.x = Overflow::Hidden;
style.overflow.y = Overflow::Hidden; style.overflow.y = Overflow::Hidden;
@@ -102,92 +167,92 @@ pub fn style_from_node<'a>(
style.overflow.y = Overflow::Scroll; style.overflow.y = Overflow::Scroll;
} }
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"overflow_x" => match &*value { "overflow_x" => match value.as_ref() {
"hidden" => style.overflow.x = Overflow::Hidden, "hidden" => style.overflow.x = Overflow::Hidden,
"visible" => style.overflow.x = Overflow::Visible, "visible" => style.overflow.x = Overflow::Visible,
"clip" => style.overflow.x = Overflow::Clip, "clip" => style.overflow.x = Overflow::Clip,
"scroll" => style.overflow.x = Overflow::Scroll, "scroll" => style.overflow.x = Overflow::Scroll,
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"overflow_y" => match &*value { "overflow_y" => match value.as_ref() {
"hidden" => style.overflow.y = Overflow::Hidden, "hidden" => style.overflow.y = Overflow::Hidden,
"visible" => style.overflow.y = Overflow::Visible, "visible" => style.overflow.y = Overflow::Visible,
"clip" => style.overflow.y = Overflow::Clip, "clip" => style.overflow.y = Overflow::Clip,
"scroll" => style.overflow.y = Overflow::Scroll, "scroll" => style.overflow.y = Overflow::Scroll,
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"min_width" => { "min_width" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.min_size.width = dim; style.min_size.width = dim;
} }
} }
"min_height" => { "min_height" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.min_size.height = dim; style.min_size.height = dim;
} }
} }
"max_width" => { "max_width" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.max_size.width = dim; style.max_size.width = dim;
} }
} }
"max_height" => { "max_height" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.max_size.height = dim; style.max_size.height = dim;
} }
} }
"width" => { "width" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.size.width = dim; style.size.width = dim;
} }
} }
"height" => { "height" => {
if let Some(dim) = parse_size_unit(&value) { if let Some(dim) = parse_size_unit(value) {
style.size.height = dim; style.size.height = dim;
} }
} }
"gap" => { "gap" => {
if let Some(val) = parse_size_unit(&value) { if let Some(val) = parse_size_unit(value) {
style.gap = val; style.gap = val;
} }
} }
"flex_basis" => { "flex_basis" => {
if let Some(val) = parse_size_unit(&value) { if let Some(val) = parse_size_unit(value) {
style.flex_basis = val; style.flex_basis = val;
} }
} }
"flex_grow" => { "flex_grow" => {
if let Some(val) = parse_val(&value) { if let Some(val) = parse_val(value) {
style.flex_grow = val; style.flex_grow = val;
} }
} }
"flex_shrink" => { "flex_shrink" => {
if let Some(val) = parse_val(&value) { if let Some(val) = parse_val(value) {
style.flex_shrink = val; style.flex_shrink = val;
} }
} }
"position" => match &*value { "position" => match value.as_ref() {
"absolute" => style.position = taffy::Position::Absolute, "absolute" => style.position = taffy::Position::Absolute,
"relative" => style.position = taffy::Position::Relative, "relative" => style.position = taffy::Position::Relative,
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"box_sizing" => match &*value { "box_sizing" => match value.as_ref() {
"border_box" => style.box_sizing = BoxSizing::BorderBox, "border_box" => style.box_sizing = BoxSizing::BorderBox,
"content_box" => style.box_sizing = BoxSizing::ContentBox, "content_box" => style.box_sizing = BoxSizing::ContentBox,
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"align_self" => match &*value { "align_self" => match value.as_ref() {
"baseline" => style.align_self = Some(AlignSelf::Baseline), "baseline" => style.align_self = Some(AlignSelf::Baseline),
"center" => style.align_self = Some(AlignSelf::Center), "center" => style.align_self = Some(AlignSelf::Center),
"end" => style.align_self = Some(AlignSelf::End), "end" => style.align_self = Some(AlignSelf::End),
@@ -196,10 +261,10 @@ pub fn style_from_node<'a>(
"start" => style.align_self = Some(AlignSelf::Start), "start" => style.align_self = Some(AlignSelf::Start),
"stretch" => style.align_self = Some(AlignSelf::Stretch), "stretch" => style.align_self = Some(AlignSelf::Stretch),
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"justify_self" => match &*value { "justify_self" => match value.as_ref() {
"center" => style.justify_self = Some(JustifySelf::Center), "center" => style.justify_self = Some(JustifySelf::Center),
"end" => style.justify_self = Some(JustifySelf::End), "end" => style.justify_self = Some(JustifySelf::End),
"flex_end" => style.justify_self = Some(JustifySelf::FlexEnd), "flex_end" => style.justify_self = Some(JustifySelf::FlexEnd),
@@ -207,10 +272,10 @@ pub fn style_from_node<'a>(
"start" => style.justify_self = Some(JustifySelf::Start), "start" => style.justify_self = Some(JustifySelf::Start),
"stretch" => style.justify_self = Some(JustifySelf::Stretch), "stretch" => style.justify_self = Some(JustifySelf::Stretch),
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"align_items" => match &*value { "align_items" => match value.as_ref() {
"baseline" => style.align_items = Some(AlignItems::Baseline), "baseline" => style.align_items = Some(AlignItems::Baseline),
"center" => style.align_items = Some(AlignItems::Center), "center" => style.align_items = Some(AlignItems::Center),
"end" => style.align_items = Some(AlignItems::End), "end" => style.align_items = Some(AlignItems::End),
@@ -219,10 +284,10 @@ pub fn style_from_node<'a>(
"start" => style.align_items = Some(AlignItems::Start), "start" => style.align_items = Some(AlignItems::Start),
"stretch" => style.align_items = Some(AlignItems::Stretch), "stretch" => style.align_items = Some(AlignItems::Stretch),
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"align_content" => match &*value { "align_content" => match value.as_ref() {
"center" => style.align_content = Some(AlignContent::Center), "center" => style.align_content = Some(AlignContent::Center),
"end" => style.align_content = Some(AlignContent::End), "end" => style.align_content = Some(AlignContent::End),
"flex_end" => style.align_content = Some(AlignContent::FlexEnd), "flex_end" => style.align_content = Some(AlignContent::FlexEnd),
@@ -233,10 +298,10 @@ pub fn style_from_node<'a>(
"start" => style.align_content = Some(AlignContent::Start), "start" => style.align_content = Some(AlignContent::Start),
"stretch" => style.align_content = Some(AlignContent::Stretch), "stretch" => style.align_content = Some(AlignContent::Stretch),
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"justify_content" => match &*value { "justify_content" => match value.as_ref() {
"center" => style.justify_content = Some(JustifyContent::Center), "center" => style.justify_content = Some(JustifyContent::Center),
"end" => style.justify_content = Some(JustifyContent::End), "end" => style.justify_content = Some(JustifyContent::End),
"flex_end" => style.justify_content = Some(JustifyContent::FlexEnd), "flex_end" => style.justify_content = Some(JustifyContent::FlexEnd),
@@ -247,22 +312,22 @@ pub fn style_from_node<'a>(
"start" => style.justify_content = Some(JustifyContent::Start), "start" => style.justify_content = Some(JustifyContent::Start),
"stretch" => style.justify_content = Some(JustifyContent::Stretch), "stretch" => style.justify_content = Some(JustifyContent::Stretch),
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
"flex_wrap" => match &*value { "flex_wrap" => match value.as_ref() {
"wrap" => style.flex_wrap = FlexWrap::Wrap, "wrap" => style.flex_wrap = FlexWrap::Wrap,
"no_wrap" => style.flex_wrap = FlexWrap::NoWrap, "no_wrap" => style.flex_wrap = FlexWrap::NoWrap,
"wrap_reverse" => style.flex_wrap = FlexWrap::WrapReverse, "wrap_reverse" => style.flex_wrap = FlexWrap::WrapReverse,
_ => {} _ => {}
}, },
"flex_direction" => match &*value { "flex_direction" => match value.as_ref() {
"column_reverse" => style.flex_direction = FlexDirection::ColumnReverse, "column_reverse" => style.flex_direction = FlexDirection::ColumnReverse,
"column" => style.flex_direction = FlexDirection::Column, "column" => style.flex_direction = FlexDirection::Column,
"row_reverse" => style.flex_direction = FlexDirection::RowReverse, "row_reverse" => style.flex_direction = FlexDirection::RowReverse,
"row" => style.flex_direction = FlexDirection::Row, "row" => style.flex_direction = FlexDirection::Row,
_ => { _ => {
print_invalid_attrib(&key, &value); print_invalid_attrib(key, value);
} }
}, },
_ => {} _ => {}

View File

@@ -1,6 +1,8 @@
use crate::{ use crate::{
layout::WidgetID, layout::WidgetID,
parser::{ParserContext, ParserFile, parse_children, parse_universal, style::style_from_node}, parser::{
ParserContext, ParserFile, iter_attribs, parse_children, parse_universal, style::parse_style,
},
widget, widget,
}; };
@@ -10,7 +12,8 @@ pub fn parse_widget_div<'a>(
node: roxmltree::Node<'a, 'a>, node: roxmltree::Node<'a, 'a>,
parent_id: WidgetID, parent_id: WidgetID,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let style = style_from_node(file, ctx, node); let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
let style = parse_style(&attribs);
let (new_id, _) = ctx let (new_id, _) = ctx
.layout .layout

View File

@@ -1,10 +1,9 @@
use crate::{ use crate::{
layout::WidgetID, layout::WidgetID,
parser::{ parser::{
ParserContext, ParserFile, iter_attribs, parse_children, parse_color_hex, parse_universal, ParserContext, ParserFile, iter_attribs, parse_children, parse_universal,
print_invalid_attrib, style::style_from_node, style::{parse_style, parse_text_style},
}, },
renderer_vk::text::{FontWeight, HorizontalAlign},
widget::text::{TextLabel, TextParams}, widget::text::{TextLabel, TextParams},
}; };
@@ -16,46 +15,19 @@ pub fn parse_widget_label<'a>(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut params = TextParams::default(); let mut params = TextParams::default();
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect(); let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
let style = parse_style(&attribs);
params.style = parse_text_style(&attribs);
for (key, value) in attribs { for (key, value) in attribs {
match &*key { match &*key {
"text" => { "text" => {
params.content = String::from(value.as_ref()); params.content = String::from(value.as_ref());
} }
"color" => {
if let Some(color) = parse_color_hex(&value) {
params.style.color = Some(color);
}
}
"align" => match &*value {
"left" => params.style.align = Some(HorizontalAlign::Left),
"right" => params.style.align = Some(HorizontalAlign::Right),
"center" => params.style.align = Some(HorizontalAlign::Center),
"justified" => params.style.align = Some(HorizontalAlign::Justified),
"end" => params.style.align = Some(HorizontalAlign::End),
_ => {
print_invalid_attrib(&key, &value);
}
},
"weight" => match &*value {
"normal" => params.style.weight = Some(FontWeight::Normal),
"bold" => params.style.weight = Some(FontWeight::Bold),
_ => {
print_invalid_attrib(&key, &value);
}
},
"size" => {
if let Ok(size) = value.parse::<f32>() {
params.style.size = Some(size);
} else {
print_invalid_attrib(&key, &value);
}
}
_ => {} _ => {}
} }
} }
let style = style_from_node(file, ctx, node);
let (new_id, _) = ctx let (new_id, _) = ctx
.layout .layout
.add_child(parent_id, TextLabel::create(params)?, style)?; .add_child(parent_id, TextLabel::create(params)?, style)?;

View File

@@ -2,11 +2,10 @@ use crate::{
drawing::GradientMode, drawing::GradientMode,
layout::WidgetID, layout::WidgetID,
parser::{ parser::{
ParserContext, ParserFile, is_percent, iter_attribs, parse_children, parse_color_hex, ParserContext, ParserFile, iter_attribs, parse_children, parse_universal, print_invalid_attrib,
parse_f32, parse_percent, parse_universal, print_invalid_attrib, print_invalid_value, style::{parse_color, parse_round, parse_style},
style::style_from_node,
}, },
widget::{self, rectangle::RectangleParams, util::WLength}, widget::{self, rectangle::RectangleParams},
}; };
pub fn parse_widget_rectangle<'a>( pub fn parse_widget_rectangle<'a>(
@@ -17,22 +16,15 @@ pub fn parse_widget_rectangle<'a>(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut params = RectangleParams::default(); let mut params = RectangleParams::default();
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect(); let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
let style = parse_style(&attribs);
for (key, value) in attribs { for (key, value) in attribs {
match &*key { match &*key {
"color" => { "color" => {
if let Some(color) = parse_color_hex(&value) { parse_color(&value, &mut params.color);
params.color = color;
} else {
print_invalid_attrib(&key, &value);
}
} }
"color2" => { "color2" => {
if let Some(color) = parse_color_hex(&value) { parse_color(&value, &mut params.color2);
params.color2 = color;
} else {
print_invalid_attrib(&key, &value);
}
} }
"gradient" => { "gradient" => {
params.gradient = match &*value { params.gradient = match &*value {
@@ -47,17 +39,7 @@ pub fn parse_widget_rectangle<'a>(
} }
} }
"round" => { "round" => {
if is_percent(&value) { parse_round(&value, &mut params.round);
if let Some(val) = parse_percent(&value) {
params.round = WLength::Percent(val);
} else {
print_invalid_value(&value);
}
} else if let Some(val) = parse_f32(&value) {
params.round = WLength::Units(val);
} else {
print_invalid_value(&value);
}
} }
"border" => { "border" => {
params.border = value.parse().unwrap_or_else(|_| { params.border = value.parse().unwrap_or_else(|_| {
@@ -66,18 +48,12 @@ pub fn parse_widget_rectangle<'a>(
}); });
} }
"border_color" => { "border_color" => {
if let Some(color) = parse_color_hex(&value) { parse_color(&value, &mut params.border_color);
params.border_color = color;
} else {
print_invalid_attrib(&key, &value);
}
} }
_ => {} _ => {}
} }
} }
let style = style_from_node(file, ctx, node);
let (new_id, _) = ctx.layout.add_child( let (new_id, _) = ctx.layout.add_child(
parent_id, parent_id,
widget::rectangle::Rectangle::create(params)?, widget::rectangle::Rectangle::create(params)?,

View File

@@ -1,8 +1,7 @@
use crate::{ use crate::{
layout::WidgetID, layout::WidgetID,
parser::{ parser::{
ParserContext, ParserFile, iter_attribs, parse_children, parse_universal, ParserContext, ParserFile, iter_attribs, parse_children, parse_universal, style::parse_style,
style::style_from_node,
}, },
renderer_vk::text::custom_glyph::{CustomGlyphContent, CustomGlyphData}, renderer_vk::text::custom_glyph::{CustomGlyphContent, CustomGlyphData},
widget::sprite::{SpriteBox, SpriteBoxParams}, widget::sprite::{SpriteBox, SpriteBoxParams},
@@ -18,6 +17,7 @@ pub fn parse_widget_sprite<'a>(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut params = SpriteBoxParams::default(); let mut params = SpriteBoxParams::default();
let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect(); let attribs: Vec<_> = iter_attribs(file, ctx, &node, false).collect();
let style = parse_style(&attribs);
let mut glyph = None; let mut glyph = None;
for (key, value) in attribs { for (key, value) in attribs {
@@ -53,8 +53,6 @@ pub fn parse_widget_sprite<'a>(
log::warn!("No source for sprite node!"); log::warn!("No source for sprite node!");
}; };
let style = style_from_node(file, ctx, node);
let (new_id, _) = ctx let (new_id, _) = ctx
.layout .layout
.add_child(parent_id, SpriteBox::create(params)?, style)?; .add_child(parent_id, SpriteBox::create(params)?, style)?;