Skip to content

Commit

Permalink
feat: heading field
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Feb 8, 2024
1 parent 23dd5e8 commit 5e9c4b3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/gamai/macros/src/field_ui/parse_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn parse_enum_variant(
let out = match &variant.fields {
syn::Fields::Unit => {
if parent_is_hidden {
quote! {Self::#variant_ident => GroupField::new("Empty Enum".to_string(),Vec::new()).into()}
quote! {Self::#variant_ident => HeadingField::new("No Fields".to_string()).into()}
// quote! {Self::#variant_ident => select.into()}
} else {
quote! {Self::#variant_ident => select.into()}
Expand Down
11 changes: 8 additions & 3 deletions crates/gamai/src/ui/field_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub trait IntoFieldUi: 'static + Clone + Sized {
// #[derive(Display)]
#[derive(Clone, Display)]
pub enum FieldUi {
// None,
Heading(HeadingField),
Group(GroupField),
Text(TextField),
Bool(BoolField),
Expand All @@ -31,6 +31,7 @@ pub enum FieldUi {
impl FieldUi {
pub fn into_string_tree(&self) -> Tree<String> {
match self {
FieldUi::Heading(val) => Tree::new(val.text.clone()),
FieldUi::Group(val) => Tree {
value: val.to_string(),
children: val
Expand Down Expand Up @@ -67,11 +68,12 @@ impl FieldUi {
.zip(other.children.iter())
.all(|(a, b)| a.is_equal_graph(b))
}
(FieldUi::Text(val), FieldUi::Text(other)) => {
(FieldUi::Heading(val), FieldUi::Heading(other)) => val == other,
(FieldUi::Bool(val), FieldUi::Bool(other)) => {
val.reflect.field_name == other.reflect.field_name
&& val.reflect.get() == other.reflect.get()
}
(FieldUi::Bool(val), FieldUi::Bool(other)) => {
(FieldUi::Text(val), FieldUi::Text(other)) => {
val.reflect.field_name == other.reflect.field_name
&& val.reflect.get() == other.reflect.get()
}
Expand Down Expand Up @@ -129,6 +131,9 @@ impl FieldUi {
}


impl Into<FieldUi> for HeadingField {
fn into(self) -> FieldUi { FieldUi::Heading(self) }
}
impl Into<FieldUi> for BoolField {
fn into(self) -> FieldUi { FieldUi::Bool(self) }
}
Expand Down
20 changes: 20 additions & 0 deletions crates/gamai/src/ui/heading_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::fmt::Display;


#[derive(Clone, PartialEq)]
pub struct HeadingField {
pub text: String,
pub size: f32,
}

impl HeadingField {
pub fn new(text: String) -> Self { Self { text, size: 0.2 } }
}
impl Display for HeadingField {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HeadingField")
.field("text", &self.text)
.field("size", &self.size)
.finish()
}
}
3 changes: 3 additions & 0 deletions crates/gamai/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub use self::field_reflect::*;
pub mod checkbox_field;
#[allow(unused_imports)]
pub use self::checkbox_field::*;
pub mod heading_field;
#[allow(unused_imports)]
pub use self::heading_field::*;
pub mod select_field;
#[allow(unused_imports)]
pub use self::select_field::*;
11 changes: 9 additions & 2 deletions crates/gamai/test/ui/field_ui_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,25 @@ pub fn works() -> Result<()> {

let ui = FieldUiRoot::new(TestEnum2::Variant1).get_ui();
// println!("{}", ui);
if let FieldUi::Group(group) = ui {
expect(group.children.len()).to_be(0)?;
if let FieldUi::Heading(heading) = ui {
expect(&heading.text).to_be(&"No Fields".to_string())?;
} else {
anyhow::bail!("waat");
}

Ok(())
}
#[sweet_test]
pub fn works_for_action_lists() -> Result<()> {
let ui = FieldUiRoot::new(BuiltinNode::EmptyAction(EmptyAction)).get_ui();
// if let FieldUi::Heading(heading) = ui {
// expect(&heading.text).to_be(&"".to_string())?;
// } else {
// anyhow::bail!("waat");
// }
if let FieldUi::Group(group) = ui {
expect(group.children.len()).to_be(1)?;

if let FieldUi::Group(group) = &group.children[0] {
expect(group.children.len()).to_be(0)?;
} else {
Expand Down

0 comments on commit 5e9c4b3

Please sign in to comment.