/*
* This file is part of laurelin_client
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::prelude::*;
use crate::AppState;
pub struct LogPlugin;
impl Plugin for LogPlugin {
fn build(&self, app: &mut App) {
app.add_event::<UpdateLogEvent>()
.add_system(setup_log.in_schedule(OnEnter(AppState::InGame)))
.add_system(update_log_button)
.add_system(toggle_log.run_if(on_event::<ToggleLogEvent>()))
.add_system(update_log.run_if(on_event::<UpdateLogEvent>()));
}
}
pub struct ToggleLogEvent;
pub struct UpdateLogEvent;
const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);
const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);
#[derive(Component)]
struct LogButton;
#[derive(Component)]
struct LogList;
fn setup_log(mut commands: Commands, asset_server: Res<AssetServer>) {
// setup a button for log toggle
// setup the log ui itself, with hidden visibility
let font = asset_server.load("fonts/FiraMono-Bold.ttf");
commands
.spawn((NodeBundle {
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(20.),
right: Val::Px(20.),
..Default::default()
},
..Default::default()
},
..Default::default()
},))
.with_children(|parent| {
parent
.spawn((
ButtonBundle {
style: Style {
size: Size::new(Val::Px(75.), Val::Px(35.)),
// center child text
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
background_color: NORMAL_BUTTON.into(),
..Default::default()
},
LogButton,
))
.with_children(|parent| {
parent.spawn((
TextBundle::from_section(
"Log",
TextStyle {
font: font.clone(),
font_size: 20.,
color: Color::WHITE,
},
),
));
});
});
}
fn update_log_button(
mut interaction_query: Query<
(&Interaction, &mut BackgroundColor),
(Changed<Interaction>, With<LogButton>),
>,
mut ul_ev_w: EventWriter<UpdateLogEvent>,
) {
for (interaction, mut color) in &mut interaction_query {
match interaction {
Interaction::Clicked => {
*color = PRESSED_BUTTON.into();
ul_ev_w.send(UpdateLogEvent);
}
Interaction::Hovered => {
*color = HOVERED_BUTTON.into();
}
Interaction::None => {
*color = NORMAL_BUTTON.into();
}
}
}
}
fn toggle_log() {
// toggle the node visibility, see the state button for e.g
}
fn update_log() {
// despawn the children ui nodes, and create new ones.
// update log ui scrolling items with... log items
}