Path: blob/master/editor/scene/3d/gizmos/decal_gizmo_plugin.cpp
9906 views
/**************************************************************************/1/* decal_gizmo_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 "decal_gizmo_plugin.h"3132#include "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/scene/3d/gizmos/gizmo_3d_helper.h"35#include "editor/settings/editor_settings.h"36#include "scene/3d/decal.h"3738DecalGizmoPlugin::DecalGizmoPlugin() {39helper.instantiate();40Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/decal");4142create_icon_material("decal_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoDecal"), EditorStringName(EditorIcons)));4344create_material("decal_material", gizmo_color);4546create_handle_material("handles");47}4849bool DecalGizmoPlugin::has_gizmo(Node3D *p_spatial) {50return Object::cast_to<Decal>(p_spatial) != nullptr;51}5253String DecalGizmoPlugin::get_gizmo_name() const {54return "Decal";55}5657int DecalGizmoPlugin::get_priority() const {58return -1;59}6061String DecalGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {62return helper->box_get_handle_name(p_id);63}6465Variant DecalGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {66Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());67return decal->get_size();68}6970void DecalGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {71helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());72}7374void DecalGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {75Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());76Vector3 size = decal->get_size();7778Vector3 sg[2];79helper->get_segment(p_camera, p_point, sg);8081Vector3 position;82helper->box_set_handle(sg, p_id, size, position);83decal->set_size(size);84decal->set_global_position(position);85}8687void DecalGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {88helper->box_commit_handle(TTR("Change Decal Size"), p_cancel, p_gizmo->get_node_3d());89}9091void DecalGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {92Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());9394p_gizmo->clear();9596Vector<Vector3> lines;97Vector3 size = decal->get_size();9899AABB aabb;100aabb.position = -size / 2;101aabb.size = size;102103for (int i = 0; i < 12; i++) {104Vector3 a, b;105aabb.get_edge(i, a, b);106if (a.y == b.y) {107lines.push_back(a);108lines.push_back(b);109} else {110Vector3 ah = a.lerp(b, 0.2);111lines.push_back(a);112lines.push_back(ah);113Vector3 bh = b.lerp(a, 0.2);114lines.push_back(b);115lines.push_back(bh);116}117}118119float half_size_y = size.y / 2;120lines.push_back(Vector3(0, half_size_y, 0));121lines.push_back(Vector3(0, half_size_y * 1.2, 0));122123Vector<Vector3> handles = helper->box_get_handles(decal->get_size());124Ref<Material> material = get_material("decal_material", p_gizmo);125const Ref<Material> icon = get_material("decal_icon", p_gizmo);126127p_gizmo->add_lines(lines, material);128p_gizmo->add_unscaled_billboard(icon, 0.05);129p_gizmo->add_handles(handles, get_material("handles"));130}131132133