summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/libflash/displaylist.cc
blob: d71cfb7c6527b6f1266303543be2a084db668198 (plain)
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
////////////////////////////////////////////////////////////
// Flash Plugin and Player
// Copyright (C) 1998,1999 Olivier Debon
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
///////////////////////////////////////////////////////////////
//  Author : Olivier Debon  <odebon@club-internet.fr>
//  

#include "swf.h"

#ifdef RCSID
static char *rcsid = "$Id$";
#endif

#define PRINT 0

void deleteButton(FlashMovie *movie, DisplayListEntry *e)
{
    /* save the focus */
    if (movie->mouse_active == 0 && e->renderState == stateOver) {
        movie->lost_over = (Button *)e->character;
        movie->cur_focus = NULL;
    }

    if (e == movie->cur_focus) {
        movie->cur_focus = NULL;
    }
}

void addButton(FlashMovie *movie, DisplayListEntry *e)
{
    if (movie->mouse_active == 0 && 
        movie->cur_focus == NULL &&
        movie->lost_over == (Button *)e->character) {
        /* restore the lost focus */
        e->renderState = stateOver;
        e->oldState = stateOver;
	((Button *)e->character)->updateButtonState(e);
        movie->lost_over = NULL;
        movie->cur_focus = e;
    }
}

DisplayList::DisplayList(FlashMovie *movie)
{
	list = NULL;
        this->movie = movie;
	bbox.reset();
	isSprite = 0;
}

DisplayList::~DisplayList()
{
	clearList();
}

void
DisplayList::clearList()
{
	DisplayListEntry *del, *e;

	for(e = list; e;)
	{
		updateBoundingBox(e);
                if (e->character->isButton()) {
                    deleteButton(movie,e);
                }
		del = e;
		e = e->next;
		delete del;
	}
	list = 0;
}

DisplayListEntry *
DisplayList::getList()
{
	return list;
}

static void bbox(Rect *rect, Matrix *m, long x1, long y1)
{
    long x,y;

    x = m->getX(x1,y1);
    y = m->getY(x1,y1);
    if (x < rect->xmin) rect->xmin = x;
    if (x > rect->xmax) rect->xmax = x;
    if (y < rect->ymin) rect->ymin = y;
    if (y > rect->ymax) rect->ymax = y;
}

// Update bb to include boundary, optional reset of bb
void transformBoundingBox(Rect *bb, Matrix *matrix, Rect *boundary, int reset)
{
    if (reset) {
        bb->reset();
    }
    
    if (boundary->xmin != LONG_MAX) {
        bbox(bb, matrix, boundary->xmin, boundary->ymin);
        bbox(bb, matrix, boundary->xmax, boundary->ymin);
        bbox(bb, matrix, boundary->xmin, boundary->ymax);
        bbox(bb, matrix, boundary->xmax, boundary->ymax);
    }
}

