Skip to content

Add iOS virtual controller support #97530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,13 @@ ProjectSettings::ProjectSettings() {
GLOBAL_DEF_BASIC("input_devices/pointing/android/enable_pan_and_scale_gestures", false);
GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "input_devices/pointing/android/rotary_input_scroll_axis", PROPERTY_HINT_ENUM, "Horizontal,Vertical"), 1);
GLOBAL_DEF("input_devices/pointing/android/override_volume_buttons", false);
GLOBAL_DEF_BASIC("input_devices/virtual_controller/ios/enable_controller", false);
GLOBAL_DEF_BASIC("input_devices/virtual_controller/ios/enable_left_thumbstick", true);
GLOBAL_DEF_BASIC("input_devices/virtual_controller/ios/enable_right_thumbstick", true);
GLOBAL_DEF_BASIC("input_devices/virtual_controller/ios/enable_button_a", true);
GLOBAL_DEF_BASIC("input_devices/virtual_controller/ios/enable_button_b", true);
GLOBAL_DEF_BASIC("input_devices/virtual_controller/ios/enable_button_x", true);
GLOBAL_DEF_BASIC("input_devices/virtual_controller/ios/enable_button_y", true);

// These properties will not show up in the dialog. If you want to exclude whole groups, use add_hidden_prefix().
GLOBAL_DEF_INTERNAL("application/config/features", PackedStringArray());
Expand Down
5 changes: 5 additions & 0 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void Input::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_emulating_mouse_from_touch"), &Input::is_emulating_mouse_from_touch);
ClassDB::bind_method(D_METHOD("set_emulate_touch_from_mouse", "enable"), &Input::set_emulate_touch_from_mouse);
ClassDB::bind_method(D_METHOD("is_emulating_touch_from_mouse"), &Input::is_emulating_touch_from_mouse);
ClassDB::bind_method(D_METHOD("get_virtual_controller"), &Input::get_virtual_controller);

ADD_PROPERTY(PropertyInfo(Variant::INT, "mouse_mode"), "set_mouse_mode", "get_mouse_mode");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_accumulated_input"), "set_use_accumulated_input", "is_using_accumulated_input");
Expand Down Expand Up @@ -1056,6 +1057,10 @@ bool Input::is_emulating_touch_from_mouse() const {
return emulate_touch_from_mouse;
}

VirtualController *Input::get_virtual_controller() {
return OS::get_singleton()->get_virtual_controller();
}

