MapCSS in progress. Vast amounts still to do, but the meat of it is here. POIs are...
[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         import net.systemeD.halcyon.Map;
8     import net.systemeD.halcyon.connection.Entity;
9 //      import bustin.dev.Inspector;
10         
11         public class RuleSet {
12
13                 private var map:Map;
14                 public var choosers:Array=new Array();  // list of StyleChoosers
15                 public var images:Object=new Object();  // loaded images
16                 private var iconCallback:Function=null; // function to call when all icons loaded
17                 private var iconsToLoad:uint=0;                 // number of icons left to load (fire callback when ==0)
18
19                 // variables for name, author etc.
20
21                 public function RuleSet(m:Map,f:Function=null):void {
22                         map=m;
23                         iconCallback=f;
24                 }
25
26                 // Get styles for an object
27
28                 public function getStyles(obj:Entity,tags:Object):StyleList {
29                         var sl:StyleList=new StyleList();
30                         for each (var sc:StyleChooser in choosers) {
31                                 sc.updateStyles(obj,tags,sl);
32                         }
33                         return sl;
34                 }
35
36                 // ---------------------------------------------------------------------------------------------------------
37                 // Loading stylesheet
38
39                 public function loadFromCSS(url:String):void {
40                         var request:URLRequest=new URLRequest(url);
41                         var loader:URLLoader=new URLLoader();
42
43                         request.method=URLRequestMethod.GET;
44                         loader.dataFormat = URLLoaderDataFormat.TEXT;
45                         loader.addEventListener(Event.COMPLETE,                                         loadedCSS,                              false, 0, true);
46                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
47                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
48                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
49                         loader.load(request);
50                 }
51
52                 private function loadedCSS(event:Event):void {
53                         var css:MapCSS=new MapCSS(map);
54                         choosers=css.parse(event.target.data);
55 //                      Inspector.getInstance().show();
56 //                      Inspector.getInstance().shelf('Choosers', choosers);
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                 // ** currently only looks in PointRules
67                 // ** will duplicate if referenced twice, shouldn't
68                 
69                 public function loadImages():void {
70                         var ps:PointStyle;
71
72                         for each (var rule:* in choosers) {
73                                 // if (!(rule is PointRule)) { continue; }
74                                 if (!(rule.pointStyle)) { continue; }
75                                 if (!(rule.pointStyle.icon_image)) { continue; }
76                                 
77                                 iconsToLoad++;
78                                 var request:URLRequest=new URLRequest(rule.pointStyle.icon_image);
79                                 var loader:ImageLoader=new ImageLoader();
80                                 loader.dataFormat=URLLoaderDataFormat.BINARY;
81                                 loader.filename=rule.pointStyle.icon_image;
82                                 loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
83                                 loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
84                                 loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
85                                 loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
86                                 loader.load(request);
87                         }
88                 }
89
90                 // data handler
91
92                 private function loadedImage(event:Event):void {
93                         images[event.target.filename]=event.target.data;
94                         iconsToLoad--;
95                         if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
96                 }
97         }
98 }