Skip to content

Commit e866541

Browse files
committed
Scene fixes with ids
1 parent fce704a commit e866541

File tree

8 files changed

+113
-48
lines changed

8 files changed

+113
-48
lines changed

src/CommonFunctions.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function destroy_entity end
2020
function destroy_ui_element end
2121
function draw end
2222
function flip end
23+
function generate_uuid end
2324
function get_last_update end
2425
function get_offset end
2526
function get_parent end

src/Entity.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module EntityModule
3030
sprite::Union{InternalSprite, Ptr{Nothing}}
3131
transform::Transform
3232

33-
function Entity(name::String = "New entity", id::String = generate_uuid(), transform::Transform = Transform(), scripts::Vector = [])
33+
function Entity(name::String = "New entity", id::String = JulGame.generate_uuid(), transform::Transform = Transform(), scripts::Vector = [])
3434
this = new()
3535

3636
this.id = id
@@ -169,7 +169,7 @@ module EntityModule
169169
return this.shape
170170
end
171171

172-
function generate_uuid()
172+
function JulGame.generate_uuid()
173173
return string(UUIDs.uuid4())
174174
end
175175
end

src/editor/JulGameEditor/Components/ComponentInputs.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,13 @@ function show_sprite_fields(sprite, animation_window_dict)
325325
# animation_window_dict[]["frame $(k)"][]["points"] = points
326326
window_info[] = animation_window_dict[][key][]
327327
else
328+
print("Adding crop window info for: $key")
328329
animation_window_dict[][key] = window_info
329330
end
330331

331-
crop_x, crop_y, crop_w, crop_h = show_animation_window("crop", window_info, sprite.texture, sprite.size.x, sprite.size.y)
332+
CImGui.PushID(sprite.parent.id)
333+
crop_x, crop_y, crop_w, crop_h = show_animation_window("crop", window_info, sprite.texture, sprite.size.x, sprite.size.y)
334+
CImGui.PopID()
332335
vec4i = Cint[crop_x, crop_y, crop_w, crop_h]
333336
@c CImGui.InputInt4("crop", vec4i)
334337
sprite.crop = JulGame.Math.Vector4(Int32(vec4i[1]), Int32(vec4i[2]), Int32(vec4i[3]), Int32(vec4i[4]))

src/editor/JulGameEditor/Components/MainMenuBar.jl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ Create a fullscreen menu bar and populate it.
99
# Arguments
1010
- `events`: An array of event functions. These are callbacks that are triggered when the user selects a menu item.
1111
"""
12-
function show_main_menu_bar(events)
12+
function show_main_menu_bar(events, main)
1313
if CImGui.BeginMainMenuBar()
1414
@cstatic buf="File"*"\0"^128 begin
1515
if CImGui.BeginMenu(buf)
1616
ShowMenuFile(events)
1717
CImGui.EndMenu()
1818
end
19+
end
20+
21+
@cstatic buf="Scene"*"\0"^128 begin
22+
if main !== nothing && CImGui.BeginMenu(buf)
23+
show_scene_menu(events)
24+
CImGui.EndMenu()
25+
end
1926

20-
CImGui.EndMainMenuBar()
2127
end
28+
CImGui.EndMainMenuBar()
2229
end
2330
end
2431

@@ -31,10 +38,23 @@ Show the file menu in the main menu bar.
3138
- `events`: An array of event functions. These are callbacks that are triggered when the user selects a menu item.
3239
"""
3340
function ShowMenuFile(events)
34-
if CImGui.MenuItem("Open", "Ctrl+O")
35-
events[end]()
41+
if CImGui.MenuItem("Open Project", "Ctrl+O")
42+
events["Select-project"]()
3643
end
37-
if length(events) > 1 && CImGui.MenuItem("Save", "Ctrl+S")
38-
events[1]()
44+
end
45+
46+
function show_scene_menu(events)
47+
if CImGui.MenuItem("Save", "Ctrl+S")
48+
events["Save"]()
49+
end
50+
if CImGui.MenuItem("Reset Camera", "Ctrl+R")
51+
events["Reset-camera"]()
3952
end
53+
54+
if CImGui.BeginMenu("Extras")
55+
if CImGui.MenuItem("Regenerate Ids")
56+
events["Regenerate-ids"]()
57+
end
58+
CImGui.EndMenu()
59+
end
4060
end

