From 60fedd28d7b2674b2c7515fb3c51c4feddb5bddf Mon Sep 17 00:00:00 2001 From: jill Date: Sun, 19 Sep 2021 23:39:05 +0300 Subject: [PATCH] mobile + port everything to use getSafeArea when possible --- main.lua | 56 +++++++++++++++++++++++++++++++++++++++++++++--- src/button.lua | 4 ++-- src/dropdown.lua | 8 +++---- src/graph.lua | 4 ++-- src/slider.lua | 2 +- src/tooltips.lua | 4 ++-- 6 files changed, 64 insertions(+), 14 deletions(-) diff --git a/main.lua b/main.lua index facbdd6..08cc0d1 100644 --- a/main.lua +++ b/main.lua @@ -18,6 +18,28 @@ function getFont(size, italic) return fontCache[size][italic] end +function getMousePosition() + local x, y = love.mouse.getPosition() + + local sx, sy, sw, sh = 0, 0, love.graphics.getWidth(), love.graphics.getHeight() + if love.isVersionCompatible('11.3') then sx, sy, sw, sh = love.window.getSafeArea() end + sx, sy, sw, sh = 100, 100, love.graphics.getWidth() - 200, love.graphics.getHeight() - 200 + + x, y = ((x - sx) / sw) * love.graphics.getWidth(), ((y - sy) / sh) * love.graphics.getHeight() + + return x, y +end + +function getMouseX() + local x, y = getMousePosition() + return x +end + +function getMouseY() + local x, y = getMousePosition() + return y +end + ease = require 'src.ease' slider = require 'src.slider' @@ -54,6 +76,8 @@ screenshot = false screenshotCanvas = nil screenshotsize = 512 +mobile = love.system.getOS() == 'iOS' or love.system.getOS() == 'Android' + -- global for convinience's sake mode = nil @@ -62,6 +86,10 @@ function love.load() love.graphics.setFont(interfaceFont) fontHeight = love.graphics.getFont():getHeight() createUI() + + if mobile then + love.window.setMode(640, 360, {borderless = true, resizable = false, minwidth = 705, minheight = 510, fullscreen = true}) + end end function love.update(dt) @@ -73,7 +101,9 @@ function love.update(dt) end function love.draw() - local sw, sh = love.graphics.getDimensions() + local sx, sy, sw, sh = 0, 0, love.graphics.getWidth(), love.graphics.getHeight() + if love.isVersionCompatible('11.3') then sx, sy, sw, sh = love.window.getSafeArea() end + local mx, my = love.mouse.getPosition() if screenshot then @@ -109,9 +139,15 @@ function love.draw() love.graphics.setLineWidth(2) love.graphics.setColor(0.09, 0.09, 0.12, 1) - love.graphics.rectangle('fill', 0, 0, sw, sh) + love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) love.graphics.setColor(0.08, 0.08, 0.1, 1) - love.graphics.rectangle('line', 0, 0, sw, sh) + love.graphics.rectangle('line', 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) + + love.graphics.push() + love.graphics.translate(sx, sy) + love.graphics.scale(sw/love.graphics.getWidth(), sh/love.graphics.getHeight()) + + sw, sh = love.graphics.getDimensions() love.graphics.setColor(0.2, 0.2, 0.3, 1) love.graphics.print('Box of Eases by oatmealine', outerpadding, sh - fontHeight - outerpadding) @@ -127,14 +163,26 @@ function love.draw() graph.render() tooltips.render() + + love.graphics.pop() end function love.mousepressed(x, y, m) + local sx, sy, sw, sh = 0, 0, love.graphics.getWidth(), love.graphics.getHeight() + if love.isVersionCompatible('11.3') then sx, sy, sw, sh = love.window.getSafeArea() end + + x, y = ((x - sx) / sw) * love.graphics.getWidth(), ((y - sy) / sh) * love.graphics.getHeight() + if dropdown.mousepressed(x, y, m) then return end button.mousepressed(x, y, m) end function love.mousereleased(x, y, m) + local sx, sy, sw, sh = 0, 0, love.graphics.getWidth(), love.graphics.getHeight() + if love.isVersionCompatible('11.3') then sx, sy, sw, sh = love.window.getSafeArea() end + + x, y = ((x - sx) / sw) * love.graphics.getWidth(), ((y - sy) / sh) * love.graphics.getHeight() + if dropdown.mousereleased(x, y, m) then return end end @@ -154,6 +202,8 @@ function love.keypressed(key) print(k, v) ::continue:: end + elseif key == 'f2' then -- mobile mode + mobile = not mobile end end diff --git a/src/button.lua b/src/button.lua index 3eb8cc0..9df96fc 100644 --- a/src/button.lua +++ b/src/button.lua @@ -157,7 +157,7 @@ end function self.update(dt) for i, v in ipairs(buttons) do - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() local targetsize = 1 if mx > v.x and mx < v.x + v.size and my > v.y and my < v.y + v.size and dropdown.openDropdown == 0 then @@ -173,7 +173,7 @@ function self.update(dt) end function self.render() - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() for i, v in ipairs(buttons) do local x, y, w, h = v.x, v.y, v.size, v.size diff --git a/src/dropdown.lua b/src/dropdown.lua index ec5a976..edf8296 100644 --- a/src/dropdown.lua +++ b/src/dropdown.lua @@ -215,7 +215,7 @@ function self.update(dt) end if love.mouse.isDown(1) then - local x, y = love.mouse.getPosition() + local x, y = getMousePosition() for i,v in ipairs(dropdowns) do local h = fontHeight + margin if self.openDropdown == i then @@ -228,12 +228,12 @@ function self.update(dt) end function self.render() - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() for i,v in ipairs(dropdowns) do local x, y, w, h = v.x, v.y, v.width, fontHeight + margin love.graphics.setColor(0.06, 0.06, 0.12, 0.6) - if love.mouse.getX() > x and love.mouse.getX() < x + w and love.mouse.getY() > y and love.mouse.getY() < y + h then + if mx > x and mx < x + w and my > y and my < y + h then love.graphics.setColor(0.8, 0.8, 1, love.mouse.isDown(1) and 0.4 or 0.3) if v.tooltip then tooltips.show(v.tooltip) end end @@ -367,7 +367,7 @@ function self.wheelmoved(x, y) if self.openDropdown ~= 0 then dropdownScroll = dropdownScroll + y else - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() for i,v in ipairs(dropdowns) do local h = fontHeight + margin if mx > v.x and mx < v.x + v.width and my > v.y and my < v.y + h + margin then diff --git a/src/graph.lua b/src/graph.lua index ad8f3a3..b4f0bac 100644 --- a/src/graph.lua +++ b/src/graph.lua @@ -37,7 +37,7 @@ function self.update(dt) local csize = 10 -- preview point size local size = math.min((sw - outerpadding) - ((dropdown.kget('ease2') or dropdown.kget('ease1')).x + dropdownWidth + padding), sh - outerpadding * 2 - padding * 3 - csize) local x, y, w, h = sw - outerpadding - size, outerpadding, size, size - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() if not (love.mouse.isDown(1) and mx > x and mx < x + w and my > y and my < y + h) then timer = (timer + dt * ((slider.kvalue('bpm') or 120)/120)) % 2 @@ -153,7 +153,7 @@ function self.render() end function self.wheelmoved(wx, wy) - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() -- may need to abstract this out, somehow -- todo diff --git a/src/slider.lua b/src/slider.lua index afd0fd3..acef733 100644 --- a/src/slider.lua +++ b/src/slider.lua @@ -126,7 +126,7 @@ function self.update(dt) end function self.render() - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() for i, v in ipairs(sliders) do local x, y, w, h = v.x, v.y, v.width, fontHeight * 1.25 diff --git a/src/tooltips.lua b/src/tooltips.lua index 6d1d456..b5979f8 100644 --- a/src/tooltips.lua +++ b/src/tooltips.lua @@ -11,7 +11,7 @@ local tooltiptargetwidth = 0 local tooltipframe = false function self.update(dt) - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() tooltipx = mix(tooltipx, mx, dt * 18) tooltipy = mix(tooltipy, my, dt * 18) @@ -36,7 +36,7 @@ local function softlimit(x, f) end function self.render() - local mx, my = love.mouse.getPosition() + local mx, my = getMousePosition() if not tooltipframe then tooltiptargetwidth = 0 end