mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-09 10:06:35 -05:00
fix lint errors in d2common/d2input
This commit is contained in:
parent
e6d418fdb2
commit
bafc637265
2
d2common/d2input/doc.go
Normal file
2
d2common/d2input/doc.go
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
// Package d2input provides common input management logic
|
||||||
|
package d2input
|
@ -2,6 +2,7 @@ package d2input
|
|||||||
|
|
||||||
import "github.com/gravestench/akara"
|
import "github.com/gravestench/akara"
|
||||||
|
|
||||||
|
// NewInputVector creates a new input vector
|
||||||
func NewInputVector() *InputVector {
|
func NewInputVector() *InputVector {
|
||||||
v := &InputVector{
|
v := &InputVector{
|
||||||
KeyVector: akara.NewBitSet(),
|
KeyVector: akara.NewBitSet(),
|
||||||
@ -12,60 +13,71 @@ func NewInputVector() *InputVector {
|
|||||||
return v.Clear()
|
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 {
|
type InputVector struct {
|
||||||
KeyVector *akara.BitSet
|
KeyVector *akara.BitSet
|
||||||
ModifierVector *akara.BitSet
|
ModifierVector *akara.BitSet
|
||||||
MouseButtonVector *akara.BitSet
|
MouseButtonVector *akara.BitSet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetKey sets the corresponding key bit in the keys bitset
|
||||||
func (iv *InputVector) SetKey(key Key) *InputVector {
|
func (iv *InputVector) SetKey(key Key) *InputVector {
|
||||||
return iv.SetKeys([]Key{key})
|
return iv.SetKeys([]Key{key})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetKeys sets multiple key bits in the keys bitset
|
||||||
func (iv *InputVector) SetKeys(keys []Key) *InputVector {
|
func (iv *InputVector) SetKeys(keys []Key) *InputVector {
|
||||||
if len(keys) == 0 {
|
if len(keys) == 0 {
|
||||||
return iv
|
return iv
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
iv.KeyVector.Set(int(key), true)
|
iv.KeyVector.Set(key, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
return iv
|
return iv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetModifier sets the corresponding modifier bit in the modifier bitset
|
||||||
func (iv *InputVector) SetModifier(mod Modifier) *InputVector {
|
func (iv *InputVector) SetModifier(mod Modifier) *InputVector {
|
||||||
return iv.SetModifiers([]Modifier{mod})
|
return iv.SetModifiers([]Modifier{mod})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetModifiers sets multiple modifier bits in the modifier bitset
|
||||||
func (iv *InputVector) SetModifiers(mods []Modifier) *InputVector {
|
func (iv *InputVector) SetModifiers(mods []Modifier) *InputVector {
|
||||||
if len(mods) == 0 {
|
if len(mods) == 0 {
|
||||||
return iv
|
return iv
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range mods {
|
for _, key := range mods {
|
||||||
iv.ModifierVector.Set(int(key), true)
|
iv.ModifierVector.Set(key, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
return iv
|
return iv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetMouseButton sets the corresponding mouse button bit in the mouse button bitset
|
||||||
func (iv *InputVector) SetMouseButton(button MouseButton) *InputVector {
|
func (iv *InputVector) SetMouseButton(button MouseButton) *InputVector {
|
||||||
return iv.SetMouseButtons([]MouseButton{button})
|
return iv.SetMouseButtons([]MouseButton{button})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetMouseButtons sets multiple mouse button bits in the mouse button bitset
|
||||||
func (iv *InputVector) SetMouseButtons(buttons []MouseButton) *InputVector {
|
func (iv *InputVector) SetMouseButtons(buttons []MouseButton) *InputVector {
|
||||||
if len(buttons) == 0 {
|
if len(buttons) == 0 {
|
||||||
return iv
|
return iv
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range buttons {
|
for _, key := range buttons {
|
||||||
iv.MouseButtonVector.Set(int(key), true)
|
iv.MouseButtonVector.Set(key, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
return iv
|
return iv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Contains returns true if this input vector is a superset of the given input vector
|
||||||
func (iv *InputVector) Contains(other *InputVector) bool {
|
func (iv *InputVector) Contains(other *InputVector) bool {
|
||||||
keys := iv.KeyVector.ContainsAll(other.KeyVector)
|
keys := iv.KeyVector.ContainsAll(other.KeyVector)
|
||||||
buttons := iv.MouseButtonVector.ContainsAll(other.MouseButtonVector)
|
buttons := iv.MouseButtonVector.ContainsAll(other.MouseButtonVector)
|
||||||
@ -76,6 +88,7 @@ func (iv *InputVector) Contains(other *InputVector) bool {
|
|||||||
return keys && mods && buttons
|
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 {
|
func (iv *InputVector) Intersects(other *InputVector) bool {
|
||||||
keys := iv.KeyVector.Intersects(other.KeyVector)
|
keys := iv.KeyVector.Intersects(other.KeyVector)
|
||||||
mods := iv.ModifierVector.Intersects(other.ModifierVector)
|
mods := iv.ModifierVector.Intersects(other.ModifierVector)
|
||||||
@ -84,6 +97,7 @@ func (iv *InputVector) Intersects(other *InputVector) bool {
|
|||||||
return keys || mods || buttons
|
return keys || mods || buttons
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear sets all bits in this input vector to 0
|
||||||
func (iv *InputVector) Clear() *InputVector {
|
func (iv *InputVector) Clear() *InputVector {
|
||||||
iv.KeyVector.Clear()
|
iv.KeyVector.Clear()
|
||||||
iv.ModifierVector.Clear()
|
iv.ModifierVector.Clear()
|
||||||
|
@ -2,8 +2,10 @@ package d2input
|
|||||||
|
|
||||||
import "github.com/hajimehoshi/ebiten/v2"
|
import "github.com/hajimehoshi/ebiten/v2"
|
||||||
|
|
||||||
|
// Key represents a keyboard key
|
||||||
type Key = int
|
type Key = int
|
||||||
|
|
||||||
|
// Keys
|
||||||
const (
|
const (
|
||||||
Key0 = Key(ebiten.Key0)
|
Key0 = Key(ebiten.Key0)
|
||||||
Key1 = Key(ebiten.Key1)
|
Key1 = Key(ebiten.Key1)
|
||||||
|
@ -2,8 +2,10 @@ package d2input
|
|||||||
|
|
||||||
import "github.com/hajimehoshi/ebiten/v2"
|
import "github.com/hajimehoshi/ebiten/v2"
|
||||||
|
|
||||||
|
// Modifier represents a keyboard modifier key
|
||||||
type Modifier = int
|
type Modifier = int
|
||||||
|
|
||||||
|
// Modifiers
|
||||||
const (
|
const (
|
||||||
ModAlt = Modifier(ebiten.KeyAlt)
|
ModAlt = Modifier(ebiten.KeyAlt)
|
||||||
ModControl = Modifier(ebiten.KeyControl)
|
ModControl = Modifier(ebiten.KeyControl)
|
||||||
|
@ -2,8 +2,10 @@ package d2input
|
|||||||
|
|
||||||
import "github.com/hajimehoshi/ebiten/v2"
|
import "github.com/hajimehoshi/ebiten/v2"
|
||||||
|
|
||||||
|
// MouseButton represents a button on a mouse
|
||||||
type MouseButton = int
|
type MouseButton = int
|
||||||
|
|
||||||
|
// MouseButtons
|
||||||
const (
|
const (
|
||||||
MouseButtonLeft = MouseButton(ebiten.MouseButtonLeft)
|
MouseButtonLeft = MouseButton(ebiten.MouseButtonLeft)
|
||||||
MouseButtonRight = MouseButton(ebiten.MouseButtonRight)
|
MouseButtonRight = MouseButton(ebiten.MouseButtonRight)
|
||||||
|
Loading…
Reference in New Issue
Block a user