src/editor/JulGameEditor/Components/SceneViewer.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function handle_mouse_click_duplication(main)
134134
end
135135

136136
copy = deepcopy(main.selectedEntity)
137+
copy.id = JulGame.generate_uuid()
137138
push!(main.scene.entities, copy)
138139
main.selectedEntity = copy
139140
end

src/editor/JulGameEditor/Components/SpriteViewer.jl

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
1818
# UI elements
1919
# grid step int input as slider with range. Min = 1, Max = 64
2020
CImGui.SliderInt("Grid step", window_info[]["grid_step"], 1, 64, "%d")
21-
CImGui.Text("Mouse Left: drag to add square,\nMouse Right: drag to scroll, click for context menu.\nCTRL+Mouse Wheel: zoom")
21+
#CImGui.Text("Mouse Left: drag to add square,\nMouse Right: drag to scroll, click for context menu.\nCTRL+Mouse Wheel: zoom")
2222
selectedPoint1 = length(window_info[]["points"][]) > 0 ? window_info[]["points"][][end-1] : ImVec2(0,0)
2323
selectedPoint2 = length(window_info[]["points"][]) > 0 ? window_info[]["points"][][end] : ImVec2(0,0)
2424
CImGui.Text("Current selection: x:$(selectedPoint1.x),y:$(selectedPoint1.y) w:$(selectedPoint2.x - selectedPoint1.x),h:$(selectedPoint2.y - selectedPoint1.y)")
@@ -47,19 +47,33 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
4747
window_info[]["scrolling"][] = ImVec2(max(window_info[]["scrolling"][].x, -canvas_max.x), max(window_info[]["scrolling"][].y, -canvas_max.y))
4848
origin = ImVec2(min(0, 0 + window_info[]["scrolling"][].x), min(0, 0 + window_info[]["scrolling"][].y)) # Lock scrolled origin
4949
mouse_pos_in_canvas = ImVec2(unsafe_load(io.MousePos).x - canvas_p0.x, unsafe_load(io.MousePos).y - canvas_p0.y)
50+
mouse_pos_in_canvas_zoom_adjusted = ImVec2(floor(mouse_pos_in_canvas.x / window_info[]["zoom_level"][]), floor(mouse_pos_in_canvas.y / window_info[]["zoom_level"][]))
51+
CImGui.Text("Zoom level: $(window_info[]["zoom_level"][])")
52+
CImGui.Text("origin: $(origin.x), $(origin.y)")
53+
CImGui.Text("Origin divided by zoom: $(origin.x / window_info[]["zoom_level"][]), $(origin.y / window_info[]["zoom_level"][])")
54+
CImGui.Text("Origin * zoom: $(origin.x * window_info[]["zoom_level"][]), $(origin.y * window_info[]["zoom_level"][])")
5055
CImGui.Text("Mouse Position: $(mouse_pos_in_canvas.x), $(mouse_pos_in_canvas.y)")
5156
CImGui.Text("Mouse Pixel: $(floor(mouse_pos_in_canvas.x / window_info[]["zoom_level"][])), $(floor(mouse_pos_in_canvas.y / window_info[]["zoom_level"][]))")
52-
53-
mouse_pos_in_canvas_zoom_adjusted = ImVec2(floor(mouse_pos_in_canvas.x / window_info[]["zoom_level"][]), floor(mouse_pos_in_canvas.y / window_info[]["zoom_level"][]))
57+
CImGui.Text("Mouse position in canvas adj: $(mouse_pos_in_canvas_zoom_adjusted.x), $(mouse_pos_in_canvas_zoom_adjusted.y)")
58+
CImGui.Text("Mouse position in canvas + origin: $(origin.x + mouse_pos_in_canvas_zoom_adjusted.x), $(origin.y + mouse_pos_in_canvas_zoom_adjusted.y)")
5459
#rounded = ImVec2(round(mouse_pos_in_canvas_zoom_adjusted.x/ window_info[]["zoom_level"][]) * window_info[]["zoom_level"][], round(mouse_pos_in_canvas_zoom_adjusted.y/ window_info[]["zoom_level"][]) * window_info[]["zoom_level"][])
5560
# Add first and second point
5661
if is_hovered && !window_info[]["adding_line"][] && CImGui.IsMouseClicked(CImGui.ImGuiMouseButton_Left)
57-
push!(window_info[]["points"][], mouse_pos_in_canvas_zoom_adjusted)
58-
push!(window_info[]["points"][], mouse_pos_in_canvas_zoom_adjusted)
62+
if unsafe_load(io.KeyShift)
63+
push!(window_info[]["points"][], ImVec2(mouse_pos_in_canvas_zoom_adjusted.x - round(origin.x* window_info[]["zoom_level"][]), mouse_pos_in_canvas_zoom_adjusted.y - round(origin.y* window_info[]["zoom_level"][])))
64+
push!(window_info[]["points"][], ImVec2(mouse_pos_in_canvas_zoom_adjusted.x - round(origin.x* window_info[]["zoom_level"][]), mouse_pos_in_canvas_zoom_adjusted.y - round(origin.y* window_info[]["zoom_level"][])))
65+
else
66+
push!(window_info[]["points"][], ImVec2(mouse_pos_in_canvas_zoom_adjusted.x, mouse_pos_in_canvas_zoom_adjusted.y))
67+
push!(window_info[]["points"][], ImVec2(mouse_pos_in_canvas_zoom_adjusted.x, mouse_pos_in_canvas_zoom_adjusted.y))
68+
end
5969
window_info[]["adding_line"][] = true
6070
end
6171
if window_info[]["adding_line"][]
62-
window_info[]["points"][][end] = mouse_pos_in_canvas_zoom_adjusted
72+
if unsafe_load(io.KeyShift)
73+
window_info[]["points"][][end] = ImVec2(mouse_pos_in_canvas_zoom_adjusted.x - round(origin.x* window_info[]["zoom_level"][]), mouse_pos_in_canvas_zoom_adjusted.y - round(origin.y* window_info[]["zoom_level"][]))
74+
else
75+
window_info[]["points"][][end] = ImVec2(mouse_pos_in_canvas_zoom_adjusted.x, mouse_pos_in_canvas_zoom_adjusted.y)
76+
end
6377
if is_hovered && !CImGui.IsMouseDown(CImGui.ImGuiMouseButton_Left)
6478
window_info[]["points"][] = [window_info[]["points"][][end-1], window_info[]["points"][][end]] # only keep last two points
6579
window_info[]["adding_line"][] = false
@@ -74,30 +88,21 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
7488

