Skip to content

Commit 4d3374e

Browse files
committed
Use protocol constraints
1 parent c894878 commit 4d3374e

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

Sources/GateEngine/UI/TileMapControl.swift

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,36 @@ public enum TileMapControlSubControlType: Sendable {
2626
case interactable
2727
}
2828

29-
public protocol TileControlSubControl: Equatable, Identifiable {
29+
public protocol TileControlSubControl<Scheme>: Equatable, Hashable, Identifiable where Control.Scheme == Scheme, ID == Int {
30+
associatedtype Scheme: TileMapControlScheme
3031
associatedtype Control: TileControl
32+
3133
var type: TileMapControlSubControlType { get }
3234
/// The arangement of coordinates (relative to zero) for this control.
3335
/// The position of this control is determined by TileMapControlScheme
3436
var coordinates: [TileMap.Layer.Coordinate] { get }
3537
/// The `regular` state tile for the layer
36-
func regularStateTile(at coordinateIndex: Int, forLayer layer: String?, mode: any TileMapControlMode, userData: any TileMapControlUserData) -> TileMap.Tile
38+
func regularStateTile(at coordinateIndex: Int, forLayer layer: String?, mode: Scheme.Mode, userData: Scheme.UserData) -> TileMap.Tile
39+
}
40+
41+
public extension TileControlSubControl {
42+
nonisolated static func == (lhs: Self, rhs: Self) -> Bool {
43+
return lhs.id == rhs.id
44+
}
45+
nonisolated func hash(into hasher: inout Hasher) {
46+
hasher.combine(id)
47+
}
3748
}
3849

3950
public protocol TileControl<Scheme>: Equatable, Identifiable where ID == String {
4051
associatedtype Scheme: TileMapControlScheme
41-
associatedtype SubControl: TileControlSubControl
52+
associatedtype SubControl: TileControlSubControl<Scheme>
4253
/// How this control behaves
4354
var type: TileMapControlType { get }
4455
var subControls: [SubControl] { get }
4556

46-
func currentStateForSubControl(_ subControl: any TileControlSubControl, mode: any TileMapControlMode, userData: any TileMapControlUserData) -> TileMapControlState
47-
func stateDidChangeForSubControl(_ subControl: any TileControlSubControl, state: TileMapControlState, mode: any TileMapControlMode, userData: inout any TileMapControlUserData)
57+
func currentStateForSubControl(_ subControl: any TileControlSubControl<Scheme>, mode: Scheme.Mode, userData: Scheme.UserData) -> TileMapControlState
58+
func stateDidChangeForSubControl(_ subControl: any TileControlSubControl<Scheme>, state: TileMapControlState, mode: Scheme.Mode, userData: inout Scheme.UserData)
4859
}
4960

5061
public protocol TileMapControlScheme {
@@ -64,7 +75,7 @@ public protocol TileMapControlMode: Equatable, Hashable, CaseIterable, Sendable
6475
@MainActor
6576
public protocol TileMapControlViewDelegate<Scheme>: AnyObject {
6677
associatedtype Scheme: TileMapControlScheme
67-
func tileMapControlView(_ tileMapControl: TileMapControlView<Scheme>, didActivateControl control: any TileControl, subControlIndex: Int)
78+
func tileMapControlView(_ tileMapControl: TileMapControlView<Scheme>, didActivateControl control: any TileControl<Scheme>, subControlIndex: Int)
6879
}
6980

7081
public final class TileMapControlView<Scheme: TileMapControlScheme>: TileMapView {
@@ -79,9 +90,9 @@ public final class TileMapControlView<Scheme: TileMapControlScheme>: TileMapView
7990
}
8091
public weak var controlDelegate: (any TileMapControlViewDelegate<Scheme>)? = nil
8192

82-
public var userData: any TileMapControlUserData = Scheme.UserData.init()
93+
public var userData: Scheme.UserData = .init()
8394

84-
var controls: [any TileControl] = []
95+
var controls: [any TileControl<Scheme>] = []
8596
var controlOrigins: [TileMap.Layer.Coordinate] = []
8697
var controlIndicies: [Int?] = []
8798
var controlStates: [[TileMapControlState]] = []
@@ -95,8 +106,6 @@ public final class TileMapControlView<Scheme: TileMapControlScheme>: TileMapView
95106

96107
guard let layer = tileMap.layers.first else {return}
97108

98-
let userData: Scheme.UserData = userData as! Scheme.UserData
99-
100109
for column in 0 ..< layer.columns {
101110
for row in 0 ..< layer.rows {
102111
let origin = TileMap.Layer.Coordinate(column: column, row: row)
@@ -139,7 +148,7 @@ public final class TileMapControlView<Scheme: TileMapControlScheme>: TileMapView
139148
return .init(column: column, row: row)
140149
}
141150

142-
func control(at coord: TileMap.Layer.Coordinate) -> (control: any TileControl, subControlIndex: Int)? {
151+
func control(at coord: TileMap.Layer.Coordinate) -> (control: any TileControl<Scheme>, subControlIndex: Int)? {
143152
let coordIndex = coordIndex(of: coord)
144153
if let controlIndex = controlIndicies[coordIndex] {
145154
let control = controls[controlIndex]
@@ -236,7 +245,7 @@ public final class TileMapControlView<Scheme: TileMapControlScheme>: TileMapView
236245

237246
}
238247

239-
var momentaryToDeactivate: [(pair: (control: any TileControl, subControlIndex: Int), duration: Float)] = []
248+
var momentaryToDeactivate: [(pair: (control: any TileControl<Scheme>, subControlIndex: Int), duration: Float)] = []
240249
func updateMomentaryToDeactivate(withTimePassed deltaTime: Float) {
241250
for index in momentaryToDeactivate.indices.reversed() {
242251
momentaryToDeactivate[index].duration -= deltaTime

0 commit comments

Comments
 (0)