noise_editor_plugin.cpp 5.4 KB

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