Skip to content

Commit 1f98da1

Browse files
committed
Merge branch '2.4.1'
2 parents 4032d9c + cb48767 commit 1f98da1

9 files changed

Lines changed: 116 additions & 21 deletions

src/Bolt.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
3-
<BoltVersion>1.4.1</BoltVersion>
3+
<BoltVersion>1.4.7</BoltVersion>
44
</PropertyGroup>
55
<PropertyGroup Condition="'$(Configuration)' == 'Release4.6'">
66
<TargetFrameworkVersionNumber>4.6</TargetFrameworkVersionNumber>

src/Events/Bolt.Addons.Community.Events.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@
4949
<Compile Include="Support\Internal\IDefinedEventUnit.cs" />
5050
<Compile Include="Units\TriggerGlobalDefinedEvent.cs" />
5151
<Compile Include="Units\TriggerDefinedEvent.cs" />
52-
<Compile Include="Units\GlobalDefinedEvent.cs" />
52+
<Compile Include="Units\GlobalDefinedEventUnit.cs" />
5353
<Compile Include="Support\DefinedEventArgs.cs" />
54-
<Compile Include="Units\DefinedEvent.cs" />
54+
<Compile Include="Units\DefinedEventUnit.cs" />
5555
</ItemGroup>
5656
<ItemGroup>
5757
<ProjectReference Include="..\Fundamentals\Bolt.Addons.Community.Fundamentals.csproj">
5858
<Project>{c7fa2fb0-acd3-4957-a653-979afadc3145}</Project>
5959
<Name>Bolt.Addons.Community.Fundamentals</Name>
6060
</ProjectReference>
61+
<ProjectReference Include="..\Utility\Bolt.Addons.Community.Utility.csproj">
62+
<Project>{A2FDB1FB-E259-45C0-B46A-4E416110BBB3}</Project>
63+
<Name>Bolt.Addons.Community.Utility</Name>
64+
</ProjectReference>
6165
</ItemGroup>
6266
<ItemGroup />
6367
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

src/Events/ScriptingProxy/DefinedEvent.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Bolt.Addons.Community.DefinedEvents.Units;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -17,7 +18,7 @@ public static class DefinedEvent
1718
/// <param name="eventData">This is a filled object of the type of event you want to trigger.</param>
1819
public static void Trigger(GameObject target, object eventData)
1920
{
20-
Bolt.Addons.Community.DefinedEvents.Units.DefinedEvent.Trigger(target, eventData);
21+
DefinedEventUnit.Trigger(target, eventData);
2122
}
2223

