Skip to content

Commit

Permalink
added absolute mode button functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Retrisma committed Oct 8, 2023
1 parent 39fedfc commit 6d200eb
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
7 changes: 5 additions & 2 deletions collision.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ function Rectangle:new(x, y, width, height, xoff, yoff)
end

--checks to see if this rectangle intersects with a given point
function Rectangle:containspoint(x, y)
return x >= self.x and x <= self.x + self.width and y >= self.y and y <= self.y + self.height
function Rectangle:containspoint(x, y, mode)
mode = mode or "relative"
local xoff = mode == "relative" and 0 or mode == "absolute" and camera.fx
local yoff = mode == "relative" and 0 or mode == "absolute" and camera.fy
return x >= self.x + xoff and x <= self.x + self.width + xoff and y >= self.y + yoff and y <= self.y + self.height + yoff
end

--checks to see if this rectangle intersects with another rectangle, with an optional offset applied to this
Expand Down
1 change: 1 addition & 0 deletions drawing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function drawobject(o)
if drawmode == "absolute" then
love.graphics.push()
love.graphics.origin()
love.graphics.scale(window.scale)
end

o:draw()
Expand Down
2 changes: 0 additions & 2 deletions input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ mouse = { x = 0, y = 0, p = false, op = false, held = false }

function updatemouse()
mouse.x, mouse.y = love.mouse.getPosition()
--mouse.x = mouse.x - camera.fx * window.scale * -1
--mouse.y = mouse.y - camera.fy * window.scale * -1

mouse.x = (mouse.x / window.scale) + camera.fx
mouse.y = (mouse.y / window.scale) + camera.fy
Expand Down
8 changes: 6 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require "drawing"
sti = require "lib/sti"
moonshine = require "lib/moonshine"

window = { width = 1050, height = 600, scale = 1.3 }
window = { width = 800, height = 460, scale = 1.5 }

speed = { target = 1 / 60, multiplier = 1 }

Expand All @@ -26,6 +26,9 @@ physics = { gravity = 100, friction = 20 }
p = {}

function love.load()
window.width = window.width * window.scale
window.height = window.height * window.scale

love.window.setMode(window.width, window.height, { vsync = false })
love.window.setTitle("Love2D Project Template")
love.graphics.setDefaultFilter("nearest", "nearest")
Expand All @@ -39,7 +42,8 @@ function love.load()
if v.user == "player" then player = v break end
end

Button:add(300, 2900, "button", function() showdebug = not showdebug end)
Button:add(50, 100, "button", function() showdebug = not showdebug end)
--Button:add(250, 2200, "button", function() showdebug = not showdebug end)
Textbox:add(500, 290, "debug button", { scroll = false })
end

Expand Down
5 changes: 3 additions & 2 deletions ui.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--button class
Button = inherits(AnimatedSprite, {
state = 1
state = 1,
drawmode = "absolute"
})
local button_mt = class(Button)

Expand All @@ -26,7 +27,7 @@ function Button:add(x, y, anim, action)
end

function Button:update(dt)
if self.bounds:containspoint(mouse.x, mouse.y) then
if self.bounds:containspoint(mouse.x, mouse.y, self.drawmode) then
self.state = 2
if mouse.p or mouse.held then
self.state = 3
Expand Down

0 comments on commit 6d200eb

Please sign in to comment.