camera_3d_editor_plugin.cpp 5.4 KB

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