parallax_background_editor_plugin.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**************************************************************************/
  2. /* parallax_background_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 "parallax_background_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/scene_tree_dock.h"
  35. #include "scene/2d/parallax_2d.h"
  36. #include "scene/2d/parallax_background.h"
  37. #include "scene/2d/parallax_layer.h"
  38. #include "scene/gui/box_container.h"
  39. #include "scene/gui/menu_button.h"
  40. void ParallaxBackgroundEditorPlugin::edit(Object *p_object) {
  41. parallax_background = Object::cast_to<ParallaxBackground>(p_object);
  42. }
  43. bool ParallaxBackgroundEditorPlugin::handles(Object *p_object) const {
  44. return Object::cast_to<ParallaxBackground>(p_object) != nullptr;
  45. }
  46. void ParallaxBackgroundEditorPlugin::make_visible(bool p_visible) {
  47. if (p_visible) {
  48. toolbar->show();
  49. } else {
  50. toolbar->hide();
  51. }
  52. }
  53. void ParallaxBackgroundEditorPlugin::_menu_callback(int p_idx) {
  54. if (p_idx == MENU_CONVERT_TO_PARALLAX_2D) {
  55. convert_to_parallax2d();
  56. }
  57. }
  58. void ParallaxBackgroundEditorPlugin::convert_to_parallax2d() {
  59. ParallaxBackground *parallax_bg = parallax_background;
  60. TypedArray<Node> children = parallax_bg->get_children();
  61. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  62. ur->create_action(TTR("Convert to Parallax2D"), UndoRedo::MERGE_DISABLE, parallax_bg);
  63. for (int i = 0; i < children.size(); i++) {
  64. ParallaxLayer *parallax_layer = Object::cast_to<ParallaxLayer>(children[i]);
  65. if (!parallax_layer) {
  66. continue;
  67. }
  68. Parallax2D *parallax2d = memnew(Parallax2D);
  69. Point2 offset = parallax_bg->get_scroll_base_offset() * parallax_layer->get_motion_scale();
  70. offset += parallax_layer->get_motion_offset() + parallax_layer->get_position();
  71. parallax2d->set_scroll_offset(offset);
  72. Point2 limit_begin = parallax2d->get_limit_begin();
  73. Point2 limit_end = parallax2d->get_limit_end();
  74. if (parallax_bg->get_limit_begin().x != 0 || parallax_bg->get_limit_end().x != 0) {
  75. limit_begin.x = parallax_bg->get_limit_begin().x;
  76. limit_end.x = parallax_bg->get_limit_end().x;
  77. }
  78. if (parallax_bg->get_limit_begin().y != 0 || parallax_bg->get_limit_end().y != 0) {
  79. limit_begin.y = parallax_bg->get_limit_begin().y;
  80. limit_end.y = parallax_bg->get_limit_end().y;
  81. }
  82. parallax2d->set_limit_begin(limit_begin);
  83. parallax2d->set_limit_end(limit_end);
  84. parallax2d->set_follow_viewport(!parallax_bg->is_ignore_camera_zoom());
  85. parallax2d->set_repeat_size(parallax_layer->get_mirroring());
  86. parallax2d->set_scroll_scale(parallax_bg->get_scroll_base_scale() * parallax_layer->get_motion_scale());
  87. SceneTreeDock::get_singleton()->replace_node(parallax_layer, parallax2d);
  88. }
  89. if (parallax_bg->is_ignore_camera_zoom()) {
  90. CanvasLayer *canvas_layer = memnew(CanvasLayer);
  91. SceneTreeDock::get_singleton()->replace_node(parallax_bg, canvas_layer);
  92. } else {
  93. Node2D *node2d = memnew(Node2D);
  94. SceneTreeDock::get_singleton()->replace_node(parallax_bg, node2d);
  95. }
  96. ur->commit_action(false);
  97. }
  98. void ParallaxBackgroundEditorPlugin::_notification(int p_what) {
  99. switch (p_what) {
  100. case NOTIFICATION_ENTER_TREE: {
  101. menu->get_popup()->connect("id_pressed", callable_mp(this, &ParallaxBackgroundEditorPlugin::_menu_callback));
  102. menu->set_icon(menu->get_editor_theme_icon(SNAME("ParallaxBackground")));
  103. } break;
  104. }
  105. }
  106. ParallaxBackgroundEditorPlugin::ParallaxBackgroundEditorPlugin() {
  107. toolbar = memnew(HBoxContainer);
  108. toolbar->hide();
  109. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  110. menu = memnew(MenuButton);
  111. menu->get_popup()->add_item(TTR("Convert to Parallax2D"), MENU_CONVERT_TO_PARALLAX_2D);
  112. menu->set_text(TTR("ParallaxBackground"));
  113. menu->set_switch_on_hover(true);
  114. toolbar->add_child(menu);
  115. }