Skip to content

Commit 69101b4

Browse files
committed
Add Resolution Presets for the embedded game window
1 parent f4c57c2 commit 69101b4

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

doc/classes/BaseButton.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
</method>
4545
</methods>
4646
<members>
47+
<member name="action" type="StringName" setter="set_action" getter="get_action" default="&amp;&quot;&quot;">
48+
The InputName action to be performed when the button is pressed and released.
49+
</member>
4750
<member name="action_mode" type="int" setter="set_action_mode" getter="get_action_mode" enum="BaseButton.ActionMode" default="1">
4851
Determines when the button is considered clicked.
4952
</member>

doc/classes/TouchScreenButton.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<class name="TouchScreenButton" inherits="Node2D" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
2+
<class name="TouchScreenButton" inherits="Node2D" api_type="core" deprecated="Use [BaseButton] derived nodes instead. They supersede this class for all intents and purposes." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
33
<brief_description>
44
Button for touch screen devices for gameplay use.
55
</brief_description>
66
<description>
7-
TouchScreenButton allows you to create on-screen buttons for touch devices. It's intended for gameplay use, such as a unit you have to touch to move. Unlike [Button], TouchScreenButton supports multitouch out of the box. Several TouchScreenButtons can be pressed at the same time with touch input.
7+
TouchScreenButton allows you to create on-screen buttons for touch devices. It's intended for gameplay use, such as a unit you have to touch to move. [Button] and TouchScreenButton support multitouch out of the box. Several TouchScreenButtons can be pressed at the same time with touch input.
88
This node inherits from [Node2D]. Unlike with [Control] nodes, you cannot set anchors on it. If you want to create menus or user interfaces, you may want to use [Button] nodes instead. To make button nodes react to touch events, you can enable [member ProjectSettings.input_devices/pointing/emulate_mouse_from_touch] in the Project Settings.
99
You can configure TouchScreenButton to be visible only on touch devices, helping you develop your game both for desktop and mobile devices.
1010
</description>

scene/gui/base_button.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
#include "base_button.h"
3232

3333
#include "core/config/project_settings.h"
34+
#include "core/input/input.h"
3435
#include "core/object/callable_mp.h"
3536
#include "core/object/class_db.h"
37+
#include "core/string/string_name.h"
3638
#include "scene/gui/label.h"
3739
#include "scene/main/timer.h"
40+
#include "scene/main/viewport.h"
3841
#include "scene/theme/theme_db.h"
3942
#include "servers/display/accessibility_server.h"
4043

@@ -203,6 +206,7 @@ void BaseButton::_notification(int p_what) {
203206
if (status.pressed_down_with_focus) {
204207
status.pressed_down_with_focus = false;
205208
emit_signal(SNAME("button_up"));
209+
_action_release();
206210
}
207211
} break;
208212

@@ -247,6 +251,7 @@ void BaseButton::on_action_event(Ref<InputEvent> p_event) {
247251
if (!status.pressed_down_with_focus) {
248252
status.pressed_down_with_focus = true;
249253
emit_signal(SNAME("button_down"));
254+
_action_press();
250255
}
251256
}
252257

@@ -281,12 +286,37 @@ void BaseButton::on_action_event(Ref<InputEvent> p_event) {
281286
if (status.pressed_down_with_focus) {
282287
status.pressed_down_with_focus = false;
283288
emit_signal(SNAME("button_up"));
289+
_action_release();
284290
}
285291
}
286292

287293
queue_redraw();
288294
}
289295

296+
void BaseButton::_action_press() {
297+
if (action == StringName()) {
298+
return;
299+
}
300+
Input::get_singleton()->action_press(action);
301+
Ref<InputEventAction> iea;
302+
iea.instantiate();
303+
iea->set_action(action);
304+
iea->set_pressed(true);
305+
get_viewport()->push_input(iea, true);
306+
}
307+
308+
void BaseButton::_action_release() {
309+
if (action == StringName()) {
310+
return;
311+
}
312+
Input::get_singleton()->action_release(action);
313+
Ref<InputEventAction> iea;
314+
iea.instantiate();
315+
iea->set_action(action);
316+
iea->set_pressed(false);
317+
get_viewport()->push_input(iea, true);
318+
}
319+
290320
void BaseButton::pressed() {
291321
}
292322

@@ -308,6 +338,7 @@ void BaseButton::set_disabled(bool p_disabled) {
308338
if (status.pressed_down_with_focus) {
309339
status.pressed_down_with_focus = false;
310340
emit_signal(SNAME("button_up"));
341+
_action_release();
311342
}
312343
}
313344
queue_accessibility_update();
@@ -554,6 +585,14 @@ Control *BaseButton::make_custom_tooltip(const String &p_text) const {
554585
return label;
555586
}
556587

588+
void BaseButton::set_action(const StringName &p_action) {
589+
action = p_action;
590+
}
591+
592+
StringName BaseButton::get_action() const {
593+
return action;
594+
}
595+
557596
void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
558597
if (button_group.is_valid()) {
559598
button_group->buttons.erase(this);
@@ -612,6 +651,9 @@ void BaseButton::_bind_methods() {
612651
ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
613652
ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
614653

654+
ClassDB::bind_method(D_METHOD("set_action", "action"), &BaseButton::set_action);
655+
ClassDB::bind_method(D_METHOD("get_action"), &BaseButton::get_action);
656+
615657
ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
616658
ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
617659

@@ -629,6 +671,7 @@ void BaseButton::_bind_methods() {
629671
ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
630672
ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
631673
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
674+
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "action", PROPERTY_HINT_INPUT_NAME, "show_builtin,loose_mode"), "set_action", "get_action");
632675
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, ButtonGroup::get_class_static()), "set_button_group", "get_button_group");
633676

634677
ADD_GROUP("Shortcut", "");

scene/gui/base_button.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class BaseButton : public Control {
7070
int touch_index = -1;
7171
} status;
7272

73+
StringName action;
7374
Ref<ButtonGroup> button_group;
7475

7576
void _unpress_group();
@@ -78,6 +79,9 @@ class BaseButton : public Control {
7879

7980
void on_action_event(Ref<InputEvent> p_event);
8081

82+
void _action_press();
83+
void _action_release();
84+
8185
Timer *shortcut_feedback_timer = nullptr;
8286
bool in_shortcut_feedback = false;
8387
void _shortcut_feedback_timeout();
@@ -143,6 +147,9 @@ class BaseButton : public Control {
143147

144148
virtual Control *make_custom_tooltip(const String &p_text) const override;
145149

150+
void set_action(const StringName &p_action);
151+
StringName get_action() const;
152+
146153
void set_button_group(const Ref<ButtonGroup> &p_group);
147154
Ref<ButtonGroup> get_button_group() const;
148155

0 commit comments

Comments
 (0)