Skip to content

Instantly share code, notes, and snippets.

@jbrennan
Created March 15, 2016 02:01
Show Gist options
  • Save jbrennan/cf62eecc9825782d2cf9 to your computer and use it in GitHub Desktop.
Save jbrennan/cf62eecc9825782d2cf9 to your computer and use it in GitHub Desktop.
Example of using setting up Prototope in a UIViewController
//
// ViewController.swift
// DrawingProto
//
// Created by Jason Brennan on 2015-12-19.
// Copyright © 2015 Jason Brennan. All rights reserved.
//
import UIKit
import Prototope
class ViewController: UIViewController {
let canvasLayer = Layer(name: "canvas")
override func viewDidLoad() {
super.viewDidLoad()
Environment.currentEnvironment = Environment.defaultEnvironmentWithRootView(view)
let rootLayer = Layer.root
rootLayer.backgroundColor = Color(brightness: 95)
canvasLayer.parent = rootLayer
canvasLayer.backgroundColor = Color.white
canvasLayer.touchEndedHandler = { [weak self] _ in
self?.selectLayer(nil)
}
let textButton = buttonWithTitle("Text")
textButton.touchEndedHandler = { [weak self] touchSequence in
let blankText = Layer(parent: self!.canvasLayer, image: Image(text: "tap to edit"))
blankText.touchBeganHandler = { _ in
self?.selectLayer(blankText)
}
blankText.touchMovedHandler = { touchSequence in
let position = touchSequence.currentSample.locationInLayer(self!.canvasLayer)
blankText.position = position.clampedInsideRect(self!.canvasLayer.frame)
}
}
textButton.origin = Point(x: 30, y: 500)
let rectangleButton = buttonWithTitle("Rectangle")
rectangleButton.moveToRightOfSiblingLayer(textButton, margin: 10)
rectangleButton.y = textButton.y
rectangleButton.touchEndedHandler = { [weak self] touchSequence in
let rectLayer = Layer(parent: self!.canvasLayer)
rectLayer.backgroundColor = Palette.red
rectLayer.cornerRadius = 4
rectLayer.touchBeganHandler = { _ in
self?.selectLayer(rectLayer)
}
rectLayer.touchMovedHandler = { touchSequence in
let position = touchSequence.currentSample.locationInLayer(self!.canvasLayer)
rectLayer.position = position.clampedInsideRect(self!.canvasLayer.frame)
}
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let width = Layer.root.width
canvasLayer.moveToCenterOfParentLayer()
canvasLayer.originY = 0
canvasLayer.size = Size(width: width, height: width)
}
func buttonWithTitle(title: String) -> Layer {
let labelColor = Color(hue: 285, brightness: 29)
let button = Layer(parent: Layer.root, image: Image(text: title, textColor: labelColor))
return button
}
var currentlySelectedLayer: Layer?
func selectLayer(layer: Layer?) {
currentlySelectedLayer?.border.width = 0
if let layer = layer {
layer.border = Border(color: Palette.selectedYellow, width: 4.0)
currentlySelectedLayer = layer
}
}
enum Kind {
case None
case Rectangle
}
}
extension Color {
/** HSB colour with values on the natural scale. Hue betwee 0-360, saturation between 0-100, brightness between 0-100. */
init(hue: Int = 0, saturation: Int = 0, brightness: Int = 0) {
self.init(hue: Double(hue) / 360.0, saturation: Double(saturation) / 100.0, brightness: Double(brightness) / 100.0)
}
}
extension Point {
func clampedInsideRect(rect: Rect) -> Point {
var p = self
if p.x < rect.minX { p.x = 0 }
if p.x > rect.maxX { p.x = rect.maxX }
if p.y < rect.minY { p.y = 0 }
if p.y > rect.maxY { p.y = rect.maxY }
return p
}
}
struct Palette {
static var red: Color { return Color(hue: 353, saturation: 99, brightness: 82) }
// Really 49.39, 76.4, 100
static var selectedYellow: Color { return Color(hue: 49, saturation: 76, brightness: 100) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment