2018-06-17 02:59:23 +00:00
|
|
|
// Package events manages mouse and keyboard SDL events for Doodle.
|
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
|
|
|
// State keeps track of event states.
|
|
|
|
type State struct {
|
|
|
|
// Mouse buttons.
|
2018-06-17 14:56:51 +00:00
|
|
|
Button1 *BoolTick
|
|
|
|
Button2 *BoolTick
|
|
|
|
|
|
|
|
// Screenshot key.
|
|
|
|
ScreenshotKey *BoolTick
|
2018-06-17 02:59:23 +00:00
|
|
|
|
|
|
|
// Cursor positions.
|
2018-06-17 14:56:51 +00:00
|
|
|
CursorX *Int32Tick
|
|
|
|
CursorY *Int32Tick
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new event state manager.
|
|
|
|
func New() *State {
|
2018-06-17 03:21:42 +00:00
|
|
|
return &State{
|
2018-06-17 14:56:51 +00:00
|
|
|
Button1: &BoolTick{},
|
|
|
|
Button2: &BoolTick{},
|
|
|
|
ScreenshotKey: &BoolTick{},
|
|
|
|
CursorX: &Int32Tick{},
|
|
|
|
CursorY: &Int32Tick{},
|
2018-06-17 03:21:42 +00:00
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Poll for events.
|
2018-06-17 03:21:42 +00:00
|
|
|
func (s *State) Poll(ticks uint64) (*State, error) {
|
2018-06-17 02:59:23 +00:00
|
|
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
|
|
|
switch t := event.(type) {
|
|
|
|
case *sdl.QuitEvent:
|
|
|
|
return s, errors.New("quit")
|
|
|
|
case *sdl.MouseMotionEvent:
|
|
|
|
if DebugMouseEvents {
|
2018-06-17 03:21:42 +00:00
|
|
|
log.Debug("[%d ms] tick:%d MouseMotion type:%d id:%d x:%d y:%d xrel:%d yrel:%d",
|
|
|
|
t.Timestamp, ticks, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel,
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the cursor position.
|
|
|
|
s.CursorX.Push(t.X)
|
|
|
|
s.CursorY.Push(t.Y)
|
|
|
|
s.Button1.Push(t.State == 1)
|
|
|
|
case *sdl.MouseButtonEvent:
|
|
|
|
if DebugClickEvents {
|
2018-06-17 03:21:42 +00:00
|
|
|
log.Debug("[%d ms] tick:%d MouseButton type:%d id:%d x:%d y:%d button:%d state:%d",
|
|
|
|
t.Timestamp, ticks, t.Type, t.Which, t.X, t.Y, t.Button, t.State,
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the cursor position.
|
|
|
|
s.CursorX.Push(t.X)
|
|
|
|
s.CursorY.Push(t.Y)
|
|
|
|
|
|
|
|
// Is a mouse button pressed down?
|
|
|
|
if t.Button == 1 {
|
2018-06-17 03:21:42 +00:00
|
|
|
var eventName string
|
2018-06-17 02:59:23 +00:00
|
|
|
if DebugClickEvents {
|
|
|
|
if t.State == 1 && s.Button1.Now == false {
|
2018-06-17 03:21:42 +00:00
|
|
|
eventName = "DOWN"
|
2018-06-17 02:59:23 +00:00
|
|
|
} else if t.State == 0 && s.Button1.Now == true {
|
2018-06-17 03:21:42 +00:00
|
|
|
eventName = "UP"
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-17 03:21:42 +00:00
|
|
|
|
|
|
|
if eventName != "" {
|
|
|
|
log.Debug("tick:%d Mouse Button1 %s BEFORE: %+v",
|
|
|
|
ticks,
|
|
|
|
eventName,
|
|
|
|
s.Button1,
|
|
|
|
)
|
|
|
|
s.Button1.Push(eventName == "DOWN")
|
|
|
|
log.Debug("tick:%d Mouse Button1 %s AFTER: %+v",
|
|
|
|
ticks,
|
|
|
|
eventName,
|
|
|
|
s.Button1,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Return the event immediately.
|
|
|
|
return s, nil
|
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 03:21:42 +00:00
|
|
|
// s.Button2.Push(t.Button == 3 && t.State == 1)
|
2018-06-17 02:59:23 +00:00
|
|
|
case *sdl.MouseWheelEvent:
|
|
|
|
if DebugMouseEvents {
|
2018-06-17 03:21:42 +00:00
|
|
|
log.Debug("[%d ms] tick:%d MouseWheel type:%d id:%d x:%d y:%d",
|
|
|
|
t.Timestamp, ticks, t.Type, t.Which, t.X, t.Y,
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
case *sdl.KeyboardEvent:
|
2018-06-17 03:21:42 +00:00
|
|
|
log.Debug("[%d ms] tick:%d Keyboard type:%d sym:%c modifiers:%d state:%d repeat:%d\n",
|
|
|
|
t.Timestamp, ticks, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat,
|
2018-06-17 02:59:23 +00:00
|
|
|
)
|
2018-06-17 14:56:51 +00:00
|
|
|
if t.Repeat == 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t.Keysym.Scancode {
|
|
|
|
case sdl.SCANCODE_F12:
|
|
|
|
s.ScreenshotKey.Push(t.State == 1)
|
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|