gradient_editor_plugin.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. Size2 GradientEditor::get_minimum_size() const {
  34. return Size2(0, 60) * EDSCALE;
  35. }
  36. void GradientEditor::_gradient_changed() {
  37. if (editing)
  38. return;
  39. editing = true;
  40. Vector<Gradient::Point> points = gradient->get_points();
  41. set_points(points);
  42. editing = false;
  43. }
  44. void GradientEditor::_ramp_changed() {
  45. editing = true;
  46. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  47. undo_redo->create_action("Gradient Edited");
  48. undo_redo->add_do_method(gradient.ptr(), "set_offsets", get_offsets());
  49. undo_redo->add_do_method(gradient.ptr(), "set_colors", get_colors());
  50. undo_redo->add_undo_method(gradient.ptr(), "set_offsets", gradient->get_offsets());
  51. undo_redo->add_undo_method(gradient.ptr(), "set_colors", gradient->get_colors());
  52. undo_redo->commit_action();
  53. editing = false;
  54. }
  55. void GradientEditor::_bind_methods() {
  56. ClassDB::bind_method("_gradient_changed", &GradientEditor::_gradient_changed);
  57. ClassDB::bind_method("_ramp_changed", &GradientEditor::_ramp_changed);
  58. }
  59. void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
  60. gradient = p_gradient;
  61. connect("ramp_changed", this, "_ramp_changed");
  62. gradient->connect("changed", this, "_gradient_changed");
  63. set_points(gradient->get_points());
  64. }
  65. GradientEditor::GradientEditor() {
  66. editing = false;
  67. }
  68. ///////////////////////
  69. bool EditorInspectorPluginGradient::can_handle(Object *p_object) {
  70. return Object::cast_to<Gradient>(p_object) != NULL;
  71. }
  72. void EditorInspectorPluginGradient::parse_begin(Object *p_object) {
  73. Gradient *gradient = Object::cast_to<Gradient>(p_object);
  74. Ref<Gradient> g(gradient);
  75. GradientEditor *editor = memnew(GradientEditor);
  76. editor->set_gradient(g);
  77. add_custom_control(editor);
  78. }
  79. GradientEditorPlugin::GradientEditorPlugin(EditorNode *p_node) {
  80. Ref<EditorInspectorPluginGradient> plugin;
  81. plugin.instance();
  82. add_inspector_plugin(plugin);
  83. }