mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 12:32:05 +01:00
Add ui.Keys
This commit is contained in:
parent
fb4751e8cd
commit
0b9f27287b
@ -70,7 +70,7 @@ func (game *Game) Update(state ui.CanvasState) {
|
|||||||
if !game.isInitialized() {
|
if !game.isInitialized() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
game.input.Update(state.Keys)
|
game.input.Update(state.PressedKeys)
|
||||||
game.sceneManager.Update(&GameState{
|
game.sceneManager.Update(&GameState{
|
||||||
SceneManager: game.sceneManager,
|
SceneManager: game.sceneManager,
|
||||||
Input: game.input,
|
Input: game.input,
|
||||||
|
@ -22,14 +22,9 @@ func (i *Input) StateForKey(key ui.Key) int {
|
|||||||
return i.states[key]
|
return i.states[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Input) Update(keys []ui.Key) {
|
func (i *Input) Update(keys ui.Keys) {
|
||||||
pressedKeys := map[ui.Key]struct{}{}
|
|
||||||
for _, key := range keys {
|
|
||||||
pressedKeys[key] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, _ := range i.states {
|
for key, _ := range i.states {
|
||||||
if _, ok := pressedKeys[key]; !ok {
|
if !keys.Includes(key) {
|
||||||
i.states[key] = 0
|
i.states[key] = 0
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,29 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Keys map[ui.Key]struct{}
|
||||||
|
|
||||||
|
func newKeys() Keys {
|
||||||
|
return Keys(map[ui.Key]struct{}{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keys) add(key ui.Key) {
|
||||||
|
k[key] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keys) remove(key ui.Key) {
|
||||||
|
delete(k, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Keys) Includes(key ui.Key) bool {
|
||||||
|
_, ok := k[key]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
type GameWindow struct {
|
type GameWindow struct {
|
||||||
state ui.CanvasState
|
state ui.CanvasState
|
||||||
title string
|
title string
|
||||||
native *C.EbitenGameWindow
|
native *C.EbitenGameWindow
|
||||||
pressedKeys map[ui.Key]struct{}
|
|
||||||
funcs chan func(*opengl.Context)
|
funcs chan func(*opengl.Context)
|
||||||
funcsDone chan struct{}
|
funcsDone chan struct{}
|
||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
@ -44,7 +62,7 @@ func newGameWindow(width, height, scale int, title string) *GameWindow {
|
|||||||
Width: width,
|
Width: width,
|
||||||
Height: height,
|
Height: height,
|
||||||
Scale: scale,
|
Scale: scale,
|
||||||
Keys: []ui.Key{},
|
PressedKeys: newKeys(),
|
||||||
MouseX: -1,
|
MouseX: -1,
|
||||||
MouseY: -1,
|
MouseY: -1,
|
||||||
IsClosed: false,
|
IsClosed: false,
|
||||||
@ -52,7 +70,6 @@ func newGameWindow(width, height, scale int, title string) *GameWindow {
|
|||||||
return &GameWindow{
|
return &GameWindow{
|
||||||
state: state,
|
state: state,
|
||||||
title: title,
|
title: title,
|
||||||
pressedKeys: map[ui.Key]struct{}{},
|
|
||||||
funcs: make(chan func(*opengl.Context)),
|
funcs: make(chan func(*opengl.Context)),
|
||||||
funcsDone: make(chan struct{}),
|
funcsDone: make(chan struct{}),
|
||||||
closed: make(chan struct{}),
|
closed: make(chan struct{}),
|
||||||
@ -145,16 +162,10 @@ func ebiten_KeyDown(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
w := windows[nativeWindow]
|
w := windows[nativeWindow]
|
||||||
w.pressedKeys[key] = struct{}{}
|
|
||||||
|
|
||||||
keys := []ui.Key{}
|
|
||||||
for key, _ := range w.pressedKeys {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Lock()
|
w.Lock()
|
||||||
defer w.Unlock()
|
defer w.Unlock()
|
||||||
w.state.Keys = keys
|
w.state.PressedKeys.(Keys).add(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export ebiten_KeyUp
|
//export ebiten_KeyUp
|
||||||
@ -164,16 +175,10 @@ func ebiten_KeyUp(nativeWindow C.EbitenGameWindowPtr, keyCode int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
w := windows[nativeWindow]
|
w := windows[nativeWindow]
|
||||||
delete(w.pressedKeys, key)
|
|
||||||
|
|
||||||
keys := []ui.Key{}
|
|
||||||
for key, _ := range w.pressedKeys {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Lock()
|
w.Lock()
|
||||||
defer w.Unlock()
|
defer w.Unlock()
|
||||||
w.state.Keys = keys
|
w.state.PressedKeys.(Keys).remove(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export ebiten_MouseStateUpdated
|
//export ebiten_MouseStateUpdated
|
||||||
|
6
ui/ui.go
6
ui/ui.go
@ -22,11 +22,15 @@ type UI interface {
|
|||||||
Terminate()
|
Terminate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Keys interface {
|
||||||
|
Includes(key Key) bool
|
||||||
|
}
|
||||||
|
|
||||||
type CanvasState struct {
|
type CanvasState struct {
|
||||||
Width int
|
Width int
|
||||||
Height int
|
Height int
|
||||||
Scale int
|
Scale int
|
||||||
Keys []Key
|
PressedKeys Keys
|
||||||
MouseX int
|
MouseX int
|
||||||
MouseY int
|
MouseY int
|
||||||
IsClosed bool
|
IsClosed bool
|
||||||
|
Loading…
Reference in New Issue
Block a user