-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguimap.py
155 lines (120 loc) · 6.35 KB
/
guimap.py
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 26 16:27:31 2022
@author: RTB
"""
import tkinter
import tkinter.ttk
import constants
class GUIMap:
def __init__(self, logger, *args, **kwargs):
self.logger = logger
self.points = []
self.xz_ratio = 16000.0/11000.0
self.x_scale = 200.0
self.x_offset = 10000
self.z_scale = self.x_scale/self.xz_ratio
self.z_offset = 6000
self.map_canvas = None
def scale_point(self, x, z):
return ((x - self.x_offset)/self.x_scale,
(z - self.z_offset)/self.z_scale)
def update(self, fdp):
if fdp.is_race_on == 0:
return
x = fdp.position_x + 10000#(fdp.position_x + 10000)/16000.0
y = fdp.position_y
z = -fdp.position_z + 6000#(-fdp.position_z + 6000)/11000.0
#self.update_car_info_pointxyz(x,y,z)
self.points.append((x,y,z))
if len(self.points) == 1:
self.x_offset = self.points[0][0] - self.x_scale/2.0
self.z_offset = self.points[0][2] - self.z_scale/2.0
xs, zs = self.scale_point(x,z)
if xs < 0 or zs < 0 or xs > 1 or zs > 1:
self.center_map()
self.logger.info("Map Scale now {:.0f} and {:.0f}".format(self.x_scale, self.z_scale))
self.refresh_map()
elif len(self.points) % (60*30) == 0:
self.logger.info('Redrawing map!')
self.center_map()
self.refresh_map()
else:
self.map_canvas.create_line(xs*582, zs*400, xs*582+1, zs*400+1, fill='white')
def reset(self):
self.points = []
self.map_canvas.delete("all")
def set_canvas (self, frame):
# place map information canvas
self.map_canvas = tkinter.Canvas(frame, background=constants.background_color, bd=0,
highlightthickness=True, width=582, height=400)
# self.map_canvas.place(relx=0.6, rely=0.5,
# width=582, height=400,
# anchor=tkinter.CENTER)
# tkinter.Label(frame, text="Map information", bg=constants.background_color, fg=constants.text_color,
# font=('Helvetica 15 bold')).place(relx=0.5, rely=0.0, anchor=tkinter.NW)
# self.map_canvas.create_line(0,0, 582-1,0, 582-1,400-1, 0,400-1, 0,0, fill='white')
#todo: optimize so that only new points are checked by saving min/max
def center_map(self):
min_x, max_x, min_z, max_z = 16000,0,11000,0
for p in self.points:
min_x = p[0] if p[0] < min_x else min_x
max_x = p[0] if p[0] > max_x else max_x
min_z = p[2] if p[2] < min_z else min_z
max_z = p[2] if p[2] > max_z else max_z
new_x_scale = 1.25 * (max_x - min_x)
new_z_scale = 1.25 * (max_z - min_z)
if new_x_scale / self.x_scale > new_z_scale / self.z_scale:
self.x_scale = new_x_scale
self.z_scale = self.x_scale/self.xz_ratio
else:
self.z_scale = new_z_scale
self.x_scale = self.z_scale*self.xz_ratio
self.x_offset = (min_x + max_x) / 2 - self.x_scale/2.0
self.z_offset = (min_z + max_z) / 2 - self.z_scale/2.0
def refresh_map(self):
self.map_canvas.delete("all")
points = [self.scale_point(x,z) for x,y,z in self.points]
points = [[x*582, z*400] for x,z in points]
# newpoints = []
# prev = points[0]
# for p in points[1:]:
# if prev[0] != p[0] or prev[1] != p[1]:
# newpoints.append(p)
points = [item for sublist in points for item in sublist]
self.map_canvas.create_line(*points, fill='white')
# self.map_canvas.create_line(0,0, 582-1,0, 582-1,400-1, 0,400-1, 0,0, fill='white')
# def init_pointxyz(self):
# self.pointx = tkinter.StringVar()
# self.pointx.set("0")
# self.pointy = tkinter.StringVar()
# self.pointy.set("0")
# self.pointz = tkinter.StringVar()
# self.pointz.set("0")
# def update_car_info_pointxyz(self, x, y, z):
# self.pointx.set(f"{str(round(x, 3))}")
# self.pointy.set(f"{str(round(y, 3))}")
# self.pointz.set(f"{str(round(z, 3))}")
# def reset_car_info_pointxyz(self):
# self.pointx.set("0")
# self.pointy.set("0")
# self.pointz.set("0")
# def set_car_perf_frame_pointxyz(self):
# # place Pos x information text
# tkinter.Label(self.car_perf_frame, text="Pos x", bg=constants.background_color, fg=constants.text_color,
# font=('Helvetica 15 bold')).place(relx=0.0, rely=0.1, anchor=tkinter.NW)
# tkinter.Label(self.car_perf_frame, textvariable=self.pointx, bg=constants.background_color,
# fg=constants.text_color, font=('Helvetica 20 bold italic')).place(relx=0.0, rely=0.2,
# anchor=tkinter.NW)
# # place pos y information test
# tkinter.Label(self.car_perf_frame, text="Pos y", bg=constants.background_color, fg=constants.text_color,
# font=('Helvetica 15 bold')).place(relx=0.0, rely=0.3, anchor=tkinter.NW)
# tkinter.Label(self.car_perf_frame, textvariable=self.pointy, bg=constants.background_color,
# fg=constants.text_color, font=('Helvetica 20 bold italic')).place(relx=0.0, rely=0.4,
# anchor=tkinter.NW)
# # place pos z information test
# tkinter.Label(self.car_perf_frame, text="Pos z", bg=constants.background_color, fg=constants.text_color,
# font=('Helvetica 15 bold')).place(relx=0.0, rely=0.5, anchor=tkinter.NW)
# tkinter.Label(self.car_perf_frame, textvariable=self.pointz, bg=constants.background_color,
# fg=constants.text_color, font=('Helvetica 20 bold italic')).place(relx=0.0, rely=0.6,
# anchor=tkinter.NW)