From 08bbf61416d11b90aaf720a340afd6e703514e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Mon, 24 Oct 2022 22:20:01 +0300 Subject: [PATCH] add a few things: * test case with folders/subfolders * renaming items by double-clicking them * naive drag test --- ControlTree.gd | 9 ++++ ControlTreeItem.gd | 19 +++++++ ControlTreeItem.tscn | 6 ++- DragHandle.gd | 5 ++ RenamableButton.gd | 42 +++++++++++++++ TestArea.tscn | 51 ++++++++++++++++--- project.godot | 1 + test_dir/file in root 1.txt | 1 + test_dir/file in root 2.txt | 1 + test_dir/folder1/file in folder.txt | 1 + .../folder1/subfolder/file in subfolder.txt | 1 + 11 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 DragHandle.gd create mode 100644 RenamableButton.gd create mode 100644 test_dir/file in root 1.txt create mode 100644 test_dir/file in root 2.txt create mode 100644 test_dir/folder1/file in folder.txt create mode 100644 test_dir/folder1/subfolder/file in subfolder.txt diff --git a/ControlTree.gd b/ControlTree.gd index bc69ae5..1a426b1 100644 --- a/ControlTree.gd +++ b/ControlTree.gd @@ -27,3 +27,12 @@ func add_item(text: String, parent_item: ControlTreeItem = null) -> ControlTreeI new_item.set_button_group(item_select_btn_group) 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 diff --git a/ControlTreeItem.gd b/ControlTreeItem.gd index d9f5a4e..b837919 100644 --- a/ControlTreeItem.gd +++ b/ControlTreeItem.gd @@ -28,13 +28,20 @@ onready var item_text_button: Button = $"%ItemTextButton" onready var subitem_container: VBoxContainer = $"%Subitems" +signal rename_allowed + func _ready() -> void: # 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]) 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: checked = v @@ -92,3 +99,15 @@ func toggle_collapsed() -> void: for i in subitems: 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") diff --git a/ControlTreeItem.tscn b/ControlTreeItem.tscn index 76fc6e6..c6f0953 100644 --- a/ControlTreeItem.tscn +++ b/ControlTreeItem.tscn @@ -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://DragHandle.gd" type="Script" id=2] +[ext_resource path="res://RenamableButton.gd" type="Script" id=3] [node name="ControlTreeItem" type="VBoxContainer"] anchor_right = 0.479 @@ -39,6 +41,7 @@ unique_name_in_owner = true margin_right = 32.0 margin_bottom = 32.0 rect_min_size = Vector2( 32, 0 ) +script = ExtResource( 2 ) [node name="CheckBox" type="CheckBox" parent="ItemAtom"] unique_name_in_owner = true @@ -56,6 +59,7 @@ toggle_mode = true text = "Item text" flat = true align = 0 +script = ExtResource( 3 ) [node name="Subitems" type="VBoxContainer" parent="."] unique_name_in_owner = true diff --git a/DragHandle.gd b/DragHandle.gd new file mode 100644 index 0000000..0d7c43d --- /dev/null +++ b/DragHandle.gd @@ -0,0 +1,5 @@ +extends Panel + + +func get_drag_data(position: Vector2): + return get_parent().get_parent() diff --git a/RenamableButton.gd b/RenamableButton.gd new file mode 100644 index 0000000..c3549ff --- /dev/null +++ b/RenamableButton.gd @@ -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() diff --git a/TestArea.tscn b/TestArea.tscn index 51b4106..5830248 100644 --- a/TestArea.tscn +++ b/TestArea.tscn @@ -5,23 +5,60 @@ [sub_resource type="GDScript" id=1] script/source = "extends Control +onready var control_tree: ControlTree = $ControlTree + var i var j +var files: Array = [] # Array[FileDef] + func _ready() -> void: - pass + files = get_dir_contents(ProjectSettings.globalize_path(\"res://test_dir\")) + construct_tree(files) -func _on_Button_pressed() -> void: - i = $ControlTree.add_item('test') +func get_dir_contents(path: String) -> Array: + 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: - j = $ControlTree.add_item('test2', i) +func construct_tree(from: Array, parent: ControlTreeItem = null): + 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: - $ControlTree.add_item('test3', j) +#func _on_Button_pressed() -> void: +# 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"] diff --git a/project.godot b/project.godot index e5fd574..7872fb9 100644 --- a/project.godot +++ b/project.godot @@ -27,6 +27,7 @@ _global_script_class_icons={ [application] config/name="customtree" +run/main_scene="res://TestArea.tscn" config/icon="res://icon.png" [gui] diff --git a/test_dir/file in root 1.txt b/test_dir/file in root 1.txt new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/test_dir/file in root 1.txt @@ -0,0 +1 @@ + diff --git a/test_dir/file in root 2.txt b/test_dir/file in root 2.txt new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/test_dir/file in root 2.txt @@ -0,0 +1 @@ + diff --git a/test_dir/folder1/file in folder.txt b/test_dir/folder1/file in folder.txt new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/test_dir/folder1/file in folder.txt @@ -0,0 +1 @@ + diff --git a/test_dir/folder1/subfolder/file in subfolder.txt b/test_dir/folder1/subfolder/file in subfolder.txt new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/test_dir/folder1/subfolder/file in subfolder.txt @@ -0,0 +1 @@ +