Permalink
Browse files

Intial undo implementation. Refactored DrawCommandReceiver to accept …

…an array of commands to execute
  • Loading branch information...
DarthMike committed Jun 15, 2015
1 parent e7f5f39 commit 95e27c8ec91d169ab8e91a16692a3e3c58a26e2f
@@ -26,12 +26,13 @@ import UIKit
protocol Canvas {
var context: CGContext {get}
func reset()
}
protocol DrawCommand {
func execute(canvas: Canvas)
}
protocol DrawCommandReceiver {
func executeCommand(command: DrawCommand)
func executeCommands(commands: [DrawCommand])
}
@@ -32,12 +32,17 @@ class DrawView : UIView, Canvas, DrawCommandReceiver {
return UIGraphicsGetCurrentContext()
}
func reset() {
self.buffer = nil
self.layer.contents = nil
}
// MARK: DrawCommandReceiver
func executeCommand(command: DrawCommand) {
func executeCommands(commands: [DrawCommand]) {
autoreleasepool {
self.buffer = drawInContext { context in
command.execute(self)
commands.map { $0.execute(self) }
}
self.layer.contents = self.buffer?.CGImage ?? nil
@@ -33,6 +33,10 @@ class DrawViewController: UIViewController {
self.toolbar.colorChangeHandler = { [unowned self] color in
self.drawController.color = color
}
self.toolbar.undoHandler = { [unowned self] in
self.drawController.undo()
}
}
var drawView: DrawView {
@@ -19,6 +19,16 @@ class FreehandDrawController : NSObject {
self.setupGestureRecognizersInView(view)
}
// MARK: API
func undo() {
if self.commandQueue.count > 0{
self.commandQueue.removeLast()
self.canvas.reset()
self.canvas.executeCommands(self.commandQueue)
}
}
// MARK: Gestures
private func setupGestureRecognizersInView(view: UIView) {
@@ -63,7 +73,7 @@ class FreehandDrawController : NSObject {
private func continueAtPoint(point: CGPoint) {
let lineCommand = LineDrawCommand(a: self.lastPoint, b: point, width: self.width, color: self.color)
self.canvas.executeCommand(lineCommand)
self.canvas.executeCommands([lineCommand])
self.commandQueue.append(lineCommand)
self.lastPoint = point
@@ -75,7 +85,7 @@ class FreehandDrawController : NSObject {
private func tapAtPoint(point: CGPoint) {
let circleCommand = CircleDrawCommand(center: point, radius: self.width/2.0, color: self.color)
self.canvas.executeCommand(circleCommand)
self.canvas.executeCommands([circleCommand])
self.commandQueue.append(circleCommand)
}
@@ -26,7 +26,7 @@ import UIKit
class Toolbar : UIView {
typealias ColorChangeHandler = UIColor -> Void
typealias UndoHandler = Void->Void
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
@@ -86,8 +86,12 @@ class Toolbar : UIView {
}
}
var colorChangeHandler: ColorChangeHandler?
@IBAction private func undo() {
self.undoHandler?()
}
var colorChangeHandler: ColorChangeHandler?
var undoHandler: UndoHandler?
@IBOutlet var color1: UIView!
@IBOutlet var color2: UIView!
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7706" systemVersion="14C1514" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7706" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7706"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
@@ -26,19 +26,34 @@
<rect key="frame" x="356" y="16" width="48" height="48"/>
<color key="backgroundColor" red="1" green="0.0" blue="0.035351606610000003" alpha="1" colorSpace="calibratedRGB"/>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="agr-xx-Fzd">
<rect key="frame" x="40" y="18" width="44" height="44"/>
<constraints>
<constraint firstAttribute="height" constant="44" id="Qqi-in-yNv"/>
<constraint firstAttribute="width" constant="44" id="b4J-dz-21G"/>
</constraints>
<state key="normal" title="Undo">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="undo" destination="iN0-l3-epB" eventType="touchUpInside" id="NB1-fH-G4a"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="L75-iP-5nx" firstAttribute="width" secondItem="v4K-Q7-6O9" secondAttribute="width" id="0qv-QT-qyR"/>
<constraint firstItem="L75-iP-5nx" firstAttribute="centerY" secondItem="v4K-Q7-6O9" secondAttribute="centerY" id="4i2-9z-WNX"/>
<constraint firstItem="0L8-d7-abR" firstAttribute="centerY" secondItem="v4K-Q7-6O9" secondAttribute="centerY" id="Ee9-kP-nkR"/>
<constraint firstItem="agr-xx-Fzd" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="40" id="Eis-rR-Zxp"/>
<constraint firstItem="0L8-d7-abR" firstAttribute="height" secondItem="v4K-Q7-6O9" secondAttribute="height" id="J6U-bW-hdD"/>
<constraint firstItem="L75-iP-5nx" firstAttribute="height" secondItem="v4K-Q7-6O9" secondAttribute="height" id="PJ4-Os-mZO"/>
<constraint firstAttribute="trailing" secondItem="L75-iP-5nx" secondAttribute="trailing" constant="40" id="SLm-Ge-EfH"/>
<constraint firstItem="L75-iP-5nx" firstAttribute="leading" secondItem="v4K-Q7-6O9" secondAttribute="trailing" constant="30" id="Z7Z-yg-psY"/>
<constraint firstAttribute="centerY" secondItem="L75-iP-5nx" secondAttribute="centerY" id="hqU-7u-2N4"/>
<constraint firstItem="0L8-d7-abR" firstAttribute="width" secondItem="v4K-Q7-6O9" secondAttribute="width" id="uDg-G8-vKG"/>
<constraint firstItem="v4K-Q7-6O9" firstAttribute="leading" secondItem="0L8-d7-abR" secondAttribute="trailing" constant="30" id="yAp-rd-xzU"/>
<constraint firstAttribute="centerY" secondItem="agr-xx-Fzd" secondAttribute="centerY" id="yr7-Dz-xGS"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>

0 comments on commit 95e27c8

Please sign in to comment.