diff --git a/crates/render/src/ui/api/functions/radialbar.rs b/crates/render/src/ui/api/functions/radialbar.rs index a1d5dd3..449df11 100644 --- a/crates/render/src/ui/api/functions/radialbar.rs +++ b/crates/render/src/ui/api/functions/radialbar.rs @@ -14,11 +14,10 @@ pub fn build_radialbar_module(state_src: Rc>) -> Module { .with_namespace(FnNamespace::Internal) .set_into_module( &mut module, - // TODO: fix ugly spaces move |name: ImmutableString, stroke: f32, color: Color, rect: Rect| { let mut ui_state = state.borrow_mut(); - if ui_state.elements.contains_key(&name) { + if ui_state.contains_name(&name) { error!("tried to make a radialbar using an existing name `{name}`"); return; } @@ -38,12 +37,23 @@ pub fn build_radialbar_module(state_src: Rc>) -> Module { .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString, val: f32| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::RadialBar(x)) => x.set_val(val), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::RadialBar(x) => x, + _ => { + error!("called `radialbar::set_val` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::RadialBar(x)) => x, _ => { - error!("called `radialbar_set_val` on an invalid name `{name}`") + error!("called `radialbar::set_val` on an invalid name `{name}`"); + return; } - } + }; + + e.set_val(val); }); return module; diff --git a/crates/render/src/ui/api/functions/scrollbox.rs b/crates/render/src/ui/api/functions/scrollbox.rs index 26207d7..fea3015 100644 --- a/crates/render/src/ui/api/functions/scrollbox.rs +++ b/crates/render/src/ui/api/functions/scrollbox.rs @@ -15,7 +15,7 @@ pub fn build_scrollbox_module(state_src: Rc>) -> Module { .set_into_module(&mut module, move |name: ImmutableString, rect: Rect| { let mut ui_state = state.borrow_mut(); - if ui_state.elements.contains_key(&name) { + if ui_state.contains_name(&name) { error!("tried to make a scrollbox using an existing name `{name}`"); return; } @@ -30,44 +30,41 @@ pub fn build_scrollbox_module(state_src: Rc>) -> Module { &mut module, move |name: ImmutableString, target: ImmutableString| { let mut ui_state = state.borrow_mut(); + + // Make sure `name` is a scrollbox match ui_state.get_mut_by_name(&name) { - Some(UiElement::Scrollbox(_)) => { - match ui_state.get_mut_by_name(&target) { - Some(UiElement::Text(_)) | Some(UiElement::Sprite(_)) => { - let e = match ui_state.remove_element_incomplete(&target) { - Some(UiElement::Sprite(s)) => { - Rc::new(RefCell::new(UiElement::Sprite(s))) - } - Some(UiElement::Text(t)) => { - Rc::new(RefCell::new(UiElement::Text(t))) - } - _ => unreachable!(), - }; - - // Add a subelement pointing to this sprite - ui_state.add_element(UiElement::SubElement { - parent: name.clone(), - element: e.clone(), - }); - - // Add this sprite to a scrollbox - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Scrollbox(s)) => { - s.add_element(e); - } - _ => unreachable!(), - }; - } - Some(_) => { - error!("cannot add `{name}` to scrollbox `{name}`, invalid type.") - } - None => { - error!("called `scrollbox::add_element` with a non-existing target `{target}`") - } + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Scrollbox(_) => {} + _ => { + error!("called `scrollbox::add_element` on an invalid name `{name}`"); + return; } - } + }, + + Some(UiElement::Scrollbox(_)) => {} _ => { - error!("called `scrollbox::add_element` on an invalid name `{name}`") + error!("called `scrollbox::add_element` on an invalid name `{name}`"); + return; + } + } + + // Replace the target with a SubElement, without changing its position in the array. + match ui_state.get_mut_by_name(&target) { + Some(UiElement::Text(_)) | Some(UiElement::Sprite(_)) => { + let e = ui_state.get_mut_by_name(&target).unwrap(); + let new = (*e).clone(); + *e = UiElement::SubElement { + parent: name.clone(), + element: Box::new(new), + }; + } + Some(_) => { + error!("cannot add `{name}` to scrollbox `{name}`, invalid type.") + } + None => { + error!( + "called `scrollbox::add_element` with a non-existing target `{target}`" + ) } } }, diff --git a/crates/render/src/ui/api/functions/sprite.rs b/crates/render/src/ui/api/functions/sprite.rs index 11d6ff6..b83e899 100644 --- a/crates/render/src/ui/api/functions/sprite.rs +++ b/crates/render/src/ui/api/functions/sprite.rs @@ -26,7 +26,7 @@ pub fn build_sprite_module(ct_src: Arc, state_src: Rc> return; } - if ui_state.elements.contains_key(&name) { + if ui_state.contains_name(&name) { error!("tried to make a sprite using an existing name `{name}`"); return; } @@ -55,7 +55,7 @@ pub fn build_sprite_module(ct_src: Arc, state_src: Rc> .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - if ui_state.elements.contains_key(&name) { + if ui_state.contains_name(&name) { ui_state.remove_element(&name); } else { error!("called `sprite::remove` on an invalid name `{name}`") @@ -96,29 +96,36 @@ pub fn build_sprite_module(ct_src: Arc, state_src: Rc> move |name: ImmutableString, edge_name: ImmutableString, duration: f32| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Sprite(x)) => { - let sprite = x.anim.get_sprite(); + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Sprite(x) => x, + _ => { + error!("called `sprite::jump_to` on an invalid name `{name}`"); + return; + } + }, - let edge = - resolve_edge_as_edge(&sprite.sections, edge_name.as_str(), duration); - let edge = match edge { - Err(_) => { - error!( - "called `sprite::jump_to` on an invalid edge `{}` on sprite `{}`", - edge_name, sprite.index - ); - return; - } - Ok(s) => s, - }; - - x.anim.jump_to(&edge); - } + Some(UiElement::Sprite(x)) => x, _ => { - error!("called `sprite::jump_to` on an invalid name `{name}`") + error!("called `sprite::jump_to` on an invalid name `{name}`"); + return; } - } + }; + + let sprite = e.anim.get_sprite(); + let edge = resolve_edge_as_edge(&sprite.sections, edge_name.as_str(), duration); + let edge = match edge { + Err(_) => { + error!( + "called `sprite::jump_to` on an invalid edge `{}` on sprite `{}`", + edge_name, sprite.index + ); + return; + } + Ok(s) => s, + }; + + e.anim.jump_to(&edge); }, ); @@ -127,12 +134,22 @@ pub fn build_sprite_module(ct_src: Arc, state_src: Rc> .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString, x: f32| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Sprite(s)) => s.set_angle(x), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Sprite(x) => x, + _ => { + error!("called `sprite::set_angle` on an invalid name `{name}`"); + return; + } + }, + Some(UiElement::Sprite(x)) => x, _ => { - error!("called `sprite::set_angle` on an invalid name `{name}`") + error!("called `sprite::set_angle` on an invalid name `{name}`"); + return; } - } + }; + + e.set_angle(x) }); let state = state_src.clone(); @@ -140,12 +157,22 @@ pub fn build_sprite_module(ct_src: Arc, state_src: Rc> .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString, x: Rect| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Sprite(s)) => s.set_rect(x), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Sprite(x) => x, + _ => { + error!("called `sprite::set_rect` on an invalid name `{name}`"); + return; + } + }, + Some(UiElement::Sprite(x)) => x, _ => { - error!("called `sprite::set_rect` on an invalid name `{name}`") + error!("called `sprite::set_rect` on an invalid name `{name}`"); + return; } - } + }; + + e.set_rect(x); }); let state = state_src.clone(); @@ -153,12 +180,22 @@ pub fn build_sprite_module(ct_src: Arc, state_src: Rc> .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString, x: Color| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Sprite(s)) => s.set_color(x), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Sprite(x) => x, + _ => { + error!("called `sprite::set_color` on an invalid name `{name}`"); + return; + } + }, + Some(UiElement::Sprite(x)) => x, _ => { - error!("called `sprite::set_color` on an invalid name `{name}`") + error!("called `sprite::set_color` on an invalid name `{name}`"); + return; } - } + }; + + e.set_color(x); }); let state = state_src.clone(); @@ -166,12 +203,46 @@ pub fn build_sprite_module(ct_src: Arc, state_src: Rc> .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString, x: bool| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Sprite(s)) => s.set_preserve_aspect(x), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Sprite(x) => x, + _ => { + error!("called `sprite::preserve_aspect` on an invalid name `{name}`"); + return; + } + }, + Some(UiElement::Sprite(x)) => x, _ => { - error!("called `sprite::set_preserve_aspect` on an invalid name `{name}`") + error!("called `sprite::preserve_aspect` on an invalid name `{name}`"); + return; } - } + }; + + e.set_preserve_aspect(x); }); + + let state = state_src.clone(); + let _ = FuncRegistration::new("set_disable_events") + .with_namespace(FnNamespace::Internal) + .set_into_module(&mut module, move |name: ImmutableString, x: bool| { + let mut ui_state = state.borrow_mut(); + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Sprite(x) => x, + _ => { + error!("called `sprite::set_disable_events` on an invalid name `{name}`"); + return; + } + }, + Some(UiElement::Sprite(x)) => x, + _ => { + error!("called `sprite::set_disable_events` on an invalid name `{name}`"); + return; + } + }; + + e.set_disable_events(x); + }); + return module; } diff --git a/crates/render/src/ui/api/functions/textbox.rs b/crates/render/src/ui/api/functions/textbox.rs index d5e8a57..bbc59de 100644 --- a/crates/render/src/ui/api/functions/textbox.rs +++ b/crates/render/src/ui/api/functions/textbox.rs @@ -27,7 +27,7 @@ pub fn build_textbox_module( color: Color| { let mut ui_state = state.borrow_mut(); - if ui_state.elements.contains_key(&name) { + if ui_state.contains_name(&name) { error!("tried to make a textbox using an existing name `{name}`"); return; } @@ -51,12 +51,24 @@ pub fn build_textbox_module( &mut module, move |name: ImmutableString, text: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_text(&mut font.borrow_mut(), text.as_str()), + + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::set_text` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::set_text` on an invalid name `{name}`") + error!("called `textbox::set_text` on an invalid name `{name}`"); + return; } - } + }; + + e.set_text(&mut font.borrow_mut(), text.as_str()); }, ); @@ -66,12 +78,24 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_align(&mut font.borrow_mut(), Align::Left), + + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::align_left` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::align_left` on an invalid name `{name}`") + error!("called `textbox::align_left` on an invalid name `{name}`"); + return; } - } + }; + + e.set_align(&mut font.borrow_mut(), Align::Left); }); let state = state_src.clone(); @@ -80,12 +104,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_align(&mut font.borrow_mut(), Align::Right), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::align_right` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::align_right` on an invalid name `{name}`") + error!("called `textbox::align_right` on an invalid name `{name}`"); + return; } - } + }; + + e.set_align(&mut font.borrow_mut(), Align::Right); }); let state = state_src.clone(); @@ -94,12 +129,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_align(&mut font.borrow_mut(), Align::Justified), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::align_justify` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::align_justify` on an invalid name `{name}`") + error!("called `textbox::align_justify` on an invalid name `{name}`"); + return; } - } + }; + + e.set_align(&mut font.borrow_mut(), Align::Justified); }); let state = state_src.clone(); @@ -108,12 +154,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_align(&mut font.borrow_mut(), Align::Center), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::align_center` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::align_center` on an invalid name `{name}`") + error!("called `textbox::align_center` on an invalid name `{name}`"); + return; } - } + }; + + e.set_align(&mut font.borrow_mut(), Align::Center); }); let state = state_src.clone(); @@ -122,12 +179,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_weight(&mut font.borrow_mut(), Weight::BOLD), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::weight_bold` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::weight_bold` on an invalid name `{name}`") + error!("called `textbox::weight_bold` on an invalid name `{name}`"); + return; } - } + }; + + e.set_weight(&mut font.borrow_mut(), Weight::BOLD); }); let state = state_src.clone(); @@ -136,12 +204,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_weight(&mut font.borrow_mut(), Weight::NORMAL), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::weight_normal` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::weight_normal` on an invalid name `{name}`") + error!("called `textbox::weight_normal` on an invalid name `{name}`"); + return; } - } + }; + + e.set_weight(&mut font.borrow_mut(), Weight::NORMAL); }); let state = state_src.clone(); @@ -150,12 +229,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_font(&mut font.borrow_mut(), FamilyOwned::Serif), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::font_serif` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::font_serif` on an invalid name `{name}`") + error!("called `textbox::font_serif` on an invalid name `{name}`"); + return; } - } + }; + + e.set_font(&mut font.borrow_mut(), FamilyOwned::Serif); }); let state = state_src.clone(); @@ -164,14 +254,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => { - x.set_font(&mut font.borrow_mut(), FamilyOwned::SansSerif) - } + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::font_sans` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::font_sans` on an invalid name `{name}`") + error!("called `textbox::font_sans` on an invalid name `{name}`"); + return; } - } + }; + + e.set_font(&mut font.borrow_mut(), FamilyOwned::SansSerif); }); let state = state_src.clone(); @@ -180,14 +279,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => { - x.set_font(&mut font.borrow_mut(), FamilyOwned::Monospace) - } + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::font_mono` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::font_mono` on an invalid name `{name}`") + error!("called `textbox::font_mono` on an invalid name `{name}`"); + return; } - } + }; + + e.set_font(&mut font.borrow_mut(), FamilyOwned::Monospace); }); let state = state_src.clone(); @@ -196,12 +304,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_style(&mut font.borrow_mut(), Style::Normal), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::style_normal` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::style_normal` on an invalid name `{name}`") + error!("called `textbox::style_normal` on an invalid name `{name}`"); + return; } - } + }; + + e.set_style(&mut font.borrow_mut(), Style::Normal); }); let state = state_src.clone(); @@ -210,12 +329,23 @@ pub fn build_textbox_module( .with_namespace(FnNamespace::Internal) .set_into_module(&mut module, move |name: ImmutableString| { let mut ui_state = state.borrow_mut(); - match ui_state.get_mut_by_name(&name) { - Some(UiElement::Text(x)) => x.set_style(&mut font.borrow_mut(), Style::Italic), + let e = match ui_state.get_mut_by_name(&name) { + Some(UiElement::SubElement { element, .. }) => match &mut **element { + UiElement::Text(x) => x, + _ => { + error!("called `textbox::style_italic` on an invalid name `{name}`"); + return; + } + }, + + Some(UiElement::Text(x)) => x, _ => { - error!("called `textbox::style_italic` on an invalid name `{name}`") + error!("called `textbox::style_italic` on an invalid name `{name}`"); + return; } - } + }; + + e.set_style(&mut font.borrow_mut(), Style::Italic); }); return module;