2018-06-17 02:59:23 +00:00
|
|
|
// Package events manages mouse and keyboard SDL events for Doodle.
|
|
|
|
package events
|
|
|
|
|
2018-08-17 03:37:19 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
2018-07-22 03:43:01 +00:00
|
|
|
|
2018-06-17 02:59:23 +00:00
|
|
|
// State keeps track of event states.
|
|
|
|
type State struct {
|
|
|
|
// Mouse buttons.
|
2019-06-09 00:02:28 +00:00
|
|
|
Button1 *BoolTick // left
|
|
|
|
Button2 *BoolTick // right
|
|
|
|
Button3 *BoolTick // middle
|
2018-06-17 14:56:51 +00:00
|
|
|
|
2019-07-03 23:22:30 +00:00
|
|
|
EscapeKey *BoolTick
|
|
|
|
EnterKey *BoolTick
|
|
|
|
ShiftActive *BoolTick
|
|
|
|
ControlActive *BoolTick
|
|
|
|
KeyName *StringTick
|
|
|
|
Up *BoolTick
|
|
|
|
Left *BoolTick
|
|
|
|
Right *BoolTick
|
|
|
|
Down *BoolTick
|
2018-06-17 02:59:23 +00:00
|
|
|
|
|
|
|
// Cursor positions.
|
2018-06-17 14:56:51 +00:00
|
|
|
CursorX *Int32Tick
|
|
|
|
CursorY *Int32Tick
|
2018-10-19 20:31:58 +00:00
|
|
|
|
|
|
|
// Window events: window has changed size.
|
|
|
|
Resized *BoolTick
|
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{
|
2019-07-03 23:22:30 +00:00
|
|
|
Button1: &BoolTick{},
|
|
|
|
Button2: &BoolTick{},
|
|
|
|
Button3: &BoolTick{},
|
|
|
|
EscapeKey: &BoolTick{},
|
|
|
|
EnterKey: &BoolTick{},
|
|
|
|
ShiftActive: &BoolTick{},
|
|
|
|
ControlActive: &BoolTick{},
|
|
|
|
KeyName: &StringTick{},
|
|
|
|
Up: &BoolTick{},
|
|
|
|
Left: &BoolTick{},
|
|
|
|
Right: &BoolTick{},
|
|
|
|
Down: &BoolTick{},
|
|
|
|
CursorX: &Int32Tick{},
|
|
|
|
CursorY: &Int32Tick{},
|
|
|
|
Resized: &BoolTick{},
|
2018-06-17 03:21:42 +00:00
|
|
|
}
|
2018-06-17 02:59:23 +00:00
|
|
|
}
|
2018-07-22 03:43:01 +00:00
|
|
|
|
|
|
|
// ReadKey returns the normalized key symbol being pressed,
|
|
|
|
// taking the Shift key into account. QWERTY keyboard only, probably.
|
|
|
|
func (ev *State) ReadKey() string {
|
|
|
|
if key := ev.KeyName.Read(); key != "" {
|
|
|
|
if ev.ShiftActive.Pressed() {
|
|
|
|
if symbol, ok := shiftMap[key]; ok {
|
|
|
|
return symbol
|
|
|
|
}
|
|
|
|
return strings.ToUpper(key)
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// shiftMap maps keys to their Shift versions.
|
|
|
|
var shiftMap = map[string]string{
|
|
|
|
"`": "~",
|
|
|
|
"1": "!",
|
|
|
|
"2": "@",
|
|
|
|
"3": "#",
|
|
|
|
"4": "$",
|
|
|
|
"5": "%",
|
|
|
|
"6": "^",
|
|
|
|
"7": "&",
|
|
|
|
"8": "*",
|
|
|
|
"9": "(",
|
|
|
|
"0": ")",
|
|
|
|
"-": "_",
|
|
|
|
"=": "+",
|
|
|
|
"[": "{",
|
|
|
|
"]": "}",
|
|
|
|
`\`: "|",
|
|
|
|
";": ":",
|
|
|
|
`'`: `"`,
|
|
|
|
",": "<",
|
|
|
|
".": ">",
|
|
|
|
"/": "?",
|
|
|
|
}
|