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