Retain preferences even when .swf filename has changed
[potlatch2.git] / net / systemeD / potlatch2 / FunctionKeyManager.as
1 package net.systemeD.potlatch2 {
2
3         import mx.collections.ArrayCollection;
4     import flash.net.SharedObject;
5         import flash.events.EventDispatcher;
6         import flash.events.Event;
7
8         /* Still to do:
9                 - this is keyed by a 'name' value, e.g. 'background'/'Bing'. If the user renames the value (e.g. renames the 'Bing'
10                   layer to 'Microsoft'), the mapping will be lost
11                 - the selectedItem call to getKeyFor generates one of those stupid "warning: unable to bind to property 
12                   'name' on class 'Object' (class is not an IEventDispatcher)" binding errors
13         */
14
15     public class FunctionKeyManager extends EventDispatcher {
16
17         private static const GLOBAL_INSTANCE:FunctionKeyManager = new FunctionKeyManager();
18         public static function instance():FunctionKeyManager { return GLOBAL_INSTANCE; }
19
20                 public static var fkeys:Array=['','F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','F13','F14','F15'];
21                 [Bindable(event="bogus")]
22                 public static var fkeysCollection:ArrayCollection=new ArrayCollection(fkeys);
23
24                 private var keys:Array=[];
25                 private var listeners:Object={};
26
27                 public function FunctionKeyManager() {
28                         for (var i:uint=1; i<16; i++) {
29                                 if (SharedObject.getLocal("user_state","/").data['F'+i]) {
30                                         keys[i]=SharedObject.getLocal("user_state","/").data['F'+i];
31                                 }
32                         }
33                 }
34
35                 /* Register a function as the handler for all keypresses with that code (e.g. 'background') */
36
37                 public function registerListener(code:String,f:Function):void {
38                         listeners[code]=f;
39                 }
40                 
41                 /* Set the code (e.g. 'background') and value (e.g. 'Bing') associated with a key */
42
43                 public function setKey(fkey:uint, code:String, value:String):void {
44                         keys[fkey]={ code:code, value:value };
45                         var obj:SharedObject=SharedObject.getLocal("user_state","/");
46                         obj.setProperty('F'+fkey,{ code:code, value:value });
47                         obj.flush();
48                         dispatchEvent(new Event("key_changed"));
49                 }
50
51                 public function setKeyFromFString(key:String, code:String, value:String):void {
52                         if (key=='') {
53                                 var oldKey:String=getKeyFor(code,value);
54                                 keys[Number(oldKey.substr(1))]=null;
55                                 var obj:SharedObject=SharedObject.getLocal("user_state","/");
56                                 obj.setProperty(oldKey,null);
57                                 obj.flush();
58                         } else {
59                                 setKey(Number(key.substr(1)),code,value);
60                         }
61                 }
62
63                 /* Find what key, if any, is currently assigned to a given code and value */
64
65                 [Bindable(event="key_changed")]
66                 public function getKeyFor(code:String, value:String):String {
67                         for (var i:uint=1; i<16; i++) {
68                                 if (keys[i] && keys[i].code==code && keys[i].value==value) { return 'F'+i; }
69                         }
70                         return '';
71                 }
72
73                 /* Dispatch function triggered by this key */
74                 
75                 public function handleKeypress(keycode:uint):Boolean {
76                         if (keycode<112 || keycode>126) { return false; }
77                         var fkey:uint=keycode-111;
78                         if (!keys[fkey]) { return false; }
79                         if (keys[fkey].value=='') { return false; }
80                         listeners[keys[fkey].code](keys[fkey].value);
81                         return true;
82                 }
83                 
84         }
85 }