Add sublayers (stacking order) for stroke drawing
[potlatch2.git] / net / systemeD / halcyon / styleparser / RuleSet.as
1 package net.systemeD.halcyon.styleparser {
2
3         import org.as3yaml.*;
4         import flash.events.*;
5         import flash.net.*;
6         import net.systemeD.halcyon.Globals;
7
8         public class RuleSet {
9
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)
14
15                 // variables for name, author etc.
16
17                 public function RuleSet(f:Function=null):void {
18                         iconCallback=f;
19                 }
20
21                 // returns array of ShapeStyle,PointStyle,TextStyle,ShieldStyle
22                 public function getStyle(isPoint:Boolean,tags:Object,scale:uint):Array {
23                         var ss:ShapeStyle;
24                         var ps:PointStyle;
25                         var ts:TextStyle;
26                         var hs:ShieldStyle;
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; }
38                                 }
39                         }
40                         return new Array(ss,ps,ts,hs);
41                 }
42
43                 // Save and load rulesets
44
45                 public function save(url:String,name:String):void {
46
47                         var request:URLRequest=new URLRequest(url);
48                         var requestVars:URLVariables=new URLVariables();
49                         var loader:URLLoader=new URLLoader();
50                         
51                         // send to server
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);
61                         loader.load(request);
62                 }
63
64                 public function load(url:String):void {
65
66                         var request:URLRequest=new URLRequest(url);
67                         var loader:URLLoader=new URLLoader();
68
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);
75                         loader.load(request);
76                 }
77
78                 // data handlers
79                 private function savedRuleSet(event:Event):void {
80                         var loader:URLLoader = URLLoader(event.target);  
81                         // do something with loader.data
82                 }
83                 
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
88                         loadImages();
89                 }
90
91                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
92                 private function securityErrorHandler( event:SecurityErrorEvent ):void { }
93                 private function ioErrorHandler( event:IOErrorEvent ):void { }
94                 
95                 // serialise/deserialise methods
96
97
98                 // ------------------------------------------------------------------------------------------------
99                 // Load all referenced images
100                 // ** currently only looks in PointRules
101                 // ** will duplicate if referenced twice, shouldn't
102                 
103                 public function loadImages():void {
104                         var ps:PointStyle;
105
106                         for each (var rule:* in rules) {
107                                 if (!(rule is PointRule)) { continue; }
108                                 if (!(rule.pointStyle)) { continue; }
109                                 if (!(rule.pointStyle.icon)) { continue; }
110                                 
111                                 iconsToLoad++;
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);
121                         }
122                 }
123
124                 // data handler
125
126                 private function loadedImage(event:Event):void {
127                         images[event.target.filename]=event.target.data;
128                         iconsToLoad--;
129                         if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
130                 }
131         }
132 }