AlignManager

Permet d’aligner un clip sur la scène en fonction du redimensionnement.
Gère le streching et les marges.

VERSIONS

v2.4 - Utilisation d’une seule classe. Corrections mineurs.
v2.0 - Gestion du stretching et des marges.
v1.0 - Première classe.

EXEMPLES

1
2
3
4
5
6
7
import com.romain.stage.AlignManager;
var myAlignement:AlignManager = new AlignManager(
	this.menuBar_MC,
 	0,
 	"stretch",
 	"bottom",
 	[0, 0, 0, 0]);

SOURCE

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
/**
* Permet d'aligner un clip sur la scène en fonction du redimensionnement.
* Gère le streching et les marges.
* @version: 2.4
**/
import mx.utils.Delegate;
 
class com.romainlaurent.stage.AlignManager {
	private var target_MC:MovieClip;
	private var acceleration:Number;
	private var alignHor:String;
	private var alignVert:String;
	private var margin:Array;
 
/**
* @param: target_MC:MovieClip            		MovieClip à aligner.
* @param: acceleration:Number            		Acceleration pour la reposition du clip. Par défaut à 0.
* @param: alignHor:String            			Type d'alignement horizontale du clip. Valeurs possibles : left, center, right, stretch. Par défaut à left.
* @param: alignVert:String            			Type d'alignement verticale du clip. Valeurs possibles : top, middle, bottom, stretch. Par défaut à top.
* @param: margin:Array            				Marges de positionnement du clip. Par défaut à [0, 0, 0, 0].
**/
	public function AlignManager (target_MC, acceleration, alignHor, alignVert, margin) {
		this.target_MC = target_MC;
		this.acceleration = acceleration;
		this.alignHor = alignHor;
		this.alignVert = alignVert;
		this.margin = margin;
 
		Stage.scaleMode = "noScale";
		Stage.align = "TL";
 
		var listen:Object = new Object();
		Stage.addListener(listen);
		listen.onResize = Delegate.create(this, this.displayMovieClip);
 
		this.displayMovieClip();
	}
 
	private function displayMovieClip ():Void {
		this.initMovieClip();
 
		if (this.acceleration == 0) {
			this.target_MC._x = this.getEndPosX();
			this.target_MC._y = this.getEndPosY();
		}
		else {
			this.target_MC.onEnterFrame = Delegate.create(this, this.accelerateMovieClip);
		}
	}
 
	private function initMovieClip ():Void {
		if (this.alignHor == 'stretch') {
			this.target_MC._width = Stage.width - this.getMargin(1) - this.getMargin(3);
		}
 
		if (this.alignVert == 'stretch') {
			this.target_MC._height = Stage.height - this.getMargin(0) - this.getMargin(2);
		}
	}
 
	private function getEndPosX ():Number {
		switch (this.alignHor) {
			case 'left':
				return this.getMargin(3) - this.getMargin(1);
				break;
			case 'right':
				return Math.floor(Stage.width - this.target_MC._width + this.getMargin(3) - this.getMargin(1));
				break;
			case 'center':
				return Math.floor((Stage.width / 2) - (this.target_MC._width / 2) + this.getMargin(3) - this.getMargin(1));
				break;
			case 'stretch':
				return this.getMargin(3);
				break;
		}
	}
 
	private function getEndPosY ():Number {
		switch (this.alignVert) {
			case 'top':
				return this.getMargin(0) - this.getMargin(2);
				break;
			case 'bottom':
				return Math.floor(Stage.height - this.target_MC._height + this.getMargin(0) - this.getMargin(2));
				break;
			case 'middle':
				return Math.floor((Stage.height / 2) - (this.target_MC._height / 2) + this.getMargin(0) - this.getMargin(2));
				break;
			case 'stretch':
				return this.getMargin(0);
				break;
		}
	}
 
	private function getMargin (i:Number):Number {
		var dimension:Number = ((i == 0) || (i == 2))?Stage.height:Stage.width;
		var marge:Number = (this.margin[i].indexOf('%'))?((margin[i].substring(0, margin[i].indexOf('%')) / 100) * dimension):(margin[i]);
 
		return Math.floor(marge);
	}
 
	private function accelerateMovieClip ():Void {
		var diffX = this.getEndPosX() - this.target_MC._x;
		var diffY = this.getEndPosY() - this.target_MC._y;
 
		this.target_MC._x += diffX / this.acceleration;
		this.target_MC._y += diffY / this.acceleration;
 
		var errorRate:Number = (this.acceleration - 1) / this.acceleration;
 
		if (Math.abs(diffX) < errorRate && Math.abs(diffY) < errorRate) {
			this.target_MC._x = this.getEndPosX();
			this.target_MC._y = this.getEndPosY();
			delete this.target_MC.onEnterFrame;
		}
	}
}