7589
# Zoom
7690
if is_hovered && unsafe_load(io.KeyCtrl)
77-
window_info[]["zoom_level"][] += unsafe_load(io.MouseWheel) * 4.0 # * 0.10
78-
window_info[]["zoom_level"][] = clamp(window_info[]["zoom_level"][], 1.0, 50.0)
91+
if unsafe_load(io.MouseWheel) == 1.0
92+
window_info[]["zoom_level"][] = clamp(window_info[]["zoom_level"][] * 4, 1.0, 50.0)
93+
elseif unsafe_load(io.MouseWheel) == -1.0
94+
window_info[]["zoom_level"][] = clamp(window_info[]["zoom_level"][] / 4, 1.0, 50.0)
95+
end
7996
end
8097

8198
# Context menu
8299
drag_delta = CImGui.GetMouseDragDelta(CImGui.ImGuiMouseButton_Right)
83100
if is_hovered && CImGui.IsMouseReleased(CImGui.ImGuiMouseButton_Right) && drag_delta.x == 0.0 && drag_delta.y == 0.0
84-
CImGui.OpenPopupOnItemClick("context")
85-
end
86-
if CImGui.BeginPopup("context")
87-
if window_info[]["adding_line"][]
88-
resize!(window_info[]["points"][], length(window_info[]["points"][]) - 2)
89-
end
90-
window_info[]["adding_line"][] = false
91-
if CImGui.MenuItem("Remove one", "", false, length(window_info[]["points"][]) > 0)
92-
resize!(window_info[]["points"][], length(window_info[]["points"][]) - 2)
93-
end
94-
if CImGui.MenuItem("Remove all", "", false, length(window_info[]["points"][]) > 0)
95-
empty!(window_info[]["points"][])
96-
end
97-
CImGui.EndPopup()
101+
#TODO: CImGui.OpenPopupOnItemClick("context-1")
102+
empty!(window_info[]["points"][])
98103
end
99104

