diff --git a/src/Main.jl b/src/Main.jl index 1d53319a..4753967b 100644 --- a/src/Main.jl +++ b/src/Main.jl @@ -201,24 +201,9 @@ module MainLoop function prepare_window(isUsingEditor::Bool = false, size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true) this::Main = MAIN - if size == Math.Vector2() - displayMode = SDL2.SDL_DisplayMode[SDL2.SDL_DisplayMode(0x12345678, 800, 600, 60, C_NULL)] - SDL2.SDL_GetCurrentDisplayMode(0, pointer(displayMode)) - size = Math.Vector2(displayMode[1].w, displayMode[1].h) - end this.autoScaleZoom = autoScaleZoom scale_zoom(this, size.x, size.y) - this.screenSize = size != C_NULL ? size : this.scene.camera.size - - flags = SDL2.SDL_RENDERER_ACCELERATED | - (isUsingEditor ? (SDL2.SDL_WINDOW_POPUP_MENU | SDL2.SDL_WINDOW_ALWAYS_ON_TOP | SDL2.SDL_WINDOW_BORDERLESS) : 0) | - (isResizable || isUsingEditor ? SDL2.SDL_WINDOW_RESIZABLE : 0) | - (size == Math.Vector2() ? SDL2.SDL_WINDOW_FULLSCREEN_DESKTOP : 0) - - this.window = SDL2.SDL_CreateWindow(this.windowName, SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, this.screenSize.x, this.screenSize.y, flags) - - JulGame.Renderer::Ptr{SDL2.SDL_Renderer} = SDL2.SDL_CreateRenderer(this.window, -1, SDL2.SDL_RENDERER_ACCELERATED) this.scene.camera.startingCoordinates = Math.Vector2f(round(size.x/2) - round(this.scene.camera.size.x/2*this.zoom), round(size.y/2) - round(this.scene.camera.size.y/2*this.zoom)) @info string("Set viewport to: ", this.scene.camera.startingCoordinates) SDL2.SDL_RenderSetViewport(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, Ref(SDL2.SDL_Rect(this.scene.camera.startingCoordinates.x, this.scene.camera.startingCoordinates.y, round(this.scene.camera.size.x*this.zoom), round(this.scene.camera.size.y*this.zoom)))) diff --git a/src/SceneManagement/SceneBuilder.jl b/src/SceneManagement/SceneBuilder.jl index e2fd7370..3c0be1b8 100644 --- a/src/SceneManagement/SceneBuilder.jl +++ b/src/SceneManagement/SceneBuilder.jl @@ -65,9 +65,13 @@ module SceneBuilderModule MAIN.globals = globals MAIN.level = this MAIN.targetFrameRate = targetFrameRate - scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene), isUsingEditor) - MAIN.scene.entities = scene[1] - MAIN.scene.uiElements = scene[2] + + if size == Math.Vector2() + displayMode = SDL2.SDL_DisplayMode[SDL2.SDL_DisplayMode(0x12345678, 800, 600, 60, C_NULL)] + SDL2.SDL_GetCurrentDisplayMode(0, pointer(displayMode)) + size = Math.Vector2(displayMode[1].w, displayMode[1].h) + end + if size.x < camSize.x && size.x > 0 camSize = Vector2(size.x, camSize.y) end @@ -75,9 +79,24 @@ module SceneBuilderModule camSize = Vector2(camSize.x, size.y) end MAIN.scene.camera = CameraModule.Camera(camSize, Vector2f(),Vector2f(), C_NULL) + + flags = SDL2.SDL_RENDERER_ACCELERATED | + (isUsingEditor ? (SDL2.SDL_WINDOW_POPUP_MENU | SDL2.SDL_WINDOW_ALWAYS_ON_TOP | SDL2.SDL_WINDOW_BORDERLESS) : 0) | + (isResizable || isUsingEditor ? SDL2.SDL_WINDOW_RESIZABLE : 0) | + (size == Math.Vector2() ? SDL2.SDL_WINDOW_FULLSCREEN_DESKTOP : 0) + + MAIN.screenSize = size != C_NULL ? size : MAIN.scene.camera.size + if !isUsingEditor + MAIN.window = SDL2.SDL_CreateWindow(MAIN.windowName, SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, MAIN.screenSize.x, MAIN.screenSize.y, flags) + JulGame.Renderer::Ptr{SDL2.SDL_Renderer} = SDL2.SDL_CreateRenderer(MAIN.window, -1, SDL2.SDL_RENDERER_ACCELERATED) + end + + scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene), isUsingEditor) + MAIN.scene.entities = scene[1] + MAIN.scene.uiElements = scene[2] for uiElement in MAIN.scene.uiElements - if "$(typeof(uiElement))" == "JulGame.UI.TextBoxModule.Textbox" && uiElement.isWorldEntity + if "$(typeof(uiElement))" == "JulGame.UI.TextBoxModule.Textbox" && !uiElement.isWorldEntity UI.center_text(uiElement) end end diff --git a/src/SceneManagement/SceneReader.jl b/src/SceneManagement/SceneReader.jl index c7bdafee..1c314121 100644 --- a/src/SceneManagement/SceneReader.jl +++ b/src/SceneManagement/SceneReader.jl @@ -112,7 +112,8 @@ module SceneReaderModule try newUIElement = nothing if uiElement.type == "TextBox" - newUIElement = TextBox(uiElement.name, uiElement.fontPath, uiElement.fontSize, Vector2(uiElement.position.x, uiElement.position.y), uiElement.text, uiElement.isCenteredX, uiElement.isCenteredY) + newUIElement = TextBox(uiElement.name, uiElement.fontPath, uiElement.fontSize, Vector2(uiElement.position.x, uiElement.position.y), uiElement.text, uiElement.isCenteredX, uiElement.isCenteredY) + newUIElement.isWorldEntity = uiElement.isWorldEntity else newUIElement = ScreenButton(uiElement.name, uiElement.buttonUpSpritePath, uiElement.buttonDownSpritePath, Vector2(uiElement.size.x, uiElement.size.y), Vector2(uiElement.position.x, uiElement.position.y), uiElement.fontPath, uiElement.text, Vector2(uiElement.textOffset.x, uiElement.textOffset.y)) end diff --git a/src/UI/TextBox.jl b/src/UI/TextBox.jl index ad0ce3c8..d23f2cdb 100644 --- a/src/UI/TextBox.jl +++ b/src/UI/TextBox.jl @@ -69,8 +69,7 @@ module TextBoxModule end function UI.load_font(this::TextBox, basePath::String, fontPath::String) - println("loading font from base: $(basePath)") - println("loading font from $(fontPath)") + @info string("loading font from $(basePath)\\$(fontPath)") this.font = CallSDLFunction(SDL2.TTF_OpenFont, joinpath(basePath, fontPath), this.fontSize) if this.font == C_NULL return diff --git a/src/Utils.jl b/src/Utils.jl index afe6f59d..07fa36e2 100644 --- a/src/Utils.jl +++ b/src/Utils.jl @@ -5,7 +5,8 @@ function CallSDLFunction(func::Function, args...) # Call SDL function and check for errors ret = func(args...) if (isa(ret, Number) && ret < 0) || ret == C_NULL - println(unsafe_string(SDL2.SDL_GetError())) + @error "$(unsafe_string(SDL2.SDL_GetError())) $(@__FILE__):$(@__LINE__)" + @info "Check Utils.jl" Base.show_backtrace(stdout, catch_backtrace()) end diff --git a/src/editor/JulGameEditor/Components/ComponentInputs.jl b/src/editor/JulGameEditor/Components/ComponentInputs.jl index b9b8219f..a3b8488b 100644 --- a/src/editor/JulGameEditor/Components/ComponentInputs.jl +++ b/src/editor/JulGameEditor/Components/ComponentInputs.jl @@ -32,7 +32,7 @@ function show_field_editor(entity, fieldName, animation_window_dict) end if isa(field, SpriteModule.InternalSprite) - show_sprite_fields(entity.sprite) + show_sprite_fields(entity.sprite, animation_window_dict) elseif isa(field, SoundSourceModule.InternalSoundSource) show_sound_source_fields(entity.soundSource) elseif isa(field, AnimatorModule.InternalAnimator) @@ -279,7 +279,7 @@ and updates the `sprite.imagePath` field with the current text in the text box. - `sprite`: The sprite object to display the fields for. """ -function show_sprite_fields(sprite) +function show_sprite_fields(sprite, animation_window_dict) for field in fieldnames(typeof(sprite)) fieldString = "$(field)" @@ -309,7 +309,30 @@ function show_sprite_fields(sprite) sprite.imagePath = imagePath end CImGui.Button("Load Image") && (Component.load_image(sprite, currentTextInTextBox)) - else + elseif fieldString == "crop" + crop_x, crop_y, crop_w, crop_h = sprite.crop.x, sprite.crop.y, sprite.crop.z, sprite.crop.t + + points = Ref(Vector{ImVec2}([ImVec2(crop_x, crop_y), ImVec2(crop_x + crop_w, crop_y + crop_h)])) + scrolling = Ref(ImVec2(0.0, 0.0)) + adding_line = Ref(false) + zoom_level = Ref(1.0) + grid_step = Ref(Int32(64)) + # 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 if animation_window_dict has the key "frame $(k)" + key = "crop-$(sprite.parent.id)" + if haskey(animation_window_dict[], key) + # animation_window_dict[]["frame $(k)"][]["points"] = points + window_info[] = animation_window_dict[][key][] + else + animation_window_dict[][key] = window_info + end + + crop_x, crop_y, crop_w, crop_h = show_animation_window("crop", window_info, sprite.texture, sprite.size.x, sprite.size.y) + vec4i = Cint[crop_x, crop_y, crop_w, crop_h] + @c CImGui.InputInt4("crop", vec4i) + sprite.crop = JulGame.Math.Vector4(Int32(vec4i[1]), Int32(vec4i[2]), Int32(vec4i[3]), Int32(vec4i[4])) + else show_component_field_input(sprite, field) end end diff --git a/src/editor/JulGameEditor/Components/MainMenuBar.jl b/src/editor/JulGameEditor/Components/MainMenuBar.jl index 5121095f..4a7c9434 100644 --- a/src/editor/JulGameEditor/Components/MainMenuBar.jl +++ b/src/editor/JulGameEditor/Components/MainMenuBar.jl @@ -35,7 +35,6 @@ function ShowMenuFile(events) events[end]() end if length(events) > 1 && CImGui.MenuItem("Save", "Ctrl+S") - @info "Trigger Save | find me here: $(@__FILE__):$(@__LINE__)" events[1]() end end \ No newline at end of file diff --git a/src/editor/JulGameEditor/Components/SpriteViewer.jl b/src/editor/JulGameEditor/Components/SpriteViewer.jl index 27bf148d..a80ec2d0 100644 --- a/src/editor/JulGameEditor/Components/SpriteViewer.jl +++ b/src/editor/JulGameEditor/Components/SpriteViewer.jl @@ -4,40 +4,8 @@ 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}() - - 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 - end - - surfaceInfo = unsafe_wrap(Array, surface, 10; own = false) - width[] = surfaceInfo[1].w - height[] = surfaceInfo[1].h - - if surface == C_NULL - @error "Failed to create SDL surface: $(unsafe_string(SDL2.SDL_GetError()))" - return false, C_NULL, 0, 0 - end - - texture_ptr = SDL2.SDL_CreateTextureFromSurface(renderer, surface) - - if texture_ptr == C_NULL - @error "Failed to create SDL texture: $(unsafe_string(SDL2.SDL_GetError()))" - end - - SDL2.SDL_FreeSurface(surface) - - return true, texture_ptr, width[], height[] #, data -end - - """ - ShowExampleAppCustomRendering(p_open::Ref{Bool}) + show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_tex_h) 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) @@ -92,7 +60,7 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_ end if window_info[]["adding_line"][] window_info[]["points"][][end] = mouse_pos_in_canvas_zoom_adjusted - if !CImGui.IsMouseDown(CImGui.ImGuiMouseButton_Left) + if is_active && !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 end @@ -105,14 +73,14 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_ end # Zoom - if unsafe_load(io.KeyCtrl) + if is_active && 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) 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 + if is_active && CImGui.IsMouseReleased(CImGui.ImGuiMouseButton_Right) && drag_delta.x == 0.0 && drag_delta.y == 0.0 CImGui.OpenPopupOnItemClick("context") end if CImGui.BeginPopup("context") @@ -139,7 +107,10 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_ 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 + CImGui.PopClipRect(draw_list) + 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)) + # 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"][])) @@ -148,8 +119,6 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_ CImGui.AddRect(draw_list, p1, p2, IM_COL32(255, 255, 0, 255)) end - 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) CImGui.End()