void
DisplayList::placeObject(GraphicDevice *gd,Character *character, long depth, Matrix *matrix, Cxform *cxform, char *name)
{
	DisplayListEntry *n,*e,*prev;

	n = new DisplayListEntry;
	if (n == NULL) return;

	n->depth = depth;
	n->matrix = matrix;
	n->cxform = cxform;
	n->character = character;
	n->instanceName = name;
	n->owner = this;

#if 0
        printf("Dl %lx: placeObject: depth=%d character=%d cxform=%p\n",
               this, n->depth,n->character ? n->character->getTagId() : 0, cxform);
#endif

	if (character == 0 || matrix == 0 || cxform == 0) {
		for (e = list; e; prev = e, e = e->next) {
			if (e->depth == n->depth) {
				if (character == 0) {
					n->character = e->character;
				}
				if (matrix == 0) {
					n->matrix = e->matrix;
				}
				if (cxform == 0) {
					n->cxform = e->cxform;
				}
				break;
			}
		}
	}

	if (n->character == 0) {
		// Not found !!!    Should not happen
            //		printf("PlaceObject cannot find character at depth %ld\n", n->depth);
		delete n;
		return;
	}

	prev = 0;
	for (e = list; e; prev = e, e = e->next)
	{
		if (e->depth == n->depth) {
                        if (e->character->isButton()) {
                            deleteButton(movie, e);
                        }

			// Do update, object has moved or been resized
		        updateBoundingBox(e);

			// Replace object
                        e->depth = n->depth;
                        e->matrix = n->matrix;
                        e->cxform = n->cxform;
                        e->character = n->character;
                        /* if it is a button, we must update its state */
                        if (e->character->isButton()) {
                            movie->buttons_updated = 1;
                            addButton(movie, e);
                        }

		        updateBoundingBox(e);

                        delete n;
                        return;
		}
		if (e->depth > n->depth) break;
	}
        /* new object */

        /* button instantiation */
        if (n->character->isButton()) {
            n->renderState = stateUp;
            n->oldState = stateUp;
	    ((Button *)n->character)->updateButtonState(n);
            addButton(movie,n);
        }

	updateBoundingBox(n);

	if (prev == 0) {
		// Object comes at first place
		n->next = list;
		list = n;
	} else {
		// Insert object
		n->next = prev->next;
		prev->next = n;
	}
}


Character *
DisplayList::removeObject(GraphicDevice *gd,Character *character, long depth)
{
    DisplayListEntry *e,*prev;
    
    // List should not be empty
    if (list == 0) return 0;
    
#if 0
    printf("removeObject: depth=%d character=%d\n",
           depth,character ? character->getTagId() : 0);
#endif
    
    prev = 0;
    for (e = list; e; prev = e, e = e->next) {
        if (e->depth == depth) {
            if (prev) {
                prev->next = e->next;
            } else {
                list = e->next;
            }
            if (character == 0) {
                character = e->character;
            }
            if (e->character->isButton()) {
                deleteButton(movie, e);
            }
            if (e->character->isSprite()) {
	    	((Sprite*)e->character)->reset();
	    }
                
	    updateBoundingBox(e);

            delete e;
            return character;
        }
    }
    return 0;	// Should not happen
}

void
DisplayList::updateBoundingBox(DisplayListEntry *e)
{
	Rect	 rect;

	//rect.reset();
	e->character->getBoundingBox(&rect,e);
	transformBoundingBox(&this->bbox, e->matrix, &rect, 0);
}

int
DisplayList::updateSprites()
{
    Sprite *sprite;
    DisplayListEntry *e;
    int refresh = 0;

    for (e = this->list; e != NULL; e = e->next) {
        if (e->character->isButton() && e->buttonCharacter) {
		if (e->buttonCharacter->isSprite()) {
			Matrix mat;

			sprite = (Sprite *)e->buttonCharacter;
			refresh |= sprite->program->dl->updateSprites();
			refresh |= sprite->program->nestedMovie(this->movie->gd,this->movie->sm, e->matrix, e->cxform);
			mat = (*e->matrix) * e->buttonMatrix;
			transformBoundingBox(&this->bbox, &mat,
					&(sprite->program->dl->bbox),
					0);
		}
	}
        if (e->character->isSprite()) {
		sprite = (Sprite *)e->character;
		refresh |= sprite->program->dl->updateSprites();
		refresh |= sprite->program->nestedMovie(this->movie->gd,this->movie->sm, e->matrix, e->cxform);
		transformBoundingBox(&this->bbox, e->matrix,
				&(sprite->program->dl->bbox),
				0);
        }
    }
    return refresh;
}

/* Function can return either 0,1 or 2
   0:  Nothing match, continue
   1:  Something matches, but continue searching
   2:  Something matches, but stop searching
*/

