From 7329758fd43e679408af077664c322d83fa3613c Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Sat, 8 Feb 2020 01:50:32 -0500 Subject: [PATCH] Add demo selector as main scene Closes #20 --- project/demos/DemoSelector.gd | 26 +++++++++ project/demos/DemoSelector.tscn | 54 +++++++++++++++++++ project/demos/PopulateItemList.gd | 64 +++++++++++++++++++++++ project/project.godot | 9 +--- project/src/Agents/GSTSpecializedAgent.gd | 12 ++--- 5 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 project/demos/DemoSelector.gd create mode 100644 project/demos/DemoSelector.tscn create mode 100644 project/demos/PopulateItemList.gd diff --git a/project/demos/DemoSelector.gd b/project/demos/DemoSelector.gd new file mode 100644 index 0000000..ab62fc8 --- /dev/null +++ b/project/demos/DemoSelector.gd @@ -0,0 +1,26 @@ +extends CenterContainer + + +onready var list := $VBoxContainer/ItemList +onready var button := $VBoxContainer/Button +var selected_demo: String + + +func _ready() -> void: + button.connect("button_down", self, "_on_Button_down") + button.disabled = true + list.connect("item_selected", self, "_on_ItemList_item_selected") + list.connect("item_activated", self, "_on_ItemList_item_activated") + + +func _on_Button_down() -> void: + get_tree().change_scene(selected_demo) + + +func _on_ItemList_item_selected(index: int) -> void: + selected_demo = list.file_paths[index] + button.disabled = false + + +func _on_ItemList_item_activated(index: int) -> void: + _on_Button_down() diff --git a/project/demos/DemoSelector.tscn b/project/demos/DemoSelector.tscn new file mode 100644 index 0000000..bfd679b --- /dev/null +++ b/project/demos/DemoSelector.tscn @@ -0,0 +1,54 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://demos/PopulateItemList.gd" type="Script" id=1] +[ext_resource path="res://assets/theme/gdquest.theme" type="Theme" id=2] +[ext_resource path="res://assets/sprites/background.png" type="Texture" id=3] +[ext_resource path="res://demos/DemoSelector.gd" type="Script" id=4] + +[node name="DemoSelector" type="CenterContainer"] +anchor_right = 1.0 +anchor_bottom = 1.0 +theme = ExtResource( 2 ) +script = ExtResource( 4 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="TextureRect" type="TextureRect" parent="."] +margin_right = 1024.0 +margin_bottom = 600.0 +rect_min_size = Vector2( 1024, 600 ) +size_flags_horizontal = 3 +size_flags_vertical = 3 +texture = ExtResource( 3 ) +expand = true +stretch_mode = 2 + +[node name="VBoxContainer" type="VBoxContainer" parent="."] +margin_left = 170.0 +margin_top = 277.0 +margin_right = 852.0 +margin_bottom = 323.0 +rect_min_size = Vector2( 682, 0 ) +size_flags_horizontal = 3 +size_flags_vertical = 3 + +[node name="ItemList" type="ItemList" parent="VBoxContainer"] +margin_right = 682.0 +margin_bottom = 12.0 +size_flags_vertical = 3 +custom_constants/vseparation = 5 +custom_constants/hseparation = 20 +auto_height = true +max_columns = 2 +same_column_width = true +script = ExtResource( 1 ) + +[node name="Button" type="Button" parent="VBoxContainer"] +margin_left = 283.0 +margin_top = 20.0 +margin_right = 398.0 +margin_bottom = 46.0 +size_flags_horizontal = 4 +size_flags_vertical = 13 +text = "Load scene" diff --git a/project/demos/PopulateItemList.gd b/project/demos/PopulateItemList.gd new file mode 100644 index 0000000..a984acb --- /dev/null +++ b/project/demos/PopulateItemList.gd @@ -0,0 +1,64 @@ +extends ItemList + + +var file_paths := PoolStringArray() + + +func _ready() -> void: + file_paths = _find_files("./", ["*Demo.tscn"], true) + populate(file_paths) + + +func populate(demos: PoolStringArray) -> void: + for demo in demos: + var start: int = demo.find_last("/")+1 + var end: int = demo.find_last("Demo") + var length := end - start + var demo_name: String = demo.substr(start, length) + demo_name = sentencify(demo_name) + add_item(demo_name) + + +func sentencify(line: String) -> String: + var word_starts := [] + for i in range(line.length()): + var code := line.ord_at(i) + if code < 97: + word_starts.append(i) + var sentence := "" + var last := 0 + for i in range(word_starts.size()-1): + var start: int = word_starts[i] + last = word_starts[i+1] + var length: = last - start + sentence += line.substr(start, length) + " " + sentence += line.substr(last) + return sentence + + +func _find_files(dirpath := "", patterns := PoolStringArray(), is_recursive := false, do_skip_hidden := true) -> PoolStringArray: + var file_paths: = PoolStringArray() + var directory: = Directory.new() + + if not directory.dir_exists(dirpath): + printerr("The directory does not exist: %s" % dirpath) + return file_paths + if not directory.open(dirpath) == OK: + printerr("Could not open the following dirpath: %s" % dirpath) + return file_paths + + directory.list_dir_begin(true, do_skip_hidden) + var file_name: = directory.get_next() + var subdirectories: = PoolStringArray() + while file_name != "": + if directory.current_is_dir() and is_recursive: + var subdirectory: = dirpath.plus_file(file_name) + file_paths.append_array(_find_files(subdirectory, patterns, is_recursive)) + else: + for pattern in patterns: + if file_name.match(pattern): + file_paths.append(dirpath.plus_file(file_name)) + file_name = directory.get_next() + + directory.list_dir_end() + return file_paths diff --git a/project/project.godot b/project/project.godot index 888d2b6..bba8571 100644 --- a/project/project.godot +++ b/project/project.godot @@ -153,11 +153,6 @@ _global_script_classes=[ { "class": "GSTUtils", "language": "GDScript", "path": "res://src/GSTUtils.gd" -}, { -"base": "EditorScript", -"class": "ReferenceCollector", -"language": "GDScript", -"path": "res://ReferenceCollector.gd" } ] _global_script_class_icons={ "GSTAgentLocation": "", @@ -188,13 +183,13 @@ _global_script_class_icons={ "GSTSteeringAgent": "", "GSTSteeringBehavior": "", "GSTTargetAcceleration": "", -"GSTUtils": "", -"ReferenceCollector": "" +"GSTUtils": "" } [application] config/name="SteeringToolkit" +run/main_scene="res://demos/DemoSelector.tscn" config/icon="res://icon.png" [display] diff --git a/project/src/Agents/GSTSpecializedAgent.gd b/project/src/Agents/GSTSpecializedAgent.gd index 2f97b7d..80daa7e 100644 --- a/project/src/Agents/GSTSpecializedAgent.gd +++ b/project/src/Agents/GSTSpecializedAgent.gd @@ -9,23 +9,23 @@ class_name GSTSpecializedAgent # frame. When `false`, the user must keep those values updated. var calculate_velocities := true -# If `true` and `calculate_velocities` is true, interpolates -# the current linear velocity towards 0 by the `linear_drag_percentage` value. +# If `true`, interpolates the current linear velocity towards 0 by the +# `linear_drag_percentage` value. # Does not apply to `RigidBody` and `RigidBody2D` nodes. var apply_linear_drag := true -# If `true` and `calculate_velocities` is true, interpolates -# the current angular velocity towards 0 by the `angular_drag_percentage` value. +# If `true`, interpolates the current angular velocity towards 0 by the +# `angular_drag_percentage` value. # Does not apply to `RigidBody` and `RigidBody2D` nodes. var apply_angular_drag := true # The percentage between the current linear velocity and 0 to interpolate by if -# `calculate_velocities` and `apply_linear_drag` are true. +# `apply_linear_drag` is true. # Does not apply to `RigidBody` and `RigidBody2D` nodes. var linear_drag_percentage := 0.0 # The percentage between the current angular velocity and 0 to interpolate by if -# `calculate_velocities` and `apply_angular_drag` are true. +# `apply_angular_drag` is true. # Does not apply to `RigidBody` and `RigidBody2D` nodes. var angular_drag_percentage := 0.0