Skip to content

Commit

Permalink
Added preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Aug 10, 2024
1 parent a0f355d commit 0e2a417
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 72 deletions.
16 changes: 16 additions & 0 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,23 @@ function show_animator_properties(animator)
CImGui.Button("Add Frame") && Component.append_array(animations[i])
CImGui.Button("Delete") && (deleteat!(animations, i); break;)
for k = eachindex(animations[i].frames)
if animator.parent.sprite != C_NULL && animator.parent.sprite !== nothing
sprite = animator.parent.sprite
show_image_with_hover_preview(sprite.texture, sprite.size.x, sprite.size.y, animations[i].frames[k])
CImGui.SameLine()
end
if CImGui.TreeNode("frame $(k)")
if animator.parent.sprite != C_NULL && animator.parent.sprite !== nothing
points = Ref(Vector{ImVec2}())
scrolling = Ref(ImVec2(0.0, 0.0))
adding_line = Ref(false)
zoom_level = Ref(1.0)
grid_step = Ref(Int32(64))

sprite = animator.parent.sprite
show_animation_window("frame $(k)", points, scrolling, adding_line, sprite.texture, sprite.size.x, sprite.size.y, zoom_level, grid_step)
end

vec = animations[i].frames[k]
vec4i = Cint[vec.x, vec.y, vec.z, vec.t]
@c CImGui.InputInt4("frame input $(k)", vec4i)
Expand Down
120 changes: 67 additions & 53 deletions src/editor/JulGameEditor/Components/SpriteViewer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,63 +36,18 @@ function load_texture_from_file(filename::String, renderer::Ptr{SDL2.SDL_Rendere
end


function get_center(rc::CImGui.ImRect)
return ImVec2((rc.Min.x + rc.Max.x) * 0.5, (rc.Min.y + rc.Max.y) * 0.5)
end

function get_width(rc::CImGui.ImRect)
return rc.Max.x - rc.Min.x
end

function get_size(rc::CImGui.ImRect)
return ImVec2(rc.Max.x - rc.Min.x, rc.Max.y - rc.Min.y)
end




"""
ShowExampleAppCustomRendering(p_open::Ref{Bool})
Demonstrate using the low-level ImDrawList to draw custom shapes.
"""
function ShowExampleAppCustomRendering(p_open::Ref{Bool}, points, scrolling, opt_enable_grid, opt_enable_context_menu, adding_line, my_tex_id, my_tex_w, my_tex_h, zoom_level, grid_step)
function show_animation_window(frame_name, points, scrolling, adding_line, my_tex_id, my_tex_w, my_tex_h, zoom_level, grid_step)
CImGui.SetNextWindowSize((350, 560), CImGui.ImGuiCond_FirstUseEver)
CImGui.Begin("Crop select", p_open) || (CImGui.End(); return)
CImGui.Begin("Animation - $(frame_name)") || (CImGui.End(); return)

draw_list = CImGui.GetWindowDrawList()

io = CImGui.GetIO()

CImGui.Text("$(my_tex_w)x$(my_tex_h)")
pos = CImGui.GetCursorScreenPos()
CImGui.Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), (0,0), (1,1), (255,255,255,255), (255,255,255,128))
if CImGui.IsItemHovered() && unsafe_load(io.KeyShift)
CImGui.BeginTooltip()
region_sz = min(32.0, min(my_tex_w, my_tex_h))
region_x = unsafe_load(io.MousePos).x - pos.x - region_sz * 0.5
if region_x < 0.0
region_x = 0.0
elseif region_x > my_tex_w - region_sz
region_x = my_tex_w - region_sz
end
region_y = unsafe_load(io.MousePos).y - pos.y - region_sz * 0.5
if region_y < 0.0
region_y = 0.0
elseif region_y > my_tex_h - region_sz
region_y = my_tex_h - region_sz
end
zoom = 4.0
CImGui.Text(string("Min: (%.2f, %.2f)", region_x, region_y))
CImGui.Text(string("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz))
uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h)
uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h)
CImGui.Image(my_tex_id, ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, (255,255,255,255), (255,255,255,128))
CImGui.EndTooltip()
end

