DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: d11c5cc5c63436f4a534ac2b395559063a96cd6f deck-builder/client/src/plugins/game/ui/log.rs -rw-r--r-- 7.4 KiB
d11c5cc5Jonni Liljamo wip(client): 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * 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::*, input::mouse::{MouseScrollUnit, MouseWheel}, a11y::{AccessibilityNode, accesskit::NodeBuilder}};

use crate::AppState;

pub struct LogPlugin;

impl Plugin for LogPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<ToggleLogEvent>()
            .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>()))
            .add_system(log_mouse_scroll);
    }
}

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");

    // log toggle button
    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,
                            },
                        ),
                    ));
                });
        });

    // 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.),
                    ..Default::default()
                },
                ..Default::default()
            },
            background_color: Color::rgb(0.15, 0.15, 0.15).into(),
            ..Default::default()
        })
        .with_children(|parent| {
            parent
                .spawn(NodeBundle {
                    style: Style {
                        flex_direction: FlexDirection::Column,
                        align_self: AlignSelf::Stretch,
                        size: Size::all(Val::Percent(95.)),
                        overflow: Overflow::Hidden,
                        ..Default::default()
                    },
                    ..Default::default()
                })
                .with_children(|parent| {
                    parent
                        .spawn((
                            NodeBundle {
                                style: Style {
                                    flex_direction: FlexDirection::Column,
                                    max_size: Size::UNDEFINED,
                                    ..Default::default()
                                },
                                ..Default::default()
                            },
                            AccessibilityNode(NodeBuilder::new(bevy::a11y::accesskit::Role::List)),
                            ScrollingList::default(),
                            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)),
                                ));
                            }
                        });
                });
        });
}

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
}

#[derive(Component, Default)]
struct ScrollingList {
    position: f32,
}

fn log_mouse_scroll(
    mut mouse_wheel_events: EventReader<MouseWheel>,
    mut query_list: Query<(&mut ScrollingList, &mut Style, &Parent, &Node)>,
    query_node: Query<&Node>,
) {
    for mouse_wheel_event in mouse_wheel_events.iter() {
        for (mut scrolling_list, mut style, parent, list_node) in &mut query_list {
            let items_height = list_node.size().y;
            let container_height = query_node.get(parent.get()).unwrap().size().y;

            let max_scroll = (items_height - container_height).max(0.);

            let dy = match mouse_wheel_event.unit {
                MouseScrollUnit::Line => mouse_wheel_event.y * 20.,
                MouseScrollUnit::Pixel => mouse_wheel_event.y,
            };

            scrolling_list.position += dy;
            scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.);
            style.position.top = Val::Px(scrolling_list.position);
        }
    }
}