static int exploreButtons1(Program *prg, void *opaque, 
                           ExploreButtonFunc func)
{
    DisplayListEntry *e;
    int ret, ret2 = 0;

    for(e=prg->dl->list; e != NULL; e = e->next) {
	if (e->character == NULL) continue;
        if (e->character->isButton()) {
            ret = func(opaque,prg,e);
	    if (ret == 2) return ret;	// Func asks to return at once !!!
            if (ret) ret2 = 1;
        }
        if (e->character->isSprite()) {
            ret = exploreButtons1(((Sprite *)e->character)->program,
                                  opaque,func);
	    if (ret == 2) return ret;	// Func asks to return at once !!!
            if (ret) ret2 = 1;
        }
    }
    return ret2;
}

int exploreButtons(FlashMovie *movie, void *opaque, ExploreButtonFunc func)
{
    CInputScript *script;
    int ret;

    script = movie->main;
    while (script != NULL) {
	if (script->program) {
		ret = exploreButtons1(script->program, opaque, func);
		if (ret) return ret;
	}
        script = script->next;
    }
    return 0;
}

typedef struct {
    long x,y;
    int hit;
    DisplayListEntry *bhit;
} HitTable;

static void button_hit_func(void *id, long y, long start, long end)
{
    HitTable *h = (HitTable *) id;
    if ( y == h->y && (h->x >= start && h->x < end) )
        h->hit = 1;
}

typedef struct {
    FlashMovie *movie;
    DisplayListEntry *bhit;
} ButtonHit;

static int button_hit(void *opaque, Program *prg, DisplayListEntry *e)
{
    ButtonHit *h = (ButtonHit *) opaque;
    HitTable hit_table;
    FlashMovie *movie = h->movie;
    Rect bb,boundary;
    Matrix mat;
    ButtonState save;

    hit_table.x = movie->mouse_x;
    hit_table.y = movie->mouse_y / FRAC;
    hit_table.hit = 0;
    
    // Compute the bounding box in screen coordinates
    save = e->renderState;
    e->renderState = stateHitTest;
    e->character->getBoundingBox(&boundary,e);
    e->renderState = save;
    mat = (*movie->gd->adjust) * e->renderMatrix;
    transformBoundingBox(&bb, &mat, &boundary, 1);
    // Check if mouse is within bb
    if (movie->mouse_x < bb.xmin) return 0;
    if (movie->mouse_x > bb.xmax) return 0;
    if (movie->mouse_y < bb.ymin) return 0;
    if (movie->mouse_y > bb.ymax) return 0;

    e->character->getRegion(movie->gd, &e->renderMatrix, 
                            &hit_table, button_hit_func);
                
    if (hit_table.hit) {
        h->bhit = e;
        return 1;
    } else {
        return 0;
    }
}

static int button_reset(void *opaque, Program *prg, DisplayListEntry *e)
{
    if (e->renderState != stateUp) {
	    e->owner->updateBoundingBox(e);
	    e->oldState = e->renderState;
	    e->renderState = stateUp;
	    ((Button *)e->character)->updateButtonState(e);
	    e->owner->updateBoundingBox(e);
    }
    return 0;
}

/* update the button states according to the current mouse state & return the list of actions */
void
DisplayList::updateButtons(FlashMovie *movie)
{
    DisplayListEntry *bhit;
    ButtonHit h;

    if (movie->mouse_active) {

        h.bhit = NULL;
        h.movie = movie;

        exploreButtons(movie, &h, button_hit);

        bhit = h.bhit;

        /* set every button to not hit */
        exploreButtons(movie, NULL, button_reset);

        if (bhit) {
	    ButtonState state;

            if (movie->button_pressed) {
                state = stateDown;
            } else {
                state = stateOver;
            }
	    if (state != bhit->renderState) {
		    bhit->owner->updateBoundingBox(bhit);
		    bhit->renderState = state;
		    ((Button *)bhit->character)->updateButtonState(bhit);
		    bhit->owner->updateBoundingBox(bhit);
		    movie->cur_focus = bhit;
		    if (movie->cursorOnOff)
			    movie->cursorOnOff(1,movie->cursorOnOffClientData);
	    }
        } else {
	    if (movie->cursorOnOff)
		    movie->cursorOnOff(0,movie->cursorOnOffClientData);
	}
    }
}

