UiElementGameAction.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Collections;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using KairoEngine.Core.GameActions;
  8. namespace KairoEngine.UI.GameActions
  9. {
  10. [System.Serializable, HideReferenceObjectPicker]
  11. public class UiElementGameAction : GameActionBase
  12. {
  13. public enum DataType
  14. {
  15. String,
  16. Variable
  17. }
  18. public enum ActionType
  19. {
  20. Show,
  21. Hide
  22. }
  23. public override string name
  24. {
  25. get
  26. {
  27. return "Toogle UI element";
  28. }
  29. }
  30. public override GameActionsController controller {
  31. get => _controller;
  32. set
  33. {
  34. _controller = value;
  35. typeName = "UiElementGameAction";
  36. className = this.GetType().AssemblyQualifiedName;
  37. GetCompatibleVariablenames();
  38. }
  39. }
  40. public override string GetTypeName() => "UiElementGameAction";
  41. public override string GetActionName() => "UI/Toggle Ui Element";
  42. [FoldoutGroup("@name")] public ActionType actionType = ActionType.Show;
  43. [FoldoutGroup("@name")] public DataType dataType = DataType.String;
  44. [FoldoutGroup("@name"), ShowIf("@dataType == DataType.String")]
  45. public string uiElementName = "UI Element Name";
  46. [FoldoutGroup("@name"), ShowIf("@dataType == DataType.Variable")]
  47. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  48. public string uiElementVariable;
  49. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  50. public UiElementGameAction(GameActionsController controller) : base(controller)
  51. {
  52. this.controller = controller;
  53. className = this.GetType().AssemblyQualifiedName;
  54. }
  55. public override void Start()
  56. {
  57. string uiElement = "";
  58. if(dataType == DataType.String) uiElement = uiElementName;
  59. else uiElement = GetVariable(uiElementVariable);
  60. if(actionType == ActionType.Show) UiManager.ShowElement(uiElement);
  61. else UiManager.HideElement(uiElement);
  62. _done = true;
  63. _started = true;
  64. }
  65. public override void Update() { }
  66. public override void Restart()
  67. {
  68. _done = false;
  69. _started = false;
  70. }
  71. private void GetCompatibleVariablenames()
  72. {
  73. if(_controller == null) return;
  74. if(_controller.context == null) return;
  75. possibleVariables = _controller.context.variables
  76. .Where(x => x.GetTypeName() is "GameActionContextString")
  77. .Select(x => new ValueDropdownItem(x.name, x.name));
  78. }
  79. public static UiElementGameAction JSONToUiElementGameAction(string data)
  80. {
  81. return JsonUtility.FromJson<UiElementGameAction>(data);
  82. }
  83. private UiElementGameAction Duplicate(GameActionsController controller = null)
  84. {
  85. UiElementGameAction action = new UiElementGameAction(controller == null ? this.controller : controller);
  86. action.controller = controller;
  87. action.actionType = actionType;
  88. action.dataType = dataType;
  89. action.uiElementName = uiElementName;
  90. action.uiElementVariable = uiElementVariable;
  91. return action;
  92. }
  93. private string GetVariable(string title)
  94. {
  95. if(controller == null) return "";
  96. if(controller.context == null) return "";
  97. for (int i = 0; i < controller.context.variables.Count; i++)
  98. {
  99. if(controller.context.variables[i].name == title)
  100. {
  101. string value = controller.context.variables[i].GetValue<string>("");
  102. if(value != "") return value;
  103. }
  104. }
  105. return "";
  106. }
  107. }
  108. }