diff --git a/d2common/d2input/doc.go b/d2common/d2input/doc.go new file mode 100644 index 00000000..748f1262 --- /dev/null +++ b/d2common/d2input/doc.go @@ -0,0 +1,2 @@ +// Package d2input provides common input management logic +package d2input diff --git a/d2common/d2input/input_vector.go b/d2common/d2input/input_vector.go index a3d98616..fbc817e3 100644 --- a/d2common/d2input/input_vector.go +++ b/d2common/d2input/input_vector.go @@ -2,6 +2,7 @@ package d2input import "github.com/gravestench/akara" +// NewInputVector creates a new input vector func NewInputVector() *InputVector { v := &InputVector{ KeyVector: akara.NewBitSet(), @@ -12,60 +13,71 @@ func NewInputVector() *InputVector { return v.Clear() } +// InputVector represents the state of keys, modifiers, and mouse buttons. +// It can be used to compare input states, and is intended to be used as such: +// * whatever manages system input keeps a "current" input vector and updates it +// * things that are listening for certain inputs will be compared using `Contains` and `Intersects` methods type InputVector struct { KeyVector *akara.BitSet ModifierVector *akara.BitSet MouseButtonVector *akara.BitSet } +// SetKey sets the corresponding key bit in the keys bitset func (iv *InputVector) SetKey(key Key) *InputVector { return iv.SetKeys([]Key{key}) } +// SetKeys sets multiple key bits in the keys bitset func (iv *InputVector) SetKeys(keys []Key) *InputVector { if len(keys) == 0 { return iv } for _, key := range keys { - iv.KeyVector.Set(int(key), true) + iv.KeyVector.Set(key, true) } return iv } +// SetModifier sets the corresponding modifier bit in the modifier bitset func (iv *InputVector) SetModifier(mod Modifier) *InputVector { return iv.SetModifiers([]Modifier{mod}) } +// SetModifiers sets multiple modifier bits in the modifier bitset func (iv *InputVector) SetModifiers(mods []Modifier) *InputVector { if len(mods) == 0 { return iv } for _, key := range mods { - iv.ModifierVector.Set(int(key), true) + iv.ModifierVector.Set(key, true) } return iv } +// SetMouseButton sets the corresponding mouse button bit in the mouse button bitset func (iv *InputVector) SetMouseButton(button MouseButton) *InputVector { return iv.SetMouseButtons([]MouseButton{button}) } +// SetMouseButtons sets multiple mouse button bits in the mouse button bitset func (iv *InputVector) SetMouseButtons(buttons []MouseButton) *InputVector { if len(buttons) == 0 { return iv } for _, key := range buttons { - iv.MouseButtonVector.Set(int(key), true) + iv.MouseButtonVector.Set(key, true) } return iv } +// Contains returns true if this input vector is a superset of the given input vector func (iv *InputVector) Contains(other *InputVector) bool { keys := iv.KeyVector.ContainsAll(other.KeyVector) buttons := iv.MouseButtonVector.ContainsAll(other.MouseButtonVector) @@ -76,6 +88,7 @@ func (iv *InputVector) Contains(other *InputVector) bool { return keys && mods && buttons } +// Intersects returns true if this input vector shares any bits with the given input vector func (iv *InputVector) Intersects(other *InputVector) bool { keys := iv.KeyVector.Intersects(other.KeyVector) mods := iv.ModifierVector.Intersects(other.ModifierVector) @@ -84,6 +97,7 @@ func (iv *InputVector) Intersects(other *InputVector) bool { return keys || mods || buttons } +// Clear sets all bits in this input vector to 0 func (iv *InputVector) Clear() *InputVector { iv.KeyVector.Clear() iv.ModifierVector.Clear() diff --git a/d2common/d2input/keys.go b/d2common/d2input/keys.go index a41ebe47..71d7e18a 100644 --- a/d2common/d2input/keys.go +++ b/d2common/d2input/keys.go @@ -2,8 +2,10 @@ package d2input import "github.com/hajimehoshi/ebiten/v2" +// Key represents a keyboard key type Key = int +// Keys const ( Key0 = Key(ebiten.Key0) Key1 = Key(ebiten.Key1) diff --git a/d2common/d2input/modifiers.go b/d2common/d2input/modifiers.go index 8b893059..4adab848 100644 --- a/d2common/d2input/modifiers.go +++ b/d2common/d2input/modifiers.go @@ -2,8 +2,10 @@ package d2input import "github.com/hajimehoshi/ebiten/v2" +// Modifier represents a keyboard modifier key type Modifier = int +// Modifiers const ( ModAlt = Modifier(ebiten.KeyAlt) ModControl = Modifier(ebiten.KeyControl) diff --git a/d2common/d2input/mouse_buttons.go b/d2common/d2input/mouse_buttons.go index e9ee53d7..cb51ddd5 100644 --- a/d2common/d2input/mouse_buttons.go +++ b/d2common/d2input/mouse_buttons.go @@ -2,8 +2,10 @@ package d2input import "github.com/hajimehoshi/ebiten/v2" +// MouseButton represents a button on a mouse type MouseButton = int +// MouseButtons const ( MouseButtonLeft = MouseButton(ebiten.MouseButtonLeft) MouseButtonRight = MouseButton(ebiten.MouseButtonRight)