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.
|
|
|
|
|
2022-02-08 07:42:29 +01:00
|
|
|
//go:build !android && !ios && !js && !ebitencbackend
|
|
|
|
// +build !android,!ios,!js,!ebitencbackend
|
2015-01-06 13:52:36 +01:00
|
|
|
|
2022-02-06 08:08:22 +01:00
|
|
|
package ui
|
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
|
|
|
|
2022-02-05 13:52:06 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
2015-01-06 13:52:36 +01: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
|
2022-02-06 10:30:31 +01:00
|
|
|
touches map[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
|
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
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
func (i *Input) AppendTouchIDs(touchIDs []TouchID) []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
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
func (i *Input) TouchPosition(id 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
|
|
|
}
|
|
|
|
|
2022-02-06 10:13:10 +01:00
|
|
|
func (i *Input) IsKeyPressed(key 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{}
|
|
|
|
}
|
2022-02-06 10:13:10 +01:00
|
|
|
gk, ok := uiKeyToGLFWKey[key]
|
2020-11-18 11:33:20 +01:00
|
|
|
return ok && i.keyPressed[gk]
|
2017-04-11 04:56:05 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
func (i *Input) IsMouseButtonPressed(button 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
|
|
|
}
|
|
|
|
|
2022-02-06 10:30:31 +01:00
|
|
|
var glfwMouseButtonToMouseButton = map[glfw.MouseButton]MouseButton{
|
|
|
|
glfw.MouseButtonLeft: MouseButtonLeft,
|
|
|
|
glfw.MouseButtonRight: MouseButtonRight,
|
|
|
|
glfw.MouseButtonMiddle: 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-02-06 09:43:52 +01:00
|
|
|
func (i *Input) update(window *glfw.Window, context Context) 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{}
|
|
|
|
}
|
2022-02-06 10:13:10 +01:00
|
|
|
for gk := range glfwKeyToUIKey {
|
2020-10-16 22:16:02 +02:00
|
|
|
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-02-05 13:52:06 +01:00
|
|
|
gamepad.Update()
|
|
|
|
return nil
|
2021-08-23 14:44:49 +02:00
|
|
|
}
|