1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-03 06:20:43 +00:00

fix lint errors in d2common/d2input

This commit is contained in:
gravestench 2020-11-28 16:02:20 -08:00
parent e6d418fdb2
commit bafc637265
5 changed files with 25 additions and 3 deletions

2
d2common/d2input/doc.go Normal file
View File

@ -0,0 +1,2 @@
// Package d2input provides common input management logic
package d2input

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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)