using EasyTalk.Controller;
using EasyTalk.Nodes.Core;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace EasyTalk.Nodes.Logic
{
///
/// A node for selecting and outputting a value based on a selected index.
///
[Serializable]
public class ValueSelectorNode : ListNode, DialogueFlowNode, FunctionalNode
{
///
/// The type of value to output.
///
[SerializeField]
private ValueOutputType valueOutputType;
///
/// The string value of the index.
///
[SerializeField]
private string indexValue = "0";
///
/// The index of the value to output.
///
private int index = 0;
///
/// Creates a new ValueSelectorNode.
///
public ValueSelectorNode()
{
this.name = "VALUE SELECT";
this.nodeType = NodeType.VALUE_SELECT;
}
///
/// Gets or sets the index of the value to choose.
///
public string Index
{
get { return indexValue; }
set { indexValue = value; }
}
///
/// Gets or sets the value output type of the node.
///
public string ValueOutputType
{
get { return this.valueOutputType.ToString(); }
set { this.valueOutputType = (ValueOutputType)Enum.Parse(typeof(ValueOutputType), value); }
}
///
public bool DetermineAndStoreValue(NodeHandler nodeHandler, Dictionary nodeValues, GameObject convoOwner = null)
{
FindIndex(nodeValues);
NodeConnection inputConnection = Inputs[index + 2];
if (inputConnection.AttachedIDs.Count > 0)
{
object value = nodeValues[inputConnection.AttachedIDs[0]];
switch (valueOutputType)
{
case EasyTalk.Nodes.Logic.ValueOutputType.INT:
nodeValues.TryAdd(FindOutputOfType(InputOutputType.INT).ID, value);
break;
case EasyTalk.Nodes.Logic.ValueOutputType.FLOAT:
nodeValues.TryAdd(FindOutputOfType(InputOutputType.FLOAT).ID, value);
break;
case EasyTalk.Nodes.Logic.ValueOutputType.BOOL:
nodeValues.TryAdd(FindOutputOfType(InputOutputType.BOOL).ID, value);
break;
case EasyTalk.Nodes.Logic.ValueOutputType.STRING:
nodeValues.TryAdd(FindOutputOfType(InputOutputType.STRING).ID, value);
break;
}
}
else
{
ValueSelectorListItem item = Items[index] as ValueSelectorListItem;
object value = null;
string valueString = nodeHandler.ReplaceVariablesInString(item.Value);
switch (valueOutputType)
{
case EasyTalk.Nodes.Logic.ValueOutputType.INT:
value = Convert.ToInt32(valueString);
nodeValues.TryAdd(FindOutputOfType(InputOutputType.INT).ID, value);
break;
case EasyTalk.Nodes.Logic.ValueOutputType.FLOAT:
value = Convert.ToSingle(valueString);
nodeValues.TryAdd(FindOutputOfType(InputOutputType.FLOAT).ID, value);
break;
case EasyTalk.Nodes.Logic.ValueOutputType.BOOL:
value = Convert.ToBoolean(valueString);
nodeValues.TryAdd(FindOutputOfType(InputOutputType.BOOL).ID, value);
break;
case EasyTalk.Nodes.Logic.ValueOutputType.STRING:
//value = nodeHandler.TranslateText(valueString);
nodeValues.TryAdd(FindOutputOfType(InputOutputType.STRING).ID, valueString);
break;
}
}
return true;
}
///
/// Determines the index value to use when selecting a value to output.
///
/// A collection of node and connection IDs to their corresponding values.
public void FindIndex(Dictionary nodeValues)
{
if (Inputs[1].AttachedIDs.Count > 0)
{
int incomingId = Inputs[1].AttachedIDs[0];
if (nodeValues.ContainsKey(incomingId))
{
index = Convert.ToInt32(nodeValues[incomingId]);
}
}
else
{
int.TryParse(indexValue, out index);
}
index = Mathf.Min(index, Items.Count - 1);
}
///
public List GetDependencyOutputIDs()
{
return FindDependencyOutputIDs();
}
///
public NodeConnection GetFlowInput()
{
return FindInputOfType(InputOutputType.DIALGOUE_FLOW);
}
///
public NodeConnection GetFlowOutput()
{
return FindOutputOfType(InputOutputType.DIALGOUE_FLOW);
}
///
public bool HasDependencies()
{
return FindDependencyOutputIDs().Count > 0;
}
}
///
/// An enumeration defining the types of values which a value selector node can output.
///
public enum ValueOutputType { INT, FLOAT, BOOL, STRING }
///
/// Defines an item used in a value selector node.
///
[Serializable]
public class ValueSelectorListItem : ListItem
{
///
/// The value of the item.
///
[SerializeField]
private string value = "";
///
/// Creates a new ValueSelectorListItem.
///
public ValueSelectorListItem() : base() { }
///
/// Creates a new ValueSelectorListItem with the provided value.
///
/// The value of the item.
public ValueSelectorListItem(string value)
{
this.value = value;
}
///
/// Gets or sets the string value of the item.
///
public string Value
{
get { return value; }
set { this.value = value; }
}
}
}