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.
|
|
|
|
|
2021-10-24 07:49:21 +02:00
|
|
|
//go:build !android && !js && !ios
|
|
|
|
// +build !android,!js,!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 (
|
2021-03-27 10:40:39 +01:00
|
|
|
"math"
|
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
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2015-01-06 13:52:36 +01:00
|
|
|
)
|
|
|
|
|
2021-07-19 15:25:43 +02:00
|
|
|
type gamepad struct {
|
2019-04-07 11:55:56 +02:00
|
|
|
valid bool
|
2020-01-09 14:11:32 +01:00
|
|
|
guid string
|
|
|
|
name string
|
2019-04-07 11:55:56 +02:00
|
|
|
axisNum int
|
|
|
|
axes [16]float64
|
|
|
|
buttonNum int
|
|
|
|
buttonPressed [256]bool
|
2021-09-11 15:46:05 +02:00
|
|
|
hatsNum int
|
|
|
|
hats [16]int
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
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
|
2021-07-19 15:25:43 +02:00
|
|
|
gamepads [16]gamepad
|
2020-10-09 20:36:14 +02:00
|
|
|
touches map[driver.TouchID]pos // TODO: Implement this (#417)
|
2019-03-31 13:20:52 +02:00
|
|
|
runeBuffer []rune
|
2019-04-07 12:27:30 +02:00
|
|
|
ui *UserInterface
|
2022-01-10 14:10:29 +01:00
|
|
|
|
|
|
|
nativeGamepads
|
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-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
return i.cursorX, i.cursorY
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 18:15:47 +02:00
|
|
|
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
2019-04-07 11:55:56 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
for id := range i.touches {
|
2021-07-09 18:15:47 +02:00
|
|
|
touchIDs = append(touchIDs, id)
|
2020-11-18 11:33:20 +01:00
|
|
|
}
|
2021-07-09 18:15:47 +02:00
|
|
|
return touchIDs
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:36:14 +02:00
|
|
|
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
for tid, pos := range i.touches {
|
|
|
|
if id == tid {
|
|
|
|
return pos.X, pos.Y
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
2019-08-18 18:01:43 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
return 0, 0
|
2019-04-07 11:55:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-09 18:15:47 +02:00
|
|
|
func (i *Input) AppendInputChars(runes []rune) []rune {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
2021-07-09 18:15:47 +02:00
|
|
|
return append(runes, i.runeBuffer...)
|
2017-08-14 21:11:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:06:42 +02:00
|
|
|
func (i *Input) resetForFrame() {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
i.runeBuffer = i.runeBuffer[:0]
|
|
|
|
i.scrollX, i.scrollY = 0, 0
|
2018-06-17 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 15:28:07 +01:00
|
|
|
func (i *Input) IsKeyPressed(key driver.Key) bool {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return false
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
if i.keyPressed == nil {
|
|
|
|
i.keyPressed = map[glfw.Key]bool{}
|
|
|
|
}
|
|
|
|
gk, ok := driverKeyToGLFWKey[key]
|
|
|
|
return ok && i.keyPressed[gk]
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2019-03-30 15:19:52 +01:00
|
|
|
func (i *Input) IsMouseButtonPressed(button driver.MouseButton) bool {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return false
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
|
|
|
}
|
|
|
|
for gb, b := range glfwMouseButtonToMouseButton {
|
|
|
|
if b != button {
|
|
|
|
continue
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
if i.mouseButtonPressed[gb] {
|
|
|
|
return true
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
}
|
|
|
|
return false
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:45 +02:00
|
|
|
func (i *Input) Wheel() (xoff, yoff float64) {
|
2019-12-11 19:20:20 +01:00
|
|
|
if !i.ui.isRunning() {
|
|
|
|
return 0, 0
|
|
|
|
}
|
2020-11-18 11:33:20 +01:00
|
|
|
|
|
|
|
i.ui.m.RLock()
|
|
|
|
defer i.ui.m.RUnlock()
|
|
|
|
return i.scrollX, i.scrollY
|
2018-06-17 17:38:30 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
// update must be called from the main thread.
|
2022-01-22 20:20:54 +01:00
|
|
|
func (i *Input) update(window *glfw.Window, context driver.UIContext) error {
|
2020-11-18 11:33:20 +01:00
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
i.onceCallback.Do(func() {
|
2021-06-13 14:43:23 +02:00
|
|
|
window.SetCharModsCallback(glfw.ToCharModsCallback(func(w *glfw.Window, char rune, mods glfw.ModifierKey) {
|
2020-11-18 11:33:20 +01:00
|
|
|
// As this function is called from GLFW callbacks, the current thread is main.
|
|
|
|
if !unicode.IsPrint(char) {
|
|
|
|
return
|
|
|
|
}
|
2020-11-18 12:43:29 +01:00
|
|
|
|
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
2020-11-18 11:33:20 +01:00
|
|
|
i.runeBuffer = append(i.runeBuffer, char)
|
2021-06-13 14:43:23 +02:00
|
|
|
}))
|
|
|
|
window.SetScrollCallback(glfw.ToScrollCallback(func(w *glfw.Window, xoff float64, yoff float64) {
|
2020-11-18 11:33:20 +01:00
|
|
|
// As this function is called from GLFW callbacks, the current thread is main.
|
2020-11-18 12:43:29 +01:00
|
|
|
i.ui.m.Lock()
|
|
|
|
defer i.ui.m.Unlock()
|
2020-11-18 11:33:20 +01:00
|
|
|
i.scrollX = xoff
|
|
|
|
i.scrollY = yoff
|
2021-06-13 14:43:23 +02:00
|
|
|
}))
|
2019-12-09 18:37:10 +01:00
|
|
|
})
|
2020-10-16 22:16:02 +02:00
|
|
|
if i.keyPressed == nil {
|
|
|
|
i.keyPressed = map[glfw.Key]bool{}
|
|
|
|
}
|
|
|
|
for gk := range glfwKeyToDriverKey {
|
|
|
|
i.keyPressed[gk] = window.GetKey(gk) == glfw.Press
|
|
|
|
}
|
|
|
|
if i.mouseButtonPressed == nil {
|
|
|
|
i.mouseButtonPressed = map[glfw.MouseButton]bool{}
|
|
|
|
}
|
|
|
|
for gb := range glfwMouseButtonToMouseButton {
|
|
|
|
i.mouseButtonPressed[gb] = window.GetMouseButton(gb) == glfw.Press
|
|
|
|
}
|
|
|
|
cx, cy := window.GetCursorPos()
|
|
|
|
// TODO: This is tricky. Rename the function?
|
2021-10-09 09:49:47 +02:00
|
|
|
m := i.ui.currentMonitor()
|
2021-11-12 14:29:03 +01:00
|
|
|
s := i.ui.deviceScaleFactor(m)
|
2021-10-10 08:38:13 +02:00
|
|
|
cx = i.ui.dipFromGLFWPixel(cx, m)
|
|
|
|
cy = i.ui.dipFromGLFWPixel(cy, m)
|
2021-11-12 14:29:03 +01:00
|
|
|
cx, cy = context.AdjustPosition(cx, cy, s)
|
2021-03-27 10:40:39 +01:00
|
|
|
|
|
|
|
// AdjustPosition can return NaN at the initialization.
|
|
|
|
if !math.IsNaN(cx) && !math.IsNaN(cy) {
|
|
|
|
i.cursorX, i.cursorY = int(cx), int(cy)
|
|
|
|
}
|
2020-10-16 22:16:02 +02:00
|
|
|
|
2022-01-22 20:20:54 +01:00
|
|
|
return i.updateGamepads()
|
2021-08-23 14:44:49 +02:00
|
|
|
}
|