1
1
package ;
2
2
3
+ import haxe .xml .Access ;
3
4
import motion .Actuate ;
5
+ import openfl .Assets ;
4
6
import openfl .display .Bitmap ;
5
7
import openfl .display .Sprite ;
6
8
import openfl .Lib ;
7
9
import openfl .display .StageDisplayState ;
8
10
import openfl .events .Event ;
9
11
import openfl .events .MouseEvent ;
10
12
import openfl .geom .Point ;
13
+ import parallax .Parallax ;
14
+ import parallax .ParallaxLayer ;
11
15
12
16
/**
13
17
* ...
14
- * @author Ludovic Bas - www.lugludum.com
18
+ * @author Ludovic Bas - www.loudoweb.fr
15
19
*/
16
20
class Main extends Sprite
17
21
{
18
-
19
- public var image : Bitmap ;
20
-
21
-
22
- var deltaZoom : Float ;
23
- var maxZoom : Float ;
24
- var minZoom : Float ;
25
- var zoom : Float ;
26
- var preZoom : Float ;
27
22
var bgPos : Point ;
28
23
var imagePivot : Point ;
24
+
25
+ var parallax : Parallax ;
26
+ var containers : Array <Sprite >;
27
+
28
+ var dragX : Float ;
29
+ var dragY : Float ;
29
30
30
31
public function new ()
31
32
{
32
33
super ();
33
34
34
- zoom = 1 ;
35
+ // parallax
36
+ var xml = Xml .parse (Assets .getText (" parallax.xml" ));
37
+ parallax = Parallax .parse (xml );
35
38
36
39
bgPos = new Point ();
37
40
imagePivot = new Point ();
38
41
42
+ containers = [];
43
+
39
44
// Assets:
40
- var data = openfl. Assets .getBitmapData (" img/background-full0000.png" );
41
- image = new Bitmap (data , null , true );
42
- addChild (image );
45
+ for (layer in parallax .layers )
46
+ {
47
+ var container = new Sprite ();
48
+ container .name = layer .id ;
49
+
50
+ container .x = layer .x ;
51
+ container .y = layer .y ;
52
+ addChild (container );
53
+ containers .push (container );
54
+ for (sprite in layer .sprites )
55
+ {
56
+ var spr = new Bitmap (Assets .getBitmapData (" img/" + sprite .img ), null , true );
57
+ spr .name = sprite .id ;
58
+ spr .x = sprite .originX ;
59
+ spr .y = sprite .originY ;
60
+ container .addChild (spr );
61
+ if (parallax .world != " " && layer .id == parallax .world )
62
+ {
63
+ parallax .width = container .width ;
64
+ parallax .height = container .height ;
65
+ parallax .world = " " ;// reset to avoid setting again with other image
66
+ }
67
+ }
68
+ }
43
69
44
- setZoomBounds ( );
70
+ trace ( parallax );
45
71
46
- imagePivot . setTo (stage .stageWidth / 2 , stage . stageHeight / 2 );
72
+ parallax . setZoomBounds (stage .stageHeight );
47
73
74
+ imagePivot .setTo (stage .stageWidth / 2 , stage .stageHeight / 2 );
48
75
49
- stage .addEventListener (MouseEvent .CLICK , onClick );
50
- stage .addEventListener (MouseEvent .MOUSE_WHEEL , onWheel );
51
- stage .addEventListener (Event .ENTER_FRAME , onUpdate );
52
- stage .addEventListener (MouseEvent .RIGHT_CLICK , onReset );
53
- stage .addEventListener (Event .RESIZE , onResize );
54
- }
55
-
56
- function setZoomBounds (): Void
57
- {
58
- // zoom bounds
59
- // to have fluid zoom
60
- minZoom = stage .stageHeight / (image .height / image .scaleY );
61
- trace (" image height" , image .height / image .scaleY , " stage" , stage .stageHeight );
62
- deltaZoom = (1 - minZoom ) / 2 ;
63
- maxZoom = 1 ;
64
- do {
65
- maxZoom + = deltaZoom ;
66
- }while (maxZoom < 1.2 );
67
76
77
+ // stage.addEventListener(MouseEvent.CLICK, onClick);
78
+ // stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
79
+ stage .addEventListener (MouseEvent .MOUSE_DOWN , onDragStart );
80
+ // stage.addEventListener(MouseEvent.RIGHT_CLICK, onReset);
81
+ // stage.addEventListener(Event.RESIZE, onResize);
68
82
}
69
83
70
84
function onResize (e : Event ): Void
71
85
{
72
- setZoomBounds ();
73
- var values = [for (i in 0 ... 5 ) i * deltaZoom + minZoom ];
86
+ // parallax.camera.width = stage.stageWidth;
87
+ // parallax.camera.height = stage.stageHeight;
88
+ parallax .setZoomBounds (stage .stageHeight );
89
+ /* var values = [for (i in 0...5) i * deltaZoom + minZoom];
74
90
trace(values);
75
91
//find closest zoom to current
76
92
var closest = 42.;
@@ -85,37 +101,51 @@ class Main extends Sprite
85
101
trace("resize", minZoom, "old", zoom, "closest", closest, maxZoom);
86
102
zoom = closest;
87
103
applyZoom();
88
- }
104
+ }*/
89
105
90
106
}
91
107
92
108
function onReset (e : MouseEvent ): Void
93
109
{
94
- zoom = 1 ;
95
- Actuate .tween (image , 0.25 , {scaleX : zoom , scaleY : zoom });
110
+ // parallax.zoom = 1;
111
+ /* for (container in containers)
112
+ {
113
+ Actuate.tween(container, 0.25, {scaleX: zoom, scaleY: zoom});
114
+ }*/
96
115
}
97
116
98
- function onUpdate (e : Event ): Void
117
+ function onDragStart (e : MouseEvent ): Void
99
118
{
100
- if (this .mouseX < 30 )
101
- {
102
- image .x + = 10 ;
103
- }else if (this .mouseX > stage .stageWidth - 30 )
104
- {
105
- image .x - = 10 ;
106
- }else if (this .mouseY < 30 )
107
- {
108
- image .y + = 10 ;
109
- }else if (this .mouseY > stage .stageHeight - 30 )
119
+ dragX = e .stageX ;
120
+ dragY = e .stageY ;
121
+ stage .addEventListener (MouseEvent .MOUSE_MOVE , onDragMove );
122
+ stage .addEventListener (MouseEvent .MOUSE_UP , onDragEnd );
123
+ stage .addEventListener (MouseEvent .RELEASE_OUTSIDE , onDragEnd );
124
+ }
125
+
126
+ function onDragEnd (e : MouseEvent ): Void
127
+ {
128
+ stage .removeEventListener (MouseEvent .MOUSE_MOVE , onDragMove );
129
+ stage .removeEventListener (MouseEvent .MOUSE_UP , onDragEnd );
130
+ stage .removeEventListener (MouseEvent .RELEASE_OUTSIDE , onDragEnd );
131
+ }
132
+
133
+ function onDragMove (e : MouseEvent ): Void
134
+ {
135
+
136
+
137
+ parallax .moveCamera (dragX - e .stageX , dragY - e .stageY );
138
+ dragX = e .stageX ;
139
+ dragY = e .stageY ;
140
+ for (i in 0 ... containers .length )
110
141
{
111
- image .y - = 10 ;
112
- }else {
113
- return ;
142
+ containers [i ].x = parallax .layers [i ].x ;
143
+ containers [i ].y = parallax .layers [i ].y ;
114
144
}
115
- bgPos .setTo (image .x , image .y );
116
- checkBounds (bgPos , stage .stageWidth , stage .stageHeight , image .width , image .height );
117
- image .x = bgPos .x ;
118
- image .y = bgPos .y ;
145
+ // bgPos.setTo(image.x, image.y);
146
+ // checkBounds(bgPos, stage.stageWidth, stage.stageHeight, image.width, image.height);
147
+ // image.x = bgPos.x;
148
+ // image.y = bgPos.y;
119
149
120
150
}
121
151
function onClick (e : MouseEvent ): Void
@@ -125,47 +155,8 @@ class Main extends Sprite
125
155
}
126
156
function onWheel (e : MouseEvent ): Void
127
157
{
128
- preZoom = zoom ;
129
- zoom + = e .delta / 100 * deltaZoom ;
130
- if (zoom > maxZoom )
131
- zoom = maxZoom ;
132
- if (zoom < minZoom )
133
- zoom = minZoom ;
134
-
135
- imagePivot = image .globalToLocal (new Point (e .stageX , e .stageY ));
136
-
137
- applyZoom ();
138
-
139
- }
140
-
141
- function applyZoom (): Void
142
- {
143
- if (preZoom == zoom )
144
- return ;
145
-
146
- var offsetX = image .x + imagePivot .x * preZoom - imagePivot .x * zoom ;
147
- var offsetY = image .y + imagePivot .y * preZoom - imagePivot .y * zoom ;
148
-
149
- bgPos .setTo (offsetX , offsetY );
150
- image .scaleX = image .scaleY = zoom ;
151
- checkBounds (bgPos , stage .stageWidth , stage .stageHeight , image .width , image .height );
152
- image .x = bgPos .x ;
153
- image .y = bgPos .y ;
154
- }
155
-
156
- function checkBounds (pt : Point , screenX : Int , screenY : Int , imageWidth : Float , imageHeight : Float ): Void {
157
- if (pt .x > 0 ){
158
- pt .x = 0 ;
159
- }else if (pt .x < screenX - imageWidth ){
160
- pt .x = screenX - imageWidth ;
161
- }
162
-
163
- if (pt .y > 0 ){
164
- pt .y = 0 ;
165
- }else if (pt .y < screenY - imageHeight ){
166
- pt .y = screenY - imageHeight ;
158
+ parallax .onZoom (e .delta / 100 );
167
159
168
- }
169
160
}
170
161
171
162
}
0 commit comments