From 29010486bb9303ab41168c7e030daca7f6495c8f Mon Sep 17 00:00:00 2001 From: workinghard Date: Wed, 15 Apr 2015 23:32:30 -0700 Subject: [PATCH] a --- Cube4Fun/Animations.swift | 56 ++++++++++++++++++++++++++--- Cube4Fun/AnimationsController.swift | 52 ++++++++++++++++++++++++--- Cube4Fun/GameView.swift | 48 +++++++++++++++++++------ Cube4Fun/GameViewController.swift | 17 +++++---- 4 files changed, 147 insertions(+), 26 deletions(-) diff --git a/Cube4Fun/Animations.swift b/Cube4Fun/Animations.swift index 21a32de..cd231d1 100644 --- a/Cube4Fun/Animations.swift +++ b/Cube4Fun/Animations.swift @@ -41,7 +41,7 @@ class Animations: NSObject { var _animationSelected: Int = 0 let _minSendDelay: NSTimeInterval = 0.200 // fastes speed 200 milliseconds let maxFrameSpeed = 3000 // the lowest possible speed allowed - let minFrameSpeed = 200 // the fastest possible speed allowed + let minFrameSpeed = 100 // the fastest possible speed allowed let frameSpeedStep = 100 // how fast increase or decrease speed // var _playSendDelay: NSTimeInterval = 0.5 // 500 milliseconds as default @@ -60,6 +60,33 @@ class Animations: NSObject { return _animationArray.count } + func sendFrame() { + let time = NSDate.timeIntervalSinceReferenceDate() + + if (_previousUpdateTime == 0.0) { + _previousUpdateTime = time; + } + // need to do this because of rounding issues + let deltaTime: Int = Int( round( (time - _previousUpdateTime) * 10 ) * 100); + + //var ms = Int((time % 1) * 1000) + if ( _playAllFrames ) { + println("Delta: \(deltaTime) Speed: \(__animations.animationSpeedInt())") + if ( deltaTime >= __animations.animationSpeedInt() ){ + _previousUpdateTime = time; + if (self.getAnimationFrameID() >= self.getAnimationFrameCount()) { + _gameView.firstButtonPressed() + }else{ + _gameView.nextButtonPressed() + } + CubeNetworkObj.updateFrame(self.getAnimDataSelected(), count: UInt32(self.getAnimationFrameID())) + + } + }else{ + CubeNetworkObj.updateFrame(self.getAnimDataSelected(), count: UInt32(self.getAnimationFrameID())) + } + } + func loadAnimations(animArray: NSArray) { // clear the array first _animationArray.removeAll(keepCapacity: true) @@ -116,7 +143,7 @@ class Animations: NSObject { } func getAnimationFrameCount(id: Int) -> (Int) { let myFrames: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData - return myFrames.length / 64 + return (myFrames.length / 64) //let value = (self.getAnimation(id)).objectForKey("AnimFrames") as Int //return value } @@ -223,16 +250,37 @@ class Animations: NSObject { return _minSendDelay } - func getAnimData() -> (UnsafePointer) { + func getAnimDataSelected() -> (UnsafePointer) { let myData: NSMutableData = (self.getAnimation(_animationSelected)).objectForKey(AnimFrames) as! NSMutableData let byteArray = UnsafePointer(myData.bytes) return byteArray } + func getAnimData(id: Int) -> (UnsafePointer) { + let myData: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData + let byteArray = UnsafePointer(myData.bytes) + return byteArray + } + + func getAnimDataLength(id: Int) -> Int { + let myFrames: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData + return myFrames.length + } + func setLEDColor(color: UInt8, led: Int) { + println("Led pressed: \(led)") var myByte: [UInt8] = [color] let myData: NSMutableData = (self.getAnimation(_animationSelected)).objectForKey(AnimFrames) as! NSMutableData - myData.replaceBytesInRange(NSMakeRange(((self.getAnimationFrameID()-1)*64)+led, 1), withBytes: myByte) + let bytePosition = NSMakeRange(((self.getAnimationFrameID()-1)*64)+led, 1) + myData.replaceBytesInRange(bytePosition, withBytes: myByte) + + // Send updated frame + self.sendFrame() + } + + func getLEDColor(pos: Int) -> (UInt8) { + let myData = self.getAnimDataSelected() + return myData[pos] } func clearLEDColor() { diff --git a/Cube4Fun/AnimationsController.swift b/Cube4Fun/AnimationsController.swift index 7617560..b0ac400 100644 --- a/Cube4Fun/AnimationsController.swift +++ b/Cube4Fun/AnimationsController.swift @@ -159,20 +159,62 @@ class AnimationsController: NSObject, NSTableViewDataSource, NSTableViewDelegate } @IBAction func exportAnimations(send: AnyObject) { - let testdata: [UInt8] = [254, 1, 128, 255] + var sendData: [UInt8] = [UInt8]() println("Import button pressed") // for each animation - - // Create header line per animation + for ( var i = 0; i < __animations.count(); i++ ) { + // Create header line per animation + // Syntax: ,F,\n + + // Key + sendData.append(UInt8(ascii: ",")) + sendData.append(UInt8(ascii: "F")) + let key = __animations.getAnimationKey(i) + let keyArray: NSData = key.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)! + let keyBytes: UnsafePointer = UnsafePointer(keyArray.bytes) + for (var j = 0; j < keyArray.length; j++) { + sendData.append(UInt8(keyBytes[j])) + } + sendData.append(UInt8(ascii: ",")) + + // Playtime + let playTime: [UInt8] = convertInt16(UInt16(__animations.getAnimationDuration(i))) + sendData.append(playTime[0]) + sendData.append(playTime[1]) - // Append frame, separated by new-Line + // Speed + let playSpeed: [UInt8] = convertInt16(UInt16(__animations.getAnimationSpeed(i))) + sendData.append(playSpeed[0]) + sendData.append(playSpeed[1]) + + // Frames + let playFrames: [UInt8] = convertInt16(UInt16(__animations.getAnimationFrameCount(i))) + sendData.append(playFrames[0]) + sendData.append(playFrames[1]) + + // End line + sendData.append(UInt8(ascii: "\n")) + + // Append frame, separated by new-Line + let animData = __animations.getAnimData(i) + for ( var count = 1; count <= __animations.getAnimDataLength(i); count++) { + sendData.append(animData[count-1]) + // End line for each frame + if ( (count % 64) == 0 ) { + sendData.append(UInt8(ascii: "\n")) + } + } + + + } + // Calculate overall data to send // Send data - CubeNetworkObj.sendBytes(testdata, count: UInt32(testdata.count)) + CubeNetworkObj.sendBytes(sendData , count: UInt32(sendData.count)) } diff --git a/Cube4Fun/GameView.swift b/Cube4Fun/GameView.swift index 2bba0b3..fed3f39 100644 --- a/Cube4Fun/GameView.swift +++ b/Cube4Fun/GameView.swift @@ -16,12 +16,29 @@ let relativeBarPosition: CGFloat = 500.0 class GameView: SCNView { + var playTimer: NSTimer = NSTimer() + + func scheduleTimer() { + playTimer = NSTimer.scheduledTimerWithTimeInterval(__animations.animationSpeedFloat(), target: __animations, selector: Selector("sendFrame"), userInfo: nil, repeats: true) + } + func resetTimer() { + playTimer.invalidate() + } + func rescheduleTimer() { + resetTimer(); + scheduleTimer(); + } + func resetView() { + println("Reset view") // goto first frame self.firstButtonPressed() // Update speed self.updateSpeedText() + + // Send updated frame + __animations.sendFrame() } func updateButtonVisibility() { @@ -99,6 +116,9 @@ class GameView: SCNView { } } + + // Send updated frame + __animations.sendFrame() } @@ -106,7 +126,7 @@ class GameView: SCNView { // get actuall frame data // let data : UnsafePointer = UnsafePointer(myFrames.mutableBytes) - let data = __animations.getAnimData() + //let data = __animations.getAnimDataSelected() //NSMutableData if let rootNode = self.scene?.rootNode { if let cubeNode = rootNode.childNodeWithName("cubeNode", recursively: true) { @@ -118,8 +138,8 @@ class GameView: SCNView { if let material: SCNMaterial = geometry.firstMaterial { var color: NSColor = NSColor() let colorPosition: Int = ((__animations.getAnimationFrameID()-1)*64) + ledID - let savedColor: UInt8 = data[colorPosition] - if data[colorPosition] == 255 { + let savedColor: UInt8 = __animations.getLEDColor(colorPosition) //data[colorPosition] + if savedColor == 255 { color = NSColor.grayColor() }else{ let hueColor = CGFloat(savedColor) / 255.0 @@ -135,6 +155,7 @@ class GameView: SCNView { } } } + //__animations.sendFrame() } func plusButtonPressed() { @@ -226,6 +247,7 @@ class GameView: SCNView { // _playSendDelay = _playSendDelay + 0.1 updateSpeedText() // } + rescheduleTimer() } func minusSpeedButtonPressed() { @@ -235,6 +257,7 @@ class GameView: SCNView { // _playSendDelay = _playSendDelay - 0.1 updateSpeedText() // } + rescheduleTimer() } func updateSpeedText() { @@ -261,6 +284,7 @@ class GameView: SCNView { // Start the animation _playAllFrames = true + scheduleTimer(); } func pauseButtonPressed() { @@ -272,6 +296,7 @@ class GameView: SCNView { // Stop the animation _playAllFrames = false + resetTimer() } func openAnimationWindow() { @@ -409,6 +434,15 @@ class GameView: SCNView { } ledPressed = Int(name.intValue) + // Update the LED frame + var myByte: UInt8 + if ledColorOn { + myByte = klickedColor + }else{ + myByte = 255 // Off + } + __animations.setLEDColor(myByte, led: ledPressed) + } } } @@ -460,14 +494,6 @@ class GameView: SCNView { SCNTransaction.commit() - // Update the LED frame - var myByte: UInt8 - if ledColorOn { - myByte = klickedColor - }else{ - myByte = 255 // Off - } - __animations.setLEDColor(myByte, led: ledPressed) //myFrames.replaceBytesInRange(NSMakeRange(((myFrameCount-1)*64)+ledPressed, 1), withBytes: myByte) } diff --git a/Cube4Fun/GameViewController.swift b/Cube4Fun/GameViewController.swift index 40b3441..e2cb133 100644 --- a/Cube4Fun/GameViewController.swift +++ b/Cube4Fun/GameViewController.swift @@ -62,10 +62,12 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate } */ - func sendFrame() { - sendFrame(NSDate.timeIntervalSinceReferenceDate()) - } + + //func sendFrame() { + // sendFrame(NSDate.timeIntervalSinceReferenceDate()) + //} + /* func sendFrame(time: NSTimeInterval) { //println(time) @@ -82,18 +84,20 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate }else{ self.gameView!.nextButtonPressed() } - CubeNetworkObj.updateFrame(__animations.getAnimData(), count: UInt32(__animations.getAnimationFrameID())) + CubeNetworkObj.updateFrame(__animations.getAnimDataSelected(), count: UInt32(__animations.getAnimationFrameID())) _previousUpdateTime = time; } }else{ if ( _deltaTime >= __animations.getMinSendDelay() ) { - CubeNetworkObj.updateFrame(__animations.getAnimData(), count: UInt32(__animations.getAnimationFrameID())) + CubeNetworkObj.updateFrame(__animations.getAnimDataSelected(), count: UInt32(__animations.getAnimationFrameID())) //println("SendFrame: \(_deltaTime)") _previousUpdateTime = time; } } //CubeNetworkObj.updateFrame(UnsafePointer(myFrames.bytes), count: myFrameCount) } + +*/ override func awakeFromNib(){ @@ -107,9 +111,10 @@ class GameViewController: NSViewController { // SCNSceneRendererDelegate // myFrameCount = 1 // Open connection to the LED cube CubeNetworkObj.openConnection() + __animations.sendFrame() // Fallback timer if nothing render at the moment - NSTimer.scheduledTimerWithTimeInterval(__animations.getMinSendDelay(), target: self, selector: Selector("sendFrame"), userInfo: nil, repeats: true) + // NSTimer.scheduledTimerWithTimeInterval(__animations.getMinSendDelay(), target: self, selector: Selector("sendFrame"), userInfo: nil, repeats: true)