Skip to content
0.2.0-dev Download ZIP
125 lines ·4.5 KB Raw
1/**************************************************************************/
2/* virtual_joystick.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "scene/gui/control.h"
34
35class VirtualJoystick : public Control {
36 GDCLASS(VirtualJoystick, Control);
37
38public:
39 enum JoystickMode {
40 JOYSTICK_FIXED,
41 JOYSTICK_DYNAMIC,
42 JOYSTICK_FOLLOWING,
43 };
44
45 enum VisibilityMode {
46 VISIBILITY_ALWAYS,
47 VISIBILITY_WHEN_TOUCHED,
48 };
49
50private:
51 JoystickMode joystick_mode = JOYSTICK_FIXED;
52 float joystick_size = 100.0f;
53 float tip_size = 50.0f;
54 float deadzone_ratio = 0.0f;
55 float clampzone_ratio = 1.0f;
56 Vector2 initial_offset_ratio = Vector2(0.5, 0.5);
57 StringName action_left = "ui_left";
58 StringName action_right = "ui_right";
59 StringName action_up = "ui_up";
60 StringName action_down = "ui_down";
61 VisibilityMode visibility = VISIBILITY_ALWAYS;
62
63 struct ThemeCache {
64 Ref<StyleBox> normal_joystick;
65 Ref<StyleBox> normal_tip;
66 Ref<StyleBox> pressed_joystick;
67 Ref<StyleBox> pressed_tip;
68 } theme_cache;
69
70 bool is_pressed = false;
71 bool has_input = false;
72 bool has_moved = false;
73 Vector2 raw_input_vector;
74 Vector2 input_vector;
75 bool is_flick_canceled = false;
76 int touch_index = -1;
77
78 Vector2 joystick_pos;
79 Vector2 tip_pos;
80
81 void _update_joystick(const Vector2 &p_pos);
82 void _handle_input_actions();
83 void _reset();
84
85protected:
86 virtual void gui_input(const Ref<InputEvent> &p_event) override;
87 void _notification(int p_what);
88 static void _bind_methods();
89
90public:
91 Vector2 get_joystick_position() const;
92
93 void set_joystick_mode(JoystickMode p_mode);
94 JoystickMode get_joystick_mode() const;
95
96 void set_joystick_size(float p_size);
97 float get_joystick_size() const;
98
99 void set_tip_size(float p_size);
100 float get_tip_size() const;
101
102 void set_deadzone_ratio(float p_ratio);
103 float get_deadzone_ratio() const;
104
105 void set_clampzone_ratio(float p_ratio);
106 float get_clampzone_ratio() const;
107
108 void set_initial_offset_ratio(const Vector2 &p_ratio);
109 Vector2 get_initial_offset_ratio() const;
110
111 void set_action_left(const StringName &p_action);
112 StringName get_action_left() const;
113 void set_action_right(const StringName &p_action);
114 StringName get_action_right() const;
115 void set_action_up(const StringName &p_action);
116 StringName get_action_up() const;
117 void set_action_down(const StringName &p_action);
118 StringName get_action_down() const;
119
120 void set_visibility_mode(VisibilityMode p_mode);
121 VisibilityMode get_visibility_mode() const;
122};
123
124VARIANT_ENUM_CAST(VirtualJoystick::JoystickMode);
125VARIANT_ENUM_CAST(VirtualJoystick::VisibilityMode);