initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
21
core/input/SCsub
Normal file
21
core/input/SCsub
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/env python
|
||||
from misc.utility.scons_hints import *
|
||||
|
||||
Import("env")
|
||||
|
||||
import input_builders
|
||||
|
||||
# Order matters here. Higher index controller database files write on top of lower index database files.
|
||||
controller_databases = [
|
||||
"gamecontrollerdb.txt",
|
||||
"godotcontrollerdb.txt",
|
||||
]
|
||||
|
||||
gensource = env.CommandNoCache(
|
||||
"default_controller_mappings.gen.cpp",
|
||||
controller_databases,
|
||||
env.Run(input_builders.make_default_controller_mappings),
|
||||
)
|
||||
|
||||
env.add_source_files(env.core_sources, "*.cpp")
|
||||
env.add_source_files(env.core_sources, gensource)
|
36
core/input/default_controller_mappings.h
Normal file
36
core/input/default_controller_mappings.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/**************************************************************************/
|
||||
/* default_controller_mappings.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
class DefaultControllerMappings {
|
||||
public:
|
||||
static const char *mappings[];
|
||||
};
|
2166
core/input/gamecontrollerdb.txt
Normal file
2166
core/input/gamecontrollerdb.txt
Normal file
File diff suppressed because it is too large
Load Diff
47
core/input/godotcontrollerdb.txt
Normal file
47
core/input/godotcontrollerdb.txt
Normal file
@@ -0,0 +1,47 @@
|
||||
# Game Controller DB for Godot in SDL 2.0.16 format
|
||||
# Source: https://github.com/godotengine/godot
|
||||
|
||||
# Windows
|
||||
__XINPUT_DEVICE__,XInput Gamepad,a:b12,b:b13,x:b14,y:b15,start:b4,guide:b10,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpdown:b1,dpleft:b2,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Windows,
|
||||
|
||||
# Android
|
||||
Default Android Gamepad,Default Controller,leftx:a0,lefty:a1,dpdown:h0.4,rightstick:b8,rightshoulder:b10,rightx:a2,start:b6,righty:a3,dpleft:h0.8,lefttrigger:a4,x:b2,dpup:h0.1,back:b4,leftstick:b7,leftshoulder:b9,y:b3,a:b0,dpright:h0.2,righttrigger:a5,b:b1,platform:Android,
|
||||
58626f7820576972656c65737320436f,Xbox Series X Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000005e04000091020000ff073f00,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000005e04000091020000ff073f80,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000005e040000e00200000ffe3f00,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000005e040000e00200000ffe3f80,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000005e040000e0020000ffff3f00,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000005e040000e0020000ffff3f80,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
050000005e040000fd020000ffff3f00,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
33356661323266333733373865656366,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
34356136633366613530316338376136,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
35623965373264386238353433656138,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
36616131643361333337396261666433,Xbox One Controller,a:b0,b:b1,back:b4,misc1:b15,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:+a4,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:+a5,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android,
|
||||
|
||||
# Web
|
||||
standard,Standard Gamepad Mapping,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:+a4,righttrigger:+a5,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b8,start:b9,leftstick:b10,rightstick:b11,dpup:b12,dpdown:b13,dpleft:b14,dpright:b15,guide:b16,leftstick:b10,rightstick:b11,platform:Web,
|
||||
Linux24c6581a,PowerA Xbox One Cabled,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web,
|
||||
Linux0e6f0301,Logic 3 Controller (xbox compatible),a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web,
|
||||
Linux045e028e,Microsoft X-Box 360 pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web,
|
||||
Linux045e02d1,Microsoft X-Box One pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web,
|
||||
Linux045e02ea,Microsoft X-Box One S pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web,
|
||||
Linux045e0b12,Microsoft X-Box Series X pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web,
|
||||
Linux044fb315,Thrustmaster dual analog 3.2,a:b0,b:b2,y:b3,x:b1,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b6,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b5,righttrigger:b7,platform:Web,
|
||||
Linux0e8f0003,PS3 Controller,a:b2,b:b1,back:b8,dpdown:+a5,dpleft:-a4,dpright:+a4,dpup:-a5,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Web,
|
||||
MacOSX24c6581a,PowerA Xbox One Cabled,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web
|
||||
MacOSX045e028e,Xbox 360 Wired Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web
|
||||
MacOSX045e02d1,Xbox One Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web
|
||||
MacOSX045e02ea,Xbox One S Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web
|
||||
MacOSX045e0b12,Xbox Series X Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web
|
||||
Linux15320a14,Razer Wolverine Ultimate,a:b0,b:b1,y:b3,x:b2,start:b7,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web
|
||||
Linux05832060,iBuffalo BSGP801,a:b1,b:b0,y:b2,x:b3,start:b7,back:b6,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web
|
||||
MacOSX05832060,iBuffalo BSGP801,a:b1,b:b0,y:b2,x:b3,start:b7,back:b6,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web
|
||||
Linux0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web
|
||||
Windows0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web
|
||||
MacOSX0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a4,dpleft:-a3,dpdown:+a4,dpright:+a3,platform:Web
|
||||
Linux046dc216,046d-c216-Logitech Logitech Dual Action,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,platform:Web
|
||||
Linux20d6a713,Bensussen Deutsch & Associates Inc.(BDA) NSW Wired controller,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,platform:Web
|
||||
Linux054c05c4,Sony Computer Entertainment Wireless Controller,a:b0,b:b1,y:b2,x:b3,start:b9,back:b8,leftstick:b11,rightstick:b12,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web
|
||||
Linux18d19400,18d1-9400-Google LLC Stadia Controller rev. A,a:b0,b:b1,y:b3,x:b2,start:b7,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a5,righttrigger:a4,platform:Web
|
||||
Linux054c0268,054c-0268-Sony PLAYSTATION(R)3 Controller,a:b0,b:b1,y:b2,x:b3,start:b9,guide:b10,back:b8,leftstick:b11,rightstick:b12,leftshoulder:b4,rightshoulder:b5,dpup:b13,dpleft:b15,dpdown:b14,dpright:b16,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web
|
41
core/input/input.compat.inc
Normal file
41
core/input/input.compat.inc
Normal file
@@ -0,0 +1,41 @@
|
||||
/**************************************************************************/
|
||||
/* input.compat.inc */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
|
||||
void Input::_vibrate_handheld_bind_compat_91143(int p_duration_ms) {
|
||||
vibrate_handheld(p_duration_ms, -1.0);
|
||||
}
|
||||
|
||||
void Input::_bind_compatibility_methods() {
|
||||
ClassDB::bind_compatibility_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::_vibrate_handheld_bind_compat_91143, DEFVAL(500));
|
||||
}
|
||||
|
||||
#endif // DISABLE_DEPRECATED
|
1976
core/input/input.cpp
Normal file
1976
core/input/input.cpp
Normal file
File diff suppressed because it is too large
Load Diff
406
core/input/input.h
Normal file
406
core/input/input.h
Normal file
@@ -0,0 +1,406 @@
|
||||
/**************************************************************************/
|
||||
/* input.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/input/input_event.h"
|
||||
#include "core/object/object.h"
|
||||
#include "core/os/keyboard.h"
|
||||
#include "core/os/thread_safe.h"
|
||||
#include "core/templates/rb_set.h"
|
||||
#include "core/variant/typed_array.h"
|
||||
|
||||
class Input : public Object {
|
||||
GDCLASS(Input, Object);
|
||||
_THREAD_SAFE_CLASS_
|
||||
|
||||
static inline Input *singleton = nullptr;
|
||||
|
||||
static constexpr uint64_t MAX_EVENT = 32;
|
||||
|
||||
public:
|
||||
// Keep synced with "DisplayServer::MouseMode" enum.
|
||||
enum MouseMode {
|
||||
MOUSE_MODE_VISIBLE,
|
||||
MOUSE_MODE_HIDDEN,
|
||||
MOUSE_MODE_CAPTURED,
|
||||
MOUSE_MODE_CONFINED,
|
||||
MOUSE_MODE_CONFINED_HIDDEN,
|
||||
MOUSE_MODE_MAX,
|
||||
};
|
||||
|
||||
#undef CursorShape
|
||||
enum CursorShape {
|
||||
CURSOR_ARROW,
|
||||
CURSOR_IBEAM,
|
||||
CURSOR_POINTING_HAND,
|
||||
CURSOR_CROSS,
|
||||
CURSOR_WAIT,
|
||||
CURSOR_BUSY,
|
||||
CURSOR_DRAG,
|
||||
CURSOR_CAN_DROP,
|
||||
CURSOR_FORBIDDEN,
|
||||
CURSOR_VSIZE,
|
||||
CURSOR_HSIZE,
|
||||
CURSOR_BDIAGSIZE,
|
||||
CURSOR_FDIAGSIZE,
|
||||
CURSOR_MOVE,
|
||||
CURSOR_VSPLIT,
|
||||
CURSOR_HSPLIT,
|
||||
CURSOR_HELP,
|
||||
CURSOR_MAX
|
||||
};
|
||||
|
||||
static constexpr int32_t JOYPADS_MAX = 16;
|
||||
|
||||
typedef void (*EventDispatchFunc)(const Ref<InputEvent> &p_event);
|
||||
|
||||
private:
|
||||
BitField<MouseButtonMask> mouse_button_mask = MouseButtonMask::NONE;
|
||||
|
||||
RBSet<Key> key_label_pressed;
|
||||
RBSet<Key> physical_keys_pressed;
|
||||
RBSet<Key> keys_pressed;
|
||||
RBSet<JoyButton> joy_buttons_pressed;
|
||||
RBMap<JoyAxis, float> _joy_axis;
|
||||
//RBMap<StringName,int> custom_action_press;
|
||||
bool gravity_enabled = false;
|
||||
Vector3 gravity;
|
||||
bool accelerometer_enabled = false;
|
||||
Vector3 accelerometer;
|
||||
bool magnetometer_enabled = false;
|
||||
Vector3 magnetometer;
|
||||
bool gyroscope_enabled = false;
|
||||
Vector3 gyroscope;
|
||||
Vector2 mouse_pos;
|
||||
int64_t mouse_window = 0;
|
||||
bool legacy_just_pressed_behavior = false;
|
||||
bool disable_input = false;
|
||||
|
||||
struct ActionState {
|
||||
uint64_t pressed_physics_frame = UINT64_MAX;
|
||||
uint64_t pressed_process_frame = UINT64_MAX;
|
||||
uint64_t released_physics_frame = UINT64_MAX;
|
||||
uint64_t released_process_frame = UINT64_MAX;
|
||||
ObjectID pressed_event_id;
|
||||
ObjectID released_event_id;
|
||||
bool exact = true;
|
||||
|
||||
struct DeviceState {
|
||||
bool pressed[MAX_EVENT] = { false };
|
||||
float strength[MAX_EVENT] = { 0.0 };
|
||||
float raw_strength[MAX_EVENT] = { 0.0 };
|
||||
};
|
||||
bool api_pressed = false;
|
||||
float api_strength = 0.0;
|
||||
HashMap<int, DeviceState> device_states;
|
||||
|
||||
// Cache.
|
||||
struct ActionStateCache {
|
||||
bool pressed = false;
|
||||
float strength = false;
|
||||
float raw_strength = false;
|
||||
} cache;
|
||||
};
|
||||
|
||||
HashMap<StringName, ActionState> action_states;
|
||||
|
||||
bool emulate_touch_from_mouse = false;
|
||||
bool emulate_mouse_from_touch = false;
|
||||
bool agile_input_event_flushing = false;
|
||||
bool use_accumulated_input = true;
|
||||
|
||||
int mouse_from_touch_index = -1;
|
||||
|
||||
struct VibrationInfo {
|
||||
float weak_magnitude;
|
||||
float strong_magnitude;
|
||||
float duration; // Duration in seconds
|
||||
uint64_t timestamp;
|
||||
};
|
||||
|
||||
HashMap<int, VibrationInfo> joy_vibration;
|
||||
|
||||
struct VelocityTrack {
|
||||
uint64_t last_tick = 0;
|
||||
Vector2 velocity;
|
||||
Vector2 screen_velocity;
|
||||
Vector2 accum;
|
||||
Vector2 screen_accum;
|
||||
float accum_t = 0.0f;
|
||||
float min_ref_frame;
|
||||
float max_ref_frame;
|
||||
|
||||
void update(const Vector2 &p_delta_p, const Vector2 &p_screen_delta_p);
|
||||
void reset();
|
||||
VelocityTrack();
|
||||
};
|
||||
|
||||
struct Joypad {
|
||||
StringName name;
|
||||
StringName uid;
|
||||
bool connected = false;
|
||||
bool last_buttons[(size_t)JoyButton::MAX] = { false };
|
||||
float last_axis[(size_t)JoyAxis::MAX] = { 0.0f };
|
||||
HatMask last_hat = HatMask::CENTER;
|
||||
int mapping = -1;
|
||||
int hat_current = 0;
|
||||
Dictionary info;
|
||||
};
|
||||
|
||||
VelocityTrack mouse_velocity_track;
|
||||
HashMap<int, VelocityTrack> touch_velocity_track;
|
||||
HashMap<int, Joypad> joy_names;
|
||||
|
||||
HashSet<uint32_t> ignored_device_ids;
|
||||
|
||||
int fallback_mapping = -1; // Index of the guid in map_db.
|
||||
|
||||
CursorShape default_shape = CURSOR_ARROW;
|
||||
|
||||
enum JoyType {
|
||||
TYPE_BUTTON,
|
||||
TYPE_AXIS,
|
||||
TYPE_HAT,
|
||||
TYPE_MAX,
|
||||
};
|
||||
|
||||
enum JoyAxisRange {
|
||||
NEGATIVE_HALF_AXIS = -1,
|
||||
FULL_AXIS = 0,
|
||||
POSITIVE_HALF_AXIS = 1
|
||||
};
|
||||
|
||||
struct JoyEvent {
|
||||
int type = TYPE_MAX;
|
||||
int index = -1; // Can be either JoyAxis or JoyButton.
|
||||
float value = 0.f;
|
||||
};
|
||||
|
||||
struct JoyBinding {
|
||||
JoyType inputType;
|
||||
union {
|
||||
JoyButton button;
|
||||
|
||||
struct {
|
||||
JoyAxis axis;
|
||||
JoyAxisRange range;
|
||||
bool invert;
|
||||
} axis;
|
||||
|
||||
struct {
|
||||
HatDir hat;
|
||||
HatMask hat_mask;
|
||||
} hat;
|
||||
|
||||
} input;
|
||||
|
||||
JoyType outputType;
|
||||
union {
|
||||
JoyButton button;
|
||||
|
||||
struct {
|
||||
JoyAxis axis;
|
||||
JoyAxisRange range;
|
||||
} axis;
|
||||
|
||||
} output;
|
||||
};
|
||||
|
||||
struct JoyDeviceMapping {
|
||||
String uid;
|
||||
String name;
|
||||
Vector<JoyBinding> bindings;
|
||||
};
|
||||
|
||||
Vector<JoyDeviceMapping> map_db;
|
||||
|
||||
void _set_joypad_mapping(Joypad &p_js, int p_map_index);
|
||||
|
||||
JoyEvent _get_mapped_button_event(const JoyDeviceMapping &mapping, JoyButton p_button);
|
||||
JoyEvent _get_mapped_axis_event(const JoyDeviceMapping &mapping, JoyAxis p_axis, float p_value, JoyAxisRange &r_range);
|
||||
void _get_mapped_hat_events(const JoyDeviceMapping &mapping, HatDir p_hat, JoyEvent r_events[(size_t)HatDir::MAX]);
|
||||
JoyButton _get_output_button(const String &output);
|
||||
JoyAxis _get_output_axis(const String &output);
|
||||
void _button_event(int p_device, JoyButton p_index, bool p_pressed);
|
||||
void _axis_event(int p_device, JoyAxis p_axis, float p_value);
|
||||
void _update_action_cache(const StringName &p_action_name, ActionState &r_action_state);
|
||||
|
||||
void _parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_emulated);
|
||||
|
||||
List<Ref<InputEvent>> buffered_events;
|
||||
#ifdef DEBUG_ENABLED
|
||||
HashSet<Ref<InputEvent>> frame_parsed_events;
|
||||
uint64_t last_parsed_frame = UINT64_MAX;
|
||||
#endif
|
||||
|
||||
friend class DisplayServer;
|
||||
|
||||
static void (*set_mouse_mode_func)(MouseMode);
|
||||
static MouseMode (*get_mouse_mode_func)();
|
||||
static void (*set_mouse_mode_override_func)(MouseMode);
|
||||
static MouseMode (*get_mouse_mode_override_func)();
|
||||
static void (*set_mouse_mode_override_enabled_func)(bool);
|
||||
static bool (*is_mouse_mode_override_enabled_func)();
|
||||
static void (*warp_mouse_func)(const Vector2 &p_position);
|
||||
|
||||
static CursorShape (*get_current_cursor_shape_func)();
|
||||
static void (*set_custom_mouse_cursor_func)(const Ref<Resource> &, CursorShape, const Vector2 &);
|
||||
|
||||
EventDispatchFunc event_dispatch_function = nullptr;
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
void _vibrate_handheld_bind_compat_91143(int p_duration_ms = 500);
|
||||
static void _bind_compatibility_methods();
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_mouse_mode(MouseMode p_mode);
|
||||
MouseMode get_mouse_mode() const;
|
||||
void set_mouse_mode_override(MouseMode p_mode);
|
||||
MouseMode get_mouse_mode_override() const;
|
||||
void set_mouse_mode_override_enabled(bool p_override_enabled);
|
||||
bool is_mouse_mode_override_enabled();
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
|
||||
#endif
|
||||
|
||||
static Input *get_singleton();
|
||||
|
||||
bool is_anything_pressed() const;
|
||||
bool is_anything_pressed_except_mouse() const;
|
||||
bool is_key_pressed(Key p_keycode) const;
|
||||
bool is_physical_key_pressed(Key p_keycode) const;
|
||||
bool is_key_label_pressed(Key p_keycode) const;
|
||||
bool is_mouse_button_pressed(MouseButton p_button) const;
|
||||
bool is_joy_button_pressed(int p_device, JoyButton p_button) const;
|
||||
bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;
|
||||
bool is_action_just_pressed(const StringName &p_action, bool p_exact = false) const;
|
||||
bool is_action_just_released(const StringName &p_action, bool p_exact = false) const;
|
||||
bool is_action_just_pressed_by_event(const StringName &p_action, const Ref<InputEvent> &p_event, bool p_exact = false) const;
|
||||
bool is_action_just_released_by_event(const StringName &p_action, const Ref<InputEvent> &p_event, bool p_exact = false) const;
|
||||
float get_action_strength(const StringName &p_action, bool p_exact = false) const;
|
||||
float get_action_raw_strength(const StringName &p_action, bool p_exact = false) const;
|
||||
|
||||
float get_axis(const StringName &p_negative_action, const StringName &p_positive_action) const;
|
||||
Vector2 get_vector(const StringName &p_negative_x, const StringName &p_positive_x, const StringName &p_negative_y, const StringName &p_positive_y, float p_deadzone = -1.0f) const;
|
||||
|
||||
float get_joy_axis(int p_device, JoyAxis p_axis) const;
|
||||
String get_joy_name(int p_idx);
|
||||
TypedArray<int> get_connected_joypads();
|
||||
Vector2 get_joy_vibration_strength(int p_device);
|
||||
float get_joy_vibration_duration(int p_device);
|
||||
uint64_t get_joy_vibration_timestamp(int p_device);
|
||||
void joy_connection_changed(int p_idx, bool p_connected, const String &p_name, const String &p_guid = "", const Dictionary &p_joypad_info = Dictionary());
|
||||
|
||||
Vector3 get_gravity() const;
|
||||
Vector3 get_accelerometer() const;
|
||||
Vector3 get_magnetometer() const;
|
||||
Vector3 get_gyroscope() const;
|
||||
|
||||
Point2 get_mouse_position() const;
|
||||
Vector2 get_last_mouse_velocity();
|
||||
Vector2 get_last_mouse_screen_velocity();
|
||||
BitField<MouseButtonMask> get_mouse_button_mask() const;
|
||||
|
||||
void warp_mouse(const Vector2 &p_position);
|
||||
Point2 warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect);
|
||||
|
||||
void parse_input_event(const Ref<InputEvent> &p_event);
|
||||
|
||||
void set_gravity(const Vector3 &p_gravity);
|
||||
void set_accelerometer(const Vector3 &p_accel);
|
||||
void set_magnetometer(const Vector3 &p_magnetometer);
|
||||
void set_gyroscope(const Vector3 &p_gyroscope);
|
||||
void set_joy_axis(int p_device, JoyAxis p_axis, float p_value);
|
||||
|
||||
void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0);
|
||||
void stop_joy_vibration(int p_device);
|
||||
void vibrate_handheld(int p_duration_ms = 500, float p_amplitude = -1.0);
|
||||
|
||||
void set_mouse_position(const Point2 &p_posf);
|
||||
|
||||
void action_press(const StringName &p_action, float p_strength = 1.f);
|
||||
void action_release(const StringName &p_action);
|
||||
|
||||
void set_emulate_touch_from_mouse(bool p_emulate);
|
||||
bool is_emulating_touch_from_mouse() const;
|
||||
void ensure_touch_mouse_raised();
|
||||
|
||||
void set_emulate_mouse_from_touch(bool p_emulate);
|
||||
bool is_emulating_mouse_from_touch() const;
|
||||
|
||||
CursorShape get_default_cursor_shape() const;
|
||||
void set_default_cursor_shape(CursorShape p_shape);
|
||||
CursorShape get_current_cursor_shape() const;
|
||||
void set_custom_mouse_cursor(const Ref<Resource> &p_cursor, CursorShape p_shape = Input::CURSOR_ARROW, const Vector2 &p_hotspot = Vector2());
|
||||
|
||||
void parse_mapping(const String &p_mapping);
|
||||
void joy_button(int p_device, JoyButton p_button, bool p_pressed);
|
||||
void joy_axis(int p_device, JoyAxis p_axis, float p_value);
|
||||
void joy_hat(int p_device, BitField<HatMask> p_val);
|
||||
|
||||
void add_joy_mapping(const String &p_mapping, bool p_update_existing = false);
|
||||
void remove_joy_mapping(const String &p_guid);
|
||||
|
||||
int get_unused_joy_id();
|
||||
|
||||
bool is_joy_known(int p_device);
|
||||
String get_joy_guid(int p_device) const;
|
||||
bool should_ignore_device(int p_vendor_id, int p_product_id) const;
|
||||
Dictionary get_joy_info(int p_device) const;
|
||||
void set_fallback_mapping(const String &p_guid);
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
void flush_frame_parsed_events();
|
||||
#endif
|
||||
void flush_buffered_events();
|
||||
bool is_agile_input_event_flushing();
|
||||
void set_agile_input_event_flushing(bool p_enable);
|
||||
void set_use_accumulated_input(bool p_enable);
|
||||
bool is_using_accumulated_input();
|
||||
|
||||
void release_pressed_events();
|
||||
|
||||
void set_event_dispatch_function(EventDispatchFunc p_function);
|
||||
|
||||
void set_disable_input(bool p_disable);
|
||||
bool is_input_disabled() const;
|
||||
|
||||
Input();
|
||||
~Input();
|
||||
};
|
||||
|
||||
VARIANT_ENUM_CAST(Input::MouseMode);
|
||||
VARIANT_ENUM_CAST(Input::CursorShape);
|
63
core/input/input_builders.py
Normal file
63
core/input/input_builders.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""Functions used to generate source files during build time"""
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
import methods
|
||||
|
||||
|
||||
def make_default_controller_mappings(target, source, env):
|
||||
with methods.generated_wrapper(str(target[0])) as file:
|
||||
file.write("""\
|
||||
#include "core/input/default_controller_mappings.h"
|
||||
|
||||
#include "core/typedefs.h"
|
||||
|
||||
""")
|
||||
|
||||
# ensure mappings have a consistent order
|
||||
platform_mappings = OrderedDict()
|
||||
for src_path in map(str, source):
|
||||
with open(src_path, "r", encoding="utf-8") as f:
|
||||
# read mapping file and skip header
|
||||
mapping_file_lines = f.readlines()[2:]
|
||||
|
||||
current_platform = None
|
||||
for line in mapping_file_lines:
|
||||
if not line:
|
||||
continue
|
||||
line = line.strip()
|
||||
if len(line) == 0:
|
||||
continue
|
||||
if line[0] == "#":
|
||||
current_platform = line[1:].strip()
|
||||
if current_platform not in platform_mappings:
|
||||
platform_mappings[current_platform] = {}
|
||||
elif current_platform:
|
||||
line_parts = line.split(",")
|
||||
guid = line_parts[0]
|
||||
if guid in platform_mappings[current_platform]:
|
||||
file.write(
|
||||
"// WARNING: DATABASE {} OVERWROTE PRIOR MAPPING: {} {}\n".format(
|
||||
src_path, current_platform, platform_mappings[current_platform][guid]
|
||||
)
|
||||
)
|
||||
platform_mappings[current_platform][guid] = line
|
||||
|
||||
PLATFORM_VARIABLES = {
|
||||
"Linux": "LINUXBSD",
|
||||
"Windows": "WINDOWS",
|
||||
"Mac OS X": "MACOS",
|
||||
"Android": "ANDROID",
|
||||
"iOS": "APPLE_EMBEDDED",
|
||||
"Web": "WEB",
|
||||
}
|
||||
|
||||
file.write("const char *DefaultControllerMappings::mappings[] = {\n")
|
||||
for platform, mappings in platform_mappings.items():
|
||||
variable = PLATFORM_VARIABLES[platform]
|
||||
file.write(f"#ifdef {variable}_ENABLED\n")
|
||||
for mapping in mappings.values():
|
||||
file.write(f'\t"{mapping}",\n')
|
||||
file.write(f"#endif // {variable}_ENABLED\n")
|
||||
|
||||
file.write("\tnullptr\n};\n")
|
163
core/input/input_enums.h
Normal file
163
core/input/input_enums.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/**************************************************************************/
|
||||
/* input_enums.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/error/error_macros.h"
|
||||
|
||||
enum class InputEventType {
|
||||
INVALID = -1,
|
||||
KEY,
|
||||
MOUSE_BUTTON,
|
||||
MOUSE_MOTION,
|
||||
JOY_MOTION,
|
||||
JOY_BUTTON,
|
||||
SCREEN_TOUCH,
|
||||
SCREEN_DRAG,
|
||||
MAGNIFY_GESTURE,
|
||||
PAN_GESTURE,
|
||||
MIDI,
|
||||
SHORTCUT,
|
||||
ACTION,
|
||||
MAX,
|
||||
};
|
||||
|
||||
enum class HatDir {
|
||||
UP = 0,
|
||||
RIGHT = 1,
|
||||
DOWN = 2,
|
||||
LEFT = 3,
|
||||
MAX = 4,
|
||||
};
|
||||
|
||||
enum class HatMask {
|
||||
CENTER = 0,
|
||||
UP = 1,
|
||||
RIGHT = 2,
|
||||
DOWN = 4,
|
||||
LEFT = 8,
|
||||
};
|
||||
|
||||
enum class JoyAxis {
|
||||
INVALID = -1,
|
||||
LEFT_X = 0,
|
||||
LEFT_Y = 1,
|
||||
RIGHT_X = 2,
|
||||
RIGHT_Y = 3,
|
||||
TRIGGER_LEFT = 4,
|
||||
TRIGGER_RIGHT = 5,
|
||||
SDL_MAX = 6,
|
||||
MAX = 10, // OpenVR supports up to 5 Joysticks making a total of 10 axes.
|
||||
};
|
||||
|
||||
enum class JoyButton {
|
||||
INVALID = -1,
|
||||
A = 0,
|
||||
B = 1,
|
||||
X = 2,
|
||||
Y = 3,
|
||||
BACK = 4,
|
||||
GUIDE = 5,
|
||||
START = 6,
|
||||
LEFT_STICK = 7,
|
||||
RIGHT_STICK = 8,
|
||||
LEFT_SHOULDER = 9,
|
||||
RIGHT_SHOULDER = 10,
|
||||
DPAD_UP = 11,
|
||||
DPAD_DOWN = 12,
|
||||
DPAD_LEFT = 13,
|
||||
DPAD_RIGHT = 14,
|
||||
MISC1 = 15,
|
||||
PADDLE1 = 16,
|
||||
PADDLE2 = 17,
|
||||
PADDLE3 = 18,
|
||||
PADDLE4 = 19,
|
||||
TOUCHPAD = 20,
|
||||
SDL_MAX = 21,
|
||||
MAX = 128, // Android supports up to 36 buttons. DirectInput supports up to 128 buttons.
|
||||
};
|
||||
|
||||
enum class MIDIMessage {
|
||||
NONE = 0,
|
||||
NOTE_OFF = 0x8,
|
||||
NOTE_ON = 0x9,
|
||||
AFTERTOUCH = 0xA,
|
||||
CONTROL_CHANGE = 0xB,
|
||||
PROGRAM_CHANGE = 0xC,
|
||||
CHANNEL_PRESSURE = 0xD,
|
||||
PITCH_BEND = 0xE,
|
||||
SYSTEM_EXCLUSIVE = 0xF0,
|
||||
QUARTER_FRAME = 0xF1,
|
||||
SONG_POSITION_POINTER = 0xF2,
|
||||
SONG_SELECT = 0xF3,
|
||||
TUNE_REQUEST = 0xF6,
|
||||
TIMING_CLOCK = 0xF8,
|
||||
START = 0xFA,
|
||||
CONTINUE = 0xFB,
|
||||
STOP = 0xFC,
|
||||
ACTIVE_SENSING = 0xFE,
|
||||
SYSTEM_RESET = 0xFF,
|
||||
};
|
||||
|
||||
enum class MouseButton {
|
||||
NONE = 0,
|
||||
LEFT = 1,
|
||||
RIGHT = 2,
|
||||
MIDDLE = 3,
|
||||
WHEEL_UP = 4,
|
||||
WHEEL_DOWN = 5,
|
||||
WHEEL_LEFT = 6,
|
||||
WHEEL_RIGHT = 7,
|
||||
MB_XBUTTON1 = 8, // "XBUTTON1" is a reserved word on Windows.
|
||||
MB_XBUTTON2 = 9, // "XBUTTON2" is a reserved word on Windows.
|
||||
};
|
||||
|
||||
enum class MouseButtonMask {
|
||||
NONE = 0,
|
||||
LEFT = (1 << (int(MouseButton::LEFT) - 1)),
|
||||
RIGHT = (1 << (int(MouseButton::RIGHT) - 1)),
|
||||
MIDDLE = (1 << (int(MouseButton::MIDDLE) - 1)),
|
||||
MB_XBUTTON1 = (1 << (int(MouseButton::MB_XBUTTON1) - 1)),
|
||||
MB_XBUTTON2 = (1 << (int(MouseButton::MB_XBUTTON2) - 1)),
|
||||
};
|
||||
|
||||
inline MouseButtonMask mouse_button_to_mask(MouseButton button) {
|
||||
ERR_FAIL_COND_V(button == MouseButton::NONE, MouseButtonMask::NONE);
|
||||
|
||||
return MouseButtonMask(1 << ((int)button - 1));
|
||||
}
|
||||
|
||||
constexpr MouseButtonMask operator|(MouseButtonMask p_a, MouseButtonMask p_b) {
|
||||
return static_cast<MouseButtonMask>(static_cast<int>(p_a) | static_cast<int>(p_b));
|
||||
}
|
||||
|
||||
constexpr MouseButtonMask &operator|=(MouseButtonMask &p_a, MouseButtonMask p_b) {
|
||||
return p_a = p_a | p_b;
|
||||
}
|
1935
core/input/input_event.cpp
Normal file
1935
core/input/input_event.cpp
Normal file
File diff suppressed because it is too large
Load Diff
622
core/input/input_event.h
Normal file
622
core/input/input_event.h
Normal file
@@ -0,0 +1,622 @@
|
||||
/**************************************************************************/
|
||||
/* input_event.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/input/input_enums.h"
|
||||
#include "core/io/resource.h"
|
||||
#include "core/math/transform_2d.h"
|
||||
#include "core/os/keyboard.h"
|
||||
#include "core/string/ustring.h"
|
||||
#include "core/typedefs.h"
|
||||
|
||||
/**
|
||||
* Input Event classes. These are used in the main loop.
|
||||
* The events are pretty obvious.
|
||||
*/
|
||||
|
||||
class Shortcut;
|
||||
|
||||
/**
|
||||
* Input Modifier Status
|
||||
* for keyboard/mouse events.
|
||||
*/
|
||||
|
||||
class InputEvent : public Resource {
|
||||
GDCLASS(InputEvent, Resource);
|
||||
|
||||
int device = 0;
|
||||
|
||||
protected:
|
||||
bool canceled = false;
|
||||
bool pressed = false;
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
static constexpr int DEVICE_ID_EMULATION = -1;
|
||||
static constexpr int DEVICE_ID_INTERNAL = -2;
|
||||
|
||||
void set_device(int p_device);
|
||||
int get_device() const;
|
||||
|
||||
bool is_action(const StringName &p_action, bool p_exact_match = false) const;
|
||||
bool is_action_pressed(const StringName &p_action, bool p_allow_echo = false, bool p_exact_match = false) const;
|
||||
bool is_action_released(const StringName &p_action, bool p_exact_match = false) const;
|
||||
float get_action_strength(const StringName &p_action, bool p_exact_match = false) const;
|
||||
float get_action_raw_strength(const StringName &p_action, bool p_exact_match = false) const;
|
||||
|
||||
bool is_canceled() const;
|
||||
bool is_pressed() const;
|
||||
bool is_released() const;
|
||||
virtual bool is_echo() const;
|
||||
|
||||
virtual String as_text() const = 0;
|
||||
|
||||
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
|
||||
|
||||
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const;
|
||||
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
|
||||
|
||||
virtual bool is_action_type() const;
|
||||
|
||||
virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
|
||||
|
||||
virtual InputEventType get_type() const { return InputEventType::INVALID; }
|
||||
|
||||
InputEvent() {}
|
||||
};
|
||||
|
||||
class InputEventFromWindow : public InputEvent {
|
||||
GDCLASS(InputEventFromWindow, InputEvent);
|
||||
|
||||
int64_t window_id = 0;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_window_id(int64_t p_id);
|
||||
int64_t get_window_id() const;
|
||||
|
||||
InputEventFromWindow() {}
|
||||
};
|
||||
|
||||
class InputEventWithModifiers : public InputEventFromWindow {
|
||||
GDCLASS(InputEventWithModifiers, InputEventFromWindow);
|
||||
|
||||
bool command_or_control_autoremap = false;
|
||||
|
||||
bool shift_pressed = false;
|
||||
bool alt_pressed = false;
|
||||
bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms.
|
||||
bool ctrl_pressed = false;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
void _validate_property(PropertyInfo &p_property) const;
|
||||
|
||||
public:
|
||||
void set_command_or_control_autoremap(bool p_enabled);
|
||||
bool is_command_or_control_autoremap() const;
|
||||
|
||||
bool is_command_or_control_pressed() const;
|
||||
|
||||
void set_shift_pressed(bool p_pressed);
|
||||
bool is_shift_pressed() const;
|
||||
|
||||
void set_alt_pressed(bool p_pressed);
|
||||
bool is_alt_pressed() const;
|
||||
|
||||
void set_ctrl_pressed(bool p_pressed);
|
||||
bool is_ctrl_pressed() const;
|
||||
|
||||
void set_meta_pressed(bool p_pressed);
|
||||
bool is_meta_pressed() const;
|
||||
|
||||
void set_modifiers_from_event(const InputEventWithModifiers *event);
|
||||
|
||||
BitField<KeyModifierMask> get_modifiers_mask() const;
|
||||
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventWithModifiers() {}
|
||||
};
|
||||
|
||||
class InputEventKey : public InputEventWithModifiers {
|
||||
GDCLASS(InputEventKey, InputEventWithModifiers);
|
||||
|
||||
Key keycode = Key::NONE; // Key enum, without modifier masks.
|
||||
Key physical_keycode = Key::NONE;
|
||||
Key key_label = Key::NONE;
|
||||
uint32_t unicode = 0; ///unicode
|
||||
KeyLocation location = KeyLocation::UNSPECIFIED;
|
||||
|
||||
bool echo = false; /// true if this is an echo key
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_pressed(bool p_pressed);
|
||||
|
||||
void set_keycode(Key p_keycode);
|
||||
Key get_keycode() const;
|
||||
|
||||
void set_physical_keycode(Key p_keycode);
|
||||
Key get_physical_keycode() const;
|
||||
|
||||
void set_key_label(Key p_key_label);
|
||||
Key get_key_label() const;
|
||||
|
||||
void set_unicode(char32_t p_unicode);
|
||||
char32_t get_unicode() const;
|
||||
|
||||
void set_location(KeyLocation p_key_location);
|
||||
KeyLocation get_location() const;
|
||||
|
||||
void set_echo(bool p_enable);
|
||||
virtual bool is_echo() const override;
|
||||
|
||||
Key get_keycode_with_modifiers() const;
|
||||
Key get_physical_keycode_with_modifiers() const;
|
||||
Key get_key_label_with_modifiers() const;
|
||||
|
||||
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
|
||||
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
|
||||
|
||||
virtual bool is_action_type() const override { return true; }
|
||||
|
||||
virtual String as_text_physical_keycode() const;
|
||||
virtual String as_text_keycode() const;
|
||||
virtual String as_text_key_label() const;
|
||||
virtual String as_text_location() const;
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks, bool p_physical = false);
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::KEY; }
|
||||
|
||||
InputEventKey() {}
|
||||
};
|
||||
|
||||
class InputEventMouse : public InputEventWithModifiers {
|
||||
GDCLASS(InputEventMouse, InputEventWithModifiers);
|
||||
|
||||
BitField<MouseButtonMask> button_mask = MouseButtonMask::NONE;
|
||||
|
||||
Vector2 pos;
|
||||
Vector2 global_pos;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_button_mask(BitField<MouseButtonMask> p_mask);
|
||||
BitField<MouseButtonMask> get_button_mask() const;
|
||||
|
||||
void set_position(const Vector2 &p_pos);
|
||||
Vector2 get_position() const;
|
||||
|
||||
void set_global_position(const Vector2 &p_global_pos);
|
||||
Vector2 get_global_position() const;
|
||||
|
||||
InputEventMouse() {}
|
||||
};
|
||||
|
||||
class InputEventMouseButton : public InputEventMouse {
|
||||
GDCLASS(InputEventMouseButton, InputEventMouse);
|
||||
|
||||
float factor = 1;
|
||||
MouseButton button_index = MouseButton::NONE;
|
||||
bool double_click = false; //last even less than double click time
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_factor(float p_factor);
|
||||
float get_factor() const;
|
||||
|
||||
void set_button_index(MouseButton p_index);
|
||||
MouseButton get_button_index() const;
|
||||
|
||||
void set_pressed(bool p_pressed);
|
||||
void set_canceled(bool p_canceled);
|
||||
|
||||
void set_double_click(bool p_double_click);
|
||||
bool is_double_click() const;
|
||||
|
||||
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
|
||||
|
||||
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
|
||||
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
|
||||
|
||||
virtual bool is_action_type() const override { return true; }
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::MOUSE_BUTTON; }
|
||||
|
||||
InputEventMouseButton() {}
|
||||
};
|
||||
|
||||
class InputEventMouseMotion : public InputEventMouse {
|
||||
GDCLASS(InputEventMouseMotion, InputEventMouse);
|
||||
|
||||
Vector2 tilt;
|
||||
float pressure = 0;
|
||||
Vector2 relative;
|
||||
Vector2 screen_relative;
|
||||
Vector2 velocity;
|
||||
Vector2 screen_velocity;
|
||||
bool pen_inverted = false;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_tilt(const Vector2 &p_tilt);
|
||||
Vector2 get_tilt() const;
|
||||
|
||||
void set_pressure(float p_pressure);
|
||||
float get_pressure() const;
|
||||
|
||||
void set_pen_inverted(bool p_inverted);
|
||||
bool get_pen_inverted() const;
|
||||
|
||||
void set_relative(const Vector2 &p_relative);
|
||||
Vector2 get_relative() const;
|
||||
|
||||
void set_relative_screen_position(const Vector2 &p_relative);
|
||||
Vector2 get_relative_screen_position() const;
|
||||
|
||||
void set_velocity(const Vector2 &p_velocity);
|
||||
Vector2 get_velocity() const;
|
||||
|
||||
void set_screen_velocity(const Vector2 &p_velocity);
|
||||
Vector2 get_screen_velocity() const;
|
||||
|
||||
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
virtual bool accumulate(const Ref<InputEvent> &p_event) override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::MOUSE_MOTION; }
|
||||
|
||||
InputEventMouseMotion() {}
|
||||
};
|
||||
|
||||
class InputEventJoypadMotion : public InputEvent {
|
||||
GDCLASS(InputEventJoypadMotion, InputEvent);
|
||||
JoyAxis axis = (JoyAxis)0; ///< Joypad axis
|
||||
float axis_value = 0; ///< -1 to 1
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_axis(JoyAxis p_axis);
|
||||
JoyAxis get_axis() const;
|
||||
|
||||
void set_axis_value(float p_value);
|
||||
float get_axis_value() const;
|
||||
|
||||
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
|
||||
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
|
||||
|
||||
virtual bool is_action_type() const override { return true; }
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
static Ref<InputEventJoypadMotion> create_reference(JoyAxis p_axis, float p_value);
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::JOY_MOTION; }
|
||||
|
||||
InputEventJoypadMotion() {}
|
||||
};
|
||||
|
||||
class InputEventJoypadButton : public InputEvent {
|
||||
GDCLASS(InputEventJoypadButton, InputEvent);
|
||||
|
||||
JoyButton button_index = (JoyButton)0;
|
||||
float pressure = 0; //0 to 1
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_button_index(JoyButton p_index);
|
||||
JoyButton get_button_index() const;
|
||||
|
||||
void set_pressed(bool p_pressed);
|
||||
|
||||
void set_pressure(float p_pressure);
|
||||
float get_pressure() const;
|
||||
|
||||
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
|
||||
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
|
||||
|
||||
virtual bool is_action_type() const override { return true; }
|
||||
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
static Ref<InputEventJoypadButton> create_reference(JoyButton p_btn_index);
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::JOY_BUTTON; }
|
||||
|
||||
InputEventJoypadButton() {}
|
||||
};
|
||||
|
||||
class InputEventScreenTouch : public InputEventFromWindow {
|
||||
GDCLASS(InputEventScreenTouch, InputEventFromWindow);
|
||||
int index = 0;
|
||||
Vector2 pos;
|
||||
bool double_tap = false;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_index(int p_index);
|
||||
int get_index() const;
|
||||
|
||||
void set_position(const Vector2 &p_pos);
|
||||
Vector2 get_position() const;
|
||||
|
||||
void set_pressed(bool p_pressed);
|
||||
void set_canceled(bool p_canceled);
|
||||
|
||||
void set_double_tap(bool p_double_tap);
|
||||
bool is_double_tap() const;
|
||||
|
||||
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::SCREEN_TOUCH; }
|
||||
|
||||
InputEventScreenTouch() {}
|
||||
};
|
||||
|
||||
class InputEventScreenDrag : public InputEventFromWindow {
|
||||
GDCLASS(InputEventScreenDrag, InputEventFromWindow);
|
||||
int index = 0;
|
||||
Vector2 pos;
|
||||
Vector2 relative;
|
||||
Vector2 screen_relative;
|
||||
Vector2 velocity;
|
||||
Vector2 screen_velocity;
|
||||
Vector2 tilt;
|
||||
float pressure = 0;
|
||||
bool pen_inverted = false;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_index(int p_index);
|
||||
int get_index() const;
|
||||
|
||||
void set_tilt(const Vector2 &p_tilt);
|
||||
Vector2 get_tilt() const;
|
||||
|
||||
void set_pressure(float p_pressure);
|
||||
float get_pressure() const;
|
||||
|
||||
void set_pen_inverted(bool p_inverted);
|
||||
bool get_pen_inverted() const;
|
||||
|
||||
void set_position(const Vector2 &p_pos);
|
||||
Vector2 get_position() const;
|
||||
|
||||
void set_relative(const Vector2 &p_relative);
|
||||
Vector2 get_relative() const;
|
||||
|
||||
void set_relative_screen_position(const Vector2 &p_relative);
|
||||
Vector2 get_relative_screen_position() const;
|
||||
|
||||
void set_velocity(const Vector2 &p_velocity);
|
||||
Vector2 get_velocity() const;
|
||||
|
||||
void set_screen_velocity(const Vector2 &p_velocity);
|
||||
Vector2 get_screen_velocity() const;
|
||||
|
||||
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
virtual bool accumulate(const Ref<InputEvent> &p_event) override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::SCREEN_DRAG; }
|
||||
|
||||
InputEventScreenDrag() {}
|
||||
};
|
||||
|
||||
class InputEventAction : public InputEvent {
|
||||
GDCLASS(InputEventAction, InputEvent);
|
||||
|
||||
StringName action;
|
||||
float strength = 1.0f;
|
||||
int event_index = -1;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_action(const StringName &p_action);
|
||||
StringName get_action() const;
|
||||
|
||||
void set_pressed(bool p_pressed);
|
||||
|
||||
void set_strength(float p_strength);
|
||||
float get_strength() const;
|
||||
|
||||
void set_event_index(int p_index);
|
||||
int get_event_index() const;
|
||||
|
||||
virtual bool is_action(const StringName &p_action) const;
|
||||
|
||||
virtual bool action_match(const Ref<InputEvent> &p_event, bool p_exact_match, float p_deadzone, bool *r_pressed, float *r_strength, float *r_raw_strength) const override;
|
||||
virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
|
||||
|
||||
virtual bool is_action_type() const override { return true; }
|
||||
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::ACTION; }
|
||||
|
||||
InputEventAction() {}
|
||||
};
|
||||
|
||||
class InputEventGesture : public InputEventWithModifiers {
|
||||
GDCLASS(InputEventGesture, InputEventWithModifiers);
|
||||
|
||||
Vector2 pos;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_position(const Vector2 &p_pos);
|
||||
Vector2 get_position() const;
|
||||
};
|
||||
|
||||
class InputEventMagnifyGesture : public InputEventGesture {
|
||||
GDCLASS(InputEventMagnifyGesture, InputEventGesture);
|
||||
real_t factor = 1.0;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_factor(real_t p_factor);
|
||||
real_t get_factor() const;
|
||||
|
||||
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::MAGNIFY_GESTURE; }
|
||||
|
||||
InputEventMagnifyGesture() {}
|
||||
};
|
||||
|
||||
class InputEventPanGesture : public InputEventGesture {
|
||||
GDCLASS(InputEventPanGesture, InputEventGesture);
|
||||
Vector2 delta;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_delta(const Vector2 &p_delta);
|
||||
Vector2 get_delta() const;
|
||||
|
||||
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::PAN_GESTURE; }
|
||||
|
||||
InputEventPanGesture() {}
|
||||
};
|
||||
|
||||
class InputEventMIDI : public InputEvent {
|
||||
GDCLASS(InputEventMIDI, InputEvent);
|
||||
|
||||
int channel = 0;
|
||||
MIDIMessage message = MIDIMessage::NONE;
|
||||
int pitch = 0;
|
||||
int velocity = 0;
|
||||
int instrument = 0;
|
||||
int pressure = 0;
|
||||
int controller_number = 0;
|
||||
int controller_value = 0;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_channel(const int p_channel);
|
||||
int get_channel() const;
|
||||
|
||||
void set_message(const MIDIMessage p_message);
|
||||
MIDIMessage get_message() const;
|
||||
|
||||
void set_pitch(const int p_pitch);
|
||||
int get_pitch() const;
|
||||
|
||||
void set_velocity(const int p_velocity);
|
||||
int get_velocity() const;
|
||||
|
||||
void set_instrument(const int p_instrument);
|
||||
int get_instrument() const;
|
||||
|
||||
void set_pressure(const int p_pressure);
|
||||
int get_pressure() const;
|
||||
|
||||
void set_controller_number(const int p_controller_number);
|
||||
int get_controller_number() const;
|
||||
|
||||
void set_controller_value(const int p_controller_value);
|
||||
int get_controller_value() const;
|
||||
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::MIDI; }
|
||||
|
||||
InputEventMIDI() {}
|
||||
};
|
||||
|
||||
class InputEventShortcut : public InputEvent {
|
||||
GDCLASS(InputEventShortcut, InputEvent);
|
||||
|
||||
Ref<Shortcut> shortcut;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_shortcut(Ref<Shortcut> p_shortcut);
|
||||
Ref<Shortcut> get_shortcut();
|
||||
|
||||
virtual String as_text() const override;
|
||||
virtual String to_string() override;
|
||||
|
||||
InputEventType get_type() const final override { return InputEventType::SHORTCUT; }
|
||||
|
||||
InputEventShortcut();
|
||||
};
|
474
core/input/input_event_codec.cpp
Normal file
474
core/input/input_event_codec.cpp
Normal file
@@ -0,0 +1,474 @@
|
||||
/**************************************************************************/
|
||||
/* input_event_codec.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "input_event_codec.h"
|
||||
|
||||
#include "core/input/input.h"
|
||||
#include "core/io/marshalls.h"
|
||||
#include "core/os/os.h"
|
||||
|
||||
enum class BoolShift : uint8_t {
|
||||
SHIFT = 0,
|
||||
CTRL,
|
||||
ALT,
|
||||
META,
|
||||
ECHO,
|
||||
PRESSED,
|
||||
DOUBLE_CLICK,
|
||||
PEN_INVERTED,
|
||||
};
|
||||
|
||||
// cast operator for BoolShift to uint8_t
|
||||
inline uint8_t operator<<(uint8_t a, BoolShift b) {
|
||||
return a << static_cast<uint8_t>(b);
|
||||
}
|
||||
|
||||
uint8_t encode_key_modifier_state(Ref<InputEventWithModifiers> p_event) {
|
||||
uint8_t bools = 0;
|
||||
bools |= (uint8_t)p_event->is_shift_pressed() << BoolShift::SHIFT;
|
||||
bools |= (uint8_t)p_event->is_ctrl_pressed() << BoolShift::CTRL;
|
||||
bools |= (uint8_t)p_event->is_alt_pressed() << BoolShift::ALT;
|
||||
bools |= (uint8_t)p_event->is_meta_pressed() << BoolShift::META;
|
||||
return bools;
|
||||
}
|
||||
|
||||
void decode_key_modifier_state(uint8_t bools, Ref<InputEventWithModifiers> p_event) {
|
||||
p_event->set_shift_pressed(bools & (1 << BoolShift::SHIFT));
|
||||
p_event->set_ctrl_pressed(bools & (1 << BoolShift::CTRL));
|
||||
p_event->set_alt_pressed(bools & (1 << BoolShift::ALT));
|
||||
p_event->set_meta_pressed(bools & (1 << BoolShift::META));
|
||||
}
|
||||
|
||||
int encode_vector2(const Vector2 &p_vector, uint8_t *p_data) {
|
||||
p_data += encode_float(p_vector.x, p_data);
|
||||
encode_float(p_vector.y, p_data);
|
||||
return sizeof(float) * 2;
|
||||
}
|
||||
|
||||
const uint8_t *decode_vector2(Vector2 &r_vector, const uint8_t *p_data) {
|
||||
r_vector.x = decode_float(p_data);
|
||||
p_data += sizeof(float);
|
||||
r_vector.y = decode_float(p_data);
|
||||
p_data += sizeof(float);
|
||||
return p_data;
|
||||
}
|
||||
|
||||
void encode_input_event_key(const Ref<InputEventKey> &p_event, PackedByteArray &r_data) {
|
||||
r_data.resize(19);
|
||||
|
||||
uint8_t *data = r_data.ptrw();
|
||||
*data = (uint8_t)InputEventType::KEY;
|
||||
data++;
|
||||
uint8_t bools = encode_key_modifier_state(p_event);
|
||||
bools |= (uint8_t)p_event->is_echo() << BoolShift::ECHO;
|
||||
bools |= (uint8_t)p_event->is_pressed() << BoolShift::PRESSED;
|
||||
*data = bools;
|
||||
data++;
|
||||
data += encode_uint32((uint32_t)p_event->get_keycode(), data);
|
||||
data += encode_uint32((uint32_t)p_event->get_physical_keycode(), data);
|
||||
data += encode_uint32((uint32_t)p_event->get_key_label(), data);
|
||||
data += encode_uint32(p_event->get_unicode(), data);
|
||||
*data = (uint8_t)p_event->get_location();
|
||||
data++;
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - r_data.ptrw() >= r_data.size());
|
||||
}
|
||||
|
||||
Error decode_input_event_key(const PackedByteArray &p_data, Ref<InputEventKey> &r_event) {
|
||||
const uint8_t *data = p_data.ptr();
|
||||
DEV_ASSERT(static_cast<InputEventType>(*data) == InputEventType::KEY);
|
||||
data++; // Skip event type.
|
||||
|
||||
uint8_t bools = *data;
|
||||
data++;
|
||||
decode_key_modifier_state(bools, r_event);
|
||||
r_event->set_echo(bools & (1 << BoolShift::ECHO));
|
||||
r_event->set_pressed(bools & (1 << BoolShift::PRESSED));
|
||||
|
||||
Key keycode = (Key)decode_uint32(data);
|
||||
data += sizeof(uint32_t);
|
||||
Key physical_keycode = (Key)decode_uint32(data);
|
||||
data += sizeof(uint32_t);
|
||||
Key key_label = (Key)decode_uint32(data);
|
||||
data += sizeof(uint32_t);
|
||||
char32_t unicode = (char32_t)decode_uint32(data);
|
||||
data += sizeof(uint32_t);
|
||||
KeyLocation location = (KeyLocation)*data;
|
||||
|
||||
r_event->set_keycode(keycode);
|
||||
r_event->set_physical_keycode(physical_keycode);
|
||||
r_event->set_key_label(key_label);
|
||||
r_event->set_unicode(unicode);
|
||||
r_event->set_location(location);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
void encode_input_event_mouse_button(const Ref<InputEventMouseButton> &p_event, PackedByteArray &r_data) {
|
||||
r_data.resize(12);
|
||||
|
||||
uint8_t *data = r_data.ptrw();
|
||||
*data = (uint8_t)InputEventType::MOUSE_BUTTON;
|
||||
data++;
|
||||
|
||||
uint8_t bools = encode_key_modifier_state(p_event);
|
||||
bools |= (uint8_t)p_event->is_pressed() << BoolShift::PRESSED;
|
||||
bools |= (uint8_t)p_event->is_double_click() << BoolShift::DOUBLE_CLICK;
|
||||
*data = bools;
|
||||
data++;
|
||||
|
||||
*data = (uint8_t)p_event->get_button_index();
|
||||
data++;
|
||||
|
||||
// Rather than use encode_variant, we explicitly encode the Vector2,
|
||||
// so decoding is easier. Specifically, we don't have to perform additional error
|
||||
// checking for decoding the variant and then checking the variant type.
|
||||
data += encode_vector2(p_event->get_position(), data);
|
||||
*data = (uint8_t)p_event->get_button_mask();
|
||||
data++;
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - r_data.ptrw() >= r_data.size());
|
||||
}
|
||||
|
||||
Error decode_input_event_mouse_button(const PackedByteArray &p_data, Ref<InputEventMouseButton> &r_event) {
|
||||
const uint8_t *data = p_data.ptr();
|
||||
DEV_ASSERT(static_cast<InputEventType>(*data) == InputEventType::MOUSE_BUTTON);
|
||||
data++; // Skip event type.
|
||||
|
||||
uint8_t bools = *data;
|
||||
data++;
|
||||
decode_key_modifier_state(bools, r_event);
|
||||
r_event->set_pressed(bools & (1 << BoolShift::PRESSED));
|
||||
r_event->set_double_click(bools & (1 << BoolShift::DOUBLE_CLICK));
|
||||
|
||||
r_event->set_button_index((MouseButton)*data);
|
||||
data++;
|
||||
|
||||
Vector2 pos;
|
||||
data = decode_vector2(pos, data);
|
||||
r_event->set_position(pos);
|
||||
r_event->set_global_position(pos); // these are the same
|
||||
BitField<MouseButtonMask> button_mask = (MouseButtonMask)*data;
|
||||
r_event->set_button_mask(button_mask);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
void encode_input_event_mouse_motion(const Ref<InputEventMouseMotion> &p_event, PackedByteArray &r_data) {
|
||||
r_data.resize(31);
|
||||
|
||||
uint8_t *data = r_data.ptrw();
|
||||
*data = (uint8_t)InputEventType::MOUSE_MOTION;
|
||||
data++;
|
||||
|
||||
uint8_t bools = encode_key_modifier_state(p_event);
|
||||
bools |= (uint8_t)p_event->get_pen_inverted() << BoolShift::PEN_INVERTED;
|
||||
*data = bools;
|
||||
data++;
|
||||
|
||||
// Rather than use encode_variant, we explicitly encode the Vector2,
|
||||
// so decoding is easier. Specifically, we don't have to perform additional error
|
||||
// checking for decoding the variant and then checking the variant type.
|
||||
data += encode_vector2(p_event->get_position(), data);
|
||||
data += encode_float(p_event->get_pressure(), data);
|
||||
data += encode_vector2(p_event->get_tilt(), data);
|
||||
data += encode_vector2(p_event->get_relative(), data);
|
||||
*data = (uint8_t)p_event->get_button_mask();
|
||||
data++;
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - r_data.ptrw() >= r_data.size());
|
||||
}
|
||||
|
||||
void decode_input_event_mouse_motion(const PackedByteArray &p_data, Ref<InputEventMouseMotion> &r_event) {
|
||||
Input *input = Input::get_singleton();
|
||||
|
||||
const uint8_t *data = p_data.ptr();
|
||||
DEV_ASSERT(static_cast<InputEventType>(*data) == InputEventType::MOUSE_MOTION);
|
||||
data++; // Skip event type.
|
||||
|
||||
uint8_t bools = *data;
|
||||
data++;
|
||||
decode_key_modifier_state(bools, r_event);
|
||||
r_event->set_pen_inverted(bools & (1 << BoolShift::PEN_INVERTED));
|
||||
|
||||
{
|
||||
Vector2 pos;
|
||||
data = decode_vector2(pos, data);
|
||||
r_event->set_position(pos);
|
||||
r_event->set_global_position(pos); // these are the same
|
||||
}
|
||||
r_event->set_pressure(decode_float(data));
|
||||
data += sizeof(float);
|
||||
{
|
||||
Vector2 tilt;
|
||||
data = decode_vector2(tilt, data);
|
||||
r_event->set_tilt(tilt);
|
||||
}
|
||||
r_event->set_velocity(input->get_last_mouse_velocity());
|
||||
r_event->set_screen_velocity(input->get_last_mouse_screen_velocity());
|
||||
{
|
||||
Vector2 relative;
|
||||
data = decode_vector2(relative, data);
|
||||
r_event->set_relative(relative);
|
||||
r_event->set_relative_screen_position(relative);
|
||||
}
|
||||
BitField<MouseButtonMask> button_mask = (MouseButtonMask)*data;
|
||||
r_event->set_button_mask(button_mask);
|
||||
data++;
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - p_data.ptr() >= p_data.size());
|
||||
}
|
||||
|
||||
void encode_input_event_joypad_button(const Ref<InputEventJoypadButton> &p_event, PackedByteArray &r_data) {
|
||||
r_data.resize(11);
|
||||
|
||||
uint8_t *data = r_data.ptrw();
|
||||
*data = (uint8_t)InputEventType::JOY_BUTTON;
|
||||
data++;
|
||||
|
||||
uint8_t bools = 0;
|
||||
bools |= (uint8_t)p_event->is_pressed() << BoolShift::PRESSED;
|
||||
*data = bools;
|
||||
data++;
|
||||
|
||||
data += encode_uint64(p_event->get_device(), data);
|
||||
*data = (uint8_t)p_event->get_button_index();
|
||||
data++;
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - r_data.ptrw() >= r_data.size());
|
||||
}
|
||||
|
||||
void decode_input_event_joypad_button(const PackedByteArray &p_data, Ref<InputEventJoypadButton> &r_event) {
|
||||
const uint8_t *data = p_data.ptr();
|
||||
DEV_ASSERT(static_cast<InputEventType>(*data) == InputEventType::JOY_BUTTON);
|
||||
data++; // Skip event type.
|
||||
|
||||
uint8_t bools = *data;
|
||||
data++;
|
||||
r_event->set_pressed(bools & (1 << BoolShift::PRESSED));
|
||||
r_event->set_device(decode_uint64(data));
|
||||
data += sizeof(uint64_t);
|
||||
r_event->set_button_index((JoyButton)*data);
|
||||
data++;
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - p_data.ptr() >= p_data.size());
|
||||
}
|
||||
|
||||
void encode_input_event_joypad_motion(const Ref<InputEventJoypadMotion> &p_event, PackedByteArray &r_data) {
|
||||
r_data.resize(14);
|
||||
|
||||
uint8_t *data = r_data.ptrw();
|
||||
*data = (uint8_t)InputEventType::JOY_MOTION;
|
||||
data++;
|
||||
|
||||
data += encode_uint64(p_event->get_device(), data);
|
||||
*data = (uint8_t)p_event->get_axis();
|
||||
data++;
|
||||
data += encode_float(p_event->get_axis_value(), data);
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - r_data.ptrw() >= r_data.size());
|
||||
}
|
||||
|
||||
void decode_input_event_joypad_motion(const PackedByteArray &p_data, Ref<InputEventJoypadMotion> &r_event) {
|
||||
const uint8_t *data = p_data.ptr();
|
||||
DEV_ASSERT(static_cast<InputEventType>(*data) == InputEventType::JOY_MOTION);
|
||||
data++; // Skip event type.
|
||||
|
||||
r_event->set_device(decode_uint64(data));
|
||||
data += sizeof(uint64_t);
|
||||
r_event->set_axis((JoyAxis)*data);
|
||||
data++;
|
||||
r_event->set_axis_value(decode_float(data));
|
||||
data += sizeof(float);
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - p_data.ptr() >= p_data.size());
|
||||
}
|
||||
|
||||
void encode_input_event_gesture_pan(const Ref<InputEventPanGesture> &p_event, PackedByteArray &r_data) {
|
||||
r_data.resize(18);
|
||||
|
||||
uint8_t *data = r_data.ptrw();
|
||||
*data = (uint8_t)InputEventType::PAN_GESTURE;
|
||||
data++;
|
||||
|
||||
uint8_t bools = encode_key_modifier_state(p_event);
|
||||
*data = bools;
|
||||
data++;
|
||||
data += encode_vector2(p_event->get_position(), data);
|
||||
data += encode_vector2(p_event->get_delta(), data);
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - r_data.ptrw() >= r_data.size());
|
||||
}
|
||||
|
||||
void decode_input_event_gesture_pan(const PackedByteArray &p_data, Ref<InputEventPanGesture> &r_event) {
|
||||
const uint8_t *data = p_data.ptr();
|
||||
DEV_ASSERT(static_cast<InputEventType>(*data) == InputEventType::PAN_GESTURE);
|
||||
data++; // Skip event type.
|
||||
|
||||
uint8_t bools = *data;
|
||||
data++;
|
||||
decode_key_modifier_state(bools, r_event);
|
||||
|
||||
Vector2 pos;
|
||||
data = decode_vector2(pos, data);
|
||||
r_event->set_position(pos);
|
||||
Vector2 delta;
|
||||
data = decode_vector2(delta, data);
|
||||
r_event->set_delta(delta);
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - p_data.ptr() >= p_data.size());
|
||||
}
|
||||
|
||||
void encode_input_event_gesture_magnify(const Ref<InputEventMagnifyGesture> &p_event, PackedByteArray &r_data) {
|
||||
r_data.resize(14);
|
||||
|
||||
uint8_t *data = r_data.ptrw();
|
||||
*data = (uint8_t)InputEventType::MAGNIFY_GESTURE;
|
||||
data++;
|
||||
|
||||
uint8_t bools = encode_key_modifier_state(p_event);
|
||||
*data = bools;
|
||||
data++;
|
||||
data += encode_vector2(p_event->get_position(), data);
|
||||
data += encode_float(p_event->get_factor(), data);
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - r_data.ptrw() >= r_data.size());
|
||||
}
|
||||
|
||||
void decode_input_event_gesture_magnify(const PackedByteArray &p_data, Ref<InputEventMagnifyGesture> &r_event) {
|
||||
const uint8_t *data = p_data.ptr();
|
||||
DEV_ASSERT(static_cast<InputEventType>(*data) == InputEventType::MAGNIFY_GESTURE);
|
||||
data++; // Skip event type.
|
||||
|
||||
uint8_t bools = *data;
|
||||
data++;
|
||||
decode_key_modifier_state(bools, r_event);
|
||||
|
||||
Vector2 pos;
|
||||
data = decode_vector2(pos, data);
|
||||
r_event->set_position(pos);
|
||||
r_event->set_factor(decode_float(data));
|
||||
data += sizeof(float);
|
||||
|
||||
// Assert we had enough space.
|
||||
DEV_ASSERT(data - p_data.ptr() >= p_data.size());
|
||||
}
|
||||
|
||||
bool encode_input_event(const Ref<InputEvent> &p_event, PackedByteArray &r_data) {
|
||||
switch (p_event->get_type()) {
|
||||
case InputEventType::KEY:
|
||||
encode_input_event_key(p_event, r_data);
|
||||
break;
|
||||
case InputEventType::MOUSE_BUTTON:
|
||||
encode_input_event_mouse_button(p_event, r_data);
|
||||
break;
|
||||
case InputEventType::MOUSE_MOTION:
|
||||
encode_input_event_mouse_motion(p_event, r_data);
|
||||
break;
|
||||
case InputEventType::JOY_MOTION:
|
||||
encode_input_event_joypad_motion(p_event, r_data);
|
||||
break;
|
||||
case InputEventType::JOY_BUTTON:
|
||||
encode_input_event_joypad_button(p_event, r_data);
|
||||
break;
|
||||
case InputEventType::MAGNIFY_GESTURE:
|
||||
encode_input_event_gesture_magnify(p_event, r_data);
|
||||
break;
|
||||
case InputEventType::PAN_GESTURE:
|
||||
encode_input_event_gesture_pan(p_event, r_data);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void decode_input_event(const PackedByteArray &p_data, Ref<InputEvent> &r_event) {
|
||||
const uint8_t *data = p_data.ptr();
|
||||
|
||||
switch (static_cast<InputEventType>(*data)) {
|
||||
case InputEventType::KEY: {
|
||||
Ref<InputEventKey> event;
|
||||
event.instantiate();
|
||||
decode_input_event_key(p_data, event);
|
||||
r_event = event;
|
||||
} break;
|
||||
case InputEventType::MOUSE_BUTTON: {
|
||||
Ref<InputEventMouseButton> event;
|
||||
event.instantiate();
|
||||
decode_input_event_mouse_button(p_data, event);
|
||||
r_event = event;
|
||||
} break;
|
||||
case InputEventType::MOUSE_MOTION: {
|
||||
Ref<InputEventMouseMotion> event;
|
||||
event.instantiate();
|
||||
decode_input_event_mouse_motion(p_data, event);
|
||||
r_event = event;
|
||||
} break;
|
||||
case InputEventType::JOY_BUTTON: {
|
||||
Ref<InputEventJoypadButton> event;
|
||||
event.instantiate();
|
||||
decode_input_event_joypad_button(p_data, event);
|
||||
r_event = event;
|
||||
} break;
|
||||
case InputEventType::JOY_MOTION: {
|
||||
Ref<InputEventJoypadMotion> event;
|
||||
event.instantiate();
|
||||
decode_input_event_joypad_motion(p_data, event);
|
||||
r_event = event;
|
||||
} break;
|
||||
case InputEventType::PAN_GESTURE: {
|
||||
Ref<InputEventPanGesture> event;
|
||||
event.instantiate();
|
||||
decode_input_event_gesture_pan(p_data, event);
|
||||
r_event = event;
|
||||
} break;
|
||||
case InputEventType::MAGNIFY_GESTURE: {
|
||||
Ref<InputEventMagnifyGesture> event;
|
||||
event.instantiate();
|
||||
decode_input_event_gesture_magnify(p_data, event);
|
||||
r_event = event;
|
||||
} break;
|
||||
default: {
|
||||
WARN_PRINT(vformat("Unknown event type %d.", static_cast<int>(*data)));
|
||||
} break;
|
||||
}
|
||||
}
|
49
core/input/input_event_codec.h
Normal file
49
core/input/input_event_codec.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/**************************************************************************/
|
||||
/* input_event_codec.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/input/input_event.h"
|
||||
|
||||
/**
|
||||
* Encodes the input event as a byte array.
|
||||
*
|
||||
* Returns `true` if the event was successfully encoded, `false` otherwise.
|
||||
*/
|
||||
bool encode_input_event(const Ref<InputEvent> &p_event, PackedByteArray &r_data);
|
||||
void decode_input_event(const PackedByteArray &p_data, Ref<InputEvent> &r_event);
|
||||
|
||||
void encode_input_event_key(const Ref<InputEventKey> &p_event, PackedByteArray &r_data);
|
||||
void encode_input_event_mouse_button(const Ref<InputEventMouseButton> &p_event, PackedByteArray &r_data);
|
||||
void encode_input_event_mouse_motion(const Ref<InputEventMouseMotion> &p_event, PackedByteArray &r_data);
|
||||
void encode_input_event_joypad_button(const Ref<InputEventJoypadButton> &p_event, PackedByteArray &r_data);
|
||||
void encode_input_event_joypad_motion(const Ref<InputEventJoypadMotion> &p_event, PackedByteArray &r_data);
|
||||
void encode_input_event_gesture_pan(const Ref<InputEventPanGesture> &p_event, PackedByteArray &r_data);
|
||||
void encode_input_event_gesture_magnify(const Ref<InputEventMagnifyGesture> &p_event, PackedByteArray &r_data);
|
41
core/input/input_map.compat.inc
Normal file
41
core/input/input_map.compat.inc
Normal file
@@ -0,0 +1,41 @@
|
||||
/**************************************************************************/
|
||||
/* input_map.compat.inc */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
|
||||
void InputMap::_add_action_bind_compat_97281(const StringName &p_action, float p_deadzone) {
|
||||
add_action(p_action, p_deadzone);
|
||||
}
|
||||
|
||||
void InputMap::_bind_compatibility_methods() {
|
||||
ClassDB::bind_compatibility_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::_add_action_bind_compat_97281, DEFVAL(0.5f));
|
||||
}
|
||||
|
||||
#endif // DISABLE_DEPRECATED
|
905
core/input/input_map.cpp
Normal file
905
core/input/input_map.cpp
Normal file
@@ -0,0 +1,905 @@
|
||||
/**************************************************************************/
|
||||
/* input_map.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "input_map.h"
|
||||
#include "input_map.compat.inc"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/input/input.h"
|
||||
#include "core/os/keyboard.h"
|
||||
#include "core/os/os.h"
|
||||
#include "core/variant/typed_array.h"
|
||||
|
||||
void InputMap::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
|
||||
ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::get_actions);
|
||||
ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(DEFAULT_DEADZONE));
|
||||
ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_action_description", "action"), &InputMap::get_action_description);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("action_set_deadzone", "action", "deadzone"), &InputMap::action_set_deadzone);
|
||||
ClassDB::bind_method(D_METHOD("action_get_deadzone", "action"), &InputMap::action_get_deadzone);
|
||||
ClassDB::bind_method(D_METHOD("action_add_event", "action", "event"), &InputMap::action_add_event);
|
||||
ClassDB::bind_method(D_METHOD("action_has_event", "action", "event"), &InputMap::action_has_event);
|
||||
ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
|
||||
ClassDB::bind_method(D_METHOD("action_erase_events", "action"), &InputMap::action_erase_events);
|
||||
ClassDB::bind_method(D_METHOD("action_get_events", "action"), &InputMap::_action_get_events);
|
||||
ClassDB::bind_method(D_METHOD("event_is_action", "event", "action", "exact_match"), &InputMap::event_is_action, DEFVAL(false));
|
||||
ClassDB::bind_method(D_METHOD("load_from_project_settings"), &InputMap::load_from_project_settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an nonexistent action error message with a suggestion of the closest
|
||||
* matching action name (if possible).
|
||||
*/
|
||||
String InputMap::suggest_actions(const StringName &p_action) const {
|
||||
StringName closest_action;
|
||||
float closest_similarity = 0.0;
|
||||
|
||||
// Find the most action with the most similar name.
|
||||
for (const KeyValue<StringName, Action> &kv : input_map) {
|
||||
const float similarity = String(kv.key).similarity(p_action);
|
||||
|
||||
if (similarity > closest_similarity) {
|
||||
closest_action = kv.key;
|
||||
closest_similarity = similarity;
|
||||
}
|
||||
}
|
||||
|
||||
String error_message = vformat("The InputMap action \"%s\" doesn't exist.", p_action);
|
||||
|
||||
if (closest_similarity >= 0.4) {
|
||||
// Only include a suggestion in the error message if it's similar enough.
|
||||
error_message += vformat(" Did you mean \"%s\"?", closest_action);
|
||||
}
|
||||
return error_message;
|
||||
}
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
void InputMap::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
|
||||
const String pf = p_function;
|
||||
bool first_argument_is_action = false;
|
||||
if (p_idx == 0) {
|
||||
first_argument_is_action = (pf == "has_action" || pf == "erase_action" ||
|
||||
pf == "action_set_deadzone" || pf == "action_get_deadzone" ||
|
||||
pf == "action_has_event" || pf == "action_add_event" || pf == "action_get_events" ||
|
||||
pf == "action_erase_event" || pf == "action_erase_events");
|
||||
}
|
||||
if (first_argument_is_action || (p_idx == 1 && pf == "event_is_action")) {
|
||||
// Cannot rely on `get_actions()`, otherwise the actions would be in the context of the Editor (no user-defined actions).
|
||||
List<PropertyInfo> pinfo;
|
||||
ProjectSettings::get_singleton()->get_property_list(&pinfo);
|
||||
|
||||
for (const PropertyInfo &pi : pinfo) {
|
||||
if (!pi.name.begins_with("input/")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String name = pi.name.substr(pi.name.find_char('/') + 1);
|
||||
r_options->push_back(name.quote());
|
||||
}
|
||||
}
|
||||
|
||||
Object::get_argument_options(p_function, p_idx, r_options);
|
||||
}
|
||||
#endif
|
||||
|
||||
void InputMap::add_action(const StringName &p_action, float p_deadzone) {
|
||||
ERR_FAIL_COND_MSG(input_map.has(p_action), vformat("InputMap already has action \"%s\".", String(p_action)));
|
||||
input_map[p_action] = Action();
|
||||
static int last_id = 1;
|
||||
input_map[p_action].id = last_id;
|
||||
input_map[p_action].deadzone = p_deadzone;
|
||||
last_id++;
|
||||
}
|
||||
|
||||
void InputMap::erase_action(const StringName &p_action) {
|
||||
ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
|
||||
|
||||
input_map.erase(p_action);
|
||||
}
|
||||
|
||||
TypedArray<StringName> InputMap::get_actions() {
|
||||
TypedArray<StringName> ret;
|
||||
|
||||
ret.resize(input_map.size());
|
||||
|
||||
uint32_t i = 0;
|
||||
for (const KeyValue<StringName, Action> &kv : input_map) {
|
||||
ret[i] = kv.key;
|
||||
i++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
List<Ref<InputEvent>>::Element *InputMap::_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const {
|
||||
ERR_FAIL_COND_V(p_event.is_null(), nullptr);
|
||||
|
||||
int i = 0;
|
||||
for (List<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
|
||||
int device = E->get()->get_device();
|
||||
if (device == ALL_DEVICES || device == p_event->get_device()) {
|
||||
if (E->get()->action_match(p_event, p_exact_match, p_action.deadzone, r_pressed, r_strength, r_raw_strength)) {
|
||||
if (r_event_index) {
|
||||
*r_event_index = i;
|
||||
}
|
||||
return E;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool InputMap::has_action(const StringName &p_action) const {
|
||||
return input_map.has(p_action);
|
||||
}
|
||||
|
||||
String InputMap::get_action_description(const StringName &p_action) const {
|
||||
ERR_FAIL_COND_V_MSG(!input_map.has(p_action), String(), suggest_actions(p_action));
|
||||
|
||||
String ret;
|
||||
const List<Ref<InputEvent>> &inputs = input_map[p_action].inputs;
|
||||
for (Ref<InputEventKey> iek : inputs) {
|
||||
if (iek.is_valid()) {
|
||||
if (!ret.is_empty()) {
|
||||
ret += RTR(" or ");
|
||||
}
|
||||
ret += iek->as_text();
|
||||
}
|
||||
}
|
||||
if (ret.is_empty()) {
|
||||
ret = RTR("Action has no bound inputs");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
float InputMap::action_get_deadzone(const StringName &p_action) {
|
||||
ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, suggest_actions(p_action));
|
||||
|
||||
return input_map[p_action].deadzone;
|
||||
}
|
||||
|
||||
void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
|
||||
ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
|
||||
|
||||
input_map[p_action].deadzone = p_deadzone;
|
||||
}
|
||||
|
||||
void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
|
||||
ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object.");
|
||||
ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
|
||||
if (_find_event(input_map[p_action], p_event, true)) {
|
||||
return; // Already added.
|
||||
}
|
||||
|
||||
input_map[p_action].inputs.push_back(p_event);
|
||||
}
|
||||
|
||||
bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
|
||||
ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, suggest_actions(p_action));
|
||||
return (_find_event(input_map[p_action], p_event, true) != nullptr);
|
||||
}
|
||||
|
||||
void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
|
||||
ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
|
||||
|
||||
List<Ref<InputEvent>>::Element *E = _find_event(input_map[p_action], p_event, true);
|
||||
if (E) {
|
||||
input_map[p_action].inputs.erase(E);
|
||||
|
||||
if (Input::get_singleton()->is_action_pressed(p_action)) {
|
||||
Input::get_singleton()->action_release(p_action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InputMap::action_erase_events(const StringName &p_action) {
|
||||
ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
|
||||
|
||||
input_map[p_action].inputs.clear();
|
||||
}
|
||||
|
||||
TypedArray<InputEvent> InputMap::_action_get_events(const StringName &p_action) {
|
||||
TypedArray<InputEvent> ret;
|
||||
const List<Ref<InputEvent>> *al = action_get_events(p_action);
|
||||
if (al) {
|
||||
for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) {
|
||||
ret.push_back(E->get());
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
const List<Ref<InputEvent>> *InputMap::action_get_events(const StringName &p_action) {
|
||||
HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
|
||||
if (!E) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &E->value.inputs;
|
||||
}
|
||||
|
||||
bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
|
||||
return event_get_action_status(p_event, p_action, p_exact_match);
|
||||
}
|
||||
|
||||
int InputMap::event_get_index(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
|
||||
int index = -1;
|
||||
bool valid = event_get_action_status(p_event, p_action, p_exact_match, nullptr, nullptr, nullptr, &index);
|
||||
return valid ? index : -1;
|
||||
}
|
||||
|
||||
bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const {
|
||||
HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
|
||||
ERR_FAIL_COND_V_MSG(!E, false, suggest_actions(p_action));
|
||||
|
||||
Ref<InputEventAction> input_event_action = p_event;
|
||||
if (input_event_action.is_valid()) {
|
||||
const bool pressed = input_event_action->is_pressed();
|
||||
if (r_pressed != nullptr) {
|
||||
*r_pressed = pressed;
|
||||
}
|
||||
const float strength = pressed ? input_event_action->get_strength() : 0.0f;
|
||||
if (r_strength != nullptr) {
|
||||
*r_strength = strength;
|
||||
}
|
||||
if (r_raw_strength != nullptr) {
|
||||
*r_raw_strength = strength;
|
||||
}
|
||||
if (r_event_index) {
|
||||
if (input_event_action->get_event_index() >= 0) {
|
||||
*r_event_index = input_event_action->get_event_index();
|
||||
} else {
|
||||
*r_event_index = E->value.inputs.size();
|
||||
}
|
||||
}
|
||||
return input_event_action->get_action() == p_action;
|
||||
}
|
||||
|
||||
List<Ref<InputEvent>>::Element *event = _find_event(E->value, p_event, p_exact_match, r_pressed, r_strength, r_raw_strength, r_event_index);
|
||||
return event != nullptr;
|
||||
}
|
||||
|
||||
const HashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
|
||||
return input_map;
|
||||
}
|
||||
|
||||
void InputMap::load_from_project_settings() {
|
||||
input_map.clear();
|
||||
|
||||
List<PropertyInfo> pinfo;
|
||||
ProjectSettings::get_singleton()->get_property_list(&pinfo);
|
||||
|
||||
for (const PropertyInfo &pi : pinfo) {
|
||||
if (!pi.name.begins_with("input/")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String name = pi.name.substr(pi.name.find_char('/') + 1);
|
||||
|
||||
Dictionary action = GLOBAL_GET(pi.name);
|
||||
float deadzone = action.has("deadzone") ? (float)action["deadzone"] : DEFAULT_DEADZONE;
|
||||
Array events = action["events"];
|
||||
|
||||
add_action(name, deadzone);
|
||||
for (int i = 0; i < events.size(); i++) {
|
||||
Ref<InputEvent> event = events[i];
|
||||
if (event.is_null()) {
|
||||
continue;
|
||||
}
|
||||
action_add_event(name, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct _BuiltinActionDisplayName {
|
||||
const char *name;
|
||||
const char *display_name;
|
||||
};
|
||||
|
||||
static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
|
||||
/* clang-format off */
|
||||
{ "ui_accept", TTRC("Accept") },
|
||||
{ "ui_select", TTRC("Select") },
|
||||
{ "ui_cancel", TTRC("Cancel") },
|
||||
{ "ui_focus_next", TTRC("Focus Next") },
|
||||
{ "ui_focus_prev", TTRC("Focus Prev") },
|
||||
{ "ui_left", TTRC("Left") },
|
||||
{ "ui_right", TTRC("Right") },
|
||||
{ "ui_up", TTRC("Up") },
|
||||
{ "ui_down", TTRC("Down") },
|
||||
{ "ui_page_up", TTRC("Page Up") },
|
||||
{ "ui_page_down", TTRC("Page Down") },
|
||||
{ "ui_home", TTRC("Home") },
|
||||
{ "ui_end", TTRC("End") },
|
||||
{ "ui_cut", TTRC("Cut") },
|
||||
{ "ui_copy", TTRC("Copy") },
|
||||
{ "ui_paste", TTRC("Paste") },
|
||||
{ "ui_focus_mode", TTRC("Toggle Tab Focus Mode") },
|
||||
{ "ui_undo", TTRC("Undo") },
|
||||
{ "ui_redo", TTRC("Redo") },
|
||||
{ "ui_text_completion_query", TTRC("Completion Query") },
|
||||
{ "ui_text_newline", TTRC("New Line") },
|
||||
{ "ui_text_newline_blank", TTRC("New Blank Line") },
|
||||
{ "ui_text_newline_above", TTRC("New Line Above") },
|
||||
{ "ui_text_indent", TTRC("Indent") },
|
||||
{ "ui_text_dedent", TTRC("Dedent") },
|
||||
{ "ui_text_backspace", TTRC("Backspace") },
|
||||
{ "ui_text_backspace_word", TTRC("Backspace Word") },
|
||||
{ "ui_text_backspace_word.macos", TTRC("Backspace Word") },
|
||||
{ "ui_text_backspace_all_to_left", TTRC("Backspace all to Left") },
|
||||
{ "ui_text_backspace_all_to_left.macos", TTRC("Backspace all to Left") },
|
||||
{ "ui_text_delete", TTRC("Delete") },
|
||||
{ "ui_text_delete_word", TTRC("Delete Word") },
|
||||
{ "ui_text_delete_word.macos", TTRC("Delete Word") },
|
||||
{ "ui_text_delete_all_to_right", TTRC("Delete all to Right") },
|
||||
{ "ui_text_delete_all_to_right.macos", TTRC("Delete all to Right") },
|
||||
{ "ui_text_caret_left", TTRC("Caret Left") },
|
||||
{ "ui_text_caret_word_left", TTRC("Caret Word Left") },
|
||||
{ "ui_text_caret_word_left.macos", TTRC("Caret Word Left") },
|
||||
{ "ui_text_caret_right", TTRC("Caret Right") },
|
||||
{ "ui_text_caret_word_right", TTRC("Caret Word Right") },
|
||||
{ "ui_text_caret_word_right.macos", TTRC("Caret Word Right") },
|
||||
{ "ui_text_caret_up", TTRC("Caret Up") },
|
||||
{ "ui_text_caret_down", TTRC("Caret Down") },
|
||||
{ "ui_text_caret_line_start", TTRC("Caret Line Start") },
|
||||
{ "ui_text_caret_line_start.macos", TTRC("Caret Line Start") },
|
||||
{ "ui_text_caret_line_end", TTRC("Caret Line End") },
|
||||
{ "ui_text_caret_line_end.macos", TTRC("Caret Line End") },
|
||||
{ "ui_text_caret_page_up", TTRC("Caret Page Up") },
|
||||
{ "ui_text_caret_page_down", TTRC("Caret Page Down") },
|
||||
{ "ui_text_caret_document_start", TTRC("Caret Document Start") },
|
||||
{ "ui_text_caret_document_start.macos", TTRC("Caret Document Start") },
|
||||
{ "ui_text_caret_document_end", TTRC("Caret Document End") },
|
||||
{ "ui_text_caret_document_end.macos", TTRC("Caret Document End") },
|
||||
{ "ui_text_caret_add_below", TTRC("Caret Add Below") },
|
||||
{ "ui_text_caret_add_below.macos", TTRC("Caret Add Below") },
|
||||
{ "ui_text_caret_add_above", TTRC("Caret Add Above") },
|
||||
{ "ui_text_caret_add_above.macos", TTRC("Caret Add Above") },
|
||||
{ "ui_text_scroll_up", TTRC("Scroll Up") },
|
||||
{ "ui_text_scroll_up.macos", TTRC("Scroll Up") },
|
||||
{ "ui_text_scroll_down", TTRC("Scroll Down") },
|
||||
{ "ui_text_scroll_down.macos", TTRC("Scroll Down") },
|
||||
{ "ui_text_select_all", TTRC("Select All") },
|
||||
{ "ui_text_select_word_under_caret", TTRC("Select Word Under Caret") },
|
||||
{ "ui_text_add_selection_for_next_occurrence", TTRC("Add Selection for Next Occurrence") },
|
||||
{ "ui_text_skip_selection_for_next_occurrence", TTRC("Skip Selection for Next Occurrence") },
|
||||
{ "ui_text_clear_carets_and_selection", TTRC("Clear Carets and Selection") },
|
||||
{ "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") },
|
||||
{ "ui_text_submit", TTRC("Submit Text") },
|
||||
{ "ui_graph_duplicate", TTRC("Duplicate Nodes") },
|
||||
{ "ui_graph_delete", TTRC("Delete Nodes") },
|
||||
{ "ui_graph_follow_left", TTRC("Follow Input Port Connection") },
|
||||
{ "ui_graph_follow_right", TTRC("Follow Output Port Connection") },
|
||||
{ "ui_filedialog_up_one_level", TTRC("Go Up One Level") },
|
||||
{ "ui_filedialog_refresh", TTRC("Refresh") },
|
||||
{ "ui_filedialog_show_hidden", TTRC("Show Hidden") },
|
||||
{ "ui_swap_input_direction ", TTRC("Swap Input Direction") },
|
||||
{ "ui_unicode_start", TTRC("Start Unicode Character Input") },
|
||||
{ "ui_colorpicker_delete_preset", TTRC("ColorPicker: Delete Preset") },
|
||||
{ "ui_accessibility_drag_and_drop", TTRC("Accessibility: Keyboard Drag and Drop") },
|
||||
{ "", ""}
|
||||
/* clang-format on */
|
||||
};
|
||||
|
||||
String InputMap::get_builtin_display_name(const String &p_name) const {
|
||||
constexpr int len = std::size(_builtin_action_display_names);
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (_builtin_action_display_names[i].name == p_name) {
|
||||
return _builtin_action_display_names[i].display_name;
|
||||
}
|
||||
}
|
||||
|
||||
return p_name;
|
||||
}
|
||||
|
||||
const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
|
||||
// Return cache if it has already been built.
|
||||
if (default_builtin_cache.size()) {
|
||||
return default_builtin_cache;
|
||||
}
|
||||
|
||||
List<Ref<InputEvent>> inputs;
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ENTER));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::SPACE));
|
||||
default_builtin_cache.insert("ui_accept", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::Y));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::SPACE));
|
||||
default_builtin_cache.insert("ui_select", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
|
||||
default_builtin_cache.insert("ui_cancel", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::TAB));
|
||||
default_builtin_cache.insert("ui_focus_next", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
|
||||
default_builtin_cache.insert("ui_focus_prev", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::LEFT));
|
||||
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_LEFT));
|
||||
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, -1.0));
|
||||
default_builtin_cache.insert("ui_left", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
|
||||
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_RIGHT));
|
||||
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, 1.0));
|
||||
default_builtin_cache.insert("ui_right", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::UP));
|
||||
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_UP));
|
||||
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, -1.0));
|
||||
default_builtin_cache.insert("ui_up", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::DOWN));
|
||||
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_DOWN));
|
||||
inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, 1.0));
|
||||
default_builtin_cache.insert("ui_down", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
|
||||
default_builtin_cache.insert("ui_page_up", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
|
||||
default_builtin_cache.insert("ui_page_down", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::HOME));
|
||||
default_builtin_cache.insert("ui_home", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::END));
|
||||
default_builtin_cache.insert("ui_end", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
default_builtin_cache.insert("ui_accessibility_drag_and_drop", inputs);
|
||||
|
||||
// ///// UI basic Shortcuts /////
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::X | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::SHIFT));
|
||||
default_builtin_cache.insert("ui_cut", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::C | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_copy", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::M | KeyModifierMask::CTRL));
|
||||
default_builtin_cache.insert("ui_focus_mode", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::V | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::SHIFT));
|
||||
default_builtin_cache.insert("ui_paste", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_undo", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::Y | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_redo", inputs);
|
||||
|
||||
// ///// UI Text Input Shortcuts /////
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::SPACE | KeyModifierMask::CTRL));
|
||||
default_builtin_cache.insert("ui_text_completion_query", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(KeyModifierMask::SHIFT | Key::TAB));
|
||||
inputs.push_back(InputEventKey::create_reference(KeyModifierMask::SHIFT | Key::ENTER));
|
||||
inputs.push_back(InputEventKey::create_reference(KeyModifierMask::SHIFT | Key::KP_ENTER));
|
||||
default_builtin_cache.insert("ui_text_completion_accept", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::TAB));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ENTER));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
|
||||
default_builtin_cache.insert("ui_text_completion_replace", inputs);
|
||||
|
||||
// Newlines
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ENTER));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
|
||||
default_builtin_cache.insert("ui_text_newline", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_newline_blank", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_newline_above", inputs);
|
||||
|
||||
// Indentation
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::TAB));
|
||||
default_builtin_cache.insert("ui_text_indent", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
|
||||
default_builtin_cache.insert("ui_text_dedent", inputs);
|
||||
|
||||
// Text Backspace and Delete
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::SHIFT));
|
||||
default_builtin_cache.insert("ui_text_backspace", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_backspace_word", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_backspace_word.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_backspace_all_to_left.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
|
||||
default_builtin_cache.insert("ui_text_delete", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_delete_word", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_delete_word.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
default_builtin_cache.insert("ui_text_delete_all_to_right", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_delete_all_to_right.macos", inputs);
|
||||
|
||||
// Text Caret Movement Left/Right
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::LEFT));
|
||||
default_builtin_cache.insert("ui_text_caret_left", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_word_left", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_caret_word_left.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
|
||||
default_builtin_cache.insert("ui_text_caret_right", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_word_right", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_caret_word_right.macos", inputs);
|
||||
|
||||
// Text Caret Movement Up/Down
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::UP));
|
||||
default_builtin_cache.insert("ui_text_caret_up", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::DOWN));
|
||||
default_builtin_cache.insert("ui_text_caret_down", inputs);
|
||||
|
||||
// Text Caret Movement Line Start/End
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::HOME));
|
||||
default_builtin_cache.insert("ui_text_caret_line_start", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::HOME));
|
||||
default_builtin_cache.insert("ui_text_caret_line_start.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::END));
|
||||
default_builtin_cache.insert("ui_text_caret_line_end", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::E | KeyModifierMask::CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::END));
|
||||
default_builtin_cache.insert("ui_text_caret_line_end.macos", inputs);
|
||||
|
||||
// Text Caret Movement Page Up/Down
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
|
||||
default_builtin_cache.insert("ui_text_caret_page_up", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
|
||||
default_builtin_cache.insert("ui_text_caret_page_down", inputs);
|
||||
|
||||
// Text Caret Movement Document Start/End
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_document_start", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_document_start.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_document_end", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_document_end.macos", inputs);
|
||||
|
||||
// Text Caret Addition Below/Above
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_add_below", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::L | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_add_below.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_add_above", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::O | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_caret_add_above.macos", inputs);
|
||||
|
||||
// Text Scrolling
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_scroll_up", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_scroll_up.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_scroll_down", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_scroll_down.macos", inputs);
|
||||
|
||||
// Text Misc
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_select_all", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_select_word_under_caret", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::CTRL | KeyModifierMask::META));
|
||||
default_builtin_cache.insert("ui_text_select_word_under_caret.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_text_add_selection_for_next_occurrence", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_text_skip_selection_for_next_occurrence", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
|
||||
default_builtin_cache.insert("ui_text_clear_carets_and_selection", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::INSERT));
|
||||
default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::MENU));
|
||||
default_builtin_cache.insert("ui_menu", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::ENTER));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
|
||||
default_builtin_cache.insert("ui_text_submit", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::U | KeyModifierMask::CTRL | KeyModifierMask::SHIFT));
|
||||
default_builtin_cache.insert("ui_unicode_start", inputs);
|
||||
|
||||
// ///// UI Graph Shortcuts /////
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_graph_duplicate", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
|
||||
default_builtin_cache.insert("ui_graph_delete", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_graph_follow_left", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_graph_follow_left.macos", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_graph_follow_right", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::ALT));
|
||||
default_builtin_cache.insert("ui_graph_follow_right.macos", inputs);
|
||||
|
||||
// ///// UI File Dialog Shortcuts /////
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
|
||||
default_builtin_cache.insert("ui_filedialog_up_one_level", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::F5));
|
||||
default_builtin_cache.insert("ui_filedialog_refresh", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::H));
|
||||
default_builtin_cache.insert("ui_filedialog_show_hidden", inputs);
|
||||
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventKey::create_reference(Key::QUOTELEFT | KeyModifierMask::CMD_OR_CTRL));
|
||||
default_builtin_cache.insert("ui_swap_input_direction", inputs);
|
||||
|
||||
// ///// UI ColorPicker Shortcuts /////
|
||||
inputs = List<Ref<InputEvent>>();
|
||||
inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::X));
|
||||
inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
|
||||
default_builtin_cache.insert("ui_colorpicker_delete_preset", inputs);
|
||||
|
||||
return default_builtin_cache;
|
||||
}
|
||||
|
||||
const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with_feature_overrides_applied() {
|
||||
if (default_builtin_with_overrides_cache.size() > 0) {
|
||||
return default_builtin_with_overrides_cache;
|
||||
}
|
||||
|
||||
const HashMap<String, List<Ref<InputEvent>>> &builtins = get_builtins();
|
||||
|
||||
// Get a list of all built in inputs which are valid overrides for the OS
|
||||
// Key = builtin name (e.g. ui_accept)
|
||||
// Value = override/feature names (e.g. macos, if it was defined as "ui_accept.macos" and the platform supports that feature)
|
||||
HashMap<String, Vector<String>> builtins_with_overrides;
|
||||
for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
|
||||
String fullname = E.key;
|
||||
|
||||
Vector<String> split = fullname.split(".");
|
||||
const String &name = split[0];
|
||||
String override_for = split.size() > 1 ? split[1] : String();
|
||||
|
||||
if (!override_for.is_empty() && OS::get_singleton()->has_feature(override_for)) {
|
||||
builtins_with_overrides[name].push_back(override_for);
|
||||
}
|
||||
}
|
||||
|
||||
for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
|
||||
String fullname = E.key;
|
||||
|
||||
Vector<String> split = fullname.split(".");
|
||||
const String &name = split[0];
|
||||
String override_for = split.size() > 1 ? split[1] : String();
|
||||
|
||||
if (builtins_with_overrides.has(name) && override_for.is_empty()) {
|
||||
// Builtin has an override but this particular one is not an override, so skip.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!override_for.is_empty() && !OS::get_singleton()->has_feature(override_for)) {
|
||||
// OS does not support this override - skip.
|
||||
continue;
|
||||
}
|
||||
|
||||
default_builtin_with_overrides_cache.insert(name, E.value);
|
||||
}
|
||||
|
||||
return default_builtin_with_overrides_cache;
|
||||
}
|
||||
|
||||
void InputMap::load_default() {
|
||||
HashMap<String, List<Ref<InputEvent>>> builtins = get_builtins_with_feature_overrides_applied();
|
||||
|
||||
for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
|
||||
String name = E.key;
|
||||
|
||||
add_action(name);
|
||||
|
||||
const List<Ref<InputEvent>> &inputs = E.value;
|
||||
for (const List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
|
||||
Ref<InputEventKey> iek = I->get();
|
||||
|
||||
// For the editor, only add keyboard actions.
|
||||
if (iek.is_valid()) {
|
||||
action_add_event(name, I->get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InputMap::InputMap() {
|
||||
ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exists.");
|
||||
singleton = this;
|
||||
}
|
||||
|
||||
InputMap::~InputMap() {
|
||||
singleton = nullptr;
|
||||
}
|
117
core/input/input_map.h
Normal file
117
core/input/input_map.h
Normal file
@@ -0,0 +1,117 @@
|
||||
/**************************************************************************/
|
||||
/* input_map.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/input/input_event.h"
|
||||
#include "core/object/object.h"
|
||||
#include "core/templates/hash_map.h"
|
||||
|
||||
template <typename T>
|
||||
class TypedArray;
|
||||
|
||||
class InputMap : public Object {
|
||||
GDCLASS(InputMap, Object);
|
||||
|
||||
public:
|
||||
/**
|
||||
* A special value used to signify that a given Action can be triggered by any device
|
||||
*/
|
||||
static constexpr int ALL_DEVICES = -1;
|
||||
|
||||
struct Action {
|
||||
int id;
|
||||
float deadzone;
|
||||
List<Ref<InputEvent>> inputs;
|
||||
};
|
||||
|
||||
static constexpr float DEFAULT_DEADZONE = 0.2f;
|
||||
// Keep bigger deadzone for toggle actions (default `ui_*` actions, axis `pressed`) (GH-103360).
|
||||
static constexpr float DEFAULT_TOGGLE_DEADZONE = 0.5f;
|
||||
|
||||
private:
|
||||
static inline InputMap *singleton = nullptr;
|
||||
|
||||
mutable HashMap<StringName, Action> input_map;
|
||||
HashMap<String, List<Ref<InputEvent>>> default_builtin_cache;
|
||||
HashMap<String, List<Ref<InputEvent>>> default_builtin_with_overrides_cache;
|
||||
|
||||
List<Ref<InputEvent>>::Element *_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr, int *r_event_index = nullptr) const;
|
||||
|
||||
TypedArray<InputEvent> _action_get_events(const StringName &p_action);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
void _add_action_bind_compat_97281(const StringName &p_action, float p_deadzone = 0.5);
|
||||
static void _bind_compatibility_methods();
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
public:
|
||||
static _FORCE_INLINE_ InputMap *get_singleton() { return singleton; }
|
||||
|
||||
bool has_action(const StringName &p_action) const;
|
||||
TypedArray<StringName> get_actions();
|
||||
void add_action(const StringName &p_action, float p_deadzone = DEFAULT_DEADZONE);
|
||||
void erase_action(const StringName &p_action);
|
||||
|
||||
String get_action_description(const StringName &p_action) const;
|
||||
|
||||
float action_get_deadzone(const StringName &p_action);
|
||||
void action_set_deadzone(const StringName &p_action, float p_deadzone);
|
||||
void action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event);
|
||||
bool action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event);
|
||||
void action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event);
|
||||
void action_erase_events(const StringName &p_action);
|
||||
|
||||
const List<Ref<InputEvent>> *action_get_events(const StringName &p_action);
|
||||
bool event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false) const;
|
||||
int event_get_index(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false) const;
|
||||
bool event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr, int *r_event_index = nullptr) const;
|
||||
|
||||
const HashMap<StringName, Action> &get_action_map() const;
|
||||
void load_from_project_settings();
|
||||
void load_default();
|
||||
|
||||
String suggest_actions(const StringName &p_action) const;
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
|
||||
#endif
|
||||
|
||||
String get_builtin_display_name(const String &p_name) const;
|
||||
// Use an Ordered Map so insertion order is preserved. We want the elements to be 'grouped' somewhat.
|
||||
const HashMap<String, List<Ref<InputEvent>>> &get_builtins();
|
||||
const HashMap<String, List<Ref<InputEvent>>> &get_builtins_with_feature_overrides_applied();
|
||||
|
||||
InputMap();
|
||||
~InputMap();
|
||||
};
|
131
core/input/shortcut.cpp
Normal file
131
core/input/shortcut.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/**************************************************************************/
|
||||
/* shortcut.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "shortcut.h"
|
||||
|
||||
void Shortcut::set_events(const Array &p_events) {
|
||||
for (int i = 0; i < p_events.size(); i++) {
|
||||
Ref<InputEventShortcut> ies = p_events[i];
|
||||
ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut.");
|
||||
}
|
||||
|
||||
events = p_events;
|
||||
emit_changed();
|
||||
}
|
||||
|
||||
void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) {
|
||||
events.clear();
|
||||
|
||||
for (const Ref<InputEvent> &ie : *p_events) {
|
||||
events.push_back(ie);
|
||||
}
|
||||
}
|
||||
|
||||
Array Shortcut::get_events() const {
|
||||
return events;
|
||||
}
|
||||
|
||||
bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {
|
||||
Ref<InputEventShortcut> ies = p_event;
|
||||
if (ies.is_valid()) {
|
||||
if (ies->get_shortcut().ptr() == this) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < events.size(); i++) {
|
||||
Ref<InputEvent> ie = events[i];
|
||||
bool valid = ie.is_valid() && ie->is_match(p_event);
|
||||
|
||||
// Stop on first valid event - don't need to check further.
|
||||
if (valid) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
String Shortcut::get_as_text() const {
|
||||
for (int i = 0; i < events.size(); i++) {
|
||||
Ref<InputEvent> ie = events[i];
|
||||
// Return first shortcut which is valid
|
||||
if (ie.is_valid()) {
|
||||
return ie->as_text();
|
||||
}
|
||||
}
|
||||
|
||||
return "None";
|
||||
}
|
||||
|
||||
bool Shortcut::has_valid_event() const {
|
||||
// Tests if there is ANY input event which is valid.
|
||||
for (int i = 0; i < events.size(); i++) {
|
||||
Ref<InputEvent> ie = events[i];
|
||||
if (ie.is_valid()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Shortcut::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events);
|
||||
ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);
|
||||
ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("InputEvent")), "set_events", "get_events");
|
||||
}
|
||||
|
||||
bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) {
|
||||
if (p_event_array1.size() != p_event_array2.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_same = true;
|
||||
for (int i = 0; i < p_event_array1.size(); i++) {
|
||||
Ref<InputEvent> ie_1 = p_event_array1[i];
|
||||
Ref<InputEvent> ie_2 = p_event_array2[i];
|
||||
|
||||
is_same = ie_1->is_match(ie_2);
|
||||
|
||||
// Break on the first that doesn't match - don't need to check further.
|
||||
if (!is_same) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return is_same;
|
||||
}
|
56
core/input/shortcut.h
Normal file
56
core/input/shortcut.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/**************************************************************************/
|
||||
/* shortcut.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/input/input_event.h"
|
||||
#include "core/io/resource.h"
|
||||
|
||||
class Shortcut : public Resource {
|
||||
GDCLASS(Shortcut, Resource);
|
||||
|
||||
Array events;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_events(const Array &p_events);
|
||||
Array get_events() const;
|
||||
|
||||
void set_events_list(const List<Ref<InputEvent>> *p_events);
|
||||
|
||||
bool matches_event(const Ref<InputEvent> &p_event) const;
|
||||
bool has_valid_event() const;
|
||||
|
||||
String get_as_text() const;
|
||||
|
||||
static bool is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2);
|
||||
};
|
Reference in New Issue
Block a user