Skip to content

Commit

Permalink
save gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
dabaopku committed Mar 10, 2012
1 parent a1dd297 commit 7ce8267
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 16 deletions.
3 changes: 3 additions & 0 deletions iStroke/Action.hh
Expand Up @@ -24,4 +24,7 @@

-(id) initWithStroke:(Stroke *)s;

-(NSDictionary *) save;
-(id) initWithDict:(NSDictionary *)dict;

@end
32 changes: 28 additions & 4 deletions iStroke/Action.mm
Expand Up @@ -32,11 +32,35 @@ - (id)initWithStroke:(iStroke::Stroke *)s

- (void)dealloc
{
[gesture release];
[image release];
[name release];
[cmd release];
if(gesture) [gesture release];
if(image) [image release];
if(name) [name release];
if(cmd) [cmd release];
[super dealloc];
}

-(NSDictionary *) save
{
NSMutableDictionary *dict=[NSMutableDictionary new];
[dict setObject:name forKey:@"name"];
[dict setObject:@"nil" forKey:@"cmd"];
[dict setObject:[gesture save] forKey:@"gesture"];

return dict;
}

-(id) initWithDict:(NSDictionary *)dict
{
self=[super init];
if(self)
{
self.name=[dict objectForKey:@"name"];
self.gesture=[[Gesture alloc] initWithDict:[dict objectForKey:@"gesture"]];
cmd=nil;
self.image=[gesture loadImage];
}

return self;
}

@end
36 changes: 28 additions & 8 deletions iStroke/Application.mm
Expand Up @@ -99,6 +99,15 @@ -(NSDictionary *) save
}
[childArray release];
}

if ([actions count]>0) {
NSMutableArray *actionArray=[NSMutableArray new];
[dict setObject:actionArray forKey:@"action"];
for (NSUInteger i=0,n=[actions count]; i<n; ++i) {
[actionArray addObject:[(Action*)[actions objectAtIndex:i] save]];
}
[actionArray release];
}
return dict;
}

Expand All @@ -110,7 +119,7 @@ -(id) initWithDict:(NSDictionary *)dict
self.parent=nil;
self.name=[dict objectForKey:@"name"];
if (!name) {
return nil;
self.name=@"No name";
}

self.identifier=[dict objectForKey:@"id"];
Expand All @@ -119,16 +128,27 @@ -(id) initWithDict:(NSDictionary *)dict
return nil;
}

self.actions=[NSMutableArray new];
self.children=[NSMutableArray new];

NSArray *childArray=[dict objectForKey:@"child"];
if (!childArray) {
return self;
if (childArray) {
for (NSUInteger i=0,n=[childArray count]; i<n; ++i) {
Application *app=[[Application alloc] initWithDict:[childArray objectAtIndex:i]];
if (app) {
[self addChildApplication:app];
[app release];
}
}
}

for (NSUInteger i=0,n=[childArray count]; i<n; ++i) {
Application *app=[[Application alloc] initWithDict:[childArray objectAtIndex:i]];
if (app) {
[self addChildApplication:app];
NSArray *actionArray=[dict objectForKey:@"action"];
if (actionArray) {
for (NSUInteger i=0,n=[actionArray count]; i<n; ++i) {
Action *act=[[Action alloc] initWithDict:[actionArray objectAtIndex:i]];
if (act) {
[actions addObject:act];
[act release];
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions iStroke/ApplicationManager.mm
Expand Up @@ -145,6 +145,9 @@ -(void) load
app.parent=nil;
[applications addObject:app];
}

if([applications count]>0)
curApp=[applications objectAtIndex:0];
}

#pragma mark - NSOutlineView
Expand Down
6 changes: 3 additions & 3 deletions iStroke/Gesture.hh
Expand Up @@ -6,8 +6,6 @@
// Copyright 2012年 PKU. All rights reserved.
//

#include <vector>
#include <string>
#import "Stroke.hh"
using namespace iStroke;

Expand All @@ -18,7 +16,6 @@ using namespace iStroke;

@private
Stroke * stroke;
std::string name;
long key;
}
@property(assign) long key;
Expand All @@ -29,4 +26,7 @@ using namespace iStroke;
-(bool) saveImage;
-(NSImage *) loadImage;

-(NSDictionary *) save;
-(id) initWithDict:(NSDictionary *)dict;

@end
44 changes: 44 additions & 0 deletions iStroke/Gesture.mm
Expand Up @@ -102,4 +102,48 @@ -(NSImage *) loadImage
NSImage *img=[[NSImage alloc] initWithContentsOfFile:file];
return img;
}

-(NSDictionary *) save
{
NSMutableDictionary *dict=[NSMutableDictionary new];
[dict setObject:[NSNumber numberWithLong:key] forKey:@"key"];

NSMutableArray *stk=[NSMutableArray new];
[dict setObject:stk forKey:@"stoke"];

for (int i=0,n=stroke->size(); i<n; ++i) {
[stk addObject:[NSNumber numberWithDouble:stroke->x(i)]];
[stk addObject:[NSNumber numberWithDouble:stroke->y(i)]];
}

[stk release];
return dict;
}

-(id) initWithDict:(NSDictionary *)dict
{
self=[super init];
if(self)
{
NSNumber *num=[dict objectForKey:@"key"];
if(!num)
return nil;
self.key=[num longValue];

NSArray *arr=[dict objectForKey:@"stoke"];
self.stroke=new iStroke::Stroke;
if(arr)
{
for (NSInteger i=0,n=[arr count]/2; i<n; ++i) {
double x=[[arr objectAtIndex:2*i] doubleValue];
double y=[[arr objectAtIndex:2*i+1] doubleValue];
self.stroke->addPoint(x, y);
}
self.stroke->finish();
}
}

return self;
}

@end
2 changes: 1 addition & 1 deletion iStroke/iStrokeAppDelegate.mm
Expand Up @@ -41,7 +41,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {


[applicationOutlineView registerForDraggedTypes:
[NSArray arrayWithObject:ApplicationPasteType] ];
[NSArray arrayWithObject:ApplicationPasteType] ];

}

Expand Down

0 comments on commit 7ce8267

Please sign in to comment.