typedef struct {
    ActionRecord *action;	// Action to do
    Program	 *prg;		// Context program
} ButtonAction;

static int button_action(void *opaque, Program *prg, DisplayListEntry *e)
{
    ButtonAction *h = (ButtonAction *)opaque;
    static ActionRecord actionRefresh;
    static ActionRecord soundFx;
    Button *b;
    ActionRecord **paction;
    int n;

    actionRefresh.action = ActionRefresh;
    actionRefresh.next = 0;
    
    soundFx.action = ActionPlaySound;
    soundFx.next = &actionRefresh;

    b = (Button *)e->character;

    if (e->oldState != e->renderState) {
        
        paction = &actionRefresh.next;
        
        if (b->conditionList) {
            *paction = b->getActionFromTransition(e->renderState, e->oldState);
        } else if (e->renderState == stateDown) {
            /* if the button is pressed and 
               no condition list is defined*/
            *paction = b->actionRecords;
        }
        
        switch(e->renderState) {
        case stateUp:
            n = 0;
            break;
        case stateOver:
            n = 1;
            break;
        default:
            /* case stateDown: */
            n = 2;
            break;
        }
        
        if (b->sound[n]) {
            soundFx.sound = b->sound[n];
            h->action = &soundFx;
        } else {
            h->action = &actionRefresh;
        }
        
        e->oldState = e->renderState;

        h->prg = prg;
        return 2;
    }
    h->action = 0;	// Nothing to do about this
    return 0;
}

int computeActions(FlashMovie *movie, Program **prg, ActionRecord **ar)
{
    ButtonAction h;

    h.action = NULL;
    exploreButtons(movie, &h, button_action);
    if (h.action) {
    	*prg = h.prg;
	*ar = h.action;
	return 1;
    }
    return 0;
}

#define FOCUS_ZOOM       1.5
/* in pixels */
#define FOCUS_SIZE_MIN   50
#define FOCUS_TRANSLATE  15

