position_2d.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*************************************************************************/
  2. /* position_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "position_2d.h"
  31. #include "core/engine.h"
  32. #include "scene/resources/texture.h"
  33. const float DEFAULT_GIZMO_EXTENTS = 10.0;
  34. void Position2D::_draw_cross() {
  35. float extents = get_gizmo_extents();
  36. // Colors taken from `axis_x_color` and `axis_y_color` (defined in `editor/editor_themes.cpp`)
  37. draw_line(Point2(-extents, 0), Point2(+extents, 0), Color(0.96, 0.20, 0.32));
  38. draw_line(Point2(0, -extents), Point2(0, +extents), Color(0.53, 0.84, 0.01));
  39. }
  40. #ifdef TOOLS_ENABLED
  41. Rect2 Position2D::_edit_get_rect() const {
  42. float extents = get_gizmo_extents();
  43. return Rect2(Point2(-extents, -extents), Size2(extents * 2, extents * 2));
  44. }
  45. bool Position2D::_edit_use_rect() const {
  46. return false;
  47. }
  48. #endif
  49. void Position2D::_notification(int p_what) {
  50. switch (p_what) {
  51. case NOTIFICATION_ENTER_TREE: {
  52. update();
  53. } break;
  54. case NOTIFICATION_DRAW: {
  55. if (!is_inside_tree()) {
  56. break;
  57. }
  58. if (Engine::get_singleton()->is_editor_hint()) {
  59. _draw_cross();
  60. }
  61. } break;
  62. }
  63. }
  64. void Position2D::set_gizmo_extents(float p_extents) {
  65. if (p_extents == DEFAULT_GIZMO_EXTENTS) {
  66. set_meta("_gizmo_extents_", Variant());
  67. } else {
  68. set_meta("_gizmo_extents_", p_extents);
  69. }
  70. update();
  71. }
  72. float Position2D::get_gizmo_extents() const {
  73. if (has_meta("_gizmo_extents_")) {
  74. return get_meta("_gizmo_extents_");
  75. } else {
  76. return DEFAULT_GIZMO_EXTENTS;
  77. }
  78. }
  79. void Position2D::_bind_methods() {
  80. ClassDB::bind_method(D_METHOD("_set_gizmo_extents", "extents"), &Position2D::set_gizmo_extents);
  81. ClassDB::bind_method(D_METHOD("_get_gizmo_extents"), &Position2D::get_gizmo_extents);
  82. ADD_PROPERTY(PropertyInfo(Variant::REAL, "gizmo_extents", PROPERTY_HINT_RANGE, "0,1000,0.1,or_greater", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_gizmo_extents", "_get_gizmo_extents");
  83. }
  84. Position2D::Position2D() {
  85. }