-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath_Editor.py
More file actions
335 lines (239 loc) · 9.88 KB
/
Copy pathPath_Editor.py
File metadata and controls
335 lines (239 loc) · 9.88 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
import pygame
from Sprites import *
import pygame_gui
import os
from Planner import *
EDITOR_WIDTH = 900
EDITOR_HEIGHT = 900
DRAWING_WIDTH = 900
DRAWING_HEIGHT = 800
def initiate_ui():
PLANNER_DIR = os.path.realpath(os.path.dirname(__file__))
manager = pygame_gui.UIManager((900, 900), os.path.join(
PLANNER_DIR, "assets", "themes", "editor-theme.json"))
global panel
panel = pygame_gui.elements.UIPanel(
relative_rect=pygame.Rect(0, 0, 900, 100),
starting_layer_height=0,
manager=manager)
global title
title = pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(10, 18, -1, -1),
text="Path Editor",
manager=manager,
container=panel)
global clear_button
clear_button = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect(325, 18, 100, 50),
text="CLEAR",
manager=manager,
container=panel,
object_id="CLEAR_BUTTON")
global pos_x_text
pos_x_text = pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(440, 18, -1, -1),
text="X: ",
manager=manager,
container=panel)
global pos_x_entry
pos_x_entry = pygame_gui.elements.UITextEntryLine(
relative_rect=pygame.Rect(485, 18, 75, 50),
manager=manager,
container=panel,
object_id="#POS_X_TEXT_ENTRY")
pos_x_entry.set_allowed_characters('numbers')
pos_x_entry.set_text_length_limit(3)
global pos_y_text
pos_y_text = pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(570, 18, -1, -1),
text="Y: ",
manager=manager,
container=panel)
global pos_y_entry
pos_y_entry = pygame_gui.elements.UITextEntryLine(
relative_rect=pygame.Rect(620, 18, 75, 50),
manager=manager,
container=panel,
object_id="#POS_Y_TEXT_ENTRY")
global angle_text
angle_text = pygame_gui.elements.UILabel(
relative_rect=pygame.Rect(705, 18, -1, -1),
text="D: ",
manager=manager,
container=panel)
global angle_entry
angle_entry = pygame_gui.elements.UITextEntryLine(
relative_rect=pygame.Rect(750, 18, 100, 50),
manager=manager,
container=panel,
object_id="#POS_Y_TEXT_ENTRY")
pos_y_entry.set_allowed_characters('numbers')
pos_y_entry.set_text_length_limit(3)
return manager
def initiate_screen():
pygame.init()
flags = pygame.RESIZABLE | pygame.SCALED
display = pygame.display.Info()
screen = pygame.display.set_mode((EDITOR_WIDTH, EDITOR_HEIGHT), flags)
screen.fill("0xA4A4A4")
pygame.display.set_caption("Path Editor")
manager = initiate_ui()
return screen, manager
def add_point(pos):
Y = 1
X = 0
global last_mouse_pos, start_pos
if (100 < pos[Y] < 900 and 0 < pos[X] < 900 and pygame.mouse.get_pos() != last_mouse_pos):
points.append(Point(pos[X], pos[Y] - 100))
last_mouse_pos = pos
if (len(points) == 1): return
last_point_angle = points[-2].heading.angle
points[-1].heading.update_angle(last_point_angle)
def draw_points(screen, points):
global start_pos, end_pos
screen.fill("0xA4A4A4")
for index, point in enumerate(points):
point.draw(screen)
if (index > 0):
pos = point.get_abs_pos()
last_pos = (points[index - 1].get_abs_pos())
pygame.draw.line(screen, (0, 0, 0), last_pos, pos, 3)
if selected_point:
pygame.draw.circle(screen, (255, 0, 0),
selected_point.get_abs_pos(), 3)
selected_point.heading.draw(screen)
for point in special_points:
point.draw(screen)
point.heading.draw(screen)
if start_pos: pygame.draw.circle(screen, (0, 255, 0), (start_pos[0], start_pos[1] + 100) , 3)
if end_pos: pygame.draw.circle(screen, (255, 0, 0), (end_pos[0], end_pos[1] + 100), 3)
def update_point(change):
# While the user is holding the left mouse button and the mouse is in the edit zone continue updating the point.
while (pygame.mouse.get_pressed()[0] and pygame.mouse.get_pos()[1] > 100):
mouse_pos = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] - 100 # Get the mouse position relative to the edit zone.
if change == "POINT":
selected_point.update(mouse_pos[0], mouse_pos[1])
if change == "HEADING":
selected_point.heading.update(selected_point.pos, mouse_pos)
process_events()
update_ui()
draw_points(screen, points)
manager.draw_ui(screen)
pygame.display.update()
manager.update(60)
clock.tick(240)
def modify_point(selected_point: Point):
global manager
mouse_pos = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1] - 100 # Get the mouse position relative to the edit zone.
if selected_point.rect.collidepoint(mouse_pos):
update_point("POINT")
elif selected_point.heading.rect.collidepoint(mouse_pos):
init_angle = selected_point.heading.angle # Used to later check if the user had changed the angle.
update_point("HEADING")
if (init_angle != selected_point.heading.angle):
point_index = points.index(selected_point)
for index, point in enumerate(points[point_index + 1:]):
if (int(point.heading.angle) == int(init_angle)):
point.heading.update_angle(selected_point.heading.angle)
else: break
special_points.append(selected_point)
def update_ui():
global selected_point
if (selected_point): # if a point is selected
pos_x_entry.show() # show the text entry boxes
pos_y_entry.show()
pos_x_text.show()
pos_y_text.show()
angle_entry.show()
angle_text.show()
if (pygame.mouse.get_pos()[1] > 100):
pos_x_entry.set_text(str(selected_point.pos[0])) # Update the text entries with the selected point's position.
pos_y_entry.set_text(str(800 - selected_point.pos[1]))
angle_entry.set_text(str(int(selected_point.heading.angle)))
else:
init_angle = selected_point.heading.angle # Used to later check if the user had changed the angle.
pos_x = int(pos_x_entry.get_text() if pos_x_entry.get_text() != "" else 0)
pos_y = DRAWING_HEIGHT - int(pos_y_entry.get_text() if pos_y_entry.get_text() != "" else 0)
angle = int(angle_entry.get_text() if angle_entry.get_text().replace("-", "") != "" else 0)
selected_point.update(pos_x if pos_x < 900 else 900,
pos_y if pos_y < 800 else 800)
selected_point.heading.update_angle(angle)
if (init_angle != selected_point.heading.angle):
point_index = points.index(selected_point)
for index, point in enumerate(points[point_index + 1:]):
if (int(point.heading.angle) == int(init_angle)):
point.heading.update_angle(selected_point.heading.angle)
else: break
special_points.append(selected_point)
else:
pos_x_entry.hide()
pos_y_entry.hide()
pos_x_text.hide()
pos_y_text.hide()
angle_entry.hide()
angle_text.hide()
def process_events():
global selected_point, points, manager, mode, special_points, start_pos, end_pos, start_pos, end_pos
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
mode = "cursor" if mode == "edit" else "edit"
print("[INFO] MODE CHANGED TO: " + mode)
selected_point = None
if event.key == pygame.K_s:
print("[INFO] SAVING PATH")
generate_path(convert_points(points, start_pos))
if event.type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == clear_button:
print("[INFO] POINTS CLEARED")
selected_point = None
points = []
special_points = []
start_pos = None
end_pos = None
manager.process_events(event)
if (len(points) > 0):
if start_pos != points[0].pos: # If the start position has changed, update the start position.
start_pos = points[0].pos
if end_pos != points[-1].pos and len(points) > 1:
end_pos = points[-1].pos
def main():
global points, last_mouse_pos, selected_point, manager, clock, screen, mode, special_points, start_pos, end_pos
screen, manager = initiate_screen()
last_mouse_pos = (0, 0), (1, 1)
clock = pygame.time.Clock()
running = True
points = []
mode = "edit"
selected_point = None
special_points = []
start_pos = (0, 0)
end_pos = (0, 0)
while running:
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_ESCAPE]:
running = False
if pygame.mouse.get_pressed()[0] and mode == "edit":
add_point(pygame.mouse.get_pos())
elif pygame.mouse.get_pressed()[0] and mode == "cursor":
for point in points:
mouse_pos = (pygame.mouse.get_pos()[0],
pygame.mouse.get_pos()[1] - 100)
if point.rect.collidepoint(mouse_pos) and not point == selected_point:
selected_point = point
print("[INFO] Selected point: ", point.pos)
break
if selected_point:
modify_point(selected_point)
process_events()
update_ui()
draw_points(screen, points)
manager.draw_ui(screen)
pygame.display.update()
manager.update(60)
clock.tick(240)
pygame.quit()
if __name__ == "__main__":
main()