Backported by hand: Add a GradientTexture2D editor plugin - fire-forge. (d76c9f8d92)

This commit is contained in:
Relintai 2022-03-20 14:59:19 +01:00
parent 9867b18f49
commit 5f7421974c
4 changed files with 471 additions and 44 deletions

View File

@ -32,40 +32,44 @@
#include <stdlib.h>
#include "core/array.h"
#include "core/class_db.h"
#include "core/color.h"
#include "core/engine.h"
#include "core/error_macros.h"
#include "core/image.h"
#include "core/io/config_file.h"
#include "core/io/image_loader.h"
#include "core/io/resource_importer.h"
#include "core/io/resource_loader.h"
#include "core/io/resource_saver.h"
#include "core/math/math_defs.h"
#include "core/math/vector2.h"
#include "core/object_id.h"
#include "core/os/dir_access.h"
#include "core/os/file_access.h"
#include "core/os/input.h"
#include "core/os/input_event.h"
#include "core/os/keyboard.h"
#include "core/os/main_loop.h"
#include "core/os/memory.h"
#include "core/os/os.h"
#include "core/pool_vector.h"
#include "core/print_string.h"
#include "core/project_settings.h"
#include "core/script_language.h"
#include "core/translation.h"
#include "core/undo_redo.h"
#include "core/version.h"
#include "main/input_default.h"
#include "main/main.h"
#include "scene/gui/control.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/file_dialog.h"
#include "scene/gui/link_button.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/panel.h"
#include "scene/gui/panel_container.h"
#include "scene/gui/split_container.h"
#include "scene/gui/tab_container.h"
#include "scene/gui/tabs.h"
#include "scene/gui/tool_button.h"
#include "scene/resources/packed_scene.h"
#include "servers/navigation_server.h"
#include "servers/physics_2d_server.h"
#include "core/version_generated.gen.h"
#include "editor/animation_track_editor.h"
#include "editor/audio_stream_preview.h"
#include "editor/dependency_editor.h"
#include "editor/doc/doc_data.h"
#include "editor/editor_about.h"
#include "editor/editor_audio_buses.h"
#include "editor/editor_export.h"
#include "editor/editor_file_dialog.h"
#include "editor/editor_file_system.h"
#include "editor/editor_help.h"
#include "editor/editor_inspector.h"
@ -85,6 +89,7 @@
#include "editor/export_template_manager.h"
#include "editor/fileserver/editor_file_server.h"
#include "editor/filesystem_dock.h"
#include "editor/import/editor_import_plugin.h"
#include "editor/import/resource_importer_bitmask.h"
#include "editor/import/resource_importer_csv_translation.h"
#include "editor/import/resource_importer_image.h"
@ -111,6 +116,7 @@
#include "editor/plugins/curve_editor_plugin.h"
#include "editor/plugins/editor_preview_plugins.h"
#include "editor/plugins/gradient_editor_plugin.h"
#include "editor/plugins/gradient_texture_2d_editor_plugin.h"
#include "editor/plugins/item_list_editor_plugin.h"
#include "editor/plugins/light_occluder_2d_editor_plugin.h"
#include "editor/plugins/line_2d_editor_plugin.h"
@ -143,53 +149,48 @@
#include "editor/progress_dialog.h"
#include "editor/project_export.h"
#include "editor/project_settings_editor.h"
#include "editor/property_editor.h"
#include "editor/quick_open.h"
#include "editor/register_exporters.h"
#include "editor/run_settings_dialog.h"
#include "editor/script_editor_debugger.h"
#include "editor/settings_config_dialog.h"
#include "scene/resources/environment.h"
#include "scene/resources/world.h"
#include "scene/main/viewport.h"
#include "core/array.h"
#include "core/color.h"
#include "core/engine.h"
#include "core/error_macros.h"
#include "core/image.h"
#include "core/io/resource_importer.h"
#include "core/math/math_defs.h"
#include "core/math/vector2.h"
#include "core/object_id.h"
#include "core/os/dir_access.h"
#include "core/os/input_event.h"
#include "core/os/main_loop.h"
#include "core/os/memory.h"
#include "core/pool_vector.h"
#include "core/script_language.h"
#include "core/undo_redo.h"
#include "core/version_generated.gen.h"
#include "editor/animation_track_editor.h"
#include "editor/doc/doc_data.h"
#include "editor/editor_file_dialog.h"
#include "editor/import/editor_import_plugin.h"
#include "editor/property_editor.h"
#include "editor/scene_tree_editor.h"
#include "editor/script_create_dialog.h"
#include "editor/script_editor_debugger.h"
#include "editor/settings_config_dialog.h"
#include "main/input_default.h"
#include "main/main.h"
#include "scene/animation/animation_player.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
#include "scene/gui/check_box.h"
#include "scene/gui/control.h"
#include "scene/gui/dialogs.h"
#include "scene/gui/file_dialog.h"
#include "scene/gui/label.h"
#include "scene/gui/link_button.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/option_button.h"
#include "scene/gui/panel.h"
#include "scene/gui/panel_container.h"
#include "scene/gui/popup.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/rich_text_label.h"
#include "scene/gui/scroll_bar.h"
#include "scene/gui/shortcut.h"
#include "scene/gui/split_container.h"
#include "scene/gui/tab_container.h"
#include "scene/gui/tabs.h"
#include "scene/gui/texture_rect.h"
#include "scene/gui/tool_button.h"
#include "scene/gui/tree.h"
#include "scene/main/scene_tree.h"
#include "scene/main/timer.h"
#include "scene/main/viewport.h"
#include "scene/resources/environment.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/world.h"
#include "servers/navigation_server.h"
#include "servers/physics_2d_server.h"
#include "servers/physics_server.h"
#include "servers/visual_server.h"
@ -2743,7 +2744,8 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
emit_signal("request_help_search", "");
} break;
case HELP_PANDEMONIUM_DOCS: {
OS::get_singleton()->shell_open("https://github.com/Relintai/pandemonium_engine/tree/master/doc/engine" "/");
OS::get_singleton()->shell_open("https://github.com/Relintai/pandemonium_engine/tree/master/doc/engine"
"/");
} break;
case HELP_GODOT_DOCS: {
OS::get_singleton()->shell_open(VERSION_DOCS_URL "/");
@ -6750,6 +6752,7 @@ EditorNode::EditorNode() {
add_editor_plugin(memnew(MeshEditorPlugin(this)));
add_editor_plugin(memnew(MaterialEditorPlugin(this)));
add_editor_plugin(memnew(ViewportPreviewEditorPlugin(this)));
add_editor_plugin(memnew(GradientTexture2DEditorPlugin(this)));
for (int i = 0; i < EditorPlugins::get_plugin_count(); i++) {
add_editor_plugin(EditorPlugins::create(i, this));

View File

@ -0,0 +1 @@
<svg clip-rule="evenodd" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><linearGradient id="a"><stop offset="0" stop-color="#e0e0e0"/><stop offset="1" stop-color="#e0e0e0" stop-opacity="0"/></linearGradient><linearGradient id="b" gradientUnits="userSpaceOnUse" x1=".99998" x2="15.00008" xlink:href="#a" y1="2.99998" y2="2.99998"/><linearGradient id="c" gradientTransform="matrix(-14.0001 0 0 14.0001 15 13)" gradientUnits="userSpaceOnUse" x1="0" x2="1" xlink:href="#a" y1="0" y2="0"/><path d="m1 1h14v4h-14z" fill="url(#b)"/><path d="m1 11h14v4h-14z" fill="url(#c)"/><path d="m6 6 2 4 2-4z" fill="#e0e0e0" fill-rule="nonzero"/></svg>

After

Width:  |  Height:  |  Size: 751 B

View File

@ -0,0 +1,299 @@
/*************************************************************************/
/* gradient_texture_2d_editor_plugin.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "gradient_texture_2d_editor_plugin.h"
#include "editor/editor_node.h"
#include "editor/editor_scale.h"
#include "scene/gui/box_container.h"
#include "scene/gui/flow_container.h"
#include "scene/gui/separator.h"
#include "editor/editor_spin_slider.h"
#include "scene/gui/texture_rect.h"
Point2 GradientTexture2DEditorRect::_get_handle_position(const Handle p_handle) {
// Get the handle's mouse position in pixels relative to offset.
const Vector2 percent = p_handle == HANDLE_FILL_FROM ? texture->get_fill_from() : texture->get_fill_to();
return Vector2(CLAMP(percent.x, 0, 1), CLAMP(percent.y, 0, 1)) * size;
}
void GradientTexture2DEditorRect::_update_fill_position() {
if (handle == HANDLE_NONE) {
return;
}
// Update the texture's fill_from/fill_to property based on mouse input.
Vector2 percent = (get_local_mouse_position() - offset) / size;
percent = Vector2(CLAMP(percent.x, 0, 1), CLAMP(percent.y, 0, 1));
if (snap_enabled) {
percent = (percent - Vector2(0.5, 0.5)).snapped(Vector2(snap_size, snap_size)) + Vector2(0.5, 0.5);
}
String property_name = handle == HANDLE_FILL_FROM ? "fill_from" : "fill_to";
undo_redo->create_action(vformat(TTR("Set %s"), property_name), UndoRedo::MERGE_ENDS);
undo_redo->add_do_property(texture.ptr(), property_name, percent);
undo_redo->add_undo_property(texture.ptr(), property_name, handle == HANDLE_FILL_FROM ? texture->get_fill_from() : texture->get_fill_to());
undo_redo->commit_action();
}
void GradientTexture2DEditorRect::_gui_input(const Ref<InputEvent> &p_event) {
// Grab/release handle.
const Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
if (mb->is_pressed()) {
Point2 mouse_position = mb->get_position() - offset;
if (Rect2(_get_handle_position(HANDLE_FILL_FROM).round() - handle_size / 2, handle_size).grow(2).has_point(mouse_position)) {
handle = HANDLE_FILL_FROM;
} else if (Rect2(_get_handle_position(HANDLE_FILL_TO).round() - handle_size / 2, handle_size).grow(2).has_point(mouse_position)) {
handle = HANDLE_FILL_TO;
} else {
handle = HANDLE_NONE;
}
} else {
_update_fill_position();
handle = HANDLE_NONE;
}
}
// Move handle.
const Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
_update_fill_position();
}
}
void GradientTexture2DEditorRect::set_texture(Ref<GradientTexture2D> &p_texture) {
texture = p_texture;
texture->connect("changed", this, "update");
}
void GradientTexture2DEditorRect::set_snap_enabled(bool p_snap_enabled) {
snap_enabled = p_snap_enabled;
update();
}
void GradientTexture2DEditorRect::set_snap_size(float p_snap_size) {
snap_size = p_snap_size;
update();
}
void GradientTexture2DEditorRect::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED: {
checkerboard->set_texture(get_icon("GuiMiniCheckerboard", "EditorIcons"));
} break;
case NOTIFICATION_DRAW: {
if (texture.is_null()) {
return;
}
const Ref<Texture> fill_from_icon = get_icon("EditorPathSmoothHandle", "EditorIcons");
const Ref<Texture> fill_to_icon = get_icon("EditorPathSharpHandle", "EditorIcons");
handle_size = fill_from_icon->get_size();
const int MAX_HEIGHT = 250 * EDSCALE;
Size2 rect_size = get_size();
// Get the size and position to draw the texture and handles at.
size = Size2(texture->get_width() * MAX_HEIGHT / texture->get_height(), MAX_HEIGHT);
if (size.width > rect_size.width) {
size.width = rect_size.width;
size.height = texture->get_height() * rect_size.width / texture->get_width();
}
offset = Point2(Math::round((rect_size.width - size.width) / 2), 0) + handle_size / 2;
set_custom_minimum_size(Size2(0, size.height));
size -= handle_size;
checkerboard->set_position(offset);
checkerboard->set_size(size);
draw_set_transform(offset, 0.0, Size2(1.0, 1.0));
draw_texture_rect(texture, Rect2(Point2(), size));
// Draw grid snap lines.
if (snap_enabled) {
const Color primary_line_color = Color(0.5, 0.5, 0.5, 0.9);
const Color line_color = Color(0.5, 0.5, 0.5, 0.5);
// Draw border and centered axis lines.
draw_rect(Rect2(Point2(), size), primary_line_color, false);
draw_line(Point2(size.width / 2, 0), Point2(size.width / 2, size.height), primary_line_color);
draw_line(Point2(0, size.height / 2), Point2(size.width, size.height / 2), primary_line_color);
// Draw vertical lines.
int prev_idx = 0;
for (int x = 0; x < size.width; x++) {
int idx = int((x / size.width - 0.5) / snap_size);
if (x > 0 && prev_idx != idx) {
draw_line(Point2(x, 0), Point2(x, size.height), line_color);
}
prev_idx = idx;
}
// Draw horizontal lines.
prev_idx = 0;
for (int y = 0; y < size.height; y++) {
int idx = int((y / size.height - 0.5) / snap_size);
if (y > 0 && prev_idx != idx) {
draw_line(Point2(0, y), Point2(size.width, y), line_color);
}
prev_idx = idx;
}
}
// Draw handles.
draw_texture(fill_from_icon, (_get_handle_position(HANDLE_FILL_FROM) - handle_size / 2).round());
draw_texture(fill_to_icon, (_get_handle_position(HANDLE_FILL_TO) - handle_size / 2).round());
} break;
}
}
void GradientTexture2DEditorRect::_bind_methods() {
ClassDB::bind_method("_gui_input", &GradientTexture2DEditorRect::_gui_input);
}
GradientTexture2DEditorRect::GradientTexture2DEditorRect() {
undo_redo = EditorNode::get_singleton()->get_undo_redo();
checkerboard = memnew(TextureRect);
checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
checkerboard->set_draw_behind_parent(true);
add_child(checkerboard);
}
///////////////////////
void GradientTexture2DEditor::_reverse_button_pressed() {
undo_redo->create_action(TTR("Swap GradientTexture2D Fill Points"));
undo_redo->add_do_property(texture.ptr(), "fill_from", texture->get_fill_to());
undo_redo->add_do_property(texture.ptr(), "fill_to", texture->get_fill_from());
undo_redo->add_undo_property(texture.ptr(), "fill_from", texture->get_fill_from());
undo_redo->add_undo_property(texture.ptr(), "fill_to", texture->get_fill_to());
undo_redo->commit_action();
}
void GradientTexture2DEditor::_set_snap_enabled(bool p_enabled) {
texture_editor_rect->set_snap_enabled(p_enabled);
snap_size_edit->set_visible(p_enabled);
}
void GradientTexture2DEditor::_set_snap_size(float p_snap_size) {
texture_editor_rect->set_snap_size(MAX(p_snap_size, 0.01));
}
void GradientTexture2DEditor::set_texture(Ref<GradientTexture2D> &p_texture) {
texture = p_texture;
texture_editor_rect->set_texture(p_texture);
}
void GradientTexture2DEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED: {
reverse_button->set_icon(get_icon("ReverseGradient", "EditorIcons"));
snap_button->set_icon(get_icon("SnapGrid", "EditorIcons"));
} break;
}
}
void GradientTexture2DEditor::_bind_methods() {
ClassDB::bind_method("_reverse_button_pressed", &GradientTexture2DEditor::_reverse_button_pressed);
ClassDB::bind_method("_set_snap_enabled", &GradientTexture2DEditor::_set_snap_enabled);
ClassDB::bind_method("_set_snap_size", &GradientTexture2DEditor::_set_snap_size);
}
GradientTexture2DEditor::GradientTexture2DEditor() {
undo_redo = EditorNode::get_singleton()->get_undo_redo();
HFlowContainer *toolbar = memnew(HFlowContainer);
add_child(toolbar);
reverse_button = memnew(Button);
reverse_button->set_tooltip(TTR("Swap Gradient Fill Points"));
toolbar->add_child(reverse_button);
reverse_button->connect("pressed", this, "_reverse_button_pressed");
toolbar->add_child(memnew(VSeparator));
snap_button = memnew(Button);
snap_button->set_tooltip(TTR("Toggle Grid Snap"));
snap_button->set_toggle_mode(true);
toolbar->add_child(snap_button);
snap_button->connect("toggled", this, "_set_snap_enabled");
snap_size_edit = memnew(EditorSpinSlider);
snap_size_edit->set_min(0.01);
snap_size_edit->set_max(0.5);
snap_size_edit->set_step(0.01);
snap_size_edit->set_value(0.1);
snap_size_edit->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
toolbar->add_child(snap_size_edit);
snap_size_edit->connect("value_changed", this, "_set_snap_size");
texture_editor_rect = memnew(GradientTexture2DEditorRect);
add_child(texture_editor_rect);
set_mouse_filter(MOUSE_FILTER_STOP);
_set_snap_enabled(snap_button->is_pressed());
_set_snap_size(snap_size_edit->get_value());
}
///////////////////////
bool EditorInspectorPluginGradientTexture2D::can_handle(Object *p_object) {
return Object::cast_to<GradientTexture2D>(p_object) != nullptr;
}
void EditorInspectorPluginGradientTexture2D::parse_begin(Object *p_object) {
GradientTexture2D *texture = Object::cast_to<GradientTexture2D>(p_object);
if (!texture) {
return;
}
Ref<GradientTexture2D> t(texture);
GradientTexture2DEditor *editor = memnew(GradientTexture2DEditor);
editor->set_texture(t);
add_custom_control(editor);
}
///////////////////////
GradientTexture2DEditorPlugin::GradientTexture2DEditorPlugin(EditorNode *p_node) {
Ref<EditorInspectorPluginGradientTexture2D> plugin;
plugin.instance();
add_inspector_plugin(plugin);
}

View File

@ -0,0 +1,124 @@
/*************************************************************************/
/* gradient_texture_2d_editor_plugin.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef GRADIENT_TEXTURE_2D_EDITOR
#define GRADIENT_TEXTURE_2D_EDITOR
#include "editor/editor_plugin.h"
#include "scene/gui/box_container.h"
#include "scene/gui/control.h"
#include "editor/editor_inspector.h"
#include "core/reference.h"
#include "scene/resources/texture.h"
class Button;
class EditorSpinSlider;
class TextureRect;
class UndoRedo;
class GradientTexture2DEditorRect : public Control {
GDCLASS(GradientTexture2DEditorRect, Control);
enum Handle {
HANDLE_NONE,
HANDLE_FILL_FROM,
HANDLE_FILL_TO
};
Ref<GradientTexture2D> texture;
UndoRedo *undo_redo = nullptr;
bool snap_enabled = false;
float snap_size = 0;
TextureRect *checkerboard = nullptr;
Handle handle = HANDLE_NONE;
Size2 handle_size;
Point2 offset;
Size2 size;
Point2 _get_handle_position(const Handle p_handle);
void _update_fill_position();
void _gui_input(const Ref<InputEvent> &p_event);
protected:
void _notification(int p_what);
static void _bind_methods();
public:
void set_texture(Ref<GradientTexture2D> &p_texture);
void set_snap_enabled(bool p_snap_enabled);
void set_snap_size(float p_snap_size);
GradientTexture2DEditorRect();
};
class GradientTexture2DEditor : public VBoxContainer {
GDCLASS(GradientTexture2DEditor, VBoxContainer);
Ref<GradientTexture2D> texture;
UndoRedo *undo_redo = nullptr;
Button *reverse_button = nullptr;
Button *snap_button = nullptr;
EditorSpinSlider *snap_size_edit = nullptr;
GradientTexture2DEditorRect *texture_editor_rect = nullptr;
void _reverse_button_pressed();
void _set_snap_enabled(bool p_enabled);
void _set_snap_size(float p_snap_size);
protected:
void _notification(int p_what);
static void _bind_methods();
public:
void set_texture(Ref<GradientTexture2D> &p_texture);
GradientTexture2DEditor();
};
class EditorInspectorPluginGradientTexture2D : public EditorInspectorPlugin {
GDCLASS(EditorInspectorPluginGradientTexture2D, EditorInspectorPlugin);
public:
virtual bool can_handle(Object *p_object);
virtual void parse_begin(Object *p_object);
};
class GradientTexture2DEditorPlugin : public EditorPlugin {
GDCLASS(GradientTexture2DEditorPlugin, EditorPlugin);
public:
GradientTexture2DEditorPlugin(EditorNode *p_node);
};
#endif