📝 added documentation and improved event scope

pull/19/head
kziolkowski 3 months ago
parent 1e004b62b8
commit a593be8273

@ -6,5 +6,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="prefabs\UI\Inventory\" /> <Folder Include="prefabs\UI\Inventory\" />
<Folder Include="scripts\CSharp\Low Code\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -144,7 +144,6 @@ directories/tres_directory={
"default_bus_layout": "res://audio/default_bus_layout.tres", "default_bus_layout": "res://audio/default_bus_layout.tres",
"default_stylebox": "res://addons/dialogic/Modules/DefaultLayoutParts/Layer_SpeakerPortraitTextbox/default_stylebox.tres", "default_stylebox": "res://addons/dialogic/Modules/DefaultLayoutParts/Layer_SpeakerPortraitTextbox/default_stylebox.tres",
"default_vn_style": "res://addons/dialogic/Modules/DefaultLayoutParts/Style_VN_Default/default_vn_style.tres", "default_vn_style": "res://addons/dialogic/Modules/DefaultLayoutParts/Style_VN_Default/default_vn_style.tres",
"events/test": "res://resources/events/test.tres",
"farming_equipment_glossary": "res://dialog/farming_equipment_glossary.tres", "farming_equipment_glossary": "res://dialog/farming_equipment_glossary.tres",
"hoe": "res://resources/items/hoe.tres", "hoe": "res://resources/items/hoe.tres",
"inventory_interactable_outline": "res://art/materials/inventory_interactable_outline.tres", "inventory_interactable_outline": "res://art/materials/inventory_interactable_outline.tres",
@ -163,6 +162,7 @@ directories/tres_directory={
"test/test_01": "res://resources/quests/test/test_01.tres", "test/test_01": "res://resources/quests/test/test_01.tres",
"test/test_02": "res://resources/quests/test/test_02.tres", "test/test_02": "res://resources/quests/test/test_02.tres",
"test/test_03": "res://resources/quests/test/test_03.tres", "test/test_03": "res://resources/quests/test/test_03.tres",
"test2": "res://resources/events/test2.tres",
"textbubble_style": "res://addons/dialogic/Modules/DefaultLayoutParts/Style_TextBubbles/textbubble_style.tres", "textbubble_style": "res://addons/dialogic/Modules/DefaultLayoutParts/Style_TextBubbles/textbubble_style.tres",
"tomato": "res://resources/items/tomato.tres", "tomato": "res://resources/items/tomato.tres",
"tomato_seed": "res://resources/items/tomato_seed.tres", "tomato_seed": "res://resources/items/tomato_seed.tres",

@ -1,10 +1,8 @@
[gd_resource type="Resource" script_class="EventResource" load_steps=2 format=3 uid="uid://beed6ikqf46vk"] [gd_resource type="Resource" script_class="EventResource" load_steps=2 format=3 uid="uid://bgfxakxxfmoxs"]
[ext_resource type="Script" uid="uid://ci3t5mvnopntg" path="res://scripts/CSharp/Events/EventResource.cs" id="1_6u8ge"] [ext_resource type="Script" uid="uid://ci3t5mvnopntg" path="res://scripts/CSharp/Low Code/Events/EventResource.cs" id="1_jm77s"]
[resource] [resource]
resource_name = "kafjhkj" script = ExtResource("1_jm77s")
script = ExtResource("1_6u8ge")
Name = "kafjhkj"
_showLog = true _showLog = true
metadata/_custom_type_script = "uid://ci3t5mvnopntg" metadata/_custom_type_script = "uid://ci3t5mvnopntg"

@ -1,11 +1,11 @@
[gd_scene load_steps=7 format=3 uid="uid://c6wnoif01ltld"] [gd_scene load_steps=7 format=3 uid="uid://c6wnoif01ltld"]
[ext_resource type="Script" uid="uid://cssdu8viimwm6" path="res://scripts/CSharp/Common/SceneTransition.cs" id="1_fj2fh"] [ext_resource type="Script" uid="uid://cssdu8viimwm6" path="res://scripts/CSharp/Common/SceneTransition.cs" id="1_fj2fh"]
[ext_resource type="Script" uid="uid://b5dotkx17gvxg" path="res://scripts/CSharp/Events/EventCaller.cs" id="3_beyjr"] [ext_resource type="Script" uid="uid://b5dotkx17gvxg" path="res://scripts/CSharp/Low Code/Events/EventRaiser.cs" id="3_5a78e"]
[ext_resource type="Texture2D" uid="uid://c7atj6ohlmir3" path="res://art/ui/StartScreen/titlescreen.png" id="3_r0y6o"] [ext_resource type="Texture2D" uid="uid://c7atj6ohlmir3" path="res://art/ui/StartScreen/titlescreen.png" id="3_r0y6o"]
[ext_resource type="Script" uid="uid://bc6uaaxsx5k5p" path="res://scripts/CSharp/Events/EventListener.cs" id="4_beyjr"] [ext_resource type="Resource" uid="uid://bgfxakxxfmoxs" path="res://resources/events/test2.tres" id="4_5a78e"]
[ext_resource type="Texture2D" uid="uid://du612t3xytly3" path="res://art/ui/StartScreen/babushkalog_white.png" id="4_dl0t1"] [ext_resource type="Texture2D" uid="uid://du612t3xytly3" path="res://art/ui/StartScreen/babushkalog_white.png" id="4_dl0t1"]
[ext_resource type="Resource" uid="uid://beed6ikqf46vk" path="res://resources/events/test.tres" id="5_5a78e"] [ext_resource type="Script" uid="uid://bc6uaaxsx5k5p" path="res://scripts/CSharp/Low Code/Events/EventListener.cs" id="6_5a78e"]
[node name="BabushkaSceneStartMenu" type="Node2D"] [node name="BabushkaSceneStartMenu" type="Node2D"]
script = ExtResource("1_fj2fh") script = ExtResource("1_fj2fh")
@ -55,7 +55,7 @@ toggle_mode = true
text = "Quit text = "Quit
" "
[node name="Quit2" type="Button" parent="CanvasLayer/TextureRect"] [node name="Test" type="Button" parent="CanvasLayer/TextureRect"]
custom_minimum_size = Vector2(100, 30) custom_minimum_size = Vector2(100, 30)
layout_mode = 1 layout_mode = 1
anchors_preset = 4 anchors_preset = 4
@ -70,8 +70,8 @@ scale = Vector2(2, 2)
toggle_mode = true toggle_mode = true
text = "Test text = "Test
" "
script = ExtResource("3_beyjr") script = ExtResource("3_5a78e")
eventResource = ExtResource("5_5a78e") _eventResources = Array[Object]([ExtResource("4_5a78e")])
[node name="Text" type="TextureRect" parent="CanvasLayer/TextureRect"] [node name="Text" type="TextureRect" parent="CanvasLayer/TextureRect"]
layout_mode = 1 layout_mode = 1
@ -83,11 +83,11 @@ scale = Vector2(0.5, 0.5)
texture = ExtResource("4_dl0t1") texture = ExtResource("4_dl0t1")
stretch_mode = 3 stretch_mode = 3
[node name="Node" type="Node" parent="."] [node name="EventListener" type="Node" parent="."]
script = ExtResource("4_beyjr") script = ExtResource("6_5a78e")
_eventResource = ExtResource("5_5a78e") _eventResources = Array[Object]([ExtResource("4_5a78e")])
_showLog = true
[connection signal="pressed" from="CanvasLayer/TextureRect/Start" to="." method="LoadScene"] [connection signal="pressed" from="CanvasLayer/TextureRect/Start" to="." method="LoadScene"]
[connection signal="pressed" from="CanvasLayer/TextureRect/Quit" to="." method="Quit"] [connection signal="pressed" from="CanvasLayer/TextureRect/Quit" to="." method="Quit"]
[connection signal="pressed" from="CanvasLayer/TextureRect/Quit2" to="CanvasLayer/TextureRect/Quit2" method="RaiseEvent"] [connection signal="pressed" from="CanvasLayer/TextureRect/Test" to="CanvasLayer/TextureRect/Test" method="RaiseEvents"]
[connection signal="EventRaised" from="EventListener" to="CanvasLayer/TextureRect/Test" method="hide"]

@ -1,13 +0,0 @@
using Godot;
namespace Babushka.scripts.CSharp.Events;
public partial class EventCaller : Node
{
[Export] EventResource eventResource;
public void RaiseEvent()
{
eventResource.Raise();
}
}

@ -1,28 +0,0 @@
using Godot;
namespace Babushka.scripts.CSharp.Events;
public partial class EventListener : Node
{
[Export] private EventResource _eventResource;
[Export] private bool _showLog;
[Signal] public delegate void EventRaisedEventHandler();
public override void _EnterTree()
{
_eventResource.RegisterListener(this);
}
public override void _ExitTree()
{
_eventResource.UnregisterListener(this);
}
public void RaiseEvent()
{
if(_showLog)
GD.Print("Event Raised on: " + Name);
EmitSignal(SignalName.EventRaised);
}
}

@ -1,44 +0,0 @@
using System.Collections.Generic;
using Godot;
namespace Babushka.scripts.CSharp.Events;
[GlobalClass] [Tool]
public partial class EventResource : Resource
{
[Export] private string Name
{
get { return ResourceName; }
set { ResourceName = value; }
}
[Export] private bool _showLog;
[ExportToolButton("Raise")] Callable _raiseAction => Callable.From(Raise);
private List<EventListener> _eventListeners = new ();
public void RegisterListener(EventListener listener)
{
if(_showLog)
GD.Print("Registering listener " + listener);
_eventListeners.Add(listener);
}
public void UnregisterListener(EventListener listener)
{
if(_showLog)
GD.Print("Unregistering listener " + listener);
_eventListeners.Remove(listener);
}
public void Raise()
{
if(_showLog)
GD.Print("Raising event: " + Name);
foreach (var eventListener in _eventListeners)
{
eventListener.RaiseEvent();
}
}
}

@ -0,0 +1,62 @@
using System;
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Low_Code.Events;
/// <summary>
/// Listens to an <see cref="EventResource"/> as long as this node is in the tree (i.e., during playtime).
/// </summary>
public partial class EventListener : Node
{
/// <summary>
/// The event resource to listen to.
/// </summary>
[Export] private Array<EventResource> _eventResources;
/// <summary>
/// Log to console when this event is being raised.
/// </summary>
[Export] private bool _showLog;
/// <summary>
/// The signal that is triggered when this listener is called by one of the <see cref="EventListener._eventResources"/>.
/// </summary>
[Signal] public delegate void EventRaisedEventHandler();
/// <summary>
/// Subscribes to all <see cref="EventResource"/>s present in the <see cref="_eventResources"/> array.
/// <inheritdoc cref="Node._EnterTree"/>
/// </summary>
public override void _EnterTree()
{
foreach (var eventRes in _eventResources)
{
eventRes.RegisterListener(this);
}
}
/// <summary>
/// Unsubscribes from all <see cref="EventResource"/>s present in the <see cref="_eventResources"/> array.
/// <inheritdoc cref="Node._ExitTree"/>
/// </summary>
public override void _ExitTree()
{
foreach (var eventRes in _eventResources)
{
eventRes.UnregisterListener(this);
}
}
/// <summary>
/// Called by a <see cref="EventResource"/>s from the <see cref="_eventResources"/> array.
/// Propagates the event by emitting <see cref="EventRaised"/> signal.
/// </summary>
public void Invoke()
{
if(_showLog)
GD.Print("Event Raised on: " + Name);
EmitSignal(SignalName.EventRaised);
}
}

@ -0,0 +1,35 @@
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Low_Code.Events;
/// <summary>
/// Raises one or more <see cref="EventResource"/>s from the scene.
/// </summary>
public partial class EventRaiser : Node
{
/// <summary>
/// The <see cref="EventResource"/>s to call
/// </summary>
[Export] Array<EventResource> _eventResources;
/// <summary>
/// Raises all <see cref="EventResource"/>s present in <see cref="_eventResources"/>.
/// </summary>
public void RaiseEvents()
{
foreach (var eventRes in _eventResources)
{
eventRes.Raise();
}
}
/// <summary>
/// Raises <see cref="EventResource"/> at index.
/// </summary>
/// <param name="index"></param>
public void RaiseEvent(int index)
{
_eventResources[index].Raise();
}
}

@ -0,0 +1,62 @@
using System.Collections.Generic;
using Godot;
namespace Babushka.scripts.CSharp.Low_Code.Events;
/// <summary>
/// Represents an event in the project space.
/// Can be called by <see cref="EventRaiser"/> and subscribed to by <see cref="EventListener"/>.
/// </summary>
[GlobalClass] [Tool]
public partial class EventResource : Resource
{
/// <summary>
/// Log into console when this event resource is adding or removing listeners, and when it's raised.
/// </summary>
[Export] private bool _showLog;
/// <summary>
/// Raise-Button Call with Editor Export for easier debugging.
/// Beware: This will only work with custom event listeners that register at edit-time.
/// Standard <see cref="EventListeners"/> register and deregister during playtime.
/// </summary>
[ExportToolButton("Raise")] Callable _raiseAction => Callable.From(Raise);
private List<EventListener> _eventListeners = new ();
/// <summary>
/// Adds an EventListener to the calling list for this event.
/// </summary>
/// <param name="listener"></param>
public void RegisterListener(EventListener listener)
{
if(_showLog)
GD.Print("Registering listener " + listener);
_eventListeners.Add(listener);
}
/// <summary>
/// Removes an Eventlistener from the calling list for this event.
/// </summary>
/// <param name="listener"></param>
public void UnregisterListener(EventListener listener)
{
if(_showLog)
GD.Print("Unregistering listener " + listener);
_eventListeners.Remove(listener);
}
/// <summary>
/// Raises this event on all current listeners.
/// </summary>
public void Raise()
{
if(_showLog)
GD.Print("Raising event: " + ResourcePath.GetFile().TrimSuffix(".tres"));
foreach (var eventListener in _eventListeners)
{
eventListener.Invoke();
}
}
}
Loading…
Cancel
Save