Skip to content

Commit

Permalink
Sanitising use of properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Whitaker committed Aug 4, 2011
1 parent b291a72 commit 8cac2a1
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 142 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
build
*.pbxuser
*.perspectivev3

Tween-o-Matic.xcodeproj/xcuserdata/simon.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
xcuserdata
18 changes: 11 additions & 7 deletions Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

#import <Cocoa/Cocoa.h>
#import "GridDelegate.h"
#define DRAG_HANDLE_RADIUS 5.0

enum draghandle {
Expand All @@ -15,16 +14,21 @@ enum draghandle {
CP_2
};

@protocol GridDelegate;

@interface Grid : NSView {
float border;
NSPoint cp1;
NSPoint cp2;
unsigned int activeDragHandle;
id delegate;
}

@property (nonatomic) CGFloat border;
@property (nonatomic) NSUInteger activeDragHandle;
@property (nonatomic) NSPoint cp1;
@property (nonatomic) NSPoint cp2;
@property (retain, nonatomic) id delegate;
@property (nonatomic, assign) id<GridDelegate> delegate;

@end

@protocol GridDelegate <NSObject>

-(void)controlPointWasDraggedAtIndex:(unsigned int)index;

@end
97 changes: 51 additions & 46 deletions Grid.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@

@implementation Grid

@synthesize delegate;
@synthesize border=_border;
@synthesize activeDragHandle=_activeDragHandle;
@synthesize delegate=_delegate;
@synthesize cp1=_cp1;
@synthesize cp2=_cp2;

#pragma mark -
#pragma mark Initialisation stuff
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
border = 20.0;
activeDragHandle = CP_NONE;
_border = 20.0;
_activeDragHandle = CP_NONE;
}
return self;
}

-(void)awakeFromNib {
//[[self window] setAcceptsMouseMovedEvents:YES];
}

