SourceForge has been redesigned. Learn more.
Close

[r241]: / trunk / framework / OSMF / org / osmf / layout / LayoutRenderer.as  Maximize  Restore  History

Download this file

682 lines (596 with data), 18.7 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
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
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.layout
{
import __AS3__.vec.Vector;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Dictionary;
import org.osmf.metadata.MetadataNamespaces;
import org.osmf.metadata.MetadataWatcher;
CONFIG::LOGGING
{
import org.osmf.logging.Logger;
}
/**
* A layout renderer that sizes and positions its targets using the LayoutMetadata
* that it looks for on its targets.
*
* @see org.osmf.layout.LayoutMetadata
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class LayoutRenderer extends LayoutRendererBase
{
// Overrides
//
/**
* @private
*/
override protected function get usedMetadatas():Vector.<String>
{
return USED_METADATAS;
}
/**
* @private
*/
override protected function processContainerChange(oldContainer:ILayoutTarget, newContainer:ILayoutTarget):void
{
if (oldContainer)
{
containerAbsoluteWatcher.unwatch();
containerAttributesWatcher.unwatch();
}
if (newContainer)
{
containerAbsoluteWatcher
= new MetadataWatcher
( newContainer.layoutMetadata
, MetadataNamespaces.ABSOLUTE_LAYOUT_PARAMETERS
, null
, function (..._):void
{
invalidate();
}
);
containerAbsoluteWatcher.watch();
containerAttributesWatcher
= new MetadataWatcher
( newContainer.layoutMetadata
, MetadataNamespaces.LAYOUT_ATTRIBUTES
, null
, function (facet:LayoutAttributesMetadata):void
{
layoutMode = facet ? facet.layoutMode : LayoutMode.NONE
invalidate();
}
);
containerAttributesWatcher.watch();
}
invalidate();
}
/**
* @private
*/
override protected function processUpdateMediaDisplayBegin(targets:Vector.<ILayoutTarget>):void
{
lastCalculatedBounds = null;
}
/**
* @private
*/
override protected function processUpdateMediaDisplayEnd():void
{
lastCalculatedBounds = null;
}
/**
* @private
*/
override protected function processTargetAdded(target:ILayoutTarget):void
{
var attributes:LayoutAttributesMetadata = target.layoutMetadata.getValue(MetadataNamespaces.LAYOUT_ATTRIBUTES) as LayoutAttributesMetadata;
// If no layout properties are set on the target ...
var relative:RelativeLayoutMetadata = target.layoutMetadata.getValue(MetadataNamespaces.RELATIVE_LAYOUT_PARAMETERS) as RelativeLayoutMetadata;
if ( layoutMode == LayoutMode.NONE
&& relative == null
&& attributes == null
&& target.layoutMetadata.getValue(MetadataNamespaces.ABSOLUTE_LAYOUT_PARAMETERS) == null
&& target.layoutMetadata.getValue(MetadataNamespaces.ANCHOR_LAYOUT_PARAMETERS) == null
)
{
// Set target to take 100% of their container's width and height
relative = new RelativeLayoutMetadata();
relative.width = 100;
relative.height = 100;
target.layoutMetadata.addValue(MetadataNamespaces.RELATIVE_LAYOUT_PARAMETERS, relative);
// Set target to scale letter box layoutMode, centered, by default:
attributes = new LayoutAttributesMetadata();
attributes.scaleMode ||= ScaleMode.LETTERBOX;
attributes.verticalAlign ||= VerticalAlign.MIDDLE;
attributes.horizontalAlign ||= HorizontalAlign.CENTER;
target.layoutMetadata.addValue(MetadataNamespaces.LAYOUT_ATTRIBUTES, attributes);
}
// Watch the index metadata attribute for change:
//
var watcher:MetadataWatcher = new MetadataWatcher
( target.layoutMetadata
, MetadataNamespaces.LAYOUT_ATTRIBUTES
, LayoutAttributesMetadata.INDEX
, function(..._):void
{
updateTargetOrder(target);
}
);
watcher.watch();
targetMetadataWatchers[target] = watcher;
}
/**
* @private
*/
override protected function processTargetRemoved(target:ILayoutTarget):void
{
var watcher:MetadataWatcher = targetMetadataWatchers[target];
delete targetMetadataWatchers[target];
watcher.unwatch();
watcher = null;
}
/**
* @private
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
override protected function compareTargets(x:ILayoutTarget, y:ILayoutTarget):Number
{
var attributesX:LayoutAttributesMetadata
= x.layoutMetadata.getValue(MetadataNamespaces.LAYOUT_ATTRIBUTES)
as LayoutAttributesMetadata;
var attributesY:LayoutAttributesMetadata
= y.layoutMetadata.getValue(MetadataNamespaces.LAYOUT_ATTRIBUTES)
as LayoutAttributesMetadata;
var indexX:Number = attributesX ? attributesX.index : NaN;
var indexY:Number = attributesY ? attributesY.index : NaN;
if (isNaN(indexX) && isNaN(indexY))
{
return 1;
}
else
{
indexX ||= 0;
indexY ||= 0;
return indexX < indexY
? -1
: indexX > indexY
? 1
: 0;
}
}
/**
* @private
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
override protected function calculateTargetBounds(target:ILayoutTarget, availableWidth:Number, availableHeight:Number):Rectangle
{
var attributes:LayoutAttributesMetadata
= target.layoutMetadata.getValue(MetadataNamespaces.LAYOUT_ATTRIBUTES) as LayoutAttributesMetadata
|| new LayoutAttributesMetadata();
if (attributes.includeInLayout == false)
{
return new Rectangle();
}
var rect:Rectangle = new Rectangle(0, 0, target.measuredWidth, target.measuredHeight);
var absolute:AbsoluteLayoutMetadata
= target.layoutMetadata.getValue(MetadataNamespaces.ABSOLUTE_LAYOUT_PARAMETERS)
as AbsoluteLayoutMetadata;
var deltaX:Number;
var deltaXExclude:Number = 0;
var deltaY:Number;
var deltaYExclude:Number = 0;
var toDo:int = ALL;
// Next, get all absolute layout values, if available:
if (absolute)
{
if (!isNaN(absolute.x))
{
rect.x = absolute.x;
toDo ^= X;
}
if (!isNaN(absolute.y))
{
rect.y = absolute.y;
toDo ^= Y;
}
if (!isNaN(absolute.width))
{
rect.width = absolute.width;
toDo ^= WIDTH;
}
if (!isNaN(absolute.height))
{
rect.height = absolute.height;
toDo ^= HEIGHT;
}
}
// If not all position and size fieds have been set yet, then continue
// processing relative parameters:
if (toDo != 0)
{
var box:BoxAttributesMetadata;
var relative:RelativeLayoutMetadata
= target.layoutMetadata.getValue(MetadataNamespaces.RELATIVE_LAYOUT_PARAMETERS)
as RelativeLayoutMetadata;
if (relative)
{
if ((toDo & X) && !isNaN(relative.x))
{
rect.x = (availableWidth * relative.x) / 100 || 0;
toDo ^= X;
}
if ((toDo & WIDTH) && !isNaN(relative.width))
{
if (layoutMode == LayoutMode.HORIZONTAL)
{
box = container.layoutMetadata.getValue(MetadataNamespaces.BOX_LAYOUT_ATTRIBUTES) as BoxAttributesMetadata
|| new BoxAttributesMetadata();
rect.width
= ( Math.max(0, availableWidth - box.absoluteSum)
* relative.width
)
/ box.relativeSum;
}
else
{
rect.width = (availableWidth * relative.width) / 100;
}
toDo ^= WIDTH;
}
if ((toDo & Y) && !isNaN(relative.y))
{
rect.y = (availableHeight * relative.y) / 100 || 0;
toDo ^= Y;
}
if ((toDo & HEIGHT) && !isNaN(relative.height))
{
if (layoutMode == LayoutMode.VERTICAL)
{
box = container.layoutMetadata.getValue(MetadataNamespaces.BOX_LAYOUT_ATTRIBUTES) as BoxAttributesMetadata
|| new BoxAttributesMetadata();
rect.height
= ( Math.max(0, availableHeight - box.absoluteSum)
* relative.height
)
/ box.relativeSum;
}
else
{
rect.height = (availableHeight * relative.height) / 100;
}
toDo ^= HEIGHT;
}
}
}
// Last, do anchors: (doing them last is a natural order because we require
// a set width and x to do 'right', as well as a set height and y to do
// 'bottom')
if (toDo != 0)
{
var anchors:AnchorLayoutMetadata
= target.layoutMetadata.getValue(MetadataNamespaces.ANCHOR_LAYOUT_PARAMETERS)
as AnchorLayoutMetadata;
// Process the anchor parameters:
if (anchors)
{
if ((toDo & X) && !isNaN(anchors.left))
{
rect.x = anchors.left;
toDo ^= X;
}
if ((toDo & Y) && !isNaN(anchors.top))
{
rect.y = anchors.top;
toDo ^= Y;
}
if (!isNaN(anchors.right) && availableWidth)
{
if ((toDo & X) && !(toDo & WIDTH))
{
rect.x = Math.max(0, availableWidth - rect.width - anchors.right);
toDo ^= X;
}
else if ((toDo & WIDTH) && !(toDo & X))
{
rect.width = Math.max(0, availableWidth - anchors.right - rect.x);
toDo ^= WIDTH;
}
else
{
rect.x = Math.max(0, availableWidth - target.measuredWidth - anchors.right);
toDo ^= X;
}
deltaXExclude += anchors.right;
}
if (!isNaN(anchors.bottom) && availableHeight)
{
if ((toDo & Y) && !(toDo & HEIGHT))
{
rect.y = Math.max(0, availableHeight - rect.height - anchors.bottom);
toDo ^= Y;
}
else if ((toDo & HEIGHT) && !(toDo & Y))
{
rect.height = Math.max(0, availableHeight - anchors.bottom - rect.y);
toDo ^= HEIGHT;
}
else
{
rect.y = Math.max(0, availableHeight - target.measuredHeight - anchors.bottom);
toDo ^= Y;
}
deltaYExclude += anchors.bottom;
}
}
}
// Apply padding, if set. Note the bottom and right padding can only be
// applied when a height and width value are available!
var padding:PaddingLayoutMetadata
= target.layoutMetadata.getValue(MetadataNamespaces.PADDING_LAYOUT_PARAMETERS)
as PaddingLayoutMetadata;
if (padding)
{
if (!isNaN(padding.left))
{
rect.x += padding.left;
}
if (!isNaN(padding.top))
{
rect.y += padding.top;
}
if (!isNaN(padding.right) && !(toDo & WIDTH))
{
rect.width -= padding.right + (padding.left || 0);
}
if (!isNaN(padding.bottom) && !(toDo & HEIGHT))
{
rect.height -= padding.bottom + (padding.top || 0);
}
}
// Apply scaling layoutMode:
if (attributes.scaleMode)
{
if (! ( toDo & WIDTH || toDo & HEIGHT)
&& target.measuredWidth
&& target.measuredHeight
)
{
var size:Point = ScaleModeUtils.getScaledSize
( attributes.scaleMode
, rect.width
, rect.height
, target.measuredWidth
, target.measuredHeight
);
deltaX = rect.width - size.x;
deltaY = rect.height - size.y;
rect.width = size.x;
rect.height = size.y;
}
}
// Set deltas:
if (layoutMode != LayoutMode.HORIZONTAL)
{
deltaX ||= availableWidth - (rect.x || 0) - (rect.width || 0) - deltaXExclude;
}
if (layoutMode != LayoutMode.VERTICAL)
{
deltaY ||= availableHeight - (rect.y || 0) - (rect.height || 0) - deltaYExclude;
}
// Apply alignment (if there's surpluss space reported:)
if (deltaY)
{
switch (attributes.verticalAlign)
{
case null:
case VerticalAlign.TOP:
// all set.
break;
case VerticalAlign.MIDDLE:
rect.y += deltaY / 2;
break;
case VerticalAlign.BOTTOM:
rect.y += deltaY;
break;
}
}
if (deltaX)
{
switch (attributes.horizontalAlign)
{
case null:
case HorizontalAlign.LEFT:
// all set.
break;
case HorizontalAlign.CENTER:
rect.x += deltaX / 2;
break;
case HorizontalAlign.RIGHT:
rect.x += deltaX;
break;
}
}
// Apply pixel snapping:
if (attributes.snapToPixel)
{
rect.x = Math.round(rect.x);
rect.y = Math.round(rect.y);
rect.width = Math.round(rect.width);
rect.height = Math.round(rect.height);
}
if (layoutMode == LayoutMode.HORIZONTAL || layoutMode == LayoutMode.VERTICAL)
{
if (lastCalculatedBounds != null)
{
// Apply either the x or y coordinate to apply the desired boxing
// behavior:
if (layoutMode == LayoutMode.HORIZONTAL)
{
rect.x = lastCalculatedBounds.x + lastCalculatedBounds.width;
}
else // layoutMode == VERTICAL
{
rect.y = lastCalculatedBounds.y + lastCalculatedBounds.height;
}
}
lastCalculatedBounds = rect;
}
CONFIG::LOGGING
{
logger.debug
( "dimensions: {0} available: ({1}, {2}), media: ({3},{4})"
, rect
, availableWidth, availableHeight
, target.measuredWidth, target.measuredHeight
);
}
return rect;
}
/**
* @private
*/
override protected function calculateContainerSize(targets:Vector.<ILayoutTarget>):Point
{
var size:Point = new Point(NaN, NaN);
var absolute:AbsoluteLayoutMetadata
= container.layoutMetadata.getValue(MetadataNamespaces.ABSOLUTE_LAYOUT_PARAMETERS)
as AbsoluteLayoutMetadata;
if (absolute)
{
size.x = absolute.width;
size.y = absolute.height;
}
if (layoutMode != LayoutMode.NONE)
{
var boxAttributes:BoxAttributesMetadata = new BoxAttributesMetadata();
container.layoutMetadata.addValue(MetadataNamespaces.BOX_LAYOUT_ATTRIBUTES, boxAttributes);
}
if (isNaN(size.x) || isNaN(size.y) || layoutMode != LayoutMode.NONE)
{
// Iterrate over all targets, calculating their bounds, combining the results
// into a bounds rectangle:
var containerBounds:Rectangle = new Rectangle();
var targetBounds:Rectangle;
var lastBounds:Rectangle;
for each (var target:ILayoutTarget in targets)
{
if (target.layoutMetadata.includeInLayout)
{
targetBounds = calculateTargetBounds(target, size.x, size.y);
targetBounds.x ||= 0;
targetBounds.y ||= 0;
targetBounds.width ||= target.measuredWidth || 0;
targetBounds.height ||= target.measuredHeight || 0;
if (layoutMode == LayoutMode.HORIZONTAL)
{
if (!isNaN(target.layoutMetadata.percentWidth))
{
boxAttributes.relativeSum += target.layoutMetadata.percentWidth;
}
else
{
boxAttributes.absoluteSum += targetBounds.width;
}
if (lastBounds)
{
targetBounds.x = lastBounds.x + lastBounds.width;
}
lastBounds = targetBounds;
}
else if (layoutMode == LayoutMode.VERTICAL)
{
if (!isNaN(target.layoutMetadata.percentHeight))
{
boxAttributes.relativeSum += target.layoutMetadata.percentHeight;
}
else
{
boxAttributes.absoluteSum += targetBounds.height;
}
if (lastBounds)
{
targetBounds.y = lastBounds.y + lastBounds.height;
}
lastBounds = targetBounds;
}
containerBounds = containerBounds.union(targetBounds);
}
}
size.x ||= (absolute == null || isNaN(absolute.width))
? containerBounds.width
: absolute.width;
size.y ||= (absolute == null || isNaN(absolute.height))
? containerBounds.height
: absolute.height;
}
CONFIG::LOGGING
{
logger.debug
( "calculated container size ({0}, {1}) (bounds: {2})"
, size.x, size.y
, containerBounds
);
}
return size;
}
// Internals
//
private static const USED_METADATAS:Vector.<String> = new Vector.<String>(5, true);
/* static */
{
USED_METADATAS[0] = MetadataNamespaces.ABSOLUTE_LAYOUT_PARAMETERS;
USED_METADATAS[1] = MetadataNamespaces.RELATIVE_LAYOUT_PARAMETERS;
USED_METADATAS[2] = MetadataNamespaces.ANCHOR_LAYOUT_PARAMETERS;
USED_METADATAS[3] = MetadataNamespaces.PADDING_LAYOUT_PARAMETERS;
USED_METADATAS[4] = MetadataNamespaces.LAYOUT_ATTRIBUTES;
}
private static const X:int = 0x1;
private static const Y:int = 0x2;
private static const WIDTH:int = 0x4;
private static const HEIGHT:int = 0x8;
private static const POSITION:int = X + Y;
private static const DIMENSIONS:int = WIDTH + HEIGHT;
private static const ALL:int = POSITION + DIMENSIONS;
private var layoutMode:String = LayoutMode.NONE;
private var lastCalculatedBounds:Rectangle;
private var targetMetadataWatchers:Dictionary = new Dictionary();
private var containerAbsoluteWatcher:MetadataWatcher;
private var containerAttributesWatcher:MetadataWatcher;
CONFIG::LOGGING private static const logger:org.osmf.logging.Logger = org.osmf.logging.Log.getLogger("org.osmf.layout.LayoutRenderer");
}
}