basic POI rendering support
[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
13                 // variables for name, author etc.
14
15                 // returns array of ShapeStyle,PointStyle,TextStyle,ShieldStyle
16                 public function getStyle(isPoint:Boolean,tags:Object,scale:uint):Array {
17                         var ss:ShapeStyle;
18                         var ps:PointStyle;
19                         var ts:TextStyle;
20                         var hs:ShieldStyle;
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; }
32                                 }
33                         }
34                         return new Array(ss,ps,ts,hs);
35                 }
36
37                 // Save and load rulesets
38
39                 public function save(url:String,name:String):void {
40
41                         var request:URLRequest=new URLRequest(url);
42                         var requestVars:URLVariables=new URLVariables();
43                         var loader:URLLoader=new URLLoader();
44                         
45                         // send to server
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);
55                         loader.load(request);
56                 }
57
58                 public function load(url:String):void {
59
60                         var request:URLRequest=new URLRequest(url);
61                         var loader:URLLoader=new URLLoader();
62
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);
69                         loader.load(request);
70                 }
71
72                 // data handlers
73                 private function savedRuleSet(event:Event):void {
74                         var loader:URLLoader = URLLoader(event.target);  
75                         // do something with loader.data
76                 }
77                 
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
82                         loadImages();
83                 }
84
85                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
86                 private function securityErrorHandler( event:SecurityErrorEvent ):void { }
87                 private function ioErrorHandler( event:IOErrorEvent ):void { }
88                 
89                 // serialise/deserialise methods
90
91
92                 // ------------------------------------------------------------------------------------------------
93                 // Load all referenced images
94                 // ** currently only looks in PointRules
95                 // ** will duplicate if referenced twice, shouldn't
96                 
97                 public function loadImages():void {
98                         var ps:PointStyle;
99                         for each (var rule:* in rules) {
100                                 if (!(rule is PointRule)) { continue; }
101                                 if (!(rule.pointStyle)) { continue; }
102                                 if (!(rule.pointStyle.icon)) { continue; }
103                                 
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);
113                         }
114                 }
115
116                 // data handler
117
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;
121                 }
122
123         }
124 }