1 package net.systemeD.halcyon.styleparser {
6 import net.systemeD.halcyon.Globals;
10 public var rules:Array=new Array(); // list of rules
11 public var images:Object=new Object(); // loaded images
12 private var iconCallback:Function=null; // function to call when all icons loaded
13 private var iconsToLoad:uint=0; // number of icons left to load (fire callback when ==0)
15 // variables for name, author etc.
17 public function RuleSet(f:Function=null):void {
21 // returns array of ShapeStyle,PointStyle,TextStyle,ShieldStyle
22 public function getStyle(isPoint:Boolean,tags:Object,scale:uint):Array {
27 for each (var rule:* in rules) {
28 if ( isPoint && rule is ShapeRule) { continue; }
29 if (!isPoint && rule is PointRule) { continue; }
30 if (scale>rule.minScale) { continue; }
31 if (scale<rule.maxScale) { continue; }
32 if (rule.test(tags)) {
33 if (rule is ShapeRule && rule.shapeStyle) { ss=rule.shapeStyle; }
34 if (rule is PointRule && rule.pointStyle) { ps=rule.pointStyle; }
35 if ( rule.textStyle ) { ts=rule.textStyle; }
36 if (rule is ShapeRule && rule.shieldStyle) { hs=rule.shieldStyle; }
37 if (rule.breaker) { break; }
40 return new Array(ss,ps,ts,hs);
43 // Save and load rulesets
45 public function save(url:String,name:String):void {
47 var request:URLRequest=new URLRequest(url);
48 var requestVars:URLVariables=new URLVariables();
49 var loader:URLLoader=new URLLoader();
52 requestVars['name']=name;
53 requestVars['data']=YAML.encode(rules);
54 request.data=requestVars;
55 request.method = URLRequestMethod.POST;
56 loader.dataFormat = URLLoaderDataFormat.TEXT;
57 loader.addEventListener(Event.COMPLETE, savedRuleSet, false, 0, true);
58 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
59 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
60 loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
64 public function load(url:String):void {
66 var request:URLRequest=new URLRequest(url);
67 var loader:URLLoader=new URLLoader();
69 request.method=URLRequestMethod.GET;
70 loader.dataFormat = URLLoaderDataFormat.TEXT;
71 loader.addEventListener(Event.COMPLETE, loadedRuleSet, false, 0, true);
72 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
73 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
74 loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
79 private function savedRuleSet(event:Event):void {
80 var loader:URLLoader = URLLoader(event.target);
81 // do something with loader.data
84 private function loadedRuleSet(event:Event):void {
85 var loader:URLLoader = URLLoader(event.target);
86 rules=YAML.decode(event.target.data) as Array;
87 // ** fire some event or other to tell map to redraw
91 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
92 private function securityErrorHandler( event:SecurityErrorEvent ):void { }
93 private function ioErrorHandler( event:IOErrorEvent ):void { }
95 // serialise/deserialise methods
98 // ------------------------------------------------------------------------------------------------
99 // Load all referenced images
100 // ** currently only looks in PointRules
101 // ** will duplicate if referenced twice, shouldn't
103 public function loadImages():void {
106 for each (var rule:* in rules) {
107 if (!(rule is PointRule)) { continue; }
108 if (!(rule.pointStyle)) { continue; }
109 if (!(rule.pointStyle.icon)) { continue; }
112 var request:URLRequest=new URLRequest(rule.pointStyle.icon);
113 var loader:ImageLoader=new ImageLoader();
114 loader.dataFormat=URLLoaderDataFormat.BINARY;
115 loader.filename=rule.pointStyle.icon;
116 loader.addEventListener(Event.COMPLETE, loadedImage, false, 0, true);
117 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
118 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
119 loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
120 loader.load(request);
126 private function loadedImage(event:Event):void {
127 images[event.target.filename]=event.target.data;
129 if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }