marker_2d.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**************************************************************************/
  2. /* marker_2d.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 "marker_2d.h"
  31. void Marker2D::_draw_cross() {
  32. const real_t extents = get_gizmo_extents();
  33. PackedVector2Array points = {
  34. Point2(+extents, 0),
  35. Point2(),
  36. Point2(),
  37. Point2(-extents, 0),
  38. Point2(0, +extents),
  39. Point2(),
  40. Point2(),
  41. Point2(0, -extents),
  42. };
  43. // Use the axis color which is brighter for the positive axis.
  44. // Use a darkened axis color for the negative axis.
  45. // This makes it possible to see in which direction the Marker3D node is rotated
  46. // (which can be important depending on how it's used).
  47. // Axis colors are taken from `axis_x_color` and `axis_y_color` (defined in `editor/themes/editor_theme_manager.cpp`).
  48. const Color color_x = Color(0.96, 0.20, 0.32);
  49. const Color color_y = Color(0.53, 0.84, 0.01);
  50. PackedColorArray colors = {
  51. color_x,
  52. color_x.darkened(0.5),
  53. color_y,
  54. color_y.darkened(0.5),
  55. };
  56. draw_multiline_colors(points, colors);
  57. }
  58. #ifdef DEBUG_ENABLED
  59. Rect2 Marker2D::_edit_get_rect() const {
  60. real_t extents = get_gizmo_extents();
  61. return Rect2(Point2(-extents, -extents), Size2(extents * 2, extents * 2));
  62. }
  63. bool Marker2D::_edit_use_rect() const {
  64. return false;
  65. }
  66. #endif // DEBUG_ENABLED
  67. void Marker2D::_notification(int p_what) {
  68. switch (p_what) {
  69. case NOTIFICATION_ENTER_TREE: {
  70. queue_redraw();
  71. } break;
  72. case NOTIFICATION_DRAW: {
  73. if (!is_inside_tree()) {
  74. break;
  75. }
  76. if (Engine::get_singleton()->is_editor_hint()) {
  77. _draw_cross();
  78. }
  79. } break;
  80. }
  81. }
  82. void Marker2D::set_gizmo_extents(real_t p_extents) {
  83. gizmo_extents = p_extents;
  84. queue_redraw();
  85. }
  86. real_t Marker2D::get_gizmo_extents() const {
  87. return gizmo_extents;
  88. }
  89. void Marker2D::_bind_methods() {
  90. ClassDB::bind_method(D_METHOD("set_gizmo_extents", "extents"), &Marker2D::set_gizmo_extents);
  91. ClassDB::bind_method(D_METHOD("get_gizmo_extents"), &Marker2D::get_gizmo_extents);
  92. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gizmo_extents", PROPERTY_HINT_RANGE, "0,1000,0.1,or_greater,suffix:px"), "set_gizmo_extents", "get_gizmo_extents");
  93. }
  94. Marker2D::Marker2D() {
  95. set_hide_clip_children(true);
  96. }