beginning of vector background layers. And a whole load of other stuff too
[potlatch2.git] / net / systemeD / halcyon / styleparser / RuleSet.as
1 package net.systemeD.halcyon.styleparser {
2
3         import flash.events.*;
4         import flash.net.*;
5         import net.systemeD.halcyon.ExtendedLoader;
6         import net.systemeD.halcyon.ExtendedURLLoader;
7     import net.systemeD.halcyon.connection.Entity;
8
9     import net.systemeD.halcyon.connection.*;
10         import net.systemeD.halcyon.Globals;
11 //      import bustin.dev.Inspector;
12         
13         public class RuleSet {
14
15                 private var minscale:uint;
16                 private var maxscale:uint;
17                 public var loaded:Boolean=false;                        // has it loaded yet?
18                 public var choosers:Array=new Array();          // list of StyleChoosers
19                 public var images:Object=new Object();          // loaded images
20                 public var imageWidths:Object=new Object();     // width of each bitmap image
21                 private var redrawCallback:Function=null;       // function to call when CSS loaded
22                 private var iconCallback:Function=null;         // function to call when all icons loaded
23                 private var iconsToLoad:uint=0;                         // number of icons left to load (fire callback when ==0)
24
25                 // variables for name, author etc.
26
27                 public function RuleSet(mins:uint,maxs:uint,redrawCall:Function=null,iconLoadedCallback:Function=null):void {
28                         minscale = mins;
29                         maxscale = maxs;
30                         redrawCallback = redrawCall;
31                         iconCallback = iconLoadedCallback;
32                 }
33
34                 // Get styles for an object
35
36                 public function getStyles(obj:Entity,tags:Object):StyleList {
37                         var sl:StyleList=new StyleList();
38                         for each (var sc:StyleChooser in choosers) {
39                                 sc.updateStyles(obj,tags,sl,imageWidths);
40                         }
41                         return sl;
42                 }
43
44                 // ---------------------------------------------------------------------------------------------------------
45                 // Loading stylesheet
46
47                 public function loadFromCSS(str:String):void {
48                         if (str.match(/[\s\n\r\t]/)!=null) { parseCSS(str); redrawCallback(); return; }
49
50                         var request:URLRequest=new URLRequest(str);
51                         var loader:URLLoader=new URLLoader();
52
53                         request.method=URLRequestMethod.GET;
54                         loader.dataFormat = URLLoaderDataFormat.TEXT;
55                         loader.addEventListener(Event.COMPLETE,                                         doRedrawCallback);
56                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler);
57                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
58                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
59                         loader.load(request);
60                 }
61
62                 private function parseCSS(str:String):void {
63                         var css:MapCSS=new MapCSS(minscale,maxscale);
64                         choosers=css.parse(str);
65 //                      Inspector.getInstance().show();
66 //                      Inspector.getInstance().shelf('Choosers', choosers);
67                         loadImages();
68 //                      map.redraw();
69                 }
70
71                 private function doRedrawCallback(e:Event):void {
72                         parseCSS(e.target.data);
73                         loaded=true;
74                         redrawCallback();
75                 }
76
77
78                 // ------------------------------------------------------------------------------------------------
79                 // Load all referenced images
80                 // ** will duplicate if referenced twice, shouldn't
81                 
82                 public function loadImages():void {
83                         var filename:String;
84                         for each (var chooser:StyleChooser in choosers) {
85                                 for each (var style:Style in chooser.styles) {
86                                         if      (style is PointStyle  && PointStyle(style).icon_image   ) { filename=PointStyle(style).icon_image; }
87                                         else if (style is ShapeStyle  && ShapeStyle(style).fill_image   ) { filename=ShapeStyle(style).fill_image; }
88                                         else if (style is ShieldStyle && ShieldStyle(style).shield_image) { filename=ShieldStyle(style).shield_image; }
89                                         else { continue; }
90                                         if (filename=='square' || filename=='circle') { continue; }
91                                 
92                                         iconsToLoad++;
93                                         var request:URLRequest=new URLRequest(filename);
94                                         var loader:ExtendedURLLoader=new ExtendedURLLoader();
95                                         loader.dataFormat=URLLoaderDataFormat.BINARY;
96                                         loader.info['filename']=filename;
97                                         loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
98                                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
99                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
100                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
101                                         loader.load(request);
102                                 }
103                         }
104                 }
105
106                 // data handler
107
108                 private function loadedImage(event:Event):void {
109                         var fn:String=event.target.info['filename'];
110                         images[fn]=event.target.data;
111
112                         var loader:ExtendedLoader = new ExtendedLoader();
113                         loader.info['filename']=fn;
114                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, measureWidth);
115                         loader.loadBytes(images[fn]);
116                 }
117                 
118                 private function measureWidth(event:Event):void {
119                         var fn:String=event.target.loader.info['filename'];
120                         imageWidths[fn]=event.target.width;
121                         // ** do we need to explicitly remove the loader object now?
122
123                         iconsToLoad--;
124                         if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
125                 }
126
127                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
128                 private function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
129                 private function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }
130
131         }
132 }