Richard told you to go away, now go away
[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.Globals;
6         import net.systemeD.halcyon.Map;
7     import net.systemeD.halcyon.connection.Entity;
8 //      import bustin.dev.Inspector;
9         
10         public class RuleSet {
11
12                 private var map:Map;
13                 public var choosers:Array=new Array();  // list of StyleChoosers
14                 public var images:Object=new Object();  // loaded images
15                 private var iconCallback:Function=null; // function to call when all icons loaded
16                 private var iconsToLoad:uint=0;                 // number of icons left to load (fire callback when ==0)
17
18                 // variables for name, author etc.
19
20                 public function RuleSet(m:Map,f:Function=null):void {
21                         map=m;
22                         iconCallback=f;
23                 }
24
25                 // Get styles for an object
26
27                 public function getStyles(obj:Entity,tags:Object):StyleList {
28                         var sl:StyleList=new StyleList();
29                         for each (var sc:StyleChooser in choosers) {
30                                 sc.updateStyles(obj,tags,sl);
31                         }
32                         return sl;
33                 }
34
35                 // ---------------------------------------------------------------------------------------------------------
36                 // Loading stylesheet
37
38                 public function loadFromCSS(url:String):void {
39                         var request:URLRequest=new URLRequest(url);
40                         var loader:URLLoader=new URLLoader();
41
42                         request.method=URLRequestMethod.GET;
43                         loader.dataFormat = URLLoaderDataFormat.TEXT;
44                         loader.addEventListener(Event.COMPLETE,                                         loadedCSS,                              false, 0, true);
45                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
46                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
47                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
48                         loader.load(request);
49                 }
50
51                 private function loadedCSS(event:Event):void {
52                         var css:MapCSS=new MapCSS(map);
53                         choosers=css.parse(event.target.data);
54 //                      Inspector.getInstance().show();
55 //                      Inspector.getInstance().shelf('Choosers', choosers);
56                         loadImages();
57                 }
58
59                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
60                 private function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
61                 private function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }
62
63
64                 // ------------------------------------------------------------------------------------------------
65                 // Load all referenced images
66                 // ** will duplicate if referenced twice, shouldn't
67                 
68                 public function loadImages():void {
69 Globals.vars.root.addDebug("entering loadImages");
70                         var filename:String;
71                         for each (var chooser:StyleChooser in choosers) {
72                                 for each (var style:Style in chooser.styles) {
73                                         if      (style is PointStyle  && PointStyle(style).icon_image   ) { filename=PointStyle(style).icon_image; }
74                                         else if (style is ShapeStyle  && ShapeStyle(style).fill_image   ) { filename=ShapeStyle(style).fill_image; }
75                                         else if (style is ShieldStyle && ShieldStyle(style).shield_image) { filename=ShieldStyle(style).shield_image; }
76                                         else { continue; }
77 Globals.vars.root.addDebug("need "+filename);
78                                 
79                                         iconsToLoad++;
80                                         var request:URLRequest=new URLRequest(filename);
81                                         var loader:ImageLoader=new ImageLoader();
82                                         loader.dataFormat=URLLoaderDataFormat.BINARY;
83                                         loader.filename=filename;
84                                         loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
85                                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
86                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
87                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
88                                         loader.load(request);
89                                 }
90                         }
91                 }
92
93                 // data handler
94
95                 private function loadedImage(event:Event):void {
96                         images[event.target.filename]=event.target.data;
97                         iconsToLoad--;
98                         if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
99                 }
100         }
101 }