Path: blob/master/editor/scene/gui/virtual_joystick_editor_plugin.cpp
21014 views
/**************************************************************************/1/* virtual_joystick_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "virtual_joystick_editor_plugin.h"3132#include "editor/scene/canvas_item_editor_plugin.h"33#include "editor/themes/editor_scale.h"34#include "scene/gui/virtual_joystick.h"3536void VirtualJoystickEditorPlugin::edit(Object *p_object) {37if (virtual_joystick) {38virtual_joystick->disconnect(SceneStringName(draw), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));39}4041virtual_joystick = Object::cast_to<VirtualJoystick>(p_object);4243if (virtual_joystick) {44virtual_joystick->connect(SceneStringName(draw), callable_mp(CanvasItemEditor::get_singleton(), &CanvasItemEditor::update_viewport));45}46CanvasItemEditor::get_singleton()->update_viewport();47}4849bool VirtualJoystickEditorPlugin::handles(Object *p_object) const {50return Object::cast_to<VirtualJoystick>(p_object) != nullptr;51}5253void VirtualJoystickEditorPlugin::forward_canvas_draw_over_viewport(Control *p_viewport_control) {54if (!virtual_joystick || !virtual_joystick->is_visible_in_tree()) {55return;56}5758Transform2D xform = CanvasItemEditor::get_singleton()->get_canvas_transform() * virtual_joystick->get_screen_transform();5960Vector2 center = virtual_joystick->get_joystick_position();61float base_radius = virtual_joystick->get_joystick_size() * 0.5f;6263float clampzone_radius = base_radius * virtual_joystick->get_clampzone_ratio();64float deadzone_radius = clampzone_radius * virtual_joystick->get_deadzone_ratio();6566// NOTE: This color is copied from Camera2DEditor::forward_canvas_draw_over_viewport.67// We may want to unify them somehow in the future.68Color clampzone_color = Color(1, 1, 0.25, 0.63);69Color deadzone_color = Color(1, 1, 0.25, 0.63);7071int width = Math::round(1 * EDSCALE);7273p_viewport_control->draw_circle(xform.xform(center), clampzone_radius * xform.get_scale().x, clampzone_color, false, width);7475p_viewport_control->draw_circle(xform.xform(center), deadzone_radius * xform.get_scale().x, deadzone_color, false, width);76}777879