Path: blob/master/editor/scene/3d/camera_3d_editor_plugin.cpp
9902 views
/**************************************************************************/1/* camera_3d_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 "camera_3d_editor_plugin.h"3132#include "core/config/project_settings.h"33#include "editor/editor_node.h"34#include "node_3d_editor_plugin.h"35#include "scene/gui/texture_rect.h"36#include "scene/main/viewport.h"3738void Camera3DEditor::_node_removed(Node *p_node) {39if (p_node == node) {40node = nullptr;41Node3DEditor::get_singleton()->set_custom_camera(nullptr);42hide();43}44}4546void Camera3DEditor::_pressed() {47Node *sn = (node && preview->is_pressed()) ? node : nullptr;48Node3DEditor::get_singleton()->set_custom_camera(sn);49}5051void Camera3DEditor::edit(Node *p_camera) {52node = p_camera;5354if (!node) {55preview->set_pressed(false);56Node3DEditor::get_singleton()->set_custom_camera(nullptr);57} else {58if (preview->is_pressed()) {59Node3DEditor::get_singleton()->set_custom_camera(p_camera);60} else {61Node3DEditor::get_singleton()->set_custom_camera(nullptr);62}63}64}6566Camera3DEditor::Camera3DEditor() {67preview = memnew(Button);68add_child(preview);6970preview->set_text(TTR("Preview"));71preview->set_toggle_mode(true);72preview->set_anchor(SIDE_LEFT, Control::ANCHOR_END);73preview->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);74preview->set_offset(SIDE_LEFT, -60);75preview->set_offset(SIDE_RIGHT, 0);76preview->set_offset(SIDE_TOP, 0);77preview->set_offset(SIDE_BOTTOM, 10);78preview->connect(SceneStringName(pressed), callable_mp(this, &Camera3DEditor::_pressed));79}8081void Camera3DPreview::_update_sub_viewport_size() {82sub_viewport->set_size(Node3DEditor::get_camera_viewport_size(camera));83}8485Camera3DPreview::Camera3DPreview(Camera3D *p_camera) :86TexturePreview(nullptr, false), camera(p_camera), sub_viewport(memnew(SubViewport)) {87RenderingServer::get_singleton()->viewport_attach_camera(sub_viewport->get_viewport_rid(), camera->get_camera());88add_child(sub_viewport);8990TextureRect *display = get_texture_display();91display->set_texture(sub_viewport->get_texture());92sub_viewport->connect("size_changed", callable_mp((CanvasItem *)display, &CanvasItem::queue_redraw));93sub_viewport->get_texture()->connect_changed(callable_mp((TexturePreview *)this, &Camera3DPreview::_update_texture_display_ratio));9495ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Camera3DPreview::_update_sub_viewport_size));96_update_sub_viewport_size();97}9899bool EditorInspectorPluginCamera3DPreview::can_handle(Object *p_object) {100return Object::cast_to<Camera3D>(p_object) != nullptr;101}102103void EditorInspectorPluginCamera3DPreview::parse_begin(Object *p_object) {104Camera3D *camera = Object::cast_to<Camera3D>(p_object);105Camera3DPreview *preview = memnew(Camera3DPreview(camera));106add_custom_control(preview);107}108109void Camera3DEditorPlugin::edit(Object *p_object) {110Node3DEditor::get_singleton()->set_can_preview(Object::cast_to<Camera3D>(p_object));111}112113bool Camera3DEditorPlugin::handles(Object *p_object) const {114return p_object->is_class("Camera3D");115}116117void Camera3DEditorPlugin::make_visible(bool p_visible) {118if (!p_visible) {119Node3DEditor::get_singleton()->set_can_preview(nullptr);120}121}122123Camera3DEditorPlugin::Camera3DEditorPlugin() {124Ref<EditorInspectorPluginCamera3DPreview> plugin;125plugin.instantiate();126add_inspector_plugin(plugin);127}128129130