#import "oSprite.h"

@implementation oSprite

NSSize defaultSize;

- (id)initWithParent:(id)parent
{
    myParent = parent;
    myName = @"Sprite";
    mySize.height = 0;
    myAction = -1;
    myValue = nil;
    myImage = nil;
    dict = [[NSMutableDictionary alloc] init];
    return [super init];
}

- (NSDictionary *)defsEntry
{
    NSMutableDictionary *entry = [[NSMutableDictionary alloc] init];
    [entry setObject:myName forKey:@"myName"];
    [entry setObject:[NSNumber numberWithDouble:mySize.width] forKey:@"width"];
    [entry setObject:[NSNumber numberWithDouble:mySize.height] forKey:@"height"];
    [entry setObject:[NSNumber numberWithInt:myAction] forKey:@"action"];
    if (myValue) [entry setObject:myValue forKey:@"value"];
    [entry setObject:dict forKey:@"dict"];
    return entry;
}

- (id)initWithDefsEntry:(NSDictionary *)entry parent:(id)parent
{
    self = [self initWithParent:parent];
    myName = [entry objectForKey:@"myName"];
    mySize.width = [[entry objectForKey:@"width"] doubleValue];
    mySize.height = [[entry objectForKey:@"height"] doubleValue];
    myAction = [[entry objectForKey:@"action"] intValue];
    myValue = [entry objectForKey:@"value"];
    [dict addEntriesFromDictionary:[entry objectForKey:@"dict"]];
    return self;
}

- (id)parent				{	return myParent;	}
- (NSString *)description		{	return myName;		}
- (NSString *)name			{	return myName;		}
- (void)setName:(NSString *)name	{	myName = name;		}
- (NSSize)size
{
    if (mySize.height == 0)
        return defaultSize;
    return mySize;
}
- (void)setSize:(NSSize)size		{	mySize = size;		}
- (void)setDefaultSize:(NSSize)size	{	defaultSize = size;	}
+ (void)setDefaultSize:(NSSize)size	{	defaultSize = size;	}
- (int)action				{	return myAction;	}
- (void)setAction:(int)anAction		{	myAction = anAction;	}
- (id)value				{	return myValue;		}
- (void)setValue:(id)value		{	myValue = [value retain];	}

- (void)setImage:(NSImage *)image
{
    if (myImage)
        [myImage release];
    myImage = [image retain];
    if (myImage)
        mySize = [myImage size];
}

- (NSImage *)image			{	return myImage;		}

- (BOOL)added				{	return added;		}
- (void)setAdded:(BOOL)flag		{	added = flag;		}

- (id)childAtIndex:(int)index 	{ 	return nil; 	}
- (int)numberOfChildren 	{ 	return 0; 	}
- (BOOL)isExpandable		{ 	return NO;	}
- (BOOL)shouldEditKey:(id)key
{
    if ([key isEqualToString:@"Name"]) return NO;
    return YES;
}
- (id)objectForKey:(id)key
{
    return [dict objectForKey:key];
    //if (!object) object = [NSNumber numberWithInt:0];
}
- (void)setObject:(id)object forKey:(id)key
{
    if (!object) return;
    if ([object isKindOfClass:[NSString class]] && [(NSString *)object length] == 0) {
        [dict removeObjectForKey:key];
        return;
    }
    [dict setObject:object forKey:key];
}

@end
