2015-01-06 13:52:36 +01:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2017-05-11 12:09:13 +02:00
|
|
|
// +build darwin freebsd linux windows
|
2015-01-06 13:52:36 +01:00
|
|
|
// +build !js
|
2016-05-18 20:17:50 +02:00
|
|
|
// +build !android
|
2016-06-15 17:49:22 +02:00
|
|
|
// +build !ios
|
2015-01-06 13:52:36 +01:00
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
package glfw
|
2015-01-06 13:52:36 +01:00
|
|
|
|
|
|
|
import (
|
2017-04-11 04:56:05 +02:00
|
|
|
"sync"
|
2017-08-14 21:11:51 +02:00
|
|
|
"unicode"
|
2017-04-11 04:56:05 +02:00
|
|
|
|
2019-03-30 15:19:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2018-12-29 11:20:41 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/glfw"
|
2015-01-06 13:52:36 +01:00
|
|
|
)
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
type gamePad struct {
|
|
|
|
valid bool
|
|
|
|
axisNum int
|
|
|
|
axes [16]float64
|
|
|
|
buttonNum int
|
|
|
|
buttonPressed [256]bool
|
|
|
|
}
|
|
|
|
|
2017-04-28 16:45:01 +02:00
|
|
|
type Input struct {
|
2019-03-31 13:20:52 +02:00
|
|
|
keyPressed map[glfw.Key]bool
|
|
|
|
mouseButtonPressed map[glfw.MouseButton]bool
|
|
|
|
onceCallback sync.Once
|
|
|
|
scrollX float64
|
|
|
|
scrollY float64
|
|
|
|
cursorX int
|
|
|
|
cursorY int
|
|
|
|
gamepads [16]gamePad
|
|
|
|
touches map[int]pos // This is not updated until GLFW 3.3 is available (#417)
|
|
|
|
runeBuffer []rune
|
2019-04-07 12:27:30 +02:00
|
|
|
ui *UserInterface
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
type pos struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) CursorPosition() (x, y int) {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
cx, cy := i.cursorX, i.cursorY
|
|
|
|
i.ui.m.RUnlock()
|
|
|
|
return i.ui.adjustPosition(cx, cy)
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) GamepadIDs() []int {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2019-04-07 11:55:56 +02:00
|
|
|
if len(i.gamepads) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r := []int{}
|
|
|
|
for id, g := range i.gamepads {
|
|
|
|
if g.valid {
|
|
|
|
r = append(r, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) GamepadAxisNum(id int) int {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2019-04-07 11:55:56 +02:00
|
|
|
if len(i.gamepads) <= id {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i.gamepads[id].axisNum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) GamepadAxis(id int, axis int) float64 {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2019-04-07 11:55:56 +02:00
|
|
|
if len(i.gamepads) <= id {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i.gamepads[id].axes[axis]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) GamepadButtonNum(id int) int {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2019-04-07 11:55:56 +02:00
|
|
|
if len(i.gamepads) <= id {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return i.gamepads[id].buttonNum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) IsGamepadButtonPressed(id int, button driver.GamepadButton) bool {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2019-04-07 11:55:56 +02:00
|
|
|
if len(i.gamepads) <= id {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return i.gamepads[id].buttonPressed[button]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) TouchIDs() []int {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2019-04-07 11:55:56 +02:00
|
|
|
|
|
|
|
if len(i.touches) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ids []int
|
|
|
|
for id := range i.touches {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) TouchPosition(id int) (x, y int) {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
found := false
|
|
|
|
var p pos
|
2019-04-07 11:55:56 +02:00
|
|
|
for tid, pos := range i.touches {
|
|
|
|
if id == tid {
|
2019-08-18 18:01:43 +02:00
|
|
|
p = pos
|
|
|
|
found = true
|
|
|
|
break
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RUnlock()
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
return i.ui.adjustPosition(p.X, p.Y)
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 21:11:51 +02:00
|
|
|
func (i *Input) RuneBuffer() []rune {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2017-08-14 21:11:51 +02:00
|
|
|
return i.runeBuffer
|
|
|
|
}
|
|
|
|
|
2019-03-31 11:47:06 +02:00
|
|
|
func (i *Input) ResetForFrame() {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2018-04-01 16:20:45 +02:00
|
|
|
i.runeBuffer = i.runeBuffer[:0]
|
2018-06-17 17:38:30 +02:00
|
|
|
i.scrollX, i.scrollY = 0, 0
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:28:07 +01:00
|
|
|
func (i *Input) IsKeyPressed(key driver.Key) bool {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.keyPressed == nil {
|
|
|
|
i.keyPressed = map[glfw.Key]bool{}
|
|
|
|
}
|
|
|
|
for gk, k := range glfwKeyCodeToKey {
|
|
|
|
if k != key {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i.keyPressed[gk] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:19:52 +01:00
|
|
|
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
|
|
|
}
|
|
|
|
for gb, b := range glfwMouseButtonToMouseButton {
|
|
|
|
if b != button {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i.mouseButtonPressed[gb] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:45 +02:00
|
|
|
func (i *Input) Wheel() (xoff, yoff float64) {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2018-06-17 17:38:30 +02:00
|
|
|
return i.scrollX, i.scrollY
|
|
|
|
}
|
|
|
|
|
2019-03-30 15:19:52 +01:00
|
|
|
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]driver.MouseButton{
|
|
|
|
glfw.MouseButtonLeft: driver.MouseButtonLeft,
|
|
|
|
glfw.MouseButtonRight: driver.MouseButtonRight,
|
|
|
|
glfw.MouseButtonMiddle: driver.MouseButtonMiddle,
|
2015-01-06 20:04:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 13:20:52 +02:00
|
|
|
func (i *Input) appendRuneBuffer(char rune) {
|
|
|
|
if !unicode.IsPrint(char) {
|
|
|
|
return
|
|
|
|
}
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.Lock()
|
2019-03-31 13:20:52 +02:00
|
|
|
i.runeBuffer = append(i.runeBuffer, char)
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.Unlock()
|
2019-03-31 13:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) setWheel(xoff, yoff float64) {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.Lock()
|
2019-03-31 13:20:52 +02:00
|
|
|
i.scrollX = xoff
|
|
|
|
i.scrollY = yoff
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.Unlock()
|
2019-03-31 13:20:52 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 11:55:56 +02:00
|
|
|
func (i *Input) update(window *glfw.Window, scale float64) {
|
2019-08-18 18:01:43 +02:00
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
2019-03-31 12:51:53 +02:00
|
|
|
|
2019-03-31 13:20:52 +02:00
|
|
|
i.onceCallback.Do(func() {
|
2017-08-14 21:11:51 +02:00
|
|
|
window.SetCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
|
2019-03-31 13:20:52 +02:00
|
|
|
i.appendRuneBuffer(char)
|
2017-08-14 21:11:51 +02:00
|
|
|
})
|
2018-06-17 17:38:30 +02:00
|
|
|
window.SetScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) {
|
2019-03-31 13:20:52 +02:00
|
|
|
i.setWheel(xoff, yoff)
|
2018-06-17 17:38:30 +02:00
|
|
|
})
|
2019-03-31 13:20:52 +02:00
|
|
|
})
|
2017-04-11 04:56:05 +02:00
|
|
|
if i.keyPressed == nil {
|
|
|
|
i.keyPressed = map[glfw.Key]bool{}
|
|
|
|
}
|
|
|
|
for gk := range glfwKeyCodeToKey {
|
|
|
|
i.keyPressed[gk] = window.GetKey(gk) == glfw.Press
|
|
|
|
}
|
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
2015-01-06 13:52:36 +01:00
|
|
|
}
|
2017-04-11 04:56:05 +02:00
|
|
|
for gb := range glfwMouseButtonToMouseButton {
|
|
|
|
i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press
|
2015-01-06 13:52:36 +01:00
|
|
|
}
|
2015-06-20 11:52:17 +02:00
|
|
|
x, y := window.GetCursorPos()
|
2016-06-18 19:59:17 +02:00
|
|
|
i.cursorX = int(x / scale)
|
|
|
|
i.cursorY = int(y / scale)
|
2015-01-12 06:36:13 +01:00
|
|
|
for id := glfw.Joystick(0); id < glfw.Joystick(len(i.gamepads)); id++ {
|
2017-10-25 19:59:06 +02:00
|
|
|
i.gamepads[id].valid = false
|
2015-01-12 06:36:13 +01:00
|
|
|
if !glfw.JoystickPresent(id) {
|
2015-01-11 17:54:18 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-10-25 19:59:06 +02:00
|
|
|
i.gamepads[id].valid = true
|
|
|
|
|
2015-06-20 11:52:17 +02:00
|
|
|
axes32 := glfw.GetJoystickAxes(id)
|
2015-01-12 06:36:13 +01:00
|
|
|
i.gamepads[id].axisNum = len(axes32)
|
|
|
|
for a := 0; a < len(i.gamepads[id].axes); a++ {
|
2015-01-11 17:54:18 +01:00
|
|
|
if len(axes32) <= a {
|
2015-01-12 06:36:13 +01:00
|
|
|
i.gamepads[id].axes[a] = 0
|
2015-01-11 17:54:18 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-01-12 06:36:13 +01:00
|
|
|
i.gamepads[id].axes[a] = float64(axes32[a])
|
2015-01-11 17:54:18 +01:00
|
|
|
}
|
2015-06-20 11:52:17 +02:00
|
|
|
buttons := glfw.GetJoystickButtons(id)
|
2015-01-12 06:36:13 +01:00
|
|
|
i.gamepads[id].buttonNum = len(buttons)
|
|
|
|
for b := 0; b < len(i.gamepads[id].buttonPressed); b++ {
|
2015-01-11 17:54:18 +01:00
|
|
|
if len(buttons) <= b {
|
2015-01-12 06:36:13 +01:00
|
|
|
i.gamepads[id].buttonPressed[b] = false
|
2015-01-11 17:54:18 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-01-12 06:36:13 +01:00
|
|
|
i.gamepads[id].buttonPressed[b] = glfw.Action(buttons[b]) == glfw.Press
|
2015-01-11 17:54:18 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-06 13:52:36 +01:00
|
|
|
}
|