Skip to content

Feature/sprite crop #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ test/projects/ProfilingTest/Platformer/.vscode/settings.json
.vscode/settings.json

playground.jl

*.ini
2 changes: 1 addition & 1 deletion src/Component/Animation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

function Component.update_array_value(this::Animation, value, field, index::Int32)
fieldToUpdate = getfield(this, field)
if get_type(this, value) == "_Vector4"
if Component.get_type(this, value) == "_Vector4"

Check warning on line 21 in src/Component/Animation.jl

View check run for this annotation

Codecov / codecov/patch

src/Component/Animation.jl#L21

Added line #L21 was not covered by tests
fieldToUpdate[index] = Math.Vector4(value.x, value.y, value.z, value.t)
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/Main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -757,13 +757,13 @@
end
colliderRenderCount += 1
collider = entity.collider
if JulGame.get_type(collider) == "CircleCollider"
if Component.get_type(collider) == "CircleCollider"

Check warning on line 760 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L760

Added line #L760 was not covered by tests
SDL2E.SDL_RenderDrawCircle(
round(Int32, (pos.x - this.scene.camera.position.x) * SCALE_UNITS - ((entity.transform.scale.x * SCALE_UNITS - SCALE_UNITS) / 2)),
round(Int32, (pos.y - this.scene.camera.position.y) * SCALE_UNITS - ((entity.transform.scale.y * SCALE_UNITS - SCALE_UNITS) / 2)),
round(Int32, collider.diameter/2 * SCALE_UNITS))
else
colSize = JulGame.get_size(collider)
colSize = Component.get_size(collider)

Check warning on line 766 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L766

Added line #L766 was not covered by tests
colSize = Math.Vector2f(colSize.x, colSize.y)
colOffset = collider.offset
colOffset = Math.Vector2f(colOffset.x, colOffset.y)
Expand Down
41 changes: 35 additions & 6 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
show_field_editor(entity, field)
Creates inputs based on the component type and populates them.
"""
function show_field_editor(entity, fieldName)
function show_field_editor(entity, fieldName, animation_window_dict)

Check warning on line 16 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L16

Added line #L16 was not covered by tests
field = getfield(entity, fieldName)
if field == C_NULL || field === nothing
return
Expand All @@ -36,7 +36,7 @@
elseif isa(field, SoundSourceModule.InternalSoundSource)
show_sound_source_fields(entity.soundSource)
elseif isa(field, AnimatorModule.InternalAnimator)
show_animator_properties(entity.animator)
show_animator_properties(entity.animator, animation_window_dict)

Check warning on line 39 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L39

Added line #L39 was not covered by tests
else
for field in fieldnames(typeof(field))
show_component_field_input(getfield(entity, Symbol(lowercase(fieldName))), field)
Expand Down Expand Up @@ -183,15 +183,15 @@
end

"""
show_animator_properties(animator)
show_animator_properties(animator, animation_window_dict)

Display the properties of an animator object in the user interface.

# Arguments
- `animator`: The animator object to display properties for.

