Page 1 of 1
Problem with buttons when changing screen resolution
Posted: Tue Jun 23, 2020 5:21 am
by Flower_Flower
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
Re: Problem with buttons when changing screen resolution
Posted: Tue Jun 23, 2020 9:48 pm
by duaner
Are you scaling your mouse input coordinates by the same factor as the screen has been scaled?
Re: Problem with buttons when changing screen resolution
Posted: Wed Jun 24, 2020 9:59 am
by Flower_Flower
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
Re: Problem with buttons when changing screen resolution
Posted: Wed Jun 24, 2020 11:30 pm
by duaner
Here's what I did:
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
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.
Re: Problem with buttons when changing screen resolution
Posted: Sat Jun 27, 2020 6:36 am
by Flower_Flower
Thank you, but that is a bit wrong. Need another way to fix this.
Re: Problem with buttons when changing screen resolution
Posted: Sat Jun 27, 2020 3:01 pm
by AuahDark
Make sure to multiply all the mouse coordinates by love.graphics.getHeight()/game.screen_height too.
Re: Problem with buttons when changing screen resolution
Posted: Sat Jun 27, 2020 4:25 pm
by Flower_Flower
AuahDark wrote: ↑Sat Jun 27, 2020 3:01 pm
Make sure to multiply all the mouse coordinates by love.graphics.getHeight()/game.screen_height too.
Yes, it worked! Thank you very much! ^_^