Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Classes/GLGestureRecognizer.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// Gestures
//
// Created by Adam Preble on 4/28/09. adam@giraffelab.com
//
//
// Largely an implementation of the $1 Unistroke Recognizer:
// http://depts.washington.edu/aimgroup/proj/dollar/
// Jacob O. Wobbrock, Andrew D. Wilson, Yang Li
//
//
#import "GLGestureRecognizer.h"

#define kSamplePoints (16)
Expand Down Expand Up @@ -72,18 +72,18 @@ - (NSString *)findBestMatchCenter:(CGPoint*)outCenter angle:(float*)outRadians s
const int samplePoints = kSamplePoints;
CGPoint samples[samplePoints];
int c = [[self touchPoints] count];

// Load up the samples. We use a very simplistic method for this; the JavaScript version is much more sophisticated.
for (i = 0; i < samplePoints; i++)
{
samples[i] = [[[self touchPoints] objectAtIndex:MAX(0, (c-1)*i/(samplePoints-1))] CGPointValue];
}

CGPoint center = Centroid(samples, samplePoints);
if (outCenter)
*outCenter = center;
Translate(samples, samplePoints, -center.x, -center.y); // Recenter

// Now rotate the path around 0,0, since the points have been transformed to that point.
// Find the angle of the first point:
CGPoint firstPoint = samples[0];
Expand All @@ -92,7 +92,7 @@ - (NSString *)findBestMatchCenter:(CGPoint*)outCenter angle:(float*)outRadians s
if (outRadians)
*outRadians = firstPointAngle;
Rotate(samples, samplePoints, -firstPointAngle);

CGPoint lowerLeft, upperRight; // For finding the boundaries of the gesture
for (i = 0; i < samplePoints; i++)
{
Expand All @@ -108,10 +108,10 @@ - (NSString *)findBestMatchCenter:(CGPoint*)outCenter angle:(float*)outRadians s
}
float scale = 2.0f/MAX(upperRight.x - lowerLeft.x, upperRight.y - lowerLeft.y);
Scale(samples, samplePoints, scale, scale);

center = Centroid(samples, samplePoints);
Translate(samples, samplePoints, -center.x, -center.y); // Recenter

// Now we can compare the samples against our known samples:
NSString *bestTemplateName = nil;
float best = INFINITY;
Expand All @@ -135,14 +135,14 @@ - (NSString *)findBestMatchCenter:(CGPoint*)outCenter angle:(float*)outRadians s
NSLog(@"Best: %@ with %0.2f", bestTemplateName, best);
if (outScore)
*outScore = best;

self.resampledPoints = [NSMutableArray arrayWithCapacity:samplePoints];
for (i = 0; i < samplePoints; i++)
{
CGPoint pt = samples[i];
[resampledPoints addObject:[NSValue valueWithCGPoint:pt]];
}

// Serialize the samples as JSON:
#ifndef NDEBUG
NSMutableString *string = [NSMutableString stringWithString:@"\"template_name\": [ "];
Expand Down
18 changes: 9 additions & 9 deletions Classes/GestureView.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ - (void)awakeFromNib
recognizer = [[GLGestureRecognizer alloc] init];
NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Gestures" ofType:@"json"]];
[recognizer loadTemplatesFromJsonData:jsonData];

self.caption = @"";
}

- (void)drawRect:(CGRect)rect
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);
CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1);
CGContextFillRect(ctx, rect);

CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
CGContextSelectFont(ctx, "Helvetica", 20, kCGEncodingMacRoman);
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1, -1));
CGContextSetTextMatrix(ctx, CGAffineTransformMakeScale(1, -1));
CGContextShowTextAtPoint(ctx, 10, 30, [caption UTF8String], [caption length]);

CGContextSetRGBFillColor(ctx, 0, 0, 0, 1);

for (NSValue *pointValue in [recognizer touchPoints])
{
CGPoint pointInView = [pointValue CGPointValue];
Expand All @@ -76,7 +76,7 @@ - (void)drawRect:(CGRect)rect
CGContextAddLineToPoint(ctx, pointInView.x, pointInView.y);
}
CGContextStrokePath(ctx);

CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextFillRect(ctx, CGRectMake(center.x, center.y, 4.0f, 4.0f));
}
Expand All @@ -101,7 +101,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[recognizer addTouches:touches fromView:self];

[self processGestureData];
[self setNeedsDisplay];
}
Expand Down
6 changes: 3 additions & 3 deletions Classes/GesturesAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ @implementation GesturesAppDelegate
@synthesize viewController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
Expand Down
4 changes: 2 additions & 2 deletions Gestures.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"circle": [
[0.80, -0.00], [0.80, 0.15], [0.46, 0.61], [0.08, 0.81], [-0.31, 0.82], [-0.84, 0.56], [-1.15, 0.15], [-1.16, -0.15], [-0.92, -0.44], [-0.59, -0.63], [-0.17, -0.64], [0.21, -0.60], [0.43, -0.45], [0.71, -0.20], [0.84, -0.01], [0.83, 0.03],
[0.80, -0.00], [0.80, 0.15], [0.46, 0.61], [0.08, 0.81], [-0.31, 0.82], [-0.84, 0.56], [-1.15, 0.15], [-1.16, -0.15], [-0.92, -0.44], [-0.59, -0.63], [-0.17, -0.64], [0.21, -0.60], [0.43, -0.45], [0.71, -0.20], [0.84, -0.01], [0.83, 0.03],
],
"line": [
[1.06, -0.00], [0.98, -0.00], [0.71, -0.01], [0.61, -0.01], [0.57, -0.01], [0.50, -0.01], [0.30, -0.02], [0.16, -0.02], [0.02, -0.02], [-0.25, -0.03], [-0.41, -0.00], [-0.59, 0.01], [-0.86, 0.03], [-0.91, 0.03], [-0.93, 0.03], [-0.94, 0.03],
[1.06, -0.00], [0.98, -0.00], [0.71, -0.01], [0.61, -0.01], [0.57, -0.01], [0.50, -0.01], [0.30, -0.02], [0.16, -0.02], [0.02, -0.02], [-0.25, -0.03], [-0.41, -0.00], [-0.59, 0.01], [-0.86, 0.03], [-0.91, 0.03], [-0.93, 0.03], [-0.94, 0.03],
],
}
2 changes: 1 addition & 1 deletion main.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
Expand Down