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