Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimima committed Jan 8, 2025
1 parent 9b00816 commit 98e9221
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 188 deletions.
19 changes: 6 additions & 13 deletions lib/src/model/game/game_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -954,14 +954,9 @@ class GameState with _$GameState {
}) = _GameState;

/// The [Position] and its legal moves at the current cursor.
(Position, IMap<Square, ISet<Square>>) getCurrentPosition(
CastlingMethod castlingMethod,
) {
(Position, IMap<Square, ISet<Square>>) getCurrentPosition(CastlingMethod castlingMethod) {
final position = game.positionAt(stepCursor);
final legalMoves = makeLegalMoves(
position,
isChess960: game.meta.variant == Variant.chess960,
);
final legalMoves = makeLegalMoves(position, isChess960: game.meta.variant == Variant.chess960);

final Map<Square, Square> castlingMap = {
Square.a1: Square.c1,
Expand All @@ -978,12 +973,10 @@ class GameState with _$GameState {
moves, //king can castle
)
? (switch (castlingMethod) {
CastlingMethod.kingOverRook =>
moves.removeAll(castlingMap.values),
CastlingMethod.kingTwoSquares =>
moves.removeAll(castlingMap.keys),
_ => moves
})
CastlingMethod.kingOverRook => moves.removeAll(castlingMap.values),
CastlingMethod.kingTwoSquares => moves.removeAll(castlingMap.keys),
_ => moves,
})
: moves,
);
}
Expand Down
136 changes: 58 additions & 78 deletions lib/src/model/settings/board_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const kBoardDefaultBrightnessFilter = 1.0;
const kBoardDefaultHueFilter = 0.0;

@riverpod
class BoardPreferences extends _$BoardPreferences
with PreferencesStorage<BoardPrefs> {
class BoardPreferences extends _$BoardPreferences with PreferencesStorage<BoardPrefs> {
// ignore: avoid_public_notifier_properties
@override
PrefCategory get prefCategory => PrefCategory.board;
Expand Down Expand Up @@ -46,9 +45,7 @@ class BoardPreferences extends _$BoardPreferences
}

Future<void> setCastlingMethod(CastlingMethod castlingMethod) {
return save(
state.copyWith(castlingMethod: castlingMethod),
);
return save(state.copyWith(castlingMethod: castlingMethod));
}

Future<void> toggleHapticFeedback() {
Expand All @@ -57,9 +54,7 @@ class BoardPreferences extends _$BoardPreferences

Future<void> toggleImmersiveModeWhilePlaying() {
return save(
state.copyWith(
immersiveModeWhilePlaying:
!(state.immersiveModeWhilePlaying ?? false)),
state.copyWith(immersiveModeWhilePlaying: !(state.immersiveModeWhilePlaying ?? false)),
);
}

Expand All @@ -84,36 +79,31 @@ class BoardPreferences extends _$BoardPreferences
}

Future<void> toggleMagnifyDraggedPiece() {
return save(
state.copyWith(magnifyDraggedPiece: !state.magnifyDraggedPiece));
return save(state.copyWith(magnifyDraggedPiece: !state.magnifyDraggedPiece));
}

Future<void> setDragTargetKind(DragTargetKind dragTargetKind) {
return save(state.copyWith(dragTargetKind: dragTargetKind));
}

Future<void> setMaterialDifferenceFormat(
MaterialDifferenceFormat materialDifferenceFormat) {
return save(
state.copyWith(materialDifferenceFormat: materialDifferenceFormat));
Future<void> setMaterialDifferenceFormat(MaterialDifferenceFormat materialDifferenceFormat) {
return save(state.copyWith(materialDifferenceFormat: materialDifferenceFormat));
}

Future<void> setClockPosition(ClockPosition clockPosition) {
return save(state.copyWith(clockPosition: clockPosition));
}

Future<void> toggleEnableShapeDrawings() {
return save(
state.copyWith(enableShapeDrawings: !state.enableShapeDrawings));
return save(state.copyWith(enableShapeDrawings: !state.enableShapeDrawings));
}

Future<void> setShapeColor(ShapeColor shapeColor) {
return save(state.copyWith(shapeColor: shapeColor));
}

Future<void> adjustColors({double? brightness, double? hue}) {
return save(state.copyWith(
brightness: brightness ?? state.brightness, hue: hue ?? state.hue));
return save(state.copyWith(brightness: brightness ?? state.brightness, hue: hue ?? state.hue));
}
}

Expand All @@ -137,24 +127,19 @@ class BoardPrefs with _$BoardPrefs implements Serializable {
)
required MaterialDifferenceFormat materialDifferenceFormat,
required ClockPosition clockPosition,
@JsonKey(
defaultValue: PieceShiftMethod.either,
unknownEnumValue: PieceShiftMethod.either)
@JsonKey(defaultValue: PieceShiftMethod.either, unknownEnumValue: PieceShiftMethod.either)
required PieceShiftMethod pieceShiftMethod,
required CastlingMethod castlingMethod,

/// Whether to enable shape drawings on the board for games and puzzles.
@JsonKey(defaultValue: true) required bool enableShapeDrawings,
@JsonKey(defaultValue: true) required bool magnifyDraggedPiece,
@JsonKey(
defaultValue: DragTargetKind.circle,
unknownEnumValue: DragTargetKind.circle)
@JsonKey(defaultValue: DragTargetKind.circle, unknownEnumValue: DragTargetKind.circle)
required DragTargetKind dragTargetKind,
@JsonKey(defaultValue: ShapeColor.green, unknownEnumValue: ShapeColor.green)
required ShapeColor shapeColor,
@JsonKey(defaultValue: false) required bool showBorder,
@JsonKey(defaultValue: kBoardDefaultBrightnessFilter)
required double brightness,
@JsonKey(defaultValue: kBoardDefaultBrightnessFilter) required double brightness,
@JsonKey(defaultValue: kBoardDefaultHueFilter) required double hue,
}) = _BoardPrefs;

Expand All @@ -181,19 +166,18 @@ class BoardPrefs with _$BoardPrefs implements Serializable {
);

bool get hasColorAdjustments =>
brightness != kBoardDefaultBrightnessFilter ||
hue != kBoardDefaultHueFilter;
brightness != kBoardDefaultBrightnessFilter || hue != kBoardDefaultHueFilter;

ChessboardSettings toBoardSettings() {
return ChessboardSettings(
pieceAssets: pieceSet.assets,
colorScheme: boardTheme.colors,
brightness: brightness,
hue: hue,
border: showBorder
? BoardBorder(
color: darken(boardTheme.colors.darkSquare, 0.2), width: 16.0)
: null,
border:
showBorder
? BoardBorder(color: darken(boardTheme.colors.darkSquare, 0.2), width: 16.0)
: null,
showValidMoves: showLegalMoves,
showLastMove: boardHighlights,
enableCoordinates: coordinates,
Expand All @@ -202,8 +186,7 @@ class BoardPrefs with _$BoardPrefs implements Serializable {
dragFeedbackOffset: Offset(0.0, magnifyDraggedPiece ? -1.0 : 0.0),
dragTargetKind: dragTargetKind,
pieceShiftMethod: pieceShiftMethod,
drawShape: DrawShapeOptions(
enable: enableShapeDrawings, newShapeColor: shapeColor.color),
drawShape: DrawShapeOptions(enable: enableShapeDrawings, newShapeColor: shapeColor.color),
);
}

Expand All @@ -223,12 +206,11 @@ enum ShapeColor {
yellow;

Color get color => Color(switch (this) {
ShapeColor.green => 0x15781B,
ShapeColor.red => 0x882020,
ShapeColor.blue => 0x003088,
ShapeColor.yellow => 0xe68f00,
})
.withAlpha(0xAA);
ShapeColor.green => 0x15781B,
ShapeColor.red => 0x882020,
ShapeColor.blue => 0x003088,
ShapeColor.yellow => 0xe68f00,
}).withAlpha(0xAA);
}

/// The chessboard theme.
Expand Down Expand Up @@ -316,27 +298,29 @@ enum BoardTheme {
}
}

Widget get thumbnail => this == BoardTheme.system
? SizedBox(
height: 44,
width: 44 * 6,
child: Row(
children: [
for (final c in const [1, 2, 3, 4, 5, 6])
Container(
width: 44,
color: c.isEven
? BoardTheme.system.colors.darkSquare
: BoardTheme.system.colors.lightSquare,
),
],
),
)
: Image.asset(
'assets/board-thumbnails/$name.jpg',
height: 44,
errorBuilder: (context, o, st) => const SizedBox.shrink(),
);
Widget get thumbnail =>
this == BoardTheme.system
? SizedBox(
height: 44,
width: 44 * 6,
child: Row(
children: [
for (final c in const [1, 2, 3, 4, 5, 6])
Container(
width: 44,
color:
c.isEven
? BoardTheme.system.colors.darkSquare
: BoardTheme.system.colors.lightSquare,
),
],
),
)
: Image.asset(
'assets/board-thumbnails/$name.jpg',
height: 44,
errorBuilder: (context, o, st) => const SizedBox.shrink(),
);
}

enum MaterialDifferenceFormat {
Expand All @@ -351,11 +335,11 @@ enum MaterialDifferenceFormat {
bool get visible => this != MaterialDifferenceFormat.hidden;

String l10n(AppLocalizations l10n) => switch (this) {
//TODO: Add l10n
MaterialDifferenceFormat.materialDifference => materialDifference.label,
MaterialDifferenceFormat.capturedPieces => capturedPieces.label,
MaterialDifferenceFormat.hidden => hidden.label,
};
//TODO: Add l10n
MaterialDifferenceFormat.materialDifference => materialDifference.label,
MaterialDifferenceFormat.capturedPieces => capturedPieces.label,
MaterialDifferenceFormat.hidden => hidden.label,
};
}

enum ClockPosition {
Expand All @@ -364,19 +348,15 @@ enum ClockPosition {

// TODO: l10n
String get label => switch (this) {
ClockPosition.left => 'Left',
ClockPosition.right => 'Right',
};
ClockPosition.left => 'Left',
ClockPosition.right => 'Right',
};
}

enum CastlingMethod {
kingOverRook,
kingTwoSquares,
either;
}
enum CastlingMethod { kingOverRook, kingTwoSquares, either }

String dragTargetKindLabel(DragTargetKind kind) => switch (kind) {
DragTargetKind.circle => 'Circle',
DragTargetKind.square => 'Square',
DragTargetKind.none => 'None',
};
DragTargetKind.circle => 'Circle',
DragTargetKind.square => 'Square',
DragTargetKind.none => 'None',
};
5 changes: 3 additions & 2 deletions lib/src/view/game/game_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ class GameBody extends ConsumerWidget {

return gameStateAsync.when(
data: (gameState) {
final (position, legalMoves) =
gameState.getCurrentPosition(boardPreferences.castlingMethod);
final (position, legalMoves) = gameState.getCurrentPosition(
boardPreferences.castlingMethod,
);
final youAre = gameState.game.youAre ?? Side.white;
final archivedBlackClock = gameState.game.archivedBlackClockAt(gameState.stepCursor);
final archivedWhiteClock = gameState.game.archivedWhiteClockAt(gameState.stepCursor);
Expand Down
Loading

0 comments on commit 98e9221

Please sign in to comment.