Skip to content

Commit

Permalink
Visual indicator for the current move
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed May 12, 2021
1 parent eb53e02 commit 92735b8
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 10 deletions.
1 change: 1 addition & 0 deletions Shared/Bridge/FEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef void(^FEngineDidUpdateCallback)();

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

- (NSArray<FEngineMove*>* _Nonnull)allMoves;
- (NSArray<FEngineMove*>* _Nonnull)movesAt:(NSUInteger)rank file:(NSUInteger)file;
Expand Down
4 changes: 4 additions & 0 deletions Shared/Bridge/FEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ - (void)moveNodesFromNode:(ChessGame::MoveNode)node
return rootNode.variations;
}

- (NSUInteger)currentMoveNodeUUID {
return engine.game.getCurrentMoveUUID();
}

#pragma mark -

- (NSString*)state {
Expand Down
1 change: 1 addition & 0 deletions Shared/Bridge/FEngineMoveNode+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readwrite) NSUInteger moveNumber;
@property (nonatomic, readwrite) BOOL whiteMove;
@property (nonatomic, readwrite) NSUInteger uuid;
@property (nonatomic, strong, readwrite) NSString * _Nonnull name;
@property (nonatomic, strong, readwrite) NSString * _Nonnull comment;
@property (nonatomic, strong, readwrite) NSMutableArray<FEngineMoveNode*> * _Nonnull variations;
Expand Down
1 change: 1 addition & 0 deletions Shared/Bridge/FEngineMoveNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface FEngineMoveNode: NSObject
@property (nonatomic, readonly) NSUInteger moveNumber;
@property (nonatomic, readonly) BOOL whiteMove;
@property (nonatomic, readonly) NSUInteger uuid;
@property (nonatomic, strong, readonly) NSString * _Nonnull name;
@property (nonatomic, strong, readonly) NSString * _Nonnull comment;
@property (nonatomic, strong, readonly) NSArray<FEngineMoveNode*> * _Nonnull variations;
Expand Down
1 change: 1 addition & 0 deletions Shared/Bridge/FEngineMoveNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ - (instancetype)initWithNode:(ChessGame::MoveNode)node {
if (self = [super init]) {
self.moveNumber = node.moveNumber;
self.whiteMove = MOVE_COLOR(node.move) == WHITE;
self.uuid = node.uuid;
self.name = NSStringFromString(FPGN::to_string(node.move, FPGN::SANType::tight));
self.comment = NSStringFromString(node.comment);
self.variations = [NSMutableArray array];
Expand Down
1 change: 1 addition & 0 deletions Shared/Engine/Engine/ChessGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void ChessGame::move(Move move, std::string comment, bool replace) {
// this is a new variation (either main variation if no variation exists yet).
if (!found || replace) {
MoveNode newNode = MoveNode();
newNode.uuid = nextMoveUUID();
newNode.moveNumber = ceil(moveIndexes.moveCursor / 2) + 1;
newNode.comment = comment;
newNode.move = move;
Expand Down
29 changes: 28 additions & 1 deletion Shared/Engine/Engine/ChessGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ class ChessGame {
// that a game represents. A node has a move associated with it, including
// the next move node (or move nodes if there is more than one variation).
struct MoveNode {
// The move number
// Unique ID for this move node, used to identify uniquely this node
// across any variations. For example, this is used to indicate
// which move node is the current one in the game.
unsigned int uuid;

// The full move number
unsigned moveNumber;

// The move itself
Expand Down Expand Up @@ -149,6 +154,14 @@ class ChessGame {
return move;
}

unsigned int getCurrentMoveUUID() {
unsigned int uuid = 0;
root.lookupNode(0, moveIndexes.moveCursor, moveIndexes, [&uuid](auto & node) {
uuid = node.uuid;
});
return uuid;
}

MoveNode getRoot() {
return root;
}
Expand Down Expand Up @@ -182,6 +195,20 @@ class ChessGame {
MoveNode root;
MoveIndexes moveIndexes;

// Unique ID for each generated move.
// Simply incrementing this variable
// is sufficient.
unsigned int moveUUID;

unsigned int nextMoveUUID() {
if (moveUUID < UINT_MAX) {
moveUUID++;
} else {
moveUUID = 0;
}
return moveUUID;
}

void replayMoves();
};

4 changes: 1 addition & 3 deletions Shared/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ struct ContentView: View {

if (showInfo) {
VStack(alignment: .leading) {
if (document.mode.value != .play) {
AnalyzeActionsView(document: $document)
}
AnalyzeActionsView(document: $document)

InformationView(document: document)
}
Expand Down
24 changes: 19 additions & 5 deletions Shared/Views/FullMoveItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ import SwiftUI
struct FullMoveItemView: View {

var item: FullMoveItem
var currentMoveUUID: UInt

func moveNameText(node: FEngineMoveNode) -> Text {
if node.uuid == currentMoveUUID {
return Text("\(node.name)")
.bold()
.underline()
} else {
return Text("\(node.name)")
}
}

var body: some View {
if let wm = item.whiteMove, let bm = item.blackMove {
Text("\(wm.moveNumber). \(wm.name)") +
Text("\(wm.moveNumber). ") +
moveNameText(node: wm) +
Text(item.whiteComment)
.italic() +
Text("\(bm.name)") +
moveNameText(node: bm) +
Text(item.blackComment)
.italic()
} else if let wm = item.whiteMove {
Text("\(wm.moveNumber). \(wm.name)") +
Text("\(wm.moveNumber). ") +
moveNameText(node: wm) +
Text(item.whiteComment)
.italic()
} else if let bm = item.blackMove {
Text("\(bm.moveNumber)... \(bm.name)") +
Text("\(bm.moveNumber)... ") +
moveNameText(node: bm) +
Text(item.whiteComment)
.italic()
} else {
Expand All @@ -36,6 +50,6 @@ struct FullMoveItemView: View {

struct FullMoveItemView_Previews: PreviewProvider {
static var previews: some View {
FullMoveItemView(item: FullMoveItem())
FullMoveItemView(item: FullMoveItem(), currentMoveUUID: 0)
}
}
2 changes: 1 addition & 1 deletion Shared/Views/InformationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct InformationView: View {
.padding(.bottom)
}
List(document.moveNodes.moveNodes, children: \FullMoveItem.children) { item in
FullMoveItemView(item: item)
FullMoveItemView(item: item, currentMoveUUID: document.engine.currentMoveNodeUUID())
.font(.system(.body, design: .monospaced))
}
Spacer()
Expand Down

0 comments on commit 92735b8

Please sign in to comment.