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
13 // variables for name, author etc.
15 // returns array of ShapeStyle,PointStyle,TextStyle,ShieldStyle
16 public function getStyle(isPoint:Boolean,tags:Object,scale:uint):Array {
21 for each (var rule:* in rules) {
22 if ( isPoint && rule is ShapeRule) { continue; }
23 if (!isPoint && rule is PointRule) { continue; }
24 if (scale>rule.minScale && !isPoint) { continue; }
25 if (scale<rule.maxScale && !isPoint) { continue; }
26 if (rule.test(tags)) {
27 if (rule is ShapeRule && rule.shapeStyle) { ss=rule.shapeStyle; }
28 if (rule is PointRule && rule.pointStyle) { ps=rule.pointStyle; }
29 if ( rule.textStyle ) { ts=rule.textStyle; }
30 if (rule is ShapeRule && rule.shieldStyle) { hs=rule.shieldStyle; }
31 if (rule.breaker) { break; }
34 return new Array(ss,ps,ts,hs);
37 // Save and load rulesets
39 public function save(url:String,name:String):void {
41 var request:URLRequest=new URLRequest(url);
42 var requestVars:URLVariables=new URLVariables();
43 var loader:URLLoader=new URLLoader();
46 requestVars['name']=name;
47 requestVars['data']=YAML.encode(rules);
48 request.data=requestVars;
49 request.method = URLRequestMethod.POST;
50 loader.dataFormat = URLLoaderDataFormat.TEXT;
51 loader.addEventListener(Event.COMPLETE, savedRuleSet, false, 0, true);
52 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
53 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
54 loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
58 public function load(url:String):void {
60 var request:URLRequest=new URLRequest(url);
61 var loader:URLLoader=new URLLoader();
63 request.method=URLRequestMethod.GET;
64 loader.dataFormat = URLLoaderDataFormat.TEXT;
65 loader.addEventListener(Event.COMPLETE, loadedRuleSet, false, 0, true);
66 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
67 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
68 loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
73 private function savedRuleSet(event:Event):void {
74 var loader:URLLoader = URLLoader(event.target);
75 // do something with loader.data
78 private function loadedRuleSet(event:Event):void {
79 var loader:URLLoader = URLLoader(event.target);
80 rules=YAML.decode(event.target.data) as Array;
81 // ** fire some event or other to tell map to redraw
85 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
86 private function securityErrorHandler( event:SecurityErrorEvent ):void { }
87 private function ioErrorHandler( event:IOErrorEvent ):void { }
89 // serialise/deserialise methods
92 // ------------------------------------------------------------------------------------------------
93 // Load all referenced images
94 // ** currently only looks in PointRules
95 // ** will duplicate if referenced twice, shouldn't
97 public function loadImages():void {
99 for each (var rule:* in rules) {
100 if (!(rule is PointRule)) { continue; }
101 if (!(rule.pointStyle)) { continue; }
102 if (!(rule.pointStyle.icon)) { continue; }
104 var request:URLRequest=new URLRequest(rule.pointStyle.icon);
105 var loader:ImageLoader=new ImageLoader();
106 loader.dataFormat=URLLoaderDataFormat.BINARY;
107 loader.filename=rule.pointStyle.icon;
108 loader.addEventListener(Event.COMPLETE, loadedImage, false, 0, true);
109 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
110 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
111 loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
112 loader.load(request);
118 private function loadedImage(event:Event):void {
119 Globals.vars.debug.appendText("Target is "+event.target+", name"+event.target.filename+"\n");
120 images[event.target.filename]=event.target.data;