# UI elements
CImGui.Checkbox("Enable grid", opt_enable_grid)
CImGui.Checkbox("Enable context menu", opt_enable_context_menu)
# grid step int input as slider with range. Min = 1, Max = 64
CImGui.SliderInt("Grid step", grid_step, 1, 64, "%d")
CImGui.Text("Mouse Left: drag to add square,\nMouse Right: drag to scroll, click for context menu.\nCTRL+Mouse Wheel: zoom")
Expand All @@ -108,7 +63,6 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool}, points, scrolling, opt
canvas_max = ImVec2(my_tex_w * 10, my_tex_h * 10)

# Draw border and background color
io = CImGui.GetIO()
draw_list = CImGui.GetWindowDrawList()
CImGui.AddRectFilled(draw_list, canvas_p0, canvas_p1, IM_COL32(50, 50, 50, 255))
CImGui.AddRect(draw_list, canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255))
Expand Down Expand Up @@ -142,7 +96,7 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool}, points, scrolling, opt
end

# Pan
mouse_threshold_for_pan = opt_enable_context_menu[] ? -1.0 : 0.0
mouse_threshold_for_pan = -1.0
if is_active && CImGui.IsMouseDragging(CImGui.ImGuiMouseButton_Right, mouse_threshold_for_pan)
scrolling[] = ImVec2(scrolling[].x + unsafe_load(io.MouseDelta).x, scrolling[].y + unsafe_load(io.MouseDelta).y)
end
Expand All @@ -155,7 +109,7 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool}, points, scrolling, opt

# Context menu
drag_delta = CImGui.GetMouseDragDelta(CImGui.ImGuiMouseButton_Right)
if opt_enable_context_menu[] && CImGui.IsMouseReleased(CImGui.ImGuiMouseButton_Right) && drag_delta.x == 0.0 && drag_delta.y == 0.0
if CImGui.IsMouseReleased(CImGui.ImGuiMouseButton_Right) && drag_delta.x == 0.0 && drag_delta.y == 0.0
CImGui.OpenPopupOnItemClick("context")
end
if CImGui.BeginPopup("context")
Expand All @@ -174,7 +128,6 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool}, points, scrolling, opt

# Draw grid and lines
CImGui.PushClipRect(draw_list, canvas_p0, canvas_p1, true)
if opt_enable_grid[]
GRID_STEP = grid_step[] * zoom_level[]

for x in 0:GRID_STEP:canvas_sz.x*10
Expand All @@ -183,7 +136,6 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool}, points, scrolling, opt
for y in 0:GRID_STEP:canvas_sz.y*10
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))
end
end

