-
Notifications
You must be signed in to change notification settings - Fork 1
/
displayStrip.js
425 lines (342 loc) · 11.1 KB
/
displayStrip.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#pragma strict
/*
*Creation : 29/04/2013
*Author : Fabien Daoulas
*Last update : 29/05/2013
This script displays in full screen the picture on which the user tapped
number of lines : 420
*/
private var window : showingWindow;
private var videoSet : videoSettings;
// states engine
enum STATES_OF_STRIP { STATE_IN , STATE_OUT , ZOOM_IN , ZOOM_OUT};
private var states : STATES_OF_STRIP = STATES_OF_STRIP.STATE_OUT;
// plane for video / screen
private var videoScreen : GameObject;
// ratio of plane
private var ratioPlane : float;
// rect when on gui
private var rectOUT : Rect;
// when the zoom started
private var zoomStart : float;
// scale at start and on full mode
private var outScale : Vector3;
private var inScale : Vector3;
// position at start and on full mode
private var inPos : Vector3;
private var outPos : Vector3;
// position of plane along z axis
private var z_coor : float = 20;
// length of zoom
private var zoomLength : float = 2;
// events are enabled ?
private var eventEnable : boolean = false;
// path of asset
private var Path : String;
/**************** listeners ****************/
function OnEnable(){
Gesture.onShortTapE += OnTap;
Gesture.onDraggingE += OnDrag;
}
function OnDisable(){
Gesture.onShortTapE -= OnTap;
Gesture.onDraggingE -= OnDrag;
}
/**************** after events ****************/
/*
*action after event tap
*/
function OnTap( v : Vector2 ){
if( eventEnable && videoScreen != null ){
var ray : Ray = camera.ScreenPointToRay( v );
var hit : RaycastHit = new RaycastHit();
if( ( videoScreen.collider.Raycast( ray , hit , 2000.0f ) && states == STATES_OF_STRIP.STATE_OUT) || states == STATES_OF_STRIP.STATE_IN )
initZoom();
}
}
/*
*action after event drag
*/
function OnDrag( dragInfo : DragInfo ){
if( states == STATES_OF_STRIP.STATE_IN && eventEnable){
dragPlane( dragInfo );
}
}
/////////////////////////
/////initialize zoom/////
/////////////////////////
private function initZoom(){
if( states == STATES_OF_STRIP.STATE_OUT ){
// the fullscreen elements are disabled
(gameObject.GetComponent( FullScreen ) as FullScreen ).disableOthers( this );
states = STATES_OF_STRIP.ZOOM_IN;
zoomStart = Time.time;
}
if( states == STATES_OF_STRIP.STATE_IN ){
states = STATES_OF_STRIP.ZOOM_OUT;
zoomStart = Time.time;
}
}
///////////////////////
/////set the plane/////
///////////////////////
/*
*on the event OnFullScreen this method is called
*/
function InitVideoScreen( path : String , r : Rect ){
Path = path;
// init state of the state machine
states = STATES_OF_STRIP.STATE_OUT;
// load asset and test if texture exists and has the right type
try{
var texture = Resources.Load( path , Texture2D );
}
catch(err){
texture = null;
}
if(texture){
// get ratio of strip
ratioPlane = (texture as Texture).width/(texture as Texture).height;
rectOUT = optimalSize( ratioPlane , r );
createStripPlane( path , rectOUT );
// compute scale and position when plane is widen
getInParameters();
enableAll();
}
else{
throw ("No strip file found here or type of file is not texture2D");
}
}
/*
*get rect to place the plane on the screen
*and create a plane
*/
private function createStripPlane( path : String , r : Rect ){
window = gameObject.GetComponent("showingWindow") as showingWindow;
// create plane
videoScreen = new GameObject.CreatePrimitive( PrimitiveType.Plane );
videoScreen.name = "GUI_stripPlane";
// extend plane
var elmtsSize : Vector2 = window.getRealSize( Vector2( r.width , r.height ),
Vector2( r.x , r.y ),
z_coor,
camera ) ;
var size = videoScreen.renderer.bounds.size ;
videoScreen.transform.localScale = Vector3( elmtsSize.x/size.x,
1,
elmtsSize.y/size.z ) ;
// set position of plane
videoScreen.transform.position = camera.ScreenToWorldPoint( Vector3( r.x + r.width/2 , r.y + r.height/2 , z_coor ) );
videoScreen.transform.rotation = camera.transform.rotation;
videoScreen.transform.rotation *= Quaternion.AngleAxis(-90, Vector3( 1,0,0) );
videoScreen.transform.rotation *= Quaternion.AngleAxis(180, Vector3( 0,1,0) );
// set position and scale when plane is out
outPos = videoScreen.transform.position;
outScale = videoScreen.transform.localScale;
// test and set renderer
var testRenderer = videoScreen.GetComponent(Renderer);
if( !testRenderer)
videoScreen.AddComponent(Renderer);
// add texture to the plane
videoScreen.renderer.material.mainTexture = Resources.Load(Path);
}
/*
*compute rectangle to place the plane at the right place on screen
*/
function placeStripFactor( stripTop : float , stripBottom :float , stripLeft : float , stripRight : float ) : Rect{
var r : Rect;
r.height = Screen.height * (stripTop-stripBottom);
r.width = Screen.width * (stripRight-stripLeft);
r.x = Screen.width * stripLeft;
r.y = Screen.height * stripBottom;
return r;
}
/*
*calculate new rect
*ratio = width/height
*/
static function optimalSize( ratio : float , r : Rect ) : Rect {
var newR : Rect;
var ratioMax : float = r.width/r.height;
// resize plane to fit ratio
if( ratio >= 1 ){
newR.width = r.width;
newR.height = newR.width/ratio;
if( newR.height > r.height ){
newR.height = r.height;
newR.width = newR.height*ratio;
}
}
if( ratio < 1 ){
newR.height = r.height;
newR.width = newR.height*ratio;
if( newR.width > r.width ){
newR.width = r.width;
newR.height = newR.width/ratio;
}
}
// replace plane
newR.x = r.x + r.width/2 - newR.width/2;
newR.y = r.y + r.height/2 - newR.height/2;
return newR;
}
////////////////////////////////
/////get position and scale/////
////////////////////////////////
private function getInParameters(){
// position of plane when state is state_in
inPos = camera.ScreenToWorldPoint( Vector3(camera.pixelWidth/2, camera.pixelHeight/2, 20 ) ) ;
var rotation = videoScreen.transform.rotation ;
videoScreen.transform.rotation = Quaternion();
// no scale modifications with rotation up
var size = videoScreen.renderer.bounds.size ;
var elmtsSize : Vector2 = window.getRealSize( Vector2( size.x * camera.pixelHeight/size.z , camera.pixelHeight ),
Vector2( inPos.x, inPos.y ),
inPos.z, camera ) ;
// scale of plane when state is state_in
inScale = Vector3( videoScreen.transform.localScale.x * elmtsSize.x/size.x, 1, videoScreen.transform.localScale.z * elmtsSize.y/size.z ) ;
videoScreen.transform.rotation = rotation ;
}
////////////////
/////update/////
////////////////
function updateStrip(){
if( states == STATES_OF_STRIP.ZOOM_IN ){
Update_ZOOM_IN();
videoScreen.renderer.material.mainTexture = Resources.Load(Path);
}
if( states == STATES_OF_STRIP.STATE_IN )// force texture -- problem with plugin displaying the video on showingwindow while zooming in and state in
videoScreen.renderer.material.mainTexture = Resources.Load(Path);
if( states == STATES_OF_STRIP.ZOOM_OUT )
Update_ZOOM_OUT();
}
//////////////////////////////////////////////////////////////////////////////////
/////about zooming in on the plane to make the picture fit 2/3 of the screen /////
//////////////////////////////////////////////////////////////////////////////////
/*
*all that have to be done in zooming_in state
*/
function Update_ZOOM_IN(){
if( Time.time > zoomStart + zoomLength )
states = STATES_OF_STRIP.STATE_IN ;
else{
videoScreen.transform.localScale = Vector3.Slerp( outScale, inScale, (Time.time - zoomStart ) / zoomLength );
videoScreen.transform.position = Vector3.Slerp( outPos, inPos, (Time.time - zoomStart ) / zoomLength );
}
}
////////////////////////////////////////
/////about zooming out on the plane/////
////////////////////////////////////////
/*
*all that have to be done in zooming_out state
*/
function Update_ZOOM_OUT(){
if( Time.time > zoomStart + zoomLength ){
videoScreen.transform.localScale = outScale;
videoScreen.transform.position = outPos;
// the fullscreen elements are enabled
(gameObject.GetComponent( FullScreen ) as FullScreen ).enableOthers( this );
states = STATES_OF_STRIP.STATE_OUT;
}
else{
videoScreen.transform.localScale = Vector3.Slerp( inScale, outScale, (Time.time - zoomStart ) / zoomLength );
videoScreen.transform.position = Vector3.Slerp( inPos, outPos, (Time.time - zoomStart ) / zoomLength );
}
}
///////////////////////////////////////////////////////////
/////about dragging plane when movie is on full screen/////
///////////////////////////////////////////////////////////
/*
*slide the plane on drag event
*/
private function dragPlane( dI : DragInfo ){
var r : Rect = getRectPlane();
var screenPos : Vector3 = camera.WorldToScreenPoint( videoScreen.transform.position );
if( !(r.x > 0 && dI.delta.x > 0 || r.x + r.width < Screen.width && dI.delta.x < 0 )){
screenPos.x += dI.delta.x;
videoScreen.transform.position = camera.ScreenToWorldPoint( screenPos );
}
}
////////////////////////////////////////
//////additionnale method/////
////////////////////////////////////////
/*
*return rect where the plane is on screen
*/
private function getRectPlane() : Rect {
var r : Rect;
// coordinates of the point on bottom left of the plane - world coordinate
var BL : Vector3 = Vector3( videoScreen.transform.position.x - videoScreen.renderer.bounds.size.x/2 ,
videoScreen.transform.position.y ,
videoScreen.transform.position.z - videoScreen.renderer.bounds.size.z/2 );
// coordinates of the point on bottom right of the plane - world coordinate
var TR : Vector3 = Vector3( videoScreen.transform.position.x + videoScreen.renderer.bounds.size.x/2 ,
videoScreen.transform.position.y ,
videoScreen.transform.position.z + videoScreen.renderer.bounds.size.z/2 );
// into screen coordinates
var screenBL : Vector3 = camera.WorldToScreenPoint(BL);
var screenTR : Vector3 = camera.WorldToScreenPoint(TR);
// build rect descripting where is the plane into screen coordinates
r = Rect( screenBL.x , Screen.height - screenTR.y , -screenBL.x + screenTR.x , -screenBL.y + screenTR.y );
return r;
}
////////////////////////////////////////////////
/////destruct when leaving full screen mode/////
////////////////////////////////////////////////
function destructStrip(){
disableAll();
if(videoScreen)
Destroy( videoScreen );
}
/*******************************************************
**** Cacher / desactiver les evennements de l'objet ****
********************************************************/
/*
* Affiche l'objet et active les evenements
*/
public function enableAll() {
show() ;
enableEvents() ;
}
/*
* Cache l'objet et desactive les evenements
*/
public function disableAll() {
hide() ;
disableEvents() ;
}
/*
* Active les evenements
*/
public function enableEvents() {
eventEnable = true ;
}
/*
* Desactive les evenements
*/
public function disableEvents() {
eventEnable = false ;
}
/*
* Affiche l'objet
*/
public function show() {
if(videoScreen)
videoScreen.renderer.enabled = true ;
}
/*
* Cache l'objet
*/
public function hide() {
if(videoScreen)
videoScreen.renderer.enabled = false ;
}
/*
* Getters
*/
public function areEventEnabled() : boolean {
return eventEnable ;
}
public function isHidden() : boolean {
return !(videoScreen.renderer.enabled) ;
}