Fixed the remaining compile issues for the gradient editor, and added it to the build.

This commit is contained in:
Relintai 2022-06-14 16:43:44 +02:00
parent dbca350602
commit 35fadcb6bb
7 changed files with 120 additions and 97 deletions

View File

@ -37,9 +37,9 @@ sources = [
"editor/widgets/float_edit/float_edit.cpp", "editor/widgets/float_edit/float_edit.cpp",
#"editor/widgets/gradient_editor/gradient_cursor.cpp", "editor/widgets/gradient_editor/gradient_cursor.cpp",
#"editor/widgets/gradient_editor/gradient_editor.cpp", "editor/widgets/gradient_editor/gradient_editor.cpp",
#"editor/widgets/gradient_editor/gradient_popup.cpp", "editor/widgets/gradient_editor/gradient_popup.cpp",
#"editor/widgets//.cpp", #"editor/widgets//.cpp",
#"editor/widgets//.cpp", #"editor/widgets//.cpp",

View File

@ -95,8 +95,8 @@ float GradientCursor::get_cursor_position() {
return get_position().x / (get_parent_control()->get_size().x - WIDTH); return get_position().x / (get_parent_control()->get_size().x - WIDTH);
} }
bool GradientCursor::sort(GradientCursor *a, const GradientCursor *b) { bool GradientCursor::operator<(const GradientCursor &b) const {
return a->get_position() < b->get_position(); return get_position() < b.get_position();
} }
bool GradientCursor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { bool GradientCursor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
@ -144,3 +144,5 @@ void GradientCursor::_bind_methods() {
ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &GradientCursor::can_drop_data_fw); ClassDB::bind_method(D_METHOD("can_drop_data_fw", "point", "data", "from"), &GradientCursor::can_drop_data_fw);
ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &GradientCursor::drop_data_fw); ClassDB::bind_method(D_METHOD("drop_data_fw", "point", "data", "from"), &GradientCursor::drop_data_fw);
} }
const int GradientCursor::WIDTH = 10;

View File

@ -27,7 +27,7 @@ public:
float get_cursor_position(); float get_cursor_position();
static bool sort(GradientCursor *a, GradientCursor *b); bool operator<(const GradientCursor &b) const;
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const; bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from); void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
@ -35,6 +35,8 @@ public:
GradientCursor(); GradientCursor();
~GradientCursor(); ~GradientCursor();
static const int WIDTH;
protected: protected:
void _notification(int p_what); void _notification(int p_what);
@ -43,8 +45,6 @@ protected:
Color color; Color color;
bool sliding; bool sliding;
Label *label; Label *label;
const int WIDTH = 10;
}; };
#endif #endif

View File

