Add FDChessboardView to your project using Swift Package Manager. In Xcode that is simply: File > Swift Packages > Add Package Dependency... and you're done. Alternative installations options are shown below for legacy projects.
If you are already using CocoaPods, just add 'FDChessboardView' to your Podfile
then run pod install
.
If you are already using Carthage, just add to your Cartfile
:
github "fulldecent/FDChessboardView" ~> 0.1
Then run carthage update
to build the framework and drag the built FDChessboardView.framework into your Xcode project.
Import the project and implement a data source:
import FDChessboardView
public protocol FDChessboardViewDataSource: class {
/// What piece is on the square?
func chessboardView(_ board: FDChessboardView, pieceForSquare square: FDChessboardSquare) -> FDChessboardPiece?
/// The last move
func chessboardViewLastMove(_ board: FDChessboardView) -> (from:FDChessboardSquare, to:FDChessboardSquare)?
/// The premove
func chessboardViewPremove(_ board: FDChessboardView) -> (from:FDChessboardSquare, to:FDChessboardSquare)?
}
If your application will allow the pieces to be moved, implement a delegate:
public protocol FDChessboardViewDelegate: class {
/// Where can this piece move to?
func chessboardView(_ board: FDChessboardView, legalDestinationsForPieceAtSquare from: FDChessboardSquare) -> [FDChessboardSquare]
/// Before a move happenes
func chessboardView(_ board: FDChessboardView, canMoveFrom from: FDChessboardSquare, to: FDChessboardSquare, withPromotion promotion: FDChessboardPiece?) -> Bool
/// After a move happened
func chessboardView(_ board: FDChessboardView, didMoveFrom from: FDChessboardSquare, to: FDChessboardSquare, withPromotion promotion: FDChessboardPiece?)
}
Then you can customize the view or call certain actions:
/// The location of a square on a chess board
public struct FDChessboardSquare: Hashable {
/// From 0...7 (a...h)
public let file: Int
/// From 0...7 (white king starting position to black king starting position)
public let rank: Int
}
/// The pieces on a chess board
public enum FDChessboardPiece: String {
case WhitePawn
case BlackPawn
case WhiteKnight
case BlackKnight
case WhiteBishop
case BlackBishop
case WhiteRook
case BlackRook
case WhiteQueen
case BlackQueen
case WhiteKing
case BlackKing
}
/// Display for a chess board
@IBDesignable open class FDChessboardView: UIView {
@IBInspectable open var lightBackgroundColor: UIColor
@IBInspectable open var darkBackgroundColor: UIColor
open var targetBackgroundColor: UIColor
open var legalBackgroundColor: UIColor
open var lastMoveColor: UIColor
open var premoveColor: UIColor
open weak var dataSource: FDChessboardViewDataSource?
open weak var delegate: FDChessboardViewDelegate?
open var doesAnimate: Bool
open var doesShowLegalSquares: Bool
open var doesShowLastMove: Bool
open var doesShowPremove: Bool
/// Add a piece onto the board
open func setPiece(_ piece: FDChessboardPiece?, forSquare square: FDChessboardSquare)
/// Repull all board information from data source
open func reloadData()
/// Move a piece on the board, clears any prior premove
open func move(_ piece: FDChessboardPiece, from: FDChessboardSquare, to: FDChessboardSquare, promotedTo promoted: FDChessboardPiece?)
/// Premove a piece on the board, clears any prior premove
open func premove(_ piece: FDChessboardPiece, from: FDChessboardSquare, to: FDChessboardSquare, promotedTo promoted: FDChessboardPiece?)
/// Removes any premove on the board
open func clearPremove()
/// Move a piece on the board, clears any prior premove
open func unmove(_ piece: FDChessboardPiece, from: FDChessboardSquare, to: FDChessboardSquare, promotedTo promoted: FDChessboardPiece?, capturing: FDChessboardPiece)
}
These following items are in the API for discussion and awaiting implementation:
See also Kibitz for Mac which is making a comeback https://github.com/fulldecent/kibitz
FDChessboardView is available under the MIT license. See the LICENSE file for more information.