InspectorScript.cs
4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//Log Dir http://docs.unity3d.com/Manual/LogFiles.html
/*---------------------------------------------------------
AnimationState-speed Doc
http://docs.unity3d.com/ScriptReference/AnimationState-speed.html
-----------------------------------------------------------*/
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Threading;
public class InspectorScript : MonoBehaviour {
// 1 is the normal animation speed, i.e, unity reproduces it without changing the speed
// 1.5 means +50% speed. It was the most appropriate speed as reported by deaf testers
// Please, se speed method of AnimationState class in unity docs in link above
const float PLAYER_DEFAULT_SPEED = 1.5f;
public Boolean isCaptionsActive = true;
string alfabeto = "0123456789,ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string aniName = "";
AnimationClip aniClip;
AnimationClip defaultClip;
GameObject ICARO;
Animation COMPONENT_ANIMATION;
Server server;
Semaphore serverSemaphore;
Thread t, tPlayer;
Queue<String> glosaQueue;
Queue<SubtitleInfo> subtitleQueue;
bool finalizeFlag;
Time time;
float playerSpeed;
string[] strArg;
void Start(){
strArg = Environment.GetCommandLineArgs();
if(strArg.Length == 4)
playerSpeed = float.Parse(strArg[3]);
serverSemaphore = new Semaphore (0, 1);
glosaQueue = new Queue<String>();
subtitleQueue = new Queue<SubtitleInfo>();
finalizeFlag = false;
server = new Server (serverSemaphore, this);
t = new Thread(new ThreadStart(server.startServer));
t.Start ();
tPlayer = new Thread(new ThreadStart(playerRun));
serverSemaphore.WaitOne (); // Waits until connection with client is established in server.startServer() method
ICARO = GameObject.FindGameObjectWithTag("avatar");
COMPONENT_ANIMATION = ICARO.GetComponent<Animation>();
addAlpha ();
sendToPlayer ("");
tPlayer.Start();
} // Start
void addAlpha( ){
foreach( char letter in alfabeto ){
aniClip = Resources.Load<AnimationClip> ("ANIMS/alpha/"+letter);
if( aniClip )
COMPONENT_ANIMATION.AddClip(aniClip, ""+letter);
else
Debug.Log("Anim "+aniName+" not found");
}
}//addAlpha
public void setFinalizeFlag(bool b){
finalizeFlag = b;
}
public void addGlosaQueue(String str){
glosaQueue.Enqueue (str);
}
public void addSubtitleQueue(SubtitleInfo si){
subtitleQueue.Enqueue (si);
}
// Gets glosa from queue to animate them
void playerRun(){
do {
if(subtitleQueue.Count > 0){
long pts = subtitleQueue.Peek().getPts();
if (Time.time > (pts/1000)){ // em segundos
String glosa = subtitleQueue.Dequeue().getGlosa ();
sendToPlayer (glosa);
}
}
try{
Thread.Sleep(100); // waits a little while
}
catch(ThreadInterruptedException e){
Console.WriteLine(e.Source);
}
if(finalizeFlag) // Core told to finalize
if(subtitleQueue.Count == 0) // if there is no glosa to animate
break;
} while(true);
while (COMPONENT_ANIMATION.isPlaying) {
try{
Thread.Sleep(500);
}
catch(ThreadInterruptedException e){
Console.WriteLine(e.Source);
}
}
server.sendFinalizeToCore (); // all frames saved
server.closeConnections ();
Application.Quit ();
} // playerRun
void sendToPlayer(String glosa){
String[] stringPos;
glosa +=" _default";
glosa = glosa.ToUpper ();
stringPos = glosa.Split(' ');
// Looks for animation
foreach( string aniName in stringPos ){
aniClip = Resources.Load<AnimationClip>("ANIMS/anims/"+aniName);
if( aniClip ){
COMPONENT_ANIMATION.AddClip(aniClip, aniName);
COMPONENT_ANIMATION.CrossFadeQueued( aniName, 0.6F, QueueMode.CompleteOthers );//0.4
}
else{ // Spells, if not found
foreach(char letter in aniName)
COMPONENT_ANIMATION.CrossFadeQueued(""+letter, 0.6F, QueueMode.CompleteOthers);
}
}
foreach(AnimationState animState in COMPONENT_ANIMATION){
// Expects to receive this speed limitation [ 0 < speed <=1 ]
if(playerSpeed > 0 && playerSpeed <= 1)
animState.speed = 1 + playerSpeed; // explained on PLAYER_DEFAULT_SPEED declaration
else // uses the default speed;
animState.speed = PLAYER_DEFAULT_SPEED;
}
} // sendToPlayer
} // InspectorIscript