Skip to content

Commit 5487709

Browse files
authored
Merge pull request #15 from deadlykam/features
Features
2 parents 3d87ce6 + 54c541e commit 5487709

32 files changed

Lines changed: 654 additions & 57 deletions

README.md

Lines changed: 82 additions & 12 deletions
Large diffs are not rendered by default.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@tool
2+
extends EditorPlugin
3+
4+
const COP_INSTANTIATE_OBJECT: GDScript = preload("res://addons/kamran_wali/code_opt_pro/cop_instantiate_object/cop_instantiate_object.gd")
5+
6+
var _dock
7+
8+
func _enter_tree():
9+
COP_INSTANTIATE_OBJECT.set_plugin(self) # Setting the plugin.
10+
_dock = preload("res://addons/kamran_wali/code_opt_pro/cop_instantiate_object/instantiate_object.tscn").instantiate()
11+
add_control_to_dock(DOCK_SLOT_RIGHT_BL, _dock)
12+
13+
func _exit_tree():
14+
COP_INSTANTIATE_OBJECT.set_plugin(null) # Removing the plugin.
15+
remove_control_from_docks(_dock)
16+
_dock.free()
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
[gd_scene load_steps=3 format=3 uid="uid://cr1tvl1q0k4y1"]
2+
3+
[ext_resource type="Script" path="res://addons/kamran_wali/code_opt_pro/cop_instantiate_object/cop_instantiate_object.gd" id="1_ixc8a"]
4+
[ext_resource type="Texture2D" uid="uid://ddib3b0wqsk84" path="res://addons/kamran_wali/code_opt_pro/textures/CodeOptProLogo.png" id="2_406mk"]
5+
6+
[node name="Instantiate Object" type="Control"]
7+
layout_mode = 3
8+
anchors_preset = 15
9+
anchor_right = 1.0
10+
anchor_bottom = 1.0
11+
grow_horizontal = 2
12+
grow_vertical = 2
13+
script = ExtResource("1_ixc8a")
14+
15+
[node name="MainContainer" type="VBoxContainer" parent="."]
16+
layout_mode = 1
17+
anchors_preset = 15
18+
anchor_right = 1.0
19+
anchor_bottom = 1.0
20+
grow_horizontal = 2
21+
grow_vertical = 2
22+
23+
[node name="SceneContainer" type="HBoxContainer" parent="MainContainer"]
24+
layout_mode = 2
25+
26+
[node name="SceneLockButton" type="CheckButton" parent="MainContainer/SceneContainer"]
27+
layout_mode = 2
28+
theme_override_font_sizes/font_size = 12
29+
text = "Lock"
30+
31+
[node name="SceneLabel" type="Label" parent="MainContainer/SceneContainer"]
32+
layout_mode = 2
33+
theme_override_font_sizes/font_size = 12
34+
text = "Scene: "
35+
36+
[node name="SceneStatusLabel" type="Label" parent="MainContainer/SceneContainer"]
37+
layout_mode = 2
38+
size_flags_horizontal = 3
39+
theme_override_colors/font_color = Color(0.803922, 0.360784, 0.360784, 1)
40+
theme_override_font_sizes/font_size = 12
41+
text = "Please select a scene object from FileSystem."
42+
autowrap_mode = 3
43+
44+
[node name="ParentContainer" type="HBoxContainer" parent="MainContainer"]
45+
layout_mode = 2
46+
47+
[node name="LockButton" type="CheckButton" parent="MainContainer/ParentContainer"]
48+
layout_mode = 2
49+
theme_override_font_sizes/font_size = 12
50+
text = "Lock"
51+
52+
[node name="ParentPathLabel" type="Label" parent="MainContainer/ParentContainer"]
53+
layout_mode = 2
54+
theme_override_font_sizes/font_size = 12
55+
text = "Parent: "
56+
57+
[node name="ParentStatus" type="Label" parent="MainContainer/ParentContainer"]
58+
layout_mode = 2
59+
size_flags_horizontal = 3
60+
theme_override_colors/font_color = Color(0.803922, 0.360784, 0.360784, 1)
61+
theme_override_font_sizes/font_size = 12
62+
text = "Please select a parent Node object."
63+
autowrap_mode = 3
64+
65+
[node name="NumberContainer" type="HBoxContainer" parent="MainContainer"]
66+
layout_mode = 2
67+
68+
[node name="NumberLabel" type="Label" parent="MainContainer/NumberContainer"]
69+
layout_mode = 2
70+
theme_override_font_sizes/font_size = 12
71+
text = "Number of Instantiation"
72+
73+
[node name="Input_Number" type="LineEdit" parent="MainContainer/NumberContainer"]
74+
layout_mode = 2
75+
size_flags_horizontal = 3
76+
theme_override_font_sizes/font_size = 12
77+
78+
[node name="InstantiateButton" type="Button" parent="MainContainer"]
79+
visible = false
80+
layout_mode = 2
81+
theme_override_font_sizes/font_size = 20
82+
text = "Instantiate Object"
83+
84+
[node name="Logo" type="TextureRect" parent="."]
85+
layout_mode = 1
86+
anchors_preset = -1
87+
anchor_top = 0.633
88+
anchor_right = 1.0
89+
anchor_bottom = 0.952
90+
offset_top = -0.184021
91+
offset_bottom = 0.104004
92+
grow_horizontal = 2
93+
grow_vertical = 2
94+
mouse_filter = 2
95+
texture = ExtResource("2_406mk")
96+
expand_mode = 1
97+
stretch_mode = 5
98+
99+
[node name="Version" type="Label" parent="."]
100+
layout_mode = 1
101+
anchors_preset = -1
102+
anchor_top = 0.96
103+
anchor_right = 1.0
104+
anchor_bottom = 1.0
105+
offset_top = -0.0800171
106+
grow_horizontal = 2
107+
grow_vertical = 0
108+
theme_override_colors/font_shadow_color = Color(0, 0, 0, 1)
109+
text = "Version - v1.6.0a"
110+
111+
[connection signal="text_changed" from="MainContainer/NumberContainer/Input_Number" to="." method="_on_input_number_text_changed"]
112+
[connection signal="pressed" from="MainContainer/InstantiateButton" to="." method="_on_duplicate_button_pressed"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[plugin]
2+
3+
name="Instantiate Object"
4+
description="This plugin instantiates a scene/packed scene object. The object must be selected from the filesystem."
5+
author="Kamran Wali"
6+
version="1.6.0"
7+
script="cop_instantiate_object_plugin.gd"

0 commit comments

Comments
 (0)