Compare commits

...

2 Commits

Author SHA1 Message Date
kziolkowski a593be8273 📝 added documentation and improved event scope 2025-10-18 17:15:35 +02:00
kziolkowski 1e004b62b8 Implemented first version of EventResources 2025-10-13 09:57:49 +02:00
12 changed files with 201 additions and 3 deletions
+1
View File
@@ -6,5 +6,6 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="prefabs\UI\Inventory\" />
<Folder Include="scripts\CSharp\Low Code\" />
</ItemGroup>
</Project>
+1
View File
@@ -8,6 +8,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACastHelpers_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fd111abf504bf42b5968a609b168fd093b2e200_003Fbb_003F1c116fcd_003FCastHelpers_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACharacterBody2D_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FSourcesCache_003Fbba0bbd7a98ee58286e9484fbe86e01afff6232283f6efd3556eb7116453_003FCharacterBody2D_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADictionary_00602_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003Fhome_003Fjonathan_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe37dc1faf08a4d5ea030ad59bdf77522523400_003Fd4_003Fbd338aeb_003FDictionary_00602_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AExportToolButtonAttribute_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe37dc1faf08a4d5ea030ad59bdf77522523400_003F31_003F3e05ef15_003FExportToolButtonAttribute_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AMustBeVariantAttribute_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fe37dc1faf08a4d5ea030ad59bdf77522523400_003Fda_003Fbb06d681_003FMustBeVariantAttribute_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANode_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FSourcesCache_003Ff1d69ec2da76ccf9bc8a75c8e0fdca9a7ba1adf8c8c9d5047e2fa5991c02eca_003FNode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AResourceLoader_002Ecs_002Fl_003AC_0021_003FUsers_003FJonathan_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FSourcesCache_003F9f4e8eb124d11f8219cb513a19bed22b2120ed29f9d6785ba56e3367b48d581_003FResourceLoader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+1
View File
@@ -162,6 +162,7 @@ directories/tres_directory={
"test/test_01": "res://resources/quests/test/test_01.tres",
"test/test_02": "res://resources/quests/test/test_02.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",
"tomato": "res://resources/items/tomato.tres",
"tomato_seed": "res://resources/items/tomato_seed.tres",
+8
View File
@@ -0,0 +1,8 @@
[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/Low Code/Events/EventResource.cs" id="1_jm77s"]
[resource]
script = ExtResource("1_jm77s")
_showLog = true
metadata/_custom_type_script = "uid://ci3t5mvnopntg"
@@ -98,7 +98,6 @@ _followNode = NodePath("../Vesna/CharacterBody2D")
[node name="BedInteraction" parent="." instance=ExtResource("8_phqdf")]
position = Vector2(-1429, 487)
_useOutline = false
_useSprite = false
_id = 1
@@ -109,7 +108,6 @@ toStatus = 2
[node name="DoorInteraction" parent="." instance=ExtResource("8_phqdf")]
position = Vector2(777, 201)
_useOutline = false
_useSprite = false
_id = 0
+28 -1
View File
@@ -1,8 +1,11 @@
[gd_scene load_steps=4 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://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="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="Script" uid="uid://bc6uaaxsx5k5p" path="res://scripts/CSharp/Low Code/Events/EventListener.cs" id="6_5a78e"]
[node name="BabushkaSceneStartMenu" type="Node2D"]
script = ExtResource("1_fj2fh")
@@ -52,6 +55,24 @@ toggle_mode = true
text = "Quit
"
[node name="Test" type="Button" parent="CanvasLayer/TextureRect"]
custom_minimum_size = Vector2(100, 30)
layout_mode = 1
anchors_preset = 4
anchor_top = 0.5
anchor_bottom = 0.5
offset_left = 205.0
offset_top = 215.0
offset_right = 405.0
offset_bottom = 255.0
grow_vertical = 2
scale = Vector2(2, 2)
toggle_mode = true
text = "Test
"
script = ExtResource("3_5a78e")
_eventResources = Array[Object]([ExtResource("4_5a78e")])
[node name="Text" type="TextureRect" parent="CanvasLayer/TextureRect"]
layout_mode = 1
offset_left = 50.0
@@ -62,5 +83,11 @@ scale = Vector2(0.5, 0.5)
texture = ExtResource("4_dl0t1")
stretch_mode = 3
[node name="EventListener" type="Node" parent="."]
script = ExtResource("6_5a78e")
_eventResources = Array[Object]([ExtResource("4_5a78e")])
[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/Test" to="CanvasLayer/TextureRect/Test" method="RaiseEvents"]
[connection signal="EventRaised" from="EventListener" to="CanvasLayer/TextureRect/Test" method="hide"]
@@ -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 @@
uid://bc6uaaxsx5k5p
@@ -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 @@
uid://b5dotkx17gvxg
@@ -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();
}
}
}
@@ -0,0 +1 @@
uid://ci3t5mvnopntg