decal_gizmo_plugin.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************/
  2. /* decal_gizmo_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. #include "decal_gizmo_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/plugins/gizmos/gizmo_3d_helper.h"
  35. #include "scene/3d/decal.h"
  36. DecalGizmoPlugin::DecalGizmoPlugin() {
  37. helper.instantiate();
  38. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/decal");
  39. create_icon_material("decal_icon", EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("GizmoDecal"), EditorStringName(EditorIcons)));
  40. create_material("decal_material", gizmo_color);
  41. create_handle_material("handles");
  42. }
  43. DecalGizmoPlugin::~DecalGizmoPlugin() {
  44. }
  45. bool DecalGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  46. return Object::cast_to<Decal>(p_spatial) != nullptr;
  47. }
  48. String DecalGizmoPlugin::get_gizmo_name() const {
  49. return "Decal";
  50. }
  51. int DecalGizmoPlugin::get_priority() const {
  52. return -1;
  53. }
  54. String DecalGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  55. return helper->box_get_handle_name(p_id);
  56. }
  57. Variant DecalGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  58. Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());
  59. return decal->get_size();
  60. }
  61. void DecalGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
  62. helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
  63. }
  64. void DecalGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  65. Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());
  66. Vector3 size = decal->get_size();
  67. Vector3 sg[2];
  68. helper->get_segment(p_camera, p_point, sg);
  69. Vector3 position;
  70. helper->box_set_handle(sg, p_id, size, position);
  71. decal->set_size(size);
  72. decal->set_global_position(position);
  73. }
  74. void DecalGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  75. helper->box_commit_handle(TTR("Change Decal Size"), p_cancel, p_gizmo->get_node_3d());
  76. }
  77. void DecalGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  78. Decal *decal = Object::cast_to<Decal>(p_gizmo->get_node_3d());
  79. p_gizmo->clear();
  80. Vector<Vector3> lines;
  81. Vector3 size = decal->get_size();
  82. AABB aabb;
  83. aabb.position = -size / 2;
  84. aabb.size = size;
  85. for (int i = 0; i < 12; i++) {
  86. Vector3 a, b;
  87. aabb.get_edge(i, a, b);
  88. if (a.y == b.y) {
  89. lines.push_back(a);
  90. lines.push_back(b);
  91. } else {
  92. Vector3 ah = a.lerp(b, 0.2);
  93. lines.push_back(a);
  94. lines.push_back(ah);
  95. Vector3 bh = b.lerp(a, 0.2);
  96. lines.push_back(b);
  97. lines.push_back(bh);
  98. }
  99. }
  100. float half_size_y = size.y / 2;
  101. lines.push_back(Vector3(0, half_size_y, 0));
  102. lines.push_back(Vector3(0, half_size_y * 1.2, 0));
  103. Vector<Vector3> handles = helper->box_get_handles(decal->get_size());
  104. Ref<Material> material = get_material("decal_material", p_gizmo);
  105. const Ref<Material> icon = get_material("decal_icon", p_gizmo);
  106. p_gizmo->add_lines(lines, material);
  107. p_gizmo->add_unscaled_billboard(icon, 0.05);
  108. p_gizmo->add_handles(handles, get_material("handles"));
  109. }