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