Modernize swift code, fix a few visualization bugs

This commit is contained in:
Charles Magahern
2016-07-06 22:11:48 -07:00
parent aac8320058
commit 2f5c807dc6
4 changed files with 46 additions and 15 deletions

View File

@@ -247,7 +247,7 @@
TargetAttributes = { TargetAttributes = {
0C3CAEA31C3350C800B856AD = { 0C3CAEA31C3350C800B856AD = {
CreatedOnToolsVersion = 7.2; CreatedOnToolsVersion = 7.2;
DevelopmentTeam = TNUURJU7SZ; DevelopmentTeam = FZ6BMA5HA9;
}; };
}; };
}; };

View File

@@ -138,7 +138,7 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate {
var activatedDevicesCount = 0 var activatedDevicesCount = 0
for device in self.devices { for device in self.devices {
if (device.state == .On) { if (device.state == .On) {
++activatedDevicesCount activatedDevicesCount += 1
} }
} }

View File

@@ -48,7 +48,7 @@ class SwitchesViewController: UIViewController,
static let count: Int = { static let count: Int = {
var max = 0 var max = 0
while let _ = ActionCell(rawValue: max) { ++max } while let _ = ActionCell(rawValue: max) { max += 1 }
return max return max
}() }()
} }
@@ -104,7 +104,7 @@ class SwitchesViewController: UIViewController,
// if we have devices now and we didn't before, or vice versa, // if we have devices now and we didn't before, or vice versa,
// we need to update the action cells // we need to update the action cells
if ((oldValue.count == 0 && self.devices.count != 0) || (self.devices.count == 0 && oldValue.count != 0)) { if ((oldValue.count == 0 && self.devices.count != 0) || (self.devices.count == 0 && oldValue.count != 0)) {
for var actionCellIdx = 0; actionCellIdx < ActionCell.count; ++actionCellIdx { for actionCellIdx in 0 ..< ActionCell.count {
let actionCellIndexPath = NSIndexPath(forItem: actionCellIdx, inSection: 0) let actionCellIndexPath = NSIndexPath(forItem: actionCellIdx, inSection: 0)
updatedIndexPaths.append(actionCellIndexPath) updatedIndexPaths.append(actionCellIndexPath)
} }
@@ -197,7 +197,7 @@ class SwitchesViewController: UIViewController,
let tappedActionCell = ActionCell(rawValue: indexPath.item) let tappedActionCell = ActionCell(rawValue: indexPath.item)
var currentDelay: NSTimeInterval = 0.0 var currentDelay: NSTimeInterval = 0.0
for var i = ActionCell.count; i < collectionView.numberOfItemsInSection(indexPath.section); ++i { for i in ActionCell.count ..< collectionView.numberOfItemsInSection(indexPath.section) {
let cell = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: i, inSection: indexPath.section)) as! WemoDeviceCellView let cell = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: i, inSection: indexPath.section)) as! WemoDeviceCellView
let animOptions = UIViewAnimationOptions([.AllowUserInteraction]) let animOptions = UIViewAnimationOptions([.AllowUserInteraction])

View File

