Formal protocol for the interface shared between Joysticks and JSActions, use new...
[enjoyable.git] / Config.m
index f62c40f..046ccc2 100644 (file)
--- a/Config.m
+++ b/Config.m
@@ -7,23 +7,39 @@
 
 #import "Config.h"
 
-@implementation Config
+#import "JSAction.h"
 
-@synthesize protect, name, entries;
+@implementation Config
 
-- (id)init {
+- (id)initWithName:(NSString *)name {
     if ((self = [super init])) {
-        entries = [[NSMutableDictionary alloc] init];
+        self.name = name ? name : @"Untitled";
+        _entries = [[NSMutableDictionary alloc] init];
     }
     return self;
 }
 
-- (void)setTarget:(Target *)target forAction:(JSAction *)jsa {
-    entries[[jsa stringify]] = target;
+- (Target *)objectForKeyedSubscript:(JSAction *)action {
+    return action ? _entries[action.uid] : nil;
+}
+
+- (void)setObject:(Target *)target forKeyedSubscript:(JSAction *)action {
+    if (action) {
+        if (target)
+            _entries[action.uid] = target;
+        else
+            [_entries removeObjectForKey:action.uid];
+    }
 }
 
-- (Target *)getTargetForAction:(JSAction *)jsa {
-    return entries[[jsa stringify]];
+- (NSDictionary *)serialize {
+    NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] initWithCapacity:_entries.count];
+    for (id key in _entries) {
+        id serialized = [_entries[key] serialize];
+        if (serialized)
+            cfgEntries[key] = serialized;
+    }
+    return @{ @"name": _name, @"entries": cfgEntries };
 }
 
 @end