253 lines
9.6 KiB
Swift
253 lines
9.6 KiB
Swift
import SwiftUI
|
|
|
|
struct BoardPosition {
|
|
/// Pieces in FEN order, a8 first. Nil is an empty square.
|
|
let squares: [Character?]
|
|
|
|
init?(fen: String) {
|
|
guard let placement = fen.split(separator: " ").first else { return nil }
|
|
let ranks = placement.split(separator: "/", omittingEmptySubsequences: false)
|
|
guard ranks.count == 8 else { return nil }
|
|
|
|
var parsed: [Character?] = []
|
|
parsed.reserveCapacity(64)
|
|
for rank in ranks {
|
|
var rankCount = 0
|
|
for symbol in rank {
|
|
guard symbol.isASCII else { return nil }
|
|
if let digit = symbol.wholeNumberValue {
|
|
guard (1...8).contains(digit) else { return nil }
|
|
rankCount += digit
|
|
for _ in 0..<digit { parsed.append(nil) }
|
|
} else {
|
|
guard "pnbrqkPNBRQK".contains(symbol) else { return nil }
|
|
rankCount += 1
|
|
parsed.append(symbol)
|
|
}
|
|
}
|
|
guard rankCount == 8 else { return nil }
|
|
}
|
|
guard parsed.count == 64 else { return nil }
|
|
squares = parsed
|
|
}
|
|
}
|
|
|
|
func squareIndex(_ name: String?) -> Int? {
|
|
guard let name else { return nil }
|
|
let bytes = Array(name.utf8)
|
|
guard bytes.count == 2 else { return nil }
|
|
let file = Int(bytes[0]) - 97
|
|
let rank = Int(bytes[1]) - 48
|
|
guard (0..<8).contains(file), (1...8).contains(rank) else { return nil }
|
|
return (8 - rank) * 8 + file
|
|
}
|
|
|
|
private func pieceAssetName(_ piece: Character) -> String? {
|
|
let white = piece.isUppercase
|
|
guard let uppercase = piece.uppercased().first else { return nil }
|
|
switch uppercase {
|
|
case "K", "Q", "R", "B", "N", "P":
|
|
return "\(white ? "w" : "b")\(uppercase)"
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
private extension BoardMarkColor {
|
|
var color: Color { Color(rgb: rgb).opacity(170.0 / 255.0) }
|
|
}
|
|
|
|
struct BoardView: View {
|
|
let node: MoveNode?
|
|
let flipped: Bool
|
|
let description: String
|
|
|
|
var body: some View {
|
|
let annotations = BoardAnnotationModel(node: node)
|
|
Group {
|
|
if let position = BoardPosition(fen: node?.fen ?? "") {
|
|
GeometryReader { geometry in
|
|
let side = min(geometry.size.width, geometry.size.height)
|
|
let cell = side / 8
|
|
ZStack {
|
|
VStack(spacing: 0) {
|
|
ForEach(0..<8, id: \.self) { row in
|
|
HStack(spacing: 0) {
|
|
ForEach(0..<8, id: \.self) { column in
|
|
BoardSquare(
|
|
visualIndex: row * 8 + column,
|
|
cell: cell,
|
|
position: position,
|
|
flipped: flipped,
|
|
highlighted: [squareIndex(node?.fromSquare), squareIndex(node?.toSquare)],
|
|
circles: annotations.circles
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
BoardArrows(arrows: annotations.arrows, cell: cell, flipped: flipped)
|
|
if let badge = annotations.badge {
|
|
BoardBadge(badge: badge, cell: cell, flipped: flipped)
|
|
}
|
|
if let ending = annotations.ending {
|
|
Text(ending == .checkmate ? L10n.checkmate : L10n.stalemate)
|
|
.font(.headline)
|
|
.foregroundStyle(Theme.onSurface)
|
|
.padding(.horizontal, 16)
|
|
.padding(.vertical, 8)
|
|
.background(Theme.containerHigh.opacity(0.94))
|
|
.clipShape(RoundedRectangle(cornerRadius: 8))
|
|
}
|
|
}
|
|
.frame(width: side, height: side)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
} else {
|
|
Text(String(localized: "invalid_position"))
|
|
.foregroundStyle(Theme.error)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
.background(Theme.containerLow)
|
|
}
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.aspectRatio(1, contentMode: .fit)
|
|
.accessibilityElement(children: .ignore)
|
|
.accessibilityLabel(description)
|
|
}
|
|
}
|
|
|
|
private struct BoardSquare: View {
|
|
let visualIndex: Int
|
|
let cell: CGFloat
|
|
let position: BoardPosition
|
|
let flipped: Bool
|
|
let highlighted: [Int?]
|
|
let circles: [BoardCircleModel]
|
|
|
|
private let files = Array("abcdefgh")
|
|
|
|
var body: some View {
|
|
let index = flipped ? 63 - visualIndex : visualIndex
|
|
let light = (index / 8 + index % 8) % 2 == 0
|
|
let isHighlighted = highlighted.contains { $0 == index }
|
|
let coordinateColor = light ? Theme.boardDark : Theme.boardLight
|
|
|
|
ZStack(alignment: .topLeading) {
|
|
Rectangle().fill(light ? Theme.boardLight : Theme.boardDark)
|
|
if isHighlighted {
|
|
Rectangle().fill(Theme.lastMove)
|
|
}
|
|
if let piece = position.squares[index], let asset = pieceAssetName(piece) {
|
|
Image(asset)
|
|
.resizable()
|
|
.interpolation(.high)
|
|
.aspectRatio(contentMode: .fit)
|
|
.padding(max(0, cell * 0.04))
|
|
}
|
|
ForEach(circles.filter { $0.square == index }) { mark in
|
|
Circle()
|
|
.stroke(mark.color.color, lineWidth: max(1, cell * 0.08))
|
|
.frame(width: cell * 0.9, height: cell * 0.9)
|
|
}
|
|
if visualIndex % 8 == 0 {
|
|
Text(String(8 - index / 8))
|
|
.font(.system(size: max(8, cell * 0.18), weight: .medium))
|
|
.foregroundStyle(coordinateColor)
|
|
.padding(2)
|
|
}
|
|
if visualIndex / 8 == 7 {
|
|
Text(String(files[index % 8]))
|
|
.font(.system(size: max(8, cell * 0.18), weight: .medium))
|
|
.foregroundStyle(coordinateColor)
|
|
.frame(width: cell, height: cell, alignment: .bottomTrailing)
|
|
.padding(2)
|
|
}
|
|
}
|
|
.frame(width: cell, height: cell)
|
|
}
|
|
}
|
|
|
|
private struct BoardArrows: View {
|
|
let arrows: [BoardArrowModel]
|
|
let cell: CGFloat
|
|
let flipped: Bool
|
|
|
|
var body: some View {
|
|
Canvas { context, _ in
|
|
for arrow in arrows {
|
|
guard let from = squareCenter(arrow.from, cell: cell, flipped: flipped),
|
|
let to = squareCenter(arrow.to, cell: cell, flipped: flipped) else { continue }
|
|
drawArrow(context: &context, from: from, to: to, color: arrow.color.color)
|
|
}
|
|
}
|
|
.allowsHitTesting(false)
|
|
}
|
|
|
|
private func squareCenter(_ index: Int, cell: CGFloat, flipped: Bool) -> CGPoint? {
|
|
guard let visual = visualSquareIndex(index, flipped: flipped) else { return nil }
|
|
return CGPoint(x: CGFloat(visual % 8) * cell + cell / 2, y: CGFloat(visual / 8) * cell + cell / 2)
|
|
}
|
|
|
|
private func drawArrow(context: inout GraphicsContext, from: CGPoint, to: CGPoint, color: Color) {
|
|
let dx = to.x - from.x
|
|
let dy = to.y - from.y
|
|
let length = hypot(dx, dy)
|
|
guard length > 1 else { return }
|
|
let direction = CGPoint(x: dx / length, y: dy / length)
|
|
let normal = CGPoint(x: -direction.y, y: direction.x)
|
|
let start = from + direction * (cell * 0.2)
|
|
let tip = to - direction * (cell * 0.12)
|
|
let base = tip - direction * (cell * 0.42)
|
|
|
|
var shaft = Path()
|
|
shaft.move(to: start)
|
|
shaft.addLine(to: base)
|
|
context.stroke(shaft, with: .color(color), style: StrokeStyle(lineWidth: cell * 0.16, lineCap: .round))
|
|
|
|
var head = Path()
|
|
head.move(to: tip)
|
|
head.addLine(to: base + normal * (cell * 0.38))
|
|
head.addLine(to: base - normal * (cell * 0.38))
|
|
head.closeSubpath()
|
|
context.fill(head, with: .color(color))
|
|
}
|
|
}
|
|
|
|
private struct BoardBadge: View {
|
|
let badge: BoardBadgeModel
|
|
let cell: CGFloat
|
|
let flipped: Bool
|
|
|
|
var body: some View {
|
|
GeometryReader { _ in
|
|
if let visual = visualSquareIndex(badge.square, flipped: flipped) {
|
|
let size = max(18, cell * 0.42)
|
|
Text(badge.text)
|
|
.font(.system(size: size * 0.52, weight: .black))
|
|
.foregroundStyle(Color.white)
|
|
.frame(width: size, height: size)
|
|
.background(Color(rgb: badge.rgb))
|
|
.clipShape(Circle())
|
|
.overlay(Circle().stroke(Color.white, lineWidth: max(1, cell * 0.025)))
|
|
.position(
|
|
x: CGFloat(visual % 8) * cell + cell * 0.82,
|
|
y: CGFloat(visual / 8) * cell + cell * 0.18
|
|
)
|
|
}
|
|
}
|
|
.allowsHitTesting(false)
|
|
}
|
|
}
|
|
|
|
private func + (lhs: CGPoint, rhs: CGPoint) -> CGPoint {
|
|
CGPoint(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
|
|
}
|
|
|
|
private func - (lhs: CGPoint, rhs: CGPoint) -> CGPoint {
|
|
CGPoint(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
|
|
}
|
|
|
|
private func * (lhs: CGPoint, rhs: CGFloat) -> CGPoint {
|
|
CGPoint(x: lhs.x * rhs, y: lhs.y * rhs)
|
|
}
|