| 1 | /**************************************************************************/ |
| 2 | /* camera_3d_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 "camera_3d_editor_plugin.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "editor/editor_node.h" |
| 35 | #include "node_3d_editor_plugin.h" |
| 36 | #include "scene/gui/texture_rect.h" |
| 37 | #include "scene/main/viewport.h" |
| 38 | |
| 39 | void Camera3DEditor::_node_removed(Node *p_node) { |
| 40 | if (p_node == node) { |
| 41 | node = nullptr; |
| 42 | Node3DEditor::get_singleton()->set_custom_camera(nullptr); |
| 43 | hide(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void Camera3DEditor::_pressed() { |
| 48 | Node *sn = (node && preview->is_pressed()) ? node : nullptr; |
| 49 | Node3DEditor::get_singleton()->set_custom_camera(sn); |
| 50 | } |
| 51 | |
| 52 | void Camera3DEditor::edit(Node *p_camera) { |
| 53 | node = p_camera; |
| 54 | |
| 55 | if (!node) { |
| 56 | preview->set_pressed(false); |
| 57 | Node3DEditor::get_singleton()->set_custom_camera(nullptr); |
| 58 | } else { |
| 59 | if (preview->is_pressed()) { |
| 60 | Node3DEditor::get_singleton()->set_custom_camera(p_camera); |
| 61 | } else { |
| 62 | Node3DEditor::get_singleton()->set_custom_camera(nullptr); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | Camera3DEditor::Camera3DEditor() { |
| 68 | preview = memnew(Button); |
| 69 | add_child(preview); |
| 70 | |
| 71 | preview->set_text(TTR("Preview")); |
| 72 | preview->set_toggle_mode(true); |
| 73 | preview->set_anchor(SIDE_LEFT, Control::ANCHOR_END); |
| 74 | preview->set_anchor(SIDE_RIGHT, Control::ANCHOR_END); |
| 75 | preview->set_offset(SIDE_LEFT, -60); |
| 76 | preview->set_offset(SIDE_RIGHT, 0); |
| 77 | preview->set_offset(SIDE_TOP, 0); |
| 78 | preview->set_offset(SIDE_BOTTOM, 10); |
| 79 | preview->connect(SceneStringName(pressed), callable_mp(this, &Camera3DEditor::_pressed)); |
| 80 | } |
| 81 | |
| 82 | void Camera3DPreview::_update_sub_viewport_size() { |
| 83 | sub_viewport->set_size(Node3DEditor::get_camera_viewport_size(camera)); |
| 84 | } |
| 85 | |
| 86 | Camera3DPreview::Camera3DPreview(Camera3D *p_camera) : |
| 87 | TexturePreview(nullptr, false), camera(p_camera), sub_viewport(memnew(SubViewport)) { |
| 88 | RenderingServer::get_singleton()->viewport_attach_camera(sub_viewport->get_viewport_rid(), camera->get_camera()); |
| 89 | add_child(sub_viewport); |
| 90 | |
| 91 | TextureRect *display = get_texture_display(); |
| 92 | display->set_texture(sub_viewport->get_texture()); |
| 93 | sub_viewport->connect("size_changed", callable_mp((CanvasItem *)display, &CanvasItem::queue_redraw)); |
| 94 | sub_viewport->get_texture()->connect_changed(callable_mp((TexturePreview *)this, &Camera3DPreview::_update_texture_display_ratio)); |
| 95 | |
| 96 | ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Camera3DPreview::_update_sub_viewport_size)); |
| 97 | _update_sub_viewport_size(); |
| 98 | } |
| 99 | |
| 100 | bool EditorInspectorPluginCamera3DPreview::can_handle(Object *p_object) { |
| 101 | return Object::cast_to<Camera3D>(p_object) != nullptr; |
| 102 | } |
| 103 | |
| 104 | void EditorInspectorPluginCamera3DPreview::parse_begin(Object *p_object) { |
| 105 | Camera3D *camera = Object::cast_to<Camera3D>(p_object); |
| 106 | Camera3DPreview *preview = memnew(Camera3DPreview(camera)); |
| 107 | add_custom_control(preview); |
| 108 | } |
| 109 | |
| 110 | void Camera3DEditorPlugin::edit(Object *p_object) { |
| 111 | Node3DEditor::get_singleton()->set_can_preview(Object::cast_to<Camera3D>(p_object)); |
| 112 | } |
| 113 | |
| 114 | bool Camera3DEditorPlugin::handles(Object *p_object) const { |
| 115 | return p_object->is_class("Camera3D"); |
| 116 | } |
| 117 | |
| 118 | void Camera3DEditorPlugin::make_visible(bool p_visible) { |
| 119 | if (!p_visible) { |
| 120 | Node3DEditor::get_singleton()->set_can_preview(nullptr); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | Camera3DEditorPlugin::Camera3DEditorPlugin() { |
| 125 | Ref<EditorInspectorPluginCamera3DPreview> plugin; |
| 126 | plugin.instantiate(); |
| 127 | add_inspector_plugin(plugin); |
| 128 | } |