PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/mstandio/UniversalMap/UniversalMap.as

http://universalmap.googlecode.com/
ActionScript | 430 lines | 300 code | 68 blank | 62 comment | 68 complexity | a168e24589bd057661eea4bc3682e3c8 MD5 | raw file
  1. package plugins.mstandio.UniversalMap {
  2. /**
  3. * UniversalMap class reads settings from xml file, and builds structures containing those settings
  4. * Creates base interface and adds hide/show functionalities
  5. * Reacts to space changes, gets changes data and sends it down to through MapViewer to NavigationPoint
  6. * @author mstandio
  7. */
  8. import flash.system.ApplicationDomain;
  9. import zephyr.BroadcastEvent;
  10. import gs.TweenLite;
  11. import flash.display.Sprite;
  12. import flash.display.StageDisplayState;
  13. import flash.events.Event;
  14. import flash.display.Bitmap;
  15. import flash.events.MouseEvent;
  16. import flash.net.URLRequest;
  17. import plugins.mstandio.UniversalMap.navigation.*;
  18. import plugins.mstandio.utils.combobox.ComboBox;
  19. import plugins.mstandio.utils.combobox.ComboBoxStyle;
  20. import plugins.mstandio.utils.combobox.ComboBoxEvent;
  21. public class UniversalMap extends Sprite {
  22. [Embed(source="images/interface/universalMap_icon.png")]
  23. public var Bitmap_universalMapIcon:Class;
  24. [Embed(source="images/interface/universalMap_close3.png")]
  25. public var Bitmap_universalMapClose:Class;
  26. private var mapWindowWidth:Number;
  27. private var mapWindowHeight:Number;
  28. public static var mapAlpha:Number;
  29. private var mapVisible:Boolean;
  30. private var verticalAlign:String;
  31. private var horizontalAlign:String;
  32. private var margin:Array;
  33. private var iconVisible:Boolean;
  34. private var comboBoxVisible:Boolean;
  35. public static var useLoadSpacePlus:Boolean;
  36. private var maps:Array;
  37. private var universalMapIcon:Sprite;
  38. private var mapWindow:Sprite;
  39. private var mapViewer:MapViewer;
  40. private var comboBox:ComboBox;
  41. private var closeIcon:Sprite;
  42. private var currentSpace:String;
  43. private var camInit:Object;
  44. private var mapChanged:Boolean;
  45. private var spaceChanged:Boolean; // change from "space.preview" to "space" is not a change
  46. private var firstBuild:Boolean;
  47. private var BroadcastEvent:Class;
  48. private var ModuleLoader:Class;
  49. private var moduleLoader:Object;
  50. /**
  51. * Constructor
  52. */
  53. public function UniversalMap() {
  54. if (stage) stageReady();
  55. else addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);
  56. }
  57. private function stageReady(e:Event = null):void {
  58. removeEventListener(Event.ADDED_TO_STAGE, stageReady);
  59. this.firstBuild = true;
  60. BroadcastEvent = ApplicationDomain.currentDomain.getDefinition("zephyr.BroadcastEvent") as Class;
  61. ModuleLoader = ApplicationDomain.currentDomain.getDefinition("ModuleLoader") as Class;
  62. moduleLoader = ModuleLoader(parent);
  63. moduleLoader.addEventListener(BroadcastEvent.SPACE_LOADED, getCurrentSpace, false, 0, true);
  64. moduleLoader.addEventListener(BroadcastEvent.CAMERA_INIT, getCamInit, false, 0, true);
  65. moduleLoader.addEventListener(BroadcastEvent.ALL_LAYERS_LOADED, layersReady, false, 0, true);
  66. moduleLoader.addEventListener(BroadcastEvent.UNIVERSALMAP_TOGGLE, toggleShowWindow, false, 0, true);
  67. // this is needed for keeping track on camera direction when switching between maps
  68. moduleLoader.addEventListener(BroadcastEvent.CAMERA_SPIN, camSpin, false, 0, true);
  69. moduleLoader.addEventListener(BroadcastEvent.CAMERA_ZOOM, camZoom, false, 0, true);
  70. }
  71. // ALL_LAYERS_LOADED -> SPACE_LOADED -> CAMERA_INIT -> on first load
  72. // SPACE_LOADED -> CAMERA_INIT otherwise
  73. private function getCurrentSpace(e:zephyr.BroadcastEvent):void {
  74. //Trace.add("SPACE_LOADED");
  75. if(this.currentSpace){
  76. if (this.getMapLabelFromSpace(this.currentSpace) == this.getMapLabelFromSpace(e.info.spaceLoaded) || (this.getMapLabelFromSpace(e.info.spaceLoaded) == this.comboBox.getActive().label) ) {
  77. this.mapChanged = false
  78. }else {
  79. this.mapChanged = true;
  80. }
  81. if (this.currentSpace == e.info.spaceLoaded + ".preview") {
  82. this.spaceChanged=false
  83. }else {
  84. this.spaceChanged = true;
  85. }
  86. }else {
  87. this.spaceChanged = true;
  88. this.mapChanged = true;
  89. }
  90. this.currentSpace = e.info.spaceLoaded;
  91. }
  92. private function getCamInit(e:zephyr.BroadcastEvent):void {
  93. //Trace.add("CAMERA_INIT");
  94. this.camInit = e.info;
  95. if(this.spaceChanged){
  96. this.buildMap();
  97. }
  98. }
  99. private function layersReady(e:zephyr.BroadcastEvent):void {
  100. //Trace.add("ALL_LAYERS_LOADED");
  101. this.getSettings();
  102. this.buildAll();
  103. this.handleStageResize();
  104. }
  105. private function camSpin(e:zephyr.BroadcastEvent):void {
  106. this.camInit.pan=e.info.spin.split(",")[1];
  107. this.camInit.tilt=e.info.spin.split(",")[0];
  108. }
  109. private function camZoom(e:zephyr.BroadcastEvent):void {
  110. this.camInit.zoom=e.info.zoom;
  111. }
  112. /**
  113. * Reads general plugin settings specified in xml file
  114. * Builds array of maps each containing array of navigation points
  115. */
  116. private function getSettings():void {
  117. var settings:XML = moduleLoader.xmlByName["UniversalMap"];
  118. // setting map window size, definig default size if attributes not present
  119. this.mapWindowWidth = (settings.@mapWindowWidth != undefined) ? Number(settings.@mapWindowWidth) : 400 ;
  120. this.mapWindowHeight = (settings.@mapWindowHeight != undefined) ? Number(settings.@mapWindowHeight) : 300 ;
  121. mapAlpha = (settings.@mapAlpha != undefined) ? Number(settings.@mapAlpha) : 1 ;
  122. this.mapVisible = (settings.@initailVisibility != undefined) ? settings.@initailVisibility == "true" : true ;
  123. this.verticalAlign = (settings.@verticalAlign != undefined) ? settings.@verticalAlign : "center" ;
  124. this.horizontalAlign = (settings.@horizontalAlign != undefined) ? settings.@horizontalAlign : "center" ;
  125. this.margin = (settings.@margin != undefined) ? String(settings.@margin).split(",") : new Array(0, 0, 0, 0) ;
  126. this.iconVisible = (settings.@iconVisible != undefined) ? settings.@iconVisible == "true" : true ;
  127. this.comboBoxVisible = (settings.@comboBoxVisible != undefined) ? settings.@comboBoxVisible == "true" : true ;
  128. useLoadSpacePlus = (settings.@useLoadSpacePlus != undefined) ? settings.@useLoadSpacePlus == "true" : false;
  129. // building array of maps
  130. this.maps = new Array();
  131. for each(var xmlMap:XML in settings.elements()) {
  132. // setting single map parameters, defining default values
  133. var mapLabel:String = (xmlMap.@label != undefined) ? String(xmlMap.@label) : "label_empty_"+xmlMap.childIndex();
  134. var mapUrl:String = (xmlMap.@url != undefined) ? String(xmlMap.@url) : "_empty";
  135. var mapAllowZoom:Boolean = (xmlMap.@url != undefined) ? xmlMap.@allowZoom=="true" : false;
  136. // building array of points for each map
  137. var navigationPoints:Array = new Array();
  138. for each(var xmlPoint:XML in xmlMap.elements()) {
  139. navigationPoints.push(new NavigationPoint(
  140. Number((xmlPoint.@x != undefined) ? xmlPoint.@x : 0),
  141. Number((xmlPoint.@y != undefined) ? xmlPoint.@y : 0),
  142. Number((xmlPoint.@panShift != undefined) ? xmlPoint.@panShift : 0),
  143. String((xmlPoint.@target != undefined) ? xmlPoint.@target : "_empty"),
  144. String((xmlPoint.@type != undefined) ? xmlPoint.@type : ((xmlMap.@type != undefined) ? xmlMap.@type : "Cam_default")),
  145. this.validateSize(Number((xmlPoint.@size != undefined) ? xmlPoint.@size : ((xmlMap.@size != undefined) ? xmlMap.@size : 100))),
  146. this.validateColor(String((xmlPoint.@color != undefined) ? xmlPoint.@color : ((xmlMap.@color != undefined) ? xmlMap.@color : "#00FF00")))
  147. ));
  148. }
  149. maps.push(new Map(mapLabel, mapUrl, navigationPoints, mapAllowZoom));
  150. }
  151. }
  152. private function validateSize(size:Number):Number {
  153. return (Math.abs(size/100) <= 1.6) ? Math.abs(size/100) : 1.6 ;
  154. }
  155. private function validateColor(color:String):String {
  156. return "0x"+color.substring( 1 ) ;
  157. }
  158. /**
  159. * Places map elements inside map window
  160. */
  161. private function buildAll():void {
  162. // adds icon to upper right corner of the application
  163. if (this.iconVisible) {
  164. this.universalMapIcon = new Sprite();
  165. var u_i:Bitmap = new Bitmap(new Bitmap_universalMapIcon().bitmapData);
  166. this.universalMapIcon.addChild(u_i);
  167. this.universalMapIcon.x = this.stage.stageWidth - this.universalMapIcon.width;
  168. this.universalMapIcon.addEventListener(MouseEvent.CLICK, toggleShowWindow);
  169. this.universalMapIcon.buttonMode = true;
  170. this.universalMapIcon.alpha = 0; // initially hidden
  171. this.universalMapIcon.visible = false;
  172. this.addChild(universalMapIcon);
  173. }
  174. // draw map window
  175. this.mapWindow = new Sprite();
  176. this.mapWindow.graphics.beginFill(0xFFFFFF);
  177. this.mapWindow.graphics.drawRect(0, 0, this.mapWindowWidth, this.mapWindowHeight);
  178. this.mapWindow.graphics.endFill();
  179. this.mapWindow.alpha = 0;
  180. this.mapWindow.visible = false; // initially hidden
  181. this.addChild(this.mapWindow);
  182. // draw combobox
  183. var labels:Array = new Array();
  184. for each (var map:Map in maps) {
  185. labels.push({label:map.label});
  186. }
  187. var comboBoxStyle:ComboBoxStyle = new ComboBoxStyle();
  188. comboBoxStyle.initiallyBlank = true;
  189. comboBoxStyle.elementSpacing = -1;
  190. comboBoxStyle.elementAlpha = 1 / mapAlpha;
  191. comboBoxStyle.mainElementAlpha = 1 / mapAlpha;
  192. comboBoxStyle.buttonTriangleColor = 0xcacaca;
  193. comboBoxStyle.buttonBackgroundColor = 0xf5f5f5;
  194. this.comboBox = new ComboBox(labels,comboBoxStyle);
  195. this.comboBox.visible = this.comboBoxVisible;
  196. this.comboBox.x = this.comboBox.y = 4; // initial position before map is loaded
  197. this.mapWindow.addChild(this.comboBox);
  198. this.mapWindow.addEventListener(ComboBoxEvent.LABEL_CHANGED, comboBoxLabelChanged);
  199. // draw close icon
  200. this.closeIcon = new Sprite();
  201. var c_i:Bitmap = new Bitmap(new Bitmap_universalMapClose().bitmapData);
  202. this.closeIcon.addChild(c_i);
  203. this.closeIcon.addEventListener(MouseEvent.CLICK, toggleShowWindow);
  204. this.closeIcon.buttonMode = true;
  205. this.closeIcon.x = this.mapWindow.width - this.closeIcon.width -4; // right upper corner of map window
  206. this.closeIcon.y = 4;
  207. this.closeIcon.alpha = 1/mapAlpha; // so it wont be ever transparent
  208. this.mapWindow.addChild(this.closeIcon);
  209. // reposition map window after fullscreen
  210. this.stage.addEventListener(Event.RESIZE, this.handleStageResize);
  211. }
  212. /**
  213. * Function is casted after camera initiation, so works either for navigation points inside map
  214. * and navigation from auter apllication
  215. * @param e
  216. */
  217. private function buildMap(label:String=null):void {
  218. // properties of initiated camera that are passed down to navigation point
  219. var params:NavigationPointParameters = new NavigationPointParameters(this.currentSpace, this.camInit.pan, this.camInit.tilt, this.camInit.zoom);
  220. var newMap:String = this.getMapLabelFromSpace(this.currentSpace);
  221. if (this.firstBuild) {
  222. this.mapViewer = new MapViewer(this.getMapfromLabel(newMap), this.mapWindowWidth, this.mapWindowHeight, params);
  223. this.mapWindow.addChild(this.mapViewer);
  224. this.mapWindow.setChildIndex(this.mapViewer, 0);
  225. this.comboBox.setSelected(newMap);
  226. }
  227. // go to map of specified label
  228. else if (label) {
  229. this.mapViewer.changeMap(this.getMapfromLabel(label), params);
  230. }
  231. // go to different navigation point in the same map
  232. else if(!this.mapChanged){
  233. this.mapViewer.updateNavigationPoints(params);
  234. }
  235. // go to navigation point on diferent map
  236. else {
  237. this.mapViewer.changeMap(this.getMapfromLabel(newMap), params);
  238. this.comboBox.setSelected(newMap); // does not cast comboBoxLabelChanged
  239. }
  240. this.mapChanged = false;
  241. }
  242. /**
  243. * Casted whenever combobox is clicked
  244. * @param e
  245. */
  246. private function comboBoxLabelChanged(e:ComboBoxEvent):void {
  247. this.buildMap(e.info.label);
  248. }
  249. /**
  250. * Refreshes map position after enetering and leaving fullscreen
  251. * @param e
  252. */
  253. private function handleStageResize(e:Event = null):void {
  254. var resultX:Number =0;
  255. var resultY:Number =0;
  256. switch (this.horizontalAlign) {
  257. case "left": {
  258. resultX = 0;
  259. }break;
  260. case "right": {
  261. resultX = this.stage.stageWidth - this.mapWindowWidth;
  262. }break;
  263. default: { // "center"
  264. resultX = this.stage.stageWidth * 0.5 - this.mapWindowWidth * 0.5;
  265. }
  266. }
  267. resultX -= Number(this.margin[1])// move right
  268. resultX += Number(this.margin[3])// move left
  269. switch (this.verticalAlign) {
  270. case "top": {
  271. resultY = 0;
  272. }break;
  273. case "bottom": {
  274. resultY = this.stage.stageHeight - this.mapWindowHeight;
  275. }break;
  276. default: { // "center"
  277. resultY = this.stage.stageHeight * 0.5 - this.mapWindowHeight * 0.5;
  278. }
  279. }
  280. resultY += Number(this.margin[0])// move up
  281. resultY -= Number(this.margin[2])// move down
  282. this.mapWindow.x = resultX;
  283. this.mapWindow.y = resultY;
  284. if(this.iconVisible){
  285. this.universalMapIcon.x = this.stage.stageWidth - this.universalMapIcon.width;
  286. }
  287. }
  288. /**
  289. * Toggles showing / hiding of map window
  290. * @param e
  291. */
  292. private function toggleShowWindow(e:Event = null): void {
  293. var windowsSpeed:Number = 0.5;
  294. TweenLite.killDelayedCallsTo(toggleShowWindowConfirm);
  295. TweenLite.delayedCall(windowsSpeed, toggleShowWindowConfirm, [this.mapVisible]);
  296. if (this.mapVisible) {
  297. if(this.iconVisible){
  298. this.universalMapIcon.visible = true;
  299. TweenLite.to(this.universalMapIcon, windowsSpeed, { alpha: 1 } );
  300. }
  301. TweenLite.to(this.mapWindow, windowsSpeed, { alpha: 0 } );
  302. }else {
  303. this.mapWindow.visible = true;
  304. if(this.iconVisible){
  305. TweenLite.to(this.universalMapIcon, windowsSpeed, { alpha: 0 } );
  306. }
  307. TweenLite.to(this.mapWindow, windowsSpeed, { alpha: mapAlpha } );
  308. }
  309. moduleLoader.dispatchEvent(new BroadcastEvent(BroadcastEvent.UNIVERSALMAP_CHANGED, {visible:!this.mapVisible}));
  310. }
  311. private function toggleShowWindowConfirm(visible:Boolean):void {
  312. if (visible) {
  313. this.mapWindow.visible = false;
  314. this.mapVisible = false;
  315. }else {
  316. if(this.iconVisible){
  317. this.universalMapIcon.visible = false;
  318. }
  319. this.mapVisible = true;
  320. }
  321. }
  322. public function firstShowWindow():void {
  323. if (this.firstBuild && this.mapVisible) {
  324. this.mapVisible = false;
  325. this.toggleShowWindow();
  326. }
  327. this.firstBuild = false;
  328. }
  329. /**
  330. * Serches navigation points in each map for specified targetspace
  331. * @param space
  332. * @return
  333. */
  334. private function getMapLabelFromSpace(space:String):String {
  335. for each(var map:Map in maps) {
  336. for each(var point:NavigationPoint in map.navigationPoints) {
  337. if ((point.targetSpace == space) || (point.targetSpace == space+".preview") || (point.targetSpace+".preview" == space)){
  338. return map.label;
  339. }
  340. }
  341. }
  342. return Map(maps[0]).label;
  343. }
  344. /**
  345. * Returns map of specified label
  346. * @param label
  347. * @return
  348. */
  349. private function getMapfromLabel(label:String):Map {
  350. for each(var map:Map in maps) {
  351. if (map.label == label) {
  352. return map;
  353. }
  354. }
  355. return maps[0];
  356. }
  357. public function placeComboBox():void {
  358. this.comboBox.y = 4;
  359. this.comboBox.x = (this.mapViewer.getDragActive()) ? this.mapViewer.getNavigationWidth() + 10 : 4 ;
  360. }
  361. }
  362. }