Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // RippleView.swift
- // Salt
- //
- // Created by EDI_MBP on 15.01.16.
- // Copyright © 2016 EDO. All rights reserved.
- //
- import UIKit
- import CoreGraphics
- class RippleView: UIView {
- var buffer1 = [UInt8]()
- var buffer2 = [UInt8]()
- var image = UIImage()
- let force = 255
- let damp = 1.0
- let cols = 256
- let rows = 256
- var newPoint = CGPoint()
- var hasNew = true
- override init(frame: CGRect) {
- super.init(frame: frame)
- addBehaviour()
- }
- convenience init() {
- self.init(frame:CGRect.zero)
- }
- required init(coder aDecoder: NSCoder) {
- fatalError("This class does not support NSCoding")
- }
- func addBehaviour() {
- backgroundColor = UIColor.yellowColor()
- buffer1 = [UInt8](count: cols*rows, repeatedValue: 0)
- buffer2 = [UInt8](count: cols*rows, repeatedValue: 0)
- newPoint = CGPoint(x: 50, y: 50)
- let provider = CGDataProviderCreateWithData(nil, buffer2, buffer2.count, nil)
- let imageRef = CGImageCreate(cols, rows, 8, 8, rows, CGColorSpaceCreateDeviceGray(), CGBitmapInfo.ByteOrderDefault, provider, nil, false, CGColorRenderingIntent.RenderingIntentDefault)
- image = UIImage.init(CGImage: imageRef!)
- NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: "bang", userInfo: nil, repeats: true)
- }
- func bang() {
- self.setNeedsDisplay()
- }
- func swapBuffers() {
- let swap = buffer1
- buffer1 = buffer2
- buffer2 = swap
- }
- func positionX(x: NSInteger, y:NSInteger) -> NSInteger {
- return ((y * cols) + x)
- }
- override func drawRect(rect: CGRect) {
- if(hasNew) {
- let x = NSInteger(newPoint.x)
- let y = NSInteger(newPoint.y)
- //print("Has new Point: (\(x), \(y))")
- let blip = force
- buffer1[positionX(x, y: y)] = UInt8.init(blip)
- buffer1[positionX(x-1, y: y)] = UInt8.init(blip)
- buffer1[positionX(x+1, y: y)] = UInt8.init(blip)
- buffer1[positionX(x-1, y: y-1)] = UInt8.init(blip)
- buffer1[positionX(x, y: y-1)] = UInt8.init(blip)
- buffer1[positionX(x+1, y: y-1)] = UInt8.init(blip)
- buffer1[positionX(x-1, y: y+1)] = UInt8.init(blip)
- buffer1[positionX(x, y: y+1)] = UInt8.init(blip)
- buffer1[positionX(x+1, y: y+1)] = UInt8.init(blip)
- hasNew = false
- }
- // propagate waves
- processRipples(buffer1, dest:&buffer2)
- // apply waves to image
- applyBuffer()
- // draw image
- image.drawAtPoint(CGPointMake(0.0, 0.0))
- // swap buffers
- swapBuffers()
- }
- func applyBuffer() {
- }
- func processRipples(source:[UInt8], inout dest: [UInt8]) {
- var position = 0
- for y in 1...(rows - 2) {
- for x in 1...(cols - 2) {
- position = (y * cols) + x
- let w = Int.init(source[position - cols])
- let a = Int.init(source[position - 1])
- let s = Int.init(source[position + 1])
- let d = Int.init(source[position + cols])
- var sum = w + a + s + d
- sum = sum / 2
- sum = sum - Int.init(dest[position])
- if(sum > 255) {
- sum = 255
- } else if(sum < 0) {
- sum = 0
- }
- dest[position] = UInt8.init(sum)
- dest[position] = UInt8.init(Double(dest[position]) * damp)
- }
- }
- }
- override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
- let point = (touches.first! as UITouch).locationInView(self)
- if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
- hasNew = true
- newPoint = point
- //newPoint.y = CGFloat(rows) - newPoint.y
- }
- }
- override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
- let point = (touches.first! as UITouch).locationInView(self)
- if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
- hasNew = true
- newPoint = point
- //newPoint.y = CGFloat(rows) - newPoint.y
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement