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




                                                                            
 
                                          


                                      
                                                                            
                                             
                                             

                                
                                                                              

                                      
                              
                                              
                                                             
                                               

                               
           
         
       
                                         
   
-- Simple immediate-mode buttons with (currently) just an onpress1 handler for
-- the left button.
--
-- Buttons can nest in principle, though I haven't actually used that yet.
--
-- Don't rely on the order in which handlers are run. Within any widget, all
-- applicable button handlers will run. If _any_ of them returns true, the
-- event will continue to propagate elsewhere in the widget.

-- draw button and queue up event handlers
function button(State, name, params)
  if State.button_handlers == nil then
    State.button_handlers = {}
  end
  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) end
  table.insert(State.button_handlers, params)
end

-- process button event handlers
function mouse_press_consumed_by_any_button_handler(State, x, y, mouse_button)
  if State.button_handlers == nil then
    return
  end
  local button_pressed = false
  local consume_press = true
  for _,ev in ipairs(State.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
        button_pressed = true
        if ev.onpress1() then
          consume_press = false
        end
      end
    end
  end
  return button_pressed and consume_press
end