"""
function show_animator_properties(animator)
function show_animator_properties(animator, animation_window_dict)

Check warning on line 194 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L194

Added line #L194 was not covered by tests
try
for field in fieldnames(typeof(animator))
fieldString = "$(field)"
Expand All @@ -214,9 +214,38 @@
CImGui.Button("Add Frame") && Component.append_array(animations[i])
CImGui.Button("Delete") && (deleteat!(animations, i); break;)
for k = eachindex(animations[i].frames)
vec = animations[i].frames[k]
anim_x, anim_y, anim_w, anim_h = vec.x, vec.y, vec.z, vec.t

Check warning on line 218 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L217-L218

Added lines #L217 - L218 were not covered by tests

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])

Check warning on line 222 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L220-L222

Added lines #L220 - L222 were not covered by tests
end
if CImGui.TreeNode("frame $(k)")
vec = animations[i].frames[k]
vec4i = Cint[vec.x, vec.y, vec.z, vec.t]
if animator.parent.sprite != C_NULL && animator.parent.sprite !== nothing

Check warning on line 225 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L225

Added line #L225 was not covered by tests

points = Ref(Vector{ImVec2}([ImVec2(anim_x, anim_y), ImVec2(anim_x + anim_w, anim_y + anim_h)]))
scrolling = Ref(ImVec2(0.0, 0.0))
adding_line = Ref(false)
zoom_level = Ref(1.0)
grid_step = Ref(Int32(64))

Check warning on line 231 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L227-L231

Added lines #L227 - L231 were not covered by tests

# put these in a ref dictionary
window_info = Ref(Dict("points" => points, "scrolling" => scrolling, "adding_line" => adding_line, "zoom_level" => zoom_level, "grid_step" => grid_step))

Check warning on line 234 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L234

Added line #L234 was not covered by tests
# check if animation_window_dict has the key "frame $(k)"
if haskey(animation_window_dict[], "frame $(k)")

Check warning on line 236 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L236

Added line #L236 was not covered by tests
# animation_window_dict[]["frame $(k)"][]["points"] = points
window_info[] = animation_window_dict[]["frame $(k)"][]

Check warning on line 238 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L238

Added line #L238 was not covered by tests
else
animation_window_dict[]["frame $(k)"] = window_info

Check warning on line 240 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L240

Added line #L240 was not covered by tests
end


sprite = animator.parent.sprite
anim_x, anim_y, anim_w, anim_h = show_animation_window("frame $(k)", window_info, sprite.texture, sprite.size.x, sprite.size.y)

Check warning on line 245 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L244-L245

Added lines #L244 - L245 were not covered by tests
end

vec4i = Cint[anim_x, anim_y, anim_w, anim_h]

Check warning on line 248 in src/editor/JulGameEditor/Components/ComponentInputs.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/ComponentInputs.jl#L248

Added line #L248 was not covered by tests
@c CImGui.InputInt4("frame input $(k)", vec4i)
Component.update_array_value(animations[i], JulGame.Math.Vector4(Int32(vec4i[1]), Int32(vec4i[2]), Int32(vec4i[3]), Int32(vec4i[4])), animationFields[j], Int32(k))
CImGui.TreePop()
Expand Down
2 changes: 1 addition & 1 deletion src/editor/JulGameEditor/Components/MainMenuBar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
- `events`: An array of event functions. These are callbacks that are triggered when the user selects a menu item.
"""
function ShowMenuFile(events)
if CImGui.MenuItem("Open", "Ctrl+O.")
if CImGui.MenuItem("Open", "Ctrl+O")

Check warning on line 34 in src/editor/JulGameEditor/Components/MainMenuBar.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/MainMenuBar.jl#L34

Added line #L34 was not covered by tests
events[end]()
end
if length(events) > 1 && CImGui.MenuItem("Save", "Ctrl+S")
Expand Down
223 changes: 223 additions & 0 deletions src/editor/JulGameEditor/Components/SpriteViewer.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using CImGui
using CImGui: ImVec2, ImVec4, IM_COL32, ImU32
using CImGui.CSyntax
using CImGui.CSyntax.CFor
using CImGui.CSyntax.CStatic

function load_texture_from_file(filename::String, renderer::Ptr{SDL2.SDL_Renderer})
width = Ref{Cint}()
height = Ref{Cint}()
channels = Ref{Cint}()

Check warning on line 10 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L7-L10

Added lines #L7 - L10 were not covered by tests

surface = SDL2.IMG_Load(filename)
if surface == C_NULL
@error "Failed to load image: $(unsafe_string(SDL2.SDL_GetError()))"
return false, C_NULL, 0, 0

Check warning on line 15 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L12-L15

Added lines #L12 - L15 were not covered by tests
end

surfaceInfo = unsafe_wrap(Array, surface, 10; own = false)
width[] = surfaceInfo[1].w
height[] = surfaceInfo[1].h

Check warning on line 20 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L18-L20

Added lines #L18 - L20 were not covered by tests

if surface == C_NULL
@error "Failed to create SDL surface: $(unsafe_string(SDL2.SDL_GetError()))"
return false, C_NULL, 0, 0

Check warning on line 24 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L22-L24

Added lines #L22 - L24 were not covered by tests
end

texture_ptr = SDL2.SDL_CreateTextureFromSurface(renderer, surface)

Check warning on line 27 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L27

Added line #L27 was not covered by tests

if texture_ptr == C_NULL
@error "Failed to create SDL texture: $(unsafe_string(SDL2.SDL_GetError()))"

Check warning on line 30 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L29-L30

Added lines #L29 - L30 were not covered by tests
end

SDL2.SDL_FreeSurface(surface)

Check warning on line 33 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L33

Added line #L33 was not covered by tests

return true, texture_ptr, width[], height[] #, data

Check warning on line 35 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L35

Added line #L35 was not covered by tests
end


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

Check warning on line 45 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L43-L45

Added lines #L43 - L45 were not covered by tests

draw_list = CImGui.GetWindowDrawList()
io = CImGui.GetIO()

Check warning on line 48 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L47-L48

Added lines #L47 - L48 were not covered by tests

# UI elements
# grid step int input as slider with range. Min = 1, Max = 64
CImGui.SliderInt("Grid step", window_info[]["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")
selectedPoint1 = length(window_info[]["points"][]) > 0 ? window_info[]["points"][][end-1] : ImVec2(0,0)
selectedPoint2 = length(window_info[]["points"][]) > 0 ? window_info[]["points"][][end] : ImVec2(0,0)
CImGui.Text("Current selection: x:$(selectedPoint1.x),y:$(selectedPoint1.y) w:$(selectedPoint2.x - selectedPoint1.x),h:$(selectedPoint2.y - selectedPoint1.y)")

Check warning on line 56 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L52-L56

Added lines #L52 - L56 were not covered by tests
# Canvas setup
canvas_p0 = CImGui.GetCursorScreenPos() # ImDrawList API uses screen coordinates!
canvas_sz = CImGui.GetContentRegionAvail() # Resize canvas to what's available
canvas_sz = ImVec2(max(canvas_sz.x, 50.0), max(canvas_sz.y, 50.0))
canvas_p1 = ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y)

Check warning on line 61 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L58-L61

Added lines #L58 - L61 were not covered by tests

canvas_max = ImVec2(my_tex_w * 10, my_tex_h * 10)

Check warning on line 63 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L63

Added line #L63 was not covered by tests

# Draw border and background color
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))

Check warning on line 68 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L66-L68

Added lines #L66 - L68 were not covered by tests

# Draw border around actual image that is being edited TODO: Fix this
# CImGui.AddRect(draw_list, ImVec2(canvas_p0.x + (my_tex_w * window_info[]["zoom_level"][]), canvas_p0.y + (my_tex_h * window_info[]["zoom_level"][])), ImVec2(canvas_p0.x, canvas_p0.y), IM_COL32(255, 255, 255, 255))

# Invisible button for interactions
CImGui.InvisibleButton("canvas", canvas_sz, CImGui.ImGuiButtonFlags_MouseButtonLeft | CImGui.ImGuiButtonFlags_MouseButtonRight)
is_hovered = CImGui.IsItemHovered() # Hovered
is_active = CImGui.IsItemActive() # Held

Check warning on line 76 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L74-L76

Added lines #L74 - L76 were not covered by tests
# origin = ImVec2(canvas_p0.x + window_info[]["scrolling"][].x, canvas_p0.y + window_info[]["scrolling"][].y) # Lock scrolled origin
window_info[]["scrolling"][] = ImVec2(min(window_info[]["scrolling"][].x, 0.0), min(window_info[]["scrolling"][].y, 0.0))
window_info[]["scrolling"][] = ImVec2(max(window_info[]["scrolling"][].x, -canvas_max.x), max(window_info[]["scrolling"][].y, -canvas_max.y))
origin = ImVec2(min(0, 0 + window_info[]["scrolling"][].x), min(0, 0 + window_info[]["scrolling"][].y)) # Lock scrolled origin
mouse_pos_in_canvas = ImVec2(unsafe_load(io.MousePos).x - canvas_p0.x, unsafe_load(io.MousePos).y - canvas_p0.y)
CImGui.Text("Mouse Position: $(mouse_pos_in_canvas.x), $(mouse_pos_in_canvas.y)")
CImGui.Text("Mouse Pixel: $(floor(mouse_pos_in_canvas.x / window_info[]["zoom_level"][])), $(floor(mouse_pos_in_canvas.y / window_info[]["zoom_level"][]))")

Check warning on line 83 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L78-L83

Added lines #L78 - L83 were not covered by tests

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"][]))

Check warning on line 85 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L85

Added line #L85 was not covered by tests
#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"][])
# Add first and second point
if is_hovered && !window_info[]["adding_line"][] && CImGui.IsMouseClicked(CImGui.ImGuiMouseButton_Left)
push!(window_info[]["points"][], mouse_pos_in_canvas_zoom_adjusted)
push!(window_info[]["points"][], mouse_pos_in_canvas_zoom_adjusted)
window_info[]["adding_line"][] = true

Check warning on line 91 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L88-L91

Added lines #L88 - L91 were not covered by tests
end
if window_info[]["adding_line"][]
window_info[]["points"][][end] = mouse_pos_in_canvas_zoom_adjusted
if !CImGui.IsMouseDown(CImGui.ImGuiMouseButton_Left)
window_info[]["points"][] = [window_info[]["points"][][end-1], window_info[]["points"][][end]] # only keep last two points
window_info[]["adding_line"][] = false

Check warning on line 97 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L93-L97

Added lines #L93 - L97 were not covered by tests
end
end

# Pan
mouse_threshold_for_pan = -1.0
if is_active && CImGui.IsMouseDragging(CImGui.ImGuiMouseButton_Right, mouse_threshold_for_pan)
window_info[]["scrolling"][] = ImVec2(window_info[]["scrolling"][].x + unsafe_load(io.MouseDelta).x, window_info[]["scrolling"][].y + unsafe_load(io.MouseDelta).y)

Check warning on line 104 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L102-L104

Added lines #L102 - L104 were not covered by tests
end

# Zoom
if unsafe_load(io.KeyCtrl)
window_info[]["zoom_level"][] += unsafe_load(io.MouseWheel) * 4.0 # * 0.10
window_info[]["zoom_level"][] = clamp(window_info[]["zoom_level"][], 1.0, 50.0)

Check warning on line 110 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L108-L110

Added lines #L108 - L110 were not covered by tests
end

# Context menu
drag_delta = CImGui.GetMouseDragDelta(CImGui.ImGuiMouseButton_Right)
if CImGui.IsMouseReleased(CImGui.ImGuiMouseButton_Right) && drag_delta.x == 0.0 && drag_delta.y == 0.0
CImGui.OpenPopupOnItemClick("context")

Check warning on line 116 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L114-L116

Added lines #L114 - L116 were not covered by tests
end
if CImGui.BeginPopup("context")
if window_info[]["adding_line"][]
resize!(window_info[]["points"][], length(window_info[]["points"][]) - 2)

Check warning on line 120 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L118-L120

Added lines #L118 - L120 were not covered by tests
end
window_info[]["adding_line"][] = false
if CImGui.MenuItem("Remove one", "", false, length(window_info[]["points"][]) > 0)
resize!(window_info[]["points"][], length(window_info[]["points"][]) - 2)

Check warning on line 124 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L122-L124

