Page 1 of 1

hovering buttons question

Posted: Sat Feb 22, 2014 4:47 pm
by Polaris
Hello i would like to have advice on this chunk of code:

Code: Select all

function love.load()
	my=love.mouse.getY   
	mx=love.mouse.getX
	x=0
	y=0
	w=0
	h=0
	over =false
	buton_id=""
	
	start = buton(50,50,100,100,"start")
	options = buton(200,50,100,100,"options")
	quit = buton(350,50,100,100,"quit")
end

function love.update(dt)
end

function love.draw()
	 start:draw(start.x,start.y,start.w,start.h)
	 quit:draw(start.x,start.y,start.w,start.h)
	 options:draw(start.x,start.y,start.w,start.h)
	 love.graphics.setColor(255,255,255)
	love.graphics.print("ID of the overed button: "..tostring(buton_id),400,380)
	love.graphics.print("mouse over start: "..tostring(start.overed),400,400)
	love.graphics.print("mouse over quit: "..tostring(quit.overed),400,420)
	love.graphics.print("mouse over optoins: "..tostring(options.overed),400,440)
	
	
	end

function love.keypressed(key)
end

function love.mousepressed()
	
end




function buton(x,y,w,h,id,overed)
  return{
			x=x ,y=y ,w=w ,h=h, id=id, overed, 
			draw = function(self) my=love.mouse.getY() mx=love.mouse.getX() 
					if mx > self.x and  mx < self.x+self.w and my > self.y and my < self.y+self.h then
						self.overed = true
						buton_id = tostring(self.id)
					else
						self.overed = false
						buton_id = nil
					end
					if self.overed == true then
						buton_id = tostring(self.id)
						love.graphics.setColor(255,255,255)
						love.graphics.print(tostring(self.id),self.x,self.y)
						love.graphics.setColor(255,255,0)
						love.graphics.rectangle("fill",self.x,self.y,self.w,self.h)
					else
						buton_id = nil
						love.graphics.setColor(0,255,0)
						love.graphics.print(tostring(self.id),self.x,self.y)
						love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
					end
			end
		}
end
I am a hobbyist that give a try from times to times at coding.
I would like to know if the code above could be a good start for a button stuff inside an app?
are there big mistake done here?
why the Id of the hovered button only return when i am on the last created button?("quit" for example)

I read this marvelous topics that helps great and got me started:viewtopic.php?f=4&t=77199&hilit=object
Kikito reply have been of great help!

I thank you for your time.
don't ask for a corrected code but want to know what i am missing.
see you

Re: hovering buttons question

Posted: Sat Feb 22, 2014 11:47 pm
by Azhukar
Polaris wrote:why the Id of the hovered button only return when i am on the last created button?("quit" for example)
Because every button always calls "buton_id = nil" when the mouse is not over it.

Re: hovering buttons question

Posted: Sun Feb 23, 2014 10:16 am
by Polaris
hello thank for help

Code: Select all

--buton_id = nil
i put this line to comment and it is ok.But i don't understand why :(
i would like the var buton_id gone nil if i am not hovering any button and i don't understand the way i would do that.
could someone put me on the way if time permit it?

i continue my search.
thanks a lot.

Re: hovering buttons question

Posted: Sun Feb 23, 2014 11:07 am
by SneakySnake
Polaris wrote: why the Id of the hovered button only return when i am on the last created button?("quit" for example)
It's not the creation order that decides it, it's the call order of buton:draw().

The call order in your code is

Code: Select all

    start:draw(start.x,start.y,start.w,start.h)
    quit:draw(start.x,start.y,start.w,start.h)
    options:draw(start.x,start.y,start.w,start.h)
Let's say you are hovering over options.
start:draw() will set buton_id to nil.
quit:draw() will set buton_id to nil.
options:draw() will set buton_id to "options".
All right, just what we wanted to happen! Now buton_id is "options"!

Let's hover over quit now.
start:draw() will set buton_id to nil.
quit:draw() will set buton_id to "quit". All right!
options:draw() will set buton_id to nil. Wait what?
Not what we wanted. Now buton_id is nil.

How could we prevent the draw() calls from setting buton_id to nil after a valid id was set?
A naive solution would be something like this:

