camera_2d_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**************************************************************************/
  2. /* camera_2d_editor_plugin.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 "camera_2d_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/scene/canvas_item_editor_plugin.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/2d/camera_2d.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/menu_button.h"
  39. void Camera2DEditor::edit(Camera2D *p_camera) {
  40. if (p_camera == selected_camera) {
  41. return;
  42. }
  43. const Callable update_overlays = callable_mp(plugin, &EditorPlugin::update_overlays);
  44. if (selected_camera) {
  45. selected_camera->disconnect(SceneStringName(draw), update_overlays);
  46. if (drag_type != Drag::NONE) {
  47. selected_camera->set_limit_rect(drag_revert);
  48. }
  49. drag_type = Drag::NONE;
  50. hover_type = Drag::NONE;
  51. CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_ARROW);
  52. }
  53. selected_camera = p_camera;
  54. if (selected_camera) {
  55. selected_camera->connect(SceneStringName(draw), update_overlays);
  56. }
  57. plugin->update_overlays();
  58. }
  59. bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
  60. if (!selected_camera || !selected_camera->is_limit_enabled()) {
  61. return false;
  62. }
  63. Ref<InputEventMouseButton> mb = p_event;
  64. if (mb.is_valid()) {
  65. if (mb->get_button_index() == MouseButton::LEFT) {
  66. if (mb->is_pressed()) {
  67. if (hover_type != Drag::NONE) {
  68. Vector2 pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(mb->get_position());
  69. const Rect2 limit_rect = selected_camera->get_limit_rect();
  70. drag_type = hover_type;
  71. drag_revert = selected_camera->get_limit_rect();
  72. center_drag_point = pos - limit_rect.position;
  73. return true;
  74. }
  75. } else if (drag_type != Drag::NONE) {
  76. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  77. ur->create_action(TTR("Edit Camera2D Limits"));
  78. ur->add_do_method(selected_camera, "_set_limit_rect", selected_camera->get_limit_rect());
  79. ur->add_do_method(this, "_update_overlays_if_needed", selected_camera);
  80. ur->add_undo_method(selected_camera, "_set_limit_rect", drag_revert);
  81. ur->add_undo_method(this, "_update_overlays_if_needed", selected_camera);
  82. ur->commit_action(false);
  83. drag_type = Drag::NONE;
  84. return true;
  85. }
  86. } else if (drag_type != Drag::NONE && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  87. selected_camera->set_limit_rect(drag_revert);
  88. drag_type = Drag::NONE;
  89. plugin->update_overlays();
  90. _update_hover(mb->get_position());
  91. return true;
  92. }
  93. return false;
  94. }
  95. Ref<InputEventMouseMotion> mm = p_event;
  96. if (mm.is_valid()) {
  97. Vector2 pos = mm->get_position();
  98. if (drag_type == Drag::NONE) {
  99. _update_hover(pos);
  100. return false;
  101. }
  102. pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(pos);
  103. pos = CanvasItemEditor::get_singleton()->snap_point(pos);
  104. switch (drag_type) {
  105. case Drag::LEFT: {
  106. selected_camera->set_limit(SIDE_LEFT, MIN(selected_camera->get_limit(SIDE_RIGHT), pos.x));
  107. plugin->update_overlays();
  108. } break;
  109. case Drag::RIGHT: {
  110. selected_camera->set_limit(SIDE_RIGHT, MAX(selected_camera->get_limit(SIDE_LEFT), pos.x));
  111. plugin->update_overlays();
  112. } break;
  113. case Drag::TOP: {
  114. selected_camera->set_limit(SIDE_TOP, MIN(selected_camera->get_limit(SIDE_BOTTOM), pos.y));
  115. plugin->update_overlays();
  116. } break;
  117. case Drag::BOTTOM: {
  118. selected_camera->set_limit(SIDE_BOTTOM, MAX(selected_camera->get_limit(SIDE_TOP), pos.y));
  119. plugin->update_overlays();
  120. } break;
  121. case Drag::TOP_LEFT: {
  122. selected_camera->set_limit(SIDE_LEFT, MIN(selected_camera->get_limit(SIDE_RIGHT), pos.x));
  123. selected_camera->set_limit(SIDE_TOP, MIN(selected_camera->get_limit(SIDE_BOTTOM), pos.y));
  124. plugin->update_overlays();
  125. } break;
  126. case Drag::TOP_RIGHT: {
  127. selected_camera->set_limit(SIDE_RIGHT, MAX(selected_camera->get_limit(SIDE_LEFT), pos.x));
  128. selected_camera->set_limit(SIDE_TOP, MIN(selected_camera->get_limit(SIDE_BOTTOM), pos.y));
  129. plugin->update_overlays();
  130. } break;
  131. case Drag::BOTTOM_LEFT: {
  132. selected_camera->set_limit(SIDE_LEFT, MIN(selected_camera->get_limit(SIDE_RIGHT), pos.x));
  133. selected_camera->set_limit(SIDE_BOTTOM, MAX(selected_camera->get_limit(SIDE_TOP), pos.y));
  134. plugin->update_overlays();
  135. } break;
  136. case Drag::BOTTOM_RIGHT: {
  137. selected_camera->set_limit(SIDE_RIGHT, MAX(selected_camera->get_limit(SIDE_LEFT), pos.x));
  138. selected_camera->set_limit(SIDE_BOTTOM, MAX(selected_camera->get_limit(SIDE_TOP), pos.y));
  139. plugin->update_overlays();
  140. } break;
  141. case Drag::CENTER: {
  142. Rect2 target_rect = selected_camera->get_limit_rect();
  143. target_rect.position = pos - center_drag_point;
  144. selected_camera->set_limit_rect(target_rect);
  145. plugin->update_overlays();
  146. } break;
  147. case Drag::NONE: {
  148. } break;
  149. }
  150. return true;
  151. }
  152. return false;
  153. }
  154. void Camera2DEditor::forward_canvas_draw_over_viewport(Control *p_overlay) {
  155. if (!selected_camera || !selected_camera->is_limit_enabled()) {
  156. return;
  157. }
  158. Rect2 limit_rect = selected_camera->get_limit_rect();
  159. limit_rect = CanvasItemEditor::get_singleton()->get_canvas_transform().xform(limit_rect);
  160. p_overlay->draw_rect(limit_rect, Color(1, 1, 0.25, 0.63), false, 3);
  161. }
  162. void Camera2DEditor::_menu_option(int p_option) {
  163. switch (p_option) {
  164. case MENU_SNAP_LIMITS_TO_VIEWPORT: {
  165. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  166. ur->create_action(TTR("Snap Camera2D Limits to the Viewport"), UndoRedo::MERGE_DISABLE, selected_camera);
  167. ur->add_do_method(this, "_snap_limits_to_viewport", selected_camera);
  168. ur->add_undo_method(selected_camera, "_set_limit_rect", selected_camera->get_limit_rect());
  169. ur->add_undo_method(this, "_update_overlays_if_needed", selected_camera);
  170. ur->commit_action();
  171. } break;
  172. }
  173. }
  174. void Camera2DEditor::_snap_limits_to_viewport(Camera2D *p_camera) {
  175. p_camera->set_limit(SIDE_LEFT, 0);
  176. p_camera->set_limit(SIDE_TOP, 0);
  177. p_camera->set_limit(SIDE_RIGHT, GLOBAL_GET("display/window/size/viewport_width"));
  178. p_camera->set_limit(SIDE_BOTTOM, GLOBAL_GET("display/window/size/viewport_height"));
  179. _update_overlays_if_needed(p_camera);
  180. }
  181. void Camera2DEditor::_update_overlays_if_needed(Camera2D *p_camera) {
  182. if (p_camera == selected_camera) {
  183. plugin->update_overlays();
  184. }
  185. }
  186. void Camera2DEditor::_update_hover(const Vector2 &p_mouse_pos) {
  187. if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
  188. hover_type = Drag::NONE;
  189. CanvasItemEditor::get_singleton()->set_cursor_shape_override();
  190. return;
  191. }
  192. const Rect2 limit_rect = CanvasItemEditor::get_singleton()->get_canvas_transform().xform(selected_camera->get_limit_rect());
  193. const float drag_tolerance = 8.0;
  194. const Vector2 tolerance_vector = Vector2(1, 1) * drag_tolerance;
  195. hover_type = Drag::NONE;
  196. if (Rect2(limit_rect.position - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) {
  197. hover_type = Drag::TOP_LEFT;
  198. } else if (Rect2(Vector2(limit_rect.get_end().x, limit_rect.position.y) - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) {
  199. hover_type = Drag::TOP_RIGHT;
  200. } else if (Rect2(Vector2(limit_rect.position.x, limit_rect.get_end().y) - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) {
  201. hover_type = Drag::BOTTOM_LEFT;
  202. } else if (Rect2(limit_rect.get_end() - tolerance_vector, tolerance_vector * 2).has_point(p_mouse_pos)) {
  203. hover_type = Drag::BOTTOM_RIGHT;
  204. } else if (p_mouse_pos.y > limit_rect.position.y && p_mouse_pos.y < limit_rect.get_end().y) {
  205. if (Math::abs(p_mouse_pos.x - limit_rect.position.x) < drag_tolerance) {
  206. hover_type = Drag::LEFT;
  207. } else if (Math::abs(p_mouse_pos.x - limit_rect.get_end().x) < drag_tolerance) {
  208. hover_type = Drag::RIGHT;
  209. }
  210. } else if (p_mouse_pos.x > limit_rect.position.x && p_mouse_pos.x < limit_rect.get_end().x) {
  211. if (Math::abs(p_mouse_pos.y - limit_rect.position.y) < drag_tolerance) {
  212. hover_type = Drag::TOP;
  213. } else if (Math::abs(p_mouse_pos.y - limit_rect.get_end().y) < drag_tolerance) {
  214. hover_type = Drag::BOTTOM;
  215. }
  216. }
  217. if (hover_type == Drag::NONE && limit_rect.has_point(p_mouse_pos)) {
  218. const Rect2 editor_rect = Rect2(Vector2(), CanvasItemEditor::get_singleton()->get_viewport_control()->get_size());
  219. const Rect2 transformed_rect = selected_camera->get_viewport()->get_canvas_transform().xform_inv(limit_rect);
  220. // Only allow center drag if any limit edge is visible on screen.
  221. bool edge_visible = false;
  222. edge_visible = edge_visible || (transformed_rect.get_end().y > editor_rect.position.y && transformed_rect.position.y < editor_rect.get_end().y && transformed_rect.position.x > editor_rect.position.x && transformed_rect.position.x < editor_rect.get_end().x);
  223. edge_visible = edge_visible || (transformed_rect.get_end().y > editor_rect.position.y && transformed_rect.position.y < editor_rect.get_end().y && transformed_rect.get_end().x > editor_rect.position.x && transformed_rect.get_end().x < editor_rect.get_end().x);
  224. edge_visible = edge_visible || (transformed_rect.get_end().x > editor_rect.position.x && transformed_rect.position.x < editor_rect.get_end().x && transformed_rect.position.y > editor_rect.position.y && transformed_rect.position.y < editor_rect.get_end().y);
  225. edge_visible = edge_visible || (transformed_rect.get_end().x > editor_rect.position.x && transformed_rect.position.x < editor_rect.get_end().x && transformed_rect.get_end().y > editor_rect.position.y && transformed_rect.get_end().y < editor_rect.get_end().y);
  226. if (edge_visible) {
  227. hover_type = Drag::CENTER;
  228. }
  229. }
  230. switch (hover_type) {
  231. case Drag::NONE: {
  232. CanvasItemEditor::get_singleton()->set_cursor_shape_override();
  233. } break;
  234. case Drag::LEFT:
  235. case Drag::RIGHT: {
  236. CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_HSIZE);
  237. } break;
  238. case Drag::TOP:
  239. case Drag::BOTTOM: {
  240. CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_VSIZE);
  241. } break;
  242. case Drag::TOP_LEFT:
  243. case Drag::BOTTOM_RIGHT: {
  244. CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_FDIAGSIZE);
  245. } break;
  246. case Drag::TOP_RIGHT:
  247. case Drag::BOTTOM_LEFT: {
  248. CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_BDIAGSIZE);
  249. } break;
  250. case Drag::CENTER: {
  251. CanvasItemEditor::get_singleton()->set_cursor_shape_override(CURSOR_MOVE);
  252. } break;
  253. }
  254. }
  255. void Camera2DEditor::_notification(int p_what) {
  256. switch (p_what) {
  257. case NOTIFICATION_THEME_CHANGED: {
  258. options->set_button_icon(get_editor_theme_icon(SNAME("Camera2D")));
  259. } break;
  260. }
  261. }
  262. void Camera2DEditor::_bind_methods() {
  263. ClassDB::bind_method(D_METHOD("_snap_limits_to_viewport", "camera"), &Camera2DEditor::_snap_limits_to_viewport);
  264. ClassDB::bind_method(D_METHOD("_update_overlays_if_needed", "camera"), &Camera2DEditor::_update_overlays_if_needed);
  265. }
  266. Camera2DEditor::Camera2DEditor(EditorPlugin *p_plugin) {
  267. plugin = p_plugin;
  268. options = memnew(MenuButton);
  269. options->set_text(TTRC("Camera2D"));
  270. options->set_flat(false);
  271. options->set_theme_type_variation("FlatMenuButtonNoIconTint");
  272. options->get_popup()->add_item(TTRC("Snap the Limits to the Viewport"), MENU_SNAP_LIMITS_TO_VIEWPORT);
  273. options->set_switch_on_hover(true);
  274. options->hide();
  275. CanvasItemEditor::get_singleton()->add_control_to_menu_panel(options);
  276. options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Camera2DEditor::_menu_option));
  277. }
  278. void Camera2DEditorPlugin::edit(Object *p_object) {
  279. camera_2d_editor->edit(Object::cast_to<Camera2D>(p_object));
  280. }
  281. bool Camera2DEditorPlugin::handles(Object *p_object) const {
  282. return p_object->is_class("Camera2D");
  283. }
  284. void Camera2DEditorPlugin::make_visible(bool p_visible) {
  285. if (p_visible) {
  286. camera_2d_editor->options->show();
  287. } else {
  288. camera_2d_editor->options->hide();
  289. }
  290. }
  291. Camera2DEditorPlugin::Camera2DEditorPlugin() {
  292. camera_2d_editor = memnew(Camera2DEditor(this));
  293. EditorNode::get_singleton()->get_gui_base()->add_child(camera_2d_editor);
  294. }