100-
# Draw grid and lines
105+
# Draw grid and lines -- Keep all draw calls between PushClipRect and PopClipRect
101106
CImGui.PushClipRect(draw_list, canvas_p0, canvas_p1, true)
102107
GRID_STEP = window_info[]["grid_step"][] * window_info[]["zoom_level"][]
103108

@@ -107,18 +112,29 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
107112
for y in 0:GRID_STEP:canvas_sz.y*10 # TODO: 10 is arbitrary
108113
CImGui.AddLine(draw_list, ImVec2(canvas_p0.x, origin.y + canvas_p0.y + y), ImVec2(canvas_p1.x, origin.y + canvas_p0.y + y), IM_COL32(200, 200, 200, 40))
109114
end
110-
CImGui.PopClipRect(draw_list)
111-
112-
CImGui.AddImage(draw_list, my_tex_id, ImVec2(origin.x + canvas_p0.x, origin.y + canvas_p0.y), ImVec2(origin.x + (my_tex_w * window_info[]["zoom_level"][]) + canvas_p0.x, origin.y + (my_tex_h * window_info[]["zoom_level"][]) + canvas_p0.y), ImVec2(0,0), ImVec2(1,1), IM_COL32(255,255,255,255))
113-
114-
# Draw squares with add rect
115-
for n in 1:2:length(window_info[]["points"][])-1
116-
p1 = ImVec2(origin.x + canvas_p0.x + (window_info[]["points"][][n].x * window_info[]["zoom_level"][]), origin.y + canvas_p0.y + (window_info[]["points"][][n].y * window_info[]["zoom_level"][]))
117-
p2 = ImVec2(origin.x + canvas_p0.x + (window_info[]["points"][][n+1].x * window_info[]["zoom_level"][]), origin.y + canvas_p0.y + (window_info[]["points"][][n+1].y * window_info[]["zoom_level"][]))
118-
# scale to zoom level
119-
CImGui.AddRect(draw_list, p1, p2, IM_COL32(255, 255, 0, 255))
120-
end
121115

116+
# Draw image that is being inspected
117+
CImGui.AddImage(draw_list, my_tex_id, ImVec2(origin.x + canvas_p0.x, origin.y + canvas_p0.y), ImVec2(origin.x + (my_tex_w * window_info[]["zoom_level"][]) + canvas_p0.x, origin.y + (my_tex_h * window_info[]["zoom_level"][]) + canvas_p0.y), ImVec2(0,0), ImVec2(1,1), IM_COL32(255,255,255,255))
118+
119+
# Draw squares with add rect
120+
for n in 1:2:length(window_info[]["points"][])-1
121+
p1 = ImVec2(origin.x + canvas_p0.x + (window_info[]["points"][][n].x * window_info[]["zoom_level"][]), origin.y + canvas_p0.y + (window_info[]["points"][][n].y * window_info[]["zoom_level"][]))
122+
p2 = ImVec2(origin.x + canvas_p0.x + (window_info[]["points"][][n+1].x * window_info[]["zoom_level"][]), origin.y + canvas_p0.y + (window_info[]["points"][][n+1].y * window_info[]["zoom_level"][]))
123+
# scale to zoom level
124+
CImGui.AddRect(draw_list, p1, p2, IM_COL32(255, 255, 0, 255))
125+
end
126+
127+
if CImGui.BeginPopup("context-1")
128+
# if window_info[]["adding_line"][]
129+
# resize!(window_info[]["points"][], length(window_info[]["points"][]) - 2)
130+
# end
131+
# window_info[]["adding_line"][] = false
132+
if CImGui.MenuItem("Remove all", "", false, length(window_info[]["points"][]) > 0)
133+
empty!(window_info[]["points"][])
134+
end
135+
CImGui.EndPopup()
136+
end
137+
CImGui.PopClipRect(draw_list)
122138

123139
CImGui.End()
124140

src/editor/JulGameEditor/Editor.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ module Editor
7373
################################## RENDER HERE
7474

