forked from julapy/ofxFlashLite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofxFlashStage.cpp
More file actions
615 lines (480 loc) · 17.1 KB
/
Copy pathofxFlashStage.cpp
File metadata and controls
615 lines (480 loc) · 17.1 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* ofxStage.cpp
* emptyExample
*
* Created by lukasz karluk on 1/11/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include "ofxFlashStage.h"
ofxFlashStage* ofxFlashStage :: _instance = NULL;
/////////////////////////////////////////////
//
/////////////////////////////////////////////
ofxFlashStage :: ofxFlashStage ()
{
typeID = OFX_FLASH_STAGE_TYPE;
_root = new ofxFlashMovieClip();
_root->name( "root1" );
this->addChild( _root );
_stageMouseX = 0;
_stageMouseY = 0;
topMostHitDisplayObject = NULL;
topMostHitDisplayObjectPrev = NULL;
bUsingListeners = false;
bTouchMode = false;
bMouseDown = false;
bMousePressed = false;
bMouseReleased = false;
bMouseChanged = false;
mouseID = 0;
showRedrawRegions( false );
#ifdef OF_USING_POCO
ofAddListener( ofEvents.mouseMoved, this, &ofxFlashStage::mouseMoved ); //-- add mouse event handlers.
ofAddListener( ofEvents.mouseDragged, this, &ofxFlashStage::mouseDragged );
ofAddListener( ofEvents.mousePressed, this, &ofxFlashStage::mousePressed );
ofAddListener( ofEvents.mouseReleased, this, &ofxFlashStage::mouseReleased );
#endif
}
ofxFlashStage :: ~ofxFlashStage ()
{
#ifdef OF_USING_POCO
ofRemoveListener( ofEvents.mouseMoved, this, &ofxFlashStage::mouseMoved ); //-- remove mouse event handlers.
ofRemoveListener( ofEvents.mouseDragged, this, &ofxFlashStage::mouseDragged );
ofRemoveListener( ofEvents.mousePressed, this, &ofxFlashStage::mousePressed );
ofRemoveListener( ofEvents.mouseReleased, this, &ofxFlashStage::mouseReleased );
#endif
}
/////////////////////////////////////////////
// LISTENERS.
/////////////////////////////////////////////
void ofxFlashStage :: addListeners ()
{
if( bUsingListeners )
return;
bUsingListeners = true;
#ifdef OF_USING_POCO
ofAddListener( ofEvents.update, this, &ofxFlashStage::update );
ofAddListener( ofEvents.draw, this, &ofxFlashStage::draw );
#endif
}
void ofxFlashStage :: removeListeners ()
{
if( !bUsingListeners )
return;
bUsingListeners = false;
#ifdef OF_USING_POCO
ofRemoveListener( ofEvents.update, this, &ofxFlashStage::update );
ofRemoveListener( ofEvents.draw, this, &ofxFlashStage::draw );
#endif
}
/////////////////////////////////////////////
//
/////////////////////////////////////////////
void ofxFlashStage :: showRedrawRegions ( bool value )
{
bShowRedrawRegions = value;
}
void ofxFlashStage :: setTouchMode ( bool value )
{
bTouchMode = value;
}
ofxFlashMovieClip* ofxFlashStage :: root ()
{
return _root;
}
/////////////////////////////////////////////
// MOUSEX / MOUSEY.
/////////////////////////////////////////////
const int& ofxFlashStage :: mouseX ()
{
return _stageMouseX;
}
const int& ofxFlashStage :: mouseY ()
{
return _stageMouseY;
}
/////////////////////////////////////////////
// CORE.
/////////////////////////////////////////////
void ofxFlashStage :: setup ()
{
//
}
void ofxFlashStage :: update ()
{
topMostHitDisplayObjectPrev = topMostHitDisplayObject;
topMostHitDisplayObject = NULL;
updateChildrenOne( this, children );
updateMouse();
updateChildrenTwo( this, children );
bMousePressed = false; // set back to false on every frame, so its only picked up once.
bMouseReleased = false; // set back to false on every frame, so its only picked up once.
bMouseChanged = false; // set back to false on every frame, so its only picked up once.
}
void ofxFlashStage :: draw ()
{
ofEnableAlphaBlending();
drawChildren( this, children );
if( bShowRedrawRegions )
{
drawChildrenDebug( this, children );
}
ofDisableAlphaBlending();
}
/////////////////////////////////////////////
// EVENT HANDLERS.
/////////////////////////////////////////////
#ifdef OF_USING_POCO
void ofxFlashStage :: update ( ofEventArgs &e )
{
update();
}
void ofxFlashStage :: draw ( ofEventArgs &e )
{
draw();
}
#endif
/////////////////////////////////////////////
// UPDATE CHILDREN.
/////////////////////////////////////////////
void ofxFlashStage :: updateChildrenOne ( ofxFlashDisplayObject* parent, vector<ofxFlashDisplayObject*>& children )
{
parent->resetPixelBounds(); // clear pixel bounds on every loop and recalculate.
for( int i=0; i<children.size(); i++ )
{
ofxFlashDisplayObject* child;
child = children[ i ];
if( !child->visible() ) // if set to invisible, ignore it and all it's children.
continue;
//=========================================== MATRIX.
ofxFlashMatrix worldMatrix; // transform child matrix by world matrix.
worldMatrix = parent->concatenatedMatrix();
worldMatrix.concatenate( child->matrix() );
child->transform( worldMatrix );
child->_compoundAlpha = parent->_compoundAlpha * child->_alpha; // compound alpha adds up down the parent-child chain.
child->updateOnFrame();
//=========================================== FIND TOP MOST HIT DISPLAY OBJECT BY MOUSE.
if( child->hitTestPoint( _stageMouseX, _stageMouseY ) ) // check if its the top most display object hit by mouse.
{
topMostHitDisplayObject = child;
}
//=========================================== UPDATE CHILDREN.
if( canHaveChildren( child ) )
{
ofxFlashDisplayObjectContainer* container;
container = (ofxFlashDisplayObjectContainer*)child;
if( container->children.size() > 0 )
{
updateChildrenOne( child, container->children );
}
}
parent->addToPixelBounds( child->pixelBounds() ); // compound pixel bounds from children.
}
}
void ofxFlashStage :: updateMouse ()
{
bool bHitDisplayObjectChanged = false;
bHitDisplayObjectChanged = ( topMostHitDisplayObject != topMostHitDisplayObjectPrev );
if( !bHitDisplayObjectChanged && !bMousePressed && !bMouseReleased && !bMouseChanged ) // update if either of these is true, otherwise return.
{
return;
}
if( false ) // debug
{
char frame[ 10 ];
sprintf( frame, "%05d", ofGetFrameNum() );
cout << "" << endl;
cout << frame << " :: bHitDisplayObjectChanged = " << ( bHitDisplayObjectChanged ? "true" : "false" ) << endl;
cout << frame << " :: bMousePressed = " << ( bMousePressed ? "true" : "false" ) << endl;
cout << frame << " :: bMouseReleased = " << ( bMouseReleased ? "true" : "false" ) << endl;
cout << frame << " :: bMouseChanged = " << ( bMouseChanged ? "true" : "false" ) << endl;
}
//=========================================== CREATE CHILD / PARENT LINES.
ofxFlashDisplayObject* dispObj;
ofxFlashInteractiveObject* intObj;
if( bHitDisplayObjectChanged )
{
lineTopDownPrev = lineTopDown;
lineBottomUpPrev = lineBottomUp;
lineTopDown.clear();
lineBottomUp.clear();
if( topMostHitDisplayObject ) // check topMostHitDisplayObject is not null.
{
dispObj = topMostHitDisplayObject;
while( dispObj != this ) // go down through child / parent nesting until stage is reached.
{
if( isInteractiveObject( dispObj ) )
{
ofxFlashInteractiveObject* intObj;
intObj = (ofxFlashInteractiveObject*)dispObj;
lineTopDown.push_back( intObj );
lineBottomUp.insert( lineBottomUp.begin(), intObj );
}
dispObj = dispObj->parent;
}
}
}
//=========================================== RESET PREVIOUS MOUSE STATES.
if( topMostHitDisplayObjectPrev ) // if there is a previous line of display objects, clear mouse states.
{
for( int i=0; i<lineTopDownPrev.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDownPrev[ i ];
intObj->mouseOverDirty = false; // reset mouse over, dirty value used for further checks.
intObj->mouseDownDirty = false; // reset mouse down, dirty value used for further checks.
}
}
if( !topMostHitDisplayObject ) // nothing is hit. mouse must not be over any objects.
{
if( topMostHitDisplayObjectPrev ) // if there is a previous line of display objects, clear mouse states.
{
for( int i=0; i<lineTopDownPrev.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDownPrev[ i ];
intObj->mouseOver( false ); // reset mouse over.
intObj->mouseDown( false ); // reset mouse down.
}
}
return; // objects cleared, nothing more to do.
}
if( bTouchMode && bMouseReleased ) // when in touch mode, object do not return to the over state.
{
if( topMostHitDisplayObject ) // if there is a current line of display objects, clear mouse states.
{
for( int i=0; i<lineTopDown.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDown[ i ];
intObj->mouseOver( false ); // reset mouse over.
intObj->mouseDown( false ); // reset mouse down.
}
}
if( topMostHitDisplayObjectPrev ) // if there is a previous line of display objects, clear mouse states.
{
for( int i=0; i<lineTopDownPrev.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDownPrev[ i ];
intObj->mouseOver( false ); // reset mouse over.
intObj->mouseDown( false ); // reset mouse down.
}
}
return; // objects cleared, nothing more to do.
}
//=========================================== CHECK MOUSE ENABLED FLAG - TOP DOWN.
bool bMouseEnabled = false; // assume all display objects in nest have mouseEnabled set to false.
for( int i=0; i<lineTopDown.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDown[ i ];
// if bEnabled is still false, check if next display object down has mouseEnabled set to true.
// once one mouseEnable has been found, all display objects below are now considered to be enabled.
if( !bMouseEnabled )
{
bMouseEnabled = bMouseEnabled || intObj->mouseEnabled();
}
intObj->mouseOverDirty = bMouseEnabled;
}
//=========================================== CHECK MOUSE CHILDREN FLAG - BOTTOM UP.
bool bMouseChildren = true; // assume all display objects in nest have mouseChildren set to true.
for( int i=0; i<lineBottomUp.size(); i++ )
{
intObj = lineBottomUp[ i ];
bool bMouseOver;
bMouseOver = intObj->mouseOverDirty; // check if display object has already been set to mouse over with previous checks.
bMouseOver = bMouseOver && bMouseChildren; // if bMouseChildren is false from previous display object, then turn off mouse over.
intObj->mouseOverDirty = bMouseOver;
if( bMouseChildren ) // if still true, check if mouseChildren has changed to false.
{
if( canHaveChildren( intObj ) )
{
ofxFlashDisplayObjectContainer* objCont;
objCont = (ofxFlashDisplayObjectContainer*)intObj;
bMouseChildren = bMouseChildren && objCont->mouseChildren();
}
}
}
//=========================================== CHECK MOUSE DOWN.
// this part checks if there have been any changes in the over / active states in any of the objects from current and previous frame.
// if so, the mouse or finger while being pressed down has moved and has gone outside a valid region.
// this means that mouse or touch is no longer down and bMouseDown is set to false.
// the only one exception is when bMousePressed is true as we know that the mouse or touch has been pressed on the current frame.
// and therefore guarantees that it is valid, until the next frame where we check if it has moved.
if( !bMousePressed ) // mouse if pressed on current frame. no need to check if states are valid and as we know they are.
{
if( bMouseDown ) // if any of the states change in the line while the mouse is down, turn mouse down off.
{
for( int i=0; i<lineTopDown.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDown[ i ];
if( intObj->mouseOver() != intObj->mouseOverDirty ) // check for a state change. if true, mouse is no longer down.
{
bMouseDown = false;
break;
}
}
}
if( bMouseDown ) // if any of the states change in the line while the mouse is down, turn mouse down off.
{
for( int i=0; i<lineTopDownPrev.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDownPrev[ i ];
if( intObj->mouseOver() != intObj->mouseOverDirty ) // check for a state change. if true, mouse is no longer down.
{
bMouseDown = false;
break;
}
}
}
}
//=========================================== FINALISE MOUSE VALUES.
for( int i=0; i<lineTopDown.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDown[ i ];
intObj->mouseOver( intObj->mouseOverDirty );
intObj->mouseDown( intObj->mouseOverDirty && bMouseDown );
}
if( !topMostHitDisplayObjectPrev )
return;
for( int i=0; i<lineTopDownPrev.size(); i++ ) // go down through child / parent nesting until stage is reached.
{
intObj = lineTopDownPrev[ i ];
intObj->mouseOver( intObj->mouseOverDirty );
intObj->mouseDown( intObj->mouseOverDirty && bMouseDown );
}
}
void ofxFlashStage :: updateChildrenTwo ( ofxFlashDisplayObject* parent, vector<ofxFlashDisplayObject*>& children )
{
for( int i=0; i<children.size(); i++ )
{
ofxFlashDisplayObject* child;
child = children[ i ];
if( !child->visible() ) // if set to invisible, ignore it and all it's children.
continue;
if( canHaveChildren( child ) )
{
ofxFlashDisplayObjectContainer* container;
container = (ofxFlashDisplayObjectContainer*)child;
if( container->children.size() > 0 )
{
updateChildrenTwo( child, container->children );
}
}
child->update(); // last thing we do is call the update method for each child.
}
}
/////////////////////////////////////////////
// DRAW CHILDREN.
/////////////////////////////////////////////
void ofxFlashStage :: drawChildren ( ofxFlashDisplayObject* parent, vector<ofxFlashDisplayObject*>& children )
{
for( int i=0; i<children.size(); i++ )
{
ofxFlashDisplayObject* child;
child = children[ i ];
if( !child->visible() ) // if set to invisible, ignore it and all it's children.
continue;
//-- matrix transform.
bool bIdentity = true;
bIdentity = child->matrix().isIdentity();
bIdentity = false;
if( !bIdentity ) // matrix is not an identity matrix.
{ // transform using openGL.
glPushMatrix();
glMultMatrixf( child->matrix().getPtr() );
}
child->drawOnFrame();
child->draw();
if( canHaveChildren( child ) )
{
ofxFlashDisplayObjectContainer* container;
container = (ofxFlashDisplayObjectContainer*)child;
if( container->children.size() > 0 )
{
drawChildren( child, container->children );
}
}
if( !bIdentity )
{
glPopMatrix();
}
}
}
void ofxFlashStage :: drawChildrenDebug ( ofxFlashDisplayObject* parent, vector<ofxFlashDisplayObject*>& children )
{
for( int i=0; i<children.size(); i++ )
{
ofxFlashDisplayObject* child;
child = children[ i ];
if( !child->visible() ) // if set to invisible, ignore it and all it's children.
continue;
child->drawTransformedOutline();
child->drawPixelBounds();
if( canHaveChildren( child ) )
{
ofxFlashDisplayObjectContainer* container;
container = (ofxFlashDisplayObjectContainer*)child;
if( container->children.size() > 0 )
{
drawChildrenDebug( child, container->children );
}
}
}
}
/////////////////////////////////////////////
// CAN HAVE CHILDREN CHECK.
/////////////////////////////////////////////
bool ofxFlashStage :: canHaveChildren ( ofxFlashDisplayObject* displayObject )
{
bool bCanHaveChildren;
bCanHaveChildren = false;
bCanHaveChildren = bCanHaveChildren || ( displayObject->typeID == OFX_FLASH_DISPLAY_OBJECT_CONTAINER_TYPE );
bCanHaveChildren = bCanHaveChildren || ( displayObject->typeID == OFX_FLASH_SPRITE_TYPE );
bCanHaveChildren = bCanHaveChildren || ( displayObject->typeID == OFX_FLASH_MOVIE_CLIP_TYPE );
return bCanHaveChildren;
}
bool ofxFlashStage :: isInteractiveObject ( ofxFlashDisplayObject* displayObject )
{
bool bIsInteractiveObject;
bIsInteractiveObject = false;
bIsInteractiveObject = bIsInteractiveObject || ( displayObject->typeID == OFX_FLASH_DISPLAY_OBJECT_CONTAINER_TYPE ); // not sure this one needs to be here.
bIsInteractiveObject = bIsInteractiveObject || ( displayObject->typeID == OFX_FLASH_SPRITE_TYPE );
bIsInteractiveObject = bIsInteractiveObject || ( displayObject->typeID == OFX_FLASH_MOVIE_CLIP_TYPE );
return bIsInteractiveObject;
}
/////////////////////////////////////////////
// MOUSE CHANGE LISTENERS.
/////////////////////////////////////////////
void ofxFlashStage :: mouseMoved ( int x, int y, int id )
{
if( mouseID != id )
return;
_stageMouseX = x;
_stageMouseY = y;
}
void ofxFlashStage :: mouseDragged ( int x, int y, int id )
{
if( mouseID != id )
return;
_stageMouseX = x;
_stageMouseY = y;
}
void ofxFlashStage :: mousePressed ( int x, int y, int id )
{
_stageMouseX = x;
_stageMouseY = y;
bMouseDown = true;
bMousePressed = true; // this gets reset on every update loop. flag that mouse state has changed.
bMouseChanged = ( mouseID != id );
if( bMouseChanged )
{
mouseID = id;
}
}
void ofxFlashStage :: mouseReleased ( int x, int y, int id )
{
if( mouseID != id )
return;
_stageMouseX = x;
_stageMouseY = y;
bMouseDown = false;
bMouseReleased = true; // this gets reset on every update loop. flag that mouse state has changed.
}