Skip to content

Commit

Permalink
Implement showing numbers on pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
calcitem committed Apr 27, 2024
1 parent c16288d commit 85987ce
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class DisplaySettings {
this.aiResponseDelayTime = 0.0,
this.isPositionalAdvantageIndicatorShown = false,
this.backgroundImagePath = '',
this.isNumbersOnPiecesShown = false,
});

/// Encodes a Json style map into a [DisplaySettings] object
Expand Down Expand Up @@ -145,6 +146,9 @@ class DisplaySettings {
@HiveField(20, defaultValue: '')
final String backgroundImagePath;

@HiveField(21, defaultValue: false)
final bool isNumbersOnPiecesShown;

/// Decodes a Json from a [DisplaySettings] object
Map<String, dynamic> toJson() => _$DisplaySettingsToJson(this);
}
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ class AppearanceSettingsPage extends StatelessWidget {
titleString: S.of(context).pointWidth,
onTap: () => setPointWidth(context),
),
SettingsListTile.switchTile(
value: displaySettings.isNumbersOnPiecesShown,
onChanged: (bool val) => DB().displaySettings =
displaySettings.copyWith(isNumbersOnPiecesShown: val),
titleString: S.of(context).showNumbersOnPieces,
),
SettingsListTile(
titleString: S.of(context).pieceWidth,
onTap: () => setPieceWidth(context),
Expand Down
28 changes: 28 additions & 0 deletions src/ui/flutter_app/lib/game_page/services/engine/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ part of '../mill.dart';

List<int> posKeyHistory = <int>[];

class SquareAttribute {
SquareAttribute({
required this.placedPieceNumber,
});

int placedPieceNumber;
}

class StateInfo {
// Copied when making a move
int rule50 = 0;
Expand All @@ -37,6 +45,12 @@ class Position {
final List<PieceColor> _grid =
List<PieceColor>.filled(7 * 7, PieceColor.none);

int placedPieceNumber = 0;
final List<SquareAttribute> sqAttrList = List<SquareAttribute>.generate(
sqNumber,
(int index) => SquareAttribute(placedPieceNumber: 0),
);

final Map<PieceColor, int> pieceInHandCount = <PieceColor, int>{
PieceColor.white: DB().ruleSettings.piecesCount,
PieceColor.black: DB().ruleSettings.piecesCount,
Expand Down Expand Up @@ -582,6 +596,10 @@ class Position {

_currentSquare = s;

// Set square number
placedPieceNumber++;
sqAttrList[s].placedPieceNumber = placedPieceNumber;

final int n = _millsCount(_currentSquare);

if (n == 0) {
Expand Down Expand Up @@ -1318,6 +1336,11 @@ extension SetupPosition on Position {
isNeedStalemateRemoval = false;
isStalemateRemoving = false;

placedPieceNumber = 0;
for (int i = 0; i < sqNumber; i++) {
sqAttrList[i].placedPieceNumber = 0;
}

for (int i = 0; i < sqNumber; i++) {
_board[i] = PieceColor.none;
}
Expand Down Expand Up @@ -1386,6 +1409,11 @@ extension SetupPosition on Position {
isNeedStalemateRemoval = pos.isNeedStalemateRemoval;
isStalemateRemoving = pos.isStalemateRemoving;

placedPieceNumber = pos.placedPieceNumber;
for (int i = 0; i < sqNumber; i++) {
sqAttrList[i].placedPieceNumber = pos.sqAttrList[i].placedPieceNumber;
}

for (int i = 0; i < sqNumber; i++) {
_board[i] = pos._board[i];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PiecePaintParam {
required this.pos,
required this.animated,
required this.diameter,
this.squareAttribute,
});

/// The color of the piece.
Expand All @@ -38,6 +39,7 @@ class PiecePaintParam {
final Offset pos;
final bool animated;
final double diameter;
final SquareAttribute? squareAttribute;
}

/// Custom Piece Painter
Expand Down Expand Up @@ -82,6 +84,10 @@ class PiecePainter extends CustomPainter {
continue;
}

final int sq = indexToSquare[index]!;
final SquareAttribute squareAttribute =
GameController().position.sqAttrList[sq];

final Offset pos = pointFromIndex(index, size);
final bool animated = focusIndex == index;

Expand All @@ -91,6 +97,7 @@ class PiecePainter extends CustomPainter {
pos: pos,
animated: animated,
diameter: pieceWidth,
squareAttribute: squareAttribute,
),
);

Expand Down Expand Up @@ -145,6 +152,33 @@ class PiecePainter extends CustomPainter {
piece.animated ? animatedPieceInnerRadius : pieceInnerRadius,
paint,
);

if (DB().displaySettings.isNumbersOnPiecesShown &&
piece.squareAttribute?.placedPieceNumber != null) {
// Text Drawing:
final TextPainter textPainter = TextPainter(
text: TextSpan(
text: piece.squareAttribute?.placedPieceNumber.toString(),
style: TextStyle(
color: piece.piece.pieceColor.computeLuminance() > 0.5
? Colors.black
: Colors.white,
fontSize: piece.diameter * 0.5, // Adjust font size as needed
),
),
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
textPainter.layout();

// Calculate offset for centering the text
final Offset textOffset = Offset(
piece.pos.dx - textPainter.width / 2,
piece.pos.dy - textPainter.height / 2,
);

textPainter.paint(canvas, textOffset);
}
}

// Draw focus and blur position
Expand Down

0 comments on commit 85987ce

Please sign in to comment.