Problem with buttons when changing screen resolution

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Flower_Flower
Prole
Posts: 4
Joined: Tue Jun 23, 2020 4:58 am

Problem with buttons when changing screen resolution

Post by Flower_Flower » Tue Jun 23, 2020 5:21 am

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

duaner
Prole
Posts: 9
Joined: Thu May 07, 2020 6:43 pm
Contact:

Re: Problem with buttons when changing screen resolution

Post by duaner » Tue Jun 23, 2020 9:48 pm

Are you scaling your mouse input coordinates by the same factor as the screen has been scaled?

Flower_Flower
Prole
Posts: 4
Joined: Tue Jun 23, 2020 4:58 am

Re: Problem with buttons when changing screen resolution

Post by Flower_Flower » Wed Jun 24, 2020 9:59 am

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

duaner
Prole
Posts: 9
Joined: Thu May 07, 2020 6:43 pm
Contact:

Re: Problem with buttons when changing screen resolution

Post by duaner » Wed Jun 24, 2020 11:30 pm

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.

Flower_Flower
Prole
Posts: 4
Joined: Tue Jun 23, 2020 4:58 am

Re: Problem with buttons when changing screen resolution

Post by Flower_Flower » Sat Jun 27, 2020 6:36 am

Thank you, but that is a bit wrong. Need another way to fix this.

User avatar
AuahDark
Prole
Posts: 47
Joined: Mon Oct 23, 2017 2:34 pm
Location: Indonesia
Contact:

Re: Problem with buttons when changing screen resolution

Post by AuahDark » Sat Jun 27, 2020 3:01 pm

Make sure to multiply all the mouse coordinates by love.graphics.getHeight()/game.screen_height too.
Profile. I don't always online in forum, so Discord is preferred. Am I LÖVE for Android dev or not? Only you can tell.

Flower_Flower
Prole
Posts: 4
Joined: Tue Jun 23, 2020 4:58 am

Re: Problem with buttons when changing screen resolution

Post by Flower_Flower » Sat Jun 27, 2020 4:25 pm

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! ^_^

Post Reply

Who is online

Users browsing this forum: Google [Bot], ivan and 35 guests