fix halcyon_viewer redraw post-refactoring
[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); loaded=true; 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                         loadImages();
66                 }
67
68                 private function doRedrawCallback(e:Event):void {
69                         parseCSS(e.target.data);
70                         loaded=true;
71                         redrawCallback();
72                 }
73
74
75                 // ------------------------------------------------------------------------------------------------
76                 // Load all referenced images
77                 // ** will duplicate if referenced twice, shouldn't
78                 
79                 public function loadImages():void {
80                         var filename:String;
81                         for each (var chooser:StyleChooser in choosers) {
82                                 for each (var style:Style in chooser.styles) {
83                                         if      (style is PointStyle  && PointStyle(style).icon_image   ) { filename=PointStyle(style).icon_image; }
84                                         else if (style is ShapeStyle  && ShapeStyle(style).fill_image   ) { filename=ShapeStyle(style).fill_image; }
85                                         else if (style is ShieldStyle && ShieldStyle(style).shield_image) { filename=ShieldStyle(style).shield_image; }
86                                         else { continue; }
87                                         if (filename=='square' || filename=='circle') { continue; }
88                                 
89                                         iconsToLoad++;
90                                         var request:URLRequest=new URLRequest(filename);
91                                         var loader:ExtendedURLLoader=new ExtendedURLLoader();
92                                         loader.dataFormat=URLLoaderDataFormat.BINARY;
93                                         loader.info['filename']=filename;
94                                         loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
95                                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
96                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
97                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
98                                         loader.load(request);
99                                 }
100                         }
101                 }
102
103                 // data handler
104
105                 private function loadedImage(event:Event):void {
106                         var fn:String=event.target.info['filename'];
107                         images[fn]=event.target.data;
108
109                         var loader:ExtendedLoader = new ExtendedLoader();
110                         loader.info['filename']=fn;
111                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, measureWidth);
112                         loader.loadBytes(images[fn]);
113                 }
114                 
115                 private function measureWidth(event:Event):void {
116                         var fn:String=event.target.loader.info['filename'];
117                         imageWidths[fn]=event.target.width;
118                         // ** do we need to explicitly remove the loader object now?
119
120                         iconsToLoad--;
121                         if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
122                 }
123
124                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
125                 private function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
126                 private function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }
127
128         }
129 }