Demo: Display custom task source code upon selection in BehaviorTreeView

This commit is contained in:
Serhii Snitsaruk 2024-02-29 23:02:06 +01:00
parent 7626c84633
commit 24730a9213
4 changed files with 186 additions and 3 deletions

Binary file not shown.

View File

@ -0,0 +1,140 @@
#*
#* code_edit.gd
#* =============================================================================
#* Copyright 2024 Serhii Snitsaruk
#*
#* Use of this source code is governed by an MIT-style
#* license that can be found in the LICENSE file or at
#* https://opensource.org/licenses/MIT.
#* =============================================================================
#*
extends CodeEdit
const RESERVED_WORDS := [
# Control flow.
"break",
"continue",
"elif",
"else",
"for",
"if",
"match",
"pass",
"return",
"when",
"while",
# Declarations.
"class",
"class_name",
"const",
"enum",
"extends",
"func",
"namespace",
"signal",
"static",
"trait",
"var",
# Other keywords.
"await",
"breakpoint",
"self",
"super",
"yield",
# Operators.
"and",
"as",
"in",
"is",
"not",
"or",
# Special values.
"false",
"null",
"true",
# Constants.
"INF",
"NAN",
"PI",
"TAU",
# Functions.
"assert",
"preload",
]
const TYPE_WORDS := [
"bool",
"int",
"float",
"void",
"String",
"Vector2",
"Vector2i",
"Rect2",
"Rect2i",
"Vector3",
"Vector3i",
"Transform2D",
"Vector4",
"Vector4i",
"Plane",
"Quaternion",
"AABB",
"Basis",
"Transform3D",
"Projection",
"Color",
"StringName",
"NodePath",
"RID",
"Callable",
"Signal",
"Dictionary",
"Array",
"PackedByteArray",
"PackedInt32Array",
"PackedInt64Array",
"PackedFloat32Array",
"PackedFloat64Array",
"PackedStringArray",
"PackedVector2Array",
"PackedVector3Array",
"PackedColorArray",
# Other types
"Status",
]
func _ready() -> void:
var highlighter := CodeHighlighter.new()
syntax_highlighter = highlighter
highlighter.number_color = Color.AQUAMARINE
highlighter.symbol_color = Color.CORNFLOWER_BLUE
highlighter.function_color = Color.DEEP_SKY_BLUE
highlighter.member_variable_color = Color.LIGHT_BLUE
# Engine types
for c in ClassDB.get_class_list():
syntax_highlighter.add_keyword_color(c, Color.AQUAMARINE)
syntax_highlighter.add_color_region("#", "", Color.DIM_GRAY, true)
syntax_highlighter.add_color_region("@", " ", Color.GOLDENROD)
syntax_highlighter.add_color_region("\"", "\"", Color.GOLD)
for keyword in RESERVED_WORDS:
syntax_highlighter.add_keyword_color(keyword, Color.INDIAN_RED)
for typeword in TYPE_WORDS:
syntax_highlighter.add_keyword_color(typeword, Color.AQUAMARINE)
func set_source_code(source_code: String) -> void:
# Hide license header
var idx: int = source_code.find("#*")
while idx != - 1:
source_code = source_code.substr(0, idx) + source_code.substr(source_code.findn("\n", idx) + 1)
idx = source_code.findn("#*", idx)
text = "" # Workaround
text = source_code

View File

