Advertisement
Guest User

RippleView.swift

a guest
Jan 16th, 2016
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.21 KB | None | 0 0
  1. //
  2. //  RippleView.swift
  3. //  Salt
  4. //
  5. //  Created by EDI_MBP on 15.01.16.
  6. //  Copyright © 2016 EDO. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import CoreGraphics
  11.  
  12. class RippleView: UIView {
  13.  
  14.     var buffer1 = [UInt8]()
  15.     var buffer2 = [UInt8]()
  16.    
  17.     var image = UIImage()
  18.    
  19.     let force = 255
  20.     let damp = 1.0
  21.    
  22.     let cols = 256
  23.     let rows = 256
  24.    
  25.     var newPoint = CGPoint()
  26.    
  27.     var hasNew = true
  28.    
  29.    
  30.     override init(frame: CGRect) {
  31.         super.init(frame: frame)
  32.         addBehaviour()
  33.     }
  34.    
  35.     convenience init() {
  36.         self.init(frame:CGRect.zero)
  37.     }
  38.    
  39.     required init(coder aDecoder: NSCoder) {
  40.         fatalError("This class does not support NSCoding")
  41.     }
  42.    
  43.     func addBehaviour() {
  44.        
  45.         backgroundColor = UIColor.yellowColor()
  46.        
  47.         buffer1 = [UInt8](count: cols*rows, repeatedValue: 0)
  48.         buffer2 = [UInt8](count: cols*rows, repeatedValue: 0)
  49.        
  50.         newPoint = CGPoint(x: 50, y: 50)
  51.        
  52.         let provider = CGDataProviderCreateWithData(nil, buffer2, buffer2.count, nil)
  53.        
  54.         let imageRef = CGImageCreate(cols, rows, 8, 8, rows, CGColorSpaceCreateDeviceGray(), CGBitmapInfo.ByteOrderDefault, provider, nil, false, CGColorRenderingIntent.RenderingIntentDefault)
  55.        
  56.         image = UIImage.init(CGImage: imageRef!)
  57.        
  58.         NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: "bang", userInfo: nil, repeats: true)
  59.        
  60.     }
  61.    
  62.     func bang() {
  63.        
  64.         self.setNeedsDisplay()
  65.        
  66.     }
  67.    
  68.     func swapBuffers() {
  69.        
  70.         let swap = buffer1
  71.         buffer1 = buffer2
  72.         buffer2 = swap
  73.        
  74.     }
  75.    
  76.     func positionX(x: NSInteger, y:NSInteger) -> NSInteger {
  77.        
  78.         return ((y * cols) + x)
  79.        
  80.     }
  81.    
  82.     override func drawRect(rect: CGRect) {
  83.        
  84.         if(hasNew) {
  85.            
  86.             let x = NSInteger(newPoint.x)
  87.             let y = NSInteger(newPoint.y)
  88.            
  89.             //print("Has new Point: (\(x), \(y))")
  90.            
  91.             let blip = force
  92.            
  93.             buffer1[positionX(x, y: y)] = UInt8.init(blip)
  94.             buffer1[positionX(x-1, y: y)] = UInt8.init(blip)
  95.             buffer1[positionX(x+1, y: y)] = UInt8.init(blip)
  96.            
  97.             buffer1[positionX(x-1, y: y-1)] = UInt8.init(blip)
  98.             buffer1[positionX(x, y: y-1)] = UInt8.init(blip)
  99.             buffer1[positionX(x+1, y: y-1)] = UInt8.init(blip)
  100.            
  101.             buffer1[positionX(x-1, y: y+1)] = UInt8.init(blip)
  102.             buffer1[positionX(x, y: y+1)] = UInt8.init(blip)
  103.             buffer1[positionX(x+1, y: y+1)] = UInt8.init(blip)
  104.            
  105.             hasNew = false
  106.            
  107.         }
  108.        
  109.         // propagate waves
  110.         processRipples(buffer1, dest:&buffer2)
  111.        
  112.         // apply waves to image
  113.         applyBuffer()
  114.        
  115.         // draw image
  116.         image.drawAtPoint(CGPointMake(0.0, 0.0))
  117.        
  118.         // swap buffers
  119.         swapBuffers()
  120.        
  121.     }
  122.    
  123.     func applyBuffer() {
  124.        
  125.        
  126.        
  127.     }
  128.    
  129.     func processRipples(source:[UInt8], inout dest: [UInt8]) {
  130.        
  131.         var position = 0
  132.        
  133.         for y in 1...(rows - 2) {
  134.            
  135.             for x in 1...(cols - 2) {
  136.                
  137.                 position = (y * cols) + x
  138.                
  139.                 let w = Int.init(source[position - cols])
  140.                 let a = Int.init(source[position - 1])
  141.                 let s = Int.init(source[position + 1])
  142.                 let d = Int.init(source[position + cols])
  143.                
  144.                 var sum = w + a + s + d
  145.                
  146.                 sum = sum / 2
  147.                
  148.                 sum = sum - Int.init(dest[position])
  149.                
  150.                 if(sum > 255) {
  151.                    
  152.                     sum = 255
  153.                    
  154.                 } else if(sum < 0) {
  155.                    
  156.                     sum = 0
  157.                    
  158.                 }
  159.                
  160.                 dest[position] = UInt8.init(sum)
  161.                
  162.                 dest[position] = UInt8.init(Double(dest[position]) * damp)
  163.                
  164.             }
  165.            
  166.         }
  167.        
  168.     }
  169.    
  170.     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  171.        
  172.         let point = (touches.first! as UITouch).locationInView(self)
  173.        
  174.         if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
  175.            
  176.             hasNew = true
  177.            
  178.             newPoint = point
  179.            
  180.             //newPoint.y = CGFloat(rows) - newPoint.y
  181.            
  182.         }
  183.        
  184.     }
  185.    
  186.     override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
  187.        
  188.         let point = (touches.first! as UITouch).locationInView(self)
  189.        
  190.         if(point.x >= 1 && point.x < 255 && point.y >= 1 && point.y < 255) {
  191.            
  192.             hasNew = true
  193.            
  194.             newPoint = point
  195.            
  196.             //newPoint.y = CGFloat(rows) - newPoint.y
  197.            
  198.         }
  199.        
  200.     }
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement