Skip to content

Commit

Permalink
Ability to tap on a move in the list of moves
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 23, 2021
1 parent 036407b commit 7361686
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Shared/Bridge/FEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ typedef NS_ENUM(NSInteger, Direction){
@property (nonatomic, strong, readonly) NSArray<FEngineGame*>* _Nonnull games;
@property (nonatomic, assign) NSUInteger currentGameIndex;

@property (nonatomic, assign) NSUInteger currentMoveNodeUUID;

- (id _Nonnull)init;

- (BOOL)loadOpening:(NSString* _Nonnull)pgn;
Expand All @@ -56,7 +58,6 @@ typedef NS_ENUM(NSInteger, Direction){

- (NSString* _Nonnull)PGNFormattedForDisplay;
- (NSArray<FEngineMoveNode*>* _Nonnull)moveNodesTree;
- (NSUInteger)currentMoveNodeUUID;

- (NSArray<FEngineMove*>* _Nonnull)allMoves;
- (NSArray<FEngineMove*>* _Nonnull)movesAt:(NSUInteger)rank file:(NSUInteger)file;
Expand Down
5 changes: 5 additions & 0 deletions Shared/Bridge/FEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ @implementation FEngine

@synthesize games;
@synthesize currentGameIndex;
@synthesize currentMoveNodeUUID;

+ (void)initialize {
if (self == [FEngine class]) {
Expand Down Expand Up @@ -160,6 +161,10 @@ - (void)moveNodesFromNode:(ChessGame::MoveNode)node
return rootNode.variations;
}

- (void)setCurrentMoveNodeUUID:(NSUInteger)currentMoveNodeUUID {
engine.game().setCurrentMoveUUID((unsigned int)currentMoveNodeUUID);
}

- (NSUInteger)currentMoveNodeUUID {
return engine.game().getCurrentMoveUUID();
}
Expand Down
8 changes: 8 additions & 0 deletions Shared/ChessDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ struct ChessDocument: FileDocument {
}
}

var currentMoveIndex: UInt = 0 {
didSet {
engine.currentMoveNodeUUID = currentMoveIndex
selection = Selection.empty()
variations.show = false
}
}

var whitePlayer: GamePlayer
var blackPlayer: GamePlayer

Expand Down
2 changes: 1 addition & 1 deletion Shared/Engine/Engine/ChessGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ std::vector<Move> ChessGame::movesAt(File file, Rank rank) {
std::vector<Move> ChessGame::allMoves() {
std::vector<Move> all;
MoveNode currentNode = root;
root.visit(0, moveIndexes, [&all](auto & node) {
root.visit(0, moveIndexes, moveIndexes.moveCursor, [&all](auto & node, int cursor) {
all.push_back(node.move);
});
return all;
Expand Down
19 changes: 15 additions & 4 deletions Shared/Engine/Engine/ChessGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class ChessGame {
std::vector<MoveNode> variations;

typedef std::function<void(MoveNode &node)> NodeCallback;
typedef std::function<void(MoveNode &node, int cursor)> NodeCallback2;

// Find the node that represents the move at `atIndex`. Note that `atIndex==0` represents
// the "root" node, which is not a move. The first move is actually at `atIndex==1`.
Expand All @@ -94,11 +95,13 @@ class ChessGame {
}
}

void visit(int cursor, MoveIndexes indexes, NodeCallback callback) {
if (cursor < indexes.moveCursor) {
void visit(int cursor, MoveIndexes indexes, int untilCursorIndex, NodeCallback2 callback) {
if (cursor < untilCursorIndex) {
int varIndex = indexes.moves[cursor];
callback(variations[varIndex]);
variations[varIndex].visit(cursor+1, indexes, callback);
assert(varIndex < variations.size());
// TODO: revisit the cursor+1 which is needed because the root node is at cursor==0
callback(variations[varIndex], cursor+1);
variations[varIndex].visit(cursor+1, indexes, untilCursorIndex, callback);
}
}

Expand Down Expand Up @@ -174,6 +177,14 @@ class ChessGame {
return uuid;
}

void setCurrentMoveUUID(unsigned int uuid) {
root.visit(0, moveIndexes, (int)moveIndexes.moves.size(), [this, &uuid](auto & node, auto cursor) {
if (node.uuid == uuid) {
moveIndexes.moveCursor = cursor;
}
});
}

MoveNode getRoot() {
return root;
}
Expand Down
2 changes: 1 addition & 1 deletion Shared/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct ContentView: View {
if (showInfo) {
VStack(alignment: .leading, spacing: 10) {
NavigationView(document: $document)
InformationView(document: document)
InformationView(document: $document)
}
.frame(minWidth: 350, idealWidth: 350, maxWidth: 350, alignment: .leading)
}
Expand Down
15 changes: 9 additions & 6 deletions Shared/Views/InformationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import SwiftUI

struct InformationView: View {

let document: ChessDocument
@Binding var document: ChessDocument

let numberFormatter = NumberFormatter()
let valueFormatter = NumberFormatter()

init(document: ChessDocument) {
self.document = document
init(document: Binding<ChessDocument>) {
self._document = document

numberFormatter.numberStyle = .decimal
numberFormatter.groupingSeparator = ","
Expand Down Expand Up @@ -75,7 +75,10 @@ struct InformationView: View {
}

List(document.game.moves, children: \FullMove.children) { item in
FullMoveView(item: item, currentMoveUUID: document.engine.currentMoveNodeUUID())
FullMoveView(item: item, currentMoveUUID: document.engine.currentMoveNodeUUID)
.onTapGesture {
document.currentMoveIndex = UInt(item.id)!
}
}

Spacer()
Expand All @@ -98,11 +101,11 @@ struct BottomInformationView_Previews: PreviewProvider {
static var previews: some View {
Group {
let doc = ChessDocument(pgn: "1. e4 e5 *")
InformationView(document: doc)
InformationView(document: .constant(doc))
}
Group {
let doc = ChessDocument(pgn: "1. e4 e5 2. Nf3 Nf6 3. Nxe5 d6 4. Nc3 dxe5 *")
InformationView(document: doc)
InformationView(document: .constant(doc))
}
}
}

0 comments on commit 7361686

Please sign in to comment.