-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLmpGUI.py
More file actions
546 lines (414 loc) · 16.2 KB
/
LmpGUI.py
File metadata and controls
546 lines (414 loc) · 16.2 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# MIT License
#
# Copyright (c) 2020 Damian Choptiany
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import ac
import acsys
import platform
import os
import configparser
if platform.architecture()[0] == "64bit":
sysdir = os.path.dirname(__file__)+'/stdlib64'
else:
sysdir = os.path.dirname(__file__)+'/stdlib'
sys.path.insert(0, sysdir)
os.environ['PATH'] = os.environ['PATH'] + ";."
from lmpgui_lib.sim_info import info
configPath = "apps/python/LmpGUI/settings.ini"
config = configparser.ConfigParser()
config.read(configPath)
appWindow = 0
label_laptime = 0
label_delta = 0
label_speed = 0
label_gear = 0
label_fuel = 0
label_tc = 0
label_abs = 0
label_brakeBias = 0
label_ERScurrent = 0
label_deploy = 0
label_estimatedLaps = 0
texture_checkeredFlag = 0
fuelAmount = 0
fuelAmountStart = 0
totalFuelBurnt = 0
maxRPM = 0
minRPMRel = 0
hasERSorKERS = 0
drsAvailable = 0
drsActive = 0
spinner_scale = 0
button_indicators = 0
button_speedInMPH = 0
button_settingsVisible = 0
settingsVisible = 0
scale = 1
scaleUpdate = False
indicatorsON = True
speedInMPH = False
indicatorsCoordinates = [(), (), (), ()]
lastLapCount = -1
lapCount = 0
lapsNotInPitCount = 0
wasInPit = 0
flagType = 0
timerFlag = 0
timer60perSecond = 0
timer10perSecond = 0
timer1perSecond = 0
def loadSettings():
global config, scale, indicatorsON, speedInMPH, minRPMRel
scale = config.getfloat("LMPGUI", "scale") / 100.0
indicatorsON = config.getboolean("LMPGUI", "indicatorsON")
speedInMPH = config.getboolean("LMPGUI", "speedInMPH")
minRPMRel = config.getfloat("LMPGUI", "minRPMRel") / 100.0
def saveSettings():
global config, scale, indicatorsON
config.set("LMPGUI", "scale", str(scale * 100.0))
config.set("LMPGUI", "indicatorsON", str(indicatorsON))
config.set("LMPGUI", "speedInMPH", str(speedInMPH))
config.set("LMPGUI", "minRPMRel", str(minRPMRel * 100.0))
with open(configPath, "w") as configFile:
config.write(configFile)
def calculateEstimatedLaps():
global totalFuelBurnt, fuelAmountStart, fuelAmount, lapsNotInPitCount
currentLapProgress = ac.getCarState(0, acsys.CS.NormalizedSplinePosition)
totalDistance = lapsNotInPitCount + currentLapProgress
if totalDistance == 0:
return 0
fuelPerLap = (totalFuelBurnt + (fuelAmountStart - fuelAmount)) / totalDistance
if fuelPerLap == 0:
return 0
return fuelAmount / fuelPerLap
def calculateDeploy():
currentKERS = info.physics.kersCurrentKJ * 1000
maxKERS = ac.getCarState(0, acsys.CS.ERSMaxJ)
return int(currentKERS * 100 / maxKERS)
def formatTime(milliseconds):
seconds = int(milliseconds / 1000)
milliseconds -= seconds * 1000
minutes = int(seconds / 60)
seconds -= minutes * 60
return "{}:{:02d}:{:03d}".format(minutes, seconds, milliseconds)
def formatGear(gear):
if gear == 0:
return "R"
if gear == 1:
return "N"
return str(gear - 1)
def formatSwitchFromBool(switchState):
return "ON" if switchState else "OFF"
def getSpeed():
global speedInMPH
return ac.getCarState(0, acsys.CS.SpeedMPH) if speedInMPH else ac.getCarState(0, acsys.CS.SpeedKMH)
def drawRPMLights():
global scale, maxRPM, minRPMRel
currentRPM = ac.getCarState(0, acsys.CS.RPM)
numberOfLights = round(12 * ((currentRPM / maxRPM) - minRPMRel) / (1 - minRPMRel))
numberOfLights = min(numberOfLights, 12)
for i in range(numberOfLights):
if i < 6:
ac.glColor4f(0, 1, 0, 1)
elif i < 9:
ac.glColor4f(1, 1, 0, 1)
else:
ac.glColor4f(1, 0, 0, 1)
ac.glQuad((20 + i * 38.5) * scale, 13 * scale, 36 * scale, 62 * scale)
def drawFlag():
global scale, flagType, timerFlag, texture_checkeredFlag
if flagType == 0: #no flag
return
if flagType < 5 and timerFlag < 1:
if flagType == 1: #blue flag
ac.glColor4f(0, 0, 1, 1)
elif flagType == 2: #yellow flag
ac.glColor4f(1, 1, 0, 1)
elif flagType == 3: #black flag
ac.glColor4f(0, 0, 0, 1)
elif flagType == 4: #white flag
ac.glColor4f(1, 1, 1, 1)
ac.glQuad(-20 * scale, 0, 20 * scale, 383 * scale)
ac.glQuad(503 * scale, 0, 20 * scale, 383 * scale)
elif flagType == 5: #checkered flag
ac.glColor4f(1, 1, 1, 1)
ac.glQuadTextured(-20 * scale, 0, 20 * scale, 383 * scale, texture_checkeredFlag)
ac.glQuadTextured(503 * scale, 0, 20 * scale, 383 * scale, texture_checkeredFlag)
elif flagType == 6: #penalty flag
if timerFlag < 1:
ac.glColor4f(0, 0, 0, 1)
ac.glQuad(-20 * scale, 0, 20 * scale, 383 * scale)
ac.glColor4f(1, 1, 1, 1)
ac.glQuad(503 * scale, 0, 20 * scale, 383 * scale)
else:
ac.glColor4f(1, 1, 1, 1)
ac.glQuad(0, 0, 20 * scale, 383 * scale)
ac.glColor4f(0, 0, 0, 1)
ac.glQuad(503 * scale, 0, 20 * scale, 383 * scale)
def drawTyresIndicators():
global scale, indicatorsCoordinates, indicatorsON
if not indicatorsON:
return
wheelSlip = info.physics.wheelSlip
for wheelIndex in range(4):
x, y = indicatorsCoordinates[wheelIndex]
if wheelSlip[wheelIndex] < 1:
continue
elif wheelSlip[wheelIndex] < 20: #tyre spin
if wheelSlip[wheelIndex] < 2:
numberOfLights = 1
elif wheelSlip[wheelIndex] < 3:
numberOfLights = 2
else:
numberOfLights = 3
ac.glColor4f(0, 0, 1, 1)
if wheelIndex % 2 == 0: #left wheels
for n in range(numberOfLights):
ac.glQuad(x + n * 75 * scale, y, 75 * scale, 15 * scale)
else: #right wheels
for n in range(numberOfLights):
ac.glQuad(503 * scale - (n + 1) * 75 * scale, y, 75 * scale, 15 * scale)
else: #tyre lock
ac.glColor4f(1, 0, 0, 1)
ac.glQuad(x, y, 225 * scale, 15 * scale)
def drawDrsIndicator():
global scale, drsAvailable, drsActive
if not drsAvailable: #DRS is unavailable
return
if drsActive: #DRS is active
ac.glColor4f(0.1, 0.1, 1, 1)
else: #DRS is available
ac.glColor4f(0.9, 0.75, 0, 1)
ac.glQuad(0, -14 * scale, 503 * scale, 14 * scale)
def updateScale():
global appWindow, label_laptime, label_delta, label_speed, label_gear, label_fuel, label_tc, label_abs
global label_brakeBias, label_ERScurrent, label_deploy, label_estimatedLaps
global spinner_scale, scale, scaleUpdate, button_settingsVisible, button_speedInMPH, button_indicators, indicatorsCoordinates
if not scaleUpdate:
return
scaleUpdate = False
ac.setSize(appWindow, 503 * scale, 383 * scale)
ac.setPosition(label_laptime, 239 * scale, 86 * scale)
ac.setFontSize(label_laptime, 40 * scale)
ac.setPosition(label_delta, 239 * scale, 155 * scale)
ac.setFontSize(label_delta, 40 * scale)
ac.setPosition(label_speed, 69 * scale, 220 * scale)
ac.setFontSize(label_speed, 40 * scale)
ac.setPosition(label_gear, 318 * scale, 226 * scale)
ac.setFontSize(label_gear, 90 * scale)
ac.setPosition(label_fuel, 195 * scale, 220 * scale)
ac.setFontSize(label_fuel, 40 * scale)
ac.setPosition(label_abs, 438 * scale, 302 * scale)
ac.setFontSize(label_abs, 40 * scale)
ac.setPosition(label_brakeBias, 438 * scale, 220 * scale)
ac.setFontSize(label_brakeBias, 40 * scale)
ac.setPosition(label_tc, 69 * scale, 302 * scale)
ac.setFontSize(label_tc, 40 * scale)
ac.setPosition(label_ERScurrent, 478 * scale, 86 * scale)
ac.setFontSize(label_ERScurrent, 40 * scale)
ac.setPosition(label_deploy, 478 * scale, 155 * scale)
ac.setFontSize(label_deploy, 40 * scale)
ac.setPosition(label_estimatedLaps, 195 * scale, 302 * scale)
ac.setFontSize(label_estimatedLaps, 40 * scale)
ac.setPosition(spinner_scale, 0, 383 * scale)
ac.setFontSize(spinner_scale, 20 * scale)
ac.setSize(spinner_scale, 201 * scale, 40 * scale)
ac.setPosition(button_indicators, 302 * scale, 383 * scale)
ac.setFontSize(button_indicators, 20 * scale)
ac.setSize(button_indicators, 201 * scale, 40 * scale)
ac.setPosition(button_speedInMPH, 201 * scale, 383 * scale)
ac.setFontSize(button_speedInMPH, 20 * scale)
ac.setSize(button_speedInMPH, 101 * scale, 40 * scale)
ac.setSize(button_settingsVisible, 503 * scale, 383 * scale)
indicatorsCoordinates = [(0, -15 * scale), (278 * scale, -15 * scale), (0, 383 * scale), (278 * scale, 383 * scale)]
def acMain(ac_version):
global appWindow, label_laptime, label_delta, label_speed, label_gear, label_fuel, label_tc, label_abs
global label_brakeBias, label_ERScurrent, label_deploy, label_estimatedLaps, texture_checkeredFlag
global spinner_scale, scale, scaleUpdate, button_settingsVisible, button_speedInMPH, button_indicators, settingsVisible
loadSettings()
ac.initFont(0, "Ubuntu", 0, 1)
appWindow = ac.newApp("LMP GUI")
ac.setTitle(appWindow, "")
ac.setIconPosition(appWindow, -7000, -3000)
ac.drawBorder(appWindow, 0)
ac.setBackgroundTexture(appWindow, "apps/python/LmpGUI/images/gui.png")
label_laptime = ac.addLabel(appWindow, "00:00:000")
ac.setCustomFont(label_laptime, "Ubuntu", 0, 1)
ac.setFontColor(label_laptime, 0.2, 0.63, 0.2, 1)
ac.setFontAlignment(label_laptime, "right")
label_delta = ac.addLabel(appWindow, "0.000")
ac.setCustomFont(label_delta, "Ubuntu", 0, 1)
ac.setFontColor(label_delta, 0.78, 0.78, 0.78, 1)
ac.setFontAlignment(label_delta, "right")
label_speed = ac.addLabel(appWindow, "0")
ac.setCustomFont(label_speed, "Ubuntu", 0, 1)
ac.setFontColor(label_speed, 0.78, 0.78, 0.78, 1)
ac.setFontAlignment(label_speed, "center")
label_gear = ac.addLabel(appWindow, "N")
ac.setCustomFont(label_gear, "Ubuntu", 0, 1)
ac.setFontColor(label_gear, 0.82, 0.78, 0, 1)
ac.setFontAlignment(label_gear, "center")
label_fuel = ac.addLabel(appWindow, "0.0")
ac.setCustomFont(label_fuel, "Ubuntu", 0, 1)
ac.setFontColor(label_fuel, 0.82, 0.78, 0, 1)
ac.setFontAlignment(label_fuel, "center")
label_tc = ac.addLabel(appWindow, "-")
ac.setCustomFont(label_tc, "Ubuntu", 0, 1)
ac.setFontColor(label_tc, 0.78, 0.78, 0.78, 1)
ac.setFontAlignment(label_tc, "center")
label_abs = ac.addLabel(appWindow, "-")
ac.setCustomFont(label_abs, "Ubuntu", 0, 1)
ac.setFontColor(label_abs, 0.23, 0.31, 0.69, 1)
ac.setFontAlignment(label_abs, "center")
label_brakeBias = ac.addLabel(appWindow, "0")
ac.setCustomFont(label_brakeBias, "Ubuntu", 0, 1)
ac.setFontColor(label_brakeBias, 0.23, 0.31, 0.69, 1)
ac.setFontAlignment(label_brakeBias, "center")
label_ERScurrent = ac.addLabel(appWindow, "-")
ac.setCustomFont(label_ERScurrent, "Ubuntu", 0, 1)
ac.setFontColor(label_ERScurrent, 0.05, 0.62, 0.65, 1)
ac.setFontAlignment(label_ERScurrent, "right")
label_deploy = ac.addLabel(appWindow, "-")
ac.setCustomFont(label_deploy, "Ubuntu", 0, 1)
ac.setFontColor(label_deploy, 0.05, 0.62, 0.65, 1)
ac.setFontAlignment(label_deploy, "right")
label_estimatedLaps = ac.addLabel(appWindow, "-")
ac.setCustomFont(label_estimatedLaps, "Ubuntu", 0, 1)
ac.setFontColor(label_estimatedLaps, 0.82, 0.78, 0, 1)
ac.setFontAlignment(label_estimatedLaps, "center")
spinner_scale = ac.addSpinner(appWindow, "")
ac.setRange(spinner_scale, 10, 250)
ac.setStep(spinner_scale, 10)
ac.addOnValueChangeListener(spinner_scale, onSpinnerScaleValueChanged)
ac.setVisible(spinner_scale, 0)
button_settingsVisible = ac.addButton(appWindow, "")
ac.setPosition(button_settingsVisible, 0, 0)
ac.setBackgroundOpacity(button_settingsVisible, 0)
ac.addOnClickedListener(button_settingsVisible, onSettingsVisibleButtonClicked)
ac.drawBorder(button_settingsVisible, 0)
button_indicators = ac.addButton(appWindow, "Wheel indicators: {}".format(formatSwitchFromBool(indicatorsON)))
ac.addOnClickedListener(button_indicators, onIndicatorsButtonClicked)
ac.setVisible(button_indicators, 0)
button_speedInMPH = ac.addButton(appWindow, "MPH: {}".format(formatSwitchFromBool(speedInMPH)))
ac.addOnClickedListener(button_speedInMPH, onSpeedInMPHButtonClicked)
ac.setVisible(button_speedInMPH, 0)
ac.setValue(spinner_scale, scale * 100)
texture_checkeredFlag = ac.newTexture("apps/python/LmpGUI/images/checkeredFlag.png")
ac.addRenderCallback(appWindow, onFormRender)
scaleUpdate = True
updateScale()
return "LMP GUI"
def onSettingsVisibleButtonClicked(*args):
global spinner_scale, button_indicators, settingsVisible
settingsVisible = not settingsVisible
ac.setVisible(spinner_scale, settingsVisible)
ac.setVisible(button_indicators, settingsVisible)
ac.setVisible(button_speedInMPH, settingsVisible)
def onIndicatorsButtonClicked(*args):
global button_indicators, indicatorsON
indicatorsON = not indicatorsON
ac.setText(button_indicators, "Wheel indicators: {}".format(formatSwitchFromBool(indicatorsON)))
def onSpinnerScaleValueChanged(*args):
global scale, scaleUpdate
scale = ac.getValue(spinner_scale) / 100
scaleUpdate = True
def onSpeedInMPHButtonClicked(*args):
global button_speedInMPH, speedInMPH
speedInMPH = not speedInMPH
ac.setText(button_speedInMPH, "MPH: {}".format(formatSwitchFromBool(speedInMPH)))
def onFormRender(deltaT):
drawRPMLights()
drawFlag()
drawTyresIndicators()
drawDrsIndicator()
updateScale()
def acUpdate(deltaT):
global label_laptime, label_delta, label_speed, label_gear, label_fuel, label_tc, label_abs, label_brakeBias, label_ERScurrent, label_deploy, label_estimatedLaps
global fuelAmount, fuelAmountStart, totalFuelBurnt, maxRPM, hasERSorKERS, lapCount, lastLapCount, lapsNotInPitCount, wasInPit, flagType, drsAvailable, drsActive
global timerFlag, timer60perSecond, timer10perSecond, timer1perSecond
timerFlag += deltaT
timer60perSecond += deltaT
timer10perSecond += deltaT
timer1perSecond += deltaT
if timer60perSecond > 0.0167:
timer60perSecond = 0
lapTime = ac.getCarState(0, acsys.CS.LapTime)
lapCount = ac.getCarState(0, acsys.CS.LapCount)
delta = ac.getCarState(0, acsys.CS.PerformanceMeter)
speed = getSpeed()
gear = ac.getCarState(0, acsys.CS.Gear)
drsAvailable = ac.getCarState(0, acsys.CS.DrsAvailable)
drsActive = ac.getCarState(0, acsys.CS.DrsEnabled)
fuelAmount = info.physics.fuel
if(lapTime > 5000 or lapCount == 0):
ac.setText(label_laptime, formatTime(lapTime))
deltaString = "{:.3f}".format(delta)
if delta == 0:
ac.setFontColor(label_delta, 0.78, 0.78, 0.78, 1)
elif delta > 0:
ac.setFontColor(label_delta, 1, 0, 0, 1)
deltaString = "+" + deltaString
else:
ac.setFontColor(label_delta, 0.2, 0.63, 0.2, 1)
if hasERSorKERS:
ERSValue = info.physics.kersCharge * 100
ac.setText(label_ERScurrent, str(int(ERSValue)))
deployValue = calculateDeploy()
ac.setText(label_deploy, str(deployValue))
ac.setText(label_delta, deltaString)
ac.setText(label_speed, str(int(speed)))
ac.setText(label_gear, formatGear(gear))
if timer10perSecond > 0.167:
timer10perSecond = 0
tcValue = info.physics.tc
absValue = info.physics.abs
brakeBias = info.physics.brakeBias * 100
ac.setText(label_tc, formatSwitchFromBool(tcValue))
ac.setText(label_abs, formatSwitchFromBool(absValue))
flagType = info.graphics.flag
ac.setText(label_brakeBias, "{:.1f}".format(brakeBias))
ac.setText(label_fuel, "{:.1f}".format(fuelAmount))
if timerFlag > 2:
timerFlag = 0
if timer1perSecond > 1:
timer1perSecond = 0
hasERS = info.static.hasERS
hasKERS = info.static.hasKERS
if hasERS or hasKERS:
hasERSorKERS = 1
maxRPM = info.static.maxRpm
if info.graphics.isInPit:
wasInPit = 1
if lastLapCount != lapCount:
lastLapCount = lapCount
if lapCount > 0 and not wasInPit:
fuelBurnt = fuelAmountStart - fuelAmount
totalFuelBurnt += fuelBurnt
lapsNotInPitCount += 1
fuelAmountStart = fuelAmount
wasInPit = 0
estimatedLaps = calculateEstimatedLaps()
if lapCount > 0 and estimatedLaps > 0:
ac.setText(label_estimatedLaps, "{:.1f}".format(estimatedLaps))
else:
ac.setText(label_estimatedLaps, "-")
def acShutdown():
saveSettings()