123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using System.Collections;
- using System.Linq;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- using KairoEngine.Core.GameActions;
- namespace KairoEngine.UI.GameActions
- {
- [System.Serializable, HideReferenceObjectPicker]
- public class UiElementGameAction : GameActionBase
- {
- public enum DataType
- {
- String,
- Variable
- }
- public enum ActionType
- {
- Show,
- Hide
- }
- public override string name
- {
- get
- {
- return "Toogle UI element";
- }
- }
- public override GameActionsController controller {
- get => _controller;
- set
- {
- _controller = value;
- typeName = "UiElementGameAction";
- className = this.GetType().AssemblyQualifiedName;
- GetCompatibleVariablenames();
- }
- }
- public override string GetTypeName() => "UiElementGameAction";
- public override string GetActionName() => "UI/Toggle Ui Element";
- [FoldoutGroup("@name")] public ActionType actionType = ActionType.Show;
- [FoldoutGroup("@name")] public DataType dataType = DataType.String;
-
- [FoldoutGroup("@name"), ShowIf("@dataType == DataType.String")]
- public string uiElementName = "UI Element Name";
- [FoldoutGroup("@name"), ShowIf("@dataType == DataType.Variable")]
- [ValueDropdown("possibleVariables", IsUniqueList = false)]
- public string uiElementVariable;
- private IEnumerable possibleVariables = new ValueDropdownList<string>();
- public UiElementGameAction(GameActionsController controller) : base(controller)
- {
- this.controller = controller;
- className = this.GetType().AssemblyQualifiedName;
- }
- public override void Start()
- {
- string uiElement = "";
- if(dataType == DataType.String) uiElement = uiElementName;
- else uiElement = GetVariable(uiElementVariable);
- if(actionType == ActionType.Show) UiManager.ShowElement(uiElement);
- else UiManager.HideElement(uiElement);
- _done = true;
- _started = true;
- }
- public override void Update() { }
- public override void Restart()
- {
- _done = false;
- _started = false;
- }
- private void GetCompatibleVariablenames()
- {
- if(_controller == null) return;
- if(_controller.context == null) return;
- possibleVariables = _controller.context.variables
- .Where(x => x.GetTypeName() is "GameActionContextString")
- .Select(x => new ValueDropdownItem(x.name, x.name));
- }
- public static UiElementGameAction JSONToUiElementGameAction(string data)
- {
- return JsonUtility.FromJson<UiElementGameAction>(data);
- }
- private UiElementGameAction Duplicate(GameActionsController controller = null)
- {
- UiElementGameAction action = new UiElementGameAction(controller == null ? this.controller : controller);
- action.controller = controller;
- action.actionType = actionType;
- action.dataType = dataType;
- action.uiElementName = uiElementName;
- action.uiElementVariable = uiElementVariable;
- return action;
- }
- private string GetVariable(string title)
- {
- if(controller == null) return "";
- if(controller.context == null) return "";
- for (int i = 0; i < controller.context.variables.Count; i++)
- {
- if(controller.context.variables[i].name == title)
- {
- string value = controller.context.variables[i].GetValue<string>("");
- if(value != "") return value;
- }
- }
- return "";
- }
- }
- }
|