Skip to content

Commit

Permalink
Adds Draw commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Angel Quinones Garcia committed Jun 3, 2015
1 parent 8a5f9a0 commit 44272d3
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
20 changes: 20 additions & 0 deletions FreehandDrawing-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
47919A4B1B0CE08600D7BFE9 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 47919A4A1B0CE08600D7BFE9 /* Images.xcassets */; };
47919A4E1B0CE08600D7BFE9 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 47919A4C1B0CE08600D7BFE9 /* LaunchScreen.xib */; };
47919A5A1B0CE08600D7BFE9 /* FreehandDrawing_iOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47919A591B0CE08600D7BFE9 /* FreehandDrawing_iOSTests.swift */; };
47D780891B1F1A9900076BD6 /* DrawCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D780881B1F1A9900076BD6 /* DrawCommands.swift */; };
47D7808C1B1F1D0100076BD6 /* LineDrawCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D7808B1B1F1D0100076BD6 /* LineDrawCommand.swift */; };
47D7808F1B1F1D1400076BD6 /* CircleDrawCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D7808E1B1F1D1400076BD6 /* CircleDrawCommand.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -42,6 +45,9 @@
47919A531B0CE08600D7BFE9 /* FreehandDrawing-iOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "FreehandDrawing-iOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
47919A581B0CE08600D7BFE9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
47919A591B0CE08600D7BFE9 /* FreehandDrawing_iOSTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FreehandDrawing_iOSTests.swift; sourceTree = "<group>"; };
47D780881B1F1A9900076BD6 /* DrawCommands.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrawCommands.swift; sourceTree = "<group>"; };
47D7808B1B1F1D0100076BD6 /* LineDrawCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LineDrawCommand.swift; sourceTree = "<group>"; };
47D7808E1B1F1D1400076BD6 /* CircleDrawCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CircleDrawCommand.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -67,6 +73,7 @@
children = (
47919A431B0CE08600D7BFE9 /* AppDelegate.swift */,
47919A451B0CE08600D7BFE9 /* DrawViewController.swift */,
47D7808A1B1F1CE700076BD6 /* DrawCommands */,
4712795C1B0CE98400A49B7E /* DrawView.swift */,
47702F9E1B0E448800417ACF /* Toolbar.swift */,
47702FA01B0E452100417ACF /* Toolbar.xib */,
Expand Down Expand Up @@ -129,6 +136,16 @@
name = "Supporting Files";
sourceTree = "<group>";
};
47D7808A1B1F1CE700076BD6 /* DrawCommands */ = {
isa = PBXGroup;
children = (
47D780881B1F1A9900076BD6 /* DrawCommands.swift */,
47D7808B1B1F1D0100076BD6 /* LineDrawCommand.swift */,
47D7808E1B1F1D1400076BD6 /* CircleDrawCommand.swift */,
);
name = DrawCommands;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -231,7 +248,10 @@
buildActionMask = 2147483647;
files = (
47919A461B0CE08600D7BFE9 /* DrawViewController.swift in Sources */,
47D7808F1B1F1D1400076BD6 /* CircleDrawCommand.swift in Sources */,
47919A441B0CE08600D7BFE9 /* AppDelegate.swift in Sources */,
47D780891B1F1A9900076BD6 /* DrawCommands.swift in Sources */,
47D7808C1B1F1D0100076BD6 /* LineDrawCommand.swift in Sources */,
4712795D1B0CE98400A49B7E /* DrawView.swift in Sources */,
47702F9F1B0E448800417ACF /* Toolbar.swift in Sources */,
);
Expand Down
41 changes: 41 additions & 0 deletions FreehandDrawing-iOS/CircleDrawCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
The MIT License (MIT)
Copyright (c) 2015-present Badoo Trading Limited.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import UIKit

struct CircleDrawCommand : DrawCommand {

let center: CGPoint
let radius: CGFloat
let color: UIColor

// MARK: DrawCommand

func execute(canvas: Canvas) {
CGContextSetFillColorWithColor(canvas.context, self.color.CGColor)

CGContextAddArc(canvas.context, self.center.x, self.center.y, self.radius, 0, 2 * CGFloat(M_PI), 1)
CGContextFillPath(canvas.context)
}
}
33 changes: 33 additions & 0 deletions FreehandDrawing-iOS/DrawCommands.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2015-present Badoo Trading Limited.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import UIKit

protocol Canvas {
var context: CGContext {get}
}

protocol DrawCommand {
func execute(canvas: Canvas)
}
45 changes: 45 additions & 0 deletions FreehandDrawing-iOS/LineDrawCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
The MIT License (MIT)
Copyright (c) 2015-present Badoo Trading Limited.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

import UIKit

struct LineDrawCommand : DrawCommand {

let a: CGPoint
let b: CGPoint
let width: CGFloat
let color: UIColor

// MARK: DrawCommand

func execute(canvas: Canvas) {
CGContextSetStrokeColorWithColor(canvas.context, self.color.CGColor)
CGContextSetLineWidth(canvas.context, self.width)
CGContextSetLineCap(canvas.context, kCGLineCapRound)

CGContextMoveToPoint(canvas.context, a.x, a.y)
CGContextAddLineToPoint(canvas.context, b.x, b.y)
CGContextStrokePath(canvas.context)
}
}

0 comments on commit 44272d3

Please sign in to comment.