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