// Calling this whenever the game window is focused helps unsticking the "touch mouse"
// if the OS or its abstraction class hasn't properly reported that touch pointers raised
void Input::ensure_touch_mouse_raised() {
Expand Down
3 changes: 3 additions & 0 deletions core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#pragma once

#include "core/input/input_event.h"
#include "core/input/virtual_controller.h"
#include "core/object/object.h"
#include "core/os/keyboard.h"
#include "core/os/thread_safe.h"
Expand Down Expand Up @@ -389,6 +390,8 @@ class Input : public Object {
void set_use_accumulated_input(bool p_enable);
bool is_using_accumulated_input();

VirtualController *get_virtual_controller();

void release_pressed_events();

void set_event_dispatch_function(EventDispatchFunc p_function);
Expand Down
49 changes: 49 additions & 0 deletions core/input/virtual_controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**************************************************************************/
/* virtual_controller.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 "virtual_controller.h"

void VirtualController::_bind_methods() {
ClassDB::bind_method(D_METHOD("enable"), &VirtualController::enable);
ClassDB::bind_method(D_METHOD("disable"), &VirtualController::disable);
ClassDB::bind_method(D_METHOD("is_enabled"), &VirtualController::is_enabled);
ClassDB::bind_method(D_METHOD("set_enabled_left_thumbstick", "enable"), &VirtualController::set_enabled_left_thumbstick);
ClassDB::bind_method(D_METHOD("is_enabled_left_thumbstick"), &VirtualController::is_enabled_left_thumbstick);
ClassDB::bind_method(D_METHOD("set_enabled_right_thumbstick", "enable"), &VirtualController::set_enabled_right_thumbstick);
ClassDB::bind_method(D_METHOD("is_enabled_right_thumbstick"), &VirtualController::is_enabled_right_thumbstick);
ClassDB::bind_method(D_METHOD("set_enabled_button_a", "enable"), &VirtualController::set_enabled_button_a);
ClassDB::bind_method(D_METHOD("is_enabled_button_a"), &VirtualController::is_enabled_button_a);
ClassDB::bind_method(D_METHOD("set_enabled_button_b", "enable"), &VirtualController::set_enabled_button_b);
ClassDB::bind_method(D_METHOD("is_enabled_button_b"), &VirtualController::is_enabled_button_b);
ClassDB::bind_method(D_METHOD("set_enabled_button_x", "enable"), &VirtualController::set_enabled_button_x);
ClassDB::bind_method(D_METHOD("is_enabled_button_x"), &VirtualController::is_enabled_button_x);
ClassDB::bind_method(D_METHOD("set_enabled_button_y", "enable"), &VirtualController::set_enabled_button_y);
ClassDB::bind_method(D_METHOD("is_enabled_button_y"), &VirtualController::is_enabled_button_y);
}
57 changes: 57 additions & 0 deletions core/input/virtual_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**************************************************************************/
/* virtual_controller.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/object/class_db.h"

class VirtualController : public Object {
GDCLASS(VirtualController, Object);

protected:
static void _bind_methods();

public:
virtual void enable() = 0;
virtual void disable() = 0;
virtual bool is_enabled() = 0;
virtual void set_enabled_left_thumbstick(bool p_enabled) = 0;
virtual bool is_enabled_left_thumbstick() = 0;
virtual void set_enabled_right_thumbstick(bool p_enabled) = 0;
virtual bool is_enabled_right_thumbstick() = 0;
virtual void set_enabled_button_a(bool p_enabled) = 0;
virtual bool is_enabled_button_a() = 0;
virtual void set_enabled_button_b(bool p_enabled) = 0;
virtual bool is_enabled_button_b() = 0;
virtual void set_enabled_button_x(bool p_enabled) = 0;
virtual bool is_enabled_button_x() = 0;
virtual void set_enabled_button_y(bool p_enabled) = 0;
virtual bool is_enabled_button_y() = 0;
};
10 changes: 10 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,16 @@ void OS::benchmark_dump() {
#endif
}

VirtualController *OS::get_virtual_controller() const {
return nullptr;
}

void OS::controller_connected() const {
}

void OS::controller_disconnected() const {
}

OS::OS() {
singleton = this;

Expand Down
7 changes: 7 additions & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#pragma once

#include "core/config/engine.h"
#include "core/input/virtual_controller.h"
#include "core/io/logger.h"
#include "core/io/remote_filesystem_client.h"
#include "core/os/time_enums.h"
Expand Down Expand Up @@ -365,6 +366,12 @@ class OS {
virtual bool _test_create_rendering_device_and_gl(const String &p_display_driver) const { return true; }
virtual bool _test_create_rendering_device(const String &p_display_driver) const { return true; }

virtual VirtualController *get_virtual_controller() const;

virtual void controller_connected() const;

virtual void controller_disconnected() const;

OS();
virtual ~OS();
};
3 changes: 3 additions & 0 deletions core/register_core_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "core/input/input.h"
#include "core/input/input_map.h"
#include "core/input/shortcut.h"
#include "core/input/virtual_controller.h"
#include "core/io/config_file.h"
#include "core/io/dir_access.h"
#include "core/io/dtls_server.h"
Expand Down Expand Up @@ -296,6 +297,8 @@ void register_core_types() {
GDREGISTER_NATIVE_STRUCT(AudioFrame, "float left;float right");
GDREGISTER_NATIVE_STRUCT(ScriptLanguageExtensionProfilingInfo, "StringName signature;uint64_t call_count;uint64_t total_time;uint64_t self_time");

GDREGISTER_ABSTRACT_CLASS(VirtualController);

worker_thread_pool = memnew(WorkerThreadPool);

OS::get_singleton()->benchmark_end_measure("Core", "Register Types");
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/Input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@
By default, the deadzone is automatically calculated from the average of the action deadzones. However, you can override the deadzone to be whatever you want (on the range of 0 to 1).
</description>
</method>
<method name="get_virtual_controller">
<return type="VirtualController" />
<description>
Return the platform-specific virtual controller or [code]null[/code] if it is not available. See also [VirtualController].
[b]Note:[/b] Currently implemented only on iOS 15.0+.
</description>
</method>
<method name="is_action_just_pressed" qualifiers="const">
<return type="bool" />
<param index="0" name="action" type="StringName" />
Expand Down
22 changes: 22 additions & 0 deletions doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,28 @@
<member name="input_devices/sensors/enable_magnetometer" type="bool" setter="" getter="" default="false">
If [code]true[/code], the magnetometer sensor is enabled and [method Input.get_magnetometer] returns valid data.
</member>
<member name="input_devices/virtual_controller/ios/enable_button_a" type="bool" setter="" getter="" default="true">
If [code]true[/code], the button "A" shows in the virtual controller.
</member>
<member name="input_devices/virtual_controller/ios/enable_button_b" type="bool" setter="" getter="" default="true">
If [code]true[/code], the button "B" shows in the virtual controller.
</member>
<member name="input_devices/virtual_controller/ios/enable_button_x" type="bool" setter="" getter="" default="true">
If [code]true[/code], the button "X" shows in the virtual controller.
</member>
<member name="input_devices/virtual_controller/ios/enable_button_y" type="bool" setter="" getter="" default="true">
If [code]true[/code], the button "Y" shows in the virtual controller.
</member>
<member name="input_devices/virtual_controller/ios/enable_controller" type="bool" setter="" getter="" default="false">
If [code]true[/code], if there are no physical controllers connected, the game shows the virtual controller.
[b]Note:[/b] Currently implemented only on iOS 15.0+.
</member>
<member name="input_devices/virtual_controller/ios/enable_left_thumbstick" type="bool" setter="" getter="" default="true">
If [code]true[/code], the left thumbstick shows in the virtual controller.
</member>
<member name="input_devices/virtual_controller/ios/enable_right_thumbstick" type="bool" setter="" getter="" default="true">
If [code]true[/code], the right thumbstick shows in the virtual controller.
</member>
<member name="internationalization/locale/fallback" type="String" setter="" getter="" default="&quot;en&quot;">
The locale to fall back to if a translation isn't available in a given language. If left empty, [code]en[/code] (English) will be used.
[b]Note:[/b] Not to be confused with [TextServerFallback].
Expand Down
110 changes: 110 additions & 0 deletions doc/classes/VirtualController.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="VirtualController" inherits="Object" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
A software emulation of a real controller that you configure specifically for your game.
</brief_description>
<description>
Use a virtual controller to display software controls that you can customize over your game. You create a virtual controller from a configuration where you choose the input elements to display. When you connect the controller to the device, users interact with it similarly to a real controller.
[b]Note:[/b] Currently implemented only on iOS 15.0+.
</description>
<tutorials>
</tutorials>
<methods>
<method name="disable">
<return type="void" />
<description>
Disconnects the virtual controller from the device and removes it from the screen.
</description>
</method>
<method name="enable">
<return type="void" />
<description>
If there are no physical controllers connected, connects the virtual controller to the device and displays it on the screen.
</description>
</method>
<method name="is_enabled">
<return type="bool" />
<description>
Returns [code]true[/code] if the virtual controller is enabled.
</description>
</method>
<method name="is_enabled_button_a">
<return type="bool" />
<description>
Returns [code]true[/code] if the button "A" is enabled.
</description>
</method>
<method name="is_enabled_button_b">
<return type="bool" />
<description>
Returns [code]true[/code] if the button "B" is enabled.
</description>
</method>
<method name="is_enabled_button_x">
<return type="bool" />
<description>
Returns [code]true[/code] if the button "X" is enabled.
</description>
</method>
<method name="is_enabled_button_y">
<return type="bool" />
<description>
Returns [code]true[/code] if the button "Y" is enabled.
</description>
</method>
<method name="is_enabled_left_thumbstick">
<return type="bool" />
<description>
Returns [code]true[/code] if the left thumbstick is enabled.
</description>
</method>
<method name="is_enabled_right_thumbstick">
<return type="bool" />
<description>
Returns [code]true[/code] if the right thumbstick is enabled.
</description>
</method>
<method name="set_enabled_button_a">
<return type="void" />
<param index="0" name="enable" type="bool" />
<description>
Changes the visibility of a button "A" element. Default is [code]true[/code].
</description>
</method>
<method name="set_enabled_button_b">
<return type="void" />
<param index="0" name="enable" type="bool" />
<description>
Changes the visibility of a button "B" element. Default is [code]true[/code].
</description>
</method>
<method name="set_enabled_button_x">
<return type="void" />
<param index="0" name="enable" type="bool" />
<description>
Changes the visibility of a button "A" element. Default is [code]true[/code].
</description>
</method>
<method name="set_enabled_button_y">
<return type="void" />
<param index="0" name="enable" type="bool" />
<description>
Changes the visibility of a button "Y" element. Default is [code]true[/code].
</description>
</method>
<method name="set_enabled_left_thumbstick">
<return type="void" />
<param index="0" name="enable" type="bool" />
<description>
Changes the visibility of a left thumbstick element. Default is [code]true[/code].
</description>
</method>
<method name="set_enabled_right_thumbstick">
<return type="void" />
<param index="0" name="enable" type="bool" />
<description>
Changes the visibility of a right thumbstick element. Default is [code]true[/code].
</description>
</method>
</methods>
</class>
3 changes: 3 additions & 0 deletions drivers/apple/joypad_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import <os/log.h>

#include "core/config/project_settings.h"
#include "core/os/os.h"
#include "main/main.h"

class API_AVAILABLE(macos(11), ios(14.0), tvos(14.0)) RumbleMotor {
Expand Down Expand Up @@ -257,6 +258,7 @@ void play_strong_pattern(CHHapticPattern *p_pattern) {
return;
}
add_joypad(controller);
OS::get_singleton()->controller_connected();
}];

disconnect_observer = [NSNotificationCenter.defaultCenter
Expand All @@ -269,6 +271,7 @@ void play_strong_pattern(CHHapticPattern *p_pattern) {
return;
}
remove_joypad(controller);
OS::get_singleton()->controller_disconnected();
}];

if (@available(macOS 11.3, iOS 14.5, tvOS 14.5, *)) {
Expand Down
1 change: 1 addition & 0 deletions platform/ios/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ ios_lib = [
"keyboard_input_view.mm",
"key_mapping_ios.mm",
"ios_terminal_logger.mm",
"virtual_controller_ios.mm",
]

env_ios = env.Clone()
Expand Down
Loading