#pragma mark -
Expand All @@ -33,8 +36,8 @@ -(void)awakeFromNib {
// grid to a point with coordinates in the range [0,1]
-(NSPoint)normalisePoint:(NSPoint)point {
NSPoint result = NSMakePoint(
(point.x - border) / ([self frame].size.width - border * 2),
(point.y - border) / ([self frame].size.height - border * 2)
(point.x - self.border) / (self.frame.size.width - self.border * 2),
(point.y - self.border) / (self.frame.size.height - self.border * 2)
);
return result;
}
Expand All @@ -43,8 +46,8 @@ -(NSPoint)normalisePoint:(NSPoint)point {
// a point with coordinates in the range of the actual grid
-(NSPoint)denormalisePoint:(NSPoint)point {
NSPoint result = NSMakePoint(
([self frame].size.width - border * 2) * point.x + border,
([self frame].size.height - border * 2) * point.y + border
(self.frame.size.width - self.border * 2) * point.x + self.border,
(self.frame.size.height - self.border * 2) * point.y + self.border
);
return result;
}
Expand All @@ -59,20 +62,20 @@ float distanceBetweenPoints(NSPoint a, NSPoint b) {
#pragma mark -
#pragma mark Control Point accessors
-(NSPoint)cp1 {
return [self normalisePoint:cp1];
return [self normalisePoint:_cp1];
}

-(void)setCp1:(NSPoint)point {
cp1 = [self denormalisePoint:point];
_cp1 = [self denormalisePoint:point];
[self setNeedsDisplay:YES];
}

-(NSPoint)cp2 {
return [self normalisePoint:cp2];
return [self normalisePoint:_cp2];
}

-(void)setCp2:(NSPoint)point {
cp2 = [self denormalisePoint:point];
_cp2 = [self denormalisePoint:point];
[self setNeedsDisplay:YES];
}

Expand All @@ -92,39 +95,41 @@ - (void)drawRect:(NSRect)dirtyRect {

NSRect rect = [self frame];

float w = rect.size.width - border * 2;
float h = rect.size.height - border * 2;
float w = rect.size.width - self.border * 2;
float h = rect.size.height - self.border * 2;

NSPoint origin = NSMakePoint(border, border);
NSPoint dest = NSMakePoint(w + border - 1, h + border - 1);
NSPoint origin = NSMakePoint(self.border, self.border);
NSPoint dest = NSMakePoint(w + self.border - 1, h + self.border - 1);

// draw the grid
[[NSColor grayColor] set];
for (int i = 0; i <= 10; i++) {
float x = w / 10 * i + border;
float y = h / 10 * i + border;
[NSBezierPath strokeLineFromPoint:NSMakePoint(border, y) toPoint:NSMakePoint(border + w - 1, y)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(x, border) toPoint:NSMakePoint(x, border + h - 1)];
float x = w / 10 * i + self.border;
float y = h / 10 * i + self.border;
[NSBezierPath strokeLineFromPoint:NSMakePoint(self.border, y)
toPoint:NSMakePoint(self.border + w - 1, y)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(x, self.border)
toPoint:NSMakePoint(x, self.border + h - 1)];
}

// draw the curve
[[NSColor blackColor] set];
NSBezierPath* line = [NSBezierPath bezierPath];
[line moveToPoint:origin];
[line curveToPoint:dest
controlPoint1:cp1
controlPoint2:cp2];
controlPoint1:_cp1
controlPoint2:_cp2];
[line stroke];

// draw control point 1
[[NSColor redColor] set];
line = [NSBezierPath bezierPath];
[line moveToPoint:origin];
[line lineToPoint:cp1];
[line lineToPoint:_cp1];
[line stroke];

line = [NSBezierPath bezierPath];
[line appendBezierPathWithArcWithCenter:cp1
[line appendBezierPathWithArcWithCenter:_cp1
radius:DRAG_HANDLE_RADIUS
startAngle:0
endAngle:360];
Expand All @@ -137,11 +142,11 @@ - (void)drawRect:(NSRect)dirtyRect {
[[NSColor blueColor] set];
line = [NSBezierPath bezierPath];
[line moveToPoint:dest];
[line lineToPoint:cp2];
[line lineToPoint:_cp2];
[line stroke];

line = [NSBezierPath bezierPath];
[line appendBezierPathWithArcWithCenter:cp2
[line appendBezierPathWithArcWithCenter:_cp2
radius:DRAG_HANDLE_RADIUS
startAngle:0
endAngle:360];
Expand All @@ -160,14 +165,14 @@ -(void)mouseDown:(NSEvent *)event
NSPoint location = [self convertPoint:[event locationInWindow]
fromView:nil];

if (distanceBetweenPoints(location, cp2) < DRAG_HANDLE_RADIUS * 2) {
activeDragHandle = CP_2;
if (distanceBetweenPoints(location, _cp2) < DRAG_HANDLE_RADIUS * 2) {
self.activeDragHandle = CP_2;
[[NSCursor closedHandCursor] push];
} else if (distanceBetweenPoints(location, cp1) < DRAG_HANDLE_RADIUS * 2) {
activeDragHandle = CP_1;
} else if (distanceBetweenPoints(location, _cp1) < DRAG_HANDLE_RADIUS * 2) {
self.activeDragHandle = CP_1;
[[NSCursor closedHandCursor] push];
} else {
activeDragHandle = CP_NONE;
self.activeDragHandle = CP_NONE;
}
}

Expand All @@ -181,38 +186,38 @@ -(void)mouseMoved:(NSEvent*)event {
NSPoint location = [self convertPoint:[event locationInWindow]
fromView:nil];

if (distanceBetweenPoints(location, cp1) < DRAG_HANDLE_RADIUS * 2 || distanceBetweenPoints(location, cp2) < DRAG_HANDLE_RADIUS * 2) {
if (distanceBetweenPoints(location, _cp1) < DRAG_HANDLE_RADIUS * 2 || distanceBetweenPoints(location, _cp2) < DRAG_HANDLE_RADIUS * 2) {
[[NSCursor openHandCursor] set];
} else {
//[[NSCursor arrowCursor] set];
}
}

-(void)mouseDragged:(NSEvent*)event {
if (activeDragHandle > CP_NONE) {
if (self.activeDragHandle > CP_NONE) {
//[[NSCursor closedHandCursor] set];
NSPoint location = [self convertPoint:[event locationInWindow]
fromView:nil];

if (location.x < border)
location.x = border;
else if (location.x > [self frame].size.width - border - 1)
location.x = [self frame].size.width - border - 1;
if (location.x < self.border)
location.x = self.border;
else if (location.x > [self frame].size.width - self.border - 1)
location.x = [self frame].size.width - self.border - 1;

if (location.y < border)
location.y = border;
else if (location.y > [self frame].size.height - border - 1)
location.y = [self frame].size.height - border - 1;
if (location.y < self.border)
location.y = self.border;
else if (location.y > [self frame].size.height - self.border - 1)
location.y = [self frame].size.height - self.border - 1;

if (activeDragHandle == CP_1) {
cp1 = location;
if (self.activeDragHandle == CP_1) {
_cp1 = location;
}
else {
cp2 = location;
_cp2 = location;
}

if (delegate && [delegate respondsToSelector:@selector(controlPointWasDraggedAtIndex:)])
[delegate controlPointWasDraggedAtIndex:activeDragHandle];
if (self.delegate && [self.delegate respondsToSelector:@selector(controlPointWasDraggedAtIndex:)])
[self.delegate controlPointWasDraggedAtIndex:self.activeDragHandle];

[self setNeedsDisplay:YES];
}
Expand Down
15 changes: 0 additions & 15 deletions GridDelegate.h

This file was deleted.

9 changes: 3 additions & 6 deletions TimingFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
#import <QuartzCore/QuartzCore.h>

@interface TimingFunction : NSObject {
NSString* function;
NSString* constantName;
NSString* description;
}

-(id)initWithFunction:(NSString*)function constantName:(NSString*)constantName andDescription:(NSString*)description;

@property (retain, nonatomic) NSString* function;
@property (retain, nonatomic) NSString* constantName;
@property (retain, nonatomic) NSString* description;
@property (copy, nonatomic) NSString* function;
@property (copy, nonatomic) NSString* constantName;
@property (copy, nonatomic) NSString* description;

@end
25 changes: 13 additions & 12 deletions TimingFunction.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@

@implementation TimingFunction

@synthesize constantName;
@synthesize function;
@synthesize description;
@synthesize constantName=_constantName;
@synthesize function=_function;
@synthesize description=_description;

-(id)initWithFunction:(NSString*)fn constantName:(NSString*)cn andDescription:(NSString*)desc {
self = [super init];
if (self) {
constantName = [cn retain];
function = [fn retain];
description = [desc retain];
-(id)initWithFunction:(NSString*)function
constantName:(NSString*)constantName
andDescription:(NSString*)description {
if ((self = [super init])) {
self.constantName = constantName;
self.function = function;
self.description = description;
}
return self;
}

-(void)dealloc {
[constantName release];
[function release];
[description release];
self.constantName = nil;
self.function = nil;
self.description = nil;
[super dealloc];
}

Expand Down
2 changes: 0 additions & 2 deletions Tween-o-Matic.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
F436C62E1162AE99002BD9A0 /* TimingFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TimingFunction.h; sourceTree = "<group>"; };
F436C62F1162AE99002BD9A0 /* TimingFunction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TimingFunction.m; sourceTree = "<group>"; };
F4394A1311608A1F00930F4D /* lorry.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = lorry.png; sourceTree = "<group>"; };
F450D2E3115D403E0033A32E /* GridDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GridDelegate.h; sourceTree = "<group>"; };
F450D34B115D4B8E0033A32E /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
F457AE75116130FC00CB5E72 /* README */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README; sourceTree = "<group>"; };
F4795C56115BFBCD008CB8CC /* Grid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Grid.h; sourceTree = "<group>"; };
Expand All @@ -62,7 +61,6 @@
children = (
F4795C56115BFBCD008CB8CC /* Grid.h */,
F4795C57115BFBCD008CB8CC /* Grid.m */,
F450D2E3115D403E0033A32E /* GridDelegate.h */,
256AC3D80F4B6AC300CF3369 /* Tween_o_MaticAppDelegate.h */,
256AC3D90F4B6AC300CF3369 /* Tween_o_MaticAppDelegate.m */,
F436C62E1162AE99002BD9A0 /* TimingFunction.h */,
Expand Down
32 changes: 15 additions & 17 deletions Tween_o_MaticAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@
#import "Grid.h"

@interface Tween_o_MaticAppDelegate : NSObject <NSApplicationDelegate, GridDelegate> {
NSWindow *window;
Grid* grid;
NSArray* curveTypes;
CAMediaTimingFunction* timingFunction;
NSImageView* demoImage;
CGFloat demoAnimationStartX;
CGFloat demoAnimationEndX;

IBOutlet NSPopUpButton* curveTypeDropDown;
IBOutlet NSTextField* cp1X;
IBOutlet NSTextField* cp1Y;
IBOutlet NSTextField* cp2X;
IBOutlet NSTextField* cp2Y;
IBOutlet NSTextField* constructor;
}

-(IBAction)updateTimingFunction:(id)sender;
Expand All @@ -32,8 +18,20 @@

-(void)updateControlPointFromGridAtIndex:(unsigned int)index;

@property (assign) IBOutlet Grid* grid;
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSImageView *demoImage;
@property (nonatomic, retain) IBOutlet Grid* grid;
@property (nonatomic, retain) IBOutlet NSWindow *window;
@property (nonatomic, retain) IBOutlet NSImageView *demoImage;

@property (nonatomic, copy) NSArray* curveTypes;
@property (nonatomic, retain) CAMediaTimingFunction* timingFunction;
@property (nonatomic) CGFloat demoAnimationStartX;
@property (nonatomic) CGFloat demoAnimationEndX;

@property (nonatomic, retain) IBOutlet NSPopUpButton* curveTypeDropDown;
@property (nonatomic, retain) IBOutlet NSTextField* cp1X;
@property (nonatomic, retain) IBOutlet NSTextField* cp1Y;
@property (nonatomic, retain) IBOutlet NSTextField* cp2X;
@property (nonatomic, retain) IBOutlet NSTextField* cp2Y;
@property (nonatomic, retain) IBOutlet NSTextField* constructor;

@end
Loading

0 comments on commit 8cac2a1

Please sign in to comment.