7575
################################# MAIN MENU BAR
76-
events = []
76+
events = Dict{String, Function}()
7777
if currentSceneMain !== nothing
78-
push!(events, save_scene_event(currentSceneMain.scene.entities, currentSceneMain.scene.uiElements, currentSelectedProjectPath, String(currentSceneName)))
78+
events["Save"] = save_scene_event(currentSceneMain.scene.entities, currentSceneMain.scene.uiElements, currentSelectedProjectPath, String(currentSceneName))
7979
end
80-
push!(events, select_project_event(currentSceneMain, scenesLoadedFromFolder))
81-
show_main_menu_bar(events)
80+
events["Select-project"] = select_project_event(currentSceneMain, scenesLoadedFromFolder)
81+
events["Reset-camera"] = reset_camera_event(currentSceneMain)
82+
events["Regenerate-ids"] = regenerate_ids_event(currentSceneMain)
83+
show_main_menu_bar(events, currentSceneMain)
8284
################################# END MAIN MENU BAR
8385

8486
@c CImGui.ShowDemoWindow(Ref{Bool}(showDemoWindow)) # Uncomment this line to show the demo window and see available widgets
@@ -254,6 +256,7 @@ module Editor
254256
CImGui.Separator()
255257
if CImGui.Button("Duplicate")
256258
copy = deepcopy(currentSceneMain.selectedEntity)
259+
copy.id = JulGame.generate_uuid()
257260
push!(currentSceneMain.scene.entities, copy)
258261
currentSceneMain.selectedEntity = copy
259262
end
@@ -290,6 +293,7 @@ module Editor
290293
# CImGui.Separator()
291294
# if CImGui.Button("Duplicate")
292295
# push!(currentSceneMain.scene.uiElements, deepcopy(currentSceneMain.scene.uiElements[uiElementIndex]))
296+
# copy.id = JulGame.generate_uuid()
293297
# # TODO: switch to duplicated entity
294298
# end
295299

@@ -320,7 +324,7 @@ module Editor
320324
if currentSceneMain !== nothing
321325
if JulGame.InputModule.get_button_held_down(currentSceneMain.input, "LCTRL") && JulGame.InputModule.get_button_pressed(currentSceneMain.input, "S")
322326
@info string("Saving scene")
323-
events[1]()
327+
events["Save"]()
324328
end
325329
# delete selected entity
326330
if JulGame.InputModule.get_button_pressed(currentSceneMain.input, "DELETE")
@@ -331,6 +335,7 @@ module Editor
331335
# duplicate selected entity with ctrl+d
332336
if JulGame.InputModule.get_button_held_down(currentSceneMain.input, "LCTRL") && JulGame.InputModule.get_button_pressed(currentSceneMain.input, "D") && currentSceneMain.selectedEntity !== nothing
333337
copy = deepcopy(currentSceneMain.selectedEntity)
338+
copy.id = JulGame.generate_uuid()
334339
push!(currentSceneMain.scene.entities, copy)
335340
currentSceneMain.selectedEntity = copy
336341
end
@@ -340,6 +345,7 @@ module Editor
340345
if duplicationMode
341346
@info "Duplication mode on"
342347
copy = deepcopy(currentSceneMain.selectedEntity)
348+
copy.id = JulGame.generate_uuid()
343349
push!(currentSceneMain.scene.entities, copy)
344350
currentSceneMain.selectedEntity = copy
345351
else

src/editor/JulGameEditor/Utils/EditorUtils.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,22 @@ function select_all_elements_in_between(hierarchyEntitySelections, lastSelectedI
306306
hierarchyEntitySelections[i] = (hierarchyEntitySelections[i][1], true)
307307
end
308308
end
309+
end
310+
311+
function regenerate_ids_event(main)
312+
event = @event begin
313+
for index in eachindex(main.scene.entities)
314+
main.scene.entities[index].id = JulGame.generate_uuid()
315+
end
316+
end
317+
318+
return event
319+
end
320+
321+
function reset_camera_event(main)
322+
event = @event begin
323+
main.camera.position = Vec2(0, 0)
324+
end
325+
326+
return event
309327
end

0 commit comments

Comments
 (0)