-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
410 lines (348 loc) · 15.5 KB
/
ui.py
File metadata and controls
410 lines (348 loc) · 15.5 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import pygame
from constants import (
COLOR_BUTTON, COLOR_BUTTON_HOVER, COLOR_BUTTON_TEXT, COLOR_TEXT,
COLOR_PANEL_BG, COLOR_TOGGLE_ACTIVE, COLOR_TOGGLE_INACTIVE,
COLOR_BAR_BFS, COLOR_BAR_DIJKSTRA, COLOR_BAR_ASTAR,
COLOR_BAR_GREEDY, COLOR_BAR_RRT, COLOR_BAR_RRT_STAR,
GRID_AREA_WIDTH, WINDOW_HEIGHT, PANEL_WIDTH,
)
pygame.font.init()
FONT_SMALL = None
FONT_MEDIUM = None
FONT_LARGE = None
def _get_fonts():
global FONT_SMALL, FONT_MEDIUM, FONT_LARGE
if FONT_SMALL is None:
FONT_SMALL = pygame.font.SysFont("consolas", 14)
FONT_MEDIUM = pygame.font.SysFont("consolas", 16)
FONT_LARGE = pygame.font.SysFont("consolas", 20)
return FONT_SMALL, FONT_MEDIUM, FONT_LARGE
ALL_ALGORITHMS = ["BFS", "Dijkstra", "A*", "Greedy", "RRT", "RRT*"]
ALGO_COLORS = {
"BFS": COLOR_BAR_BFS,
"Dijkstra": COLOR_BAR_DIJKSTRA,
"A*": COLOR_BAR_ASTAR,
"Greedy": COLOR_BAR_GREEDY,
"RRT": COLOR_BAR_RRT,
"RRT*": COLOR_BAR_RRT_STAR,
}
class Button:
def __init__(self, x, y, width, height, text, callback=None):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.callback = callback
self.hovered = False
self.enabled = True
def handle_event(self, event) -> bool:
if not self.enabled:
return False
if event.type == pygame.MOUSEMOTION:
self.hovered = self.rect.collidepoint(event.pos)
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.rect.collidepoint(event.pos):
if self.callback:
self.callback()
return True
return False
def draw(self, surface) -> None:
_, font_med, _ = _get_fonts()
color = COLOR_BUTTON_HOVER if self.hovered else COLOR_BUTTON
if not self.enabled:
color = (50, 50, 60)
pygame.draw.rect(surface, color, self.rect, border_radius=6)
pygame.draw.rect(surface, (120, 120, 160), self.rect, 1, border_radius=6)
text_surf = font_med.render(self.text, True, COLOR_BUTTON_TEXT)
text_rect = text_surf.get_rect(center=self.rect.center)
surface.blit(text_surf, text_rect)
class AlgorithmToggle:
"""Two-row toggle: 3 algorithms per row."""
def __init__(self, x, y, width, row_height):
self.x = x
self.y = y
self.width = width
self.row_height = row_height
self.rows = [
["BFS", "Dijkstra", "A*"],
["Greedy", "RRT", "RRT*"],
]
self.selected_index = (0, 0) # (row, col)
self.enabled = True
self.cols_per_row = 3
self.seg_width = width // self.cols_per_row
self.total_height = row_height * len(self.rows) + 4
@property
def selected(self) -> str:
r, c = self.selected_index
return self.rows[r][c]
def handle_event(self, event) -> bool:
if not self.enabled:
return False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mx, my = event.pos
for ri, row in enumerate(self.rows):
row_y = self.y + ri * (self.row_height + 4)
if row_y <= my <= row_y + self.row_height:
for ci in range(len(row)):
seg_x = self.x + ci * self.seg_width
if seg_x <= mx <= seg_x + self.seg_width:
self.selected_index = (ri, ci)
return True
return False
def draw(self, surface) -> None:
font_sm, _, _ = _get_fonts()
for ri, row in enumerate(self.rows):
row_y = self.y + ri * (self.row_height + 4)
for ci, option in enumerate(row):
seg_x = self.x + ci * self.seg_width
rect = pygame.Rect(seg_x, row_y, self.seg_width, self.row_height)
is_selected = (ri, ci) == self.selected_index
color = COLOR_TOGGLE_ACTIVE if is_selected else COLOR_TOGGLE_INACTIVE
if not self.enabled:
color = (40, 40, 50)
pygame.draw.rect(surface, color, rect, border_radius=4)
pygame.draw.rect(surface, (100, 100, 120), rect, 1, border_radius=4)
algo_color = ALGO_COLORS.get(option, COLOR_TEXT)
text_color = algo_color if is_selected else (140, 140, 140)
text_surf = font_sm.render(option, True, text_color)
text_rect = text_surf.get_rect(center=rect.center)
surface.blit(text_surf, text_rect)
class AnalyticsPanel:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.results: dict[str, dict] = {}
# Scrollable region starts below status/instructions
self.scroll_top = 300
self.scroll_bottom = height - 10
self.scroll_view_h = self.scroll_bottom - self.scroll_top
self.scroll_offset = 0
self.content_height = 0 # measured each frame
# Chart scale (1.0 = default, 0.5 = half, 2.0 = double)
self.chart_scale = 1.0
self.SCALE_MIN = 0.5
self.SCALE_MAX = 3.0
self.SCALE_STEP = 0.25
# +/- button rects (drawn in fixed header)
btn_size = 24
self.btn_minus = pygame.Rect(x + width - 20 - btn_size * 2 - 8,
self.scroll_top - 30, btn_size, btn_size)
self.btn_plus = pygame.Rect(x + width - 20 - btn_size,
self.scroll_top - 30, btn_size, btn_size)
# Scrollbar state
self.scrollbar_width = 10
self.scrollbar_x = x + width - self.scrollbar_width - 4
self.dragging = False
self.drag_start_y = 0
self.drag_start_offset = 0
def update_result(self, algo_name: str, time_ms: float,
nodes_visited: int, path_length: float) -> None:
self.results[algo_name] = {
"time_ms": round(time_ms, 1),
"nodes": nodes_visited,
"path_len": round(path_length, 1),
}
def clear_results(self) -> None:
self.results.clear()
self.scroll_offset = 0
def handle_event(self, event) -> bool:
# Mouse wheel scrolling inside panel
if event.type == pygame.MOUSEWHEEL:
mx, my = pygame.mouse.get_pos()
if mx >= self.x and my >= self.scroll_top:
self.scroll_offset -= event.y * 20
self._clamp_scroll()
return True
# Chart scale +/- buttons
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mx, my = event.pos
if self.btn_plus.collidepoint(mx, my):
self.chart_scale = min(self.SCALE_MAX, self.chart_scale + self.SCALE_STEP)
return True
if self.btn_minus.collidepoint(mx, my):
self.chart_scale = max(self.SCALE_MIN, self.chart_scale - self.SCALE_STEP)
return True
# Scrollbar drag
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mx, my = event.pos
thumb_rect = self._get_thumb_rect()
if thumb_rect and thumb_rect.collidepoint(mx, my):
self.dragging = True
self.drag_start_y = my
self.drag_start_offset = self.scroll_offset
return True
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
if self.dragging:
self.dragging = False
return True
if event.type == pygame.MOUSEMOTION and self.dragging:
mx, my = event.pos
dy = my - self.drag_start_y
max_scroll = max(0, self.content_height - self.scroll_view_h)
if max_scroll > 0:
track_h = self.scroll_view_h
thumb_h = max(20, int(track_h * self.scroll_view_h / self.content_height))
usable = track_h - thumb_h
if usable > 0:
self.scroll_offset = self.drag_start_offset + int(dy * max_scroll / usable)
self._clamp_scroll()
return True
return False
def _clamp_scroll(self):
max_scroll = max(0, self.content_height - self.scroll_view_h)
self.scroll_offset = max(0, min(self.scroll_offset, max_scroll))
def _get_thumb_rect(self) -> pygame.Rect | None:
if self.content_height <= self.scroll_view_h:
return None
track_h = self.scroll_view_h
thumb_h = max(20, int(track_h * self.scroll_view_h / self.content_height))
usable = track_h - thumb_h
max_scroll = self.content_height - self.scroll_view_h
thumb_y = self.scroll_top + int(usable * self.scroll_offset / max_scroll) if max_scroll > 0 else self.scroll_top
return pygame.Rect(self.scrollbar_x, thumb_y, self.scrollbar_width, thumb_h)
def draw(self, surface, state_text: str = "", algo_text: str = "") -> None:
font_sm, font_med, font_lg = _get_fonts()
# Panel background
pygame.draw.rect(surface, COLOR_PANEL_BG,
(self.x, self.y, self.width, self.height))
pygame.draw.line(surface, (80, 80, 80),
(self.x, 0), (self.x, self.height), 2)
pad = 20
cx = self.x + pad
# --- Fixed header area (not scrolled) ---
# Status area
y = 160
status_color = (255, 60, 60) if "BLOCKED" in state_text else COLOR_TEXT
status_surf = font_med.render(f"State: {state_text}", True, status_color)
surface.blit(status_surf, (cx, y))
y += 24
algo_surf = font_med.render(f"Algorithm: {algo_text}", True, COLOR_TEXT)
surface.blit(algo_surf, (cx, y))
# Instructions / warnings
y += 36
if "BLOCKED" in state_text:
hint = font_sm.render("Click Start to re-plan!", True, (255, 200, 100))
surface.blit(hint, (cx, y))
elif state_text == "SETUP":
instructions = [
"Click grid to place:",
" 1st click = Start (green)",
" 2nd click = End (red)",
" Then click = Obstacles",
]
for line in instructions:
s = font_sm.render(line, True, (160, 160, 160))
surface.blit(s, (cx, y))
y += 18
# --- Chart scale controls ---
scale_label = font_sm.render(f"Chart Size", True, (150, 150, 150))
surface.blit(scale_label, (cx, self.scroll_top - 26))
for btn, label in [(self.btn_minus, "-"), (self.btn_plus, "+")]:
pygame.draw.rect(surface, (70, 70, 90), btn, border_radius=4)
pygame.draw.rect(surface, (110, 110, 130), btn, 1, border_radius=4)
lbl = font_med.render(label, True, COLOR_TEXT)
lr = lbl.get_rect(center=btn.center)
surface.blit(lbl, lr)
# --- Scrollable content area ---
# Render content onto a temp surface, then blit a clipped portion
content_w = self.width - self.scrollbar_width - 12
temp_h = 1000 # generous buffer
content_surf = pygame.Surface((content_w, temp_h), pygame.SRCALPHA)
content_surf.fill((0, 0, 0, 0))
cy = 0 # y position within content surface
local_cx = pad
# Title
title_surf = font_lg.render("Comparison", True, COLOR_TEXT)
content_surf.blit(title_surf, (local_cx, cy))
cy += 28
if self.results:
# Header
headers = ["Algo", "Time", "Nodes", "Path"]
col_widths = [80, 80, 80, 80]
for i, h in enumerate(headers):
hx = local_cx + sum(col_widths[:i])
s = font_sm.render(h, True, (180, 180, 180))
content_surf.blit(s, (hx, cy))
cy += 20
pygame.draw.line(content_surf, (80, 80, 80),
(local_cx, cy), (local_cx + 310, cy))
cy += 4
# Rows
for algo_name in ALL_ALGORITHMS:
if algo_name not in self.results:
continue
r = self.results[algo_name]
color = ALGO_COLORS.get(algo_name, COLOR_TEXT)
values = [algo_name, str(r["time_ms"]), str(r["nodes"]), str(r["path_len"])]
for i, v in enumerate(values):
vx = local_cx + sum(col_widths[:i])
s = font_sm.render(v, True, color)
content_surf.blit(s, (vx, cy))
cy += 20
# Bar charts
cy += 14
cy = self._draw_bar_charts(content_surf, local_cx, cy, font_sm, content_w - pad * 2)
# Record total content height
self.content_height = cy
self._clamp_scroll()
# Blit visible portion of the content surface
src_rect = pygame.Rect(0, self.scroll_offset, content_w, self.scroll_view_h)
surface.blit(content_surf, (self.x, self.scroll_top), src_rect)
# --- Scrollbar ---
if self.content_height > self.scroll_view_h:
# Track
track_rect = pygame.Rect(self.scrollbar_x, self.scroll_top,
self.scrollbar_width, self.scroll_view_h)
pygame.draw.rect(surface, (55, 55, 55), track_rect, border_radius=5)
# Thumb
thumb_rect = self._get_thumb_rect()
if thumb_rect:
thumb_color = (120, 120, 140) if self.dragging else (90, 90, 110)
pygame.draw.rect(surface, thumb_color, thumb_rect, border_radius=5)
def _draw_bar_charts(self, surface, cx, y, _font, available_w) -> int:
s = self.chart_scale
bar_h = max(6, int(12 * s))
row_h = max(10, int(16 * s))
title_gap = max(12, int(18 * s))
group_gap = max(6, int(8 * s))
label_offset = max(60, int(80 * s))
max_bar_width = available_w - label_offset - 20
# Scaled font for chart labels
chart_font_size = max(10, int(14 * s))
chart_font = pygame.font.SysFont("consolas", chart_font_size)
metrics = [
("Time (ms)", "time_ms"),
("Nodes Visited", "nodes"),
("Path Length", "path_len"),
]
for metric_label, metric_key in metrics:
title_s = chart_font.render(metric_label, True, COLOR_TEXT)
surface.blit(title_s, (cx, y))
y += title_gap
values = {}
for algo_name in ALL_ALGORITHMS:
if algo_name in self.results:
values[algo_name] = self.results[algo_name][metric_key]
max_val = max(values.values()) if values else 1
if max_val == 0:
max_val = 1
for algo_name in ALL_ALGORITHMS:
if algo_name not in values:
continue
val = values[algo_name]
bar_w = int((val / max_val) * max_bar_width)
bar_w = max(bar_w, 2)
color = ALGO_COLORS.get(algo_name, COLOR_TEXT)
label_s = chart_font.render(f"{algo_name:>8}", True, color)
surface.blit(label_s, (cx, y))
bar_x = cx + label_offset
bar_y_offset = max(1, (row_h - bar_h) // 2)
pygame.draw.rect(surface, color,
(bar_x, y + bar_y_offset, bar_w, bar_h),
border_radius=max(2, bar_h // 4))
val_s = chart_font.render(str(val), True, COLOR_TEXT)
surface.blit(val_s, (bar_x + bar_w + 4, y))
y += row_h
y += group_gap
return y