2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* color_ramp_editor_plugin.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2017-06-13 23:47:34 +02:00
|
|
|
#include "gradient_editor_plugin.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2016-01-12 21:09:09 +01:00
|
|
|
#include "canvas_item_editor_plugin.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "spatial_editor_plugin.h"
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
GradientEditorPlugin::GradientEditorPlugin(EditorNode *p_node) {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
editor = p_node;
|
2017-06-13 23:47:34 +02:00
|
|
|
ramp_editor = memnew(GradientEdit);
|
2016-05-23 22:10:26 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, ramp_editor);
|
2016-01-12 21:09:09 +01:00
|
|
|
|
2015-05-24 20:18:52 +02:00
|
|
|
ramp_editor->set_custom_minimum_size(Size2(100, 48));
|
|
|
|
ramp_editor->hide();
|
|
|
|
ramp_editor->connect("ramp_changed", this, "ramp_changed");
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
void GradientEditorPlugin::edit(Object *p_object) {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-08-24 22:58:51 +02:00
|
|
|
Gradient *gradient = Object::cast_to<Gradient>(p_object);
|
2017-06-13 23:47:34 +02:00
|
|
|
if (!gradient)
|
2015-05-24 20:18:52 +02:00
|
|
|
return;
|
2017-06-13 23:47:34 +02:00
|
|
|
gradient_ref = Ref<Gradient>(gradient);
|
|
|
|
ramp_editor->set_points(gradient_ref->get_points());
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
bool GradientEditorPlugin::handles(Object *p_object) const {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
return p_object->is_class("Gradient");
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
void GradientEditorPlugin::make_visible(bool p_visible) {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
|
|
|
if (p_visible) {
|
|
|
|
ramp_editor->show();
|
|
|
|
} else {
|
|
|
|
ramp_editor->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
void GradientEditorPlugin::_ramp_changed() {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
if (gradient_ref.is_valid()) {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
//Not sure if I should convert this data to PoolVector
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector<float> new_offsets = ramp_editor->get_offsets();
|
|
|
|
Vector<Color> new_colors = ramp_editor->get_colors();
|
2017-06-13 23:47:34 +02:00
|
|
|
Vector<float> old_offsets = gradient_ref->get_offsets();
|
|
|
|
Vector<Color> old_colors = gradient_ref->get_colors();
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (old_offsets.size() != new_offsets.size())
|
2016-05-04 03:25:37 +02:00
|
|
|
ur->create_action(TTR("Add/Remove Color Ramp Point"));
|
2015-05-24 20:18:52 +02:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
ur->create_action(TTR("Modify Color Ramp"), UndoRedo::MERGE_ENDS);
|
2017-06-13 23:47:34 +02:00
|
|
|
ur->add_do_method(this, "undo_redo_gradient", new_offsets, new_colors);
|
|
|
|
ur->add_undo_method(this, "undo_redo_gradient", old_offsets, old_colors);
|
2015-05-24 20:18:52 +02:00
|
|
|
ur->commit_action();
|
|
|
|
|
|
|
|
//color_ramp_ref->set_points(ramp_editor->get_points());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 21:28:22 +02:00
|
|
|
void GradientEditorPlugin::_undo_redo_gradient(const Vector<float> &offsets, const Vector<Color> &colors) {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
gradient_ref->set_offsets(offsets);
|
|
|
|
gradient_ref->set_colors(colors);
|
|
|
|
ramp_editor->set_points(gradient_ref->get_points());
|
2015-05-24 20:18:52 +02:00
|
|
|
ramp_editor->update();
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
GradientEditorPlugin::~GradientEditorPlugin() {
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:47:34 +02:00
|
|
|
void GradientEditorPlugin::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("ramp_changed"), &GradientEditorPlugin::_ramp_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("undo_redo_gradient", "offsets", "colors"), &GradientEditorPlugin::_undo_redo_gradient);
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|