about summary refs log blame commit diff stats
path: root/button.lua
blob: 4cafc86d7e35705d654fb7cd7fb2282bdb7d524b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12



                                                                              
 
                    





                                                                            
                                       


                                
                                                         
                                        
                                                             
                                               
                            
         


       
-- Simple immediate-mode buttons with (currently) just an onpress1 handler for
-- the left button.
-- If the handler returns true, it'll prevent any further processing of the
-- event.

Button_handlers = {}

-- draw button and queue up event handlers
function button(name, params)
  love.graphics.setColor(params.color[1], params.color[2], params.color[3])
  love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5)
  if params.icon then params.icon(params.x, params.y) end
  table.insert(Button_handlers, params)
end

-- process button event handlers
function propagate_to_button_handlers(x, y, mouse_button)
  for _,ev in ipairs(Button_handlers) do
    if x>ev.x and x<ev.x+ev.w and y>ev.y and y<ev.y+ev.h then
      if ev.onpress1 and mouse_button == 1 then
        return ev.onpress1()
      end
    end
  end
end