2324
/// <summary>
@@ -28,7 +29,37 @@ public static void Trigger(GameObject target, object eventData)
2829
/// <param name="eventData">This is a filled object of the type of event you want to trigger.</param>
2930
public static void TriggerGlobal(object eventData)
3031
{
31-
Bolt.Addons.Community.DefinedEvents.Units.GlobalDefinedEvent.Trigger(eventData);
32+
GlobalDefinedEventUnit.Trigger(eventData);
33+
}
34+
35+
/// <summary>
36+
/// Registers a C# listener for an event on the target object. This is the scripting
37+
/// equivalent to the Defined Event unit. Notice the IDisposable return value, which allows you
38+
/// to end the subscription for the event (via calling the .Dispose() method).
39+
/// </summary>
40+
/// <typeparam name="T">The type to listen for.</typeparam>
41+
/// <param name="target">The game object to listen on to receive the event.</param>
42+
/// <param name="onEvent">The action or method to call when the event occurs</param>
43+
/// <returns>A disposable that, when .Dispose is called, will unsubscribe from the
44+
/// event, essentially cancelling the call to RegisterListener.</returns>
45+
public static IDisposable RegisterListener<T>(GameObject target, Action<T> onEvent)
46+
{
47+
return DefinedEventUnit.RegisterListener<T>(target, onEvent);
48+
}
49+
50+
/// <summary>
51+
/// Registers a C# listener for an event globally. This is the scripting
52+
/// equivalent to the Global Defined Event unit. Notice the IDisposable return
53+
/// value, which allows you to end the subscription for the event (via calling
54+
/// the .Dispose() method).
55+
/// </summary>
56+
/// <typeparam name="T">The type to listen for.</typeparam>
57+
/// <param name="onEvent">The action or method to call when the event occurs</param>
58+
/// <returns>A disposable that, when .Dispose is called, will unsubscribe from the
59+
/// event, essentially cancelling the call to RegisterListener.</returns>
60+
public static IDisposable RegisterGlobalListener<T>(Action<T> onEvent)
61+
{
62+
return GlobalDefinedEventUnit.RegisterListener<T>(onEvent);
3263
}
3364
}
34-
}
65+
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Bolt.Addons.Community.DefinedEvents.Support;
22
using Bolt.Addons.Community.DefinedEvents.Support.Internal;
3+
using Bolt.Addons.Community.Utility;
34
using Ludiq;
45
using System;
56
using System.Collections.Generic;
@@ -15,7 +16,8 @@ namespace Bolt.Addons.Community.DefinedEvents.Units
1516
/// </summary>
1617
[UnitCategory("Events")]
1718
[UnitTitle("Defined Event")]
18-
public class DefinedEvent : GameObjectEventUnit<DefinedEventArgs>, IDefinedEventUnit
19+
[RenamedFrom("Bolt.Addons.Community.DefinedEvents.Units.DefinedEvent")]
20+
public class DefinedEventUnit : GameObjectEventUnit<DefinedEventArgs>, IDefinedEventUnit
1921
{
2022
const string EventName = "OnDefinedEvent";
2123

@@ -148,5 +150,16 @@ public static void Trigger(GameObject target,object eventData)
148150
var eventHook = ConstructHook(target, eventData.GetType());
149151
EventBus.Trigger(eventHook, new DefinedEventArgs(eventData));
150152
}
153+
154+
155+
156+
public static IDisposable RegisterListener<T>(GameObject target, Action<T> onEvent)
157+
{
158+
var eventHook = ConstructHook(target, typeof(T));
159+
Action<DefinedEventArgs> action = (x) => { onEvent((T)x.eventData); };
160+
EventBus.Register<DefinedEventArgs>(eventHook, action);
161+
162+
return Disposable.Create(() => { EventBus.Unregister(eventHook, action); });
163+
}
151164
}
152165
}

src/Events/Units/GlobalDefinedEvent.cs renamed to src/Events/Units/GlobalDefinedEventUnit.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Bolt.Addons.Community.DefinedEvents.Support;
22
using Bolt.Addons.Community.DefinedEvents.Support.Internal;
3+
using Bolt.Addons.Community.Utility;
34
using Ludiq;
45
using System;
56
using System.Collections.Generic;
@@ -16,7 +17,8 @@ namespace Bolt.Addons.Community.DefinedEvents.Units
1617
/// </summary>
1718
[UnitCategory("Events")]
1819
[UnitTitle("Global Defined Event")]
19-
public class GlobalDefinedEvent : EventUnit<DefinedEventArgs>, IDefinedEventUnit
20+
[RenamedFrom("Bolt.Addons.Community.DefinedEvents.Units.GlobalDefinedEvent")]
21+
public class GlobalDefinedEventUnit : EventUnit<DefinedEventArgs>, IDefinedEventUnit
2022
{
2123
const string EventName = "OnGlobalDefinedEvent";
2224

@@ -138,6 +140,16 @@ protected override void AssignArguments(Flow flow, DefinedEventArgs args)
138140
}
139141
}
140142

143+
private static EventHook ConstructHook(Type eventType)
144+
{
145+
EventHook hook;
146+
if (DefinedEventSupport.IsOptimized())
147+
hook = new EventHook(EventName, tag: eventType.GetTypeInfo().FullName);
148+
else
149+
hook = new EventHook(EventName);
150+
return hook;
151+
}
152+
141153
public static void Trigger(object eventData)
142154
{
143155
//var tag = eventData.GetType().GetTypeInfo().FullName;
@@ -146,14 +158,14 @@ public static void Trigger(object eventData)
146158
EventBus.Trigger(hook, new DefinedEventArgs(eventData));
147159
}
148160

