Formal protocol for the interface shared between Joysticks and JSActions, use new...
[enjoyable.git] / Config.m
index 84e6c32..046ccc2 100644 (file)
--- a/Config.m
+++ b/Config.m
@@ -9,31 +9,37 @@
 
 #import "JSAction.h"
 
-@implementation Config {
-    NSMutableDictionary *entries;
-}
-
-@synthesize name;
-@synthesize 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;
 }
 
 - (Target *)objectForKeyedSubscript:(JSAction *)action {
-    return action ? entries[action.uid] : nil;
+    return action ? _entries[action.uid] : nil;
 }
 
 - (void)setObject:(Target *)target forKeyedSubscript:(JSAction *)action {
     if (action) {
         if (target)
-            entries[action.uid] = target;
+            _entries[action.uid] = target;
         else
-            [entries removeObjectForKey:action.uid];
+            [_entries removeObjectForKey:action.uid];
+    }
+}
+
+- (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