|
| 1 | +@tool |
| 2 | +extends "res://addons/kamran_wali/code_opt_pro/scripts/code_opt_pros/base_plugin.gd" |
| 3 | + |
| 4 | +const _FROM_TSCN = ".tscn" |
| 5 | + |
| 6 | +# Properties from the scene. |
| 7 | +var _parent_status_lbl: Label |
| 8 | +var _scene_status_lbl: Label |
| 9 | +var _num_instantiate: LineEdit |
| 10 | +var _instantiate_button: CanvasItem |
| 11 | +var _parent_lock_button: CheckButton |
| 12 | +var _scene_lock_button: CheckButton |
| 13 | + |
| 14 | +# Properties needed internally. |
| 15 | +var _parent_node: Node |
| 16 | +var _scene_object: PackedScene |
| 17 | +var _temp_object |
| 18 | +var _object_instantiate |
| 19 | +var _index: int |
| 20 | +var _counter: int |
| 21 | +var _index_name: int |
| 22 | +var _counter_name: int |
| 23 | +var _is_parent: bool |
| 24 | +var _is_scene: bool |
| 25 | +var _is_number: bool |
| 26 | + |
| 27 | +func _enter_tree() -> void: |
| 28 | + super._enter_tree() |
| 29 | + |
| 30 | + # Setting up the scene variables |
| 31 | + _parent_status_lbl = $MainContainer/ParentContainer/ParentStatus |
| 32 | + _scene_status_lbl = $MainContainer/SceneContainer/SceneStatusLabel |
| 33 | + _num_instantiate = $MainContainer/NumberContainer/Input_Number |
| 34 | + _instantiate_button = $MainContainer/InstantiateButton |
| 35 | + _parent_lock_button = $MainContainer/ParentContainer/LockButton |
| 36 | + _scene_lock_button = $MainContainer/SceneContainer/SceneLockButton |
| 37 | + |
| 38 | +func _is_var_init() -> bool: |
| 39 | + return (_parent_status_lbl != null && _scene_lock_button != null && _scene_status_lbl != null |
| 40 | + && _num_instantiate != null && _instantiate_button != null && _parent_lock_button != null) |
| 41 | + |
| 42 | +func update(delta: float) -> void: |
| 43 | + if _is_var_init(): # Checking if all the variables are done loading up |
| 44 | + if _is_parent_selected(): # Checking if parent has been selected. |
| 45 | + # Checking if new parent has been selected and if locked button is disabled. |
| 46 | + if _parent_node != _get_selected_parent_node() && !_parent_lock_button.button_pressed: |
| 47 | + _parent_node = _get_selected_parent_node() # Updating the parent. |
| 48 | + |
| 49 | + if _parent_node != null: # Checking if parent node is NOT null. |
| 50 | + _parent_status_lbl.set_text(_parent_node.name) # Updating the parent status. |
| 51 | + set_control_font_colour(_parent_status_lbl, Color.GREEN) |
| 52 | + _is_parent = true # Parent selected |
| 53 | + else: # Condition for giving error for NOT selecting parent OR too many selection. |
| 54 | + _parent_status_lbl.set_text("Please select a parent Node object.") |
| 55 | + set_control_font_colour(_parent_status_lbl, Color.INDIAN_RED) |
| 56 | + _is_parent = false # Parent NOT selected |
| 57 | + |
| 58 | + if !_scene_lock_button.button_pressed: # Condition for updating the scene object selection |
| 59 | + if _is_filesystem_object_selected(): # Checking if filesystem object has been selected |
| 60 | + _temp_object = _get_selected_filesystem_object() # Getting the selected filesystem object |
| 61 | + |
| 62 | + if _temp_object is PackedScene: # Checking if filesystem object is scene object. |
| 63 | + if _scene_object != _temp_object: # Condition for updating the scene object |
| 64 | + _scene_object = _temp_object # Updating the scene object |
| 65 | + |
| 66 | + if _scene_object != null: # Condition for updating scene object's status |
| 67 | + _scene_status_lbl.set_text(_get_scene_object_name(_scene_object.resource_path)) |
| 68 | + set_control_font_colour(_scene_status_lbl, Color.GREEN) |
| 69 | + _is_scene = true # Scene selected |
| 70 | + else: # Condition for NOT selected scene object |
| 71 | + _scene_status_lbl.set_text("Scene object NOT selected.") |
| 72 | + set_control_font_colour(_scene_status_lbl, Color.INDIAN_RED) |
| 73 | + _scene_object = null # Making null so later can select the previous object. |
| 74 | + _is_scene = false # Scene NOT selected |
| 75 | + else: # Condition for filesystem object NOT selected |
| 76 | + _scene_status_lbl.set_text("Please select a scene object from FileSystem.") |
| 77 | + set_control_font_colour(_scene_status_lbl, Color.INDIAN_RED) |
| 78 | + _is_scene = false # Scene NOT selected |
| 79 | + |
| 80 | + if _is_show_button(): # Showing the button. |
| 81 | + _instantiate_button.show() |
| 82 | + else: # Hiding the button. |
| 83 | + _instantiate_button.hide() |
| 84 | + |
| 85 | +func get_version_lbl_path() -> String: |
| 86 | + return "Version" |
| 87 | + |
| 88 | +func _on_input_number_text_changed(value: String): |
| 89 | + if is_int(value) && value.to_int() > 0: |
| 90 | + set_control_font_colour(_num_instantiate, Color.WHITE) |
| 91 | + _is_number = true |
| 92 | + else: |
| 93 | + set_control_font_colour(_num_instantiate, Color.INDIAN_RED) |
| 94 | + _is_number = false |
| 95 | + |
| 96 | +func _on_duplicate_button_pressed(): |
| 97 | + _counter = _num_instantiate.get_text().to_int() if _is_number else 1 # Getting the counter value |
| 98 | + |
| 99 | + for _index in range(0, _counter): # Loop for creating objects. |
| 100 | + _object_instantiate = _scene_object.instantiate() |
| 101 | + _parent_node.add_child(_object_instantiate, true) # Adding object and making name readable. |
| 102 | + _object_instantiate.set_owner(get_scene_root_node()) # Setting the ownership to the correct scene. |
| 103 | + |
| 104 | +## This method checks if the a parent object has been selected. |
| 105 | +func _is_parent_selected() -> bool: |
| 106 | + return (EDITOR_PLUGIN.get_editor_interface().get_selection().get_selected_nodes().size() != 0 |
| 107 | + || _parent_node != null) |
| 108 | + |
| 109 | +## This method checks if any file system object has been selected. |
| 110 | +func _is_filesystem_object_selected() -> bool: |
| 111 | + return (EDITOR_PLUGIN.get_editor_interface().get_selected_paths().size() != 0 |
| 112 | + || _scene_object != null) |
| 113 | + |
| 114 | +## This method gets the selected parent node. |
| 115 | +func _get_selected_parent_node() -> Node: |
| 116 | + if EDITOR_PLUGIN.get_editor_interface().get_selection().get_selected_nodes().size() == 0: |
| 117 | + return null |
| 118 | + |
| 119 | + return EDITOR_PLUGIN.get_editor_interface().get_selection().get_selected_nodes()[0] |
| 120 | + |
| 121 | +## This method gets the selected filesystem object. |
| 122 | +func _get_selected_filesystem_object(): |
| 123 | + if (EDITOR_PLUGIN.get_editor_interface().get_selected_paths().size() != 0 && |
| 124 | + EDITOR_PLUGIN.get_editor_interface().get_selected_paths()[0].substr( |
| 125 | + EDITOR_PLUGIN.get_editor_interface().get_selected_paths()[0].length() - _FROM_TSCN.length(), |
| 126 | + _FROM_TSCN.length()).contains(_FROM_TSCN)): |
| 127 | + return load(EDITOR_PLUGIN.get_editor_interface().get_selected_paths()[0]) |
| 128 | + return null |
| 129 | + |
| 130 | +## This method checks if the given object exists. |
| 131 | +func _is_object_exist(path: String) -> bool: |
| 132 | + return FileAccess.file_exists(path) || EDITOR_PLUGIN.get_editor_interface().get_edited_scene_root().has_node(path) |
| 133 | + |
| 134 | +## This method checks if to show the button. |
| 135 | +func _is_show_button() -> bool: |
| 136 | + return _is_parent && _is_scene |
| 137 | + |
| 138 | +## This method gets the name of the scene object. |
| 139 | +func _get_scene_object_name(name: String) -> String: |
| 140 | + for _counter_name in range(name.length() - 1, -1, -1): # Loop for finding the name |
| 141 | + if name[_counter_name] == "/": # Condition for breaking the loop |
| 142 | + _index_name = _counter_name + 1 # Start index of the sub-string |
| 143 | + break |
| 144 | + return name.substr(_index_name, name.length()) # Returning the actual name sub-string |
0 commit comments