tlfres cursor change works but click doesn’t

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
randy0428
Citizen
Posts: 51
Joined: Sun Jul 31, 2016 10:19 am

tlfres cursor change works but click doesn’t

Post by randy0428 »

I have a game I want to export to Android, so I’m trying to use tlfres to get it to scale to different resolutions. When I first tried it, it seemed to be working. It scaled as expected.

The game has a function to change the cursor when it is over a clickable area and this worked as expected.

However, when I actually clicked the mouse (Actually, I’m using a laptop with touchpad.), it seemed to be using the UNSCALED dimensions for the clicking.

I made a much simpler “game” to test this to see if the problem is something in the complexity of my game, but the results are the same. This simpler game is just a box with text inside to say how many times the box has been clicked. It scales fine, The cusor changes when it is in the box. But the clicking seems to occur based on the original dimensions.

A couple of notes:
The background is black, so in tlfres.lua I change the definition of black to {1,1,0} so the letterboxing is yellow to contrast with the black background.
I used global variables defined in conf.lua for the screen width and height and these variables are used instead of 800, 600, although I did try it with the numbers, not the variables and got the same result.

Any suggestions on what I’m doing wrong? I’ve uploaded the zip file so you can look at the code and the *.love file so you can run it.

Code: Select all

local TLfres = require "tlfres"

local numClicks = 0
boxXleft = 100
boxYtop = 100
boxWidth = 300
boxHeight = 200
boxXright = boxXleft + boxWidth
boxYbottom = boxYtop + boxHeight

function love.load()
    stdCursor = love.mouse.newCursor( "bandeira-usa-e.png", 15, 8 )
    clickCursor = love.mouse.newCursor( "bandeira-brasil.png", 15, 8 ) 
    love.mouse.setCursor( stdCursor )  
end

function love.update(dt)
  alterarCursor()
  love.mousereleased = function(x, y, button)
    if inBox( x, y ) then
      numClicks = numClicks + 1
    return
    end
  end 
end
  
function love.draw()love.graphics.setColor(1,1,1)
TLfres.beginRendering( SCREEN_WIDTH, SCREEN_HEIGHT )
  love.graphics.rectangle( "fill", boxXleft, boxYtop, boxWidth, boxHeight )
  love.graphics.setColor(1,0,0) 
  love.graphics.print( "This box has been clicked " .. numClicks .. " times", boxXleft + 25, boxYtop + 100 )
TLfres.endRendering()
end

function inBox( x, y )
  return (x > boxXleft and x < boxXright) and (y > boxYtop and y < boxYbottom)
end

function alterarCursor()
  local x, y = TLfres.getMousePosition( SCREEN_WIDTH, SCREEN_HEIGHT )
  
      if inBox( x, y ) then 
        love.mouse.setCursor( clickCursor )
      else
        love.mouse.setCursor( stdCursor )      
      end
  return
end
Attachments
tlfres_test.zip
(3.97 KiB) Downloaded 110 times
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: tlfres cursor change works but click doesn’t

Post by grump »

Disclaimer: I don't know what tlfres is or how it works.
randy0428 wrote: Mon Dec 17, 2018 4:02 pm However, when I actually clicked the mouse (Actually, I’m using a laptop with touchpad.), it seemed to be using the UNSCALED dimensions for the clicking.

Code: Select all

function love.update(dt)
  alterarCursor()
  love.mousereleased = function(x, y, button)
    if inBox( x, y ) then
      numClicks = numClicks + 1
    return
    end
  end 
end
...
function inBox( x, y )
  return (x > boxXleft and x < boxXright) and (y > boxYtop and y < boxYbottom)
end
This looks like you're using unscaled coordinates when you check where the player has clicked. You need to scale the mouse position; there's probably some function in tlfres that does it for you.

Also, remove love.mousereleased from love.update and put it at the top level of your code. It doesn't belong in update and putting it there has some negative implications.
randy0428
Citizen
Posts: 51
Joined: Sun Jul 31, 2016 10:19 am

Re: tlfres cursor change works but click doesn’t

Post by randy0428 »

After looking more, I suspect that the problem is that

Code: Select all

love.mousereleased = function(x, y, button)
is giving the original coordinates. I don't know how to make them adjust for this function like

Code: Select all

TLfres.getMousePosition(width, height)
does for

Code: Select all

TLfres.getMousePosition
.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: tlfres cursor change works but click doesn’t

Post by grump »

Did you even read what I responded to you? Like, at all?
randy0428
Citizen
Posts: 51
Joined: Sun Jul 31, 2016 10:19 am

Re: tlfres cursor change works but click doesn’t

Post by randy0428 »

Thanks, grump.
First, I moved the love.mousereleased callback to a position above the 3 primary löve functions (load, update and draw).
Then I used the TLfres.getScale() function from TLfres to adjust the x and y coordinates. This seems to be working. The code is below.

But this makes me wonder about the necessity of the TLfres.getMousePosition() function. Why not just scale the coordinates derived from love.mouse.getPosition() in the same way?

Code: Select all

love.mousereleased = function(x, y, button)
  x = x * TLfres.getScale( SCREEN_WIDTH, SCREEN_HEIGHT )
  y = y * TLfres.getScale( SCREEN_WIDTH, SCREEN_HEIGHT )
   if inBox( x, y ) then
     numClicks = numClicks + 1
   return
   end
end
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: tlfres cursor change works but click doesn’t

Post by grump »

randy0428 wrote: Mon Dec 17, 2018 4:48 pm First, I moved the love.mousereleased callback to a position above the 3 primary löve functions (load, update and draw).
The actual order of those functions is not relevant, just don't put them in other functions that are called frequently. In most cases it's best and easiest to define them at the top level of the function hierarchy.
But this makes me wonder about the necessity of the TLfres.getMousePosition() function. Why not just scale the coordinates derived from love.mouse.getPosition() in the same way?
Maybe the library provides some other way to do what you ask, or maybe the author just didn't think that clicking on something is as important as pointing at something.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests