From 00a186093b89f3241fb4dfe84d5d768bd7d0727d Mon Sep 17 00:00:00 2001 From: Non0w Date: Wed, 9 Sep 2020 23:41:48 +0200 Subject: [PATCH] Add choice buttons ; Run async using yield --- addons/yarn_spinner/yarn_runner.gd | 18 ++++---- addons/yarn_spinner/yarn_spinner_importer.gd | 5 --- main/scenes/ChoicesBox.tscn | 13 ++++++ main/scenes/LogPanel.tscn | 34 ++++++++++++++ main/scenes/test_viteuf.tscn | 12 +++-- {yarn => main}/scripts/ChoicesBox.gd | 2 +- {yarn => main}/scripts/LogPanel.gd | 0 main/scripts/test_viteuf.gd | 40 +++++++++-------- project.godot | 4 +- yarn/scenes/YarnSpinner.tscn | 47 ++------------------ 10 files changed, 95 insertions(+), 80 deletions(-) create mode 100644 main/scenes/ChoicesBox.tscn create mode 100644 main/scenes/LogPanel.tscn rename {yarn => main}/scripts/ChoicesBox.gd (97%) rename {yarn => main}/scripts/LogPanel.gd (100%) diff --git a/addons/yarn_spinner/yarn_runner.gd b/addons/yarn_spinner/yarn_runner.gd index a40a198..6e7bf70 100644 --- a/addons/yarn_spinner/yarn_runner.gd +++ b/addons/yarn_spinner/yarn_runner.gd @@ -5,12 +5,13 @@ var YarnScript = preload("yarn_script.gd") export (Resource) var yarnScript var say_func : FuncRef = null +var choices_func : FuncRef = null func run_all(): var next_node = yarnScript.nodes["Start"] while next_node != null: print("-> Jump to " + next_node["title"]) - var next_node_key = run_body(next_node["body"]) + var next_node_key = yield(run_body(next_node["body"]), "completed") if (yarnScript.nodes.has(next_node_key)): next_node = yarnScript.nodes[next_node_key] else: @@ -21,25 +22,26 @@ func run_body(body): if element["type"] == "jump": return element["node"] elif element["type"] == "choice_blocks": - var block = decide_choice_block(element["blocks"]) + var block = yield(decide_choice_block(element["blocks"]), "completed") + print(block) return block["node"] # todo change later with "-> choices" elif element["type"] == "condition_blocks": var block = decide_condition_block(element["blocks"]) - var next_node_key = run_body(block["body"]) + var next_node_key = yield(run_body(block["body"]), "completed") if next_node_key != "": return next_node_key else: if say_func == null or !say_func.is_valid(): print(element) else: - say_func.call_func(element) - + yield(say_func.call_func(element), "completed") return "" -export var choice = 0 - func decide_choice_block(blocks): - return blocks[choice] + if choices_func == null or !choices_func.is_valid(): + return blocks[0] + else: + return yield(choices_func.call_func(blocks), "completed") func decide_condition_block(blocks): for block in blocks: diff --git a/addons/yarn_spinner/yarn_spinner_importer.gd b/addons/yarn_spinner/yarn_spinner_importer.gd index 8f32876..2824c4f 100644 --- a/addons/yarn_spinner/yarn_spinner_importer.gd +++ b/addons/yarn_spinner/yarn_spinner_importer.gd @@ -220,11 +220,6 @@ func get_preset_name(preset): func get_import_options(preset): match preset: - Presets.DEFAULT: - return [{ - "name": "use_red_anyway", - "default_value": false - }] _: return [] diff --git a/main/scenes/ChoicesBox.tscn b/main/scenes/ChoicesBox.tscn new file mode 100644 index 0000000..ec1ef99 --- /dev/null +++ b/main/scenes/ChoicesBox.tscn @@ -0,0 +1,13 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://main/scripts/ChoicesBox.gd" type="Script" id=1] + +[node name="ChoicesBox" type="VBoxContainer"] +anchor_left = 0.3 +anchor_top = 0.1 +anchor_right = 0.7 +anchor_bottom = 0.45 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/main/scenes/LogPanel.tscn b/main/scenes/LogPanel.tscn new file mode 100644 index 0000000..902933c --- /dev/null +++ b/main/scenes/LogPanel.tscn @@ -0,0 +1,34 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://main/scripts/LogPanel.gd" type="Script" id=1] + +[node name="LogPanel" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="Panel" type="Panel" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 + +[node name="RichTextLabel" type="RichTextLabel" parent="."] +anchor_left = 0.02 +anchor_top = 0.05 +anchor_right = 0.98 +anchor_bottom = 0.95 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="TextButton" type="Button" parent="."] +modulate = Color( 1, 1, 1, 0.235294 ) +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -49.0 +flat = true +__meta__ = { +"_edit_use_anchors_": false +} diff --git a/main/scenes/test_viteuf.tscn b/main/scenes/test_viteuf.tscn index 299333b..bdb694e 100644 --- a/main/scenes/test_viteuf.tscn +++ b/main/scenes/test_viteuf.tscn @@ -1,8 +1,10 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://main/scripts/test_viteuf.gd" type="Script" id=1] [ext_resource path="res://main/dialogues/test.yarn" type="Resource" id=2] [ext_resource path="res://addons/yarn_spinner/yarn_runner.gd" type="Script" id=3] +[ext_resource path="res://main/scenes/LogPanel.tscn" type="PackedScene" id=4] +[ext_resource path="res://main/scenes/ChoicesBox.tscn" type="PackedScene" id=5] [node name="Node" type="Control"] anchor_right = 1.0 @@ -16,6 +18,10 @@ __meta__ = { script = ExtResource( 3 ) yarnScript = ExtResource( 2 ) -[node name="RichTextLabel" type="RichTextLabel" parent="."] -anchor_right = 1.0 +[node name="LogPanel" parent="." instance=ExtResource( 4 )] +anchor_bottom = 0.6 + +[node name="ChoicesBox" parent="." instance=ExtResource( 5 )] +anchor_top = 0.6 anchor_bottom = 1.0 +alignment = 1 diff --git a/yarn/scripts/ChoicesBox.gd b/main/scripts/ChoicesBox.gd similarity index 97% rename from yarn/scripts/ChoicesBox.gd rename to main/scripts/ChoicesBox.gd index 0296865..371b9a1 100644 --- a/yarn/scripts/ChoicesBox.gd +++ b/main/scripts/ChoicesBox.gd @@ -11,7 +11,7 @@ func on_choices(choices_list): for choice in choices_list: var choiceButton := Button.new() choiceButton.text = choice["text"] - choiceButton.connect("pressed", self, "on_choice_made", [choice["marker"]]) + choiceButton.connect("pressed", self, "on_choice_made", [choice]) add_child(choiceButton) var res = yield(self, "choice_made") hide() diff --git a/yarn/scripts/LogPanel.gd b/main/scripts/LogPanel.gd similarity index 100% rename from yarn/scripts/LogPanel.gd rename to main/scripts/LogPanel.gd diff --git a/main/scripts/test_viteuf.gd b/main/scripts/test_viteuf.gd index 97570b9..3487123 100644 --- a/main/scripts/test_viteuf.gd +++ b/main/scripts/test_viteuf.gd @@ -1,24 +1,28 @@ extends Control func _ready(): - $YarnRunner.say_func = funcref(self, "say") - $YarnRunner.run_all() +# $ChoicesBox.hide() +# $LogPanel.hide() + on_dialogue_start() + $YarnRunner.say_func = funcref(self, "on_new_line") + $YarnRunner.choices_func = funcref(self, "on_choices") + yield($YarnRunner.run_all(), "completed") + on_dialogue_end() -func say(element): - $RichTextLabel.text += str(element["raw"]) + "\n" +func on_dialogue_start(): + $LogPanel.show() + $ChoicesBox.show() + $LogPanel.clear() + $ChoicesBox.clear() -func exec(stri): - var expr := Expression.new() - print("input : " + stri) - var err := expr.parse(stri) - if err == OK: - var res = expr.execute() - print("res : " + str(res)) - else: - print("err : " + str(err)) - -func jajoujaj(): - print("jaaaja") +func on_new_line(line): + yield($LogPanel.on_new_line(line["text"]), "completed") -var current_node = "Start" -var current_line = 0 +func on_choices(choices_list): + print(choices_list) + return yield($ChoicesBox.on_choices(choices_list), "completed") + +func on_dialogue_end(): + pass +# $ChoicesBox.hide() +# $LogPanel.hide() diff --git a/project.godot b/project.godot index d7b1c9a..76cc070 100644 --- a/project.godot +++ b/project.godot @@ -12,12 +12,12 @@ _global_script_classes=[ { "base": "VBoxContainer", "class": "ChoicesBox", "language": "GDScript", -"path": "res://yarn/scripts/ChoicesBox.gd" +"path": "res://main/scripts/ChoicesBox.gd" }, { "base": "Control", "class": "LogPanel", "language": "GDScript", -"path": "res://yarn/scripts/LogPanel.gd" +"path": "res://main/scripts/LogPanel.gd" }, { "base": "Node", "class": "YarnImporter", diff --git a/yarn/scenes/YarnSpinner.tscn b/yarn/scenes/YarnSpinner.tscn index e25401d..0344840 100644 --- a/yarn/scenes/YarnSpinner.tscn +++ b/yarn/scenes/YarnSpinner.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=4 format=2] -[ext_resource path="res://yarn/scripts/LogPanel.gd" type="Script" id=1] -[ext_resource path="res://yarn/scripts/ChoicesBox.gd" type="Script" id=2] +[ext_resource path="res://main/scenes/ChoicesBox.tscn" type="PackedScene" id=1] +[ext_resource path="res://main/scenes/LogPanel.tscn" type="PackedScene" id=2] [ext_resource path="res://yarn/scripts/YarnSpinner.gd" type="Script" id=3] [node name="YarnSpinner" type="Control"] @@ -12,45 +12,6 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ChoicesBox" type="VBoxContainer" parent="."] -anchor_left = 0.3 -anchor_top = 0.1 -anchor_right = 0.7 -anchor_bottom = 0.45 -script = ExtResource( 2 ) -__meta__ = { -"_edit_use_anchors_": false -} +[node name="ChoicesBox" parent="." instance=ExtResource( 1 )] -[node name="LogPanel" type="Control" parent="."] -anchor_top = 0.5 -anchor_right = 1.0 -anchor_bottom = 1.0 -script = ExtResource( 1 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="Panel" type="Panel" parent="LogPanel"] -anchor_right = 1.0 -anchor_bottom = 1.0 - -[node name="RichTextLabel" type="RichTextLabel" parent="LogPanel"] -anchor_left = 0.02 -anchor_top = 0.05 -anchor_right = 0.98 -anchor_bottom = 0.95 -text = "Coucou je suis un text de test, ou bien l'inverse" -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="TextButton" type="Button" parent="LogPanel"] -modulate = Color( 1, 1, 1, 0.235294 ) -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_right = -20.0 -flat = true -__meta__ = { -"_edit_use_anchors_": false -} +[node name="LogPanel" parent="." instance=ExtResource( 2 )]