-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMap_Editor_Revised.py
More file actions
1223 lines (938 loc) · 44.3 KB
/
Copy pathMap_Editor_Revised.py
File metadata and controls
1223 lines (938 loc) · 44.3 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
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
from ursina import *
from ursina.mesh_importer import *
from tkinter import simpledialog, messagebox
import importlib
import sys
app = Ursina()
# ─── Globals ──────────────────────────────────────────────
deleting = False
selecting = None
snap_enabled = False
snap_size = 1.0
rotation_snap = 15.0
objects = []
delete_button = None
snap_button = None
def toggle_vis(self:Entity):
global selecting, deleting
# Find this entity’s DebugBehaviour (if it exists)
dbg = None
for script in self.scripts:
if isinstance(script, DebugBehaviour):
dbg = script
break
if deleting:
if self in objects:
objects.remove(self)
destroy(self)
refresh_container()
# If this was the selected entity, clear that reference:
if dbg is not None and selecting == dbg:
selecting = None
return # ← ADD this RETURN so we do not run the “wireframe/selection” code afterward
self.wireframe=not self.wireframe
# Also set/unset selecting to that DebugBehaviour instance (if present)
if dbg:
if selecting == dbg:
selecting = None
# restore color if needed
if dbg._orig_color is not None:
dbg.entity.color = dbg._orig_color
else:
# Deselect previous (if any)
if selecting is not None and selecting._orig_color is not None:
selecting.entity.color = selecting._orig_color
selecting = dbg
# Highlight newly selected (store original color if not already stored)
if dbg._orig_color is None:
dbg._orig_color = dbg.entity.color
dbg.entity.color = color.azure
Entity.toggle_vis=toggle_vis
# ─── DebugBehaviour (with Highlight + Snap-on-Release) ─────
class DebugBehaviour():
def __init__(self) -> None:
self.entity: Entity
self._orig_color = None
# Assign once, not every frame:
self.entity.on_click = self.toggle
def update(self):
# Ensure clicking calls toggle()
# self.entity.on_click = self.toggle
# Only allow transformations if this instance is selected
if selecting != self:
return
# — Scale vs. Move (continuous) —
if held_keys["shift"]:
# Continuous scaling while Shift + arrows/page keys held
self.entity.scale_z += (held_keys['up arrow'] - held_keys['down arrow']) \
* (time.dt * (1 if held_keys['control'] else 5) if not held_keys['alt'] else 0)
self.entity.scale_y += (held_keys['page up'] - held_keys['page down']) \
* (time.dt * (1 if held_keys['control'] else 5) if not held_keys['alt'] else 0)
self.entity.scale_x += (held_keys['right arrow'] - held_keys['left arrow']) \
* (time.dt * (1 if held_keys['control'] else 5) if not held_keys['alt'] else 0)
else:
# Continuous movement while arrows/page keys held
self.entity.z += (held_keys['up arrow'] - held_keys['down arrow']) \
* (time.dt * (1 if held_keys['control'] else 5) if not held_keys['alt'] else 0)
self.entity.y += (held_keys['page up'] - held_keys['page down']) \
* (time.dt * (1 if held_keys['control'] else 5) if not held_keys['alt'] else 0)
self.entity.x += (held_keys['right arrow'] - held_keys['left arrow']) \
* (time.dt * (1 if held_keys['control'] else 5) if not held_keys['alt'] else 0)
# — Continuous Rotation (X/Z, C/V, B/N) —
self.entity.rotation_y += (held_keys['x'] - held_keys['z']) \
* time.dt * (10 if held_keys['control'] else 20)
self.entity.rotation_x += (held_keys['c'] - held_keys['v']) \
* time.dt * (10 if held_keys['control'] else 20)
self.entity.rotation_z += (held_keys['b'] - held_keys['n']) \
* time.dt * (10 if held_keys['control'] else 20)
# ▶ Removed per-frame snapping from here.
# ▶ Snapping now occurs on key-release in input().
def input(self, key):
# Only respond if this instance is selected
if selecting != self:
return
# Press F to print debug info
if key == 'f':
print(f"'{self.entity.name}' pos : {self.entity.position}")
print(f"'{self.entity.name}' rot : {self.entity.rotation}")
# — Alt + arrows/page keys: discrete ±1 steps, then integer‐snap immediately —
if held_keys["shift"]:
if held_keys['alt']:
# Discrete scaling steps
self.entity.scale_z += (int(key == 'up arrow') - int(key == 'down arrow'))
self.entity.scale_y += (int(key == 'page up') - int(key == 'page down'))
self.entity.scale_x += (int(key == 'right arrow') - int(key == 'left arrow'))
if key not in ('alt',):
# Snap scale to nearest integer
self.entity.scale_x = int(self.entity.scale_x)
self.entity.scale_y = int(self.entity.scale_y)
self.entity.scale_z = int(self.entity.scale_z)
else:
if held_keys['alt']:
# Discrete movement steps
self.entity.z += (int(key == 'up arrow') - int(key == 'down arrow'))
self.entity.y += (int(key == 'page up') - int(key == 'page down'))
self.entity.x += (int(key == 'right arrow') - int(key == 'left arrow'))
if key not in ('alt',):
# Snap position to nearest integer
self.entity.x = int(self.entity.x)
self.entity.y = int(self.entity.y)
self.entity.z = int(self.entity.z)
# — Snap-on-release if snap_enabled is True —
if snap_enabled:
# Snap position when any arrow or page key is released
if key in (
'up arrow up', 'down arrow up',
'left arrow up', 'right arrow up',
'page up up', 'page down up'
):
px = round(self.entity.x / snap_size) * snap_size
py = round(self.entity.y / snap_size) * snap_size
pz = round(self.entity.z / snap_size) * snap_size
self.entity.position = Vec3(px, py, pz)
# Snap rotation when any rotation key is released
if key in ('x up', 'z up', 'c up', 'v up', 'b up', 'n up'):
ry = round(self.entity.rotation_y / rotation_snap) * rotation_snap
rx = round(self.entity.rotation_x / rotation_snap) * rotation_snap
rz = round(self.entity.rotation_z / rotation_snap) * rotation_snap
self.entity.rotation = Vec3(rx, ry, rz)
def toggle(self):
global selecting, deleting
# If delete mode is active, destroy entity immediately
if deleting:
if self.entity in objects:
objects.remove(self.entity)
destroy(self.entity)
refresh_container() # ← insert this line
del self
return
# Deselect if clicking the already-selected entity
if selecting == self:
if self._orig_color is not None:
self.entity.color = self._orig_color
selecting = None
return
# If some other entity was selected, restore its color first
if selecting is not None:
if selecting._orig_color is not None:
selecting.entity.color = selecting._orig_color
selecting = None
# Now select this entity: store original color and apply highlight
selecting = self
if self._orig_color is None:
self._orig_color = self.entity.color
self.entity.color = color.azure
# ─── Global Input (Escape to Unselect) ─────────────────────
def input(key):
global selecting
if key == 'escape' and selecting is not None:
# Restore original color on Escape
if selecting._orig_color is not None:
selecting.entity.color = selecting._orig_color
selecting = None
# ─── “Add New Object” ──────────────────────────────────────
def addnew():
name = simpledialog.askstring("Add new Object", "Enter Object's name")
model = simpledialog.askstring("Add new Object", "Enter Object's Model name")
texture = simpledialog.askstring("Add new Object", "Enter Object's Texture name")
clr=None
try:
if isinstance(eval(texture), color.Color): clr=eval(texture)
except: pass
collider = simpledialog.askstring("Add new Object", "Enter Object's Collider type")
double_sided=messagebox.askyesno("Map Editor", "Double Sided?")
try:
e = Entity(
name = name if name not in (None, "") else None,
model = model if model not in (None, "") else "cube",
texture = texture if texture not in (None, "") else "grass",
collider = collider if collider not in (None, "") else None,
double_sided = double_sided
)
if clr: e.color=clr
e.add_script(DebugBehaviour())
if e.model is None: e.model = 'cube'
objects.append(e)
# Recompute page so the new object is visible:
global current_page
last_page = max(0, ceil(len(objects) / OBJECTS_PER_PAGE) - 1)
current_page = last_page
except:
pass
refresh_container()
# ─── “Toggle Delete Mode” ──────────────────────────────────
def toggleDelete():
global deleting
deleting = not deleting
if deleting:
delete_button.text = 'Delete: ON'
delete_button.color = color.red
delete_button.text_color = color.white
else:
delete_button.text = 'Delete: OFF'
delete_button.color = color.white
delete_button.text_color = color.black
# ─── “Toggle Snap Mode” ────────────────────────────────────
def toggleSnap():
global snap_enabled
snap_enabled = not snap_enabled
if snap_enabled:
snap_button.text = 'Snap: ON'
snap_button.color = color.azure
snap_button.text_color = color.white
else:
snap_button.text = 'Snap: OFF'
snap_button.color = color.white
snap_button.text_color = color.black
OBJECTS_PER_PAGE=5
current_page=0
container_o=[]
def pg(i):
global current_page
# Recompute max_page whenever objects changes:
max_page = max(0, ceil(len(objects) / OBJECTS_PER_PAGE) - 1)
# Try moving to page “current_page + i,” but clamp into [0..max_page]
proposed = current_page + i
current_page = max(0, min(proposed, max_page))
refresh_container()
container=Entity(model=Quad(.1, aspect=.7),
color=color.black33,
parent=camera.ui,
scale=(.7,1), x=0.6479293,
eternal=True)
Button('Previous',
position=Vec3(0.4540579, -0.36839267, -1.3447969),
color=color.white,
on_click=lambda:pg(-1),
scale=(.25,.1),
text_color=color.black,
eternal=True)
Button('Next',
position=Vec3(0.736449, -0.36839267, -1.3447969),
color=color.white,
on_click=lambda:pg(1),
scale=(.25,.1),
text_color=color.black,
eternal=True)
def refresh_container():
for btn in container_o:
destroy(btn)
container_o.clear()
start = current_page * OBJECTS_PER_PAGE
end = min((current_page+1)*OBJECTS_PER_PAGE, len(objects))
# We want up to 5 buttons, each spaced by –0.28 in Y, starting at Y=0.4
for page_index in range(start, end):
obj = objects[page_index]
local_row = page_index - start # 0..4
y_pos = 0.4 + local_row * (-0.28)
btn = Button(
obj.name or f"Entity {page_index}",
position = Vec3(0.65, y_pos, 0),
scale = (.5, .1),
color = color.white,
text_color = color.black,
on_click = obj.toggle_vis,
eternal = True
)
container_o.append(btn)
# ─── “Save” (restores color before writing) ────────────────
def save():
global selecting
# If an entity is still selected, restore its color first
if selecting is not None:
if selecting._orig_color is not None:
selecting.entity.color = selecting._orig_color
selecting = None
# Serialize each entity in 'objects' using repr()
if messagebox.askyesno("Map Editor", "Do you want to save?"):
code=f"from ursina import *\n\n"
for i in objects:
code+=repr(i)+'\n'
with open('scene.py', 'w') as file:
file.write(code)
# ─── “Load” (clears scene and reloads scene.py) ─────────────
def load():
if messagebox.askyesno("Map Editor", "Do you want to load?"):
scene.clear()
camera.overlay.color = color.clear
if 'scene' in sys.modules:
importlib.reload(sys.modules['scene'])
else:
importlib.import_module('scene')
objects.clear()
for i in scene.entities:
i:Entity
if not i.eternal:
objects.append(i)
i.add_script(DebugBehaviour())
# Always go back to page 0
global current_page
current_page = 0
refresh_container()
# ─── UI SETUP ──────────────────────────────────────────────
# Background panel for buttons
Entity(
model = Quad(.1, aspect = .7),
color = color.black33,
parent = camera.ui,
scale = (.7, 1),
x = -0.6479,
eternal = True
)
# “Add new Object” button
Button(
'Add new Object',
position = Vec3(-0.6127, 0.3932, -0.895),
color = color.white,
highlight_color = color.light_gray, # ← this line
on_click = addnew,
scale = (.5, .1),
text_color = color.black,
eternal = True
)
# “Toggle Delete Mode” button
delete_button = Button(
'Delete: OFF',
position = Vec3(-0.6127, 0.1932, -0.895),
color = color.white,
highlight_color = color.light_gray, # ← this line
on_click = toggleDelete,
scale = (.5, .1),
text_color = color.black,
eternal = True
)
# “Snap-to-Grid” button
snap_button = Button(
'Snap: OFF',
position = Vec3(-0.6127, 0.05, -0.895),
color = color.white,
highlight_color = color.light_gray, # ← this line
on_click = toggleSnap,
scale = (.5, .1),
text_color = color.black,
eternal = True
)
# “Save” button
Button(
'Save',
position = Vec3(-0.6127, -0.1932, -0.895),
color = color.white,
on_click = save,
highlight_color = color.light_gray, # ← this line
scale = (.5, .1),
text_color = color.black,
eternal = True
)
# “Load” button
Button(
'Load',
position = Vec3(-0.6127, -0.3032, -0.895),
color = color.white,
highlight_color = color.light_gray, # ← this line
on_click = load,
scale = (.5, .1),
text_color = color.black,
eternal = True
)
# Ground grid (eternal)
Entity(
model = Grid(512, 512),
rotation_x = 90,
scale = 512,
color = color.white33,
x = .5,
z = .5,
y = -.5,
eternal = True
)
# Sky and EditorCamera (both eternal)
Sky(eternal=True)
EditorCamera(eternal=True)
refresh_container()
app.run()
"""
Below are several ideas—ranging from small usability tweaks to larger feature additions—that can make your in‐scene editor even more powerful, intuitive, and robust. You can pick and choose whichever feel most useful for your workflow.
---
## 1. On‐Screen Numeric Input & Sliders (Instead of Tkinter Pop‐ups)
### Why
Having to type every model/texture/collider name into separate Tkinter dialogs can be interruptive. An in‐Ursina UI panel with text fields and sliders lets you stay in one window.
### How
1. **Create a collapsible UI panel** (e.g. a `Panel` or a `Window`‐like `Entity` parented to `camera.ui`) that appears when you click “Add new Object.”
2. Inside that panel, place `InputField` objects for:
* Object Name
* Model Name (drop‐down or free‐form)
* Texture Name (drop‐down or free‐form)
* Collider Type (drop‐down of `None`, `box`, `sphere`, `mesh`)
Example:
```python
panel = Entity(parent=camera.ui, model='quad', color=color.rgba(30,30,30,200),
scale=(.6, .7), x=0.2, y=0.2, visible=False) # hidden by default
name_input = InputField(parent=panel, hint='Object Name', x=-.2, y=0.25, scale=(.4,.07))
model_input = InputField(parent=panel, hint='Model (e.g. cube)', x=-.2, y=0.1, scale=(.4,.07))
texture_input = InputField(parent=panel, hint='Texture (e.g. grass)', x=-.2, y=-.05, scale=(.4,.07))
collider_input = InputField(parent=panel, hint='Collider (box/sphere/…)', x=-.2, y=-.2, scale=(.4,.07))
Button('Create', parent=panel, x=0.25, y=-.35, scale=(.3,.1),
on_click=lambda: finalize_new_object())
Button('Cancel', parent=panel, x=-0.25, y=-.35, scale=(.3,.1),
on_click=lambda: setattr(panel, 'visible', False))
```
3. When the user clicks **“Add new Object,”** set `panel.visible = True`.
4. In `finalize_new_object()`, read `name_input.text`, etc., and do exactly what `addnew()` did (create the entity, attach `DebugBehaviour()`, append to `objects`). Then hide the panel.
> **Benefit:**
> • No more external windows popping up.
> • You can also add sliders right in this panel for initial scale, rotation, or position if you want more control.
---
## 2. Visual Move/Rotate/Scale Gizmos
### Why
Clicking and holding arrow keys works, but it’s hard to know exactly which axis you’re transforming at a glance. A gizmo (colored arrows/handles for X/Y/Z) gives clear, direct manipulation.
### How
* **Add a “Gizmo” entity** that appears around the selected object:
1. When an entity is selected, spawn three arrow models (one for X, one for Y, one for Z) at its position. Color them:
* X‐axis: red
* Y‐axis: green
* Z‐axis: blue
2. Detect when the user clicks+drags on one of these arrows:
* Raycast from the mouse into the world (using `mouse.point` or `mouse.world_point`) to see if the cursor intersects one of your arrow colliders.
* If they click and drag along that arrow’s axis, move the object along that axis in real time.
Example approach (pseudocode):
```python
class Gizmo:
def __init__(self, target_entity):
self.target = target_entity
self.arrows = {
'x': Entity(model='arrow.obj', color=color.red, parent=target_entity, scale=0.5, rotation=Vec3(0,0,90)),
'y': Entity(model='arrow.obj', color=color.green, parent=target_entity, scale=0.5),
'z': Entity(model='arrow.obj', color=color.blue, parent=target_entity, scale=0.5, rotation=Vec3(90,0,0))
}
for e in self.arrows.values():
e.collider = 'box'
e.always_on_top = True # so you can click it even if behind the mesh
self.selected_axis = None
def update(self):
# Position the gizmo at target’s pivot each frame
for axis, arrow in self.arrows.items():
arrow.world_parent = None
arrow.position = self.target.world_position
arrow.rotation = arrow.rotation # keep arrow oriented
# Raycast to see if mouse is hovering any arrow; highlight it
if held_keys['left mouse']:
hit_info = raycast(camera.world_position, camera.forward, ignore=(self.target,), distance=100)
if hit_info.entity in self.arrows.values():
self.selected_axis = [k for k,v in self.arrows.items() if v == hit_info.entity][0]
else:
self.selected_axis = None
if self.selected_axis:
# While mouse is down, project mouse movement onto that axis and move target
# e.g. compute delta = (mouse.world_point - last_point).dot(axis_vector)
# target.position += axis_vector * delta
pass
```
* **Attach the gizmo when an entity becomes selected** (inside `DebugBehaviour.toggle()`). Remove it when deselected or when entering Delete mode.
> **Benefit:**
> • More precise, visual control, just like in Unity/Blender.
> • Axis‐locking comes for free—drag only along the colored arrow.
---
## 3. Multi‐Select & Group Transforms
### Why
Often you want to move or rotate several objects together (e.g. a table + chairs). Right now you can only select one at a time.
### How
1. **Shift‐Click or Ctrl‐Click to Add to Selection**
* Maintain a `selected_entities = []` list instead of a single `selecting`.
* If you click an entity while holding **Shift**, append it to `selected_entities` if it isn’t already there; otherwise remove it from the list.
* If you click without any modifier, clear `selected_entities` and only select that one.
2. **Drawing a Selection Rectangle**
* On **mouse down**, record `mouse.screen_position`. On **mouse up**, measure the rectangle area and check which entities’ screen‐space bounding boxes fall inside, adding them to `selected_entities`.
3. **Transform All Together**
* In your `update()` and `input()` logic, if `selected_entities` has more than one, apply the same delta to each entity.
* You might also generate a single “group pivot” (e.g. the centroid) and rotate/scale around that.
Example snippet:
```python
selected_entities = []
def debug_toggle(self):
global selected_entities
if deleting:
destroy(self.entity)
if self.entity in objects: objects.remove(self.entity)
return
if held_keys['shift']:
# multi‐select logic
if self.entity in [d.entity for d in selected_entities]:
# deselect this one
for d in selected_entities:
if d.entity == self.entity:
# restore its color
if d._orig_color: d.entity.color = d._orig_color
selected_entities.remove(d)
break
else:
# add to selection
self._orig_color = self.entity.color
self.entity.color = color.azure
selected_entities.append(self)
else:
# single‐select mode: clear old selection, then pick this
for d in selected_entities:
if d._orig_color: d.entity.color = d._orig_color
selected_entities = [self]
if self._orig_color is None:
self._orig_color = self.entity.color
self.entity.color = color.azure
```
> **Benefit:**
> • Faster batch operations, e.g. line up chairs in one go.
> • More flexible scene composition.
---
## 4. Undo / Redo Stack
### Why
Everyone makes mistakes. If you accidentally move or delete something, it’s good to have an “undo” command (Ctrl+Z, Ctrl+Y).
### How
1. **Data Structure**
* Maintain two stacks: `undo_stack` and `redo_stack`.
* Each “action” you do (move, rotate, scale, delete, create) pushes a record onto `undo_stack` describing how to revert it. E.g. `{ type: 'move', entity: e, from: old_pos, to: new_pos }`.
2. **Capturing Actions**
* In `DebugBehaviour.update()` or `input()`, detect the moment a transformation starts (e.g. first frame arrow key is pressed), record the entity’s original position/rotation/scale, then on key‐release, record the final state.
* For deletes, record `{ type: 'delete', entity_repr: repr(e) }` so you can recreate it on undo. For create, record `{ type: 'create', entity: e }`.
3. **Implementing Undo**
* Hook `input('control z')` at the top‐level. When triggered, pop from `undo_stack` and:
* For `'move'`: set `entity.position = from`;
* For `'rotate'`: set `entity.rotation = from`;
* For `'scale'`: set `entity.scale = from`;
* For `'delete'`: re‐`exec(entity_repr)` or regenerate the entity from the saved state;
* For `'create'`: destroy the newly created entity.
* Then push the inverse action onto `redo_stack` so you can redo it.
4. **Implementing Redo**
* Hook `input('control y')`. Pop from `redo_stack` and reapply that action, then push back onto `undo_stack`.
> **Benefit:**
> • Safer editing—no accidental data loss.
> • Encourages “playful” experimentation because you know you can always step back.
---
## 5. JSON or Custom Scene Format (Instead of Raw Python)
### Why
Writing a `.py` via `repr(entity)` is simple, but brittle:
* If the user or engine changes default parameters, reloading might break.
* Arbitrary code injection risk if someone edits `scene.py` by hand.
* Harder to extend to lights, cameras, scripts with custom logic.
### How
1. **Define a JSON schema** for each entity:
```json
{
"name": "Cube",
"model": "cube",
"texture": "grass",
"position": [1, 2, 3],
"rotation": [0, 45, 0],
"scale": [1, 1, 1],
"collider": "box",
"color": [255, 255, 255, 255]
}
```
2. **Serialization** (`save()`):
* Loop through `objects` and build a Python list of dicts, then `json.dump()` it to `scene.json`.
```python
import json
def save_json():
data = []
for e in objects:
data.append({
"name": e.name,
"model": e.model.name if hasattr(e.model, 'name') else str(e.model),
"texture": e.texture.name if hasattr(e.texture, 'name') else str(e.texture),
"position": [e.x, e.y, e.z],
"rotation": [e.rotation_x, e.rotation_y, e.rotation_z],
"scale": [e.scale_x, e.scale_y, e.scale_z],
"collider": e.collider,
"color": [e.color.r, e.color.g, e.color.b, e.color.a],
})
with open('scene.json', 'w') as f:
json.dump(data, f, indent=2)
```
3. **Deserialization** (`load_json()`):
* `json.load()` the file, then for each entry do:
```python
e = Entity(
name = obj["name"],
model = obj["model"],
texture = obj["texture"],
position = tuple(obj["position"]),
rotation = tuple(obj["rotation"]),
scale = tuple(obj["scale"]),
collider = obj["collider"],
color = tuple(obj["color"]),
)
e.add_script(DebugBehaviour())
objects.append(e)
```
* Clear the scene exactly as before, then call `load_json()`.
> **Benefit:**
> • Safer and more portable format.
> • Easier for other tools or scripts to inspect/modify your scene.
> • You can extend it later (e.g. save lights, cameras, custom scripts).
---
## 6. Visual Feedback for Snap Grid and Axis
### Why
When snapping is on, it helps to see the grid spacing (especially if `snap_size` is something like 0.5 or 2.0). Also—if you do axis‐locked transforms—it’s good to see an indicator on the entity.
### How
1. **Grid Overlay**
* When the user toggles Snap (and if `snap_size > 1`), render a thin wireframe grid on the ground that matches the snap spacing.
* Example:
```python
def show_grid_overlay():
# destroy existing overlay if any
if hasattr(self, 'grid_overlay'): destroy(self.grid_overlay)
size = 20 # half‐extent in world units
lines = []
step = snap_size
for i in range(-int(size/step), int(size/step)+1):
# vertical lines
lines.append(Entity(model=Mesh(vertices=[(-size, 0, i*step), (size, 0, i*step)]), color=color.gray))
# horizontal lines
lines.append(Entity(model=Mesh(vertices=[(i*step, 0, -size), (i*step, 0, size)]), color=color.gray))
self.grid_overlay = Entity() # parent for all lines
for l in lines: l.parent = self.grid_overlay
```
* Call `show_grid_overlay()` whenever `snap_enabled` toggles on, and hide/destroy those lines when you toggle off.
2. **Axis Highlight**
* When you begin dragging on a gizmo arrow (from section 2), change that arrow’s color to a brighter shade (e.g. from `red` to `color.orange`) and/or throttle the other two arrows to a darker gray so you know which axis is active.
---
## 7. “Duplicate” / “Clone” Functionality
### Why
Rather than “Add new Object” from scratch, often you want to duplicate an existing object (including its position/rotation/scale/texture).
### How
1. When an entity is selected, offer a keyboard shortcut (e.g. **Ctrl+D**) or a button labeled “Duplicate.”
2. In your handler, do:
```python
if selected_entity:
orig = selected_entity.entity
clone = Entity(
name = f"{orig.name}_copy",
model = orig.model.name if hasattr(orig.model, 'name') else orig.model,
texture = orig.texture.name if hasattr(orig.texture, 'name') else orig.texture,
position = orig.position + Vec3(1,0,1), # offset so they’re not exactly overlapped
rotation = orig.rotation,
scale = orig.scale,
collider = orig.collider,
color = orig.color
)
clone.add_script(DebugBehaviour())
objects.append(clone)
# Deselect the old one, select the clone
selected_entity.entity.color = selected_entity._orig_color
selecting = None
# Now select clone:
db = clone.scripts[-1] # assuming it’s the only script
db._orig_color = clone.color
clone.color = color.azure
selecting = db
```
* This gives you a brand‐new object identical to the old one, just moved slightly.
> **Benefit:**
> • Speeds up scene building.
> • Encourages rapid iteration.
---
## 8. Context Menus & Right‐Click Actions
### Why
Right now you click to select or delete (if in Delete mode). It might be more intuitive if right‐clicking brought up a small menu (e.g. “Delete,” “Duplicate,” “Rename,” “Change Texture”).
### How
1. In `DebugBehaviour.input()`, watch for `key == 'right mouse down'` when `selecting == self`.
2. Spawn a tiny context panel near the mouse screen position:
```python
def on_right_click(self):
if self.menu: self.menu.enabled = False
self.menu = Entity(parent=camera.ui, model='quad', scale=(.2,.2), color=color.rgba(50,50,50,200))
# Position it at mouse.screen_position
self.menu.position = mouse.screen_position
Button('Delete', parent=self.menu, y=0.05, scale=(.9,.2),
on_click=lambda: (toggleDelete(), self.menu.disable()))
Button('Duplicate', parent=self.menu, y=-0.1, scale=(.9,.2),
on_click=lambda: (duplicate_entity(self.entity), self.menu.disable()))
# etc…
```
3. Tie that into `toggle()` logic so you don’t conflict with left‐click select.
> **Benefit:**
> • More discoverable options per‐object without cluttering the main UI.
> • Conveys additional operations (rename, change texture) right at the entity.
---
## 9. Property Inspector Panel
### Why
Seeing raw numbers for position/rotation/scale is nice, but sometimes you want exact control or to tweak something by typing “3.47” or “45°.”
### How
1. Create a second panel on the side (or bottom) that displays the currently selected entity’s properties in real time:
* `InputField` for X, Y, Z
* `InputField` for rotation\_x, rotation\_y, rotation\_z
* `InputField` for scale\_x, scale\_y, scale\_z
* Buttons or drop‐downs to change `model`, `texture`, `collider`, `color`.
2. Whenever `selecting` changes, populate those fields with the selected entity’s `.position`, `.rotation`, `.scale`.
3. Whenever you type into those fields and press Enter, immediately apply that value to the entity. For instance:
```python
def on_pos_x_enter():
if selecting:
try:
selecting.entity.x = float(pos_x_input.text)
except:
pass # invalid number → ignore
```
4. Hook all six fields similarly, plus `on_attr_change` for `model_input`, `texture_input`, etc., so you can type a new model name (e.g. “sphere”) and it updates the mesh on the fly.
> **Benefit:**
> • Precise numeric control without relying solely on key-press increments.
> • A central “inspector” just like professional editors.
---
## 10. Improved Error Handling & Validation
### Why
Right now if you type a bogus model name, nothing appears, and you get no feedback. It’s better to give an error message or disable the “Create” button until inputs are valid.
### How
1. **Validate Model & Texture**
* Before spawning, check `application.asset_folder` or `loader.models` to see if `model_input.text` actually exists. If not, show a small red warning text underneath the input.
* Similarly for `texture_input.text`: verify `load_texture(texture_input.text)` succeeds or catch an exception and display an error.
2. **Disable “Create” Until Valid**
* In your `on_text_changed` callback for `model_input`/`texture_input`, run validation. If everything is OK, set the “Create” button’s `enabled = True`; otherwise `enabled = False`.
> **Benefit:**
> • Prevents frustration from invisible objects.
> • Provides immediate feedback for typos.
---
## 11. Tooltips & Help Overlay
### Why
With all these advanced features (snap modes, gizmos, context menus), it helps to have a quick reference.
### How
1. **Tooltips on Hover**
* For each button, set `button.tooltip = "…"`. Ursina will show a small popup text when the mouse lingers. E.g. `delete_button.tooltip = "Toggle delete mode (click object to delete)"`.
2. **Help Panel**
* A small “?” icon on the UI that, when clicked, toggles a translucent overlay explaining:
* “Arrows = move; Shift+arrows = scale; X/Z = rotate around Y; C/V = rotate around X; B/N = rotate around Z.”
* “Alt + arrows = snap ±1; Ctrl + modifier = fine movement.”
* “Esc = unselect; Ctrl+Z = undo; Ctrl+D = duplicate.”
* “Right‐click entity = context menu….”
> **Benefit:**
> • Speeds up the learning curve for new users.
> • Reduces need for external documentation.
---
## 12. Performance & Clean‐Up
### Why
As scenes grow large, constantly updating every `DebugBehaviour` (and potentially dozens of gizmos, grid lines, etc.) can slow down the frame rate.
### How
1. **Only Update When Necessary**
* In `DebugBehaviour.update()`, wrap heavy logic in a check so that if this entity isn’t near the camera or not selected, you skip certain checks (e.g. snapping or gizmo positioning).
* For entities outside some radius, you might temporarily disable their scripts until the camera approaches.
2. **Batch Grid Drawing**
* Instead of spawning hundreds of separate line entities for the snap grid overlay, use one `Mesh` with all lines in a single vertex buffer. This draws faster.
3. **Destroy Gizmos & Overlays Promptly**
* When you deselect or close panels, make sure to call `destroy(gizmo_entity)` or `destroy(grid_overlay)`. Lingering hidden Entities still consume CPU/GPU time if they’re constantly in the render/update loop.
> **Benefit:**
> • Keeps the frame rate high even as complexity grows.
> • Avoids memory leaks from forgotten UI elements.
---
## 13. Custom Script Attachment & Parameterized Entities
### Why
Maybe you want some entities in your scene to run custom behavior scripts (e.g. a rotating fan, a bouncing ball). It’s convenient to let the editor assign scripts to objects.
### How
1. In your “Add New Object” panel (from section 1), add a drop‐down listing all available script classes (e.g. `SpinForever`, `FloatUpDown`, etc.).
2. When creating the entity, call `e.add_script(ChosenScript())`.
3. In your JSON or Python‐based save format, include a `"script": "SpinForever"` field so that on load you do:
```python
if 'script' in obj:
cls = getattr(sys.modules['my_game_scripts'], obj['script'])
e.add_script(cls())
```
> **Benefit:**
> • Integrates level editing with gameplay logic.
> • You can prototype interactive objects directly in the editor.
---