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