JD2022-TU1/main/tools/legacy/ActorAnimPlayer/ActorAnimPlayer.cs

444 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
namespace ActorAnimPlayer
{
public partial class ActorAnimPlayer : Form
{
private pluginWrapper m_plugin = null;
private engineWrapper m_engine = null;
private ActorsManager m_actorsManager = null;
private bool m_connected = false;
private bool m_updateConnectionStatus = true;
private string m_animData = null;
private bool m_updateData = false;
// Messages
private const string strMsgAnimData = "AnimData";
private const string strMsgClear = "Clear";
private const string strInputValue = "InputValue";
private const string strActionValue = "ActionValue";
private const string strPauseActor = "PauseActor";
private const string strPluginName = "ActorAnimPlayer_Plugin";
private const string strPluginId = "PluginId";
public ActorAnimPlayer()
{
InitializeComponent();
m_engine = new engineWrapper("");
m_actorsManager = new ActorsManager("ActorAnimPlayer");
inputList.PropertyValueChanged += new PropertyValueChangedEventHandler(inputList_PropertyValueChanged);
actionsList.SelectedIndexChanged += new EventHandler(actionsList_SelectedIndexChanged);
pauseActor.Enabled = false;
CultureInfo tmp_ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
tmp_ci.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = tmp_ci;
timerUpdate.Start();
Thread trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
updateLayout();
}
private void updateConnectedStatus()
{
bool connected = m_connected;
if (connected)
{
blobWrapper blob = new blobWrapper();
connected = blob.sendToHost();
}
if (!connected)
{
if (m_connected)
{
m_engine.disconnect();
m_plugin = null;
}
connected = m_engine.connectToHost("127.0.0.1", 1001);
if (connected)
{
m_plugin = new pluginWrapper();
m_engine.addPlugin(strPluginName, m_plugin);
sendConnected();
}
}
if (connected != m_connected)
{
lock (this)
{
m_connected = connected;
m_updateConnectionStatus = true;
}
}
}
private void timerUpdate_Tick(object sender, EventArgs e)
{
if (m_updateConnectionStatus)
{
lock (this)
{
m_updateConnectionStatus = false;
if (m_connected)
{
Text = "ActorAnimPlayer [Connected]";
}
else
{
Text = "ActorAnimPlayer [Disconnected]";
}
}
}
if ( m_updateData )
{
lock (this)
{
m_updateData = false;
if (m_animData != null)
{
setAnimData(m_animData);
m_animData = null;
}
else
{
clearData();
}
}
}
}
private void ThreadTask()
{
CultureInfo tmp_ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();
tmp_ci.NumberFormat.NumberDecimalSeparator = ".";
System.Threading.Thread.CurrentThread.CurrentCulture = tmp_ci;
while (true)
{
updateConnectedStatus();
if (m_connected)
{
m_engine.update();
blobWrapper blob = new blobWrapper();
while (m_plugin.dispatch(blob))
{
string szMessage = "";
blob.extract(ref szMessage);
if (szMessage == strMsgAnimData)
{
receiveMsgAnimData(blob);
}
else if (szMessage == strMsgClear)
{
receiveMsgClear();
}
}
}
Thread.Sleep(100);
}
}
private void receiveMsgAnimData(blobWrapper blob)
{
string szAnimData = "";
blob.extract(ref szAnimData);
lock (this)
{
m_animData = szAnimData;
m_updateData = true;
}
}
private void receiveMsgClear()
{
lock (this)
{
m_animData = null;
m_updateData = true;
}
}
private void sendConnected()
{
blobWrapper blob = new blobWrapper();
blob.push(strPluginId);
blob.push(strPluginName);
blob.sendToHost();
}
private void clearData()
{
actionsList.Items.Clear();
inputList.SelectedObject = null;
actorName.Text = "Actor :";
pauseActor.Checked = false;
pauseActor.Enabled = false;
}
private bool readToken( string _data, ref int _index, out string _result )
{
_result = "";
if ( _index >= _data.Length )
{
return false;
}
int cursor = _data.IndexOf(",", _index);
if (_index == -1)
{
return false;
}
_result = _data.Substring(_index, cursor - _index);
_index = cursor + 1;
return true;
}
private void setAnimData(string _data)
{
int textIndex = 0;
string strActorName;
if (!readToken(_data, ref textIndex, out strActorName))
{
return;
}
actorName.Text = "Actor: " + strActorName;
readActions(_data, ref textIndex);
readInputs(_data, strActorName, ref textIndex);
pauseActor.Enabled = true;
}
private void readActions( string _data, ref int _textIndex )
{
string token;
if (!readToken(_data, ref _textIndex, out token))
{
return;
}
actionsList.BeginUpdate();
actionsList.Items.Clear();
int numActions = int.Parse(token);
for (int i = 0; i < numActions; i++)
{
if (!readToken(_data, ref _textIndex, out token))
{
break;
}
actionsList.Items.Add(token);
}
actionsList.EndUpdate();
}
private Type translateStringToType( string _type )
{
if ( _type == "0" )
{
return typeof(Decimal);
}
else if ( _type == "1" )
{
return typeof(UInt32);
}
return null;
}
private void readInputs(string _data, string _actorName, ref int _textIndex)
{
ActorsManager.ActorType actorType = m_actorsManager.findActorType(_actorName);
if (actorType == null)
{
actorType = m_actorsManager.buildNewType(_actorName);
createActorInput(_data, _textIndex, ref actorType);
}
object inputObject = actorType.createType();
string token;
if (!readToken(_data, ref _textIndex,out token))
{
return;
}
int numInputs = int.Parse(token);
string inputName;
string inputType;
string inputVal;
for (int i = 0; i < numInputs; i++)
{
if (!readToken(_data,ref _textIndex, out inputName))
{
return;
}
if (!readToken(_data, ref _textIndex, out inputType))
{
return;
}
if (!readToken(_data, ref _textIndex, out inputVal))
{
return;
}
Type attrType = translateStringToType(inputType);
actorType.setAttributeValue(inputObject, inputName, attrType, inputVal);
}
inputList.SelectedObject = inputObject;
}
private void createActorInput( string _data, int _textIndex, ref ActorsManager.ActorType _type )
{
string token;
if (!readToken(_data, ref _textIndex, out token))
{
return;
}
int numInputs = int.Parse(token);
string inputName;
string inputType;
string inputVal;
for (int i = 0; i < numInputs; i++)
{
if (!readToken(_data, ref _textIndex, out inputName))
{
return;
}
if (!readToken(_data, ref _textIndex, out inputType))
{
return;
}
if (!readToken(_data, ref _textIndex, out inputVal))
{
return;
}
Type attrType = translateStringToType(inputType);
if (attrType == typeof(Decimal))
{
// uint
_type.addProperty(inputName, typeof(Decimal), typeof(DecimalVarConverter));
}
else if (attrType == typeof(UInt32))
{
// float
_type.addProperty(inputName, typeof(UInt32), typeof(UInt32VarConverter));
}
}
}
private void updateLayout()
{
int height = pauseActor.Location.Y - actionsList.Location.Y - 10;
if ( height > 0 )
{
actionsList.Size = new Size(actionsList.Size.Width,height);
inputList.Size = new Size(inputList.Size.Width, height);
}
}
private void inputList_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
blobWrapper blob = new blobWrapper();
blob.push(strPluginName);
blob.push(strInputValue);
blob.push(e.ChangedItem.Label);
blob.push(e.ChangedItem.Value.ToString());
blob.sendToHost();
}
private void actionsList_SelectedIndexChanged(object sender, System.EventArgs e)
{
if ( actionsList.SelectedItem == null )
{
return;
}
blobWrapper blob = new blobWrapper();
blob.push(strPluginName);
blob.push(strActionValue);
blob.push(actionsList.SelectedItem.ToString());
blob.sendToHost();
}
private void pauseActor_CheckedChanged(object sender, EventArgs e)
{
blobWrapper blob = new blobWrapper();
blob.push(strPluginName);
blob.push(strPauseActor);
blob.push(pauseActor.Checked);
blob.sendToHost();
}
private void ActorAnimPlayer_SizeChanged(object sender, EventArgs e)
{
updateLayout();
}
private void ActorAnimPlayer_FormClosed(object sender, FormClosedEventArgs e)
{
if (m_engine != null)
{
m_engine.destroy();
m_engine = null;
}
}
}
}