Path: blob/master/modules/noise/editor/noise_editor_plugin.cpp
10279 views
/**************************************************************************/1/* noise_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "noise_editor_plugin.h"3132#include "../noise.h"33#include "../noise_texture_2d.h"3435#include "editor/inspector/editor_inspector.h"36#include "editor/themes/editor_scale.h"37#include "scene/gui/button.h"38#include "scene/gui/texture_rect.h"3940class NoisePreview : public Control {41GDCLASS(NoisePreview, Control)4243static const int PREVIEW_HEIGHT = 150;44static const int PADDING_3D_SPACE_SWITCH = 2;4546Ref<Noise> _noise;47Size2i _preview_texture_size;4849TextureRect *_texture_rect = nullptr;50Button *_3d_space_switch = nullptr;5152public:53NoisePreview() {54set_custom_minimum_size(Size2(0, EDSCALE * PREVIEW_HEIGHT));5556_texture_rect = memnew(TextureRect);57_texture_rect->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);58_texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_COVERED);59add_child(_texture_rect);6061_3d_space_switch = memnew(Button);62_3d_space_switch->set_text(TTR("3D"));63_3d_space_switch->set_tooltip_text(TTR("Toggles whether the noise preview is computed in 3D space."));64_3d_space_switch->set_toggle_mode(true);65_3d_space_switch->set_offset(SIDE_LEFT, PADDING_3D_SPACE_SWITCH);66_3d_space_switch->set_offset(SIDE_TOP, PADDING_3D_SPACE_SWITCH);67_3d_space_switch->connect(SceneStringName(pressed), callable_mp(this, &NoisePreview::_on_3d_button_pressed));68add_child(_3d_space_switch);69}7071void set_noise(Ref<Noise> noise) {72if (_noise == noise) {73return;74}75_noise = noise;76if (_noise.is_valid()) {77if (_noise->has_meta("_preview_in_3d_space_")) {78_3d_space_switch->set_pressed(true);79}8081update_preview();82}83}8485private:86void _on_3d_button_pressed() {87if (_3d_space_switch->is_pressed()) {88_noise->set_meta("_preview_in_3d_space_", true);89} else {90_noise->remove_meta("_preview_in_3d_space_");91}92}9394void _notification(int p_what) {95switch (p_what) {96case NOTIFICATION_RESIZED: {97_preview_texture_size = get_size();98update_preview();99} break;100}101}102103void update_preview() {104if (MIN(_preview_texture_size.width, _preview_texture_size.height) > 0) {105Ref<NoiseTexture2D> tex;106tex.instantiate();107tex->set_width(_preview_texture_size.width);108tex->set_height(_preview_texture_size.height);109tex->set_in_3d_space(_3d_space_switch->is_pressed());110tex->set_noise(_noise);111_texture_rect->set_texture(tex);112}113}114};115116/////////////////////////////////////////////////////////////////////////////////117118class NoiseEditorInspectorPlugin : public EditorInspectorPlugin {119GDCLASS(NoiseEditorInspectorPlugin, EditorInspectorPlugin)120public:121bool can_handle(Object *p_object) override {122return Object::cast_to<Noise>(p_object) != nullptr;123}124125void parse_begin(Object *p_object) override {126Noise *noise_ptr = Object::cast_to<Noise>(p_object);127if (noise_ptr) {128Ref<Noise> noise(noise_ptr);129130NoisePreview *viewer = memnew(NoisePreview);131viewer->set_noise(noise);132add_custom_control(viewer);133}134}135};136137/////////////////////////////////////////////////////////////////////////////////138139String NoiseEditorPlugin::get_plugin_name() const {140return Noise::get_class_static();141}142143NoiseEditorPlugin::NoiseEditorPlugin() {144Ref<NoiseEditorInspectorPlugin> plugin;145plugin.instantiate();146add_inspector_plugin(plugin);147}148149150