Path: blob/master/editor/scene/2d/parallax_background_editor_plugin.cpp
9903 views
/**************************************************************************/1/* parallax_background_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "parallax_background_editor_plugin.h"3132#include "editor/docks/scene_tree_dock.h"33#include "editor/editor_node.h"34#include "editor/editor_undo_redo_manager.h"35#include "editor/scene/canvas_item_editor_plugin.h"36#include "scene/2d/parallax_2d.h"37#include "scene/2d/parallax_background.h"38#include "scene/2d/parallax_layer.h"39#include "scene/gui/box_container.h"40#include "scene/gui/menu_button.h"4142void ParallaxBackgroundEditorPlugin::edit(Object *p_object) {43parallax_background = Object::cast_to<ParallaxBackground>(p_object);44}4546bool ParallaxBackgroundEditorPlugin::handles(Object *p_object) const {47return Object::cast_to<ParallaxBackground>(p_object) != nullptr;48}4950void ParallaxBackgroundEditorPlugin::make_visible(bool p_visible) {51if (p_visible) {52toolbar->show();53} else {54toolbar->hide();55}56}5758void ParallaxBackgroundEditorPlugin::_menu_callback(int p_idx) {59if (p_idx == MENU_CONVERT_TO_PARALLAX_2D) {60convert_to_parallax2d();61}62}6364void ParallaxBackgroundEditorPlugin::convert_to_parallax2d() {65ParallaxBackground *parallax_bg = parallax_background;66TypedArray<Node> children = parallax_bg->get_children();6768EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();69ur->create_action(TTR("Convert to Parallax2D"), UndoRedo::MERGE_DISABLE, parallax_bg);7071for (int i = 0; i < children.size(); i++) {72ParallaxLayer *parallax_layer = Object::cast_to<ParallaxLayer>(children[i]);7374if (!parallax_layer) {75continue;76}7778Parallax2D *parallax2d = memnew(Parallax2D);7980Point2 offset = parallax_bg->get_scroll_base_offset() * parallax_layer->get_motion_scale();81offset += parallax_layer->get_motion_offset() + parallax_layer->get_position();82parallax2d->set_scroll_offset(offset);8384Point2 limit_begin = parallax2d->get_limit_begin();85Point2 limit_end = parallax2d->get_limit_end();8687if (parallax_bg->get_limit_begin().x != 0 || parallax_bg->get_limit_end().x != 0) {88limit_begin.x = parallax_bg->get_limit_begin().x;89limit_end.x = parallax_bg->get_limit_end().x;90}9192if (parallax_bg->get_limit_begin().y != 0 || parallax_bg->get_limit_end().y != 0) {93limit_begin.y = parallax_bg->get_limit_begin().y;94limit_end.y = parallax_bg->get_limit_end().y;95}9697parallax2d->set_limit_begin(limit_begin);98parallax2d->set_limit_end(limit_end);99parallax2d->set_follow_viewport(!parallax_bg->is_ignore_camera_zoom());100parallax2d->set_repeat_size(parallax_layer->get_mirroring());101parallax2d->set_scroll_scale(parallax_bg->get_scroll_base_scale() * parallax_layer->get_motion_scale());102103SceneTreeDock::get_singleton()->replace_node(parallax_layer, parallax2d);104}105106if (parallax_bg->is_ignore_camera_zoom()) {107CanvasLayer *canvas_layer = memnew(CanvasLayer);108SceneTreeDock::get_singleton()->replace_node(parallax_bg, canvas_layer);109} else {110Node2D *node2d = memnew(Node2D);111SceneTreeDock::get_singleton()->replace_node(parallax_bg, node2d);112}113114ur->commit_action(false);115}116117void ParallaxBackgroundEditorPlugin::_notification(int p_what) {118switch (p_what) {119case NOTIFICATION_ENTER_TREE: {120menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ParallaxBackgroundEditorPlugin::_menu_callback));121menu->set_button_icon(menu->get_editor_theme_icon(SNAME("ParallaxBackground")));122} break;123}124}125126ParallaxBackgroundEditorPlugin::ParallaxBackgroundEditorPlugin() {127toolbar = memnew(HBoxContainer);128toolbar->hide();129add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);130131menu = memnew(MenuButton);132menu->get_popup()->add_item(TTR("Convert to Parallax2D"), MENU_CONVERT_TO_PARALLAX_2D);133menu->set_text(TTR("ParallaxBackground"));134menu->set_switch_on_hover(true);135menu->set_flat(false);136menu->set_theme_type_variation("FlatMenuButton");137toolbar->add_child(menu);138}139140141