Created
March 30, 2015 01:24
-
-
Save jason-feng/a4a0bae92db9d05ac720 to your computer and use it in GitHub Desktop.
Refactored Objective C code into Swift for easier integration with parse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CopyDrawView.swift | |
// eyeTrainTest2 | |
// | |
// Created by Jason Feng on 2/25/15. | |
// Copyright (c) 2015 edu.dartmouth.dali. All rights reserved. | |
// | |
import Foundation | |
import QuartzCore | |
class CopyDrawView: UIView { | |
// <#properties and methods#> | |
var path: UIBezierPath; | |
var incremental_image: UIImage?; | |
var background_image: UIImage; | |
var answer_image: UIImage; | |
var model_image: UIImage; | |
var guide_image: UIImage; | |
var incremental_imageview: UIImageView; | |
var background_imageview: UIImageView; | |
var answer_imageview: UIImageView?; | |
var model_imageview: UIImageView; | |
var guide_imageview: UIImageView; | |
var grade_imageview: UIImageView; | |
var incremental_image_init: Bool; | |
// Final score, with the first being gradeA and last being gradeF | |
var grades = [UIImage?](count:5, repeatedValue: nil); | |
var pts = [CGPoint](); | |
var ctr: Int; // we now need to keep track of the four points of a Bezier segment and the first control point of the next segment | |
func initCommon() { | |
let background_imagename = "background_paper.jpg"; | |
let background_image = UIImage(named: background_imagename); | |
let answer_imagename = "answer.png"; | |
let answer_image = UIImage(named: answer_imagename); | |
let model_imagename = "model.png"; | |
let model_image = UIImage(named: model_imagename); | |
let guide_imagename = "guide.png"; | |
let guide_image = UIImage(named: guide_imagename); | |
let gradeA_imagename = "grade_a.png"; | |
let gradeB_imagename = "grade_b.png"; | |
let gradeC_imagename = "grade_c.png"; | |
let gradeD_imagename = "grade_d.png"; | |
let gradeE_imagename = "grade_e.png"; | |
let incremental_image_init = false; | |
// Init all grades in the grades folder as images | |
var count = 0; | |
let folderPath = "grades"; | |
let fileManager = NSFileManager.defaultManager(); | |
let enumerator:NSDirectoryEnumerator = fileManager.enumeratorAtPath(folderPath)! | |
while var element = enumerator.nextObject() as? String { | |
if element.hasSuffix("png") { | |
grades.append(UIImage(named:element)); | |
} | |
} | |
path = UIBezierPath(); | |
path.lineWidth = 8.0; | |
model_imageview = UIImageView(image: model_image); | |
model_imageview.frame = (CGRectMake(342, 375, 328, 547)); | |
model_imageview.alpha = 0.5; | |
self.addSubview(model_imageview); | |
guide_imageview = UIImageView(image: guide_image); | |
guide_imageview.frame = CGRectMake(10, 32, 300, 288); | |
self.addSubview(guide_imageview); | |
self.multipleTouchEnabled = false; | |
self.backgroundColor = UIColor(patternImage: background_image!); | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder); | |
self.initCommon(); | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame); | |
self.initCommon(); | |
} | |
// Overides drawRect for custom drawing | |
// An empty implementation adversely affects performance during animation | |
override func drawRect(rect: CGRect) { | |
incremental_image!.drawInRect(rect); | |
path.stroke(); | |
incremental_image_init = true; | |
} | |
// Keeps track when the user begins drawing | |
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { | |
ctr = 0; | |
let touch = touches.anyObject() as UITouch; | |
pts[0] = touch.locationInView(self); | |
} | |
// Adding additional points to the path | |
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { | |
let touch = touches.anyObject() as UITouch; | |
let point = touch.locationInView(self); | |
ctr++ | |
pts[ctr] = point; | |
// The four points of a Bezier segment | |
if(ctr == 4) { | |
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); | |
// move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment | |
path.moveToPoint(pts[0]); | |
path.addCurveToPoint(pts[3], controlPoint1: pts[1], controlPoint2: pts[2]); | |
// add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2] | |
self.setNeedsDisplay(); | |
// Replace points to handle the next segment | |
pts[0] = pts[3]; | |
pts[1] = pts[4]; | |
ctr = 1; | |
} | |
} | |
// When you finish drawing, draw the path and reset the path | |
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { | |
self.drawBitmap(); | |
self.setNeedsDisplay(); | |
path.removeAllPoints(); | |
ctr = 0; | |
} | |
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) { | |
self.touchesEnded(touches, withEvent: event); | |
} | |
func drawBitmap() { | |
UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0.0); | |
if(!incremental_image_init) { // The first time, paint the background white | |
let rect_path = UIBezierPath(rect: self.bounds).CGPath; | |
let answer_path = self.convertSVGWithName("SampleAssests_lines"); | |
// TODO: Port compare lesson path | |
} | |
} | |
// Uses PocketSVG found here: https://github.com/arielelkin/PocketSVG | |
func convertSVGWithName(path_name: NSString) -> CGPath { | |
//1: Turn your SVG into a CGPath: | |
var myPath = PocketSVG.pathFromSVGFileNamed(path_name).takeUnretainedValue() | |
return myPath; | |
} | |
// Clears the images when finished drawing | |
func clear() { | |
answer_imageview!.image = nil; | |
grade_imageview.image = nil; | |
incremental_image = nil; | |
self.setNeedsDisplay(); | |
} | |
// Shows the actual answer after you are finished drawing | |
func showAnswer() { | |
if((answer_imageview!.image) != nil) { | |
return; | |
} | |
answer_imageview!.frame = CGRectMake(92, 289, 835, 720); | |
answer_imageview!.image = guide_image; | |
answer_imageview!.alpha = 0.5; | |
self.addSubview(answer_imageview!); | |
} | |
// func MyCGPathApplierFunc(info: UnsafeMutablePointer<Void>, element: UnsafePointer<CGPathElement>){ | |
// var bezierPoints = info; | |
// var points = element.memory.points; | |
// var type = element.memory.type; | |
// | |
// switch(type) { | |
// case kCGPathElementMoveToPoint: // contains 1 point | |
// bezierPoints.append(points[0]); | |
// case kCGPathElementAddLineToPoint: | |
// bezierPoints.append(points[0]); | |
// case kCGPathElementAddQuadCurveToPoint: | |
// bezierPoints.append(points[0]); | |
// bezierPoints.append(points[1]); | |
// case kCGPathElementAddCurveToPoint: | |
// bezierPoints.append(points[0]); | |
// bezierPoints.append(points[1]); | |
// bezierPoints.append(points[2]); | |
// case kCGPathElementCloseSubpath: | |
// break; | |
// default: | |
// break; | |
// } | |
// } | |
// | |
// func compareLessonPath(answer: UIBezierPath, attempt: UIBezierPath) { | |
// var answerPoints: NSMutableArray; | |
// var attemptPoints: NSMutableArray; | |
// | |
// CGPathApply(answer.CGPath, answerPoints, MyCGPathApplierFunc); | |
// CGPathApply(attempt.CGPath, attemptPoints, MyCGPathApplierFunc); | |
// | |
// } | |
// Calculates the score based on the difference value | |
func calculateScore() -> Int { | |
// finish return calculateScore; | |
} | |
// HELPER FUNCTIONS | |
func analysePixels(image: UIImage) -> Int { | |
let scored_pixel_number = 0; | |
let pixel_data = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); | |
let data = CFDataGetBytePtr(pixel_data); | |
for var x:CGFloat = 0; x < image.size.width; x++ { | |
for var y:CGFloat = 0; y < image.size.height; y++ { | |
//TODO Finish function | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment