Skip to content

Commit 9db86dc

Browse files
committed
Sprite rotation and center, get by id and copy id to clipboard editor
1 parent 7d90c08 commit 9db86dc

File tree

7 files changed

+47
-14
lines changed

7 files changed

+47
-14
lines changed

src/editor/JulGameEditor/Components/ComponentInputs.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ The function checks the type of the field value and displays the corresponding i
8686
"""
8787
function show_component_field_input(component, componentField, newScriptText)
8888
fieldValue = getfield(component, componentField)
89-
if isa(fieldValue, Math._Vector2{Float64}) || isa(fieldValue, Math._Vector2{Int32})
89+
if isa(fieldValue, String) && String(componentField) == "id"
90+
#display id as text and add a button to copy it to clipboard
91+
CImGui.Text("$(componentField): $(fieldValue)")
92+
CImGui.SameLine()
93+
CImGui.Button("Copy") && SDL2.SDL_SetClipboardText(fieldValue)
94+
elseif isa(fieldValue, Math._Vector2{Float64}) || isa(fieldValue, Math._Vector2{Int32})
9095
isFloat::Bool = isa(fieldValue, Math._Vector2{Float64}) ? true : false
9196

9297
x = isFloat ? Cfloat(fieldValue.x) : Cint(fieldValue.x)
@@ -348,13 +353,29 @@ function show_sprite_fields(sprite, animation_window_dict)
348353
end
349354

350355
CImGui.PushID(sprite.parent.id)
351-
crop_x, crop_y, crop_w, crop_h = show_animation_window("crop", window_info, sprite.texture, sprite.size.x, sprite.size.y)
356+
crop_x, crop_y, crop_w, crop_h = show_animation_window("Sprite crop", window_info, sprite.texture, sprite.size.x, sprite.size.y)
352357
CImGui.PopID()
353358
vec4i = Cint[crop_x, crop_y, crop_w, crop_h]
354359
@c CImGui.InputInt4("crop", vec4i)
355360
window_info[]["points"][][1] = ImVec2(vec4i[1], vec4i[2])
356361
window_info[]["points"][][2] = ImVec2(round(vec4i[1] + vec4i[3]), round(vec4i[2] + vec4i[4]))
357362
sprite.crop = JulGame.Math.Vector4(Int32(vec4i[1]), Int32(vec4i[2]), Int32(vec4i[3]), Int32(vec4i[4]))
363+
elseif fieldString == "rotation"
364+
x = Cfloat(sprite.rotation)
365+
@c CImGui.InputFloat("rotation", &x, 1)
366+
x = Float64(x)
367+
sprite.rotation = x
368+
elseif fieldString == "center"
369+
#float that is min 0 and max 1
370+
x = Cfloat(sprite.center.x)
371+
y = Cfloat(sprite.center.y)
372+
@c CImGui.InputFloat("center x", &x, 0.01)
373+
@c CImGui.InputFloat("center y", &y, 0.01)
374+
x = Float64(x)
375+
y = Float64(y)
376+
c = clamp(x, 0, 1)
377+
y = clamp(y, 0, 1)
378+
sprite.center = Vector2f(x, y)
358379
else
359380
show_component_field_input(sprite, field, "")
360381
end

src/editor/JulGameEditor/Components/SpriteViewer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ using CImGui.CSyntax.CStatic
99
Demonstrate using the low-level ImDrawList to draw custom shapes.
1010
"""
1111
function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_tex_h)
12-
CImGui.SetNextWindowSize((350, 560), CImGui.ImGuiCond_FirstUseEver)
13-
CImGui.Begin("Animation - $(frame_name)") || (CImGui.End(); return)
12+
#CImGui.SetNextWindowSize((350, 560), CImGui.ImGuiCond_FirstUseEver)
13+
CImGui.Begin("$(frame_name)") || (CImGui.End(); return)
1414

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

src/engine/Component/Sprite.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ module SpriteModule
1414
position::Math.Vector2f
1515
rotation::Float64
1616
pixelsPerUnit::Int32
17-
center::Math.Vector2
17+
center::Math.Vector2f
1818
end
1919

2020
export InternalSprite
2121
mutable struct InternalSprite
22-
center::Math.Vector2
22+
center::Math.Vector2f
2323
color::Math.Vector3
2424
crop::Union{Ptr{Nothing}, Math.Vector4}
2525
isFlipped::Bool
@@ -36,7 +36,7 @@ module SpriteModule
3636
size::Math.Vector2
3737
texture::Union{Ptr{Nothing}, Ptr{SDL2.LibSDL2.SDL_Texture}}
3838

