Skip to content

Commit

Permalink
Remove MaskNode and use SKShapeNode as root of Node (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyphs21 authored and efremidze committed Aug 19, 2019
1 parent 23944fd commit 4bedcfd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ extension ViewController: MagneticDelegate {
class ImageNode: Node {
override var image: UIImage? {
didSet {
sprite.texture = image.map { SKTexture(image: $0) }
texture = image.map { SKTexture(image: $0) }
}
}
override func selectedAnimation() {}
Expand Down
65 changes: 11 additions & 54 deletions Sources/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import SpriteKit

@objcMembers open class Node: MaskNode {
@objcMembers open class Node: SKShapeNode {

public lazy var label: SKMultilineLabelNode = { [unowned self] in
let label = SKMultilineLabelNode()
Expand All @@ -18,17 +18,10 @@ import SpriteKit
label.verticalAlignmentMode = .center
label.width = self.frame.width
label.separator = " "
self.mask.addChild(label)
addChild(label)
return label
}()

public lazy var sprite: SKSpriteNode = { [unowned self] in
let sprite = SKSpriteNode()
sprite.size = self.frame.size
sprite.colorBlendFactor = 0.5
self.mask.addChild(sprite)
return sprite
}()

/**
The text displayed by the node.
Expand All @@ -46,7 +39,6 @@ import SpriteKit
// let url = URL(string: "https://picsum.photos/1200/600")!
// let image = UIImage(data: try! Data(contentsOf: url))
texture = image.map { SKTexture(image: $0.aspectFill(self.frame.size)) }
sprite.size = texture?.size() ?? self.frame.size
}
}

Expand All @@ -55,18 +47,13 @@ import SpriteKit
Also blends the color with the image.
*/
open var color: UIColor {
get { return sprite.color }
set { sprite.color = newValue }
}

override open var strokeColor: UIColor {
open var color: UIColor? {
didSet {
maskOverlay.strokeColor = strokeColor
self.fillColor = color ?? .white
}
}

private(set) var texture: SKTexture?
open var texture: SKTexture?

/**
The selection state of the node.
Expand Down Expand Up @@ -95,8 +82,8 @@ import SpriteKit
- Returns: A new node.
*/
public init(text: String?, image: UIImage?, color: UIColor, path: CGPath, marginScale: CGFloat = 1.01) {
super.init(path: path)

super.init()
self.path = path
self.physicsBody = {
var transform = CGAffineTransform.identity.scaledBy(x: marginScale, y: marginScale)
let body = SKPhysicsBody(polygonFrom: path.copy(using: &transform)!)
Expand All @@ -105,9 +92,8 @@ import SpriteKit
body.linearDamping = 3
return body
}()
self.fillColor = .white
self.color = color
self.strokeColor = .white
_ = self.sprite
_ = self.text
configure(text: text, image: image, color: color)
}
Expand Down Expand Up @@ -151,7 +137,7 @@ import SpriteKit
open func selectedAnimation() {
run(.scale(to: 4/3, duration: 0.2))
if let texture = texture {
sprite.run(.setTexture(texture))
self.fillTexture = texture
}
}

Expand All @@ -160,7 +146,8 @@ import SpriteKit
*/
open func deselectedAnimation() {
run(.scale(to: 1, duration: 0.2))
sprite.texture = nil
self.fillTexture = nil
self.fillColor = color ?? .white
}

/**
Expand All @@ -175,33 +162,3 @@ import SpriteKit
}

}

open class MaskNode: SKShapeNode {

let mask: SKCropNode
let maskOverlay: SKShapeNode

public init(path: CGPath) {
mask = SKCropNode()
mask.maskNode = {
let node = SKShapeNode(path: path)
node.fillColor = .white
node.strokeColor = .clear
return node
}()

maskOverlay = SKShapeNode(path: path)
maskOverlay.fillColor = .clear

super.init()
self.path = path

self.addChild(mask)
self.addChild(maskOverlay)
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

0 comments on commit 4bedcfd

Please sign in to comment.