Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Update componentes to modern Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
txaiwieser committed Aug 11, 2021
1 parent 6622776 commit e5f5cd4
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 279 deletions.
2 changes: 1 addition & 1 deletion Example/Example/StackNodeDemoScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class StackNodeDemoScene: SKScene {
size = view.bounds.size
backgroundColor = .lightGray

stack = TWStackNode(lenght: view.frame.size.width * 0.8, fillMode: .vertical)
stack = TWStackNode(length: view.frame.size.width * 0.8, fillMode: .vertical)

stack.position = CGPoint(x: view.frame.midX, y: view.frame.midY)
stack.color = .red
Expand Down
32 changes: 0 additions & 32 deletions Sources/TWSpriteKitUtils/TWControls/TWButton.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
//
// TWButton.swift
//
// The MIT License (MIT)
//
// Created by Txai Wieser on 25/02/15.
// Copyright (c) 2015 Txai Wieser.
//
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
//
//

import SpriteKit

open class TWButton: TWControl {
Expand All @@ -48,9 +19,6 @@ open class TWButton: TWControl {
self.init(size: size, normalColor: normalColor, selectedColor: nil, singleHighlightedColor: highlightedColor, disabledColor: nil)
}




// MARK: TWButton Events

internal override func touchDown() {
Expand Down
58 changes: 3 additions & 55 deletions Sources/TWSpriteKitUtils/TWControls/TWControl+Helpers.swift
Original file line number Diff line number Diff line change
@@ -1,62 +1,10 @@
//
// TWControl+Helpers.swift
//
// The MIT License (MIT)
//
// Created by Txai Wieser on 25/02/15.
// Copyright (c) 2015 Txai Wieser.
//
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
//
//

import SpriteKit

internal extension TWControl {

// Sounds
func playSound(instanceSoundFileName fileName: String?, defaultSoundFileName: String?) {
guard TWControl.defaultSoundEffectsEnabled ?? soundEffectsEnabled else { return }
guard let soundFileName = fileName ?? defaultSoundFileName else { return }

let soundEnabled = TWControl.defaultSoundEffectsEnabled ?? soundEffectsEnabled

if soundEnabled {
if let soundFileName = fileName {
let action = SKAction.playSoundFileNamed(soundFileName, waitForCompletion: true)
// Warning: This is sad, but if you run the sound effect `SKAction` only one time
// SpriteKit simply fails to load the .wav in memory on some devices..
// It's SpriteKit, so this bug will probably never going to get fixed
// like many many other radars I filled..
// SpriteKit I 💔 you!
self.run(action)
self.run(action)
}
else if let soundFileName = defaultSoundFileName {
let action = SKAction.playSoundFileNamed(soundFileName, waitForCompletion: true)
// Warning: This is sad, but if you run the sound effect `SKAction` only one time
// SpriteKit simply fails to load the .wav in memory on some devices..
// It's SpriteKit, so this bug will probably never going to get fixed
// like many many other radars I filled..
// SpriteKit I 💔 you!
self.run(action)
self.run(action)
}
}
run(.playSoundFileNamed(soundFileName, waitForCompletion: true))
}
}
57 changes: 9 additions & 48 deletions Sources/TWSpriteKitUtils/TWControls/TWControl+NestedTypes.swift
Original file line number Diff line number Diff line change
@@ -1,48 +1,16 @@
//
// TWControl+NestedTypes.swift
//
// The MIT License (MIT)
//
// Created by Txai Wieser on 25/02/15.
// Copyright (c) 2015 Txai Wieser.
//
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
//
//

import SpriteKit

public extension TWControl {

// MARK: Nested Types
enum ControlEvent {

case touchDown // on all touch downs
case touchDown
case touchUpInside
case touchUpOutside
case touchCancel
case touchDragExit
case touchDragOutside
case touchDragEnter
case touchDragInside
case valueChanged // sliders, etc.
case valueChanged
case disabledTouchDown
}

Expand All @@ -51,18 +19,6 @@ public extension TWControl {
case highlighted
case selected
case disabled
func asString() -> String {
switch self {
case .normal:
return "Normal"
case .highlighted:
return "Highlighted"
case .selected:
return "Selected"
case .disabled:
return "Disabled"
}
}
}

internal enum TWControlType {
Expand Down Expand Up @@ -94,6 +50,7 @@ public extension TWControl {

internal class TWSpriteNode: SKSpriteNode {
weak var control: TWControl?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
control?.touchesBegan(touches, with: event)
Expand All @@ -119,6 +76,7 @@ public extension SKShapeNode {
self.init()
self.redefine(definition)
}

func redefine(_ definition: Definition) {
self.path = definition.path
self.strokeColor = definition.strokeColor
Expand Down Expand Up @@ -151,17 +109,20 @@ public extension SKShapeNode {
public init(path: CGPath) {
self.path = path
}

public init(path: CGPath, color: SKColor) {
self.path = path
self.fillColor = color
self.strokeColor = color
}

public init(_ node: SKShapeNode) {
self = node.definition()
}

public init?(_ node: SKShapeNode?) {
if let shape = node { self.init(shape) }
else { return nil }
guard let shape = node else { return nil }
self.init(shape)
}
}
}
Loading

0 comments on commit e5f5cd4

Please sign in to comment.