Debian Codegraph @

editor/scene/2d/parallax_background_editor_plugin.cpp

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