From 977e440bf1876853ee128c287bd2f5c02cce82f4 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Sun, 21 May 2023 14:00:09 +0300 Subject: [PATCH] wip(client): log ui --- client/src/plugins/game/mod.rs | 4 +- client/src/plugins/game/ui/log.rs | 138 +++++++++++++++++++++--------- client/src/plugins/game/ui/mod.rs | 2 +- client/src/util/action_to_log.rs | 30 +++++++ client/src/util/mod.rs | 3 + 5 files changed, 134 insertions(+), 43 deletions(-) create mode 100644 client/src/util/action_to_log.rs diff --git a/client/src/plugins/game/mod.rs b/client/src/plugins/game/mod.rs index 2c53b0f..ec45a07 100644 --- a/client/src/plugins/game/mod.rs +++ b/client/src/plugins/game/mod.rs @@ -11,7 +11,7 @@ use bevy_rapier3d::prelude::*; use crate::{api::game::{Game, GameState}, game_status::GameStatus, AppState, Global}; -use self::{hand::SpawnHandEvent, supply::SpawnSupplyPilesEvent}; +use self::{hand::SpawnHandEvent, supply::SpawnSupplyPilesEvent, ui::log::UpdateLogEvent}; use super::GameDetailsCallEvent; @@ -93,7 +93,9 @@ pub struct FinishedRefreshGameEvent; fn handle_finished_refresh_game_event( mut ssp_ev_w: EventWriter, mut sh_ev_w: EventWriter, + mut ul_ev_w: EventWriter, ) { ssp_ev_w.send(SpawnSupplyPilesEvent); sh_ev_w.send(SpawnHandEvent); + ul_ev_w.send(UpdateLogEvent); } diff --git a/client/src/plugins/game/ui/log.rs b/client/src/plugins/game/ui/log.rs index 33a8a3d..eb9cbe4 100644 --- a/client/src/plugins/game/ui/log.rs +++ b/client/src/plugins/game/ui/log.rs @@ -8,7 +8,7 @@ use bevy::{prelude::*, input::mouse::{MouseScrollUnit, MouseWheel}, a11y::{AccessibilityNode, accesskit::NodeBuilder}}; -use crate::AppState; +use crate::{AppState, plugins::GameData, util::action_to_log}; pub struct LogPlugin; @@ -24,7 +24,7 @@ impl Plugin for LogPlugin { } } -pub struct ToggleLogEvent; +struct ToggleLogEvent; pub struct UpdateLogEvent; const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15); @@ -34,12 +34,13 @@ const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35); #[derive(Component)] struct LogButton; #[derive(Component)] +struct LogListParent; +#[derive(Component)] struct LogList; +#[derive(Component)] +struct LogListEntry; fn setup_log(mut commands: Commands, asset_server: Res) { - // setup a button for log toggle - // setup the log ui itself, with hidden visibility - let font = asset_server.load("fonts/FiraMono-Bold.ttf"); // log toggle button @@ -88,26 +89,30 @@ fn setup_log(mut commands: Commands, asset_server: Res) { // log window commands - .spawn(NodeBundle { - style: Style { - flex_direction: FlexDirection::Column, - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - size: Size { - width: Val::Px(300.), - height: Val::Px(300.), - }, - position_type: PositionType::Absolute, - position: UiRect { - bottom: Val::Percent(12.5), - right: Val::Px(100.), + .spawn(( + NodeBundle { + style: Style { + flex_direction: FlexDirection::Column, + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + size: Size { + width: Val::Px(300.), + height: Val::Percent(75.), + }, + position_type: PositionType::Absolute, + position: UiRect { + bottom: Val::Percent(12.5), + right: Val::Px(100.), + ..Default::default() + }, ..Default::default() }, + background_color: Color::rgb(0.15, 0.15, 0.15).into(), + visibility: Visibility::Hidden, ..Default::default() }, - background_color: Color::rgb(0.15, 0.15, 0.15).into(), - ..Default::default() - }) + LogListParent, + )) .with_children(|parent| { parent .spawn(NodeBundle { @@ -136,19 +141,18 @@ fn setup_log(mut commands: Commands, asset_server: Res) { LogList, )) .with_children(|parent| { - for i in 0..30 { - parent.spawn(( - TextBundle::from_section( - format!("Item {i}"), - TextStyle { - font: font.clone(), - font_size: 20., - color: Color::WHITE, - }, - ), - AccessibilityNode(NodeBuilder::new(bevy::a11y::accesskit::Role::ListItem)), - )); - } + parent.spawn(( + TextBundle::from_section( + format!("Empty"), + TextStyle { + font: font.clone(), + font_size: 20., + color: Color::WHITE, + }, + ), + AccessibilityNode(NodeBuilder::new(bevy::a11y::accesskit::Role::ListItem)), + LogListEntry, + )); }); }); }); @@ -159,13 +163,13 @@ fn update_log_button( (&Interaction, &mut BackgroundColor), (Changed, With), >, - mut ul_ev_w: EventWriter, + mut tl_ev_w: EventWriter, ) { for (interaction, mut color) in &mut interaction_query { match interaction { Interaction::Clicked => { *color = PRESSED_BUTTON.into(); - ul_ev_w.send(UpdateLogEvent); + tl_ev_w.send(ToggleLogEvent); } Interaction::Hovered => { *color = HOVERED_BUTTON.into(); @@ -177,13 +181,65 @@ fn update_log_button( } } -fn toggle_log() { - // toggle the node visibility, see the state button for e.g +fn toggle_log( + mut log_parent_query: Query<&mut Visibility, With>, +) { + for mut visibility in &mut log_parent_query { + if *visibility == Visibility::Hidden { + *visibility = Visibility::Inherited; + } else { + *visibility = Visibility::Hidden; + } + } } -fn update_log() { - // despawn the children ui nodes, and create new ones. - // update log ui scrolling items with... log items +fn update_log( + mut commands: Commands, + asset_server: Res, + log_query: Query>, + log_entries_query: Query>, + game_data: Res, +) { + for entity in &log_entries_query { + commands.entity(entity).despawn_recursive(); + } + for entity in &log_query { + let Some(status) = game_data.clone().game_status else { + return; + }; + + let font = asset_server.load("fonts/FiraMono-Regular.ttf"); + let font_bold = asset_server.load("fonts/FiraMono-Bold.ttf"); + + let style = TextStyle { + font: font.clone(), + font_size: 20., + color: Color::WHITE, + }; + + let index_spaces = status.actions.len().to_string().len(); + + for (i, action) in status.actions.iter().enumerate() { + let spaces = "".repeat(index_spaces - i.to_string().len()); + + commands.spawn(( + TextBundle::from_sections([[ + TextSection { + value: format!("{}{}. ", spaces, i), + style: TextStyle { + font: font_bold.clone(), + ..style + }, + }, + ], + action_to_log(action, font, style).into_iter() + ].concat()), + AccessibilityNode(NodeBuilder::new(bevy::a11y::accesskit::Role::ListItem)), + LogListEntry, + )) + .set_parent(entity); + } + } } #[derive(Component, Default)] diff --git a/client/src/plugins/game/ui/mod.rs b/client/src/plugins/game/ui/mod.rs index aa0b9ee..3c26788 100644 --- a/client/src/plugins/game/ui/mod.rs +++ b/client/src/plugins/game/ui/mod.rs @@ -18,7 +18,7 @@ use crate::{ use super::{GameData, RefreshGameEvent}; -mod log; +pub mod log; mod state_button; pub struct GameUIPlugin; diff --git a/client/src/util/action_to_log.rs b/client/src/util/action_to_log.rs new file mode 100644 index 0000000..caeaf0a --- /dev/null +++ b/client/src/util/action_to_log.rs @@ -0,0 +1,30 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use bevy::prelude::*; + +use crate::api::game::Action; + +pub fn action_to_log( + action: &Action, + font: Handle, + style: TextStyle, +) -> Vec { + match &action.command { + _ => { + vec![ + TextSection { + value: format!("No log configuration for this command"), + style: TextStyle { + ..style + }, + }, + ] + } + } +} diff --git a/client/src/util/mod.rs b/client/src/util/mod.rs index f480390..d151761 100644 --- a/client/src/util/mod.rs +++ b/client/src/util/mod.rs @@ -7,3 +7,6 @@ */ pub mod egui; + +mod action_to_log; +pub use action_to_log::action_to_log; -- 2.44.1