| 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) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
| 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 |  | 
| 31 | #include "light_occluder_2d_editor_plugin.h" | 
| 32 |  | 
| 33 | #include "editor/editor_node.h" | 
| 34 | #include "editor/editor_undo_redo_manager.h" | 
| 35 |  | 
| 36 | Ref<OccluderPolygon2D> LightOccluder2DEditor::_ensure_occluder() const { | 
| 37 | 	Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon(); | 
| 38 | 	if (!occluder.is_valid()) { | 
| 39 | 		occluder = Ref<OccluderPolygon2D>(memnew(OccluderPolygon2D)); | 
| 40 | 		node->set_occluder_polygon(occluder); | 
| 41 | 	} | 
| 42 | 	return occluder; | 
| 43 | } | 
| 44 |  | 
| 45 | Node2D *LightOccluder2DEditor::_get_node() const { | 
| 46 | 	return node; | 
| 47 | } | 
| 48 |  | 
| 49 | void LightOccluder2DEditor::_set_node(Node *p_polygon) { | 
| 50 | 	node = Object::cast_to<LightOccluder2D>(p_polygon); | 
| 51 | } | 
| 52 |  | 
| 53 | bool LightOccluder2DEditor::_is_line() const { | 
| 54 | 	Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon(); | 
| 55 | 	if (occluder.is_valid()) { | 
| 56 | 		return !occluder->is_closed(); | 
| 57 | 	} else { | 
| 58 | 		return false; | 
| 59 | 	} | 
| 60 | } | 
| 61 |  | 
| 62 | int LightOccluder2DEditor::_get_polygon_count() const { | 
| 63 | 	Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon(); | 
| 64 | 	if (occluder.is_valid()) { | 
| 65 | 		return occluder->get_polygon().size(); | 
| 66 | 	} else { | 
| 67 | 		return 0; | 
| 68 | 	} | 
| 69 | } | 
| 70 |  | 
| 71 | Variant LightOccluder2DEditor::_get_polygon(int p_idx) const { | 
| 72 | 	Ref<OccluderPolygon2D> occluder = node->get_occluder_polygon(); | 
| 73 | 	if (occluder.is_valid()) { | 
| 74 | 		return occluder->get_polygon(); | 
| 75 | 	} else { | 
| 76 | 		return Variant(Vector<Vector2>()); | 
| 77 | 	} | 
| 78 | } | 
| 79 |  | 
| 80 | void LightOccluder2DEditor::_set_polygon(int p_idx, const Variant &p_polygon) const { | 
| 81 | 	Ref<OccluderPolygon2D> occluder = _ensure_occluder(); | 
| 82 | 	occluder->set_polygon(p_polygon); | 
| 83 | } | 
| 84 |  | 
| 85 | void LightOccluder2DEditor::_action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon) { | 
| 86 | 	Ref<OccluderPolygon2D> occluder = _ensure_occluder(); | 
| 87 | 	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); | 
| 88 | 	undo_redo->add_do_method(occluder.ptr(), "set_polygon" , p_polygon); | 
| 89 | 	undo_redo->add_undo_method(occluder.ptr(), "set_polygon" , p_previous); | 
| 90 | } | 
| 91 |  | 
| 92 | bool LightOccluder2DEditor::_has_resource() const { | 
| 93 | 	return node && node->get_occluder_polygon().is_valid(); | 
| 94 | } | 
| 95 |  | 
| 96 | void LightOccluder2DEditor::_create_resource() { | 
| 97 | 	if (!node) { | 
| 98 | 		return; | 
| 99 | 	} | 
| 100 |  | 
| 101 | 	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); | 
| 102 | 	undo_redo->create_action(TTR("Create Occluder Polygon" )); | 
| 103 | 	undo_redo->add_do_method(node, "set_occluder_polygon" , Ref<OccluderPolygon2D>(memnew(OccluderPolygon2D))); | 
| 104 | 	undo_redo->add_undo_method(node, "set_occluder_polygon" , Variant(Ref<RefCounted>())); | 
| 105 | 	undo_redo->commit_action(); | 
| 106 |  | 
| 107 | 	_menu_option(MODE_CREATE); | 
| 108 | } | 
| 109 |  | 
| 110 | LightOccluder2DEditor::LightOccluder2DEditor() {} | 
| 111 |  | 
| 112 | LightOccluder2DEditorPlugin::LightOccluder2DEditorPlugin() : | 
| 113 | 		AbstractPolygon2DEditorPlugin(memnew(LightOccluder2DEditor), "LightOccluder2D" ) { | 
| 114 | } | 
| 115 |  |