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
217
218
219
220
221
222
223
224
225
226
227
228
use bevy::app::{App, Plugin};
use bevy::input::mouse::MouseButtonInput;
use bevy::prelude::{
    Camera, Camera2dBundle, Commands, Component, Event, EventReader, EventWriter, GlobalTransform,
    Query, Res, Startup, Update, With,
};
use bevy::window::Window;
use bevy_egui::{egui, EguiContexts};

use crate::chess_board::{BoardPosition, ChessBoard, GameEndStatus, ResetBoardEvent};
use crate::fen::Fen;

mod board;
mod piece;

pub(super) struct UIPlugin;

impl Plugin for UIPlugin {
    #[cfg(not(tarpaulin_include))]
    fn build(&self, app: &mut App) {
        use bevy_egui::EguiPlugin;

        app.add_plugins(EguiPlugin)
            .init_resource::<piece::PieceProperties>()
            .init_resource::<board::BoardProperties>()
            .add_event::<BoardClickEvent>()
            .add_systems(Startup, (setup, board::setup))
            .add_systems(
                Update,
                (
                    mouse_event_handler,
                    ui_system,
                    piece::piece_click_handler,
                    piece::piece_undragger,
                    piece::piece_creator,
                    piece::piece_move_audio,
                    piece::piece_dragger,
                    piece::piece_mover,
                    piece::piece_resetter,
                    board::highlight_valid_squares,
                ),
            );
    }
}

#[derive(Component)]
struct MainCamera;

fn setup(mut commands: Commands) {
    commands.spawn((Camera2dBundle::default(), MainCamera));
}

fn ui_system(
    mut contexts: EguiContexts,
    mut setup_event: EventWriter<ResetBoardEvent>,
    board: Res<ChessBoard>,
) {
    let ctx = contexts.ctx_mut();
    egui::SidePanel::left("left_panel")
        .default_width(200.0)
        .show(ctx, |ui| {
            // Reset board button
            if ui.button("Reset Board").clicked() {
                setup_event.send(ResetBoardEvent::new(Fen::default()));
            }
        });

    egui::SidePanel::right("right_panel")
        .default_width(200.0)
        .show(ctx, |ui| {
            // Past moves list
            ui.heading("Past Moves");

            let text_style = egui::TextStyle::Body;
            let row_height = ui.text_style_height(&text_style);
            let total_rows = (board.past_moves().len() as f32 / 2.0).ceil() as usize;
            egui::ScrollArea::vertical()
                .auto_shrink([false; 2])
                .stick_to_bottom(true)
                .max_height(ui.available_height() * 4.0 / 5.0)
                .show_rows(ui, row_height, total_rows, |ui, row_range| {
                    for row in row_range {
                        let white_move = board.past_moves()[row * 2].as_algebraic();
                        let black_move =
                            if (row == total_rows - 1) && ((board.past_moves().len() & 1) == 1) {
                                "".to_string()
                            } else {
                                board.past_moves()[row * 2 + 1].as_algebraic()
                            };
                        let mut move_number = row + *board.move_number() as usize - total_rows;
                        if (board.past_moves().len() & 1) == 1 {
                            move_number += 1;
                        }
                        let move_text = format!("{}. {} {}", move_number, white_move, black_move);
                        ui.label(move_text);
                    }
                });

            // Game end status
            if board.game_end_status().is_some() {
                ui.label(match board.game_end_status().unwrap() {
                    GameEndStatus::Checkmate => "Checkmate",
                    GameEndStatus::Resignation => "Resignation",
                    GameEndStatus::Stalemate => "Stalemate",
                    GameEndStatus::DeadPosition => "Dead Position",
                    GameEndStatus::FlagFall => "Flag Fall",
                });
                ui.label(format!(
                    "Winner: {}",
                    match board.winner() {
                        Some(x) => x.to_string(),
                        None => "Draw".to_string(),
                    }
                ));
            }
        });
}

#[derive(Debug, Copy, Clone, Event)]
struct BoardClickEvent {
    position: Option<BoardPosition>,
    input: MouseButtonInput,
}

fn mouse_event_handler(
    windows: Query<&Window>,
    mut mouse_input: EventReader<MouseButtonInput>,
    properties: Res<board::BoardProperties>,
    mut board_click_event: EventWriter<BoardClickEvent>,
    camera: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
) {
    let window = windows.get_single().expect("No window has been created.");
    let (camera, camera_transform) = camera.single();
    for input in mouse_input.iter() {
        // Check if the cursor is in the window and convert to world coordinates
        if let Some(world_position) = window
            .cursor_position()
            .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor))
            .map(|ray| ray.origin.truncate())
        {
            // Check if the mouse is over the board
            let board_position = properties.transform_to_position(&world_position);
            // Send a board click event
            let event = BoardClickEvent {
                position: board_position,
                input: *input,
            };
            board_click_event.send(event);
        }
    }
}

#[cfg(test)]
mod tests {
    use bevy::{
        input::InputPlugin,
        prelude::{AssetPlugin, Entity, Events, Vec2},
        window::{Window, WindowPlugin},
        MinimalPlugins,
    };

    use crate::ui::board::BoardProperties;

    use super::*;

    #[test]
    fn test_setup() {
        // Setup app
        let mut app = App::new();
        app.add_plugins((MinimalPlugins, AssetPlugin::default()));
        app.add_systems(Startup, setup);

        // Run systems
        app.update();

        // Confirm that the camera has been created
        assert_eq!(app.world.query::<&Camera>().iter(&app.world).len(), 1);
        assert_eq!(app.world.query::<&MainCamera>().iter(&app.world).len(), 1);
    }

    #[test]
    #[ignore]
    fn test_mouse_event_handler() {
        // Setup app
        let mut app = App::new();
        app.add_plugins((
            MinimalPlugins,
            AssetPlugin::default(),
            WindowPlugin::default(),
            InputPlugin,
        ));
        app.init_resource::<board::BoardProperties>();
        app.add_event::<BoardClickEvent>();
        app.add_systems(Startup, setup);
        app.add_systems(Update, mouse_event_handler);

        // Run systems
        app.update();

        // Send MouseInputEvent
        let click_position = Vec2::new(34.0, 765.0);
        let (entity, _window) = app.world.query::<(Entity, &Window)>().single(&app.world);
        let mouse_button_input = MouseButtonInput {
            button: bevy::prelude::MouseButton::Left,
            state: bevy::input::ButtonState::Pressed,
            window: entity,
        };
        app.world
            .resource_mut::<Events<MouseButtonInput>>()
            .send(mouse_button_input);

        // Run systems
        app.update();

        // Get BoardClickEvent event reader
        let board_click_events = app.world.resource::<Events<BoardClickEvent>>();
        let mut board_click_reader = board_click_events.get_reader();
        let board_click = board_click_reader.iter(board_click_events).next().unwrap();

        // Check the event has been sent
        let board_properties = app.world.get_resource::<BoardProperties>().unwrap();
        assert_eq!(
            board_click.position,
            board_properties.transform_to_position(&click_position)
        );
        assert_eq!(board_click.input, mouse_button_input);
    }
}