add more ways to control the viewer from JavaScript
[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.ImageURLLoader;
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(str:String):void {
40                         if (str.match(/[\s\n\r\t]/)!=null) { parseCSS(str); return; }
41
42                         var request:URLRequest=new URLRequest(str);
43                         var loader:URLLoader=new URLLoader();
44
45                         request.method=URLRequestMethod.GET;
46                         loader.dataFormat = URLLoaderDataFormat.TEXT;
47                         loader.addEventListener(Event.COMPLETE,                                         loadedCSS,                              false, 0, true);
48                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
49                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
50                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
51                         loader.load(request);
52                 }
53
54                 private function loadedCSS(event:Event):void {
55                         parseCSS(event.target.data);
56                 }
57                 
58                 private function parseCSS(str:String):void {
59                         var css:MapCSS=new MapCSS(map);
60                         choosers=css.parse(str);
61 //                      Inspector.getInstance().show();
62 //                      Inspector.getInstance().shelf('Choosers', choosers);
63                         loadImages();
64                 }
65
66
67                 // ------------------------------------------------------------------------------------------------
68                 // Load all referenced images
69                 // ** will duplicate if referenced twice, shouldn't
70                 
71                 public function loadImages():void {
72                         var filename:String;
73                         for each (var chooser:StyleChooser in choosers) {
74                                 for each (var style:Style in chooser.styles) {
75                                         if      (style is PointStyle  && PointStyle(style).icon_image   ) { filename=PointStyle(style).icon_image; }
76                                         else if (style is ShapeStyle  && ShapeStyle(style).fill_image   ) { filename=ShapeStyle(style).fill_image; }
77                                         else if (style is ShieldStyle && ShieldStyle(style).shield_image) { filename=ShieldStyle(style).shield_image; }
78                                         else { continue; }
79                                 
80                                         iconsToLoad++;
81                                         var request:URLRequest=new URLRequest(filename);
82                                         var loader:ImageURLLoader=new ImageURLLoader();
83                                         loader.dataFormat=URLLoaderDataFormat.BINARY;
84                                         loader.filename=filename;
85                                         loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
86                                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
87                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
88                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
89                                         loader.load(request);
90                                 }
91                         }
92                 }
93
94                 // data handler
95
96                 private function loadedImage(event:Event):void {
97                         images[event.target.filename]=event.target.data;
98                         iconsToLoad--;
99                         if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
100                 }
101
102                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
103                 private function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
104                 private function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }
105
106         }
107 }