@ -2,11 +2,19 @@
#include "gradient_editor.h" #include "gradient_editor.h"
#include "../../../nodes/bases/gradient_base.h" #include "../../../nodes/bases/gradient_base.h"
#include "../../../nodes/mm_node_universal_property.h"
#include "../../mm_graph_node.h" #include "../../mm_graph_node.h"
#include "gradient_cursor.h"
#include "gradient_popup.h"
#include "../color_picker_popup/color_picker_popup.h"
#include "scene/gui/color_picker.h"
#include "scene/gui/color_rect.h" #include "scene/gui/color_rect.h"
#include "scene/gui/label.h" #include "scene/gui/label.h"
#include "scene/gui/option_button.h" #include "scene/gui/option_button.h"
#include "scene/gui/texture_rect.h"
#include "scene/resources/material.h" #include "scene/resources/material.h"
MMGraphNode *GradientEditor::get_graph_node() { MMGraphNode *GradientEditor::get_graph_node() {
@ -23,6 +31,8 @@ Ref<GradientBase> GradientEditor::get_value() {
void GradientEditor::set_value(const Ref<GradientBase> &val) { void GradientEditor::set_value(const Ref<GradientBase> &val) {
value = val; value = val;
update_preview();
call_deferred("update_cursors");
} }
bool GradientEditor::get_embedded() const { bool GradientEditor::get_embedded() const {
@ -84,77 +94,75 @@ void GradientEditor::undo_redo_save_color_state() {
op.push_back(_saved_points[i]); op.push_back(_saved_points[i]);
} }
for (v in value->get_points()) { PoolRealArray array = value->get_points();
np.push_back(v);
for (int i = 0; i < array.size(); ++i) {
np.push_back(array[i]);
} }
_undo_redo.create_action("MMGD: gradient colors changed"); _undo_redo->create_action("MMGD: gradient colors changed");
_undo_redo.add_do_method(value, "set_points", np); _undo_redo->add_do_method(*value, "set_points", np);
_undo_redo.add_undo_method(value, "set_points", op); _undo_redo->add_undo_method(*value, "set_points", op);
_undo_redo.commit_action(); _undo_redo->commit_action();
ignore_changes(false); ignore_changes(false);
} }
void GradientEditor::set_undo_redo(const UndoRedo &ur) {
_undo_redo = ur;
}
void GradientEditor::set_value(const Variant &v) {
value = v;
update_preview();
call_deferred("update_cursors");
}
void GradientEditor::update_cursors() { void GradientEditor::update_cursors() {
for (c in get_children()) { for (int i = 0; i < get_child_count(); ++i) {
if (c is GradientCursor) { GradientCursor *c = Object::cast_to<GradientCursor>(get_child(i));
if (c) {
remove_child(c); remove_child(c);
c.free(); c->queue_delete();
} }
} }
int vs = value.get_point_count(); int vs = value->get_point_count();
for (int i = 0; i < vs; ++i) { //i in range(vs) for (int i = 0; i < vs; ++i) { //i in range(vs)
add_cursor(value.get_point_value(i) * (rect_size.x - GradientCursor.WIDTH), value.get_point_color(i)); add_cursor(value->get_point_value(i) * (get_size().x - GradientCursor::WIDTH), value->get_point_color(i));
} }
$Interpolation.selected = value.interpolation_type; interpolation->select(value->get_interpolation_type());
} }
void GradientEditor::update_value() { void GradientEditor::update_value() {
value.clear(); value->clear();
Array sc = get_sorted_cursors(); Vector<GradientCursor *> sc = get_sorted_cursors();
PoolRealArray points = PoolRealArray(); PoolRealArray points;
for (c in sc) { for (int i = 0; i < sc.size(); ++i) {
points.push_back(c.rect_position.x / (rect_size.x - GradientCursor.WIDTH)); GradientCursor *c = sc[i];
Color color = c.color;
points.push_back(c->get_position().x / (get_size().x - GradientCursor::WIDTH));
Color color = c->get_color();
points.push_back(color.r); points.push_back(color.r);
points.push_back(color.g); points.push_back(color.g);
points.push_back(color.b); points.push_back(color.b);
points.push_back(color.a); points.push_back(color.a);
} }
value.set_points(points); value->set_points(points);
update_preview(); update_preview();
} }
void GradientEditor::add_cursor(const Variant &x, const Variant &color) { void GradientEditor::add_cursor(const float x, const Color &color) {
GradientCursor *cursor = GradientCursor.new(); GradientCursor *cursor = memnew(GradientCursor);
cursor->set_label(label);
add_child(cursor); add_child(cursor);
cursor.rect_position.x = x;
cursor.color = color; Vector2 rp = cursor->get_position();
rp.x = x;
cursor->set_position(rp);
cursor->set_color(color);
} }
void GradientEditor::_gui_input(const Ref<InputEvent> &ev) { void GradientEditor::_gui_input(const Ref<InputEvent> &ev) {
Ref<InputEventMouseButton> iemb = ev; Ref<InputEventMouseButton> iemb = ev;
if (iemb.is_valid() && ev->get_button_index() == 1 && iemb->get_doubleclick()) { if (iemb.is_valid() && iemb->get_button_index() == 1 && iemb->is_doubleclick()) {
if (iemb->get_position().y > 15) { if (iemb->get_position().y > 15) {
Variant = clamp(iemb->get_position().x, 0, rect_size.x - GradientCursor.WIDTH); float p = CLAMP(iemb->get_position().x, 0, get_size().x - GradientCursor::WIDTH);
save_color_state(); save_color_state();
add_cursor(p, get_gradient_color(p)); add_cursor(p, get_gradient_color(p));
update_value(); update_value();
@ -163,10 +171,10 @@ void GradientEditor::_gui_input(const Ref<InputEvent> &ev) {
GradientPopup *popup = memnew(GradientPopup); GradientPopup *popup = memnew(GradientPopup);
add_child(popup); add_child(popup);
Vector2 popup_size = popup->get_size(); Vector2 popup_size = popup->get_size();
popup->popup(Rect2(ev->get_global_position(), Vector2(0, 0))); popup->popup(Rect2(iemb->get_global_position(), Vector2(0, 0)));
popup->set_global_position(ev->get_global_position() - Vector2(popup_size.x / 2, popup_size.y)); popup->set_global_position(iemb->get_global_position() - Vector2(popup_size.x / 2, popup_size.y));
popup->init(value, graph_node, _undo_redo); popup->init(value, graph_node, _undo_redo);
popup->connect("updated", self, "set_value"); popup->connect("updated", this, "set_value");
popup->connect("popup_hide", popup, "queue_free"); popup->connect("popup_hide", popup, "queue_free");
} }
@ -174,61 +182,67 @@ void GradientEditor::_gui_input(const Ref<InputEvent> &ev) {
} }
} }
void GradientEditor::select_color(const GradientCursor *cursor, const Vector2 &position) { void GradientEditor::select_color(GradientCursor *cursor, const Vector2 &position) {
active_cursor = cursor; active_cursor = cursor;
//var color_picker_popup = preload("res://addons/mat_maker_gd/widgets/color_picker_popup/color_picker_popup.tscn").instance();
ColorPickerPopup *color_picker_popup = memnew(ColorPickerPopup);
add_child(color_picker_popup); add_child(color_picker_popup);
Variant = color_picker_popup.get_node("ColorPicker");
color_picker.color = cursor.color; ColorPicker *color_picker = color_picker_popup->color_picker;
color_picker.connect("color_changed", cursor, "set_color"); color_picker->set_pick_color(cursor->get_color());
color_picker_popup.rect_position = position; color_picker->connect("color_changed", cursor, "set_color");
color_picker_popup.connect("popup_hide", self, "undo_redo_save_color_state");
color_picker_popup.connect("popup_hide", color_picker_popup, "queue_free"); color_picker_popup->set_position(position);
color_picker_popup.popup(); color_picker_popup->connect("popup_hide", this, "undo_redo_save_color_state");
color_picker_popup->connect("popup_hide", color_picker_popup, "queue_free");
color_picker_popup->popup();
} }
// Calculating a color from the gradient and generating the shader; // Calculating a color from the gradient and generating the shader;
Array GradientEditor::get_sorted_cursors() { Vector<GradientCursor *> GradientEditor::get_sorted_cursors() {
Variant = []; Vector<GradientCursor *> array;
for (c in get_children()) { for (int i = 0; i < get_child_count(); ++i) {
if (c is GradientCursor) { GradientCursor *c = Object::cast_to<GradientCursor>(get_child(i));
array.append(c);
if (c) {
array.push_back(c);
} }
} }
array.sort_custom(GradientCursor, "sort"); array.sort();
return array; return array;
} }
void GradientEditor::generate_preview_image() { void GradientEditor::generate_preview_image() {
Ref<ImageTexture> tex = $Gradient.texture; Ref<ImageTexture> tex = gradient->get_texture();
if (!tex.is_valid()) { if (!tex.is_valid()) {
tex.instance(); tex.instance();
$Gradient.texture = tex; gradient->set_texture(tex);
} }
Ref<Image> img = tex.get_data(); Ref<Image> img = tex->get_data();
float w = $Gradient.rect_size.x; float w = gradient->get_size().x;
float h = $Gradient.rect_size.y; float h = gradient->get_size().y;
if (!img.is_valid()) { if (!img.is_valid()) {
img = memnew(Image); img.instance();
} }
if (img->get_size().x != w || img.get_size().y != h) { if (img->get_size().x != w || img->get_size().y != h) {
img->create(w, h, false, Image.FORMAT_RGBA8); img->create(w, h, false, Image::FORMAT_RGBA8);
} }
img->lock(); img->lock();
for (int i = 0; i < w; ++i) { //i in range(w) for (int i = 0; i < w; ++i) {
float x = float(i) / float(w); float x = float(i) / float(w);
Color col = value->get_gradient_color(x); Color col = value->get_gradient_color(x);
for (int j = 0; j < h; ++j) { //j in range(h) for (int j = 0; j < h; ++j) {
img->set_pixel(i, j, col); img->set_pixel(i, j, col);
} }
} }
@ -238,7 +252,7 @@ void GradientEditor::generate_preview_image() {
} }
Color GradientEditor::get_gradient_color(const float x) { Color GradientEditor::get_gradient_color(const float x) {
return value->get_gradient_color(x / (get_size().x - GradientCursor->WIDTH)); return value->get_gradient_color(x / (get_size().x - GradientCursor::WIDTH));
} }
void GradientEditor::update_preview() { void GradientEditor::update_preview() {
@ -247,10 +261,10 @@ void GradientEditor::update_preview() {
void GradientEditor::_on_Interpolation_item_selected(const int ID) { void GradientEditor::_on_Interpolation_item_selected(const int ID) {
ignore_changes(true); ignore_changes(true);
_undo_redo.create_action("MMGD: gradient interpolation_type changed"); _undo_redo->create_action("MMGD: gradient interpolation_type changed");
_undo_redo.add_do_method(value, "set_interpolation_type", ID); _undo_redo->add_do_method(value.ptr(), "set_interpolation_type", ID);
_undo_redo.add_undo_method(value, "set_interpolation_type", value->get_interpolation_type()); _undo_redo->add_undo_method(value.ptr(), "set_interpolation_type", value->get_interpolation_type());
_undo_redo.commit_action(); _undo_redo->commit_action();
ignore_changes(false); ignore_changes(false);
update_preview(); update_preview();
} }
@ -287,22 +301,22 @@ GradientEditor::GradientEditor() {
Ref<ShaderMaterial> background_control_prop_material; Ref<ShaderMaterial> background_control_prop_material;
background_control_prop_material.instance(); background_control_prop_material.instance();
background_control_prop_material->set_shader(bg_shader_code); background_control_prop_material->set_shader(bg_shader);
background_control->set_material(background_control_prop_material); background_control->set_material(background_control_prop_material);
add_child(background_control); add_child(background_control);
TextureRect *gradient_control = memnew(TextureRect); gradient = memnew(TextureRect);
add_child(gradient_control); gradient->set_custom_minimum_size(Vector2(112, 17));
gradient_control->set_rect_min_size(Vector2(112, 17)); gradient->set_mouse_filter(MOUSE_FILTER_IGNORE);
gradient_control->set_mouse_filter(MOUSE_FILTER_IGNORE); add_child(gradient);
Ref<Theme> gradient_control_prop_theme; Ref<Theme> gradient_control_prop_theme;
gradient_control_prop_theme.instance(); gradient_control_prop_theme.instance();
gradient_control->set_theme(gradient_control_prop_theme); gradient->set_theme(gradient_control_prop_theme);
OptionButton *interpolation_control = memnew(OptionButton); OptionButton *interpolation_control = memnew(OptionButton);
interpolation_control->set_rect_scale(Vector2(0.5, 0.5)); interpolation_control->set_scale(Vector2(0.5, 0.5));
interpolation_control->add_item("0", 0); interpolation_control->add_item("0", 0);
interpolation_control->add_item("1", 1); interpolation_control->add_item("1", 1);
@ -314,19 +328,20 @@ GradientEditor::GradientEditor() {
//interpolation_control->set_icon(interpolation_control_prop_icon); //interpolation_control->set_icon(interpolation_control_prop_icon);
//interpolation_control->set("icon", interpolation_control_prop_icon); //interpolation_control->set("icon", interpolation_control_prop_icon);
//interpolation_control property items TYPE_ARRAY value: [, [AtlasTexture:19169], False, 0, Null, , [AtlasTexture:19168], False, 1, Null, , [AtlasTexture:19170], False, 2, Null, , [AtlasTexture:19171], False, 3, Null] //interpolation_control property items TYPE_ARRAY value: [, [AtlasTexture:19169], False, 0, Null, , [AtlasTexture:19168], False, 1, Null, , [AtlasTexture:19170], False, 2, Null, , [AtlasTexture:19171], False, 3, Null]
interpolation_control->set_selected(1); interpolation_control->select(1);
interpolation_control->connect("item_selected", this, "_on_Interpolation_item_selected"); interpolation_control->connect("item_selected", this, "_on_Interpolation_item_selected");
add_child(interpolation_control); add_child(interpolation_control);
Label *value_control = memnew(Label); Label *value_control = memnew(Label);
value_control->set_align(ALIGN_CENTER); value_control->set_align(Label::ALIGN_CENTER);
add_child(value_control);
value_control->set("custom_colors/font_color", Color(1, 1, 1, 1)); value_control->set("custom_colors/font_color", Color(1, 1, 1, 1));
value_control->set("custom_colors/font_color_shadow", Color(0, 0, 0, 1)); value_control->set("custom_colors/font_color_shadow", Color(0, 0, 0, 1));
value_control->set("custom_constants/shadow_offset_x", 1); value_control->set("custom_constants/shadow_offset_x", 1);
value_control->set("custom_constants/shadow_offset_y", 1); value_control->set("custom_constants/shadow_offset_y", 1);
value_control->set("custom_constants/shadow_as_outline", 1); value_control->set("custom_constants/shadow_as_outline", 1);
add_child(value_control);
} }
GradientEditor::~GradientEditor() { GradientEditor::~GradientEditor() {
@ -351,8 +366,8 @@ void GradientEditor::_bind_methods() {
//ClassDB::bind_method(D_METHOD("set_undo_redo", "value"), &GradientEditor::set_undo_redo); //ClassDB::bind_method(D_METHOD("set_undo_redo", "value"), &GradientEditor::set_undo_redo);
//ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "undo_redo", PROPERTY_HINT_RESOURCE_TYPE, "UndoRedo"), "set_undo_redo", "get_undo_redo"); //ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "undo_redo", PROPERTY_HINT_RESOURCE_TYPE, "UndoRedo"), "set_undo_redo", "get_undo_redo");
ClassDB::bind_method(D_METHOD("get_saved_points"), &GradientEditor::get__saved_points); ClassDB::bind_method(D_METHOD("get_saved_points"), &GradientEditor::get_saved_points);
ClassDB::bind_method(D_METHOD("set_saved_points", "value"), &GradientEditor::set__saved_points); ClassDB::bind_method(D_METHOD("set_saved_points", "value"), &GradientEditor::set_saved_points);
ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "saved_points"), "set_saved_points", "get_saved_points"); ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "saved_points"), "set_saved_points", "get_saved_points");
//ClassDB::bind_method(D_METHOD("get_active_cursor"), &GradientEditor::get_active_cursor); //ClassDB::bind_method(D_METHOD("get_active_cursor"), &GradientEditor::get_active_cursor);
@ -374,9 +389,9 @@ void GradientEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_gui_input", "ev"), &GradientEditor::_gui_input); ClassDB::bind_method(D_METHOD("_gui_input", "ev"), &GradientEditor::_gui_input);
ClassDB::bind_method(D_METHOD("select_color", "cursor", " position"), &GradientEditor::select_color); //ClassDB::bind_method(D_METHOD("select_color", "cursor", " position"), &GradientEditor::select_color);
ClassDB::bind_method(D_METHOD("get_sorted_cursors"), &GradientEditor::get_sorted_cursors); //ClassDB::bind_method(D_METHOD("get_sorted_cursors"), &GradientEditor::get_sorted_cursors);
ClassDB::bind_method(D_METHOD("generate_preview_image"), &GradientEditor::generate_preview_image); ClassDB::bind_method(D_METHOD("generate_preview_image"), &GradientEditor::generate_preview_image);

View File

@ -6,12 +6,15 @@
#include "core/reference.h" #include "core/reference.h"
#include "core/undo_redo.h" #include "core/undo_redo.h"
#include "core/variant.h" #include "core/variant.h"
#include "core/vector.h"
#include "scene/gui/control.h" #include "scene/gui/control.h"
class MMGraphNode; class MMGraphNode;
class GradientBase; class GradientBase;
class GradientCursor; class GradientCursor;
class TextureRect;
class OptionButton;
class GradientEditor : public Control { class GradientEditor : public Control {
GDCLASS(GradientEditor, Control); GDCLASS(GradientEditor, Control);
@ -41,7 +44,6 @@ public:
void save_color_state(); void save_color_state();
void undo_redo_save_color_state(); void undo_redo_save_color_state();
void set_undo_redo(const UndoRedo &ur);
void update_cursors(); void update_cursors();
void update_value(); void update_value();
@ -49,9 +51,9 @@ public:
void add_cursor(const float x, const Color &color); void add_cursor(const float x, const Color &color);
void _gui_input(const Ref<InputEvent> &ev); void _gui_input(const Ref<InputEvent> &ev);
void select_color(const GradientCursor *cursor, const Vector2 &position); void select_color(GradientCursor *cursor, const Vector2 &position);
Array get_sorted_cursors(); Vector<GradientCursor *> get_sorted_cursors();
void generate_preview_image(); void generate_preview_image();
Color get_gradient_color(const float x); Color get_gradient_color(const float x);
@ -72,6 +74,8 @@ protected:
UndoRedo *_undo_redo; UndoRedo *_undo_redo;
PoolRealArray _saved_points; PoolRealArray _saved_points;
GradientCursor *active_cursor; GradientCursor *active_cursor;
TextureRect *gradient;
OptionButton *interpolation;
}; };
#endif #endif

View File

@ -4,9 +4,11 @@
#include "core/object.h" #include "core/object.h"
#include "gradient_editor.h" #include "gradient_editor.h"
#include "../../../nodes/bases/gradient_base.h"
#include "../../../nodes/mm_node_universal_property.h"
#include "scene/gui/panel.h" #include "scene/gui/panel.h"
void GradientPopup::init(const Ref<GradientBase> &value, MMGraphNode *graph_node, UndoRedo &undo_redo) { void GradientPopup::init(const Ref<GradientBase> &value, MMGraphNode *graph_node, UndoRedo *undo_redo) {
_gradient_editor->set_undo_redo(undo_redo); _gradient_editor->set_undo_redo(undo_redo);
_gradient_editor->set_graph_node(graph_node); _gradient_editor->set_graph_node(graph_node);
_gradient_editor->set_value(value); _gradient_editor->set_value(value);

View File

@ -14,7 +14,7 @@ class GradientPopup : public Popup {
GDCLASS(GradientPopup, Popup); GDCLASS(GradientPopup, Popup);
public: public:
void init(const Ref<GradientBase> &value, MMGraphNode *graph_node, UndoRedo &undo_redo); void init(const Ref<GradientBase> &value, MMGraphNode *graph_node, UndoRedo *undo_redo);
void _on_Control_updated(const Ref<GradientBase> &value); void _on_Control_updated(const Ref<GradientBase> &value);
void _on_GradientPopup_popup_hide(); void _on_GradientPopup_popup_hide();