@ -1,3 +1,14 @@
#*
#* showcase.gd
#* =============================================================================
#* Copyright 2024 Serhii Snitsaruk
#*
#* Use of this source code is governed by an MIT-style
#* license that can be found in the LICENSE file or at
#* https://opensource.org/licenses/MIT.
#* =============================================================================
#*
extends Node2D extends Node2D
@onready var behavior_tree_view: BehaviorTreeView = %BehaviorTreeView @onready var behavior_tree_view: BehaviorTreeView = %BehaviorTreeView
@ -10,6 +21,8 @@ extends Node2D
@onready var begin_tutorial: Button = %BeginTutorial @onready var begin_tutorial: Button = %BeginTutorial
@onready var navigation_hint: Label = %NavigationHint @onready var navigation_hint: Label = %NavigationHint
@onready var scene_title: Label = %SceneTitle @onready var scene_title: Label = %SceneTitle
@onready var code_popup = %CodePopup
@onready var code_edit = %CodeEdit
var bt_player: BTPlayer var bt_player: BTPlayer
var selected_tree_index: int = -1 var selected_tree_index: int = -1
@ -17,6 +30,7 @@ var agent_files: Array[String]
var agents_dir: String var agents_dir: String
var is_tutorial: bool = false var is_tutorial: bool = false
func _ready() -> void: func _ready() -> void:
agent_selection.get_popup().id_pressed.connect(_on_agent_selection_id_pressed) agent_selection.get_popup().id_pressed.connect(_on_agent_selection_id_pressed)
previous.pressed.connect(func(): _on_agent_selection_id_pressed(selected_tree_index - 1)) previous.pressed.connect(func(): _on_agent_selection_id_pressed(selected_tree_index - 1))
@ -123,7 +137,6 @@ func _on_switch_to_game_pressed() -> void:
get_tree().change_scene_to_file("res://demo/scenes/game.tscn") get_tree().change_scene_to_file("res://demo/scenes/game.tscn")
func _on_minimize_description_button_down() -> void: func _on_minimize_description_button_down() -> void:
description.visible = not description.visible description.visible = not description.visible
minimize_description.text = "-" if description.visible else "+" minimize_description.text = "-" if description.visible else "+"
@ -132,3 +145,10 @@ func _on_minimize_description_button_down() -> void:
func _on_tutorial_pressed() -> void: func _on_tutorial_pressed() -> void:
is_tutorial = not is_tutorial is_tutorial = not is_tutorial
_initialize() _initialize()
func _on_behavior_tree_view_task_selected(_type_name: String, p_script_path: String) -> void:
if not p_script_path.is_empty():
var sc: Script = load(p_script_path)
code_edit.set_source_code(sc.source_code)
code_popup.popup.call_deferred()

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=15 format=3 uid="uid://b3ae14mc2ty3y"] [gd_scene load_steps=16 format=3 uid="uid://b3ae14mc2ty3y"]
[ext_resource type="Script" path="res://demo/scenes/showcase.gd" id="1_l12ql"] [ext_resource type="Script" path="res://demo/scenes/showcase.gd" id="1_l12ql"]
[ext_resource type="Theme" uid="uid://boqtjf88xcpu4" path="res://demo/assets/ui.theme" id="2_3d7dj"] [ext_resource type="Theme" uid="uid://boqtjf88xcpu4" path="res://demo/assets/ui.theme" id="2_3d7dj"]
@ -10,6 +10,7 @@
[ext_resource type="Texture2D" uid="uid://bjakugmqbbtw7" path="res://demo/assets/arrow_right.png" id="7_5do2y"] [ext_resource type="Texture2D" uid="uid://bjakugmqbbtw7" path="res://demo/assets/arrow_right.png" id="7_5do2y"]
[ext_resource type="PackedScene" uid="uid://bsig1usigbbuy" path="res://demo/scenes/base/arena.tscn" id="7_42nq6"] [ext_resource type="PackedScene" uid="uid://bsig1usigbbuy" path="res://demo/scenes/base/arena.tscn" id="7_42nq6"]
[ext_resource type="PackedScene" uid="uid://c5fhe3tulhlco" path="res://demo/props/dummy.tscn" id="8_apshw"] [ext_resource type="PackedScene" uid="uid://c5fhe3tulhlco" path="res://demo/props/dummy.tscn" id="8_apshw"]
[ext_resource type="Script" path="res://demo/scenes/base/code_edit.gd" id="9_txke7"]
[sub_resource type="LabelSettings" id="LabelSettings_rdr7a"] [sub_resource type="LabelSettings" id="LabelSettings_rdr7a"]
font = ExtResource("3_7vli5") font = ExtResource("3_7vli5")
@ -242,6 +243,27 @@ bbcode_enabled = true
text = "[b]Behavior Trees[/b] are composed of tasks that represent specific actions or decision-making rules. Tasks can be broadly categorized into two main types: control tasks and leaf tasks. Control tasks determine the execution flow within the tree. They include Sequence, Selector, and Invert. Leaf tasks represent specific actions to perform, like moving or attacking, or conditions that need to be checked. The BTTask class provides the foundation for various building blocks of the Behavior Trees. BT tasks can share data with the help of the Blackboard." text = "[b]Behavior Trees[/b] are composed of tasks that represent specific actions or decision-making rules. Tasks can be broadly categorized into two main types: control tasks and leaf tasks. Control tasks determine the execution flow within the tree. They include Sequence, Selector, and Invert. Leaf tasks represent specific actions to perform, like moving or attacking, or conditions that need to be checked. The BTTask class provides the foundation for various building blocks of the Behavior Trees. BT tasks can share data with the help of the Blackboard."
fit_content = true fit_content = true
[node name="CodePopup" type="PopupPanel" parent="UI Layer/Control"]
unique_name_in_owner = true
position = Vector2i(135, 60)
size = Vector2i(1024, 708)
visible = true
[node name="CodeEdit" type="CodeEdit" parent="UI Layer/Control/CodePopup"]
unique_name_in_owner = true
custom_minimum_size = Vector2(800, 700)
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 4.0
offset_top = 4.0
offset_right = -4.0
offset_bottom = -4.0
grow_horizontal = 2
grow_vertical = 2
gutters_draw_line_numbers = true
script = ExtResource("9_txke7")
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(400, 0) position = Vector2(400, 0)
zoom = Vector2(0.88, 0.88) zoom = Vector2(0.88, 0.88)
@ -262,4 +284,5 @@ position = Vector2(1106, 423)
[connection signal="pressed" from="UI Layer/Control/Toolbar/HBoxContainer/SwitchToGame" to="." method="_on_switch_to_game_pressed"] [connection signal="pressed" from="UI Layer/Control/Toolbar/HBoxContainer/SwitchToGame" to="." method="_on_switch_to_game_pressed"]
[connection signal="pressed" from="UI Layer/Control/Toolbar/HBoxContainer/BeginTutorial" to="." method="_on_tutorial_pressed"] [connection signal="pressed" from="UI Layer/Control/Toolbar/HBoxContainer/BeginTutorial" to="." method="_on_tutorial_pressed"]
[connection signal="task_selected" from="UI Layer/Control/BehaviorInspector/VBoxContainer/BehaviorTreeView" to="." method="_on_behavior_tree_view_task_selected"]
[connection signal="button_down" from="UI Layer/Control/PanelContainer/Control/Header/MinimizeDescription" to="." method="_on_minimize_description_button_down"] [connection signal="button_down" from="UI Layer/Control/PanelContainer/Control/Header/MinimizeDescription" to="." method="_on_minimize_description_button_down"]