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