marker_2d.cpp 4.5 KB

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