From 27f1dc71c0572da7655f9e453f5f9a4c513a3b56 Mon Sep 17 00:00:00 2001 From: binarycat Date: Sat, 12 Mar 2022 11:12:29 -0500 Subject: [PATCH 1/3] added Rect class --- code/collision.lua | 4 +++ code/rect.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++ code/require.lua | 1 + 3 files changed, 71 insertions(+) create mode 100644 code/rect.lua diff --git a/code/collision.lua b/code/collision.lua index 1acae56..43d68ef 100644 --- a/code/collision.lua +++ b/code/collision.lua @@ -88,3 +88,7 @@ function Collision:draw(color) love.graphics.setColor(0,1,90,0.5) love.graphics.rectangle("line",self.from.x-Camera.pos.x, self.from.y-Camera.pos.y, self.width, self.height) end + +function Collision:asRect() + return Rect:fromCoords(self.from.x, self.from.y, self.to.x, self.to.y) +end diff --git a/code/rect.lua b/code/rect.lua new file mode 100644 index 0000000..c67c9a6 --- /dev/null +++ b/code/rect.lua @@ -0,0 +1,66 @@ +-- based of of plan9's Rectangle struct +-- rect.max is not counted as "in" the rectangle +Rect = {} +Rect.__index = Rect + +function Rect:fromPoints(pt1, pt2) + local o = { min = pt1, max = pt2 } + setmetatable(o, self) +end + +function Rect:fromCoords(x1, y1, x2, y2) + return Rect:fromPoints(Point:new(x1, y1), Point:new(x2, y2)) +end + +-- clone refers to a deep copy +function Rect:clone() + return Rect:fromCoods(self.min.x, self.min.y, self.max.x, self.max.y) +end + +-- make sure min and max refer to the correct corners +-- acts in place, returns self +function Rect:fix() + if self.min.x > self.max.x then + self.min.x, self.max.x = self.max.x, self.min.x + end + if self.min.y > self.max.y then + self.min.y, self.max.y = self.max.y, self.min.y + end +end + +function Rect:width() + return self.max.x - self.min.x +end + +function Rect:height() + return self.max.y - self.min.y +end + +function Rect:size() + return Point:new(self:width(), self:height()) +end + +function Rect:__add(pt) + return Rect:fromPoints(self.min + pt, self.max + pt) +end + +function Rect:corners() + return { + self.min:copy(), -- top left + Point:new(self.max.x, self.min.y), -- top right + Point:new(self.min.x, self.max.y), -- bottom left + self.max:copy(), -- bottom right + } +end + +function Rect:containsPoint(pt) + return self.min.x < pt.x and + self.min.y < pt.y and + pt.x < self.max.x and + pt.y < self.max.y +end + +function Rect:overlapsRect(other) + return self.min.x Date: Sat, 12 Mar 2022 11:46:45 -0500 Subject: [PATCH 2/3] change editor_mode to editor.active, minor refactor, and start work on multiselect --- code/editor.lua | 13 +++++-------- code/game.lua | 4 ---- main.lua | 18 ++++++++++++++---- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/code/editor.lua b/code/editor.lua index a4b3135..8103be0 100644 --- a/code/editor.lua +++ b/code/editor.lua @@ -1,11 +1,15 @@ assert(editor == nil) editor = { + active = false, room_mode = false, - --palette_mode = false, palette = { active = false, scroll = Point:new(0, 0), }, + multiselect = { + active = false, + box = nil, + }, pan = { fixed = false, speed = 3 }, } @@ -89,13 +93,6 @@ function stepEditor() end, }):activate() end - - if Keybind:checkPressed(Keybind.debug.editor) then - editor_mode = not editor_mode - deselectSpawns() - createTileObjects() - restartGame() - end end function scrollEditor(y) diff --git a/code/game.lua b/code/game.lua index 044bf1f..13d4b74 100644 --- a/code/game.lua +++ b/code/game.lua @@ -53,10 +53,6 @@ function stepGame() initMenu("dialog",dialog_sequence.example) end - if Keybind:checkPressed(Keybind.debug.editor) then - editor_mode = true - end - if Keybind:checkPressed(Keybind.debug.recording) then if DemoRecording then Demo:endRecord() diff --git a/main.lua b/main.lua index 2437051..96f284e 100644 --- a/main.lua +++ b/main.lua @@ -6,9 +6,10 @@ function love.load() secs = 0 menu_type = "no" + -- FIXME: this overrides a standard library! debug = false debug_collision = false - editor_mode = false + --editor_mode = false text_size = 1 @@ -103,6 +104,15 @@ function love.update(dt) return end + if Keybind:checkPressed(Keybind.debug.editor) then + if editor.active then + deselectSpawns() + createTileObjects() + restartGame() + end + editor.active = not editor.active + end + if love.keyboard.isDown("f7") then local test_prompt = Prompt:new({ name = "test prompt", @@ -127,7 +137,7 @@ function love.update(dt) if menu_type ~= nil then stepMenu(menu_type) end --editor - if editor_mode then + if editor.active then stepEditor() else stepGame() @@ -136,7 +146,7 @@ end function love.wheelmoved(_, y) - if editor_mode then + if editor.active then scrollEditor(y) end end @@ -150,7 +160,7 @@ function love.draw() game_resize = false end - if editor_mode then + if editor.active then drawEditor() else drawGame() From 82246dc0c689d012fea49d1590e9fd4736500996 Mon Sep 17 00:00:00 2001 From: binarycat Date: Sat, 12 Mar 2022 13:16:39 -0500 Subject: [PATCH 3/3] multiselect region can be drawn with the right mouse button --- code/camera.lua | 13 +++++++++++++ code/editor.lua | 40 +++++++++++++++++++++++++++++++++++++++- code/rect.lua | 9 +++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/code/camera.lua b/code/camera.lua index 35f3a2d..b67e4dc 100644 --- a/code/camera.lua +++ b/code/camera.lua @@ -77,3 +77,16 @@ function Camera:positionAt(x,y) self.pos.y = math.floor((y/self.height)*self.height) end +-- translate screen coordinates to game coordinates +function Camera:ptScreenToGame(pt) + return self.pos + pt +end + +function Camera:mouseScreenPos() + return Point:new(love.mouse.getX(),love.mouse.getY()) / game.scale +end + +-- return the mouse position as game coordinates +function Camera:mouseGamePos() + return self:ptScreenToGame(self:mouseScreenPos()) +end diff --git a/code/editor.lua b/code/editor.lua index 8103be0..fc066de 100644 --- a/code/editor.lua +++ b/code/editor.lua @@ -8,6 +8,7 @@ editor = { }, multiselect = { active = false, + sweeping = false, box = nil, }, pan = { fixed = false, speed = 3 }, @@ -16,6 +17,20 @@ editor = { function stepEditor() animateTiles() + local osweep = editor.multiselect.sweeping + editor.multiselect.sweeping = love.mouse.isDown(2) + frameDebug("sweeping: "..tostring(editor.multiselect.sweeping)) + if editor.multiselect.sweeping and not editor.multiselect.active then + print("multiselect enabled") + editor.multiselect.active = true + end + if not osweep and osweep ~= editor.multiselect.sweeping then + editor.multiselect.box = nil + end + if editor.multiselect.active then + doEditorMultiselect() + end + if Keybind:checkPressed(Keybind.editor.room_mode) then if love.keyboard.isDown("lshift") then editor.room_mode = "delete" @@ -125,6 +140,10 @@ function drawEditor() if editor.palette.active then doEditorPalette() end + if editor.multiselect.box ~= nil then + frameDebug("drawing multiselect "..tostring(editor.multiselect.box)) + drawEditorMultiselect() + end end function doEditorEdit() @@ -351,6 +370,25 @@ end function drawEditorRooms() for _, room in pairs(LoadedObjects.Rooms) do love.graphics.setColor(0,0,100,1) - love.graphics.rectangle("line",room.from.x-Camera.pos.x, room.from.y-Camera.pos.y, room.width, room.height) + room:asRect():draw("line") + --love.graphics.rectangle("line",room.from.x-Camera.pos.x, room.from.y-Camera.pos.y, room.width, room.height) end end + +function drawEditorMultiselect() + love.graphics.setColor(100,100,50,1) + editor.multiselect.box:draw("line") +end + +function doEditorMultiselect() + local mousept = Camera:mouseGamePos() + if editor.multiselect.box == nil then + print("sweep started") + editor.multiselect.box = Rect:fromPoints(mousept, mousept) + print("box: "..tostring(editor.multiselect.box)) + elseif editor.multiselect.sweeping then + editor.multiselect.box.max = mousept + frameDebug("swept to "..tostring(mousept)) + end +end + diff --git a/code/rect.lua b/code/rect.lua index c67c9a6..55e84a8 100644 --- a/code/rect.lua +++ b/code/rect.lua @@ -6,6 +6,7 @@ Rect.__index = Rect function Rect:fromPoints(pt1, pt2) local o = { min = pt1, max = pt2 } setmetatable(o, self) + return o end function Rect:fromCoords(x1, y1, x2, y2) @@ -64,3 +65,11 @@ function Rect:overlapsRect(other) return self.min.x