Navigator.as 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. package iTop
  2. {
  3. import flash.display.*;
  4. import flash.geom.*;
  5. import flash.net.*;
  6. import flash.events.*;
  7. import iTop.GraphNode;
  8. import fl.controls.Slider;
  9. import fl.events.SliderEvent;
  10. import fl.controls.Label;
  11. // For callbacks from Javascript
  12. import flash.external.ExternalInterface;
  13. // The main canvas
  14. public class Navigator extends MovieClip
  15. {
  16. protected var m_oLoader:URLLoader;
  17. protected var m_aNodes:Object;
  18. protected var m_aLinks:Array;
  19. protected var m_oRootNode:GraphNode;
  20. protected var m_oCanvas:NavigatorCanvas;
  21. public var m_bChildDragging:Boolean;
  22. // Parameters
  23. protected var m_sStartPosition:String;
  24. protected var m_sDataUrl:String;
  25. protected var m_sDetailsUrl:String;
  26. protected var m_sRelation:String;
  27. protected var m_sObjClass:String;
  28. protected var m_sObjId:String;
  29. protected var m_sExclude:String;
  30. // Constants
  31. protected var m_RADIUS = 150;
  32. protected var m_Q = 0.9; // Electrostatic forces coeff
  33. protected var m_K = 1.0; // Elastic forces coeff
  34. protected var m_Kf = 0.7; // Fluid friction coeff
  35. protected var m_Ks = 30; // Solid friction coeff
  36. protected var m_deltaT = 0.1; // Interval of time between updates
  37. protected var m_MAX_ITEMS_PER_ROW = 8;
  38. protected var m_FOCUS_DELAY_COUNTDOWN = 30; // 30 images to zoom & pan correctly
  39. protected var m_fZoom:Number;
  40. // Constructor
  41. public function Navigator()
  42. {
  43. m_aLinks = new Array();
  44. m_aNodes = new Array();
  45. m_sExclude = '';
  46. m_fZoom = 1;
  47. initParameters();
  48. var success = true;
  49. if (ExternalInterface.available)
  50. {
  51. //Security.allowDomain(loader.contentLoaderInfo.url);
  52. //the addCallback registers a function to be called from javascript, and this is what you wanted to do:
  53. try
  54. {
  55. ExternalInterface.addCallback("Filter", DoFilter);
  56. }
  57. catch(err)
  58. {
  59. m_sTitle.text = 'E: '+err.description;
  60. success = false
  61. }
  62. }
  63. if (success)
  64. {
  65. //m_sTitle.text = 'Ok...';
  66. doLoadData();
  67. }
  68. else
  69. {
  70. m_sTitle.text = 'Failed to addCallback';
  71. }
  72. addEventListener(Event.ENTER_FRAME, initGraphics);
  73. //Stop scaling the flash content
  74. stage.scaleMode = StageScaleMode.NO_SCALE;
  75. }
  76. function Reset()
  77. {
  78. m_aLinks = new Array();
  79. for (var i:String in m_aNodes)
  80. {
  81. m_oCanvas.removeChild(m_aNodes[i]);
  82. }
  83. m_aNodes = new Array();
  84. removeEventListener(Event.ENTER_FRAME, drawLines);
  85. stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown)
  86. stage.removeEventListener(MouseEvent.MOUSE_UP, mouseReleased);
  87. }
  88. function DoFilter(sExcludeList:String):void
  89. {
  90. //do something
  91. m_sExclude = sExcludeList;
  92. doLoadData();
  93. }
  94. protected function initParameters():void
  95. {
  96. m_sDataUrl = ReadParam('xmlUrl', 'http://localhost:81/pages/xml.navigator.php?operation=relation');
  97. m_sDetailsUrl = ReadParam('drillUrl', 'http://localhost/pages/UI.php?operation=details');
  98. m_sRelation = ReadParam('relation', 'impacts');
  99. m_sObjClass = ReadParam('obj_class', 'DBServerInstance');
  100. m_sObjId = ReadParam('obj_id', '7');
  101. m_sStartPosition = ReadParam('start_pos', 'left');
  102. }
  103. function initGraphics(event:Event):void
  104. {
  105. m_oCanvas = new NavigatorCanvas(); // All drawings will occur here
  106. addChild(m_oCanvas);
  107. m_oCanvas.scaleX = m_fZoom;
  108. m_oCanvas.scaleY = m_fZoom;
  109. // Handle listeners...
  110. removeEventListener(Event.ENTER_FRAME,initGraphics);
  111. m_oZoomSlider.value = 100;
  112. m_oZoomSlider.addEventListener(SliderEvent.CHANGE, onZoomChange);
  113. }
  114. function mouseDown(event:MouseEvent):void
  115. {
  116. trace("Click in canvas");
  117. if (!m_bChildDragging)
  118. {
  119. m_oCanvas.startDrag();
  120. }
  121. }
  122. function mouseReleased(event:MouseEvent):void
  123. {
  124. if (!m_bChildDragging)
  125. {
  126. m_oCanvas.stopDrag();
  127. }
  128. }
  129. function onZoomChange(event:SliderEvent):void
  130. {
  131. SetZoomLevel(event.value/100);
  132. }
  133. function SetZoomLevel(fZoomLevel:Number):void
  134. {
  135. m_fZoom = fZoomLevel;
  136. m_oCanvas.scaleX = m_fZoom;
  137. m_oCanvas.scaleY = m_fZoom;
  138. }
  139. function GetZoomLevel()
  140. {
  141. return m_fZoom;
  142. }
  143. function doLoadData()
  144. {
  145. m_sTitle.text = "Loading...";
  146. m_oPreloader.visible = true;
  147. m_oZoomSlider.enabled = true;
  148. var sSeparator:String = '?';
  149. if (m_sDataUrl.indexOf(sSeparator) != -1)
  150. {
  151. sSeparator = '&';
  152. }
  153. var myString:String = m_sDataUrl+sSeparator+'relation='+m_sRelation+'&class='+m_sObjClass+'&id='+m_sObjId+'&exclude='+m_sExclude;
  154. trace("Requesting:"+myString);
  155. var myXMLURL:URLRequest = new URLRequest(myString);
  156. m_oLoader = new URLLoader();
  157. m_oLoader.addEventListener(Event.COMPLETE, onXMLLoadComplete);
  158. m_oLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onXMLLoadError);
  159. m_oLoader.addEventListener(IOErrorEvent.IO_ERROR, onXMLLoadError);
  160. m_oLoader.load(myXMLURL);
  161. }
  162. function onXMLLoadComplete(event:Event):void
  163. {
  164. try
  165. {
  166. Reset();
  167. var myXML:XML = XML(m_oLoader.data);
  168. //trace("Data loaded." + myXML);
  169. //trace("===========================");
  170. parseXMLData(null, myXML, 0, 0);
  171. m_sTitle.text = decodeEntities(myXML.attribute("title").toString());
  172. m_oZoomSlider.enabled = true;
  173. addEventListener(Event.ENTER_FRAME, drawLines);
  174. stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown)
  175. stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
  176. //trace('======= Initial Posistions =========');
  177. //DumpPositions();
  178. }
  179. catch(error:IOErrorEvent)
  180. {
  181. m_sTitle.text = "I/O Error: unable to load the graph data ("+error+")";
  182. }
  183. catch(error:TypeError)
  184. {
  185. m_sTitle.text = "Error: unable to load the graph data (Invalid XML data)";
  186. }
  187. catch(error:Error)
  188. {
  189. m_sTitle.text = "Error: unable to load the graph data ("+error+")";
  190. }
  191. finally
  192. {
  193. if (m_oPreloader != null)
  194. {
  195. m_oPreloader.visible = false;
  196. }
  197. }
  198. }
  199. function onXMLLoadError(event:IOErrorEvent):void
  200. {
  201. if (m_oPreloader != null)
  202. {
  203. m_oPreloader.visible = false;
  204. }
  205. m_sTitle.text = "I/O Error: unable to load the graph data ("+event+")";
  206. }
  207. function parseXMLData(oParentNode:GraphNode, oXMLData:XML, iChildIndex:Number, iChildCount:Number)
  208. {
  209. //trace(oXMLData.child("node").length());
  210. var oNode:GraphNode;
  211. oNode = addNode(oParentNode, oXMLData.child("node")[0], iChildIndex, iChildCount);
  212. if (oParentNode != null)
  213. {
  214. AddLink(oParentNode.GetKey(), oNode.GetKey());
  215. }
  216. //trace('Root node:'+oRoot.toString());
  217. var oLinks = oXMLData.child("node")[0].links;
  218. var iChildIndex:Number = 0;
  219. if (oLinks.length() > 0)
  220. {
  221. //trace('links: '+oLinks.toString());
  222. var oLink = oLinks.link;
  223. for each(var oChild:XML in oLink)
  224. {
  225. parseXMLData(oNode, oChild, iChildIndex, oLinks.link.length());
  226. iChildIndex++;
  227. }
  228. }
  229. }
  230. function decodeEntities(str: String)
  231. {
  232. return str.replace(/&amp;/g, '&').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&quot;/g, '"').replace(/&apos;/g, "'");
  233. }
  234. function addNode(oParent:GraphNode, oXMLData:XML, iChildIndex:Number, iChildCount:Number)
  235. {
  236. var sClass:String = oXMLData.@obj_class;
  237. var sClassName:String = oXMLData.@obj_class_name;
  238. var iId = oXMLData.@id;
  239. var sLabel:String = decodeEntities(oXMLData.@name);
  240. var sIcon:String = oXMLData.@icon;
  241. var oDetails:Object = new Object;
  242. var sZlist:String = oXMLData.@zlist;
  243. var oNode:GraphNode = GetNode(sClass+'/'+iId);
  244. if (oNode == null)
  245. {
  246. // If the node does not already exist, let's create it
  247. var oPt:Point = GetNextFreePosition(oParent, iChildIndex, iChildCount);
  248. var sParentKey = null;
  249. if (oParent != null)
  250. {
  251. sParentKey = oParent.GetKey();
  252. }
  253. // Read the details
  254. var aDetails:Array;
  255. aDetails = sZlist.split(',');
  256. for(var i:String in aDetails)
  257. {
  258. //if (oXMLData.hasOwnProperty('att_'+i))
  259. //{
  260. oDetails[aDetails[i]] = oXMLData.attribute('att_'+i).toString();
  261. //}
  262. }
  263. oNode = new GraphNode(this, oPt, sClass, sClassName, iId, sLabel, sIcon, sParentKey, m_fZoom, oDetails);
  264. this.m_aNodes[oNode.GetKey()] = oNode; //Keep it referenced
  265. if (oParent == null)
  266. {
  267. m_oRootNode = oNode;
  268. }
  269. m_oCanvas.addChild(oNode);
  270. }
  271. return oNode;
  272. //trace("class: "+sClass+", id: "+iId+", name: "+sLabel+", Icon: "+sIcon);
  273. }
  274. function GetNode(sKey)
  275. {
  276. if (m_aNodes.hasOwnProperty(sKey))
  277. {
  278. return m_aNodes[sKey];
  279. }
  280. else
  281. {
  282. return null;
  283. }
  284. }
  285. function GetNextFreePosition(oParent:GraphNode, iChildIndex:Number, iChildCount:Number):Point
  286. {
  287. var oPt:Point = GetInitialPosition();
  288. var angle:Number = GetInitialAngle();
  289. if (oParent != null)
  290. {
  291. oPt.x = oParent.x;
  292. oPt.y = oParent.y;
  293. var sGrandParentKey:String = oParent.GetParentKey();
  294. if (sGrandParentKey != null)
  295. {
  296. var oGrandParent:GraphNode = GetNode(sGrandParentKey);
  297. var dx:Number = oParent.x - oGrandParent.x;
  298. var dy:Number = oParent.y - oGrandParent.y;
  299. if ((dx == 0) && (dy == 0))
  300. {
  301. angle = GetInitialAngle();
  302. }
  303. else
  304. {
  305. angle = Math.atan2(dy, dx);
  306. }
  307. }
  308. var nbItemsOnRow:Number = 0;
  309. var nbRows:Number = 0;
  310. // Determines the position of this element
  311. // The elements are placed on circles of maximum m_MAX_ITEMS_PER_ROW elements per row
  312. // The last row containing potentially less items
  313. // nbRows indicates on which row (first row = 0) the item is to be placed
  314. if (iChildCount > m_MAX_ITEMS_PER_ROW)
  315. {
  316. nbRows = Math.floor(iChildIndex / m_MAX_ITEMS_PER_ROW);
  317. if ( iChildIndex > (Math.floor(iChildCount / m_MAX_ITEMS_PER_ROW)*m_MAX_ITEMS_PER_ROW))
  318. {
  319. // node is on the last (incomplete) row
  320. nbItemsOnRow = (iChildCount % m_MAX_ITEMS_PER_ROW);
  321. }
  322. else
  323. {
  324. nbItemsOnRow = m_MAX_ITEMS_PER_ROW;
  325. }
  326. }
  327. else
  328. {
  329. if (iChildCount == 2)
  330. {
  331. nbItemsOnRow = 4; // Nicer display than everything aligned at 180 deg.
  332. }
  333. else
  334. {
  335. nbItemsOnRow = iChildCount;
  336. }
  337. }
  338. var radius = this.m_RADIUS * (1 + nbRows);
  339. angle += (1 - 2*((1+iChildIndex) % 2))*(Math.floor((1+iChildIndex) / 2))*(2*Math.PI) / nbItemsOnRow;
  340. oPt.x += radius * Math.cos(angle);
  341. oPt.y += radius * 0.7 * Math.sin(angle); // Ellipse because the labels are written horizontally !
  342. //trace("iChildIndex: "+iChildIndex+" (iChildCount: "+iChildCount+") x: "+oPt.x+" y: "+oPt.y+" sGdParentKey: "+sGrandParentKey);
  343. }
  344. return oPt;
  345. }
  346. function GetInitialPosition():Point
  347. {
  348. trace('width: '+stage.stageWidth+' height: '+stage.stageHeight);
  349. var oPos:Point = new Point(0,0);
  350. switch(m_sStartPosition)
  351. {
  352. case 'left':
  353. oPos.x = m_RADIUS;
  354. oPos.y = stage.stageHeight / 2;
  355. break;
  356. case 'right':
  357. oPos.x = stage.stageWidth - m_RADIUS;
  358. oPos.y = stage.stageHeight / 2;
  359. break;
  360. case 'top':
  361. oPos.x = stage.stageWidth/2;
  362. oPos.y = m_RADIUS;
  363. break;
  364. case 'bottom':
  365. oPos.x = stage.stageWidth/2;
  366. oPos.y = stage.stageHeight - m_RADIUS;
  367. break;
  368. }
  369. return oPos;
  370. }
  371. function GetInitialAngle():Number
  372. {
  373. var angle:Number;
  374. switch(m_sStartPosition)
  375. {
  376. case 'left':
  377. angle = 0;
  378. break;
  379. case 'right':
  380. angle = Math.PI;
  381. break;
  382. case 'top':
  383. angle = -Math.PI / 2;
  384. break;
  385. case 'right':
  386. angle = Math.PI / 2;
  387. break;
  388. }
  389. return angle;
  390. }
  391. function AddLink(sStart:String, sEnd:String)
  392. {
  393. var oLink = new Link(sStart, sEnd);
  394. m_aLinks.push(oLink);
  395. }
  396. function drawLines(event:Event):void
  397. {
  398. var color:uint = 0x666666;
  399. m_oCanvas.graphics.clear();
  400. m_oCanvas.graphics.lineStyle(2,0x666666,100);
  401. UpdatePositions();
  402. for (var index:String in m_aLinks)
  403. {
  404. var oStartNode:GraphNode = GetNode(m_aLinks[index].GetStart());
  405. var oEndNode = GetNode(m_aLinks[index].GetEnd());
  406. m_oCanvas.graphics.moveTo(oStartNode.x, oStartNode.y);
  407. m_oCanvas.graphics.lineTo(oEndNode.x, oEndNode.y);
  408. var oMiddlePoint:Point = new Point((oEndNode.x+oStartNode.x)/2, (oEndNode.y+oStartNode.y)/2);
  409. drawArrow(oMiddlePoint, oEndNode.x - oStartNode.x, oEndNode.y - oStartNode.y, color);
  410. }
  411. if (this.m_FOCUS_DELAY_COUNTDOWN > 0)
  412. {
  413. this.m_FOCUS_DELAY_COUNTDOWN--;
  414. trace('FOCUS_DELAY:'+this.m_FOCUS_DELAY_COUNTDOWN);
  415. UpdatePanAndZoom(m_FOCUS_DELAY_COUNTDOWN / 30);
  416. }
  417. else if (this.m_FOCUS_DELAY_COUNTDOWN == 0)
  418. {
  419. // Increase the friction so that manually manipulating objects gets easier
  420. trace("More friction now...");
  421. m_Ks = 5*m_Ks; // 5 times more friction
  422. this.m_FOCUS_DELAY_COUNTDOWN--;
  423. }
  424. }
  425. function drawArrow(oPt:Point, dx:Number, dy:Number, color:uint):void
  426. {
  427. var l:Number = Math.sqrt(dx*dx+dy*dy);
  428. var arrowSize:Number = 5;
  429. if (l > 0)
  430. {
  431. m_oCanvas.graphics.lineStyle(2,color,100,false,"normal",CapsStyle.ROUND);
  432. m_oCanvas.graphics.moveTo(oPt.x, oPt.y);
  433. m_oCanvas.graphics.lineTo(oPt.x + arrowSize*(dy-dx)/l, oPt.y - arrowSize*(dx+dy)/l);
  434. m_oCanvas.graphics.moveTo(oPt.x, oPt.y);
  435. m_oCanvas.graphics.lineTo(oPt.x - arrowSize*(dx+dy)/l, oPt.y - arrowSize*(dy-dx)/l);
  436. }
  437. }
  438. public function ReadParam(sName:String, sDefaultValue:String)
  439. {
  440. var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
  441. if (paramObj.hasOwnProperty(sName))
  442. {
  443. return unescape(paramObj[sName]);
  444. }
  445. else
  446. {
  447. return sDefaultValue;
  448. }
  449. }
  450. public function ComputeElectrostaticForces():Array
  451. {
  452. var aForces:Array = new Array;
  453. //trace('====== BEGIN ComputeElectrostaticForces() =======');
  454. for (var i:String in this.m_aNodes)
  455. {
  456. aForces[i] = new Object;
  457. aForces[i].FxTotal = 0;
  458. aForces[i].FyTotal = 0;
  459. var oCurrentNode:GraphNode = GetNode(i);
  460. for (var j:String in this.m_aNodes)
  461. {
  462. if (i != j)
  463. {
  464. var oRemoteNode:GraphNode = GetNode(j);
  465. var dx:Number = oRemoteNode.x - oCurrentNode.x;
  466. var dy:Number = oRemoteNode.y - oCurrentNode.y;
  467. var d2:Number = (dx*dx + dy*dy) / (this.m_RADIUS * this.m_RADIUS);
  468. var d:Number = Math.sqrt(d2);
  469. var Fx:Number = 0;
  470. var Fy:Number = 0;
  471. if (d2 < 0.05)
  472. {
  473. d2 = 0.05;
  474. }
  475. if (d2 < 2 ) // Full influence under 2 * m_RADIUS px
  476. {
  477. Fx = -this.m_Q * dx / d2;
  478. Fy = -this.m_Q * dy / d2;
  479. aForces[i].FxTotal += Fx;
  480. aForces[i].FyTotal += Fy;
  481. }
  482. else if (d2 < 4 ) // Decrease the influence to between 4 and 2 * m_RADIUS px
  483. {
  484. Fx = -this.m_Q * (4 - d2) * dx / d2;
  485. Fy = -this.m_Q * (4 - d2) * dy / d2;
  486. aForces[i].FxTotal += Fx;
  487. aForces[i].FyTotal += Fy;
  488. }
  489. }
  490. }
  491. }
  492. //for (i in this.m_aNodes)
  493. //{
  494. // trace('ELECTROSTATIC forces on '+i+': Fx='+aForces[i].FxTotal+', Fy='+aForces[i].FyTotal);
  495. // if (Math.abs(aForces[i].FyTotal) > 1)
  496. // {
  497. // for (i in this.m_aNodes)
  498. // {
  499. // var oNode:GraphNode = GetNode(i);
  500. // trace('node: '+i+' (x='+oNode.x+', y='+oNode.y+')');
  501. // }
  502. // }
  503. //}
  504. //trace('====== END ComputeElectrostaticForces() =======');
  505. return aForces;
  506. }
  507. function ComputeElasticForces()
  508. {
  509. //trace('====== BEGIN ComputeElasticForces() =======');
  510. var aForces:Array = new Array;
  511. for (var i:String in this.m_aNodes)
  512. {
  513. aForces[i] = new Object;
  514. aForces[i].FxTotal = 0;
  515. aForces[i].FyTotal = 0;
  516. }
  517. // Elastic forces: each link applies a force proportional to its length (F = - K * x)
  518. for(i in this.m_aLinks)
  519. {
  520. var oStartNode:GraphNode = GetNode(m_aLinks[i].GetStart());
  521. var oEndNode = GetNode(m_aLinks[i].GetEnd());
  522. var dx = oStartNode.x - oEndNode.x;
  523. var dy = oStartNode.y - oEndNode.y;
  524. //d = Math.sqrt(dx*dx + dy*dy);
  525. //Fx = -K * d * dx / d;
  526. //Fy = -K * d * dy / d;
  527. // Links with more weight attached are more rigid !
  528. //weightCoeff = (aWeights[aLinks[l].start] + aWeights[aLinks[l].end])/2;
  529. var Fx = -this.m_K * dx;
  530. var Fy = -this.m_K * dy;
  531. aForces[oStartNode.GetKey()].FxTotal += Fx;
  532. aForces[oStartNode.GetKey()].FyTotal += Fy;
  533. aForces[oEndNode.GetKey()].FxTotal -= Fx;
  534. aForces[oEndNode.GetKey()].FyTotal -= Fy;
  535. }
  536. //for (i in this.m_aNodes)
  537. //{
  538. // trace('Elastic forces on '+i+': Fx='+aForces[i].FxTotal+', Fy='+aForces[i].FyTotal);
  539. // if (Math.abs(aForces[i].FyTotal) > 1)
  540. // {
  541. // for (i in this.m_aNodes)
  542. // {
  543. // var oNode:GraphNode = GetNode(i);
  544. // trace('node: '+i+' (x='+oNode.x+', y='+oNode.y+')');
  545. // }
  546. // }
  547. //}
  548. //trace('====== END ComputeElasticForces() =======');
  549. return aForces;
  550. }
  551. /**
  552. * Update the nodes' position based on their current movement and the forces applied
  553. */
  554. function UpdatePositions()
  555. {
  556. //trace('====== BEGIN UpdatePositions() =======');
  557. var aElasticForces:Array = ComputeElasticForces();
  558. var aElectrostaticForces:Array = ComputeElectrostaticForces();
  559. //DrawForces(aElectrostaticForces, 0xcc0000);
  560. //DrawForces(aElectrostaticForces, 0x0000cc);
  561. for (var i:String in this.m_aNodes)
  562. {
  563. var oNode:GraphNode = GetNode(i);
  564. if (!oNode.m_bInDrag)
  565. {
  566. var Fx:Number = aElasticForces[i].FxTotal + aElectrostaticForces[i].FxTotal;
  567. var Fy:Number = aElasticForces[i].FyTotal + aElectrostaticForces[i].FyTotal;
  568. if ( (Fx*Fx + Fy*Fy) < (this.m_Ks*this.m_Ks))
  569. {
  570. // Movement is less than minimum level (solid friction) => object is stopped
  571. // otherwise let's keep it moving
  572. oNode.m_speed_x = 0;
  573. oNode.m_speed_y = 0;
  574. //trace('object '+i+' stopped ! (x='+oNode.x+', y='+oNode.y+')');
  575. }
  576. else
  577. {
  578. oNode.m_speed_x = oNode.m_speed_x*this.m_Kf + this.m_deltaT*Fx;
  579. oNode.m_speed_y = oNode.m_speed_y*this.m_Kf + this.m_deltaT*Fy;
  580. var dx:Number = oNode.m_speed_x * this.m_deltaT;
  581. var dy:Number = oNode.m_speed_y * this.m_deltaT;
  582. oNode.x = Math.round(oNode.x + dx);
  583. oNode.y = Math.round(oNode.y + dy);
  584. //trace('object '+i+' moves (Force: Fx='+Fx+', Fy='+Fy+')! ');
  585. }
  586. }
  587. }
  588. //trace('======= Updated Positions =========');
  589. //DumpPositions();
  590. //trace('====== END UpdatePositions() =======');
  591. }
  592. public function DrawForces(aForces:Array, color:uint)
  593. {
  594. for (var i:String in aForces)
  595. {
  596. var oNode:GraphNode = GetNode(i);
  597. var oForce:Object = aForces[i];
  598. m_oCanvas.graphics.lineStyle(2,color,100,false,"normal",CapsStyle.ROUND);
  599. m_oCanvas.graphics.moveTo(oNode.x, oNode.y);
  600. var oEndPoint:Point = new Point;
  601. oEndPoint.x = Math.round(oNode.x + oForce.FxTotal);
  602. oEndPoint.y = Math.round(oNode.y + oForce.FyTotal);
  603. m_oCanvas.graphics.lineTo(oEndPoint.x, oEndPoint.y);
  604. drawArrow(oEndPoint, oForce.FxTotal, oForce.FyTotal, color);
  605. //trace('Drawinf vector '+i+': (x='+oNode.x+', y='+oNode.y+') to (x='+oEndPoint.x+', y='+oEndPoint.y+')');
  606. }
  607. }
  608. public function UpdatePanAndZoom(countDownRatio:Number)
  609. {
  610. var sceneRect:Rectangle = null;
  611. for(var i:String in this.m_aNodes)
  612. {
  613. if (sceneRect == null)
  614. {
  615. sceneRect = GetNode(i).getBounds(m_oCanvas);
  616. }
  617. else
  618. {
  619. sceneRect = sceneRect.union(GetNode(i).getBounds(m_oCanvas));
  620. }
  621. }
  622. if (sceneRect != null)
  623. {
  624. var idealZoomLevel:Number = 1;
  625. trace('Stage dimensions: width:'+stage.stageWidth+' height:'+stage.stageHeight);
  626. if ((sceneRect.width > stage.stageWidth) || (sceneRect.height > (stage.stageHeight - 50)))
  627. {
  628. var wRatio:Number = stage.stageWidth / sceneRect.width;
  629. var hRatio:Number = (stage.stageHeight - 50) / sceneRect.height;
  630. idealZoomLevel = Math.min(wRatio, hRatio);
  631. SetZoomLevel(idealZoomLevel);
  632. m_oZoomSlider.value = Math.round(100*idealZoomLevel);
  633. }
  634. var xOffset:Number = 0;
  635. var yOffset:Number = 50;
  636. if (stage.stageWidth > sceneRect.width)
  637. {
  638. xOffset = (stage.stageWidth-sceneRect.width)/2
  639. }
  640. if (stage.stageHeight > sceneRect.height)
  641. {
  642. yOffset = 50 + (stage.stageHeight-50-sceneRect.height)/2
  643. }
  644. m_oCanvas.x = xOffset-sceneRect.x;
  645. m_oCanvas.y = yOffset-sceneRect.y;
  646. trace('Scene bounding rect: x:'+sceneRect.x+' y:'+sceneRect.y+' width:'+sceneRect.width+' height:'+sceneRect.height+' zoomLevel:'+idealZoomLevel);
  647. }
  648. }
  649. public function DumpPositions()
  650. {
  651. for (var i:String in this.m_aNodes)
  652. {
  653. var oNode:GraphNode = GetNode(i);
  654. trace(i+' Position: (x='+oNode.x+', y='+oNode.y+')');
  655. }
  656. }
  657. }
  658. }
  659. class Link extends Object
  660. {
  661. protected var m_sStart:String;
  662. protected var m_sEnd:String;
  663. public function Link(sStartNodeKey:String, sEndNodeKey:String)
  664. {
  665. m_sStart = sStartNodeKey;
  666. m_sEnd = sEndNodeKey;
  667. }
  668. public function GetStart():String
  669. {
  670. return m_sStart;
  671. }
  672. public function GetEnd():String
  673. {
  674. return m_sEnd;
  675. }
  676. }
  677. import flash.display.*;
  678. import flash.geom.*;
  679. import flash.events.*;
  680. class NavigatorCanvas extends Sprite
  681. {
  682. public function NavigatorCanvas()
  683. {
  684. }
  685. }