gui_pb/drawing/
tab.rs

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::drawing::extra_opts::draw_extra_opts;
use crate::drawing::game::{draw_game, draw_grid};
use crate::drawing::imu::draw_imu_data;
use crate::drawing::motors::draw_motors;
use crate::drawing::over_the_air::draw_over_the_air;
use crate::drawing::settings::draw_settings;
use crate::drawing::timings::draw_timings;
use crate::transform::Transform;
use crate::App;
use core_pb::constants::{ROBOT_DISPLAY_HEIGHT, ROBOT_DISPLAY_WIDTH};
use eframe::egui::{Color32, Pos2, Rect, Rounding, Stroke, Ui, WidgetText};
use egui_dock::TabViewer;

pub enum Tab {
    /// Main game grid
    Grid,
    /// Detailed timings
    Stopwatch,
    /// User settings
    Settings,
    /// Motor configuration and testing
    Motors,
    /// Robot view
    Robot,
    /// Keybindings
    Keybindings,
    /// Status of OTA programming
    OverTheAirProgramming,
    /// For widgets that don't have corresponding tabs
    Unknown,
    /// Simulated robot display
    RobotDisplay,
    /// Simulated robot controls
    RobotButtonPanel,
    /// Extra options for temporary testing
    ExtraOpts,
    /// Data about individual sensors on the IMU
    Imu,
}

impl TabViewer for App {
    type Tab = Tab;

    fn title(&mut self, tab: &mut Self::Tab) -> WidgetText {
        match tab {
            Tab::Grid => "Main Grid",
            Tab::Stopwatch => "Stopwatch",
            Tab::Settings => "Settings",
            Tab::Motors => "Motors",
            Tab::Robot => "Robot",
            Tab::Keybindings => "Keybindings",
            Tab::OverTheAirProgramming => "OTA Programming",
            Tab::RobotDisplay => "Robot Display",
            Tab::RobotButtonPanel => "Robot Button Panel",
            Tab::ExtraOpts => "Extra Opts",
            Tab::Imu => "Imu",
            Tab::Unknown => "?",
        }
        .into()
    }

    fn ui(&mut self, ui: &mut Ui, tab: &mut Self::Tab) {
        match tab {
            Tab::Grid => {
                let rect = ui.max_rect();
                let (src_p1, src_p2) = self.settings.standard_grid.get_soft_boundaries();

                self.world_to_screen = if self.rotated_grid {
                    Transform::new_letterboxed(
                        Pos2::new(src_p1.x, src_p2.y),
                        Pos2::new(src_p2.x, src_p1.y),
                        Pos2::new(rect.left(), rect.top()),
                        Pos2::new(rect.right(), rect.bottom()),
                        true,
                    )
                } else {
                    Transform::new_letterboxed(
                        Pos2::new(src_p1.x, src_p1.y),
                        Pos2::new(src_p2.x, src_p2.y),
                        Pos2::new(rect.top(), rect.left()),
                        Pos2::new(rect.bottom(), rect.right()),
                        false,
                    )
                };

                let painter = ui.painter_at(rect);
                draw_grid(self, &painter);
                draw_game(self, &painter);
            }
            Tab::Stopwatch => draw_timings(self, ui),
            Tab::Settings => draw_settings(self, ui),
            Tab::OverTheAirProgramming => draw_over_the_air(self, ui),
            Tab::Motors => draw_motors(self, ui),
            Tab::RobotDisplay => {
                let rect = ui.max_rect();

                let wts = Transform::new_letterboxed(
                    Pos2::new(-1.0, 1.0),
                    Pos2::new(
                        ROBOT_DISPLAY_HEIGHT as f32 + 1.0,
                        ROBOT_DISPLAY_WIDTH as f32 + 1.0,
                    ),
                    Pos2::new(rect.top(), rect.left()),
                    Pos2::new(rect.bottom(), rect.right()),
                    false,
                );

                let painter = ui.painter_at(rect);

                painter.rect(
                    Rect::from_two_pos(
                        wts.map_point(Pos2::new(-1.0, 1.0)),
                        wts.map_point(Pos2::new(
                            ROBOT_DISPLAY_HEIGHT as f32 + 1.0,
                            ROBOT_DISPLAY_WIDTH as f32 + 1.0,
                        )),
                    ),
                    Rounding::ZERO,
                    Color32::BLACK,
                    Stroke::new(1.0, Color32::RED),
                );

                if let Some(display) =
                    &self.server_status.robots[self.ui_settings.selected_robot as usize].display
                {
                    for (y, row) in display.iter().enumerate() {
                        for x in 0u128..128 {
                            if (row >> x) % 2 == 1 {
                                painter.rect(
                                    Rect::from_two_pos(
                                        wts.map_point(Pos2::new(y as f32, x as f32)),
                                        wts.map_point(Pos2::new(y as f32 + 1.0, x as f32 + 1.0)),
                                    ),
                                    Rounding::ZERO,
                                    Color32::WHITE,
                                    Stroke::NONE,
                                );
                            }
                        }
                    }
                }
            }
            Tab::RobotButtonPanel => {
                let rect = ui.max_rect();

                let wts = Transform::new_letterboxed(
                    Pos2::new(0.0, 0.0),
                    Pos2::new(4.0, 10.0),
                    Pos2::new(rect.top(), rect.left()),
                    Pos2::new(rect.bottom(), rect.right()),
                    false,
                );
                self.robot_buttons_wts = wts;

                let painter = ui.painter_at(rect);

                painter.rect(
                    Rect::from_two_pos(
                        wts.map_point(Pos2::new(0.0, 0.0)),
                        wts.map_point(Pos2::new(4.0, 10.0)),
                    ),
                    Rounding::ZERO,
                    Color32::BLACK,
                    Stroke::NONE,
                );

                for (x, y) in [
                    (8.0, 1.0),
                    (9.0, 2.0),
                    (7.0, 2.0),
                    (8.0, 3.0),
                    (4.3, 3.0),
                    (5.7, 3.0),
                ] {
                    painter.circle_filled(
                        wts.map_point(Pos2::new(y, x)),
                        wts.map_dist(0.4),
                        Color32::WHITE,
                    );
                }
            }
            Tab::Keybindings => {
                ui.label("General");
                ui.label("[Left click] Set simulated robot position");
                ui.label("[Right click] Set target position");
                ui.label("[Y] Toggle rotated grid");
                ui.label("[P] Toggle selected robot connection");
                ui.separator();
                ui.label("Movement, relative to estimated location");
                ui.label("[W] Up");
                ui.label("[A] Left");
                ui.label("[S] Down");
                ui.label("[D] Right");
                ui.label("[Q] Rotate counterclockwise");
                ui.label("[E] Rotate clockwise");
                ui.separator();
                ui.label("Test raw motor control");
                ui.label("[U] First motor forwards");
                ui.label("[J] First motor backwards");
                ui.label("[I] Second motor forwards");
                ui.label("[K] Second motor backwards");
                ui.label("[O] Third motor forwards");
                ui.label("[L] Third motor backwards");
                ui.separator();
                ui.label("Gameplay");
                ui.label("[space] Pause/unpause");
                ui.label("[R] Reset pacman game");
                ui.separator();
                ui.label("Strategy");
                ui.label("[Z] Manual");
                ui.label("[X] AI");
                ui.label("[C] Test uniform");
                ui.label("[V] Test forwards");
                ui.separator();
                ui.label("Grid");
                ui.label("[B] Pacman grid");
                ui.label("[N] Playground grid");
                ui.separator();
                ui.label("CV position source");
                ui.label("[T] Mouse pointer");
                ui.label("[G] Game state");
                ui.label("[H] Particle filter");
                // ui.separator();
                // ui.label("Replay controls");
                // ui.label("[shift + left] Go to beginning");
                // ui.label("[left] Previous frame");
                // ui.label("[space] Pause/unpause");
                // ui.label("[right] Next frame");
                // ui.label("[shift + right] Go to end");
            }
            Tab::ExtraOpts => {
                draw_extra_opts(self, ui);
            }
            Tab::Imu => {
                draw_imu_data(self, ui);
            }
            _ => {
                ui.label(self.title(tab));
            }
        };
    }
}