# Draw squares with add rect
for n in 1:2:length(points[])-1
Expand All @@ -197,4 +149,66 @@ function ShowExampleAppCustomRendering(p_open::Ref{Bool}, points, scrolling, opt
CImGui.PopClipRect(draw_list)

CImGui.End()
end
end

function show_image_with_hover_preview(my_tex_id, my_tex_w, my_tex_h, crop)
io = CImGui.GetIO()

x, y, w, h = crop.x, crop.y, crop.z, crop.t
W, H = my_tex_w, my_tex_h # dimensions of the texture or image

u1, v1, u2, v2 = rect_to_uv(x, y, w, h, W, H)
crop = true
if u1 == 0 && v1 == 0 && u2 == 0 && v2 == 0
u1, v1 = 0
u2, v2 = 1
crop = false
else
my_tex_w = w
my_tex_h = h
end
println("Start UV Coordinates: ($u1, $v1)")
println("End UV Coordinates: ($u2, $v2)")


# CImGui.Text("$(my_tex_w)x$(my_tex_h)")
pos = CImGui.GetCursorScreenPos()
CImGui.Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), (u1,v1), (u2,v2), (255,255,255,255), (255,255,255,128))
if CImGui.IsItemHovered() && unsafe_load(io.KeyShift)
CImGui.BeginTooltip()
region_sz = min(32.0, min(my_tex_w, my_tex_h))
region_x = unsafe_load(io.MousePos).x - pos.x - region_sz * 0.5
if region_x < 0.0
region_x = 0.0
elseif region_x > my_tex_w - region_sz
region_x = my_tex_w - region_sz
end
region_y = unsafe_load(io.MousePos).y - pos.y - region_sz * 0.5
if region_y < 0.0
region_y = 0.0
elseif region_y > my_tex_h - region_sz
region_y = my_tex_h - region_sz
end
zoom = 4.0
CImGui.Text(string("Min: (%.2f, %.2f)", region_x, region_y))
CImGui.Text(string("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz))
uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h)
uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h)
CImGui.Image(my_tex_id, ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, (255,255,255,255), (255,255,255,128))
CImGui.EndTooltip()
end
end

function rect_to_uv(x, y, w, h, W, H)
# Start UV coordinates (top-left)
u1 = x / W
v1 = y / H

# End UV coordinates (bottom-right)
u2 = (x + w) / W
v2 = (y + h) / H

return u1, v1, u2, v2
end


21 changes: 2 additions & 19 deletions src/editor/JulGameEditor/Editor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ module Editor
# Static variables
points = Ref(Vector{ImVec2}())
scrolling = Ref(ImVec2(0.0, 0.0))
opt_enable_grid = Ref(true)
opt_enable_context_menu = Ref(true)
adding_line = Ref(false)
show_app_custom_rendering = Ref(true)
zoom_level = Ref(1.0)
grid_step = Ref(Int32(64))

Expand Down Expand Up @@ -119,22 +116,8 @@ module Editor
end

# if unsafe_load(io.KeyShift) # && unsafe_load(io.MouseDown)[1] && mouseUVCoord.x >= 0.0 && mouseUVCoord.y >= 0.0
try
ShowExampleAppCustomRendering(show_app_custom_rendering, points, scrolling, opt_enable_grid, opt_enable_context_menu, adding_line, my_texture[], my_image_width[], my_image_height[], zoom_level, grid_step)
CImGui.Begin("SDL2/SDL_Renderer Texture Test")
CImGui.Text(string("pointer = ", my_texture))
CImGui.Text(string("size = $(my_image_width[]) x $(my_image_height[])"))
CImGui.Image(my_texture[], ImVec2(my_image_width[], my_image_height[]))

# CImGui.ImageButton(pickerImage.textureID, ImVec2(pickerImage.mWidth, pickerImage.mHeight))
rc = CImGui.ImRect(CImGui.GetItemRectMin(), CImGui.GetItemRectMax())
mouseUVCoord = ImVec2(unsafe_load(io.MousePos).x - rc.Min.x / get_size(rc).x, unsafe_load(io.MousePos).y - rc.Min.y / get_size(rc).y)
mouseUVCoord = ImVec2(mouseUVCoord.x, 1.0 - mouseUVCoord.y)
CImGui.Text("Mouse Position:")
CImGui.SameLine()
CImGui.Text(string("x = $(unsafe_load(io.MousePos).x), y = $(unsafe_load(io.MousePos).y)"))
displayedTextureSize = ImVec2(unsafe_load(io.DisplaySize).x, unsafe_load(io.DisplaySize).y)
CImGui.End()
try
# show_animation_window(points, scrolling, adding_line, my_texture[], my_image_width[], my_image_height[], zoom_level, grid_step)
catch e
@error "Error" exception=e
Base.show_backtrace(stderr, catch_backtrace())
Expand Down

0 comments on commit 0e2a417

Please sign in to comment.