Posted By

burnandbass on 08/10/11


Tagged

model as3


Versions (?)

Model


 / Published in: ActionScript 3
 

  1. package bassta.model{
  2.  
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.events.Event;
  6. import flash.events.EventDispatcher;
  7. import flash.display.LoaderInfo;
  8. import flash.text.StyleSheet;
  9.  
  10. import com.greensock.loading.LoaderMax;
  11. import com.greensock.loading.ImageLoader;
  12. import com.greensock.loading.XMLLoader;
  13. import com.greensock.loading.CSSLoader;
  14.  
  15. import com.greensock.events.LoaderEvent;
  16. import bassta.events.ModelEvent;
  17. import bassta.events.PageEvent;
  18.  
  19. import bassta.routing.Router;
  20. import bassta.structure.Initializer;
  21. import bassta.utils.Hash;
  22. import flash.net.URLRequest;
  23. import flash.external.ExternalInterface;
  24.  
  25. public class Model{
  26.  
  27. private const SINGLETON_MSG:String = 'Model singleton already constructed!';
  28.  
  29. private static var _instance:Model;
  30.  
  31. private var _loader:LoaderMax;
  32. private var _xmlInitialLoader:LoaderMax;
  33.  
  34. private var _process:Function;
  35. private var hasProcessingFunction:Boolean = false;
  36.  
  37. private var _data:XML;
  38. private var _dispatcher:EventDispatcher = new EventDispatcher();
  39.  
  40. public static var useSWFAddress:Boolean = true;
  41. private var _embededVariables:Hash = new Hash();
  42.  
  43. public function Model(se:SingletonEnforcer) {
  44. if (_instance != null){
  45. throw new Error(SINGLETON_MSG);
  46. }
  47. _instance = this;
  48. initModel();
  49. }
  50.  
  51. public static function getInstance():Model {
  52. if (_instance == null){
  53. _instance = new Model(new SingletonEnforcer());
  54. }
  55. return _instance;
  56. }
  57.  
  58. private function initModel():void{
  59. var variables:Object = LoaderInfo(Initializer.instance.root.loaderInfo).parameters;
  60. for (var name:String in variables){
  61. _embededVariables[name] = variables[name];
  62. }
  63. LoaderMax.activate([XMLLoader, ImageLoader, CSSLoader]);
  64. }
  65.  
  66. /*
  67. Call this function to load external xml,
  68. @_xmlPath - url to the XML file
  69. @process - function to be executed when the XML is loaded, probably load some pictures
  70. using the "addInternalImageLoader" method
  71. */
  72. public function loadInitialXML(_xmlPath:String, process:Function = null):void{
  73. _process = process;
  74. if(_process != null){
  75. hasProcessingFunction = true;
  76. }
  77.  
  78. _xmlInitialLoader = new LoaderMax();
  79. _xmlInitialLoader.append(new XMLLoader(_xmlPath, {name:"XMLData", noCache:true}));
  80. _xmlInitialLoader.addEventListener(LoaderEvent.PROGRESS, onXMLLoadingProgress);
  81. _xmlInitialLoader.addEventListener(LoaderEvent.COMPLETE, onXMLLoadingComplete);
  82. _xmlInitialLoader.addEventListener(LoaderEvent.ERROR, onXMLLoadingError);
  83. _xmlInitialLoader.load(false);
  84. }
  85.  
  86. private function onXMLLoadingProgress(event:LoaderEvent):void{
  87. var xmlPercentLoaded:Number = Math.ceil(event.target.progress*100);
  88.  
  89. if(hasProcessingFunction){
  90. _dispatcher.dispatchEvent(new ModelEvent(ModelEvent.PROGRESS, Number(xmlPercentLoaded*0.1)));
  91. } else {
  92. _dispatcher.dispatchEvent(new ModelEvent(ModelEvent.PROGRESS, Number(xmlPercentLoaded)));
  93. }
  94. }
  95.  
  96. private function onXMLLoadingError(event:LoaderEvent):void{
  97. trace("[Model]:: ERROR :: onXMLLoadingError function");
  98. }
  99.  
  100. private function onXMLLoadingComplete(event:LoaderEvent):void{
  101. _data = new XML(_xmlInitialLoader.getContent("XMLData"));
  102. _xmlInitialLoader.removeEventListener(LoaderEvent.PROGRESS, onXMLLoadingProgress);
  103. _xmlInitialLoader.removeEventListener(LoaderEvent.COMPLETE, onXMLLoadingComplete);
  104. _xmlInitialLoader.removeEventListener(LoaderEvent.ERROR, onXMLLoadingError);
  105. _xmlInitialLoader.unload();
  106. _xmlInitialLoader.dispose(true);
  107. _dispatcher.dispatchEvent(new ModelEvent(ModelEvent.XML_ASSIGNED));
  108. _dispatcher.dispatchEvent(new ModelEvent(ModelEvent.XML_COMPLETE));
  109.  
  110. if(hasProcessingFunction){
  111. _loader = new LoaderMax();
  112. _loader.addEventListener(LoaderEvent.PROGRESS, onExternallProgress);
  113. _loader.addEventListener(LoaderEvent.ERROR, onExternallError);
  114. _loader.addEventListener(LoaderEvent.COMPLETE, onAllComplete);
  115. _process.call(this);
  116. _loader.load();
  117. } else {
  118. dispatchAllComplete();
  119. }
  120. }
  121.  
  122. public function dispatchAllComplete():void{
  123.  
  124. if(ExternalInterface.available){
  125. ExternalInterface.call("switchColors");
  126. }
  127.  
  128. if(Model.useSWFAddress){
  129. //little trick, wait for SWFAddress Event, so the new page is recognised, than dispatch event
  130. Router.addEventListener(PageEvent.CHANGE, function():void{
  131. _dispatcher.dispatchEvent(new ModelEvent(ModelEvent.ALL_COMPLETE));
  132. });
  133. Router.init();
  134. } else {
  135. _dispatcher.dispatchEvent(new ModelEvent(ModelEvent.ALL_COMPLETE));
  136. }
  137. }
  138.  
  139. public function onExternallProgress(event:LoaderEvent):void{
  140. var allPercentLoaded:Number = 10 + (Math.ceil(event.target.progress*100)*0.9);
  141. _dispatcher.dispatchEvent(new ModelEvent(ModelEvent.PROGRESS, allPercentLoaded));
  142. }
  143.  
  144. public function onExternallError(event:LoaderEvent):void{
  145. trace("[Model]:: ERROR :: onExternallError function");
  146. }
  147.  
  148. public function onAllComplete(event:LoaderEvent):void{
  149. _loader.removeEventListener(LoaderEvent.PROGRESS, onExternallProgress);
  150. _loader.removeEventListener(LoaderEvent.ERROR, onExternallError);
  151. _loader.removeEventListener(LoaderEvent.COMPLETE, onAllComplete);
  152.  
  153. dispatchAllComplete();
  154. }
  155.  
  156. //////////// Addng external images, retriving content from the loader /////////////
  157.  
  158. /*
  159. Add image to be loaded, simplifies loading images that will be needed in the whole project
  160. */
  161. public function addInternalImageLoader(url:String, parameters:Object = null):void{
  162. _loader.append(new ImageLoader(url, parameters));
  163. }
  164.  
  165. /*
  166. @nameOrURL - url of loaded image or "name" if set
  167. */
  168. public function getLoaderBitmap(nameOrURL:String):Bitmap{
  169. return (getContent(nameOrURL).rawContent as Bitmap);
  170. }
  171.  
  172. /*
  173. Same as getLoaderBitmap, but returns the BitmapData
  174. */
  175. public function getLoaderBitmapData(nameOrURL:String):BitmapData{
  176. return (getContent(nameOrURL).rawContent as Bitmap).bitmapData;
  177. }
  178.  
  179. /*
  180. Returns XML if any was added in the process function
  181. */
  182. public function getLoaderXML(nameOrURL:String):XML{
  183. return new XML(_loader.getLoader(nameOrURL).content);
  184. }
  185.  
  186. /*
  187. Retrives any loader that was added
  188. */
  189. public function getInternalLoader(nameOrURL:String):*{
  190. return _loader.getLoader(nameOrURL);
  191. }
  192.  
  193. /*
  194. Retrives any conted that was added
  195. */
  196. public function getContent(nameOrURL:String):*{
  197. return _loader.getContent(nameOrURL);
  198. }
  199.  
  200. public function addInternalCSS(url:String, parameters:Object = null):void{
  201. _loader.append(new CSSLoader(url, parameters));
  202. }
  203.  
  204. public function getLoaderCSS(nameOrURL:String):StyleSheet{
  205. return LoaderMax.getContent(nameOrURL) as StyleSheet
  206. }
  207.  
  208. ///////////////////////////////////////// IEventDispatcher ///////////////////////////////////////////////////
  209.  
  210. public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeak:Boolean = true):void {
  211. _dispatcher.addEventListener(type, listener, useCapture, priority, useWeak);
  212. }
  213.  
  214. public function removeEventListener(type:String, listener:Function):void {
  215. _dispatcher.removeEventListener(type, listener);
  216. }
  217.  
  218. public function dispatchEvent(event:Event):void {
  219. _dispatcher.dispatchEvent(event);
  220. }
  221.  
  222. //////////////////////////////////////// Culture /////////////////////////////////////////////////////////////
  223.  
  224. public function i18nContent(xml_list:*, culture:String = null):String{
  225. if(!culture){
  226. culture = Router.language;
  227. }
  228. if (xml_list is XMLList){
  229. xml_list = xml_list.(@lang == culture);
  230. return xml_list && xml_list.length() ? xml_list[0] : 'not translated'
  231. }
  232. return xml_list;
  233. }
  234.  
  235. //////////////// USEFULL XML FUNCTIONS ////////////////////////////////////////////////////////////////////
  236.  
  237. /*
  238. Easy function to get any XMLList, just pass the nodename
  239. */
  240. public function list(nodeName:String):XMLList{
  241. return xmlData..*.(name() == nodeName);
  242. }
  243.  
  244. /*
  245. Easy get any XML
  246. @nodeName - name of the node
  247. @id - the id tag of the node, must have
  248. @offset - offset of the node, 1 will return the next node, -1 - the previous
  249. */
  250. public function element(nodeName:String, id:String = null, offset:int = 0):XML {
  251. var xml:XML = xmlData..*.(name() == nodeName && attribute("id") == id)[0];
  252. if (offset == 0) {
  253. return xml;
  254. } else {
  255. var offset_xml:* = xml.parent().children()[xml.childIndex() + offset];
  256. return (offset_xml && offset_xml is XML && offset_xml.name() == nodeName) ? offset_xml : null;
  257. }
  258. }
  259.  
  260. /*
  261. Dictionary - returns XMLList in special "dictionary" node in the XML
  262. */
  263. public function dictionary(word:String):* {
  264. var dict_lsit:XMLList = xmlData.dictionary..*.(name() == word);
  265. if (dict_lsit.length == 1){
  266. return dict_lsit[0];
  267. }else{
  268. return dict_lsit;
  269. }
  270. }
  271.  
  272. //////////////////////////// SOME SETTERS ///////////////////////////////////////////
  273.  
  274. //////////////////////////// GETTER FUNCTIONS ///////////////////////////////////////
  275.  
  276. /*
  277. get the loaded initial XML
  278. */
  279. public function get xmlData():XML{
  280. return _data;
  281. }
  282.  
  283. /*
  284. get the LoaderMax that loads the images from the process function
  285. */
  286. public function get loader():LoaderMax {
  287. return _loader;
  288. }
  289.  
  290. /*
  291. get the model EventDispatcher
  292. */
  293. public function get dispatcher():EventDispatcher{
  294. return _dispatcher;
  295. }
  296.  
  297. /*
  298. Returns embeded variables, passed via swfobject
  299. */
  300. public function get embededVariables():Hash{
  301. return _embededVariables;
  302. }
  303.  
  304.  
  305. }//end class
  306. }
  307.  
  308. internal class SingletonEnforcer { }

Report this snippet  

You need to login to post a comment.