light_occluder_2d_editor_plugin.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*************************************************************************/
  2. /* light_occluder_2d_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 "light_occluder_2d_editor_plugin.h"
  31. Ref<OccluderPolygon2D> LightOccluder2DEditor::_ensure_occluder() const {
  32. Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon();
  33. if (!occluder.is_valid()) {
  34. occluder = Ref<OccluderPolygon2D>(memnew(OccluderPolygon2D));
  35. node->set_occluder_polygon(occluder);
  36. }
  37. return occluder;
  38. }
  39. Node2D *LightOccluder2DEditor::_get_node() const {
  40. return node;
  41. }
  42. void LightOccluder2DEditor::_set_node(Node *p_polygon) {
  43. node = Object::cast_to<LightOccluder2D>(p_polygon);
  44. }
  45. bool LightOccluder2DEditor::_is_line() const {
  46. Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon();
  47. if (occluder.is_valid())
  48. return !occluder->is_closed();
  49. else
  50. return false;
  51. }
  52. int LightOccluder2DEditor::_get_polygon_count() const {
  53. Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon();
  54. if (occluder.is_valid())
  55. return occluder->get_polygon().size();
  56. else
  57. return 0;
  58. }
  59. Variant LightOccluder2DEditor::_get_polygon(int p_idx) const {
  60. Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon();
  61. if (occluder.is_valid())
  62. return occluder->get_polygon();
  63. else
  64. return Variant(Vector<Vector2>());
  65. }
  66. void LightOccluder2DEditor::_set_polygon(int p_idx, const Variant &p_polygon) const {
  67. Ref<OccluderPolygon2D> occluder = _ensure_occluder();
  68. occluder->set_polygon(p_polygon);
  69. }
  70. void LightOccluder2DEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) {
  71. Ref<OccluderPolygon2D> occluder = _ensure_occluder();
  72. undo_redo->add_do_method(occluder.ptr(), "set_polygon", p_polygon);
  73. undo_redo->add_undo_method(occluder.ptr(), "set_polygon", p_previous);
  74. }
  75. bool LightOccluder2DEditor::_has_resource() const {
  76. return node && node->get_occluder_polygon().is_valid();
  77. }
  78. void LightOccluder2DEditor::_create_resource() {
  79. if (!node)
  80. return;
  81. undo_redo->create_action(TTR("Create Occluder Polygon"));
  82. undo_redo->add_do_method(node, "set_occluder_polygon", Ref<OccluderPolygon2D>(memnew(OccluderPolygon2D)));
  83. undo_redo->add_undo_method(node, "set_occluder_polygon", Variant(REF()));
  84. undo_redo->commit_action();
  85. _menu_option(MODE_CREATE);
  86. }
  87. LightOccluder2DEditor::LightOccluder2DEditor(EditorNode *p_editor) :
  88. AbstractPolygon2DEditor(p_editor) {
  89. node = NULL;
  90. }
  91. LightOccluder2DEditorPlugin::LightOccluder2DEditorPlugin(EditorNode *p_node) :
  92. AbstractPolygon2DEditorPlugin(p_node, memnew(LightOccluder2DEditor(p_node)), "LightOccluder2D") {
  93. }