131f19571a5aadd5397e539a6b6707ecabe22dc5
[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
7         public class RuleSet {
8
9                 public var rules:Array=new Array();             // list of rules
10
11                 // variables for name, author etc.
12
13                 // returns array of ShapeStyle,PointStyle,TextStyle,ShieldStyle
14                 public function getStyle(isPoint:Boolean,tags:Object,scale:uint):Array {
15                         var ss:ShapeStyle;
16                         var ps:PointStyle;
17                         var ts:TextStyle;
18                         var hs:ShieldStyle;
19                         for each (var rule:* in rules) {
20                                 if ( isPoint && rule is ShapeRule) { continue; }
21                                 if (!isPoint && rule is PointRule) { continue; }
22                                 if (scale>rule.minScale) { continue; }
23                                 if (scale<rule.maxScale) { continue; }
24                                 if (rule.test(tags)) {
25                                         if (rule is ShapeRule && rule.shapeStyle)  { ss=rule.shapeStyle; }
26                                         if (rule is PointRule && rule.pointStyle)  { ps=rule.pointStyle; }
27                                         if (                     rule.textStyle )  { ts=rule.textStyle; }
28                                         if (rule is ShapeRule && rule.shieldStyle) { hs=rule.shieldStyle; }
29                                         if (rule.breaker)     { break; }
30                                 }
31                         }
32                         return new Array(ss,ps,ts,hs);
33                 }
34
35                 // Save and load rulesets
36
37                 public function save(url:String,name:String):void {
38
39                         var request:URLRequest=new URLRequest(url);
40                         var requestVars:URLVariables=new URLVariables();
41                         var loader:URLLoader=new URLLoader();
42                         
43                         // send to server
44                         requestVars['name']=name;
45                         requestVars['data']=YAML.encode(rules);
46                         request.data=requestVars;
47                         request.method = URLRequestMethod.POST;  
48                         loader.dataFormat = URLLoaderDataFormat.TEXT;
49                         loader.addEventListener(Event.COMPLETE,                                         savedRuleSet,                   false, 0, true);
50                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
51                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
52                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
53                         loader.load(request);
54                 }
55
56                 public function load(url:String):void {
57
58                         var request:URLRequest=new URLRequest(url);
59                         var loader:URLLoader=new URLLoader();
60
61                         request.method=URLRequestMethod.GET;
62                         loader.dataFormat = URLLoaderDataFormat.TEXT;
63                         loader.addEventListener(Event.COMPLETE,                                         loadedRuleSet,                  false, 0, true);
64                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
65                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
66                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
67                         loader.load(request);
68                 }
69
70                 // data handlers
71                 private function savedRuleSet(event:Event):void {
72                         var loader:URLLoader = URLLoader(event.target);  
73                         // do something with loader.data
74                 }
75                 
76                 private function loadedRuleSet(event:Event):void {
77                         var loader:URLLoader = URLLoader(event.target);  
78                         rules=YAML.decode(event.target.data) as Array;
79                         // fire some event or other to tell map to redraw
80                 }
81
82                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
83                 private function securityErrorHandler( event:SecurityErrorEvent ):void { }
84                 private function ioErrorHandler( event:IOErrorEvent ):void { }
85                 
86                 // serialise/deserialise methods
87         }
88 }