Added lines #L122 - L124 were not covered by tests
end
if CImGui.MenuItem("Remove all", "", false, length(window_info[]["points"][]) > 0)
empty!(window_info[]["points"][])

Check warning on line 127 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L126-L127

Added lines #L126 - L127 were not covered by tests
end
CImGui.EndPopup()

Check warning on line 129 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L129

Added line #L129 was not covered by tests
end

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

Check warning on line 134 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L133-L134

Added lines #L133 - L134 were not covered by tests

for x in 0:GRID_STEP:canvas_sz.x*10 # TODO: 10 is arbitrary
CImGui.AddLine(draw_list, ImVec2(origin.x + canvas_p0.x + x, canvas_p0.y), ImVec2(origin.x + canvas_p0.x + x, canvas_p1.y), IM_COL32(200, 200, 200, 40))
end
for y in 0:GRID_STEP:canvas_sz.y*10 # TODO: 10 is arbitrary
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

Check warning on line 141 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L136-L141

Added lines #L136 - L141 were not covered by tests

# Draw squares with add rect
for n in 1:2:length(window_info[]["points"][])-1
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"][]))
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"][]))

Check warning on line 146 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L144-L146

Added lines #L144 - L146 were not covered by tests
# scale to zoom level
CImGui.AddRect(draw_list, p1, p2, IM_COL32(255, 255, 0, 255))
end

Check warning on line 149 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L148-L149

Added lines #L148 - L149 were not covered by tests

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))
CImGui.PopClipRect(draw_list)

Check warning on line 152 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L151-L152

Added lines #L151 - L152 were not covered by tests

CImGui.End()

Check warning on line 154 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L154

Added line #L154 was not covered by tests

# x, y, w, h = crop.x, crop.y, crop.z, crop.t
return selectedPoint1.x, selectedPoint1.y, selectedPoint2.x - selectedPoint1.x, selectedPoint2.y - selectedPoint1.y

Check warning on line 157 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L157

Added line #L157 was not covered by tests
end

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

Check warning on line 161 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L160-L161

Added lines #L160 - L161 were not covered by tests

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

Check warning on line 164 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L163-L164

Added lines #L163 - L164 were not covered by tests

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, 0
u2, v2 = 1, 1
crop = false
return

Check warning on line 172 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L166-L172

Added lines #L166 - L172 were not covered by tests
else
my_tex_w = w
my_tex_h = h

Check warning on line 175 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L174-L175

Added lines #L174 - L175 were not covered by tests
end

# 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

Check warning on line 188 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L179-L188

Added lines #L179 - L188 were not covered by tests
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

Check warning on line 194 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L190-L194

Added lines #L190 - L194 were not covered by tests
end
zoom = 4.0
CImGui.Text("Min: ($(region_x), $(region_y))")
CImGui.Text("Max: ($(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)
if crop
uv0 = (u1,v1)
uv1 = (u2,v2)

Check warning on line 203 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L196-L203

Added lines #L196 - L203 were not covered by tests
end
CImGui.Image(my_tex_id, ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, (255,255,255,255), (255,255,255,128))
CImGui.EndTooltip()

Check warning on line 206 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L205-L206

Added lines #L205 - L206 were not covered by tests
end
CImGui.SameLine()

Check warning on line 208 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L208

Added line #L208 was not covered by tests
end

function rect_to_uv(x, y, w, h, W, H)

Check warning on line 211 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L211

Added line #L211 was not covered by tests
# Start UV coordinates (top-left)
u1 = x / W
v1 = y / H

Check warning on line 214 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L213-L214

Added lines #L213 - L214 were not covered by tests

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

Check warning on line 218 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L217-L218

Added lines #L217 - L218 were not covered by tests

return u1, v1, u2, v2

Check warning on line 220 in src/editor/JulGameEditor/Components/SpriteViewer.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Components/SpriteViewer.jl#L220

Added line #L220 was not covered by tests
end


Loading
Loading