PageRenderTime 96ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/junkbyte/console/view/Ruler.as

https://github.com/zeffrin/ZType
ActionScript | 277 lines | 227 code | 12 blank | 38 comment | 13 complexity | 4f1fa2060518fcbacfb0a5e7abd978f7 MD5 | raw file
  1. /*
  2. *
  3. * Copyright (c) 2008-2010 Lu Aye Oo
  4. *
  5. * @author Lu Aye Oo
  6. *
  7. * http://code.google.com/p/flash-console/
  8. *
  9. *
  10. * This software is provided 'as-is', without any express or implied
  11. * warranty. In no event will the authors be held liable for any damages
  12. * arising from the use of this software.
  13. * Permission is granted to anyone to use this software for any purpose,
  14. * including commercial applications, and to alter it and redistribute it
  15. * freely, subject to the following restrictions:
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software. If you use this software
  18. * in a product, an acknowledgment in the product documentation would be
  19. * appreciated but is not required.
  20. * 2. Altered source versions must be plainly marked as such, and must not be
  21. * misrepresented as being the original software.
  22. * 3. This notice may not be removed or altered from any source distribution.
  23. *
  24. */
  25. package com.junkbyte.console.view {
  26. import flash.geom.Matrix;
  27. import flash.display.BitmapData;
  28. import flash.display.Bitmap;
  29. import com.junkbyte.console.ConsoleConfig;
  30. import com.junkbyte.console.ConsoleStyle;
  31. import com.junkbyte.console.Console;
  32. import flash.display.BlendMode;
  33. import flash.display.Graphics;
  34. import flash.display.Shape;
  35. import flash.display.Sprite;
  36. import flash.events.Event;
  37. import flash.events.MouseEvent;
  38. import flash.geom.Point;
  39. import flash.geom.Rectangle;
  40. import flash.text.TextField;
  41. import flash.text.TextFieldAutoSize;
  42. import flash.text.TextFormat;
  43. import flash.text.TextFormatAlign;
  44. import flash.ui.Mouse;
  45. public class Ruler extends Sprite{
  46. private var _master:Console;
  47. private var _config : ConsoleConfig;
  48. private var _area:Rectangle;
  49. private var _pointer:Shape;
  50. private var _posTxt:TextField;
  51. private var _zoom:Bitmap;
  52. private var _points:Array;
  53. public function Ruler(console:Console) {
  54. _master = console;
  55. _config = console.config;
  56. buttonMode = true;
  57. _points = new Array();
  58. _pointer = new Shape();
  59. addChild(_pointer);
  60. var p:Point = new Point();
  61. p = globalToLocal(p);
  62. _area = new Rectangle(-console.stage.stageWidth*1.5+p.x, -console.stage.stageHeight*1.5+p.y, console.stage.stageWidth*3, console.stage.stageHeight*3);
  63. graphics.beginFill(_config.style.backgroundColor, 0.2);
  64. graphics.drawRect(_area.x, _area.y, _area.width, _area.height);
  65. graphics.endFill();
  66. //
  67. _posTxt = _master.panels.mainPanel.makeTF("positionText", true);
  68. _posTxt.autoSize = TextFieldAutoSize.LEFT;
  69. addChild(_posTxt);
  70. //
  71. _zoom = new Bitmap();
  72. _zoom.scaleY = _zoom.scaleX = 2;
  73. addChild(_zoom);
  74. //
  75. addEventListener(MouseEvent.CLICK, onMouseClick, false, 0, true);
  76. addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true);
  77. onMouseMove();
  78. if(_config.rulerHidesMouse) Mouse.hide();
  79. _master.report("<b>Ruler started. Click on two locations to measure.</b>", -1);
  80. }
  81. private function onMouseMove(e:MouseEvent = null):void{
  82. _pointer.graphics.clear();
  83. _pointer.graphics.lineStyle(1, 0xAACC00, 1);
  84. _pointer.graphics.moveTo(_area.x, mouseY);
  85. _pointer.graphics.lineTo(_area.x+_area.width, mouseY);
  86. _pointer.graphics.moveTo(mouseX, _area.y);
  87. _pointer.graphics.lineTo(mouseX, _area.y+_area.height);
  88. _pointer.blendMode = BlendMode.INVERT;
  89. _posTxt.text = "<low>"+mouseX+","+mouseY+"</low>";
  90. //
  91. var bmd:BitmapData = new BitmapData(30, 30);
  92. try{
  93. var m:Matrix = new Matrix();
  94. m.tx = -stage.mouseX+15;
  95. m.ty = -stage.mouseY+15;
  96. bmd.draw(stage, m);
  97. }catch(err:Error){
  98. bmd = null;
  99. }
  100. _zoom.bitmapData = bmd;
  101. //
  102. var d:int = 10;
  103. _posTxt.x = mouseX-_posTxt.width-d;
  104. _posTxt.y = mouseY-_posTxt.height-d;
  105. _zoom.x = _posTxt.x+_posTxt.width-_zoom.width;
  106. _zoom.y = _posTxt.y-_zoom.height;
  107. if(_posTxt.x < 16){
  108. _posTxt.x = mouseX+d;
  109. _zoom.x = _posTxt.x;
  110. }
  111. if(_posTxt.y < 38){
  112. _posTxt.y = mouseY+d;
  113. _zoom.y = _posTxt.y+_posTxt.height;
  114. }
  115. }
  116. private function onMouseClick(e:MouseEvent):void{
  117. e.stopPropagation();
  118. var p:Point;
  119. var style : ConsoleStyle = _config.style;
  120. if(_points.length==0){
  121. p = new Point(e.localX, e.localY);
  122. graphics.lineStyle(1, 0xFF0000);
  123. graphics.drawCircle(p.x, p.y, 3);
  124. _points.push(p);
  125. }else if(_points.length==1){
  126. _zoom.bitmapData = null;
  127. if(_config.rulerHidesMouse) Mouse.show();
  128. removeChild(_pointer);
  129. removeChild(_posTxt);
  130. removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  131. p = _points[0];
  132. var p2:Point = new Point(e.localX, e.localY);
  133. _points.push(p2);
  134. graphics.clear();
  135. graphics.beginFill(style.backgroundColor, 0.4);
  136. graphics.drawRect(_area.x, _area.y, _area.width, _area.height);
  137. graphics.endFill();
  138. graphics.lineStyle(1.5, 0xFF0000);
  139. graphics.drawCircle(p.x, p.y, 4);
  140. graphics.lineStyle(1.5, 0xFF9900);
  141. graphics.drawCircle(p2.x, p2.y, 4);
  142. var mp:Point = Point.interpolate(p, p2, 0.5);
  143. graphics.lineStyle(1, 0xAAAAAA);
  144. graphics.drawCircle(mp.x, mp.y, 4);
  145. //
  146. var xmin:Point = p;
  147. var xmax:Point = p2;
  148. if(p.x>p2.x){
  149. xmin = p2;
  150. xmax = p;
  151. }
  152. var ymin:Point = p;
  153. var ymax:Point = p2;
  154. if(p.y>p2.y){
  155. ymin = p2;
  156. ymax = p;
  157. }
  158. //
  159. var w:Number = xmax.x-xmin.x;
  160. var h:Number = ymax.y-ymin.y;
  161. var d:Number = Point.distance(p, p2);
  162. //
  163. var txt:TextField = makeTxtField(style.highColor);
  164. txt.text = round(p.x)+","+ round(p.y);
  165. txt.x = p.x;
  166. txt.y = p.y-(ymin==p?14:0);
  167. addChild(txt);
  168. //
  169. txt = makeTxtField(style.highColor);
  170. txt.text = round(p2.x)+","+ round(p2.y);
  171. txt.x = p2.x;
  172. txt.y = p2.y-(ymin==p2?14:0);;
  173. addChild(txt);
  174. //
  175. if(w>40 || h>25){
  176. txt = makeTxtField(style.lowColor);
  177. txt.text = round(mp.x)+","+ round(mp.y);
  178. txt.x = mp.x;
  179. txt.y = mp.y;
  180. addChild(txt);
  181. }
  182. //
  183. graphics.lineStyle(1, 0xAACC00, 0.5);
  184. graphics.moveTo(_area.x, ymin.y);
  185. graphics.lineTo(_area.x+_area.width, ymin.y);
  186. graphics.moveTo(_area.x, ymax.y);
  187. graphics.lineTo(_area.x+_area.width, ymax.y);
  188. graphics.moveTo(xmin.x, _area.y);
  189. graphics.lineTo(xmin.x, _area.y+_area.height);
  190. graphics.moveTo(xmax.x, _area.y);
  191. graphics.lineTo(xmax.x, _area.y+_area.height);
  192. //
  193. var a1:Number = round(angle(p,p2),100);
  194. var a2:Number = round(angle(p2,p),100);
  195. graphics.lineStyle(1, 0xAA0000, 0.8);
  196. drawCircleSegment(graphics, 10,p, a1, -90);
  197. graphics.lineStyle(1, 0xCC8800, 0.8);
  198. drawCircleSegment(graphics, 10,p2, a2, -90);
  199. //
  200. graphics.lineStyle(2, 0x00FF00, 0.7);
  201. graphics.moveTo(p.x, p.y);
  202. graphics.lineTo(p2.x, p2.y);
  203. //
  204. _master.report("Ruler results: (red) <b>["+p.x+","+p.y+"]</b> to (orange) <b>["+p2.x+","+p2.y+"]</b>", -2);
  205. _master.report("Distance: <b>"+round(d,100) +"</b>", -2);
  206. _master.report("Mid point: <b>["+mp.x+","+mp.y+"]</b>", -2);
  207. _master.report("Width:<b>"+w+"</b>, Height: <b>"+h+"</b>", -2);
  208. _master.report("Angle from first point (red): <b>"+a1+"°</b>", -2);
  209. _master.report("Angle from second point (orange): <b>"+a2+"°</b>", -2);
  210. }else{
  211. exit();
  212. }
  213. }
  214. public function exit():void{
  215. _master = null;
  216. dispatchEvent(new Event(Event.COMPLETE));
  217. }
  218. private function makeTxtField(col:Number, b:Boolean = true):TextField{
  219. var format:TextFormat = new TextFormat(_config.style.menuFont, _config.style.menuFontSize, col, b, true, null, null, TextFormatAlign.RIGHT);
  220. var txt:TextField = new TextField();
  221. txt.autoSize = TextFieldAutoSize.RIGHT;
  222. txt.selectable = false;
  223. txt.defaultTextFormat = format;
  224. return txt;
  225. }
  226. private function round(n:Number, d:uint = 10):Number{
  227. return Math.round(n*d)/d;
  228. }
  229. private function angle(srcP:Point, point:Point):Number {
  230. var a:Number = Math.atan2(point.y - srcP.y , point.x - srcP.x)/Math.PI * 180;
  231. a +=90;
  232. if(a>180) a -= 360;
  233. return a;
  234. }
  235. private function drawCircleSegment(g : Graphics,radius :Number,pos:Point, deg:Number = 180, startDeg:Number = 0):Point
  236. {
  237. var reversed:Boolean = false;
  238. if(deg<0){
  239. reversed = true;
  240. deg = Math.abs(deg);
  241. }
  242. var rad:Number = (deg*Math.PI)/180;
  243. var rad2:Number = (startDeg*Math.PI)/180;
  244. var p:Point = getPointOnCircle(radius, rad2);
  245. p.offset(pos.x,pos.y);
  246. g.moveTo(p.x,p.y);
  247. var pra:Number = 0;
  248. for (var i:int = 1; i<=(rad+1); i++) {
  249. var ra:Number = i<=rad?i:rad;
  250. var diffr:Number = ra-pra;
  251. var offr:Number = 1+(0.12*diffr*diffr);
  252. var ap:Point = getPointOnCircle(radius*offr, ((ra-(diffr/2))*(reversed?-1:1))+rad2);
  253. ap.offset(pos.x,pos.y);
  254. p = getPointOnCircle(radius, (ra*(reversed?-1:1))+rad2);
  255. p.offset(pos.x,pos.y);
  256. g.curveTo(ap.x,ap.y, p.x,p.y);
  257. pra = ra;
  258. }
  259. return p;
  260. }
  261. private function getPointOnCircle(radius:Number, rad:Number):Point {
  262. return new Point(radius * Math.cos(rad),radius * Math.sin(rad));
  263. }
  264. }
  265. }