|
| 1 | +package; |
| 2 | + |
| 3 | +import motion.Actuate; |
| 4 | +import openfl.display.Bitmap; |
| 5 | +import openfl.display.Sprite; |
| 6 | +import openfl.Lib; |
| 7 | +import openfl.display.StageDisplayState; |
| 8 | +import openfl.events.Event; |
| 9 | +import openfl.events.MouseEvent; |
| 10 | +import openfl.geom.Point; |
| 11 | + |
| 12 | +/** |
| 13 | + * ... |
| 14 | + * @author Ludovic Bas - www.lugludum.com |
| 15 | + */ |
| 16 | +class Main extends Sprite |
| 17 | +{ |
| 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 bgPos:Point; |
| 27 | + |
| 28 | + public function new() |
| 29 | + { |
| 30 | + super(); |
| 31 | + |
| 32 | + zoom = 1; |
| 33 | + |
| 34 | + //stage.displayState = StageDisplayState.FULL_SCREEN; |
| 35 | + |
| 36 | + |
| 37 | + // Assets: |
| 38 | + var data = openfl.Assets.getBitmapData("img/background-full0000.png"); |
| 39 | + image = new Bitmap(data, null, true); |
| 40 | + addChild(image); |
| 41 | + |
| 42 | + setZoomBounds(); |
| 43 | + |
| 44 | + bgPos = new Point(); |
| 45 | + |
| 46 | + stage.addEventListener(MouseEvent.CLICK, onClick); |
| 47 | + stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel); |
| 48 | + stage.addEventListener(Event.ENTER_FRAME, onUpdate); |
| 49 | + stage.addEventListener(MouseEvent.RIGHT_CLICK, onReset); |
| 50 | + stage.addEventListener(Event.RESIZE, onResize); |
| 51 | + } |
| 52 | + |
| 53 | + function setZoomBounds():Void |
| 54 | + { |
| 55 | + //zoom bounds |
| 56 | + //to have fluid zoom |
| 57 | + minZoom = stage.stageHeight / image.height; |
| 58 | + deltaZoom = (1 - minZoom) / 2; |
| 59 | + maxZoom = 1; |
| 60 | + do { |
| 61 | + maxZoom += deltaZoom; |
| 62 | + }while (maxZoom < 1.2); |
| 63 | + |
| 64 | + trace(minZoom, zoom, maxZoom); |
| 65 | + } |
| 66 | + |
| 67 | + function onResize(e:Event):Void |
| 68 | + { |
| 69 | + trace("resize"); |
| 70 | + setZoomBounds(); |
| 71 | + //find closest zoom to current |
| 72 | + var closest = minZoom - deltaZoom; |
| 73 | + var temp = closest; |
| 74 | + var diff = 42.; |
| 75 | + do { |
| 76 | + temp += deltaZoom; |
| 77 | + if (zoom - temp < diff) |
| 78 | + { |
| 79 | + diff = zoom - temp; |
| 80 | + closest = temp; |
| 81 | + } |
| 82 | + }while (closest < maxZoom && diff != 0); |
| 83 | + |
| 84 | + if (closest != zoom) |
| 85 | + { |
| 86 | + zoom = closest; |
| 87 | + //apply zoom TODO |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + function onReset(e:MouseEvent):Void |
| 92 | + { |
| 93 | + zoom = 1; |
| 94 | + Actuate.tween(image, 0.25, {scaleX: zoom, scaleY: zoom}); |
| 95 | + } |
| 96 | + |
| 97 | + function onUpdate(e:Event):Void |
| 98 | + { |
| 99 | + if (this.mouseX < 30) |
| 100 | + { |
| 101 | + image.x += 10; |
| 102 | + }else if (this.mouseX > stage.stageWidth - 30) |
| 103 | + { |
| 104 | + image.x -= 10; |
| 105 | + }else if (this.mouseY < 30) |
| 106 | + { |
| 107 | + image.y += 10; |
| 108 | + }else if (this.mouseY > stage.stageHeight - 30) |
| 109 | + { |
| 110 | + image.y -= 10; |
| 111 | + }else{ |
| 112 | + return; |
| 113 | + } |
| 114 | + bgPos.setTo(image.x, image.y); |
| 115 | + checkBounds(bgPos, stage.stageWidth, stage.stageHeight, image.width, image.height); |
| 116 | + image.x = bgPos.x; |
| 117 | + image.y = bgPos.y; |
| 118 | + |
| 119 | + } |
| 120 | + function onClick(e:MouseEvent):Void |
| 121 | + { |
| 122 | + stage.displayState = StageDisplayState.FULL_SCREEN; |
| 123 | + |
| 124 | + } |
| 125 | + function onWheel(e:MouseEvent):Void |
| 126 | + { |
| 127 | + var preZoom = zoom; |
| 128 | + zoom += e.delta/100 * deltaZoom; |
| 129 | + if (zoom > maxZoom) |
| 130 | + zoom = maxZoom; |
| 131 | + if (zoom < minZoom) |
| 132 | + zoom = minZoom; |
| 133 | + |
| 134 | + if (preZoom == zoom) |
| 135 | + return; |
| 136 | + |
| 137 | + var imagePivot = image.globalToLocal(new Point(e.stageX, e.stageY)); |
| 138 | + |
| 139 | + var offsetX = image.x + imagePivot.x * preZoom - imagePivot.x * zoom; |
| 140 | + var offsetY = image.y + imagePivot.y * preZoom - imagePivot.y * zoom; |
| 141 | + |
| 142 | + bgPos.setTo(offsetX, offsetY); |
| 143 | + image.scaleX = image.scaleY = zoom; |
| 144 | + checkBounds(bgPos, stage.stageWidth, stage.stageHeight, image.width, image.height); |
| 145 | + image.x = bgPos.x; |
| 146 | + image.y = bgPos.y; |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + function checkBounds(pt:Point, screenX:Int, screenY:Int, imageWidth:Float, imageHeight:Float):Void{ |
| 151 | + if (pt.x > 0){ |
| 152 | + pt.x = 0; |
| 153 | + }else if (pt.x < screenX - imageWidth){ |
| 154 | + pt.x = screenX - imageWidth; |
| 155 | + } |
| 156 | + |
| 157 | + if (pt.y > 0){ |
| 158 | + pt.y = 0; |
| 159 | + }else if (pt.y < screenY - imageHeight){ |
| 160 | + pt.y = screenY - imageHeight; |
| 161 | + |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments