diff --git a/Sources/GameWidget/Commons/Display.swift b/Sources/GameWidget/Commons/Display.swift index fc96ff1..20093eb 100644 --- a/Sources/GameWidget/Commons/Display.swift +++ b/Sources/GameWidget/Commons/Display.swift @@ -13,26 +13,26 @@ public struct Display { public init() {} /// ``Display/Single`` の ``Display/Single/place(block:)`` により生成され, 二つの ``WidgetList`` を保持します. - public struct Link { + public struct Link { let value: (T, U) } /// 一つの widget から ``Display/Link`` を生成するためのラッパー. /// ``Display`` の ``Display/place(block:)`` により生成され, 一つの ``WidgetList`` を保持します. - public struct Single { + public struct Single { let value: T } } public extension Display { - func place(@GroupBuilder block: () -> T) -> Display.Single { + func place(@GroupBuilder block: () -> T) -> Display.Single { .init(value: block()) } } -extension Display.Link: Widget, WidgetList { - public func place(@GroupBuilder block: () -> V) -> Display.Link { +extension Display.Link: Widget, WidgetListProtocol { + public func place(@GroupBuilder block: () -> V) -> Display.Link { .init(value: (self, block())) } @@ -51,8 +51,8 @@ extension Display.Link: Widget, WidgetList { } } -extension Display.Single: Widget, WidgetList { - public func place(@GroupBuilder block: () -> U) -> Display.Link { +extension Display.Single: Widget, WidgetListProtocol { + public func place(@GroupBuilder block: () -> U) -> Display.Link { .init(value: (self, block())) } diff --git a/Sources/GameWidget/Commons/StructuringTools/Extension.swift b/Sources/GameWidget/Commons/StructuringTools/Extension.swift index e3b2e50..3b02d41 100644 --- a/Sources/GameWidget/Commons/StructuringTools/Extension.swift +++ b/Sources/GameWidget/Commons/StructuringTools/Extension.swift @@ -9,7 +9,7 @@ import SpriteKit /// widget 数を増やす際に使用します. 10 個以下の widget を内包することができます. /// - note: モディファイアはありませんが, メモリのオーバーヘッドがありません. -public struct Extension: WidgetListElementType { +public struct Extension: WidgetListElementType { var content: Content diff --git a/Sources/GameWidget/Commons/StructuringTools/NodeWidget/NodeWidget.swift b/Sources/GameWidget/Commons/StructuringTools/NodeWidget/NodeWidget.swift index 4bd1632..6dc8183 100644 --- a/Sources/GameWidget/Commons/StructuringTools/NodeWidget/NodeWidget.swift +++ b/Sources/GameWidget/Commons/StructuringTools/NodeWidget/NodeWidget.swift @@ -8,7 +8,7 @@ import SpriteKit /// 10 個以下の widget を一つの widget としてまとめます. 座標, スケール, 回転を内包するコンテンツと共に調整することができます. -public struct NodeWidget: Widget, ContextPresentPlugIn { +public struct NodeWidget: Widget, ContextPresentPlugIn { public typealias Context = NodeContext public var content: Content diff --git a/Sources/GameWidget/Commons/WidgetList/WidgetList.swift b/Sources/GameWidget/Commons/WidgetList/WidgetList.swift new file mode 100644 index 0000000..5924b75 --- /dev/null +++ b/Sources/GameWidget/Commons/WidgetList/WidgetList.swift @@ -0,0 +1,196 @@ +// +// WidgetList.swift +// +// +// Created by rrbox on 2023/04/09. +// + +import SpriteKit + +// 一つの widget から RecursiveGroup を生成するためのラッパー. +struct Single: WidgetListProtocol { + + var widget: T + + func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widget.addTo(buffer: &result, center: center) + return result + } + +} + +public struct WidgetList2: WidgetListProtocol { + let widgets: (C0, C1) + + public init(_ c0: C0, _ c1: C1) { + self.widgets = (c0, c1) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + return result + } + +} + +public struct WidgetList3: WidgetListProtocol { + let widgets: (C0, C1, C2) + + public init(_ c0: C0, _ c1: C1, _ c2: C2) { + self.widgets = (c0, c1, c2) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + return result + } + +} + +public struct WidgetList4: WidgetListProtocol { + let widgets: (C0, C1, C2, C3) + + public init(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3) { + self.widgets = (c0, c1, c2, c3) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + self.widgets.3.addTo(buffer: &result, center: center) + return result + } + +} + +public struct WidgetList5: WidgetListProtocol { + let widgets: (C0, C1, C2, C3, C4) + + public init(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4) { + self.widgets = (c0, c1, c2, c3, c4) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + self.widgets.3.addTo(buffer: &result, center: center) + self.widgets.4.addTo(buffer: &result, center: center) + return result + } + +} + +public struct WidgetList6: WidgetListProtocol { + let widgets: (C0, C1, C2, C3, C4, C5) + + public init(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5) { + self.widgets = (c0, c1, c2, c3, c4, c5) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + self.widgets.3.addTo(buffer: &result, center: center) + self.widgets.4.addTo(buffer: &result, center: center) + self.widgets.5.addTo(buffer: &result, center: center) + return result + } +} + +public struct WidgetList7: WidgetListProtocol { + let widgets: (C0, C1, C2, C3, C4, C5, C6) + + public init(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6) { + self.widgets = (c0, c1, c2, c3, c4, c5, c6) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + self.widgets.3.addTo(buffer: &result, center: center) + self.widgets.4.addTo(buffer: &result, center: center) + self.widgets.5.addTo(buffer: &result, center: center) + self.widgets.6.addTo(buffer: &result, center: center) + return result + } +} + +public struct WidgetList8: WidgetListProtocol { + let widgets: (C0, C1, C2, C3, C4, C5, C6, C7) + + public init(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7) { + self.widgets = (c0, c1, c2, c3, c4, c5, c6, c7) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + self.widgets.3.addTo(buffer: &result, center: center) + self.widgets.4.addTo(buffer: &result, center: center) + self.widgets.5.addTo(buffer: &result, center: center) + self.widgets.6.addTo(buffer: &result, center: center) + self.widgets.7.addTo(buffer: &result, center: center) + return result + } +} + +public struct WidgetList9: WidgetListProtocol { + let widgets: (C0, C1, C2, C3, C4, C5, C6, C7, C8) + + public init(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8) { + self.widgets = (c0, c1, c2, c3, c4, c5, c6, c7, c8) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + self.widgets.3.addTo(buffer: &result, center: center) + self.widgets.4.addTo(buffer: &result, center: center) + self.widgets.5.addTo(buffer: &result, center: center) + self.widgets.6.addTo(buffer: &result, center: center) + self.widgets.7.addTo(buffer: &result, center: center) + self.widgets.8.addTo(buffer: &result, center: center) + return result + } +} + +public struct WidgetList10: WidgetListProtocol { + let widgets: (C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) + + public init(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9) { + self.widgets = (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) + } + + public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { + var result = [SKNode]() + self.widgets.0.addTo(buffer: &result, center: center) + self.widgets.1.addTo(buffer: &result, center: center) + self.widgets.2.addTo(buffer: &result, center: center) + self.widgets.3.addTo(buffer: &result, center: center) + self.widgets.4.addTo(buffer: &result, center: center) + self.widgets.5.addTo(buffer: &result, center: center) + self.widgets.6.addTo(buffer: &result, center: center) + self.widgets.7.addTo(buffer: &result, center: center) + self.widgets.8.addTo(buffer: &result, center: center) + self.widgets.9.addTo(buffer: &result, center: center) + return result + } +} diff --git a/Sources/GameWidget/Commons/WidgetList.swift b/Sources/GameWidget/Commons/WidgetList/WidgetListBuilder.swift similarity index 59% rename from Sources/GameWidget/Commons/WidgetList.swift rename to Sources/GameWidget/Commons/WidgetList/WidgetListBuilder.swift index 7f2dc6f..d24415f 100644 --- a/Sources/GameWidget/Commons/WidgetList.swift +++ b/Sources/GameWidget/Commons/WidgetList/WidgetListBuilder.swift @@ -1,5 +1,5 @@ // -// Group.swift +// WidgetListBuilder.swift // // // Created by rrbox on 2022/07/24. @@ -10,44 +10,34 @@ import SpriteKit @resultBuilder public struct GroupBuilder { - public static func buildBlock(_ c0: C0) -> some WidgetList { + public static func buildBlock(_ c0: C0) -> some WidgetListProtocol { Single(widget: c0) } public static func buildBlock(_ c0: C0, _ c1: C1) -> some WidgetList { - Single(widget: c0) - .append(c1) + C1: WidgetListElementType>(_ c0: C0, _ c1: C1) -> some WidgetListProtocol { + WidgetList2(c0, c1) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) + C2: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2) -> some WidgetListProtocol { + WidgetList3(c0, c1, c2) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) - .append(c3) + C3: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3) -> some WidgetListProtocol { + WidgetList4(c0, c1, c2, c3) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) - .append(c3) - .append(c4) + C4: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4) -> some WidgetListProtocol { + WidgetList5(c0, c1, c2, c3, c4) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) - .append(c3) - .append(c4) - .append(c5) + C5: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5) -> some WidgetListProtocol { + WidgetList6(c0, c1, c2, c3, c4, c5) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) - .append(c3) - .append(c4) - .append(c5) - .append(c6) + C6: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6) -> some WidgetListProtocol { + WidgetList7(c0, c1, c2, c3, c4, c5, c6) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) - .append(c3) - .append(c4) - .append(c5) - .append(c6) - .append(c7) + C7: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7) -> some WidgetListProtocol { + WidgetList8(c0, c1, c2, c3, c4, c5, c6, c7) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) - .append(c3) - .append(c4) - .append(c5) - .append(c6) - .append(c7) - .append(c8) + C8: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8) -> some WidgetListProtocol { + WidgetList9(c0, c1, c2, c3, c4, c5, c6, c7, c8) } public static func buildBlock(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9) -> some WidgetList { - Single(widget: c0) - .append(c1) - .append(c2) - .append(c3) - .append(c4) - .append(c5) - .append(c6) - .append(c7) - .append(c8) - .append(c9) + C9: WidgetListElementType>(_ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9) -> some WidgetListProtocol { + WidgetList10(c0, c1, c2, c3, c4, c5, c6, c7, c8, c9) } } -public protocol WidgetList { +public protocol WidgetListProtocol { func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] } - -/// 再帰可能. widget のペアです. オーバーヘッドはありません. -public struct RecursiveGroup: WidgetList { - - var first: T - var second: U - - func append(_ newWidget: V) -> RecursiveGroup { - .init(first: self, second: newWidget) - } - - public func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { - var result = self.first.widgetNodes(center: center) - self.second.addTo(buffer: &result, center: center) - return result - } - -} - -// 一つの widget から RecursiveGroup を生成するためのラッパー. -struct Single: WidgetList { - - var widget: T - - func append(_ newWidget: U) -> RecursiveGroup { - .init(first: self, second: newWidget) - } - - func widgetNodes(center: WidgetNotificationSystem) -> [SKNode] { - var result = [SKNode]() - self.widget.addTo(buffer: &result, center: center) - return result - } - -} diff --git a/Tests/GameWidgetTests/GroupTests.swift b/Tests/GameWidgetTests/GroupTests.swift index d66b013..917253b 100644 --- a/Tests/GameWidgetTests/GroupTests.swift +++ b/Tests/GameWidgetTests/GroupTests.swift @@ -10,7 +10,7 @@ import SpriteKit import GameWidget final class GroupTest: XCTestCase { - func generateNodesNameReduced(@GroupBuilder _ list: () -> T) -> String { + func generateNodesNameReduced(@GroupBuilder _ list: () -> T) -> String { list().widgetNodes(center: WidgetNotificationSystem()).reduce(into: "") { partialResult, node in guard let name = node.name else { XCTFail()