From 04a04abc8c38f2542131bb63934749551c267d25 Mon Sep 17 00:00:00 2001 From: Txai Wieser Date: Tue, 17 Aug 2021 17:08:38 -0300 Subject: [PATCH] Refactor TWCollectionNode --- Example/Example/CollectionNodeDemoScene.swift | 11 +- .../TWLayout/TWCollectionNode.swift | 107 ++++++++---------- 2 files changed, 52 insertions(+), 66 deletions(-) diff --git a/Example/Example/CollectionNodeDemoScene.swift b/Example/Example/CollectionNodeDemoScene.swift index 5465dbb..56e722a 100644 --- a/Example/Example/CollectionNodeDemoScene.swift +++ b/Example/Example/CollectionNodeDemoScene.swift @@ -16,8 +16,10 @@ class CollectionNodeDemoScene: SKScene { size = view.bounds.size backgroundColor = .lightGray - collection = TWCollectionNode(fillMode: TWCollectionNode.FillMode(columns: 3, width: view.frame.size.width*0.8, verticalMargin: 30, objectSize: CGSize(width: 100, height: 100))) + collection = TWCollectionNode(fillMode: .init(columnsCount: 3, lineSpacing: 30, elementSize: CGSize(width: 100, height: 100))) + collection.size.width = view.frame.size.width * 0.8 collection.position = CGPoint(x: (view.frame).midX, y: (view.frame).midY) + collection.color = .brown addChild(collection) let normalLabelAdd = SKLabelNode(text: "Add") @@ -26,7 +28,7 @@ class CollectionNodeDemoScene: SKScene { highlightedLabelAdd.color = .black let addButton = TWButton(normal: normalLabelAdd, highlighted: highlightedLabelAdd, disabled: nil) - addButton.position = CGPoint(x: (view.frame).midX + 200, y: (view.frame).midY + 400) + addButton.position = CGPoint(x: (view.frame).midX + 150, y: (view.frame).midY + 150) addButton.addClosure(.touchUpInside) { [unowned self] _ in addToCollection() } @@ -38,12 +40,11 @@ class CollectionNodeDemoScene: SKScene { let removeButton = TWButton(normal: normalLabelRemove, highlighted: highlightedLabelRemove, disabled: nil) - removeButton.position = CGPoint(x: (view.frame).midX - 200, y: (view.frame).midY + 400) + removeButton.position = CGPoint(x: (view.frame).midX - 150, y: (view.frame).midY + 150) removeButton.addClosure(.touchUpInside) { [unowned self] _ in removeFromCollection() } - addChild(addButton) addChild(removeButton) } @@ -55,7 +56,7 @@ class CollectionNodeDemoScene: SKScene { } func removeFromCollection() { - let node = collection.subNodes.last + let node = collection.childNodes.last collection.remove(node: node, reload: true) } } diff --git a/Sources/TWSpriteKitUtils/TWLayout/TWCollectionNode.swift b/Sources/TWSpriteKitUtils/TWLayout/TWCollectionNode.swift index 5af260a..8da7387 100755 --- a/Sources/TWSpriteKitUtils/TWLayout/TWCollectionNode.swift +++ b/Sources/TWSpriteKitUtils/TWLayout/TWCollectionNode.swift @@ -1,95 +1,80 @@ -// -// File.swift -// Repel -// -// Created by Txai Wieser on 7/22/15. -// -// - import SpriteKit -open class TWCollectionNode: SKSpriteNode { - open fileprivate(set) var fillMode: FillMode - open fileprivate(set) var subNodes: [SKNode] = [] +public class TWCollectionNode: SKSpriteNode { + public private(set) var fillMode: FillMode + public private(set) var childNodes: [SKNode] = [] + public var reloadCompletion: (()->())? = nil - open var reloadCompletion: (()->())? = nil required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } - public init(fillMode: FillMode) { self.fillMode = fillMode - super.init(texture: nil, color: .clear, size: CGSize(width: fillMode.width, height: 0)) + super.init(texture: nil, color: .clear, size: .zero) } - - open func reloadCollection() { - let elements = subNodes.count - let columns = fillMode.columns + func reloadCollection() { + guard childNodes.isEmpty == false else { + size.height = 0 + return + } + + let linesCount = Int(ceil(CGFloat(childNodes.count)/CGFloat(fillMode.columnsCount))) + let availableHSize = size.width - fillMode.elementSize.width + + let perElementHSpace = availableHSize / CGFloat(fillMode.columnsCount - 1) + let perElementVSpace = fillMode.elementSize.height - let xDiv = (fillMode.width - CGFloat(fillMode.columns)*fillMode.objectSize.width) / CGFloat(fillMode.columns-1) - let lines = Int(ceil(CGFloat(elements)/CGFloat(columns))) - var accumulatedHeight = CGFloat(0) - for lineIndex in 0..