Skip to content

Commit

Permalink
Fixed up Z ordering of pieces and styled board.
Browse files Browse the repository at this point in the history
  • Loading branch information
richy486 committed Mar 24, 2024
1 parent 5012dbc commit 0a5b256
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions Shared/Presentation/BoardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ struct BoardView: View {

private enum Constants {
static let targetColor = Color.red
static let oddBoardColor = Color.gray
static let evenBoardColor = Color.white
static let boardOutline = Color.gray
}

private enum ZIndex: Int {
case normal = 0
case target = 1
case selected = 2
}

var body: some View {
Expand All @@ -24,6 +33,8 @@ struct BoardView: View {
let isTarget = appState.positioningState.targetGrid?.column == columnIndex
&& appState.positioningState.targetGrid?.row == rowIndex
let gridPosition = GridCoordinate(column: columnIndex, row: rowIndex)
let isSelected = appState.positioningState.selectedGridPosition?.column == columnIndex
&& appState.positioningState.selectedGridPosition?.row == rowIndex
Group {
if let piece {
let offset = appState.positioningState.pieceOffset(gridPosition)
Expand All @@ -43,24 +54,38 @@ struct BoardView: View {
positioningInteractor.endDrag()
}
)
.zIndex(Double(isSelected ? ZIndex.selected.rawValue : ZIndex.normal.rawValue))
} else {
Color.clear
.frame(width: appState.layoutState.elementDiameter,
height: appState.layoutState.elementDiameter)
}
} // Group
.background(
Rectangle()
.stroke(isTarget ? .red : .green,
lineWidth: 1)
)
// TODO: this puts the dragged icon behind other icons sometimes.
.zIndex(isTarget ? 0 : -1)
.background(background(isTarget: isTarget,
rowIndex: rowIndex,
columnIndex: columnIndex,
rowCount: appState.gameState.rowCount))
}
}
}
} // Grid
.border(Constants.boardOutline, width: 1)
} // body

func background(isTarget: Bool, rowIndex: Int, columnIndex: Int, rowCount: Int) -> some View {
let index = rowIndex * rowCount + columnIndex
let color: Color = switch (isTarget, (index % 2 == 0)) {
case (true, _):
Constants.targetColor
default:
.clear
}
return ZStack {
Rectangle().fill((index % 2 == 0) ? Constants.oddBoardColor : Constants.evenBoardColor)
Rectangle().strokeBorder(color, lineWidth: 2)
}
.zIndex(Double(isTarget ? ZIndex.target.rawValue : ZIndex.normal.rawValue))
}
}

#Preview {
Expand Down

0 comments on commit 0a5b256

Please sign in to comment.