@@ -50,6 +50,8 @@ class VisualizationViewController: UIViewController {
_setupModel() _setupModel()
_setupAnimation() _setupAnimation()
_setupEffects() _setupEffects()
_beginModelResetTimer()
} }
override func viewDidAppear(animated: Bool) override func viewDidAppear(animated: Bool)
@@ -110,7 +112,7 @@ class VisualizationViewController: UIViewController {
for cublet in shuffledCublets { for cublet in shuffledCublets {
_setCubletActivated(cublet, activated: (cubletsActivated < cubletsToActivate)) _setCubletActivated(cublet, activated: (cubletsActivated < cubletsToActivate))
++cubletsActivated cubletsActivated += 1
} }
if (animated) { if (animated) {
@@ -175,6 +177,7 @@ class VisualizationViewController: UIViewController {
internal func _setupModel() internal func _setupModel()
{ {
_cubletsNode.enumerateChildNodesUsingBlock { $0.0.removeFromParentNode() }
_cublets.removeAll() _cublets.removeAll()
let sz = Float(VisualizationViewController.cubletsSize) let sz = Float(VisualizationViewController.cubletsSize)
@@ -191,13 +194,13 @@ class VisualizationViewController: UIViewController {
material.transparency = 0.75 material.transparency = 0.75
// generate nodes for each cublet // generate nodes for each cublet
for var xi = 0; xi < dim; ++xi { for xi in 0 ..< dim {
let x = orig + Float(xi) * (sz + spacing) let x = orig + Float(xi) * (sz + spacing)
for var yi = 0; yi < dim; ++yi { for yi in 0 ..< dim {
let y = orig + Float(yi) * (sz + spacing) let y = orig + Float(yi) * (sz + spacing)
for var zi = 0; zi < dim; ++zi { for zi in 0 ..< dim {
let z = orig + Float(zi) * (sz + spacing) let z = orig + Float(zi) * (sz + spacing)
let nodeGeom = geom.copy() as! SCNGeometry let nodeGeom = geom.copy() as! SCNGeometry
@@ -212,14 +215,17 @@ class VisualizationViewController: UIViewController {
} }
_cubletsNode.position = SCNVector3Zero _cubletsNode.position = SCNVector3Zero
if (_cubletsNode.parentNode == nil) {
_scene.rootNode.addChildNode(_cubletsNode) _scene.rootNode.addChildNode(_cubletsNode)
} }
}
internal func _setupAnimation() internal func _setupAnimation()
{ {
let rotAngle = 2.0 * π let rotAngle = 2.0 * π
let rotAxis = SCNVector3Make(0.25, 1.75, 1.0) let rotAxis = SCNVector3Make(0.25, 1.75, 1.0)
let rotationAction = _createLongTermRotationAnimation(rotAngle, rotAxis) let rotationAction = _createLongTermRotationAnimation(rotAngle, rotAxis)
_cubletsNode.removeAllActions()
_cubletsNode.runAction(rotationAction, forKey: VisualizationViewController.rotationAnimationKey) _cubletsNode.runAction(rotationAction, forKey: VisualizationViewController.rotationAnimationKey)
} }
@@ -232,6 +238,30 @@ class VisualizationViewController: UIViewController {
_sceneView?.technique = technique _sceneView?.technique = technique
} }
internal func _beginModelResetTimer()
{
/* since this visualization is running all the time, trigonometric functions begin
malfunctioning at very large numbers. just reload the model every 24 hours so we
don't have to see it */
let reloadModelTime = dispatch_time(DISPATCH_TIME_NOW, Int64(60 * 60 * 24 * NSEC_PER_SEC))
dispatch_after(reloadModelTime, dispatch_get_main_queue()) { [weak self] in
if let strongSelf = self {
strongSelf._cubletsNode.removeFromParentNode()
strongSelf._cubletsNode = SCNNode()
strongSelf._setupModel()
strongSelf._setupAnimation()
let currentPercentActivated = strongSelf.percentActivated
let currentConnectionStatus = strongSelf.connectionStatus
strongSelf.percentActivated = currentPercentActivated
strongSelf.connectionStatus = currentConnectionStatus
strongSelf._beginModelResetTimer()
}
}
}
internal func _setCubletActivated(cublet: SCNNode, activated: Bool) internal func _setCubletActivated(cublet: SCNNode, activated: Bool)
{ {
let material = cublet.geometry?.firstMaterial let material = cublet.geometry?.firstMaterial
@@ -241,11 +271,12 @@ class VisualizationViewController: UIViewController {
internal func _setAnimationSpeed(speed: CGFloat) internal func _setAnimationSpeed(speed: CGFloat)
{ {
let key = VisualizationViewController.rotationAnimationKey let key = VisualizationViewController.rotationAnimationKey
let action = _cubletsNode.actionForKey(key) if let action = _cubletsNode.actionForKey(key) {
_cubletsNode.removeActionForKey(key) _cubletsNode.removeActionForKey(key)
action?.speed = speed action.speed = speed
_cubletsNode.runAction(action!, forKey: key) _cubletsNode.runAction(action, forKey: key)
}
} }
internal func _createLongTermRotationAnimation(rotAngle: CGFloat, _ rotAxis: SCNVector3) -> SCNAction internal func _createLongTermRotationAnimation(rotAngle: CGFloat, _ rotAxis: SCNVector3) -> SCNAction