DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: da3cdb5843a022f5b2f6d8ade6255715fa252ff9 deck-builder/client/src/plugins/game/ui/log.rs -rw-r--r-- 3.5 KiB
da3cdb58Jonni Liljamo wip(client): better log ui 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * 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
}