add a few things:
* test case with folders/subfolders * renaming items by double-clicking them * naive drag test
This commit is contained in:
parent
cdc4e3f14e
commit
08bbf61416
@ -27,3 +27,12 @@ func add_item(text: String, parent_item: ControlTreeItem = null) -> ControlTreeI
|
|||||||
new_item.set_button_group(item_select_btn_group)
|
new_item.set_button_group(item_select_btn_group)
|
||||||
|
|
||||||
return new_item
|
return new_item
|
||||||
|
|
||||||
|
|
||||||
|
func get_items_by_name(s: String) -> Array:
|
||||||
|
var res = []
|
||||||
|
for i in items:
|
||||||
|
if i.get_text() == s:
|
||||||
|
res.append(i)
|
||||||
|
|
||||||
|
return res
|
||||||
|
@ -28,13 +28,20 @@ onready var item_text_button: Button = $"%ItemTextButton"
|
|||||||
|
|
||||||
onready var subitem_container: VBoxContainer = $"%Subitems"
|
onready var subitem_container: VBoxContainer = $"%Subitems"
|
||||||
|
|
||||||
|
signal rename_allowed
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
# warning-ignore:return_value_discarded
|
# warning-ignore:return_value_discarded
|
||||||
|
# warning-ignore:return_value_discarded
|
||||||
|
# warning-ignore:return_value_discarded
|
||||||
# warning-ignore:return_value_discarded
|
# warning-ignore:return_value_discarded
|
||||||
check_box.connect("pressed", self, "set_enabled", [true])
|
check_box.connect("pressed", self, "set_enabled", [true])
|
||||||
collapse_button.connect("pressed", self, "toggle_collapsed")
|
collapse_button.connect("pressed", self, "toggle_collapsed")
|
||||||
|
|
||||||
|
item_text_button.connect("rename_request", self, "rename_request")
|
||||||
|
connect("rename_allowed", item_text_button, "create_lineedit")
|
||||||
|
|
||||||
|
|
||||||
func set_checked(v: bool, from_checkbox: bool = false) -> void:
|
func set_checked(v: bool, from_checkbox: bool = false) -> void:
|
||||||
checked = v
|
checked = v
|
||||||
@ -92,3 +99,15 @@ func toggle_collapsed() -> void:
|
|||||||
|
|
||||||
for i in subitems:
|
for i in subitems:
|
||||||
i.visible = !collapsed
|
i.visible = !collapsed
|
||||||
|
|
||||||
|
|
||||||
|
func can_drop_data(_position: Vector2, data) -> bool:
|
||||||
|
if data is Control && data.has_method("i_am_item"):
|
||||||
|
return true
|
||||||
|
|
||||||
|
return false
|
||||||
|
|
||||||
|
|
||||||
|
func rename_request() -> void:
|
||||||
|
if editable:
|
||||||
|
emit_signal("rename_allowed")
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
[gd_scene load_steps=2 format=2]
|
[gd_scene load_steps=4 format=2]
|
||||||
|
|
||||||
[ext_resource path="res://ControlTreeItem.gd" type="Script" id=1]
|
[ext_resource path="res://ControlTreeItem.gd" type="Script" id=1]
|
||||||
|
[ext_resource path="res://DragHandle.gd" type="Script" id=2]
|
||||||
|
[ext_resource path="res://RenamableButton.gd" type="Script" id=3]
|
||||||
|
|
||||||
[node name="ControlTreeItem" type="VBoxContainer"]
|
[node name="ControlTreeItem" type="VBoxContainer"]
|
||||||
anchor_right = 0.479
|
anchor_right = 0.479
|
||||||
@ -39,6 +41,7 @@ unique_name_in_owner = true
|
|||||||
margin_right = 32.0
|
margin_right = 32.0
|
||||||
margin_bottom = 32.0
|
margin_bottom = 32.0
|
||||||
rect_min_size = Vector2( 32, 0 )
|
rect_min_size = Vector2( 32, 0 )
|
||||||
|
script = ExtResource( 2 )
|
||||||
|
|
||||||
[node name="CheckBox" type="CheckBox" parent="ItemAtom"]
|
[node name="CheckBox" type="CheckBox" parent="ItemAtom"]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
@ -56,6 +59,7 @@ toggle_mode = true
|
|||||||
text = "Item text"
|
text = "Item text"
|
||||||
flat = true
|
flat = true
|
||||||
align = 0
|
align = 0
|
||||||
|
script = ExtResource( 3 )
|
||||||
|
|
||||||
[node name="Subitems" type="VBoxContainer" parent="."]
|
[node name="Subitems" type="VBoxContainer" parent="."]
|
||||||
unique_name_in_owner = true
|
unique_name_in_owner = true
|
||||||
|
5
DragHandle.gd
Normal file
5
DragHandle.gd
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
extends Panel
|
||||||
|
|
||||||
|
|
||||||
|
func get_drag_data(position: Vector2):
|
||||||
|
return get_parent().get_parent()
|
42
RenamableButton.gd
Normal file
42
RenamableButton.gd
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
extends Button
|
||||||
|
|
||||||
|
var le: LineEdit = null
|
||||||
|
|
||||||
|
signal rename_request
|
||||||
|
|
||||||
|
|
||||||
|
func _gui_input(event: InputEvent) -> void:
|
||||||
|
if event is InputEventMouseButton && event.is_doubleclick() && !le:
|
||||||
|
emit_signal("rename_request")
|
||||||
|
|
||||||
|
|
||||||
|
func remove_lineedit() -> void:
|
||||||
|
le.queue_free()
|
||||||
|
le = null
|
||||||
|
|
||||||
|
|
||||||
|
func create_lineedit() -> void:
|
||||||
|
le = LineEdit.new()
|
||||||
|
get_tree().get_root().add_child(le)
|
||||||
|
le.rect_size = get_global_rect().size
|
||||||
|
# le.rect_size.x += 64 # some padding to make it more readable
|
||||||
|
le.rect_position = get_global_rect().position
|
||||||
|
le.expand_to_text_length = true
|
||||||
|
le.text = text
|
||||||
|
le.select_all()
|
||||||
|
le.grab_focus()
|
||||||
|
le.connect("text_entered", self, "lineeedit_text_entered")
|
||||||
|
|
||||||
|
|
||||||
|
func lineeedit_text_entered(new_text: String) -> void:
|
||||||
|
le.queue_free()
|
||||||
|
le = null
|
||||||
|
text = new_text
|
||||||
|
|
||||||
|
|
||||||
|
func _input(event: InputEvent) -> void:
|
||||||
|
if le && event is InputEventMouseButton && !(le.get_global_rect().has_point(event.position)):
|
||||||
|
remove_lineedit()
|
||||||
|
|
||||||
|
if event is InputEventKey && event.scancode == KEY_ESCAPE && le:
|
||||||
|
remove_lineedit()
|
@ -5,23 +5,60 @@
|
|||||||
[sub_resource type="GDScript" id=1]
|
[sub_resource type="GDScript" id=1]
|
||||||
script/source = "extends Control
|
script/source = "extends Control
|
||||||
|
|
||||||
|
onready var control_tree: ControlTree = $ControlTree
|
||||||
|
|
||||||
var i
|
var i
|
||||||
var j
|
var j
|
||||||
|
|
||||||
|
var files: Array = [] # Array[FileDef]
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
pass
|
files = get_dir_contents(ProjectSettings.globalize_path(\"res://test_dir\"))
|
||||||
|
construct_tree(files)
|
||||||
|
|
||||||
|
|
||||||
func _on_Button_pressed() -> void:
|
func get_dir_contents(path: String) -> Array:
|
||||||
i = $ControlTree.add_item('test')
|
var res = []
|
||||||
|
var dir = Directory.new()
|
||||||
|
if dir.open(path) == OK:
|
||||||
|
dir.list_dir_begin(true)
|
||||||
|
var fname = dir.get_next()
|
||||||
|
while fname != \"\":
|
||||||
|
var f = FileDef.new()
|
||||||
|
f.path = fname
|
||||||
|
# print(f.path)
|
||||||
|
if dir.current_is_dir():
|
||||||
|
var a = get_dir_contents(path.plus_file(fname))
|
||||||
|
f.contains.append_array(a)
|
||||||
|
|
||||||
|
res.append(f)
|
||||||
|
fname = dir.get_next()
|
||||||
|
dir.list_dir_end()
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
func _on_Button2_pressed() -> void:
|
func construct_tree(from: Array, parent: ControlTreeItem = null):
|
||||||
j = $ControlTree.add_item('test2', i)
|
for f in from:
|
||||||
|
var item = control_tree.add_item(f.path, parent)
|
||||||
|
if f.contains:
|
||||||
|
item.editable = false
|
||||||
|
construct_tree(f.contains, item)
|
||||||
|
|
||||||
|
|
||||||
func _on_Button3_pressed() -> void:
|
#func _on_Button_pressed() -> void:
|
||||||
$ControlTree.add_item('test3', j)
|
# i = $ControlTree.add_item('test')
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#func _on_Button2_pressed() -> void:
|
||||||
|
# j = $ControlTree.add_item('test2', i)
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#func _on_Button3_pressed() -> void:
|
||||||
|
# $ControlTree.add_item('test3', j)
|
||||||
|
|
||||||
|
class FileDef:
|
||||||
|
var path: String
|
||||||
|
var contains: Array # Array[FileDef]
|
||||||
"
|
"
|
||||||
|
|
||||||
[node name="TestArea" type="Control"]
|
[node name="TestArea" type="Control"]
|
||||||
|
@ -27,6 +27,7 @@ _global_script_class_icons={
|
|||||||
[application]
|
[application]
|
||||||
|
|
||||||
config/name="customtree"
|
config/name="customtree"
|
||||||
|
run/main_scene="res://TestArea.tscn"
|
||||||
config/icon="res://icon.png"
|
config/icon="res://icon.png"
|
||||||
|
|
||||||
[gui]
|
[gui]
|
||||||
|
1
test_dir/file in root 1.txt
Normal file
1
test_dir/file in root 1.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
1
test_dir/file in root 2.txt
Normal file
1
test_dir/file in root 2.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
1
test_dir/folder1/file in folder.txt
Normal file
1
test_dir/folder1/file in folder.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
1
test_dir/folder1/subfolder/file in subfolder.txt
Normal file
1
test_dir/folder1/subfolder/file in subfolder.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
Loading…
Reference in New Issue
Block a user