ItemContainerEvents.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace KairoEngine.Inventory
  5. {
  6. public class ItemContainerEvents
  7. {
  8. public static Dictionary<string, System.Action<ItemContainer>> OnOpen = new Dictionary<string, System.Action<ItemContainer>>();
  9. public static Dictionary<string, System.Action> OnClose = new Dictionary<string, System.Action>();
  10. public static event System.Action<ItemContainer> OnUpdate;
  11. public static void Open(string title, ItemContainer itemContainer)
  12. {
  13. if(OnOpen[title] != null)
  14. {
  15. //Debug.Log("ItemContainerEvents.OnOpen has been triggered");
  16. OnOpen[title](itemContainer);
  17. }
  18. }
  19. public static void Close(string title)
  20. {
  21. if(OnClose[title] != null)
  22. {
  23. //Debug.Log("ItemContainerEvents.OnOpen has been triggered");
  24. OnClose[title]();
  25. }
  26. }
  27. public static void Update(ItemContainer itemContainer)
  28. {
  29. if(OnUpdate != null)
  30. {
  31. //Debug.Log("ItemContainerEvents.OnOpen has been triggered");
  32. OnUpdate(itemContainer);
  33. }
  34. }
  35. public static void StartListeningOnOpen(string title, System.Action<ItemContainer> listener)
  36. {
  37. System.Action<ItemContainer> action = null;
  38. if (OnOpen.TryGetValue(title, out action))
  39. {
  40. action += listener;
  41. OnOpen[title] = action;
  42. }
  43. else
  44. {
  45. action += listener;
  46. OnOpen.Add(title, action);
  47. }
  48. }
  49. public static void StartListeningOnClose(string title, System.Action listener)
  50. {
  51. System.Action action = null;
  52. if (OnClose.TryGetValue(title, out action))
  53. {
  54. action += listener;
  55. OnClose[title] = action;
  56. }
  57. else
  58. {
  59. action += listener;
  60. OnClose.Add(title, action);
  61. }
  62. }
  63. public static void StopListeningOnOpen(string title, System.Action<ItemContainer> listener)
  64. {
  65. System.Action<ItemContainer> action = null;
  66. if (OnOpen.TryGetValue(title, out action))
  67. {
  68. action -= listener;
  69. OnOpen[title] = action;
  70. }
  71. }
  72. public static void StopListeningOnClose(string title, System.Action listener)
  73. {
  74. System.Action action = null;
  75. if (OnClose.TryGetValue(title, out action))
  76. {
  77. action -= listener;
  78. OnClose[title] = action;
  79. }
  80. }
  81. }
  82. }