mirror of
https://github.com/workinghard/Cube4Fun.git
synced 2025-12-13 20:32:09 +00:00
a
This commit is contained in:
@@ -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<UInt8>) {
|
||||
func getAnimDataSelected() -> (UnsafePointer<UInt8>) {
|
||||
let myData: NSMutableData = (self.getAnimation(_animationSelected)).objectForKey(AnimFrames) as! NSMutableData
|
||||
let byteArray = UnsafePointer<UInt8>(myData.bytes)
|
||||
return byteArray
|
||||
}
|
||||
|
||||
func getAnimData(id: Int) -> (UnsafePointer<UInt8>) {
|
||||
let myData: NSMutableData = (self.getAnimation(id)).objectForKey(AnimFrames) as! NSMutableData
|
||||
let byteArray = UnsafePointer<UInt8>(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() {
|
||||
|
||||
@@ -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<key:12Byte>,<playtime:2Byte><speed:2Byte><frames:2Byte>\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<UInt8> = UnsafePointer<UInt8>(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))
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Byte> = UnsafePointer<Byte>(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)
|
||||
|
||||
}
|
||||
|
||||
@@ -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<UInt8>(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)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user