39-
function InternalSprite(parent::Any, imagePath::String, crop::Union{Ptr{Nothing}, Math.Vector4}=C_NULL, isFlipped::Bool=false, color::Math.Vector3 = Math.Vector3(255,255,255), isCreatedInEditor::Bool=false; pixelsPerUnit::Int32=Int32(-1), isWorldEntity::Bool=true, position::Math.Vector2f = Math.Vector2f(0,0), rotation::Float64 = 0.0, layer::Int32 = Int32(0), center::Math.Vector2 = Math.Vector2(0,0))
39+
function InternalSprite(parent::Any, imagePath::String, crop::Union{Ptr{Nothing}, Math.Vector4}=C_NULL, isFlipped::Bool=false, color::Math.Vector3 = Math.Vector3(255,255,255), isCreatedInEditor::Bool=false; pixelsPerUnit::Int32=Int32(-1), isWorldEntity::Bool=true, position::Math.Vector2f = Math.Vector2f(0,0), rotation::Float64 = 0.0, layer::Int32 = Int32(0), center::Math.Vector2f = Math.Vector2f(0.5,0.5))
4040
this = new()
4141

4242
this.offset = Math.Vector2f()
@@ -139,14 +139,15 @@ module SpriteModule
139139
end
140140
end
141141

142-
# Calculate center position on sprite using the center property.
142+
# Calculate center position on sprite using the center property
143+
# center should be a point from 0 to 1, where 0.5 is the center of the sprite
143144
# The value is a pointer to a point indicating the point around which dstrect will be rotated
144145
# (if C_NULL, rotation will be done around dstrect.w / 2, dstrect.h / 2)
145146
# Todo: don't allocate this every frame
146-
calculatedCenter = Math.Vector2f(dstRect[].w/2 + this.center.x, dstRect[].h/2 + this.center.y)
147+
calculatedCenter = Math.Vector2(dstRect[].w * (this.center.x%1), dstRect[].h * (this.center.y%1))
147148
calculatedCenter = !this.isFloatPrecision ? Ref(SDL2.SDL_Point(round(calculatedCenter.x), round(calculatedCenter.y))) : Ref(SDL2.SDL_FPoint(calculatedCenter.x, calculatedCenter.y))
148149

149-
if this.isFloatPrecision && SDL2.SDL_RenderCopyExF(
150+
if this.isFloatPrecision && SDL2.SDL_RenderCopyExF(
150151
JulGame.Renderer::Ptr{SDL2.SDL_Renderer},
151152
this.texture,
152153
srcRect,

src/engine/Entity.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ module EntityModule
144144
return newSoundSource
145145
end
146146

147-
function JulGame.add_sprite(this::Entity, isCreatedInEditor::Bool = false, sprite::Sprite = Sprite(Math.Vector3(255, 255, 255), C_NULL, false, "", true, 0, Math.Vector2f(0,0), Math.Vector2f(0,0), 0, -1, Math.Vector2(0,0)))
147+
function JulGame.add_sprite(this::Entity, isCreatedInEditor::Bool = false, sprite::Sprite = Sprite(Math.Vector3(255, 255, 255), C_NULL, false, "", true, 0, Math.Vector2f(0,0), Math.Vector2f(0,0), 0, -1, Math.Vector2f(0.5,0.5)))
148148
if this.sprite != C_NULL
149149
println("Sprite already exists on entity named ", this.name)
150150
return

src/engine/Scene.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,16 @@
4848
end
4949
return entities
5050
end
51+
52+
function get_entity_by_id(this::Scene, id)
53+
for entity in this.entities
54+
if entity.id == id
55+
return entity
56+
end
57+
end
58+
59+
@warn "No entity with id $id found"
60+
return nothing
61+
end
5162
end
5263

src/engine/SceneManagement/SceneBuilder.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ const DEFAULT_CONFIG = Dict(
326326

327327
# Function to read and parse the config file
328328
function parse_config()
329-
filename = joinpath(pwd(), "..", "config.julgame")
329+
filename = joinpath(JulGame.BasePath, "..", "config.julgame")
330330
config = copy(DEFAULT_CONFIG)
331331

332332
if isfile(filename)

src/engine/SceneManagement/SceneReader.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ module SceneReaderModule
167167
position = !haskey(component, "position") ? Vector2f() : Vector2f(component.position.x, component.position.y)
168168
rotation = !haskey(component, "rotation") ? 0.0 : convert(Float64, component.rotation)
169169
pixelsPerUnit = !haskey(component, "pixelsPerUnit") ? -1 : component.pixelsPerUnit
170-
center = !haskey(component, "center") ? Vector2(0,0) : Vector2(component.center.x, component.center.y)
171-
newComponent = Sprite(color::Vector3, crop::Union{Ptr{Nothing}, Math.Vector4}, component.isFlipped::Bool, component.imagePath::String, isWorldEntity::Bool, Int32(layer), offset::Vector2f, position::Vector2f, rotation::Float64, Int32(pixelsPerUnit), center::Vector2)
170+
center = !haskey(component, "center") ? Vector2f(0.5,0.5) : Vector2f(component.center.x, component.center.y)
171+
newComponent = Sprite(color::Vector3, crop::Union{Ptr{Nothing}, Math.Vector4}, component.isFlipped::Bool, component.imagePath::String, isWorldEntity::Bool, Int32(layer), offset::Vector2f, position::Vector2f, rotation::Float64, Int32(pixelsPerUnit), center::Vector2f)
172172
end
173173

174174
return newComponent

0 commit comments

Comments
 (0)