using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Core.GameActions { [System.Serializable, HideReferenceObjectPicker] public class StringManipulationGameAction : GameActionBase { public enum VariableType { String, Variable } public enum StringManipulation { AddToString, SubtractFromString, } public override string name { get { return $"Manipulate string"; } } public override GameActionsController controller { get => _controller; set { _controller = value; typeName = "StringManipulationGameAction"; className = this.GetType().AssemblyQualifiedName; GetCompatibleVariablenames(); } } public override string GetTypeName() => "StringManipulationGameAction"; public override string GetActionName() => "Core/String Manipulation"; [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/DebugLogGameActionIcon.png")] public VariableType variableType1 = VariableType.String; [IconFoldoutGroup("@name"), ShowIf("@variableType1 == VariableType.String")] public string value1 = "custom string"; [IconFoldoutGroup("@name"), ShowIf("@variableType1 == VariableType.Variable")] [ValueDropdown("possibleVariables", IsUniqueList = false)] public string variable1; [IconFoldoutGroup("@name")] public VariableType variableType2 = VariableType.String; [IconFoldoutGroup("@name"), ShowIf("@variableType2 == VariableType.String")] public string value2 = "custom string"; [IconFoldoutGroup("@name"), ShowIf("@variableType2 == VariableType.Variable")] [ValueDropdown("possibleVariables", IsUniqueList = false)] public string variable2; [IconFoldoutGroup("@name")] public StringManipulation operation = StringManipulation.SubtractFromString; [IconFoldoutGroup("@name"), ShowIf("@operation == StringManipulation.AddToString")] public string separator = ""; [IconFoldoutGroup("@name")] [ValueDropdown("possibleVariables", IsUniqueList = false)] public string saveToVariable; private string result = ""; private IEnumerable possibleVariables = new ValueDropdownList(); public StringManipulationGameAction(GameActionsController controller) : base(controller) { this.controller = controller; className = this.GetType().AssemblyQualifiedName; } public override void Start() { result = ""; if(variableType1 == VariableType.Variable) value1 = GetVariable(variable1); if(variableType2 == VariableType.Variable) value2 = GetVariable(variable2); switch (operation) { case StringManipulation.AddToString: result = value1 + separator + value2; break; case StringManipulation.SubtractFromString: result = value1.Replace(value2, ""); break; default: break; } SaveToVariable(saveToVariable, result); _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.data is GameActionContextString) .Select(x => new ValueDropdownItem(x.name, x.name)); } public static StringManipulationGameAction JSONToStringManipulationGameAction(string data) { return JsonUtility.FromJson(data); } private StringManipulationGameAction Duplicate(GameActionsController controller = null) { StringManipulationGameAction action = new StringManipulationGameAction(controller == null ? this.controller : controller); action.controller = controller; action.variableType1 = variableType1; action.value1 = value1; action.variable1 = variable1; action.variableType2 = variableType2; action.value2 = value2; action.variable2 = variable2; action.operation = operation; action.separator = separator; action.saveToVariable = saveToVariable; 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(""); if(value != "") return value; } } return ""; } private void SaveToVariable(string title, string data) { 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) { controller.context.variables[i].SetValue(data); break; } } } } }