using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Unity.VisualScripting;
using UnityEngine;
using XLua;
namespace LuaFormula
{
///
/// Lua言語版Formulaノード
///
[UnitTitle("Lua Formula")]
[UnitShortTitle("Lua Formula")]
[UnitCategory("LuaFormula")]
public class LuaFormula : Unit, IMultiInputUnit
{
///
/// 実行するLuaコードを入力します。
///
/// 入力データポートにつなげたノードのデータは、
/// Lua上ではAにつなげたものはグローバル変数A Bにつなげたものはグローバル変数Bに格納されます。
///
[Inspectable, InspectorTextArea, UnitHeaderInspectable]
public string LuaCode { get; private set; }
///
/// Luaコード実行の結果、エラーや例外が発生しなかった場合はTrueになります。
///
[DoNotSerialize]
public ValueOutput Success { get; private set; }
///
/// Luaコード実行の結果、エラーや例外が発生した場合に例外の文字列が格納されます。
/// 発生しなかった場合は空文字列になります。
///
[DoNotSerialize]
public ValueOutput ErrorMessage { get; private set; }
///
/// Luaコード内でグローバル変数Rに代入した値が格納されます。
/// ただし、res変数内にテーブルを代入するとLuaTable型が格納され、
/// そのままではVisual Scripting内でデータを扱えないので注意してください。
///
[DoNotSerialize]
public ValueOutput Result { get; private set; }
///
/// 入力データポートに接続したデータを次のLuaFormulaノードでも使えるようにするかどうか設定します。
/// チェックをつけると入力データポート関係の変数A~Iの内容が終了後も保持されます。
///
/// ※Luaコード内でA~Iの変数の内容を変更した場合はその変更も保持されます。
/// ※変数を保持していても次のLuaFormulaノードで入力データポートにノードを接続した場合は接続したノードデータに上書きされます。
///
[Serialize]
[Inspectable(order = int.MaxValue)]
[InspectorExpandTooltip]
public bool cacheArguments { get; set; }
///
/// 入力データポートの数を指定できます。
/// 接続したノードのデータはLua環境にA~Iのグローバル変数として格納されます。
/// 0~9の間で設定できます。
///
[DoNotSerialize]
[Inspectable, UnitHeaderInspectable("Inputs")]
public virtual int inputCount
{
get
{
return _inputCount;
}
set
{
_inputCount = Mathf.Clamp(value, minInputCount, maxInputCount);
}
}
[DoNotSerialize]
public ReadOnlyCollection multiInputs { get; protected set; }
[SerializeAs(nameof(inputCount))]
private int _inputCount = 0;
[DoNotSerialize]
protected virtual int maxInputCount => 9;
[DoNotSerialize]
protected virtual int minInputCount => 0;
[DoNotSerialize, PortLabelHidden]
public ControlInput InputTrigger { get; private set; }
[DoNotSerialize, PortLabelHidden]
public ControlOutput OutputTrigger { get; private set; }
private bool _isSuccess;
private string _errorMessage;
private object _resultValue;
protected override void Definition()
{
var mi = new List();
multiInputs = mi.AsReadOnly();
for (var i = 0; i < inputCount; i++)
{
mi.Add(ValueInput