doodle/events/events.go
Noah Petherbridge 5434484b6e Abstract Drawing Canvas into Reusable Widget
The `level.Canvas` is a widget that holds onto its Palette and Grid and
has interactions to allow scrolling and editing the grid using the
swatches available on the palette.

Thus all of the logic in the Editor Mode for drawing directly onto the
root SDL surface are now handled inside a level.Canvas instance.

The `level.Canvas` widget has the following properties:
* Like any widget it has an X,Y position and a width/height.
* It has a Scroll position to control which slice of its drawing will be
  visible inside its bounding box.
* It supports levels having negative coordinates for their pixels. It
  doesn't care. The default Scroll position is (0,0) at the top left
  corner of the widget but you can scroll into the negatives and see the
  negative pixels.
* Keyboard keys will scroll the viewport inside the canvas.
* The canvas draws only the pixels that are visible inside its bounding
  box.

This feature will eventually pave the way toward:
* Doodads being dropped on top of your map, each Doodad being its own
  Canvas widget.
* Using drawings as button icons for the user interface, as the Canvas
  is a normal widget.
2018-08-16 20:37:19 -07:00

88 lines
1.7 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
// Screenshot key.
ScreenshotKey *BoolTick
EscapeKey *BoolTick
EnterKey *BoolTick
ShiftActive *BoolTick
KeyName *StringTick
Up *BoolTick
Left *BoolTick
Right *BoolTick
Down *BoolTick
// Cursor positions.
CursorX *Int32Tick
CursorY *Int32Tick
}
// New creates a new event state manager.
func New() *State {
return &State{
Button1: &BoolTick{},
Button2: &BoolTick{},
ScreenshotKey: &BoolTick{},
EscapeKey: &BoolTick{},
EnterKey: &BoolTick{},
ShiftActive: &BoolTick{},
KeyName: &StringTick{},
Up: &BoolTick{},
Left: &BoolTick{},
Right: &BoolTick{},
Down: &BoolTick{},
CursorX: &Int32Tick{},
CursorY: &Int32Tick{},
}
}
// 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": ")",
"-": "_",
"=": "+",
"[": "{",
"]": "}",
`\`: "|",
";": ":",
`'`: `"`,
",": "<",
".": ">",
"/": "?",
}