mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
Unify keyboard and mouse to input
This commit is contained in:
parent
225f1fa7b1
commit
d1f513a043
@ -27,8 +27,7 @@ import (
|
|||||||
type canvas struct {
|
type canvas struct {
|
||||||
window *glfw.Window
|
window *glfw.Window
|
||||||
context *opengl.GraphicsContext
|
context *opengl.GraphicsContext
|
||||||
keyboard keyboard
|
input input
|
||||||
mouse mouse
|
|
||||||
funcs chan func()
|
funcs chan func()
|
||||||
funcsDone chan struct{}
|
funcsDone chan struct{}
|
||||||
}
|
}
|
||||||
@ -87,6 +86,5 @@ func (c *canvas) use(f func()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *canvas) update() {
|
func (c *canvas) update() {
|
||||||
c.keyboard.update(c.window)
|
c.input.update(c.window)
|
||||||
c.mouse.update(c.window)
|
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,26 @@ package glfw
|
|||||||
import (
|
import (
|
||||||
glfw "github.com/go-gl/glfw3"
|
glfw "github.com/go-gl/glfw3"
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
type keyboard struct {
|
type input struct {
|
||||||
keyPressed [ebiten.KeyMax]bool
|
keyPressed [ebiten.KeyMax]bool
|
||||||
|
mouseButtonPressed [ebiten.MouseButtonMax]bool
|
||||||
|
cursorX int
|
||||||
|
cursorY int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *keyboard) IsKeyPressed(key ebiten.Key) bool {
|
func (i *input) IsKeyPressed(key ebiten.Key) bool {
|
||||||
return k.keyPressed[key]
|
return i.keyPressed[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *input) IsMouseButtonPressed(button ebiten.MouseButton) bool {
|
||||||
|
return i.mouseButtonPressed[button]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *input) CursorPosition() (x, y int) {
|
||||||
|
return i.cursorX, i.cursorY
|
||||||
}
|
}
|
||||||
|
|
||||||
var glfwKeyCodeToKey = map[glfw.Key]ebiten.Key{
|
var glfwKeyCodeToKey = map[glfw.Key]ebiten.Key{
|
||||||
@ -37,8 +49,14 @@ var glfwKeyCodeToKey = map[glfw.Key]ebiten.Key{
|
|||||||
glfw.KeyDown: ebiten.KeyDown,
|
glfw.KeyDown: ebiten.KeyDown,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *keyboard) update(window *glfw.Window) {
|
func (i *input) update(window *glfw.Window) {
|
||||||
for g, u := range glfwKeyCodeToKey {
|
for g, u := range glfwKeyCodeToKey {
|
||||||
k.keyPressed[u] = window.GetKey(g) == glfw.Press
|
i.keyPressed[u] = window.GetKey(g) == glfw.Press
|
||||||
}
|
}
|
||||||
|
for b := ebiten.MouseButtonLeft; b < ebiten.MouseButtonMax; b++ {
|
||||||
|
i.mouseButtonPressed[b] = window.GetMouseButton(glfw.MouseButton(b)) == glfw.Press
|
||||||
|
}
|
||||||
|
x, y := window.GetCursorPosition()
|
||||||
|
i.cursorX = int(math.Floor(x))
|
||||||
|
i.cursorY = int(math.Floor(y))
|
||||||
}
|
}
|
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2014 Hajime Hoshi
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package glfw
|
|
||||||
|
|
||||||
import (
|
|
||||||
glfw "github.com/go-gl/glfw3"
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
|
||||||
"math"
|
|
||||||
)
|
|
||||||
|
|
||||||
type mouse struct {
|
|
||||||
buttonPressed [ebiten.MouseButtonMax]bool
|
|
||||||
x int
|
|
||||||
y int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mouse) CursorPosition() (x, y int) {
|
|
||||||
return m.x, m.y
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mouse) IsMouseButtonPressed(button ebiten.MouseButton) bool {
|
|
||||||
return m.buttonPressed[button]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mouse) update(window *glfw.Window) {
|
|
||||||
x, y := window.GetCursorPosition()
|
|
||||||
m.x = int(math.Floor(x))
|
|
||||||
m.y = int(math.Floor(y))
|
|
||||||
for i := ebiten.MouseButtonLeft; i < ebiten.MouseButtonMax; i++ {
|
|
||||||
m.buttonPressed[i] = window.GetMouseButton(glfw.MouseButton(i)) == glfw.Press
|
|
||||||
}
|
|
||||||
}
|
|
@ -49,8 +49,7 @@ func (u *UI) Start(width, height, scale int, title string) (ebiten.Canvas, error
|
|||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
funcsDone: make(chan struct{}),
|
funcsDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
ebiten.SetKeyboard(&c.keyboard)
|
ebiten.SetInput(&c.input)
|
||||||
ebiten.SetMouse(&c.mouse)
|
|
||||||
ebiten.SetTextureFactory(c)
|
ebiten.SetTextureFactory(c)
|
||||||
|
|
||||||
c.run(width, height, scale)
|
c.run(width, height, scale)
|
||||||
|
@ -16,6 +16,18 @@ limitations under the License.
|
|||||||
|
|
||||||
package ebiten
|
package ebiten
|
||||||
|
|
||||||
|
type Key int
|
||||||
|
|
||||||
|
// TODO: Add more keys.
|
||||||
|
const (
|
||||||
|
KeyUp Key = iota
|
||||||
|
KeyDown
|
||||||
|
KeyLeft
|
||||||
|
KeyRight
|
||||||
|
KeySpace
|
||||||
|
KeyMax
|
||||||
|
)
|
||||||
|
|
||||||
type MouseButton int
|
type MouseButton int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -25,27 +37,35 @@ const (
|
|||||||
MouseButtonMax
|
MouseButtonMax
|
||||||
)
|
)
|
||||||
|
|
||||||
var currentMouse Mouse
|
type Input interface {
|
||||||
|
IsKeyPressed(key Key) bool
|
||||||
type Mouse interface {
|
|
||||||
CursorPosition() (x, y int)
|
CursorPosition() (x, y int)
|
||||||
IsMouseButtonPressed(mouseButton MouseButton) bool
|
IsMouseButtonPressed(mouseButton MouseButton) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetMouse(mouse Mouse) {
|
var currentInput Input
|
||||||
currentMouse = mouse
|
|
||||||
|
func SetInput(input Input) {
|
||||||
|
currentInput = input
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsKeyPressed(key Key) bool {
|
||||||
|
if currentInput == nil {
|
||||||
|
panic("ebiten.IsKeyPressed: currentInput is not set")
|
||||||
|
}
|
||||||
|
return currentInput.IsKeyPressed(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CursorPosition() (x, y int) {
|
func CursorPosition() (x, y int) {
|
||||||
if currentMouse == nil {
|
if currentInput == nil {
|
||||||
panic("input.CurrentPosition: currentMouse is not set")
|
panic("ebiten.CurrentPosition: currentInput is not set")
|
||||||
}
|
}
|
||||||
return currentMouse.CursorPosition()
|
return currentInput.CursorPosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsMouseButtonPressed(button MouseButton) bool {
|
func IsMouseButtonPressed(button MouseButton) bool {
|
||||||
if currentMouse == nil {
|
if currentInput == nil {
|
||||||
panic("input.IsMouseButtonPressed: currentMouse is not set")
|
panic("ebiten.IsMouseButtonPressed: currentInput is not set")
|
||||||
}
|
}
|
||||||
return currentMouse.IsMouseButtonPressed(button)
|
return currentInput.IsMouseButtonPressed(button)
|
||||||
}
|
}
|
46
keyboard.go
46
keyboard.go
@ -1,46 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2014 Hajime Hoshi
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package ebiten
|
|
||||||
|
|
||||||
type Key int
|
|
||||||
|
|
||||||
// TODO: Add more keys.
|
|
||||||
const (
|
|
||||||
KeyUp Key = iota
|
|
||||||
KeyDown
|
|
||||||
KeyLeft
|
|
||||||
KeyRight
|
|
||||||
KeySpace
|
|
||||||
KeyMax
|
|
||||||
)
|
|
||||||
|
|
||||||
var currentKeyboard Keyboard
|
|
||||||
|
|
||||||
type Keyboard interface {
|
|
||||||
IsKeyPressed(key Key) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetKeyboard(keyboard Keyboard) {
|
|
||||||
currentKeyboard = keyboard
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsKeyPressed(key Key) bool {
|
|
||||||
if currentKeyboard == nil {
|
|
||||||
panic("input.IsKeyPressed: currentKeyboard is not set")
|
|
||||||
}
|
|
||||||
return currentKeyboard.IsKeyPressed(key)
|
|
||||||
}
|
|
11
run.go
11
run.go
@ -28,8 +28,11 @@ type Game interface {
|
|||||||
Draw(context GraphicsContext) error
|
Draw(context GraphicsContext) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(u UI, game Game, width, height, scale int, title string, fps int) error {
|
// Run runs the game. Basically, this function executes ui.Start() at the start,
|
||||||
canvas, err := u.Start(width, height, scale, title)
|
// calls ui.DoEvent(), game.Update() and game.Draw() at a regular interval, and finally
|
||||||
|
// calls ui.Terminate().
|
||||||
|
func Run(ui UI, game Game, width, height, scale int, title string, fps int) error {
|
||||||
|
canvas, err := ui.Start(width, height, scale, title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -39,9 +42,9 @@ func Run(u UI, game Game, width, height, scale int, title string, fps int) error
|
|||||||
sigterm := make(chan os.Signal, 1)
|
sigterm := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
|
||||||
|
|
||||||
defer u.Terminate()
|
defer ui.Terminate()
|
||||||
for {
|
for {
|
||||||
u.DoEvents()
|
ui.DoEvents()
|
||||||
select {
|
select {
|
||||||
default:
|
default:
|
||||||
if err := canvas.Draw(game); err != nil {
|
if err := canvas.Draw(game); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user