Hello! Please help solve the problem: we are working on a game that will changed the screen resolution, and in general everything works, but there is a problem with the buttons - when you move the cursor, they are highlighted out of place. How to fix it?
love.graphics.scale(love.graphics.getHeight() / game.screen_height) - scale for height
buttons code:
arial = love.graphics.newFont("arial.ttf", game.screen_height / 40)
function button_draw()
for i, v in ipairs(button) do
if v.mouseover == false then
love.graphics.setColor(0, 0, 0)
end
if v.mouseover == true then
love.graphics.setColor(0, 255, 255)
end
love.graphics.setFont(arial)
love.graphics.print(v.text, v.x, v.y)
end
end
function button_check()
for i, v in ipairs(button) do
if mousex > v.x and
mousex < v.x + arial:getWidth(v.text) and
mousey > v.y and
mousey < v.y + arial:getHeight() then
v.mouseover = true
else
v.mouseover = false
end
end
end
Problem with buttons when changing screen resolution
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 4
- Joined: Tue Jun 23, 2020 4:58 am
Re: Problem with buttons when changing screen resolution
Are you scaling your mouse input coordinates by the same factor as the screen has been scaled?
-
- Prole
- Posts: 4
- Joined: Tue Jun 23, 2020 4:58 am
Re: Problem with buttons when changing screen resolution
When the screen changes, the buttons also change the size of the scale, save position.
button_spawn(love.graphics.getWidth() / 2, love.graphics.getHeight() / 1.8, "Start game", "start") - position button
But at the same time, if move the mouse cursor over them, then they do not work in their place, that is, the buttons are highlighted and work when you hover over a void, and not at the button itself.
I had a thought that the matter is in setting button_check(), but i do not know what to write
button_spawn(love.graphics.getWidth() / 2, love.graphics.getHeight() / 1.8, "Start game", "start") - position button
But at the same time, if move the mouse cursor over them, then they do not work in their place, that is, the buttons are highlighted and work when you hover over a void, and not at the button itself.
I had a thought that the matter is in setting button_check(), but i do not know what to write
Re: Problem with buttons when changing screen resolution
Here's what I did:
Note that I had to adjust the mouse input based on the scale before it would find the right hotspot. I don't know if you're already doing that. You might need to.
Code: Select all
function screen_class:redraw()
...
love.graphics.scale(self.graphic_scale, self.graphic_scale)
...
-- draw stuff
...
love.graphics.scale(1, 1)
...
end
function love.mousereleased(x, y, button, istouch, presses)
game:mouse_input(x, y)
end
function game_class:mouse_input(ix, iy)
-- get the button's key equivalent
local input = self.screen:get_control(ix, iy)
if input then
self:state_table(input)
end
end
function screen_class:get_control(ix, iy)
-- adjust for scaling
local x = dh.round(ix / self.graphic_scale)
local y = dh.round(iy / self.graphic_scale)
-- look for the matching hotspot
for _, sp in ipairs(self.hotspots) do
if x >= sp.x1 and x <= sp.x2 and y >= sp.y1 and y <= sp.y2 then
return sp.key
end
end
end
-
- Prole
- Posts: 4
- Joined: Tue Jun 23, 2020 4:58 am
Re: Problem with buttons when changing screen resolution
Thank you, but that is a bit wrong. Need another way to fix this.
Re: Problem with buttons when changing screen resolution
Make sure to multiply all the mouse coordinates by love.graphics.getHeight()/game.screen_height too.
Profile. Do you encounter crashes in LÖVE Android and wanna send me logcats? Please hit me up in LÖVE Discord and send the full logcat file!
-
- Prole
- Posts: 4
- Joined: Tue Jun 23, 2020 4:58 am
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 5 guests