BooleanNotifier.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace UniRx
  5. {
  6. /// <summary>
  7. /// Notify boolean flag.
  8. /// </summary>
  9. public class BooleanNotifier : IObservable<bool>
  10. {
  11. readonly Subject<bool> boolTrigger = new Subject<bool>();
  12. bool boolValue;
  13. /// <summary>Current flag value</summary>
  14. public bool Value
  15. {
  16. get { return boolValue; }
  17. set
  18. {
  19. boolValue = value;
  20. boolTrigger.OnNext(value);
  21. }
  22. }
  23. /// <summary>
  24. /// Setup initial flag.
  25. /// </summary>
  26. public BooleanNotifier(bool initialValue = false)
  27. {
  28. this.Value = initialValue;
  29. }
  30. /// <summary>
  31. /// Set and raise true if current value isn't true.
  32. /// </summary>
  33. public void TurnOn()
  34. {
  35. if (Value != true)
  36. {
  37. Value = true;
  38. }
  39. }
  40. /// <summary>
  41. /// Set and raise false if current value isn't false.
  42. /// </summary>
  43. public void TurnOff()
  44. {
  45. if (Value != false)
  46. {
  47. Value = false;
  48. }
  49. }
  50. /// <summary>
  51. /// Set and raise reverse value.
  52. /// </summary>
  53. public void SwitchValue()
  54. {
  55. Value = !Value;
  56. }
  57. /// <summary>
  58. /// Subscribe observer.
  59. /// </summary>
  60. public IDisposable Subscribe(IObserver<bool> observer)
  61. {
  62. return boolTrigger.Subscribe(observer);
  63. }
  64. }
  65. }