Permalink
Browse files

I can erase, but not draw over erased parts

  • Loading branch information...
1 parent 0464f39 commit f29250fd8eb4493e6cb5b47ecfe46d59ca4246d1 @paulaj paulaj committed Jun 3, 2010
Showing with 56 additions and 16 deletions.
  1. +3 −0 Classes/WhiteboardView.h
  2. +52 −15 Classes/WhiteboardView.m
  3. +1 −1 Tin-Can-Whiteboard.xcodeproj/project.pbxproj
View
@@ -13,6 +13,9 @@
CGPoint lastLocation;
CGPoint location;
NSMutableArray *letsDraw;
+ NSMutableArray *letsErase;
+ NSMutableArray *myArray;
+ Boolean isErasing;
}
//@property (nonatomic, assign) CGPoint lastLocation;
View
@@ -17,6 +17,9 @@ - (id)initWithFrame:(CGRect)frame {
lastLocation =CGPointMake(0.0, 0.0);
location =CGPointMake(0.0, 0.0);
letsDraw = [[NSMutableArray array] retain];
+ letsErase = [[NSMutableArray array] retain];
+ myArray = [[NSMutableArray array] retain];
+ isErasing=false;
}
return self;
}
@@ -27,13 +30,13 @@ - (void)drawRect:(CGRect)rect {
//if we have a context
if(ctx != nil) {
-
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
- CGContextSetRGBStrokeColor(ctx, 0, 0, 1.0, 1);
CGContextFillRect(ctx, CGRectMake(0, 0, 768, 1024));
- CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1);
- NSLog(@"current size = %d", [letsDraw count]);
+
+ }
if ([letsDraw count] >0) { //under the condition that there is something in our Array
+ CGContextSetRGBStrokeColor(ctx, 0, 0, 1.0, 1);
+ CGContextSetLineWidth(ctx, 1);
for (int c=0; c< [letsDraw count]; c++) {//as long as we don't go past the number of available objects
NSArray *storedPoints=[ letsDraw objectAtIndex:c];//lets name the array at that index
@@ -55,21 +58,53 @@ - (void)drawRect:(CGRect)rect {
}
}
}
+ if ([letsErase count] >0) { //under the condition that there is something in our Array
+ CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1);
+ CGContextSetLineWidth(ctx, 5);
+ for (int c=0; c< [letsErase count]; c++) {//as long as we don't go past the number of available objects
+ NSArray *storedEPoints=[ letsErase objectAtIndex:c];//lets name the array at that index
+
+ if ([storedEPoints count] >2) { //if we have at least one full x,y pair
+ //store them as a float in a variable so we can use them
+ float storedEX=[[storedEPoints objectAtIndex:0] floatValue];
+ float storedEY=[[storedEPoints objectAtIndex:1] floatValue];
+ CGContextBeginPath(ctx);//Lets start a path
+ CGContextMoveToPoint(ctx, storedEX, storedEY);// and start where we first touched
+
+ for (int i =2; i < [storedEPoints count]; i+=2) { // as long as there are xy pairs left in our array
+ //store them as a float in a variable so we can use them
+ storedEX=[[storedEPoints objectAtIndex:i] floatValue];
+ storedEY=[[storedEPoints objectAtIndex:i+1] floatValue];
+ CGContextAddLineToPoint (ctx, storedEX, storedEY);//lets create a line between them, then move to the end of that line
+ }
+ NSLog(@"I'm about to draw");
+ CGContextStrokePath(ctx);
+ }
+ }
+ }
}
-}
+
+
+
//Begins Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
+ if (isErasing){
+ myArray=letsErase;
+ }
+ else {
+ myArray=letsDraw;
+ }
UITouch *touch = [[event allTouches] anyObject];
//Stores current location of touch in our instance variable lastLocation
lastLocation = [touch locationInView:self];
//Creates an Array and adds it into our initial Array letsDraw
- [letsDraw addObject:[[NSMutableArray alloc] initWithCapacity:4]];
- NSLog(@"%@", [letsDraw lastObject]);
+ [myArray addObject:[[NSMutableArray alloc] initWithCapacity:4]];
+ NSLog(@"%@", [myArray lastObject]);
//Adds the CGPoint x,y coordininates into the array (must be done this way because CGPoints are not Objects
- [[letsDraw lastObject] addObject: [NSNumber numberWithFloat: lastLocation.x]];
- [[letsDraw lastObject] addObject: [NSNumber numberWithFloat: lastLocation.y]];
+ [[myArray lastObject] addObject: [NSNumber numberWithFloat: lastLocation.x]];
+ [[myArray lastObject] addObject: [NSNumber numberWithFloat: lastLocation.y]];
NSLog(@"I got a touch");
NSLog(@"touch pos= %f,%f,", lastLocation.x, lastLocation.y);
@@ -81,8 +116,8 @@ -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//Stores every recorded movement CGPoint in the instance variable location
location = [touch locationInView:self];
//Adds the x,y coordinates of the CGPoint location
- [[letsDraw lastObject] addObject: [NSNumber numberWithFloat: location.x]];
- [[letsDraw lastObject] addObject: [NSNumber numberWithFloat: location.y]];
+ [[myArray lastObject] addObject: [NSNumber numberWithFloat: location.x]];
+ [[myArray lastObject] addObject: [NSNumber numberWithFloat: location.y]];
[self setNeedsDisplay];//This displays what we've done so far, letting us draw as we go.
NSLog(@"I'm moving");
@@ -93,11 +128,11 @@ -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//Stores touch in location
location = [touch locationInView:self];
//adds to array
- [[letsDraw lastObject] addObject:[NSNumber numberWithFloat: location.x]];
- [[letsDraw lastObject] addObject:[NSNumber numberWithFloat: location.y]];
+ [[myArray lastObject] addObject:[NSNumber numberWithFloat: location.x]];
+ [[myArray lastObject] addObject:[NSNumber numberWithFloat: location.y]];
[self setNeedsDisplay];// and displays
- NSLog(@"I've ended: %@", [letsDraw lastObject]);
-
+ NSLog(@"I've ended: %@", [myArray lastObject]);
+ isErasing= !isErasing;
}
@@ -107,6 +142,8 @@ -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
- (void)dealloc {
[super dealloc];
[letsDraw release];
+ [letsErase release];
+ [myArray release];
}
@@ -33,7 +33,7 @@
32CA4F630368D1EE00C91783 /* Tin_Can_Whiteboard_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Tin_Can_Whiteboard_Prefix.pch; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Tin_Can_Whiteboard-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Tin_Can_Whiteboard-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
DAFEB76B11B570F10050A558 /* WhiteboardView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WhiteboardView.h; sourceTree = "<group>"; };
- DAFEB76C11B570F10050A558 /* WhiteboardView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WhiteboardView.m; sourceTree = "<group>"; };
+ DAFEB76C11B570F10050A558 /* WhiteboardView.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = WhiteboardView.m; sourceTree = "<group>"; tabWidth = 4; usesTabs = 1; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */

0 comments on commit f29250f

Please sign in to comment.