doodle/lib/events/events.go
Noah Petherbridge 52a2545692 Remove ScreenshotKey Event, Add F* Key Handlers
* The F3 key now toggles the Debug Overlay, which is now OFF by default.
* The F4 key now toggles the Debug Collision Boxes feature.
2019-04-19 16:21:04 -07:00

89 lines
1.6 KiB
Go

// Package events manages mouse and keyboard SDL events for Doodle.
package events
import (
"strings"
)
// State keeps track of event states.
type State struct {
// Mouse buttons.
Button1 *BoolTick
Button2 *BoolTick
EscapeKey *BoolTick
EnterKey *BoolTick
ShiftActive *BoolTick
KeyName *StringTick
Up *BoolTick
Left *BoolTick
Right *BoolTick
Down *BoolTick
// Cursor positions.
CursorX *Int32Tick
CursorY *Int32Tick
// Window events: window has changed size.
Resized *BoolTick
}
// New creates a new event state manager.
func New() *State {
return &State{
Button1: &BoolTick{},
Button2: &BoolTick{},
EscapeKey: &BoolTick{},
EnterKey: &BoolTick{},
ShiftActive: &BoolTick{},
KeyName: &StringTick{},
Up: &BoolTick{},
Left: &BoolTick{},
Right: &BoolTick{},
Down: &BoolTick{},
CursorX: &Int32Tick{},
CursorY: &Int32Tick{},
Resized: &BoolTick{},
}
}
// 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": ")",
"-": "_",
"=": "+",
"[": "{",
"]": "}",
`\`: "|",
";": ":",
`'`: `"`,
",": "<",
".": ">",
"/": "?",
}