Skip to content
Snippets Groups Projects
Commit 50a369ca authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Merge branch '49-improve-draw-board-game' into 'master'

Resolve "Improve draw board game"

Closes #49

See merge request !48
parents f56b5a90 2c4b81c8
No related branches found
No related tags found
1 merge request!48Resolve "Improve draw board game"
Pipeline #4877 passed
org.gradle.jvmargs=-Xmx1536M org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
app.versionName=1.0.38 app.versionName=1.0.39
app.versionCode=39 app.versionCode=40
...@@ -8,12 +8,12 @@ part 'game_state.dart'; ...@@ -8,12 +8,12 @@ part 'game_state.dart';
class GameCubit extends HydratedCubit<GameState> { class GameCubit extends HydratedCubit<GameState> {
GameCubit() : super(const GameState()); GameCubit() : super(const GameState());
void getData(GameState state) { void getData(GameState gameState) {
emit(state); emit(gameState);
} }
void updateGameState(GameData game) { void updateGameState(GameData gameData) {
emit(GameState(game: game)); emit(GameState(game: gameData));
} }
@override @override
......
import 'dart:ui' as ui;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:random/config/app_colors.dart'; import 'package:random/config/app_colors.dart';
import 'package:random/utils/color_extensions.dart';
class CellPainter extends CustomPainter { class CellPainter extends CustomPainter {
const CellPainter({required this.value}); const CellPainter({required this.value});
...@@ -24,12 +27,33 @@ class CellPainter extends CustomPainter { ...@@ -24,12 +27,33 @@ class CellPainter extends CustomPainter {
@override @override
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
final paintBackground = Paint(); final baseColor = getIndexedColor(value);
paintBackground.color = getIndexedColor(value);
paintBackground.style = PaintingStyle.fill; const borderWidth = 0.05;
final Rect baseSquare = Rect.fromPoints(Offset(0, 0), Offset(size.width, size.height));
final paintBaseSquare = Paint()
..style = PaintingStyle.fill
..color = baseColor.darken(40);
canvas.drawRect(baseSquare, paintBaseSquare);
final Rect innerGradientBackground = Rect.fromPoints(
Offset(size.width * borderWidth, size.height * borderWidth),
Offset(size.width * (1 - borderWidth), size.height * (1 - borderWidth)));
final paintInnerBackground = Paint()
..shader = ui.Gradient.linear(
baseSquare.topCenter,
baseSquare.bottomCenter,
[
baseColor.lighten(10),
baseColor.darken(20),
],
);
final Rect rectBackground = Rect.fromPoints(Offset(0, 0), Offset(size.width, size.height)); canvas.drawRect(innerGradientBackground, paintInnerBackground);
canvas.drawRect(rectBackground, paintBackground);
} }
@override @override
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unicons/unicons.dart';
import 'package:random/cubit/game_cubit.dart'; import 'package:random/cubit/game_cubit.dart';
import 'package:random/models/game_data.dart'; import 'package:random/models/game_data.dart';
import 'package:random/ui/widgets/game_board.dart'; import 'package:random/ui/widgets/game_board.dart';
import 'package:unicons/unicons.dart';
class GamePage extends StatefulWidget { class GamePage extends StatefulWidget {
const GamePage({super.key}); const GamePage({super.key});
...@@ -13,31 +13,35 @@ class GamePage extends StatefulWidget { ...@@ -13,31 +13,35 @@ class GamePage extends StatefulWidget {
State<GamePage> createState() => _GamePageState(); State<GamePage> createState() => _GamePageState();
} }
void createNewGame(GameCubit gameCubit, int boardSize) {
final GameData newGame = GameData.createRandom(boardSize);
gameCubit.updateGameState(newGame);
}
class _GamePageState extends State<GamePage> { class _GamePageState extends State<GamePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<GameCubit, GameState>( return BlocBuilder<GameCubit, GameState>(
builder: (context, gameState) { builder: (context, gameState) {
const boardSize = 6;
const double boardWidgetWidth = 300;
const double boardWidgetHeight = 300;
final GameCubit gameCubit = BlocProvider.of<GameCubit>(context);
return Column( return Column(
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: [ children: [
gameState.game != null gameState.game != null
? Container( ? GameBoardWidget(
margin: EdgeInsets.all(4), gameData: gameState.game!,
padding: EdgeInsets.all(4), size: Size(boardWidgetWidth, boardWidgetHeight),
child: GameBoardWidget(
gameData: gameState.game!,
),
) )
: SizedBox.shrink(), : SizedBox.shrink(),
IconButton( IconButton(
onPressed: () { onPressed: () {
const boardSize = 6; createNewGame(gameCubit, boardSize);
final GameData newGame = GameData.createRandom(boardSize);
BlocProvider.of<GameCubit>(context).updateGameState(newGame);
print(gameState);
}, },
icon: Icon(UniconsSolid.star), icon: Icon(UniconsSolid.star),
color: Colors.white, color: Colors.white,
......
...@@ -3,29 +3,39 @@ import 'package:flutter/material.dart'; ...@@ -3,29 +3,39 @@ import 'package:flutter/material.dart';
import 'package:random/models/game_data.dart'; import 'package:random/models/game_data.dart';
import 'package:random/ui/painters/cell_painter.dart'; import 'package:random/ui/painters/cell_painter.dart';
class GameBoardWidget extends StatelessWidget { class GameBoardWidget extends StatefulWidget {
const GameBoardWidget({super.key, required this.gameData}); const GameBoardWidget({
super.key,
required this.gameData,
required this.size,
});
final GameData gameData; final GameData gameData;
final Size size;
@override
State<GameBoardWidget> createState() => _GameBoardWidgetState();
}
class _GameBoardWidgetState extends State<GameBoardWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
const staticBoardWidth = 300; final widgetWidth = widget.size.width;
const staticBoardHeight = 300; final widgetHeight = widget.size.height;
final rowsCount = this.gameData.board.length; final rowsCount = widget.gameData.board.length;
final columnsCount = this.gameData.board[0].length; final columnsCount = widget.gameData.board[0].length;
print('counts: rows=' + rowsCount.toString() + ' / columns=' + columnsCount.toString()); print('counts: rows=' + rowsCount.toString() + ' / columns=' + columnsCount.toString());
final cellWidth = staticBoardWidth / columnsCount; final cellWidth = widgetWidth / columnsCount;
final cellHeight = staticBoardHeight / rowsCount; final cellHeight = widgetHeight / rowsCount;
print('cell: width=' + cellWidth.toString() + ' / height=' + cellHeight.toString()); print('cell: width=' + cellWidth.toString() + ' / height=' + cellHeight.toString());
final List<Widget> cells = []; final List<Widget> cells = [];
for (var y = 0; y < rowsCount; y++) { for (var y = 0; y < rowsCount; y++) {
for (var x = 0; x < columnsCount; x++) { for (var x = 0; x < columnsCount; x++) {
final GameDataItem item = this.gameData.board[y][x]; final GameDataItem item = widget.gameData.board[y][x];
final Widget cellContent = CustomPaint( final Widget cellContent = CustomPaint(
size: Size(cellWidth, cellHeight), size: Size(cellWidth, cellHeight),
...@@ -33,24 +43,23 @@ class GameBoardWidget extends StatelessWidget { ...@@ -33,24 +43,23 @@ class GameBoardWidget extends StatelessWidget {
painter: CellPainter(value: item.value), painter: CellPainter(value: item.value),
); );
final Widget widget = Positioned( final Widget cellWidget = Positioned(
left: (x * cellWidth).toDouble(), left: (x * cellWidth).toDouble(),
top: (y * cellHeight).toDouble(), top: (y * cellHeight).toDouble(),
child: Container( child: Container(
width: cellWidth, width: cellWidth,
height: cellHeight, height: cellHeight,
color: Colors.deepPurpleAccent,
child: cellContent, child: cellContent,
), ),
); );
cells.add(widget); cells.add(cellWidget);
} }
} }
return Container( return Container(
width: staticBoardWidth.toDouble(), width: widgetWidth,
height: staticBoardHeight.toDouble(), height: widgetHeight,
color: Colors.grey, color: Colors.grey,
child: Stack( child: Stack(
children: cells, children: cells,
......
import 'dart:ui';
extension ColorExtension on Color {
Color darken([int percent = 40]) {
assert(1 <= percent && percent <= 100);
final value = 1 - percent / 100;
return Color.fromARGB(
alpha,
(red * value).round(),
(green * value).round(),
(blue * value).round(),
);
}
Color lighten([int percent = 40]) {
assert(1 <= percent && percent <= 100);
final value = percent / 100;
return Color.fromARGB(
alpha,
(red + ((255 - red) * value)).round(),
(green + ((255 - green) * value)).round(),
(blue + ((255 - blue) * value)).round(),
);
}
Color avg(Color other) {
final red = (this.red + other.red) ~/ 2;
final green = (this.green + other.green) ~/ 2;
final blue = (this.blue + other.blue) ~/ 2;
final alpha = (this.alpha + other.alpha) ~/ 2;
return Color.fromARGB(alpha, red, green, blue);
}
}
...@@ -3,7 +3,7 @@ description: A random application, for testing purpose only. ...@@ -3,7 +3,7 @@ description: A random application, for testing purpose only.
publish_to: 'none' publish_to: 'none'
version: 1.0.38+39 version: 1.0.39+40
environment: environment:
sdk: '^3.0.0' sdk: '^3.0.0'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment