1
0
mirror of https://github.com/thes3m/XNI synced 2024-12-26 13:26:06 +01:00
Matej Jan 08abf7ffe0 NSCoding support added to data classes.
git-svn-id: http://xni.googlecode.com/svn/XNI@120 ac433895-eea3-a490-d80a-17149a75e588
2012-10-11 21:45:34 +00:00

128 lines
3.2 KiB
Objective-C

//
// Rectangle.m
// XNI
//
// Created by Matej Jan on 22.7.10.
// Copyright 2010 Retronator, Razum. All rights reserved.
//
#import "Rectangle.h"
#import "Retronator.Xni.Framework.h"
@implementation Rectangle
// CONSTRUCTORS
- (id) initWithX:(int)x y:(int)y width:(int)width height:(int)height {
if (self = [super init]) {
data = RectangleMake(x, y, width, height);
}
return self;
}
- (id) initWithRectangleStruct:(RectangleStruct*) rectangleStruct {
if (self = [super init]) {
data = *rectangleStruct;
}
return self;
}
- (id) initWithRectangle:(Rectangle*) rectangle {
return [self initWithRectangleStruct:rectangle.data];
}
- (id) initWithCoder:(NSCoder *)aDecoder {
return [self initWithX:[aDecoder decodeIntForKey:@"x"]
y:[aDecoder decodeIntForKey:@"y"]
width:[aDecoder decodeIntForKey:@"width"]
height:[aDecoder decodeIntForKey:@"height"]];
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInt:data.x forKey:@"x"];
[aCoder encodeInt:data.y forKey:@"y"];
[aCoder encodeInt:data.width forKey:@"width"];
[aCoder encodeInt:data.height forKey:@"height"];
}
+ (Rectangle*) rectangleWithX:(int)x y:(int)y width:(int)width height:(int)height {
return [[[Rectangle alloc] initWithX:x y:y width:width height:height] autorelease];
}
+ (Rectangle*) rectangleWithStruct:(RectangleStruct*) rectangleStruct {
return [[[Rectangle alloc] initWithRectangleStruct:rectangleStruct] autorelease];
}
+ (Rectangle*) rectangleWithRectangle:(Rectangle*) rectangle {
return [[[Rectangle alloc] initWithRectangle:rectangle] autorelease];
}
+ (Rectangle*) rectangleWithCGRect:(CGRect) cgRect {
return [Rectangle rectangleWithX:cgRect.origin.x y:cgRect.origin.y
width:cgRect.size.width height:cgRect.size.height];
}
// PROPERTIES
- (int) x {return data.x;}
- (void) setX:(int)value {data.x = value;}
- (int) y {return data.y;}
- (void) setY:(int)value {data.y = value;}
- (int) width {return data.width;}
- (void) setWidth:(int)value {data.width = value;}
- (int) height {return data.height;}
- (void) setHeight:(int)value {data.height = value;}
- (RectangleStruct*) data {return &data;}
// METHODS
- (Rectangle*) set:(Rectangle*)value {
data = *value.data;
return self;
}
- (BOOL) containsX:(int)x y:(int)y {
return x >= data.x && x <= data.x + data.width && y >= data.y && y <= data.y + data.height;
}
- (BOOL) containsPoint:(XniPoint*)point {
return [self containsX:point.x y:point.y];
}
- (id) copyWithZone:(NSZone *)zone {
return [[Rectangle allocWithZone:zone] initWithRectangleStruct:&data];
}
- (BOOL) equals:(Rectangle*)rectangle {
if (!rectangle) return NO;
return rectangle.data->x == data.x && rectangle.data->y == data.y &&
rectangle.data->width == data.width && rectangle.data->height == data.height;
}
- (BOOL) isEqual:(id)object {
if ([object isKindOfClass:[Rectangle class]]) {
return [self equals:object];
}
return NO;
}
- (NSUInteger) hash {
return data.x ^ data.y ^ data.width ^ data.height;
}
- (NSString *) description {
return [NSString stringWithFormat:@"Rectangle(%i, %i, %i, %i)", data.x, data.y, data.width, data.height];
}
// CONSTANTS
+ (Rectangle*) empty {return [Rectangle rectangleWithX:0 y:0 width:0 height:0];}
@end