noise_editor_plugin.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* noise_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 "noise_editor_plugin.h"
  31. #include "../noise.h"
  32. #include "../noise_texture_2d.h"
  33. #include "editor/editor_inspector.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/texture_rect.h"
  37. class NoisePreview : public Control {
  38. GDCLASS(NoisePreview, Control)
  39. static const int PREVIEW_HEIGHT = 150;
  40. static const int PADDING_3D_SPACE_SWITCH = 2;
  41. Ref<Noise> _noise;
  42. Size2i _preview_texture_size;
  43. TextureRect *_texture_rect = nullptr;
  44. Button *_3d_space_switch = nullptr;
  45. public:
  46. NoisePreview() {
  47. set_custom_minimum_size(Size2(0, EDSCALE * PREVIEW_HEIGHT));
  48. _texture_rect = memnew(TextureRect);
  49. _texture_rect->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  50. _texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_COVERED);
  51. add_child(_texture_rect);
  52. _3d_space_switch = memnew(Button);
  53. _3d_space_switch->set_text(TTR("3D"));
  54. _3d_space_switch->set_tooltip_text(TTR("Toggles whether the noise preview is computed in 3D space."));
  55. _3d_space_switch->set_toggle_mode(true);
  56. _3d_space_switch->set_offset(SIDE_LEFT, PADDING_3D_SPACE_SWITCH);
  57. _3d_space_switch->set_offset(SIDE_TOP, PADDING_3D_SPACE_SWITCH);
  58. _3d_space_switch->connect(SceneStringName(pressed), callable_mp(this, &NoisePreview::_on_3d_button_pressed));
  59. add_child(_3d_space_switch);
  60. }
  61. void set_noise(Ref<Noise> noise) {
  62. if (_noise == noise) {
  63. return;
  64. }
  65. _noise = noise;
  66. if (_noise.is_valid()) {
  67. if (_noise->has_meta("_preview_in_3d_space_")) {
  68. _3d_space_switch->set_pressed(true);
  69. }
  70. update_preview();
  71. }
  72. }
  73. private:
  74. void _on_3d_button_pressed() {
  75. if (_3d_space_switch->is_pressed()) {
  76. _noise->set_meta("_preview_in_3d_space_", true);
  77. } else {
  78. _noise->remove_meta("_preview_in_3d_space_");
  79. }
  80. }
  81. void _notification(int p_what) {
  82. switch (p_what) {
  83. case NOTIFICATION_RESIZED: {
  84. _preview_texture_size = get_size();
  85. update_preview();
  86. } break;
  87. }
  88. }
  89. void update_preview() {
  90. if (MIN(_preview_texture_size.width, _preview_texture_size.height) > 0) {
  91. Ref<NoiseTexture2D> tex;
  92. tex.instantiate();
  93. tex->set_width(_preview_texture_size.width);
  94. tex->set_height(_preview_texture_size.height);
  95. tex->set_in_3d_space(_3d_space_switch->is_pressed());
  96. tex->set_noise(_noise);
  97. _texture_rect->set_texture(tex);
  98. }
  99. }
  100. };
  101. /////////////////////////////////////////////////////////////////////////////////
  102. class NoiseEditorInspectorPlugin : public EditorInspectorPlugin {
  103. GDCLASS(NoiseEditorInspectorPlugin, EditorInspectorPlugin)
  104. public:
  105. bool can_handle(Object *p_object) override {
  106. return Object::cast_to<Noise>(p_object) != nullptr;
  107. }
  108. void parse_begin(Object *p_object) override {
  109. Noise *noise_ptr = Object::cast_to<Noise>(p_object);
  110. if (noise_ptr) {
  111. Ref<Noise> noise(noise_ptr);
  112. NoisePreview *viewer = memnew(NoisePreview);
  113. viewer->set_noise(noise);
  114. add_custom_control(viewer);
  115. }
  116. }
  117. };
  118. /////////////////////////////////////////////////////////////////////////////////
  119. String NoiseEditorPlugin::get_plugin_name() const {
  120. return Noise::get_class_static();
  121. }
  122. NoiseEditorPlugin::NoiseEditorPlugin() {
  123. Ref<NoiseEditorInspectorPlugin> plugin;
  124. plugin.instantiate();
  125. add_inspector_plugin(plugin);
  126. }