Skip to content

Commit eed6846

Browse files
committed
allow zoom from mouse position; don't scale depth 0 images
1 parent a0ad1cd commit eed6846

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

parallax/Parallax.hx

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package parallax;
2+
import haxe.ds.WeakMap;
23
import haxe.xml.Access;
34
using parallax.ParallaxHelper;
45

@@ -41,6 +42,9 @@ class Parallax
4142

4243
public var speed:Int;
4344

45+
public var zoomOffsetX:Null<Float>;
46+
public var zoomOffsetY:Null<Float>;
47+
4448
public function new(id:String, world:String, cameraX:Float, cameraY:Float, speed:Int = 1 )
4549
{
4650
this.id = id;
@@ -92,53 +96,58 @@ class Parallax
9296
{
9397
trace("resize", camera.minZoom, "old", camera.zoom, "closest", closest, camera.maxZoom);
9498
camera.zoom = closest;
95-
//applyZoom();
99+
//this.zoomOffsetX = zoomOffsetX;
100+
//this.zoomOffsetY = zoomOffsetY;
101+
102+
applyZoom();
96103
}
97104

98105
}
99106

100107
public function checkBounds():Void{
108+
101109
if (camera.x < 0){
102110
camera.x = 0;
103-
}else if (camera.x > width - camera.width){
104-
camera.x = width - camera.width;
111+
}else if (camera.x > (width * camera.zoom - camera.width)){
112+
camera.x = (width * camera.zoom - camera.width) ;
105113
}
106114

107115
if (camera.y < 0){
108116
camera.y = 0;
109-
}else if (camera.y > height - camera.height){
110-
camera.y = height - camera.height;
117+
}else if (camera.y > (height * camera.zoom - camera.height)){
118+
camera.y = (height * camera.zoom - camera.height);
111119

112120
}
113121
}
114122

115-
public function onZoom(delta:Float):Void {
123+
public function onZoom(delta:Float, zoomOffsetX:Null<Float> = null, zoomOffsetY:Null<Float> = null):Void {
116124

117125
camera.onZoom(delta);
118126

119-
//imagePivot = image.globalToLocal(new Point(e.stageX, e.stageY));
120-
127+
this.zoomOffsetX = zoomOffsetX;
128+
this.zoomOffsetY = zoomOffsetY;
129+
121130
applyZoom();
122131

123132
}
124133

125-
function applyZoom():Void
134+
public function applyZoom():Void
126135
{
127136
if (!camera.canApplyZoom())
128137
return;
129138

130-
//var offsetX = image.x + imagePivot.x * preZoom - imagePivot.x * zoom;
131-
//var offsetY = image.y + imagePivot.y * preZoom - imagePivot.y * zoom;
132-
133-
//bgPos.setTo(offsetX, offsetY);
134-
/*for (container in containers)
139+
if (zoomOffsetX != null)
135140
{
141+
142+
var offsetX = zoomOffsetX * camera.preZoom - zoomOffsetX * camera.zoom;
143+
var offsetY = zoomOffsetY * camera.preZoom - zoomOffsetY * camera.zoom;
136144

137-
container.scaleX = container.scaleY = parallax.zoom;
138-
}*/
139-
//checkBounds(bgPos, stage.stageWidth, stage.stageHeight);
140-
//image.x = bgPos.x;
141-
//image.y = bgPos.y;
145+
camera.x -= offsetX;
146+
camera.y -= offsetY;
147+
}
148+
149+
checkBounds();
150+
updateLayers();
142151
}
143152

144153
public static function parse(xml:Xml):Parallax

parallax/engines/OpenflHelper.hx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import parallax.ParallaxLayer;
1010
*/
1111
class OpenflHelper
1212
{
13+
/**
14+
* Helper to get width and height of main image to auto set the bounds of the parallax world.
15+
* @param parallax
16+
* @param layer
17+
* @param container
18+
*/
1319
inline public static function setWorldBounds(parallax:Parallax, layer:ParallaxLayer, container:DisplayObject ):Void
1420
{
1521
if (parallax.world != "" && layer.id == parallax.world)

sample/OpenflSample.hx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import parallax.engines.OpenflHelper;
2020
*/
2121
class OpenflSample extends Sprite
2222
{
23-
var bgPos:Point;
24-
var imagePivot:Point;
2523

2624
var parallax:Parallax;
2725
var containers:Array<Sprite>;
@@ -37,9 +35,6 @@ class OpenflSample extends Sprite
3735
var xml = Xml.parse(Assets.getText("parallax.xml"));
3836
parallax = Parallax.parse(xml);
3937

40-
bgPos = new Point();
41-
imagePivot = new Point();
42-
4338
containers = [];
4439

4540
// Assets:
@@ -63,15 +58,11 @@ class OpenflSample extends Sprite
6358
}
6459
}
6560

66-
parallax.setZoomBounds(stage.stageHeight);
67-
68-
imagePivot.setTo(stage.stageWidth / 2, stage.stageHeight / 2);
61+
parallax.setZoomBounds(stage.stageHeight);
6962

70-
71-
//stage.addEventListener(MouseEvent.CLICK, onClick);
7263
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
7364
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDragStart);
74-
//stage.addEventListener(MouseEvent.RIGHT_CLICK, onReset);
65+
stage.addEventListener(MouseEvent.MIDDLE_CLICK, onReset);
7566
stage.addEventListener(Event.RESIZE, onResize);
7667
}
7768

@@ -84,11 +75,15 @@ class OpenflSample extends Sprite
8475

8576
function onReset(e:MouseEvent):Void
8677
{
87-
//parallax.zoom = 1;
88-
/*for (container in containers)
78+
e.preventDefault();
79+
parallax.camera.zoom = 1;
80+
parallax.checkBounds();
81+
for (i in 0...containers.length)
8982
{
90-
Actuate.tween(container, 0.25, {scaleX: zoom, scaleY: zoom});
91-
}*/
83+
var container = containers[i];
84+
if(parallax.layers[i].depth != 0)
85+
container.scaleX = container.scaleY = parallax.layers[i].scale * parallax.camera.zoom;
86+
}
9287
}
9388

9489
function onDragStart(e:MouseEvent):Void
@@ -124,7 +119,18 @@ class OpenflSample extends Sprite
124119

125120
function onWheel(e:MouseEvent):Void
126121
{
127-
parallax.onZoom(e.delta / 100);
122+
parallax.onZoom(e.delta / 100, e.stageX + parallax.camera.x, e.stageY + parallax.camera.y);
123+
124+
for (i in 0...containers.length)
125+
{
126+
var container = containers[i];
127+
if (parallax.layers[i].depth != 0)
128+
{
129+
container.scaleX = container.scaleY = parallax.layers[i].scale * parallax.camera.zoom;
130+
container.x = parallax.layers[i].x;
131+
container.y = parallax.layers[i].y;
132+
}
133+
}
128134

129135
}
130136

0 commit comments

Comments
 (0)