Repository navigation
Expand file tree
/
Copy pathgui.py
More file actions
435 lines (299 loc) · 7.52 KB
/
Copy pathgui.py
File metadata and controls
435 lines (299 loc) · 7.52 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import tkinter as tk
from tkinter import filedialog
from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg,
NavigationToolbar2Tk
)
from TelemetryReader import TelemetryReader
from graphics import TelemetryGraph
# Main application window
homeWindow = tk.Tk()
homeWindow.title("iRacing Telemetry")
homeWindow.geometry("1200x800")
# Session and graph state
session = None
incidentList = []
graph = None
highlightedRegion = None
# Control panel
select_items_container = tk.Frame(
homeWindow,
bg="white",
padx=5,
pady=5
)
select_items_container.pack(
side=LEFT,
fill=Y
)
# Graph display area
graph_container = tk.Frame(homeWindow)
graph_container.pack(
side=RIGHT,
fill=BOTH,
expand=True
)
# Matplotlib figure and axes
figure, ax = plt.subplots()
# Embed the Matplotlib figure in the Tkinter window
canvas = FigureCanvasTkAgg(
figure,
master=graph_container
)
canvas.draw()
canvas.get_tk_widget().pack(
side=TOP,
fill=BOTH,
expand=True
)
# Graph navigation toolbar
toolbar = NavigationToolbar2Tk(
canvas,
graph_container
)
toolbar.update()
def file_button():
"""Load an IBT file and initialize the telemetry session and incident list."""
global session
global incidentList
global graph
global highlightedRegion
file_path = filedialog.askopenfilename(
parent=homeWindow,
title="Select a File",
filetypes=[
("IBT files", "*.ibt"),
("All files", "*.*")
]
)
if not file_path:
return
reader = TelemetryReader()
session = reader.loadFile(file_path)
fileTitle.config(text=file_path)
incidentList = session.detectIncidents()
graph = TelemetryGraph(session)
incidentListBox.delete(0, END)
for incident in incidentList:
incidentListBox.insert(
END,
str(incident.toString())
)
highlightedRegion = None
select_graph_type()
def select_graph_type():
"""Display the selected telemetry graph while preserving the current time range."""
global highlightedRegion
if graph is None:
return
selected = button_selected.get()
# Save the current time range before replacing the graph.
keepZoom = len(ax.lines) > 0
if keepZoom:
oldXLim = ax.get_xlim()
if selected == 1:
graph.graphPedals(ax)
elif selected == 2:
graph.graphSteering(ax)
elif selected == 3:
graph.graphSlipAngles(ax)
elif selected == 4:
graph.graphLateralAccelerationDisparities(ax)
# Restore the previous time range and rescale the y-axis.
if keepZoom:
ax.set_xlim(oldXLim)
autoscale_y(
oldXLim[0],
oldXLim[1]
)
highlightedRegion = None
selection = incidentListBox.curselection()
if selection:
highlightGraph(selection)
figure.tight_layout()
canvas.draw_idle()
def getTelemetryLines():
"""Return telemetry plot lines, excluding vertical lap markers."""
telemetryLines = []
for line in ax.lines:
xData = line.get_xdata()
# Lap markers contain fewer data points than telemetry lines.
if len(xData) > 2:
telemetryLines.append(line)
return telemetryLines
def autoscale_y(startTime, endTime):
"""Adjust the y-axis to fit telemetry values within the specified time range."""
telemetryLines = getTelemetryLines()
visibleYValues = []
for line in telemetryLines:
xData = line.get_xdata()
yData = line.get_ydata()
for x, y in zip(xData, yData):
if startTime <= x <= endTime:
visibleYValues.append(y)
if not visibleYValues:
return
minY = min(visibleYValues)
maxY = max(visibleYValues)
yRange = maxY - minY
# Add 15% padding, or a fixed amount for constant values.
if yRange == 0:
yPadding = 1
else:
yPadding = yRange * 0.15
ax.set_ylim(
minY - yPadding,
maxY + yPadding
)
def highlightGraph(incident_index):
"""Highlight the time range of the selected incident on the graph."""
global highlightedRegion
if not incident_index:
return
if session is None:
return
incident = incidentList[
incident_index[0]
]
# Remove the previous highlight before displaying a new one.
if highlightedRegion is not None:
try:
highlightedRegion.remove()
# The previous highlight may have been removed when the axes were cleared.
except ValueError:
pass
highlightedRegion = ax.axvspan(
incident.startTime,
incident.endTime,
color="red",
alpha=0.25
)
canvas.draw_idle()
def focusIncident(incident_index):
"""Zoom into the selected incident and adjust the y-axis to fit its data."""
if not incident_index:
return
if session is None:
return
incident = incidentList[
incident_index[0]
]
# Include two seconds of data before and after the incident.
xPadding = 2
startTime = (
incident.startTime
- xPadding
)
endTime = (
incident.endTime
+ xPadding
)
ax.set_xlim(
startTime,
endTime
)
autoscale_y(
startTime,
endTime
)
canvas.draw_idle()
def resetGraph():
"""Restore the full graph view while retaining the selected incident."""
if graph is None:
return
selected = button_selected.get()
if selected == 1:
graph.graphPedals(ax)
elif selected == 2:
graph.graphSteering(ax)
elif selected == 3:
graph.graphSlipAngles(ax)
elif selected == 4:
graph.graphLateralAccelerationDisparities(ax)
global highlightedRegion
highlightedRegion = None
selection = incidentListBox.curselection()
if selection:
highlightGraph(selection)
figure.tight_layout()
canvas.draw_idle()
# Graph selection controls
button_selected = IntVar(
homeWindow,
1
)
graph_types = {
"Pedals": 1,
"Steering wheel angle": 2,
"Slip angle": 3,
"LatAccel Disparities": 4
}
for text, value in graph_types.items():
Radiobutton(
select_items_container,
text=text,
variable=button_selected,
value=value,
indicator=0,
background="white",
width=20,
command=select_graph_type
).pack(
side=TOP,
ipady=5
)
# File selection controls
fileTitle = tk.Label(
select_items_container,
text="No file selected",
bg="white",
wraplength=180
)
fileTitle.pack(
pady=10
)
selectFileButton = tk.Button(
select_items_container,
text="Select .ibt file",
command=file_button
)
selectFileButton.pack(
padx=10,
pady=10
)
resetButton = tk.Button(
select_items_container,
text="Reset Graph",
command=resetGraph
)
resetButton.pack(
padx=10,
pady=5
)
# Incident selection list
incidentListBox = tk.Listbox(
select_items_container,
height=20,
width=20
)
# Single-click highlights an incident; double-click zooms into it.
incidentListBox.bind(
"<<ListboxSelect>>",
lambda event: highlightGraph(
incidentListBox.curselection()
)
)
incidentListBox.bind(
"<Double-Button-1>",
lambda event: focusIncident(
incidentListBox.curselection()
)
)
incidentListBox.pack(
side=BOTTOM,
pady=10
)
# Start the application
homeWindow.mainloop()