Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/gui/virtual_joystick_editor_plugin.cpp
21014 views
1
/**************************************************************************/
2
/* virtual_joystick_editor_plugin.cpp */
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
#include "virtual_joystick_editor_plugin.h"
32
33
#include "editor/scene/canvas_item_editor_plugin.h"
34
#include "editor/themes/editor_scale.h"
35
#include "scene/gui/virtual_joystick.h"
36
37
void VirtualJoystickEditorPlugin::edit(Object *p_object) {
38
if (virtual_joystick) {
39
virtual_joystick->disconnect(SceneStringName(draw), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
40
}
41
42
virtual_joystick = Object::cast_to<VirtualJoystick>(p_object);
43
44
if (virtual_joystick) {
45
virtual_joystick->connect(SceneStringName(draw), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));
46
}
47
CanvasItemEditor::get_singleton()->update_viewport();
48
}
49
50
bool VirtualJoystickEditorPlugin::handles(Object *p_object) const {
51
return Object::cast_to<VirtualJoystick>(p_object) != nullptr;
52
}
53
54
void VirtualJoystickEditorPlugin::forward_canvas_draw_over_viewport(Control *p_viewport_control) {
55
if (!virtual_joystick || !virtual_joystick->is_visible_in_tree()) {
56
return;
57
}
58
59
Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * virtual_joystick->get_screen_transform();
60
61
Vector2 center = virtual_joystick->get_joystick_position();
62
float base_radius = virtual_joystick->get_joystick_size() * 0.5f;
63
64
float clampzone_radius = base_radius * virtual_joystick->get_clampzone_ratio();
65
float deadzone_radius = clampzone_radius * virtual_joystick->get_deadzone_ratio();
66
67
// NOTE: This color is copied from Camera2DEditor::forward_canvas_draw_over_viewport.
68
// We may want to unify them somehow in the future.
69
Color clampzone_color = Color(1, 1, 0.25, 0.63);
70
Color deadzone_color = Color(1, 1, 0.25, 0.63);
71
72
int width = Math::round(1 * EDSCALE);
73
74
p_viewport_control->draw_circle(xform.xform(center), clampzone_radius * xform.get_scale().x, clampzone_color, false, width);
75
76
p_viewport_control->draw_circle(xform.xform(center), deadzone_radius * xform.get_scale().x, deadzone_color, false, width);
77
}
78
79