149-
private static EventHook ConstructHook(Type eventType)
161+
162+
public static IDisposable RegisterListener<T>(Action<T> onEvent)
150163
{
151-
EventHook hook;
152-
if (DefinedEventSupport.IsOptimized())
153-
hook = new EventHook(EventName, tag: eventType.GetTypeInfo().FullName);
154-
else
155-
hook = new EventHook(EventName);
156-
return hook;
164+
var eventHook = ConstructHook(typeof(T));
165+
Action<DefinedEventArgs> action = (x) => { onEvent((T)x.eventData); };
166+
EventBus.Register<DefinedEventArgs>(eventHook, action);
167+
168+
return Disposable.Create(() => { EventBus.Unregister(eventHook, action); });
157169
}
158170
}
159171
}

src/Events/Units/TriggerDefinedEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ private ControlOutput Trigger(Flow flow)
181181
}
182182
}
183183

184-
DefinedEvent.Trigger(flow.GetValue<GameObject>(zzzEventTarget), eventInstance);
184+
DefinedEventUnit.Trigger(flow.GetValue<GameObject>(zzzEventTarget), eventInstance);
185185

186186
return exit;
187187
}

src/Events/Units/TriggerGlobalDefinedEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private ControlOutput Trigger(Flow flow)
158158
}
159159
}
160160

161-
GlobalDefinedEvent.Trigger(eventInstance);
161+
GlobalDefinedEventUnit.Trigger(eventInstance);
162162

163163
return exit;
164164
}

src/Utility/Bolt.Addons.Community.Utility.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
@@ -13,13 +13,13 @@
1313
</PropertyGroup>
1414
<Import Project="$(SolutionDir)\Bolt.targets" />
1515
<ItemGroup>
16-
<Reference Include="UnityEngine">
16+
<Reference Include="UnityEngine">
1717
<HintPath>$(SolutionDir)..\Dependencies\UnityBinaries\UnityEngine.dll</HintPath>
1818
</Reference>
1919
<Reference Include="UnityEngine.UI">
2020
<HintPath>$(SolutionDir)..\Dependencies\UnityBinaries\UnityEngine.UI.dll</HintPath>
2121
</Reference>
22-
<Reference Include="Bolt.Core.Runtime">
22+
<Reference Include="Bolt.Core.Runtime">
2323
<HintPath>$(SolutionDir)\..\Dependencies\BoltBinaries\$(BoltVersion)\.NET$(TargetFrameworkVersionNumber)\Bolt.Core.Runtime.dll</HintPath>
2424
</Reference>
2525
<Reference Include="Bolt.Flow.Runtime">
@@ -35,6 +35,7 @@
3535
<Reference Include="System.Core" />
3636
</ItemGroup>
3737
<ItemGroup>
38+
<Compile Include="Disposable.cs" />
3839
<Compile Include="Properties\AssemblyInfo.cs" />
3940
<Compile Include="UnitButton.cs" />
4041
<Compile Include="UnitButtonAttribute.cs" />

src/Utility/Disposable.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Bolt.Addons.Community.Utility
8+
{
9+
public class Disposable : IDisposable
10+
{
11+
public static IDisposable Create(Action action)
12+
{
13+
return new Disposable(action);
14+
}
15+
16+
public static IDisposable Empty = new Disposable(null);
17+
18+
Disposable(Action action)
19+
{
20+
_action = action;
21+
}
22+
23+
void IDisposable.Dispose()
24+
{
25+
if (_action != null)
26+
{
27+
_action();
28+
_action = null;
29+
}
30+
}
31+
32+
private Action _action;
33+
}
34+
}

0 commit comments

Comments
 (0)