about summary refs log tree commit diff stats
path: root/button.lua
blob: 42c44e3eecb0f9942bb5c6389cc3c7be46220ce0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-- simple immediate-mode buttons

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 ev.onpress1() end
    end
  end
end
>, content DrawableInteractive) (*UI, error) { screen, err := tcell.NewScreen() if err != nil { return nil, err } if err = screen.Init(); err != nil { return nil, err } screen.Clear() screen.HideCursor() width, height := screen.Size() state := UI{ Content: content, ctx: NewContext(width, height, screen), screen: screen, tcEvents: make(chan tcell.Event, 10), invalidations: make(chan interface{}), } state.exit.Store(false) go (func() { for !state.ShouldExit() { state.tcEvents <- screen.PollEvent() } })() go (func() { state.invalidations <- nil })() content.OnInvalidate(func(_ Drawable) { go (func() { state.invalidations <- nil })() }) content.Focus(true) return &state, nil } func (state *UI) ShouldExit() bool { return state.exit.Load().(bool) } func (state *UI) Exit() { state.exit.Store(true) } func (state *UI) Close() { state.screen.Fini() } func (state *UI) Tick() bool { select { case event := <-state.tcEvents: switch event := event.(type) { case *tcell.EventResize: state.screen.Clear() width, height := event.Size() state.ctx = NewContext(width, height, state.screen) state.Content.Invalidate() } state.Content.Event(event) case <-state.invalidations: for { // Flush any other pending invalidations select { case <-state.invalidations: break default: goto done } } done: state.Content.Draw(state.ctx) state.screen.Show() default: return false } return true }