Code: Select all

function love.load()
   my=love.mouse.getY   
   mx=love.mouse.getX
   x=0
   y=0
   w=0
   h=0
   over =false
   buton_id=""
   
   start = buton(50,50,100,100,"start")
   options = buton(200,50,100,100,"options")
   quit = buton(350,50,100,100,"quit")
end

function love.draw()
    valid_id_was_set = false -- No valid id was set yet
    start:draw(start.x,start.y,start.w,start.h)
    quit:draw(start.x,start.y,start.w,start.h)
    options:draw(start.x,start.y,start.w,start.h)
    love.graphics.setColor(255,255,255)
   love.graphics.print("ID of the overed button: "..tostring(buton_id),400,380)
   love.graphics.print("mouse over start: "..tostring(start.overed),400,400)
   love.graphics.print("mouse over quit: "..tostring(quit.overed),400,420)
   love.graphics.print("mouse over optoins: "..tostring(options.overed),400,440)
   
   
   end

function buton(x,y,w,h,id,overed)
  return{
         x=x ,y=y ,w=w ,h=h, id=id, overed, 
         draw = function(self) my=love.mouse.getY() mx=love.mouse.getX() 
               if mx > self.x and  mx < self.x+self.w and my > self.y and my < self.y+self.h then
                  self.overed = true
               else
                  self.overed = false
               end
               if self.overed == true then
                  buton_id = tostring(self.id)
                  valid_id_was_set = true -- A valid id was set
                  love.graphics.setColor(255,255,255)
                  love.graphics.print(tostring(self.id),self.x,self.y)
                  love.graphics.setColor(255,255,0)
                  love.graphics.rectangle("fill",self.x,self.y,self.w,self.h)
               else
                   -- Only set it to nil if no valid id was set
                  if not valid_id_was_set then
                    buton_id = nil
                  end
                  love.graphics.setColor(0,255,0)
                  love.graphics.print(tostring(self.id),self.x,self.y)
                  love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
               end
         end
      }
end
But wait, isn't buton_id being nil mean that no valid id was set yet?
We don't need a separate variable.

Code: Select all

function love.load()
   my=love.mouse.getY   
   mx=love.mouse.getX
   x=0
   y=0
   w=0
   h=0
   over =false
   buton_id=""
   
   start = buton(50,50,100,100,"start")
   options = buton(200,50,100,100,"options")
   quit = buton(350,50,100,100,"quit")
end

function love.draw()
    buton_id = nil -- No valid id was set yet
    start:draw(start.x,start.y,start.w,start.h)
    quit:draw(start.x,start.y,start.w,start.h)
    options:draw(start.x,start.y,start.w,start.h)
    love.graphics.setColor(255,255,255)
   love.graphics.print("ID of the overed button: "..tostring(buton_id),400,380)
   love.graphics.print("mouse over start: "..tostring(start.overed),400,400)
   love.graphics.print("mouse over quit: "..tostring(quit.overed),400,420)
   love.graphics.print("mouse over optoins: "..tostring(options.overed),400,440)
   
   
   end

function buton(x,y,w,h,id,overed)
  return{
         x=x ,y=y ,w=w ,h=h, id=id, overed, 
         draw = function(self) my=love.mouse.getY() mx=love.mouse.getX() 
               if mx > self.x and  mx < self.x+self.w and my > self.y and my < self.y+self.h then
                  self.overed = true
               else
                  self.overed = false
               end
               if self.overed == true then
                  buton_id = tostring(self.id)
                  love.graphics.setColor(255,255,255)
                  love.graphics.print(tostring(self.id),self.x,self.y)
                  love.graphics.setColor(255,255,0)
                  love.graphics.rectangle("fill",self.x,self.y,self.w,self.h)
               else
                  love.graphics.setColor(0,255,0)
                  love.graphics.print(tostring(self.id),self.x,self.y)
                  love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
               end
         end
      }
end

Re: hovering buttons question

Posted: Sun Feb 23, 2014 11:43 am
by Polaris
ha ok finally!

i thought that my self.id was looked up as the program was running, i did realise that it was only during the draw callbacks.
i understand the mistake.(you have to think "what happend every frame?")
and voila.

i thank you much