Skip to content

Instantly share code, notes, and snippets.

@NEO97online
Created February 18, 2022 22:15
Show Gist options
  • Save NEO97online/350420d7f93bc1bd917f3972b8d9ccf8 to your computer and use it in GitHub Desktop.
Save NEO97online/350420d7f93bc1bd917f3972b8d9ccf8 to your computer and use it in GitHub Desktop.
pub trait UiCommand: std::fmt::Debug + 'static {
fn write(&self, commands: &mut Commands);
}
#[derive(Default, Debug)]
pub struct UiNode {
queue: Vec<Box<dyn UiCommand>>,
children: UiTree,
}
impl UiNode {
pub fn with_children(&mut self, f: impl FnOnce(&mut UiTree)) -> &mut Self {
f(&mut self.children);
self
}
}
#[derive(Debug)]
pub struct ShowUiBundle<T: Bundle> {
pub id: String,
pub bundle: T,
}
impl<T: Bundle + Clone + std::fmt::Debug> UiCommand for ShowUiBundle<T> {
fn write(&self, commands: &mut Commands) {
println!("spawning {:?}", self.bundle);
commands.spawn_bundle(self.bundle.clone());
}
}
#[derive(Default, Debug)]
pub struct UiTree(HashMap<String, UiNode>);
impl UiTree {
pub fn show_bundle<T: Bundle + std::fmt::Debug + Clone>(
&mut self,
id: String,
bundle: T,
) -> &mut UiNode {
let mut node = UiNode::default();
node.queue.push(Box::new(ShowUiBundle {
id: id.clone(),
bundle,
}));
self.0.insert(id.clone(), node);
self.0.get_mut(&id).unwrap()
}
pub fn render(&mut self, commands: &mut Commands) {
for (id, node) in self.0.iter_mut() {
for command in node.queue.iter_mut() {
command.write(commands);
}
node.children.render(commands);
}
}
}
pub fn show_info_panels(
mut commands: Commands,
fonts: Res<GameFonts>,
turn: Res<TurnData>,
menu_assets: Res<MenuAssets>,
) {
let mut ui = UiTree::default();
ui.show_bundle(
"container".to_string(),
NodeBundle {
color: Color::NONE.into(),
style: Style {
flex_direction: FlexDirection::ColumnReverse,
align_items: AlignItems::FlexStart,
justify_content: JustifyContent::FlexStart,
..Default::default()
},
..Default::default()
},
)
.with_children(|parent| {
parent.show_bundle(
"day_text".to_string(),
TextBundle {
text: Text::with_section(
format!("Day {}", turn.day).to_string(),
TextStyle {
font: fonts.main.clone(),
font_size: 24.0,
color: Color::BLACK,
},
Default::default(),
),
..Default::default()
},
);
// .insert(DayText);
parent
.show_bundle(
"team_text".to_string(),
TextBundle {
text: Text::with_section(
format!("{:?}'s turn", turn.team).to_string(),
TextStyle {
font: fonts.main.clone(),
font_size: 24.0,
color: match turn.team {
Team::Red => Color::RED,
Team::Blue => Color::BLUE,
},
},
Default::default(),
..Default::default()
},
);
)
// .insert(TeamText);
});
println!("{:#?}", ui);
ui.render(&mut commands);
// let content_entity = commands
// .spawn_bundle(NodeBundle {
// color: Color::NONE.into(),
// style: Style {
// flex_direction: FlexDirection::ColumnReverse,
// align_items: AlignItems::FlexStart,
// justify_content: JustifyContent::FlexStart,
// ..Default::default()
// },
// ..Default::default()
// })
// .with_children(|parent| {
// parent
// .spawn_bundle(TextBundle {
// text: Text::with_section(
// format!("Day {}", turn.day).to_string(),
// TextStyle {
// font: fonts.main.clone(),
// font_size: 24.0,
// color: Color::BLACK,
// },
// Default::default(),
// ),
// ..Default::default()
// })
// .insert(DayText);
// parent
// .spawn_bundle(TextBundle {
// text: Text::with_section(
// format!("{:?}'s turn", turn.team).to_string(),
// TextStyle {
// font: fonts.main.clone(),
// font_size: 24.0,
// color: match turn.team {
// Team::Red => Color::RED,
// Team::Blue => Color::BLUE,
// },
// },
// Default::default(),
// ),
// ..Default::default()
// })
// .insert(TeamText);
// })
// .id();
// commands
// .spawn_bundle(NinePatchBundle {
// style: Style {
// margin: Rect::all(Val::Auto),
// justify_content: JustifyContent::Center,
// align_items: AlignItems::Center,
// size: Size::new(Val::Px(100.0), Val::Px(80.0)),
// position_type: PositionType::Absolute,
// position: Rect {
// left: Val::Px(16.0),
// right: Val::Undefined,
// top: Val::Px(16.0),
// bottom: Val::Undefined,
// },
// ..Default::default()
// },
// nine_patch_data: NinePatchData::with_single_content(
// menu_assets.nine_patch_texture.clone(),
// menu_assets.nine_patch.clone(),
// content_entity,
// ),
// ..Default::default()
// })
// .insert(TurnInfoPanel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment