Advertisement
angelhdz12

Zoom And Pan

Aug 8th, 2018
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.desktop.NativeApplication;
  2. import flash.display.DisplayObject;
  3. import flash.display.DisplayObjectContainer;
  4. import flash.events.Event;
  5. import flash.display.Sprite;
  6. import flash.display.StageAlign;
  7. import flash.display.StageScaleMode;
  8. import flash.events.KeyboardEvent;
  9. import flash.events.MouseEvent;
  10. import flash.geom.Rectangle;
  11. import flash.ui.Keyboard;
  12. import flash.ui.Multitouch;
  13. import flash.ui.MultitouchInputMode;
  14.  
  15. var DEFAULT_WIDTH:Number;
  16. var DEFAULT_HEIGHT:Number;
  17. var MIN_SCALE:Number = 100;
  18. var MAX_SCALE:Number = 800;
  19. var MOVE_SPEED: Number = 6;
  20. var SIZE_SCALE:Number = 6;
  21. var clickMore: Boolean = false;
  22. var clickLess: Boolean = false;
  23. var clickLeft: Boolean = false;
  24. var clickRight: Boolean = false;
  25. var clickUp: Boolean = false;
  26. var clickDown: Boolean = false;
  27. var clickCenter: Boolean = false;
  28. var keys:Object = {};
  29.  
  30. /////////////// VERY IMPORTANT ///////////
  31. stage.scaleMode = StageScaleMode.NO_SCALE;
  32. stage.align = StageAlign.TOP_LEFT;
  33. /////////////////////////////////////////
  34.  
  35.  
  36.  
  37.  
  38.  
  39. addChild(myMCimg);
  40.  
  41. //Set default width and height for the object so when clicking CENTER button it will default to that.
  42. DEFAULT_WIDTH = myMCimg.width;
  43. DEFAULT_HEIGHT = myMCimg.height;
  44.  
  45. //Alignment constants
  46. var Align:Object = {
  47.     CENTER: "center",
  48.    LEFT: "left",
  49.    RIGHT:  "right",
  50.     TOP:"top",
  51.     BOTTOM: "bottom",
  52.     CURRENT_PIVOT: {}
  53. }
  54.  
  55. var Scaling:Object = {
  56. UP: "up",
  57. DOWN: "down",
  58. scale: function($obj:Object, $scaling:String, $percentage:Number):Object{
  59. $percentage = $percentage/100;
  60. var width:Number = $obj.width * $percentage;
  61. var height:Number =  $obj.height * $percentage;
  62. var scalingWidth:Number = $scaling == Scaling.UP ? + width : - width;
  63. var scalingHeight:Number = $scaling == Scaling.UP ? + height : - height;
  64.   $obj.width += scalingWidth;
  65.   $obj.height += scalingHeight;
  66.   return $obj;
  67. }
  68. }
  69.  
  70. function resize($obj:DisplayObject, $scaling:String, $percentage:Number, $min:Number=1, $max:Number=3000):void
  71. {
  72.   var size:Object = Scaling.scale({width:$obj.width, height:$obj.height}, $scaling, $percentage);
  73.   if (size.width < $min || size.height < $min) return;
  74.   if (size.width > $max || size.height > $max) return;
  75.   $obj.width = size.width;
  76.   $obj.height = size.height;
  77. }
  78.  
  79.  
  80. /* Function for setting the object's registration/pivot/anchor point
  81.  
  82. Note: Call setPivot() function only ONCE along the code to set the object's
  83. registration/pivot/anchor point to desired position.*/
  84. function setPivot(s:DisplayObjectContainer, alignX:String = "center", alignY:String = "center"):Object{
  85.     Align.CURRENT_PIVOT[s.name] = {x:alignX, y:alignY};
  86.     var obj:Object = {x:s.x, y:s.y};
  87.     var rect:Rectangle = s.getBounds(s);
  88.     var _x:Number;
  89.     var _y:Number;
  90.     if (alignX == Align.CENTER) {
  91.         _x = s.width / 2;
  92.     } else if (alignX == Align.LEFT) {
  93.         _x = 0;
  94.     }
  95.     else if (alignX == Align.RIGHT) {
  96.         _x = s.width;
  97.     }
  98.     if (alignY == Align.CENTER) {
  99.         _y = s.height / 2;
  100.     } else if (alignY == Align.TOP) {
  101.         _y = 0;
  102.     }
  103.     else if (alignY == Align.BOTTOM) {
  104.         _y = s.height;
  105.     }
  106.     var x_offset:Number = _x + rect.x;
  107.     var y_offset:Number = _y + rect.y;
  108.     obj.x -= x_offset;
  109.     obj.y -= y_offset;
  110.     return obj;
  111. }
  112.  
  113. //Function to center object on stage
  114. function centerImage(image:DisplayObjectContainer):void {
  115.     var x:Number = (stage.fullScreenWidth - image.width)/2;
  116.     var y:Number = (stage.fullScreenHeight - image.height) / 2;
  117.     image.x = x;
  118.     image.y = y;
  119. }
  120.  
  121. /*Function to show the current registration/pivot/anchor point of the object.
  122. It will appear as a 10x10 blue square. */
  123. function showPivot(image:DisplayObjectContainer):void {
  124.     if (!Align.CURRENT_PIVOT[image.name]) return;
  125.     var spr:Sprite = new Sprite();
  126.     spr.graphics.beginFill(0x0000ff);
  127.     spr.graphics.drawRect(0, 0, 10, 10);
  128.     addChild(spr);
  129.     var x:Number, y:Number;
  130.     var pivotX:String = Align.CURRENT_PIVOT[image.name].x;
  131.     var pivotY:String = Align.CURRENT_PIVOT[image.name].y;
  132.    
  133.     if (pivotX == Align.CENTER) {
  134.         x = image.x + ((image.width - spr.width)/2);
  135.     }  else if (pivotX == Align.LEFT) {
  136.         x = image.x;
  137.     } else if (pivotX == Align.RIGHT) {
  138.         x = image.x + (image.width - (spr.width));
  139.     }
  140.    
  141.     if (pivotY == Align.CENTER) {
  142.         y = image.y + ((image.height - spr.height) / 2);
  143.     } else if (pivotY == Align.TOP) {
  144.         y = image.y;
  145.     } else if (pivotY == Align.BOTTOM) {
  146.         y = image.y + (image.height-(spr.height));
  147.     }
  148.     spr.x = x;
  149.     spr.y = y;
  150. }
  151.  
  152. //INIT
  153.  
  154. /* Note: Call setPivot() function only ONCE along the code to set the object's
  155. registration/pivot/anchor point to desired position.*/
  156. myMCimg.x = setPivot(myMCimg, Align.LEFT, Align.BOTTOM).x;
  157. myMCimg.y = setPivot(myMCimg, Align.LEFT, Align.BOTTOM).y;
  158.  
  159. //Center object on stage on when app runs for the first time
  160. centerImage(myMCimg);
  161.  
  162. //You can uncomment the below function call to show the object's current registration/pivot/anchor point
  163. //showPivot(myMCimg);
  164.  
  165. // --- Click Zoom more:
  166. btMore.addEventListener(MouseEvent.ROLL_OUT,  stopMoreZoom);
  167. btMore.addEventListener(MouseEvent.MOUSE_DOWN, moreZoom);
  168. function moreZoom(event: MouseEvent): void {
  169.  clickMore = true;
  170. }
  171. btMore.addEventListener(MouseEvent.MOUSE_UP, stopMoreZoom);
  172. function stopMoreZoom(event: MouseEvent): void {
  173.  clickMore = false;
  174. }
  175. // --- Click Zoom less:
  176. btLess.addEventListener(MouseEvent.ROLL_OUT,  stopLessZoom);
  177. btLess.addEventListener(MouseEvent.MOUSE_DOWN, lessZoom);
  178. function lessZoom(event: MouseEvent): void {
  179.  clickLess = true;
  180. }
  181. btLess.addEventListener(MouseEvent.MOUSE_UP, stopLessZoom);
  182. function stopLessZoom(event: MouseEvent): void {
  183.  clickLess = false;
  184. }
  185. // --- Click Move left:
  186. btLeft.addEventListener(MouseEvent.ROLL_OUT, stopLeftMove);
  187. btLeft.addEventListener(MouseEvent.MOUSE_DOWN, leftMove);
  188. function leftMove(event: MouseEvent): void {
  189.  clickLeft = true;
  190. }
  191. btLeft.addEventListener(MouseEvent.MOUSE_UP, stopLeftMove);
  192. function stopLeftMove(event: MouseEvent): void {
  193.  clickLeft = false;
  194. }
  195. // --- Click Move right:
  196. btRight.addEventListener(MouseEvent.ROLL_OUT, stopRightMove);
  197. btRight.addEventListener(MouseEvent.MOUSE_DOWN, rightMove);
  198. function rightMove(event: MouseEvent): void {
  199.  clickRight = true;
  200. }
  201. btRight.addEventListener(MouseEvent.MOUSE_UP, stopRightMove);
  202. function stopRightMove(event: MouseEvent): void {
  203.  clickRight = false;
  204. }
  205. // --- Click Move up:
  206. btUp.addEventListener(MouseEvent.ROLL_OUT, stopUpMove);
  207. btUp.addEventListener(MouseEvent.MOUSE_DOWN, upMove);
  208. function upMove(event: MouseEvent): void {
  209.  clickUp = true;
  210. }
  211. btUp.addEventListener(MouseEvent.MOUSE_UP, stopUpMove);
  212. function stopUpMove(event: MouseEvent): void {
  213.  clickUp = false;
  214. }
  215. // --- Click Move Down:
  216. btDown.addEventListener(MouseEvent.ROLL_OUT, stopDownMove);
  217. btDown.addEventListener(MouseEvent.MOUSE_DOWN, downMove);
  218. function downMove(event: MouseEvent): void {
  219.  clickDown = true;
  220. }
  221. btDown.addEventListener(MouseEvent.MOUSE_UP, stopDownMove);
  222. function stopDownMove(event: MouseEvent): void {
  223.  clickDown = false;
  224. }
  225. // --- Click Move Center:
  226. btCenter.addEventListener(MouseEvent.ROLL_OUT, stopCenterMove);
  227. btCenter.addEventListener(MouseEvent.MOUSE_DOWN, centerMove);
  228. function centerMove(event: MouseEvent): void {
  229.  clickCenter = true;
  230. }
  231. btCenter.addEventListener(MouseEvent.MOUSE_UP, stopCenterMove);
  232. function stopCenterMove(event: MouseEvent): void {
  233.  clickCenter = false;
  234. }
  235.  
  236.  
  237. // --- Click Zoom Wheel:
  238.  
  239. stage.addEventListener(MouseEvent.MOUSE_WHEEL, handleMouseWheel);
  240. function handleMouseWheel(event: MouseEvent): void {
  241. if(event.delta > 0) {
  242.     //Going up
  243.     resize(myMCimg, Scaling.UP, Math.abs(event.delta) * SIZE_SCALE, MIN_SCALE, MAX_SCALE);
  244. } else if(event.delta < 0){
  245.     //Going down
  246.     resize(myMCimg, Scaling.DOWN, Math.abs(event.delta) * SIZE_SCALE, MIN_SCALE, MAX_SCALE);
  247. }
  248. centerImage(myMCimg);
  249. trace("Using the mouse wheel!");
  250. // --- Actions:
  251. }
  252. addEventListener(Event.ENTER_FRAME, enterFrame);
  253. function enterFrame(event: Event): void {
  254.      if (keys[Keyboard.LEFT]) {
  255.  clickLeft = true;
  256.  
  257.  }
  258.  if (keys[Keyboard.RIGHT]) {
  259.  clickRight = true;
  260.  }
  261.  if (keys[Keyboard.UP]) {
  262.     clickUp = true;
  263.  }
  264.  if (keys[Keyboard.DOWN]) {
  265.   clickDown = true;
  266.  }
  267.  if (keys[Keyboard.ENTER]) {
  268. // --- Centralize:
  269.  
  270.  clickCenter = true;
  271.  }
  272.  if (clickMore == true) {
  273.   resize(myMCimg, Scaling.UP, SIZE_SCALE, MIN_SCALE, MAX_SCALE);
  274.   centerImage(myMCimg);
  275.  }
  276.  if ((clickLess == true)) {
  277.   resize(myMCimg, Scaling.DOWN, SIZE_SCALE, MIN_SCALE, MAX_SCALE);
  278.   centerImage(myMCimg);
  279.  }
  280.  if (clickLeft == true) {
  281.    
  282.  myMCimg.x -= MOVE_SPEED;
  283.  
  284.  }
  285.  if (clickRight == true) {
  286.  myMCimg.x += MOVE_SPEED;
  287.  }
  288.  if (clickUp == true) {
  289.  myMCimg.y -= MOVE_SPEED;
  290.  }
  291.  if (clickDown == true) {
  292.  myMCimg.y += MOVE_SPEED;
  293.  }
  294.  if (clickCenter == true) {
  295. // --- Centralize:
  296.  
  297.  myMCimg.width = DEFAULT_WIDTH;
  298.  myMCimg.height = DEFAULT_HEIGHT;
  299.  clickCenter = false;
  300.  centerImage(myMCimg);
  301.  }
  302. setChildIndex(myMCimg, 0);
  303. }
  304. // --- Click keys directions keyboard:
  305. stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKeys);
  306. function pressKeys(event:KeyboardEvent): void {
  307. keys[event.keyCode] = true;
  308. }
  309. stage.addEventListener(KeyboardEvent.KEY_UP, releaseKeys);
  310. function releaseKeys(event:KeyboardEvent):void {
  311.     keys[event.keyCode] = false;
  312. }
  313.    
  314. // --- Click Drag:
  315.  
  316.  myMCimg.addEventListener(MouseEvent.MOUSE_DOWN, dragMove);
  317.  function dragMove(event: MouseEvent): void {
  318.  myMCimg.startDrag();
  319. }
  320.  myMCimg.addEventListener(MouseEvent.MOUSE_UP, stopDragMove);
  321.  function stopDragMove(event: MouseEvent): void {
  322.  stopDrag();
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement