runtime_node_select.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**************************************************************************/
  2. /* runtime_node_select.h */
  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. #pragma once
  31. #ifdef DEBUG_ENABLED
  32. #include "scene/gui/view_panner.h"
  33. #ifndef _3D_DISABLED
  34. #include "scene/resources/mesh.h"
  35. #endif // _3D_DISABLED
  36. class PopupMenu;
  37. class RuntimeNodeSelect : public Object {
  38. GDCLASS(RuntimeNodeSelect, Object);
  39. public:
  40. enum NodeType {
  41. NODE_TYPE_NONE,
  42. NODE_TYPE_2D,
  43. NODE_TYPE_3D,
  44. NODE_TYPE_MAX,
  45. };
  46. enum SelectMode {
  47. SELECT_MODE_SINGLE,
  48. SELECT_MODE_LIST,
  49. SELECT_MODE_MAX,
  50. };
  51. private:
  52. friend class SceneDebugger;
  53. NodeType node_select_type = NODE_TYPE_2D;
  54. SelectMode node_select_mode = SELECT_MODE_SINGLE;
  55. struct SelectResult {
  56. Node *item = nullptr;
  57. real_t order = 0;
  58. _FORCE_INLINE_ bool operator<(const SelectResult &p_rr) const { return p_rr.order < order; }
  59. };
  60. const int SELECTION_MIN_AREA = 8 * 8;
  61. enum SelectionDragState {
  62. SELECTION_DRAG_NONE,
  63. SELECTION_DRAG_MOVE,
  64. SELECTION_DRAG_END,
  65. };
  66. SelectionDragState selection_drag_state = SELECTION_DRAG_NONE;
  67. bool has_selection = false;
  68. int max_selection = 1;
  69. Point2 selection_position = Point2(Math::INF, Math::INF);
  70. Rect2 selection_drag_area;
  71. PopupMenu *selection_list = nullptr;
  72. Color selection_area_fill;
  73. Color selection_area_outline;
  74. bool selection_visible = true;
  75. bool selection_update_queued = false;
  76. bool avoid_locked_nodes = false;
  77. bool prefer_group_selection = false;
  78. bool multi_shortcut_pressed = false;
  79. bool list_shortcut_pressed = false;
  80. RID draw_canvas;
  81. RID sel_drag_ci;
  82. bool camera_override = false;
  83. bool camera_first_override = true;
  84. // Values taken from EditorZoomWidget.
  85. const float VIEW_2D_MIN_ZOOM = 1.0 / 128;
  86. const float VIEW_2D_MAX_ZOOM = 128;
  87. Ref<ViewPanner> panner;
  88. Vector2 view_2d_offset;
  89. real_t view_2d_zoom = 1.0;
  90. bool warped_panning = false;
  91. LocalVector<ObjectID> selected_ci_nodes;
  92. real_t sel_2d_grab_dist = 0;
  93. int sel_2d_scale = 1;
  94. RID sbox_2d_ci;
  95. #ifndef _3D_DISABLED
  96. struct Cursor {
  97. Vector3 pos;
  98. real_t x_rot, y_rot, distance, fov_scale;
  99. Vector3 eye_pos; // Used in freelook mode.
  100. Cursor() {
  101. // These rotations place the camera in +X +Y +Z, aka south east, facing north west.
  102. x_rot = 0.5;
  103. y_rot = -0.5;
  104. distance = 4;
  105. fov_scale = 1.0;
  106. }
  107. };
  108. Cursor cursor;
  109. // Values taken from Node3DEditor.
  110. const float VIEW_3D_MIN_ZOOM = 0.01;
  111. #ifdef REAL_T_IS_DOUBLE
  112. const double VIEW_3D_MAX_ZOOM = 1'000'000'000'000;
  113. #else
  114. const float VIEW_3D_MAX_ZOOM = 10'000;
  115. #endif // REAL_T_IS_DOUBLE
  116. const float CAMERA_MIN_FOV_SCALE = 0.1;
  117. const float CAMERA_MAX_FOV_SCALE = 2.5;
  118. bool camera_freelook = false;
  119. real_t camera_fov = 0;
  120. real_t camera_znear = 0;
  121. real_t camera_zfar = 0;
  122. bool invert_x_axis = false;
  123. bool invert_y_axis = false;
  124. bool warped_mouse_panning_3d = false;
  125. real_t freelook_base_speed = 0;
  126. real_t freelook_sensitivity = 0;
  127. real_t orbit_sensitivity = 0;
  128. real_t translation_sensitivity = 0;
  129. Vector2 previous_mouse_position;
  130. struct SelectionBox3D : public RefCounted {
  131. RID instance;
  132. RID instance_ofs;
  133. RID instance_xray;
  134. RID instance_xray_ofs;
  135. Transform3D transform;
  136. AABB bounds;
  137. ~SelectionBox3D() {
  138. if (instance.is_valid()) {
  139. RS::get_singleton()->free_rid(instance);
  140. RS::get_singleton()->free_rid(instance_ofs);
  141. RS::get_singleton()->free_rid(instance_xray);
  142. RS::get_singleton()->free_rid(instance_xray_ofs);
  143. }
  144. }
  145. };
  146. HashMap<ObjectID, Ref<SelectionBox3D>> selected_3d_nodes;
  147. Color sbox_3d_color;
  148. Ref<ArrayMesh> sbox_3d_mesh;
  149. Ref<ArrayMesh> sbox_3d_mesh_xray;
  150. RID sbox_3d;
  151. RID sbox_3d_ofs;
  152. RID sbox_3d_xray;
  153. RID sbox_3d_xray_ofs;
  154. #endif // _3D_DISABLED
  155. void _setup(const Dictionary &p_settings);
  156. void _node_set_type(NodeType p_type);
  157. void _select_set_mode(SelectMode p_mode);
  158. void _set_camera_override_enabled(bool p_enabled);
  159. void _root_window_input(const Ref<InputEvent> &p_event);
  160. void _items_popup_index_pressed(int p_index, PopupMenu *p_popup);
  161. void _update_input_state();
  162. void _process_frame();
  163. void _physics_frame();
  164. void _send_ids(const Vector<Node *> &p_picked_nodes, bool p_invert_new_selections = true);
  165. void _set_selected_nodes(const Vector<Node *> &p_nodes);
  166. void _queue_selection_update();
  167. void _update_selection();
  168. void _clear_selection();
  169. void _update_selection_drag(const Point2 &p_end_pos = Point2());
  170. void _set_selection_visible(bool p_visible);
  171. void _set_avoid_locked(bool p_enabled);
  172. void _set_prefer_group(bool p_enabled);
  173. void _open_selection_list(const Vector<SelectResult> &p_items, const Point2 &p_pos);
  174. void _close_selection_list();
  175. void _find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  176. void _find_canvas_items_at_rect(const Rect2 &p_rect, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
  177. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  178. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  179. void _reset_camera_2d();
  180. void _update_view_2d();
  181. #ifndef _3D_DISABLED
  182. void _find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items);
  183. void _find_3d_items_at_rect(const Rect2 &p_rect, Vector<SelectResult> &r_items);
  184. Vector3 _get_screen_to_space(const Vector3 &p_vector3);
  185. bool _handle_3d_input(const Ref<InputEvent> &p_event);
  186. void _set_camera_freelook_enabled(bool p_enabled);
  187. void _cursor_scale_distance(real_t p_scale);
  188. void _scale_freelook_speed(real_t p_scale);
  189. void _cursor_look(Ref<InputEventWithModifiers> p_event);
  190. void _cursor_pan(Ref<InputEventWithModifiers> p_event);
  191. void _cursor_orbit(Ref<InputEventWithModifiers> p_event);
  192. Point2 _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_event, Rect2 p_border) const;
  193. Transform3D _get_cursor_transform();
  194. void _reset_camera_3d();
  195. #endif // _3D_DISABLED
  196. RuntimeNodeSelect() { singleton = this; }
  197. inline static RuntimeNodeSelect *singleton = nullptr;
  198. public:
  199. static RuntimeNodeSelect *get_singleton();
  200. ~RuntimeNodeSelect();
  201. };
  202. #endif // DEBUG_ENABLED