-
Notifications
You must be signed in to change notification settings - Fork 1
/
pikmingen_widgets.py
1846 lines (1452 loc) · 69 KB
/
pikmingen_widgets.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import traceback
import os
from time import sleep
from timeit import default_timer
from io import StringIO
from math import sin, cos, atan2, radians, degrees, pi, tan
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5.QtGui import QMouseEvent, QWheelEvent, QPainter, QColor, QFont, QFontMetrics, QPolygon, QImage, QPixmap, QKeySequence
from PyQt5.QtWidgets import (QWidget, QListWidget, QListWidgetItem, QDialog, QMenu, QLineEdit,
QMdiSubWindow, QHBoxLayout, QVBoxLayout, QLabel, QPushButton, QTextEdit, QAction, QShortcut)
import PyQt5.QtWidgets as QtWidgets
import PyQt5.QtCore as QtCore
from PyQt5.QtCore import QSize, pyqtSignal, QPoint, QRect
from PyQt5.QtCore import Qt
import PyQt5.QtGui as QtGui
from helper_functions import calc_zoom_in_factor, calc_zoom_out_factor
from custom_widgets import (MapViewer, rotate_rel,
catch_exception, CheckableButton, Collision)
from pikmingen import PikminObject
from libpiktxt import PikminTxt
from opengltext import draw_collision
from lib.vectors import Matrix4x4, Vector3, Line, Plane, Triangle
import pikmingen
from lib.model_rendering import TexturedPlane
ENTITY_SIZE = 14
DEFAULT_ENTITY = QColor("black")
DEFAULT_MAPZONE = QColor("grey")
DEFAULT_SELECTED = QColor("red")
DEFAULT_ANGLE_MARKER = QColor("blue")
SHOW_TERRAIN_NO_TERRAIN = 0
SHOW_TERRAIN_REGULAR = 1
SHOW_TERRAIN_LIGHT = 2
MOUSE_MODE_NONE = 0
MOUSE_MODE_MOVEWP = 1
MOUSE_MODE_ADDWP = 2
MOUSE_MODE_CONNECTWP = 3
BRIDGE_LENGTHS = {pikmingen.BRIDGE_LONG: 360,
pikmingen.BRIDGE_SHORT_UP: 120,
pikmingen.BRIDGE_SHORT: 180}
BRIDGE_GRAPHICS = {pikmingen.BRIDGE_SHORT: QtGui.QImage("resources/sbridge.png", "png"),
pikmingen.BRIDGE_SHORT_UP: QtGui.QImage("resources/ubridge.png", "png"),
pikmingen.BRIDGE_LONG: QtGui.QImage("resources/lbridge.png", "png")}
GATE_GRAPHICS = {pikmingen.GATE_SAND: QtGui.QImage("resources/gate.png", "png"),
pikmingen.GATE_ELECTRIC: QtGui.QImage("resources/dgat.png", "png")}
DOWNFLOOR_GRAPHICS = {"0": QtGui.QImage("resources/downfloor1.png"),
"1": QtGui.QImage("resources/downfloor2.png"),
"2": QtGui.QImage("resources/paperbag.png")}
ONION_COLORTABLE = {pikmingen.ONYN_ROCKET: QColor("grey"),
pikmingen.ONYN_BLUEONION: QColor("blue"),
pikmingen.ONYN_REDONION: QColor(255, 55, 55),
pikmingen.ONYN_YELLOWONION: QColor(255, 212, 0)}
OBJECT_SIZES = {
pikmingen.ONYN_BLUEONION: 47,
pikmingen.ONYN_REDONION: 47,
pikmingen.ONYN_YELLOWONION: 47,
pikmingen.ONYN_ROCKET: 55,
}
def catch_exception_with_dialog(func):
def handle(*args, **kwargs):
try:
print(args, kwargs)
return func(*args, **kwargs)
except Exception as e:
traceback.print_exc()
open_error_dialog(str(e), None)
return handle
def catch_exception_with_dialog_nokw(func):
def handle(*args, **kwargs):
try:
print(args, kwargs)
return func(*args, **kwargs)
except Exception as e:
traceback.print_exc()
open_error_dialog(str(e), None)
return handle
MODE_TOPDOWN = 0
MODE_3D = 1
class GenMapViewer(QtWidgets.QOpenGLWidget):
mouse_clicked = pyqtSignal(QMouseEvent)
entity_clicked = pyqtSignal(QMouseEvent, str)
mouse_dragged = pyqtSignal(QMouseEvent)
mouse_released = pyqtSignal(QMouseEvent)
mouse_wheel = pyqtSignal(QWheelEvent)
position_update = pyqtSignal(QMouseEvent, tuple)
height_update = pyqtSignal(float)
select_update = pyqtSignal()
move_points = pyqtSignal(float, float)
connect_update = pyqtSignal(int, int)
create_waypoint = pyqtSignal(float, float)
create_waypoint_3d = pyqtSignal(float, float, float)
ENTITY_SIZE = ENTITY_SIZE
rotate_current = pyqtSignal(pikmingen.PikminObject, float)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._zoom_factor = 10
self.setFocusPolicy(Qt.ClickFocus)
self.SIZEX = 1024#768#1024
self.SIZEY = 1024#768#1024
self.canvas_width, self.canvas_height = self.width(), self.height()
#self.setMinimumSize(QSize(self.SIZEX, self.SIZEY))
#self.setMaximumSize(QSize(self.SIZEX, self.SIZEY))
self.setObjectName("bw_map_screen")
self.origin_x = self.SIZEX//2
self.origin_z = self.SIZEY//2
self.offset_x = 0
self.offset_z = 0
self.point_x = 0
self.point_y = 0
self.polygon_cache = {}
# This value is used for switching between several entities that overlap.
self.next_selected_index = 0
self.left_button_down = False
self.mid_button_down = False
self.right_button_down = False
self.drag_last_pos = None
self.current_waypoint = None
self.selected = []
self.terrain = None
self.terrain_scaled = None
self.terrain_buffer = QImage()
self.p = QPainter()
self.p2 = QPainter()
# self.show_terrain_mode = SHOW_TERRAIN_REGULAR
self.selectionbox_start = None
self.selectionbox_end = None
self.visualize_cursor = None
self.click_mode = 0
self.level_image = None
self.collision = None
self.highlighttriangle = None
self.setMouseTracking(True)
self.pikmin_generators = None
self.waterboxes = []
self.mousemode = MOUSE_MODE_NONE
self.overlapping_wp_index = 0
self.editorconfig = None
#self.setContextMenuPolicy(Qt.CustomContextMenu)
self.spawnpoint = None
self.shift_is_pressed = False
self.rotation_is_pressed = False
self.last_drag_update = 0
self.change_height_is_pressed = False
self.last_mouse_move = None
self.timer = QtCore.QTimer()
self.timer.setInterval(2)
self.timer.timeout.connect(self.render_loop)
self.timer.start()
self._lastrendertime = 0
self._lasttime = 0
self._frame_invalid = False
self.MOVE_UP = 0
self.MOVE_DOWN = 0
self.MOVE_LEFT = 0
self.MOVE_RIGHT = 0
self.MOVE_FORWARD = 0
self.MOVE_BACKWARD = 0
self.SPEEDUP = 0
self._wasdscrolling_speed = 1
self._wasdscrolling_speedupfactor = 3
self.main_model = None
self.buffered_deltas = []
# 3D Setup
self.mode = MODE_TOPDOWN
self.camera_horiz = pi*(1/2)
self.camera_vertical = -pi*(1/4)
self.camera_height = 1000
self.last_move = None
self.selection_queue = []
self.selectionbox_projected_start = None
self.selectionbox_projected_end = None
self.selectionbox_projected_2d = None
self.selectionbox_projected_origin = None
self.selectionbox_projected_up = None
self.selectionbox_projected_right = None
self.selectionbox_projected_coords = None
self.last_position_update = 0
self.move_collision_plane = Plane(Vector3(0.0, 0.0, 0.0), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0))
@catch_exception_with_dialog
def initializeGL(self):
self.setup_grid()
self.testimage = TexturedPlane(100, 100, BRIDGE_GRAPHICS[pikmingen.BRIDGE_SHORT])
self.bridge_models = {pikmingen.BRIDGE_LONG: TexturedPlane(130, 360+80,
QtGui.QImage("resources/lbridge.png", "png")),
pikmingen.BRIDGE_SHORT: TexturedPlane(130, 180+80,
QtGui.QImage("resources/sbridge.png", "png")),
pikmingen.BRIDGE_SHORT_UP: TexturedPlane(130, 120+80,
QtGui.QImage("resources/ubridge.png", "png"))}
self.bridge_models[pikmingen.BRIDGE_SHORT].set_offset(0, (180+80)/2 - 42)
self.bridge_models[pikmingen.BRIDGE_LONG].set_offset(0, (360+80)/2 - 42)
self.bridge_models[pikmingen.BRIDGE_SHORT_UP].set_offset(0, (120+80)/2 - 42)
self.gate_models = {pikmingen.GATE_ELECTRIC: TexturedPlane(267, 35,
QtGui.QImage("resources/dgat.png", "png")),
pikmingen.GATE_SAND: TexturedPlane(267, 70,
QtGui.QImage("resources/gate.png", "png"))}
self.gate_models[pikmingen.GATE_ELECTRIC].set_offset(0, -10)
self.onion_models = {
pikmingen.ONYN_BLUEONION: TexturedPlane(47*2, 47*2,
QtGui.QImage("resources/generic_circle.png", "png")),
pikmingen.ONYN_REDONION: TexturedPlane(47*2, 47*2,
QtGui.QImage("resources/generic_circle.png", "png")),
pikmingen.ONYN_YELLOWONION: TexturedPlane(47*2, 47*2,
QtGui.QImage("resources/generic_circle.png", "png")),
pikmingen.ONYN_ROCKET: TexturedPlane(55*2, 55*2,
QtGui.QImage("resources/generic_circle.png", "png"))
}
self.onion_models[pikmingen.ONYN_BLUEONION].set_color((0.0, 0.0, 1.0))
self.onion_models[pikmingen.ONYN_REDONION].set_color((255/255.0, 55/255.0, 55/255.0))
self.onion_models[pikmingen.ONYN_YELLOWONION].set_color((255/255.0, 212/255.0, 0.0))
self.onion_models[pikmingen.ONYN_ROCKET].set_color((0.5, 0.5, 0.5))
self.downfloor_models = {
"0": TexturedPlane(100, 100,
QtGui.QImage("resources/downfloor1.png", "png")),
"1": TexturedPlane(150, 120,
QtGui.QImage("resources/downfloor2.png", "png")),
"2": TexturedPlane(256,197,
QtGui.QImage("resources/paperbag.png", "png"))
}
self.generic_object = TexturedPlane(20*2, 20*2, QtGui.QImage("resources/generic_circle.png", "png"))
self.rotation_visualizer = glGenLists(1)
glNewList(self.rotation_visualizer, GL_COMPILE)
glColor4f(0.0, 0.0, 1.0, 1.0)
glLineWidth(2.0)
glBegin(GL_LINES)
glVertex3f(0.0, 0.0, 0.0)
glVertex3f(0.0, 40.0, 0.0)
glEnd()
glEndList()
def resizeGL(self, width, height):
# Called upon window resizing: reinitialize the viewport.
# update the window size
self.canvas_width, self.canvas_height = width, height
# paint within the whole window
glEnable(GL_DEPTH_TEST)
glViewport(0, 0, self.canvas_width, self.canvas_height)
#glMatrixMode(GL_MODELVIEW)
#glLoadIdentity()
@catch_exception
def set_editorconfig(self, config):
self.editorconfig = config
self._wasdscrolling_speed = config.getfloat("wasdscrolling_speed")
self._wasdscrolling_speedupfactor = config.getfloat("wasdscrolling_speedupfactor")
def change_from_topdown_to_3d(self):
if self.mode == MODE_3D:
return
else:
self.mode = MODE_3D
if self.mousemode == MOUSE_MODE_NONE:
self.setContextMenuPolicy(Qt.DefaultContextMenu)
# This is necessary so that the position of the 3d camera equals the middle of the topdown view
self.offset_x *= -1
self.do_redraw()
def change_from_3d_to_topdown(self):
if self.mode == MODE_TOPDOWN:
return
else:
self.mode = MODE_TOPDOWN
if self.mousemode == MOUSE_MODE_NONE:
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.offset_x *= -1
self.do_redraw()
@catch_exception
def render_loop(self):
now = default_timer()
diff = now-self._lastrendertime
timedelta = now-self._lasttime
if self.mode == MODE_TOPDOWN:
self.handle_arrowkey_scroll(timedelta)
else:
self.handle_arrowkey_scroll_3d(timedelta)
"""if len(self.buffered_deltas) > 0:
deltax, deltay = self.buffered_deltas.pop(0)
self.offset_x += deltax
self.offset_z += deltay
self._frame_invalid = True"""
if diff > 1 / 60.0:
if self._frame_invalid:
self.update()
self._lastrendertime = now
self._frame_invalid = False
self._lasttime = now
def handle_arrowkey_scroll(self, timedelta):
diff_x = diff_y = 0
#print(self.MOVE_UP, self.MOVE_DOWN, self.MOVE_LEFT, self.MOVE_RIGHT)
speedup = 1
if self.shift_is_pressed:
speedup = self._wasdscrolling_speedupfactor
if self.MOVE_FORWARD == 1 and self.MOVE_BACKWARD == 1:
diff_y = 0
elif self.MOVE_FORWARD == 1:
diff_y = 1*speedup*self._wasdscrolling_speed*timedelta
elif self.MOVE_BACKWARD == 1:
diff_y = -1*speedup*self._wasdscrolling_speed*timedelta
if self.MOVE_LEFT == 1 and self.MOVE_RIGHT == 1:
diff_x = 0
elif self.MOVE_LEFT == 1:
diff_x = 1*speedup*self._wasdscrolling_speed*timedelta
elif self.MOVE_RIGHT == 1:
diff_x = -1*speedup*self._wasdscrolling_speed*timedelta
if diff_x != 0 or diff_y != 0:
if self.zoom_factor > 1.0:
self.offset_x += diff_x * (1.0 + (self.zoom_factor - 1.0) / 2.0)
self.offset_z += diff_y * (1.0 + (self.zoom_factor - 1.0) / 2.0)
else:
self.offset_x += diff_x
self.offset_z += diff_y
# self.update()
self.do_redraw()
def handle_arrowkey_scroll_3d(self, timedelta):
if self.selectionbox_projected_origin is not None:
return
diff_x = diff_y = diff_height = 0
#print(self.MOVE_UP, self.MOVE_DOWN, self.MOVE_LEFT, self.MOVE_RIGHT)
speedup = 1
forward_vec = Vector3(cos(self.camera_horiz), sin(self.camera_horiz), 0)
sideways_vec = Vector3(sin(self.camera_horiz), -cos(self.camera_horiz), 0)
if self.shift_is_pressed:
speedup = self._wasdscrolling_speedupfactor
if self.MOVE_FORWARD == 1 and self.MOVE_BACKWARD == 1:
forward_move = forward_vec*0
elif self.MOVE_FORWARD == 1:
forward_move = forward_vec*(1*speedup*self._wasdscrolling_speed*timedelta)
elif self.MOVE_BACKWARD == 1:
forward_move = forward_vec*(-1*speedup*self._wasdscrolling_speed*timedelta)
else:
forward_move = forward_vec*0
if self.MOVE_LEFT == 1 and self.MOVE_RIGHT == 1:
sideways_move = sideways_vec*0
elif self.MOVE_LEFT == 1:
sideways_move = sideways_vec*(-1*speedup*self._wasdscrolling_speed*timedelta)
elif self.MOVE_RIGHT == 1:
sideways_move = sideways_vec*(1*speedup*self._wasdscrolling_speed*timedelta)
else:
sideways_move = sideways_vec*0
if self.MOVE_UP == 1 and self.MOVE_DOWN == 1:
diff_height = 0
elif self.MOVE_UP == 1:
diff_height = 1*speedup*self._wasdscrolling_speed*timedelta
elif self.MOVE_DOWN == 1:
diff_height = -1 * speedup * self._wasdscrolling_speed * timedelta
if not forward_move.is_zero() or not sideways_move.is_zero() or diff_height != 0:
#if self.zoom_factor > 1.0:
# self.offset_x += diff_x * (1.0 + (self.zoom_factor - 1.0) / 2.0)
# self.offset_z += diff_y * (1.0 + (self.zoom_factor - 1.0) / 2.0)
#else:
self.offset_x += (forward_move.x + sideways_move.x)
self.offset_z += (forward_move.y + sideways_move.y)
self.camera_height += diff_height
# self.update()
self.do_redraw()
def set_arrowkey_movement(self, up, down, left, right):
self.MOVE_UP = up
self.MOVE_DOWN = down
self.MOVE_LEFT = left
self.MOVE_RIGHT = right
def do_redraw(self):
self._frame_invalid = True
def set_visibility(self, visibility):
self.visibility_toggle = visibility
def resize_map(self, newsizex, newsizey):
self.SIZEX = newsizex
self.SIZEY = newsizey
self.origin_x = self.SIZEX // 2
self.origin_z = self.SIZEY // 2
def reset(self, keep_collision=False):
self.overlapping_wp_index = 0
self.shift_is_pressed = False
self.SIZEX = 1024
self.SIZEY = 1024
self.origin_x = self.SIZEX//2
self.origin_z = self.SIZEY//2
self.last_drag_update = 0
self.left_button_down = False
self.mid_button_down = False
self.right_button_down = False
self.drag_last_pos = None
self.selectionbox_start = None
self.selectionbox_end = None
self.selected = []
if not keep_collision:
# Potentially: Clear collision object too?
self.level_image = None
self.offset_x = 0
self.offset_z = 0
self._zoom_factor = 10
#self.waterboxes = []
self.pikmin_generators = None
self.mousemode = MOUSE_MODE_NONE
self.spawnpoint = None
self.rotation_is_pressed = False
self._frame_invalid = False
self.MOVE_UP = 0
self.MOVE_DOWN = 0
self.MOVE_LEFT = 0
self.MOVE_RIGHT = 0
self.SPEEDUP = 0
def setup_grid(self):
offset = +0.2
self.grid = glGenLists(1)
glNewList(self.grid, GL_COMPILE)
glColor3f(0.0, 0.0, 0.0)
glLineWidth(4.0)
glBegin(GL_LINES)
glVertex3f(-6000, 0, offset)
glVertex3f(6000, 0, offset)
glVertex3f(0, -6000, offset)
glVertex3f(0, 6000, offset)
glEnd()
glLineWidth(1.0)
glBegin(GL_LINES)
for ix in range(-6000, 6000+500, 500):
glVertex3f(ix, -6000, offset)
glVertex3f(ix, 6000, offset)
for iy in range(-6000, 6000+500, 500):
glVertex3f(-6000, iy, offset)
glVertex3f(6000, iy, offset)
glEnd()
glEndList()
def set_collision(self, verts, faces):
self.collision = Collision(verts, faces)
if self.main_model is None:
self.main_model = glGenLists(1)
glNewList(self.main_model, GL_COMPILE)
#glBegin(GL_TRIANGLES)
draw_collision(verts, faces)
#glEnd()
glEndList()
def set_mouse_mode(self, mode):
assert mode in (MOUSE_MODE_NONE, MOUSE_MODE_ADDWP, MOUSE_MODE_CONNECTWP, MOUSE_MODE_MOVEWP)
self.mousemode = mode
if self.mousemode == MOUSE_MODE_NONE and self.mode == MODE_TOPDOWN:
self.setContextMenuPolicy(Qt.CustomContextMenu)
else:
self.setContextMenuPolicy(Qt.DefaultContextMenu)
@property
def zoom_factor(self):
return self._zoom_factor/10.0
def zoom(self, fac):
if 0.1 < (self.zoom_factor + fac) <= 25:
self._zoom_factor += int(fac*10)
#self.update()
self.do_redraw()
def mouse_coord_to_world_coord(self, mouse_x, mouse_y):
zf = self.zoom_factor
width, height = self.canvas_width, self.canvas_height
camera_width = width * zf
camera_height = height * zf
topleft_x = -camera_width / 2 - self.offset_x
topleft_y = camera_height / 2 + self.offset_z
relx = mouse_x / width
rely = mouse_y / height
res = (topleft_x + relx*camera_width, topleft_y - rely*camera_height)
return res
def mouse_coord_to_world_coord_transform(self, mouse_x, mouse_y):
mat4x4 = Matrix4x4.from_opengl_matrix(*glGetFloatv(GL_PROJECTION_MATRIX))
width, height = self.canvas_width, self.canvas_height
result = mat4x4.multiply_vec4(mouse_x-width/2, mouse_y-height/2, 0, 1)
return result
#@catch_exception_with_dialog
@catch_exception
def paintGL(self):
offset_x = self.offset_x
offset_z = self.offset_z
#start = default_timer()
glClearColor(1.0, 1.0, 1.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
width, height = self.canvas_width, self.canvas_height
if self.mode == MODE_TOPDOWN:
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
zf = self.zoom_factor
#glOrtho(-6000.0, 6000.0, -6000.0, 6000.0, -3000.0, 2000.0)
camera_width = width*zf
camera_height = height*zf
glOrtho(-camera_width / 2 - offset_x, camera_width / 2 - offset_x,
-camera_height / 2 + offset_z, camera_height / 2 + offset_z, -3000.0, 2000.0)
#glScalef(1.0 / zf, 1.0 / zf, 1.0 / zf)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
else:
#glEnable(GL_CULL_FACE)
# set yellow color for subsequent drawing rendering calls
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(75, width / height, 1.0, 12800.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
look_direction = Vector3(cos(self.camera_horiz), sin(self.camera_horiz), sin(self.camera_vertical))
# look_direction.unify()
fac = 1.01 - abs(look_direction.z)
# print(fac, look_direction.z, look_direction)
gluLookAt(self.offset_x, self.offset_z, self.camera_height,
self.offset_x + look_direction.x * fac, self.offset_z + look_direction.y * fac,
self.camera_height + look_direction.z,
0, 0, 1)
self.camera_direction = Vector3(look_direction.x * fac, look_direction.y * fac, look_direction.z)
#glScalef(1.0 / zf, 1.0 / zf, 1.0 / zf)
#glTranslatef(self.offset_x, -self.offset_z, 0)
if len(self.selection_queue) > 0:
click_x, click_y, clickwidth, clickheight, shiftpressed = self.selection_queue.pop(0)
click_y = height - click_y
print("Queued test:", click_x, click_y, clickwidth, clickheight)
if self.pikmin_generators is not None:
objects = self.pikmin_generators.objects
for i, pikminobject in enumerate(objects):
x, y, z = pikminobject.x, pikminobject.y, pikminobject.z
name = pikminobject.get_useful_object_name()
glPushMatrix()
glTranslatef(x, -z, y + 2)
if name in self.onion_models:
self.onion_models[name].render_coloredid(i)
else:
self.generic_object.render_coloredid(i)
glPopMatrix()
pixels = glReadPixels(click_x, click_y, clickwidth, clickheight, GL_RGB, GL_UNSIGNED_BYTE)
selected ={}
#for i in range(0, clickwidth*clickheight, 4):
for x in range(0, clickwidth, 3):
for y in range(0, clickheight, 3):
i = (x + y*clickwidth)*3
index = (pixels[i+1] << 8) | pixels[i+2] # | (pixels[i*3+0] << 16)
if index != 0xFFFF:
pikminobject = objects[index]
selected[pikminobject] = True
selected = list(selected.keys())
print("result:", selected)
if not shiftpressed:
self.selected = selected
self.select_update.emit()
elif shiftpressed:
for obj in selected:
if obj not in self.selected:
self.selected.append(obj)
self.select_update.emit()
glClearColor(1.0, 1.0, 1.0, 0.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_DEPTH_TEST)
glDisable(GL_TEXTURE_2D)
glColor4f(1.0, 1.0, 1.0, 1.0)
if self.main_model is not None:
#print(self.main_model, type(self.main_model))
glCallList(self.main_model)
if self.mode == MODE_TOPDOWN:
glDisable(GL_DEPTH_TEST)
glCallList(self.grid)
if self.mode == MODE_TOPDOWN:
glDisable(GL_ALPHA_TEST)
glDisable(GL_TEXTURE_2D)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
for waterbox in self.waterboxes:
waterbox.render()
glDisable(GL_DEPTH_TEST)
glDisable(GL_BLEND)
glEnable(GL_ALPHA_TEST)
glAlphaFunc(GL_GEQUAL, 0.5)
#glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
#self.testimage.render()
if self.pikmin_generators is not None:
selected = self.selected
objects = self.pikmin_generators.objects
#links = self.pikmin_routes.links
#for waypoint, wp_info in self.waypoints.items():
for pikminobject in objects:
x, y, z = pikminobject.x, pikminobject.y, pikminobject.z
#glColor3f(1.0, 1.0, 1.0)
name = pikminobject.get_useful_object_name()
glPushMatrix()
glColor4f(1.0, 1.0, 1.0, 1.0)
if pikminobject.object_type == "{item}":
angle = pikminobject.get_horizontal_rotation()
glTranslatef(x, -z, y + 1)
glRotate(angle + 180, 0, 0, 1)
if name in self.bridge_models:
model = self.bridge_models[name]
model.render()
elif name in self.gate_models:
model = self.gate_models[name]
model.render()
else:
itemdata = pikminobject._object_data[0]
if itemdata[0] == "{dwfl}":
downfloortype = itemdata[4]
if downfloortype in self.downfloor_models:
model = self.downfloor_models[downfloortype]
model.render()
if pikminobject in selected:
glColor4f(1.0, 0.0, 0.0, 1.0)
elif name in self.onion_models:
self.onion_models[name].apply_color()
else:
glColor4f(0.0, 0.0, 0.0, 1.0)
glPopMatrix()
glPushMatrix()
glTranslatef(x, -z, y+2)
angle = pikminobject.get_horizontal_rotation()
if angle is not None:
glRotate(angle + 180, 0, 0, 1)
if name in self.onion_models:
self.onion_models[name].render()
else:
self.generic_object.render()
if pikminobject in selected:
angle = pikminobject.get_horizontal_rotation()
if angle is not None:
#glRotate(angle + 180, 0, 0, 1)
glDisable(GL_ALPHA_TEST)
glCallList(self.rotation_visualizer)
glEnable(GL_ALPHA_TEST)
glPopMatrix()
glDisable(GL_TEXTURE_2D)
if self.mode != MODE_TOPDOWN:
glDisable(GL_ALPHA_TEST)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glEnable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
for waterbox in self.waterboxes:
waterbox.render()
glDisable(GL_BLEND)
glDisable(GL_DEPTH_TEST)
if self.selectionbox_start is not None and self.selectionbox_end is not None:
startx, startz = self.selectionbox_start
endx, endz = self.selectionbox_end
glColor4f(1.0, 0.0, 0.0, 1.0)
glLineWidth(2.0)
glBegin(GL_LINE_LOOP)
glVertex3f(startx, startz, 0)
glVertex3f(startx, endz, 0)
glVertex3f(endx, endz, 0)
glVertex3f(endx, startz, 0)
glEnd()
if self.selectionbox_projected_origin is not None and self.selectionbox_projected_coords:
origin = self.selectionbox_projected_origin
point2, point3, point4 = self.selectionbox_projected_coords
glColor4f(1.0, 0.0, 0.0, 1.0)
glLineWidth(2.0)
point1 = origin
glBegin(GL_LINE_LOOP)
glVertex3f(point1.x, point1.y, point1.z)
glVertex3f(point2.x, point2.y, point2.z)
glVertex3f(point3.x, point3.y, point3.z)
glVertex3f(point4.x, point4.y, point4.z)
glEnd()
glEnable(GL_DEPTH_TEST)
glFinish()
@catch_exception
def mousePressEvent(self, event):
if self.mode == MODE_TOPDOWN:
if (event.buttons() & Qt.LeftButton and not self.left_button_down):
mouse_x, mouse_z = (event.x(), event.y())
selectstartx, selectstartz = self.mouse_coord_to_world_coord(mouse_x, mouse_z)
if True: #(self.mousemode == MOUSE_MODE_MOVEWP or self.mousemode == MOUSE_MODE_NONE):
self.left_button_down = True
self.selectionbox_start = (selectstartx, selectstartz)
if self.pikmin_generators is not None:
hit = False
all_hit_waypoints = []
for pikminobject in self.pikmin_generators.objects:
name = pikminobject.get_useful_object_name()
if name in OBJECT_SIZES:
size = OBJECT_SIZES[name]
else:
size = ENTITY_SIZE
#if abs(mouse_x-objx) <= size and abs(mouse_z - objz) <= size:
if abs(selectstartx-pikminobject.x) <= size//2 and abs(selectstartz+pikminobject.z) <= size//2:
# print("hit!")
all_hit_waypoints.append(pikminobject)
if len(all_hit_waypoints) > 0:
wp_index = all_hit_waypoints[self.overlapping_wp_index%len(all_hit_waypoints)]
if not self.shift_is_pressed:
self.selected = [wp_index]
else:
if wp_index not in self.selected:
self.selected.append(wp_index)
else:
self.selected.remove(wp_index)
# print("hit")
hit = True
self.select_update.emit()
#if self.connect_first_wp is not None and self.mousemode == MOUSE_MODE_CONNECTWP:
# self.connect_update.emit(self.connect_first_wp, wp_index)
#self.connect_first_wp = wp_index
#self.move_startpos = [wp_index]
#self.update()
self.do_redraw()
self.overlapping_wp_index = (self.overlapping_wp_index+1)%len(all_hit_waypoints)
if not hit:
if not self.shift_is_pressed:
self.selected = []
self.select_update.emit()
self.connect_first_wp = None
self.move_startpos = []
#self.update()
self.do_redraw()
if event.buttons() & Qt.MiddleButton and not self.mid_button_down:
self.mid_button_down = True
self.drag_last_pos = (event.x(), event.y())
if event.buttons() & Qt.RightButton:
self.right_button_down = True
if self.mousemode == MOUSE_MODE_MOVEWP:
mouse_x, mouse_z = (event.x(), event.y())
movetox, movetoz = self.mouse_coord_to_world_coord(mouse_x, mouse_z)
if self.rotation_is_pressed and len(self.selected) == 1:
obj = self.selected[0]
relx = obj.x - movetox
relz = -obj.z - movetoz
self.rotate_current.emit(obj, degrees(atan2(-relx, relz)))
elif not self.rotation_is_pressed:
if len(self.selected) > 0:
sumx, sumz = 0, 0
wpcount = len(self.selected)
for obj in self.selected:
sumx += obj.x
sumz += obj.z
x = sumx/float(wpcount)
z = sumz/float(wpcount)
self.move_points.emit(movetox-x, -movetoz-z)
elif self.mousemode == MOUSE_MODE_ADDWP:
mouse_x, mouse_z = (event.x(), event.y())
destx, destz = self.mouse_coord_to_world_coord(mouse_x, mouse_z)
self.create_waypoint.emit(destx, -destz)
else:
if event.buttons() & Qt.RightButton and not (event.buttons() & Qt.LeftButton & self.mousemode == MOUSE_MODE_NONE):
self.last_move = (event.x(), event.y())
self.right_button_down = True
if (event.buttons() & Qt.LeftButton and not self.left_button_down):
self.left_button_down = True
# Do selection
if self.mousemode == MOUSE_MODE_NONE and not self.right_button_down:
self.selection_queue.append((event.x(), event.y(), 1, 1,
self.shift_is_pressed))
self.do_redraw()
self.camera_direction.normalize()
self.selectionbox_projected_2d = (event.x(), event.y())
view = self.camera_direction.copy()
h = view.cross(Vector3(0, 0, 1))
v = h.cross(view)
h.normalize()
v.normalize()
rad = 75 * pi / 180.0
vLength = tan(rad / 2) * 1.0
hLength = vLength * (self.canvas_width / self.canvas_height)
v *= vLength
h *= hLength
mirror_y = self.canvas_height - event.y()
x = event.x() - self.canvas_width / 2
y = mirror_y - self.canvas_height / 2
x /= (self.canvas_width / 2)
y /= (self.canvas_height / 2)
camerapos = Vector3(self.offset_x, self.offset_z, self.camera_height)
pos = camerapos + view * 1.01 + h * x + v * y
self.selectionbox_projected_origin = pos
elif self.mousemode == MOUSE_MODE_ADDWP:
print("shooting rays")
ray = self.create_ray_from_mouseclick(event.x(), event.y())
place_at = None
if self.collision is not None:
print("colliding with collision")
verts = self.collision.verts
faces = self.collision.faces
best_distance = None
for tri in self.collision.triangles:
collision = ray.collide(tri)
if collision is not False:
point, distance = collision
if best_distance is None or distance < best_distance:
place_at = point
best_distance = distance
if place_at is None:
print("colliding with plane")
front = Vector3(1.0, 0.0, 0.0)
left = Vector3(0.0, 1.0, 0.0)
plane = Plane(Vector3(0.0, 0.0, 0.0), front, left)
collision = ray.collide_plane(plane)
if collision is not False:
place_at, _ = collision
if place_at is not None:
print("collided")
self.create_waypoint_3d.emit(place_at.x, place_at.z, -place_at.y)
else:
print("nothing collided, aw")
elif self.mousemode == MOUSE_MODE_MOVEWP and not self.change_height_is_pressed:
mouse_x, mouse_z = (event.x(), event.y())
ray = self.create_ray_from_mouseclick(event.x(), event.y())