Files
Continentis/Assets/Plugins/UniRx/Scripts/Notifiers/BooleanNotifier.cs

66 lines
1.5 KiB
C#
Raw Normal View History

2026-03-20 11:56:50 -04:00
using System;
2025-10-03 00:02:43 -04:00
namespace UniRx
{
/// <summary>
2026-03-20 11:56:50 -04:00
/// Notify boolean flag.
2025-10-03 00:02:43 -04:00
/// </summary>
public class BooleanNotifier : IObservable<bool>
{
2026-03-20 11:56:50 -04:00
private readonly Subject<bool> boolTrigger = new();
private bool boolValue;
/// <summary>
/// Setup initial flag.
/// </summary>
public BooleanNotifier(bool initialValue = false)
{
Value = initialValue;
}
2025-10-03 00:02:43 -04:00
/// <summary>Current flag value</summary>
public bool Value
{
2026-03-20 11:56:50 -04:00
get => boolValue;
2025-10-03 00:02:43 -04:00
set
{
boolValue = value;
boolTrigger.OnNext(value);
}
}
2026-03-20 11:56:50 -04:00
2025-10-03 00:02:43 -04:00
/// <summary>
2026-03-20 11:56:50 -04:00
/// Subscribe observer.
2025-10-03 00:02:43 -04:00
/// </summary>
2026-03-20 11:56:50 -04:00
public IDisposable Subscribe(IObserver<bool> observer)
2025-10-03 00:02:43 -04:00
{
2026-03-20 11:56:50 -04:00
return boolTrigger.Subscribe(observer);
2025-10-03 00:02:43 -04:00
}
/// <summary>
2026-03-20 11:56:50 -04:00
/// Set and raise true if current value isn't true.
2025-10-03 00:02:43 -04:00
/// </summary>
public void TurnOn()
{
2026-03-20 11:56:50 -04:00
if (!Value) Value = true;
2025-10-03 00:02:43 -04:00
}
/// <summary>
2026-03-20 11:56:50 -04:00
/// Set and raise false if current value isn't false.
2025-10-03 00:02:43 -04:00
/// </summary>
public void TurnOff()
{
2026-03-20 11:56:50 -04:00
if (Value) Value = false;
2025-10-03 00:02:43 -04:00
}
/// <summary>
2026-03-20 11:56:50 -04:00
/// Set and raise reverse value.
2025-10-03 00:02:43 -04:00
/// </summary>
public void SwitchValue()
{
Value = !Value;
}
}
}