gradient_editor_plugin.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*************************************************************************/
  2. /* gradient_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gradient_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "spatial_editor_plugin.h"
  33. GradientEditorPlugin::GradientEditorPlugin(EditorNode *p_node) {
  34. editor = p_node;
  35. ramp_editor = memnew(GradientEdit);
  36. add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, ramp_editor);
  37. ramp_editor->set_custom_minimum_size(Size2(100, 48));
  38. ramp_editor->hide();
  39. ramp_editor->connect("ramp_changed", this, "ramp_changed");
  40. }
  41. void GradientEditorPlugin::edit(Object *p_object) {
  42. Gradient *gradient = Object::cast_to<Gradient>(p_object);
  43. if (!gradient)
  44. return;
  45. gradient_ref = Ref<Gradient>(gradient);
  46. ramp_editor->set_points(gradient_ref->get_points());
  47. }
  48. bool GradientEditorPlugin::handles(Object *p_object) const {
  49. return p_object->is_class("Gradient");
  50. }
  51. void GradientEditorPlugin::make_visible(bool p_visible) {
  52. if (p_visible) {
  53. ramp_editor->show();
  54. } else {
  55. ramp_editor->hide();
  56. }
  57. }
  58. void GradientEditorPlugin::_ramp_changed() {
  59. if (gradient_ref.is_valid()) {
  60. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  61. //Not sure if I should convert this data to PoolVector
  62. Vector<float> new_offsets = ramp_editor->get_offsets();
  63. Vector<Color> new_colors = ramp_editor->get_colors();
  64. Vector<float> old_offsets = gradient_ref->get_offsets();
  65. Vector<Color> old_colors = gradient_ref->get_colors();
  66. if (old_offsets.size() != new_offsets.size())
  67. ur->create_action(TTR("Add/Remove Color Ramp Point"));
  68. else
  69. ur->create_action(TTR("Modify Color Ramp"), UndoRedo::MERGE_ENDS);
  70. ur->add_do_method(this, "undo_redo_gradient", new_offsets, new_colors);
  71. ur->add_undo_method(this, "undo_redo_gradient", old_offsets, old_colors);
  72. ur->commit_action();
  73. //color_ramp_ref->set_points(ramp_editor->get_points());
  74. }
  75. }
  76. void GradientEditorPlugin::_undo_redo_gradient(const Vector<float> &offsets, const Vector<Color> &colors) {
  77. gradient_ref->set_offsets(offsets);
  78. gradient_ref->set_colors(colors);
  79. ramp_editor->set_points(gradient_ref->get_points());
  80. ramp_editor->update();
  81. }
  82. GradientEditorPlugin::~GradientEditorPlugin() {
  83. }
  84. void GradientEditorPlugin::_bind_methods() {
  85. ClassDB::bind_method(D_METHOD("ramp_changed"), &GradientEditorPlugin::_ramp_changed);
  86. ClassDB::bind_method(D_METHOD("undo_redo_gradient", "offsets", "colors"), &GradientEditorPlugin::_undo_redo_gradient);
  87. }