int
DisplayList::render(GraphicDevice *gd, Matrix *render_matrix, Cxform *cxform)
{
	DisplayListEntry *e,*cur_focus;
	int sprite = 0;
	long n = 0;
        Cxform cxf,*cxf1;
	Rect bb,boundary;

        cur_focus = NULL;

	/*
	if (isSprite == 0) {
		if (this->bbox.xmin == LONG_MAX) return 0;
		gd->updateClippingRegion(&this->bbox, render_matrix);
		gd->clearCanvas();
	}
	*/

	for (e = list; e; e = e->next)
	{
#if PRINT
		printf("Character %3d @ %3d\n", e->character ? e->character->getTagId() : 0, e->depth);
#endif
		if (e->character) {
			Matrix mat;

			if (render_matrix) {
				mat = *render_matrix;
			}

			if (e->matrix) {
				mat = mat * (*e->matrix);
			}

                        /* fast clipping */
			// If object boundaries are outside current clip region give up with rendering
                        e->character->getBoundingBox(&boundary,e);
                        if (boundary.xmin != LONG_MAX) {
                            Matrix tmat;

                            tmat = (*gd->adjust) * mat;
                            transformBoundingBox(&bb, &tmat, &boundary, 1);

                            bb.xmin = bb.xmin >> FRAC_BITS;
                            bb.ymin = bb.ymin >> FRAC_BITS;
                            bb.xmax = (bb.xmax + FRAC - 1) >> FRAC_BITS;
                            bb.ymax = (bb.ymax + FRAC - 1) >> FRAC_BITS;

                            if (bb.xmin >= gd->clip_rect.xmax ||
                                bb.xmax <= gd->clip_rect.xmin ||
                                bb.ymin >= gd->clip_rect.ymax ||
                                bb.ymax <= gd->clip_rect.ymin) {
                                continue;
                            }
                        }

                        if (cxform == NULL) {
                            cxf1 = e->cxform;
                        }
			else if (e->cxform == NULL) {
                            cxf1 = cxform;
                        }
			else {
                            cxf1 = &cxf;
                            cxf.ra = cxform->ra * e->cxform->ra;
                            cxf.ga = cxform->ga * e->cxform->ga;
                            cxf.ba = cxform->ba * e->cxform->ba;
                            cxf.aa = cxform->aa * e->cxform->aa;
                            
                            cxf.rb = (long)(cxform->ra * e->cxform->rb + cxform->rb);
                            cxf.gb = (long)(cxform->ga * e->cxform->gb + cxform->gb);
                            cxf.bb = (long)(cxform->ba * e->cxform->bb + cxform->bb);
                            cxf.ab = (long)(cxform->aa * e->cxform->ab + cxform->ab);
                        }

                        if (e->character->isButton()) {
                            Button *b = (Button *) e->character;

                            e->renderMatrix = mat;

                            if (e->renderState != stateUp && movie->mouse_active == 0) {
                                cur_focus = e;
				((Button *)e->character)->updateButtonState(e);
                            }

                            if (b->execute(gd, &mat, cxf1, e->renderState)) {
				sprite = 1;
                            }
                        } else {
                            if (e->character->execute(gd, &mat, cxf1)) {
				sprite = 1;
                            }
                        }

			n++;
		}
	}

#if 0
    {
	/* display the bounding box (debug) */
	Matrix tmat;
	long x1,x2,y1,y2;
	Color white;

	white.red = 255;
	white.green = white.blue = 0;
	gd->setForegroundColor(white);

	if (render_matrix) {
		tmat = (*gd->adjust) * (*render_matrix);
	} else {
		tmat = *gd->adjust;
	}
	x1 = bbox.xmin;
	y1 = bbox.ymin;
	x2 = bbox.xmax;
	y2 = bbox.ymax;
	gd->drawLine(tmat.getX(x1,y1),tmat.getY(x1,y1),tmat.getX(x2,y1),tmat.getY(x2,y1),10*FRAC);
	gd->drawLine(tmat.getX(x2,y1),tmat.getY(x2,y1),tmat.getX(x2,y2),tmat.getY(x2,y2),10*FRAC);
	gd->drawLine(tmat.getX(x2,y2),tmat.getY(x2,y2),tmat.getX(x1,y2),tmat.getY(x1,y2),10*FRAC);
	gd->drawLine(tmat.getX(x1,y2),tmat.getY(x1,y2),tmat.getX(x1,y1),tmat.getY(x1,y1),10*FRAC);
	bbox.print();
    }
#endif
        
	// Reset clipping zone
        bbox.reset();

	return sprite;
}

void
DisplayList::getBoundary(Rect *bb)
{
	DisplayListEntry *e;
	Rect boundary;

	bb->reset();
	for (e = list; e; e = e->next)
	{
		if (e->character) {
			e->character->getBoundingBox(&boundary,e);
			transformBoundingBox(bb, e->matrix, &boundary, 0);
		}
	}
}

extern "C" {

void dump_buttons(FlashHandle flashHandle)
{
#if 0
    Rect rect;
    DisplayListEntry *e;
    FlashMovie *movie;

    movie = (FlashMovie *)flashHandle;

    for (e = movie->first_button; e; e = e->next_button) {
        computeBBox(movie,&rect,e);
        printf("button: id=%d pos=%d %d %d %d\n",
               e->character->getTagId(),
               rect.xmin, rect.ymin, rect.xmax, rect.ymax);
    }
#endif
}

}