Permalink
Browse files

Adds composed command and uses it to undo whole lines instead of segm…

…ents of lines
  • Loading branch information...
DarthMike committed Jun 15, 2015
1 parent 95e27c8 commit 69d26fdf5da106eb34a1f9a37ae0d26fb4242bab
@@ -20,6 +20,7 @@
47D7808C1B1F1D0100076BD6 /* LineDrawCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D7808B1B1F1D0100076BD6 /* LineDrawCommand.swift */; };
47D7808F1B1F1D1400076BD6 /* CircleDrawCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D7808E1B1F1D1400076BD6 /* CircleDrawCommand.swift */; };
47D780931B1F238300076BD6 /* FreehandDrawController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47D780921B1F238300076BD6 /* FreehandDrawController.swift */; };
+ 47E090BD1B2F007D002549B2 /* ComposedCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47E090BC1B2F007D002549B2 /* ComposedCommand.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -50,6 +51,7 @@
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>"; };
47D780921B1F238300076BD6 /* FreehandDrawController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FreehandDrawController.swift; sourceTree = "<group>"; };
+ 47E090BC1B2F007D002549B2 /* ComposedCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComposedCommand.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -145,6 +147,7 @@
47D780881B1F1A9900076BD6 /* DrawCommands.swift */,
47D7808B1B1F1D0100076BD6 /* LineDrawCommand.swift */,
47D7808E1B1F1D1400076BD6 /* CircleDrawCommand.swift */,
+ 47E090BC1B2F007D002549B2 /* ComposedCommand.swift */,
);
name = DrawCommands;
sourceTree = "<group>";
@@ -256,6 +259,7 @@
47D780891B1F1A9900076BD6 /* DrawCommands.swift in Sources */,
47D7808C1B1F1D0100076BD6 /* LineDrawCommand.swift in Sources */,
4712795D1B0CE98400A49B7E /* DrawView.swift in Sources */,
+ 47E090BD1B2F007D002549B2 /* ComposedCommand.swift in Sources */,
47D780931B1F238300076BD6 /* FreehandDrawController.swift in Sources */,
47702F9F1B0E448800417ACF /* Toolbar.swift in Sources */,
);
@@ -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.
+*/
+
+struct ComposedCommand : DrawCommand {
+ init(commands: [DrawCommand]) {
+ self.commands = commands;
+ }
+
+ // MARK: DrawCommand
+
+ func execute(canvas: Canvas) {
+ self.commands.map { $0.execute(canvas) }
+ }
+
+ mutating func addCommand(command: DrawCommand) {
+ self.commands.append(command)
+ }
+
+ private var commands: [DrawCommand]
+}
@@ -68,19 +68,25 @@ class FreehandDrawController : NSObject {
private func startAtPoint(point: CGPoint) {
self.lastPoint = point
+ self.lineStrokeCommand = ComposedCommand(commands: [])
}
private func continueAtPoint(point: CGPoint) {
let lineCommand = LineDrawCommand(a: self.lastPoint, b: point, width: self.width, color: self.color)
self.canvas.executeCommands([lineCommand])
- self.commandQueue.append(lineCommand)
-
+
+ self.lineStrokeCommand?.addCommand(lineCommand)
self.lastPoint = point
}
private func endAtPoint(point: CGPoint) {
+ if let lineStrokeCommand = self.lineStrokeCommand {
+ self.commandQueue.append(lineStrokeCommand)
+ }
+
self.lastPoint = CGPointZero
+ self.lineStrokeCommand = nil
}
private func tapAtPoint(point: CGPoint) {
@@ -90,6 +96,7 @@ class FreehandDrawController : NSObject {
}
private let canvas: protocol<Canvas, DrawCommandReceiver>
+ private var lineStrokeCommand: ComposedCommand?
private var commandQueue: Array<DrawCommand> = []
private var lastPoint: CGPoint = CGPointZero
}

0 comments on commit 69d26fd

Please sign in to comment.