Created
June 8, 2017 10:05
-
-
Save fluidityt/f6593608adf6b20e2c1017788e93f514 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Adjustor: | |
public final class Adjustor: SKLabelNode { | |
// Helper classes: | |
private final class AdjustorButtonLeft: SKSpriteNode { | |
#if os(iOS) | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let node = parent { | |
if let adjustor = node.parent as? Adjustor { adjustor.number -= (1 * adjustor.step) } | |
} | |
} | |
#endif | |
} | |
private final class AdjustorButtonRight: SKSpriteNode { | |
#if os(iOS) | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let node = parent { | |
if let adjustor = node.parent as? Adjustor { adjustor.number += (1 * adjustor.step) } | |
} | |
} | |
#endif | |
} | |
// Props: | |
private let left = AdjustorButtonLeft() | |
private let right = AdjustorButtonRight() | |
private let numLabel = SKLabelNode() | |
private let txtLabel = SKLabelNode() | |
private let width = CGFloat(140) | |
private let node = SKSpriteNode() | |
public override var text: String? { | |
get { | |
if let txt = txtLabel.text{ | |
return txt | |
} else { return "error" } | |
} | |
set { | |
txtLabel.text = newValue | |
} | |
} | |
private var refDouble: Reference<Double> | |
var number: Double { | |
get { | |
guard let txt = numLabel.text else { return -99 } | |
if let num = Double(txt) { | |
return num | |
} else { return -99 } | |
} | |
set { | |
refDouble.value = newValue | |
numLabel.text = String(newValue) | |
} | |
} | |
var step = 1.0 | |
// Init funcs: | |
private func activateButtons() { | |
left .isUserInteractionEnabled = true | |
right.isUserInteractionEnabled = true | |
} | |
private func getBiggerSize() -> CGSize { | |
if numLabel.frame.width > txtLabel.frame.width { | |
return numLabel.frame.size | |
} else { | |
return txtLabel.frame.size | |
} | |
} | |
private func setColor(to color: SKColor) { | |
left.color = color | |
right.color = color | |
txtLabel.fontColor = color | |
numLabel.fontColor = color | |
} | |
private func setSizes() { | |
node.size.width = (getBiggerSize().width + width) | |
node.size.height = (numLabel.frame.size.height + txtLabel.frame.size.height) | |
} | |
private func makeButtonsUI() { | |
left.size = CGSize(width: 30, height: 30) | |
left.color = .black | |
right.size = CGSize(width: 45, height: 45) | |
right.color = .black | |
left .position.x = node.frame.minX + (left.frame.halfWidth) + 5 | |
right.position.x = node.frame.maxX - (right.frame.halfWidth) - 5 | |
right.zPosition += 50 | |
} | |
private func makeLabelsUI() { | |
txtLabel.fontName = "Chalkduster" | |
numLabel.fontName = "Chalkduster" | |
txtLabel.fontColor = .black | |
numLabel.fontColor = .black | |
txtLabel.verticalAlignmentMode = .center | |
numLabel.verticalAlignmentMode = .center | |
txtLabel.position.y = node.frame.maxY - (txtLabel.frame.halfHeight) | |
numLabel.position.y = node.frame.minY + (numLabel.frame.halfHeight) | |
} | |
init(text: String, refDouble: Reference<Double>, step: Double) { | |
self.refDouble = refDouble | |
super.init() | |
let number = refDouble.value | |
self.text = text | |
self.number = number | |
if step <= 0 { | |
self.step = 1 | |
} else { self.step = step } | |
// What have I become?: | |
setSizes() | |
makeButtonsUI() | |
makeLabelsUI() | |
setColor(to: .black) | |
activateButtons() | |
let shape = SKShapeNode(rect: frame) | |
shape.strokeColor = .black | |
func addChildren(_ nodes: [SKNode]) { for cnode in nodes { node.addChild(cnode) } } | |
addChild(node) | |
addChildren([left, right, numLabel, txtLabel, shape]) | |
} | |
public required init?(coder aDecoder: NSCoder) { fatalError() } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment