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

Merge branch '93-use-activityparameters-widgets-from-flutter_custom_toolbox' into 'master'

Resolve "Use ActivityParameters widgets from flutter_custom_toolbox"

Closes #93

See merge request !89
parents 18c44ca4 a4cff224
No related branches found
No related tags found
1 merge request!89Resolve "Use ActivityParameters widgets from flutter_custom_toolbox"
Pipeline #7133 passed
Showing
with 359 additions and 735 deletions
Use ActivityParameters widgets from flutter_custom_toolbox.
Utilisation des widgets ActivityParameters de flutter_custom_toolbox.
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/common/ui/pages/game.dart';
import 'package:sudoku/common/ui/pages/parameters.dart';
import 'package:sudoku/cubit/activity/activity_cubit.dart';
import 'package:sudoku/config/application_config.dart';
import 'package:sudoku/models/activity/activity.dart';
import 'package:sudoku/ui/pages/game.dart';
class ActivityPageItem {
final String code;
final Icon icon;
final Widget page;
final Widget Function({required Activity currentActivity})? builder;
const ActivityPageItem({
required this.code,
required this.icon,
required this.page,
required this.builder,
});
}
......@@ -20,20 +22,27 @@ class ActivityPage {
static const bool displayBottomNavBar = false;
static const indexHome = 0;
static const pageHome = ActivityPageItem(
static final ActivityPageItem pageHome = ActivityPageItem(
code: 'page_home',
icon: Icon(UniconsLine.home),
page: PageParameters(),
builder: ({required Activity currentActivity}) {
return PageParameters(
config: ApplicationConfig.config,
canBeResumed: currentActivity.canBeResumed,
);
},
);
static const indexGame = 1;
static const pageGame = ActivityPageItem(
static final pageGame = ActivityPageItem(
code: 'page_game',
icon: Icon(UniconsLine.star),
page: PageGame(),
builder: ({required Activity currentActivity}) {
return PageGame();
},
);
static const Map<int, ActivityPageItem> items = {
static final Map<int, ActivityPageItem> items = {
indexHome: pageHome,
indexGame: pageGame,
};
......@@ -45,6 +54,16 @@ class ActivityPage {
}
static Widget getWidget(int pageIndex) {
return items[pageIndex]?.page ?? pageHome.page;
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
if (items.keys.contains(pageIndex)) {
return items[pageIndex]?.builder!(currentActivity: currentActivity) ?? Text('oups');
} else {
return getWidget(defaultPageIndex);
}
},
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/common/cubit/nav/nav_cubit_pages.dart';
import 'package:sudoku/common/ui/parameters/parameter_widget.dart';
import 'package:sudoku/config/default_activity_settings.dart';
import 'package:sudoku/config/default_global_settings.dart';
import 'package:sudoku/cubit/activity/activity_cubit.dart';
import 'package:sudoku/cubit/settings/settings_activity_cubit.dart';
import 'package:sudoku/cubit/settings/settings_global_cubit.dart';
import 'package:sudoku/models/activity/activity.dart';
class PageParameters extends StatelessWidget {
const PageParameters({super.key});
final double separatorHeight = 8.0;
@override
Widget build(BuildContext context) {
return BlocBuilder<ActivitySettingsCubit, ActivitySettingsState>(
builder: (BuildContext context, ActivitySettingsState activitySettingsState) {
return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>(
builder: (BuildContext context, GlobalSettingsState globalSettingsState) {
return BlocBuilder<ActivityCubit, ActivityState>(
builder: (BuildContext context, ActivityState activityState) {
final Activity currentActivity = activityState.currentActivity;
final List<Widget> lines = [];
// Activity settings
for (String code in DefaultActivitySettings.availableParameters) {
lines.add(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: buildParametersLine(
code: code,
isGlobal: false,
),
));
lines.add(SizedBox(height: separatorHeight));
}
lines.add(Expanded(
child: SizedBox(height: separatorHeight),
));
if (currentActivity.canBeResumed == false) {
// Start new activity
lines.add(
AspectRatio(
aspectRatio: 3,
child: ActivityButtonStartNew(
onPressed: () {
BlocProvider.of<ActivityCubit>(context).startNewActivity(
activitySettings: activitySettingsState.settings,
globalSettings: globalSettingsState.settings,
);
BlocProvider.of<NavCubitPage>(context).goToPageGame();
},
),
),
);
} else {
// Resume activity
lines.add(AspectRatio(
aspectRatio: 3,
child: ActivityButtonResumeSaved(
onPressed: () {
BlocProvider.of<ActivityCubit>(context).resumeSavedActivity();
BlocProvider.of<NavCubitPage>(context).goToPageGame();
},
),
));
// Delete saved activity
lines.add(SizedBox.square(
dimension: MediaQuery.of(context).size.width / 5,
child: ActivityButtonDeleteSaved(
onPressed: () {
BlocProvider.of<ActivityCubit>(context).deleteSavedActivity();
},
),
));
}
lines.add(SizedBox(height: separatorHeight));
// Global settings
for (String code in DefaultGlobalSettings.availableParameters) {
lines.add(Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: buildParametersLine(
code: code,
isGlobal: true,
),
));
lines.add(SizedBox(height: separatorHeight));
}
return Column(
children: lines,
);
},
);
},
);
},
);
}
List<Widget> buildParametersLine({
required String code,
required bool isGlobal,
}) {
final List<Widget> parameterButtons = [];
final List<String> availableValues = isGlobal
? DefaultGlobalSettings.getAvailableValues(code)
: DefaultActivitySettings.getAvailableValues(code);
if (availableValues.length <= 1) {
return [];
}
for (String value in availableValues) {
final Widget parameterButton = BlocBuilder<ActivitySettingsCubit, ActivitySettingsState>(
builder: (BuildContext context, ActivitySettingsState activitySettingsState) {
return BlocBuilder<GlobalSettingsCubit, GlobalSettingsState>(
builder: (BuildContext context, GlobalSettingsState globalSettingsState) {
final ActivitySettingsCubit activitySettingsCubit =
BlocProvider.of<ActivitySettingsCubit>(context);
final GlobalSettingsCubit globalSettingsCubit =
BlocProvider.of<GlobalSettingsCubit>(context);
final String currentValue = isGlobal
? globalSettingsCubit.getParameterValue(code)
: activitySettingsCubit.getParameterValue(code);
final bool isSelected = (value == currentValue);
final double displayWidth = MediaQuery.of(context).size.width;
final double itemWidth = displayWidth / availableValues.length - 4;
return SizedBox.square(
dimension: itemWidth,
child: ParameterWidget(
code: code,
value: value,
isSelected: isSelected,
size: itemWidth,
activitySettings: activitySettingsState.settings,
globalSettings: globalSettingsState.settings,
onPressed: () {
isGlobal
? globalSettingsCubit.setParameterValue(code, value)
: activitySettingsCubit.setParameterValue(code, value);
},
),
);
},
);
},
);
parameterButtons.add(parameterButton);
}
return parameterButtons;
}
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/common/ui/parameters/parameter_painter.dart';
import 'package:sudoku/config/default_activity_settings.dart';
import 'package:sudoku/config/default_global_settings.dart';
import 'package:sudoku/models/settings/settings_activity.dart';
import 'package:sudoku/models/settings/settings_global.dart';
class ParameterWidget extends StatelessWidget {
const ParameterWidget({
super.key,
required this.code,
required this.value,
required this.isSelected,
required this.size,
required this.activitySettings,
required this.globalSettings,
required this.onPressed,
});
final String code;
final String value;
final bool isSelected;
final double size;
final ActivitySettings activitySettings;
final GlobalSettings globalSettings;
final VoidCallback onPressed;
static const Color buttonColorActive = Colors.blue;
static const Color buttonColorInactive = Colors.white;
static const double buttonBorderWidth = 4.0;
static const double buttonBorderRadius = 12.0;
@override
Widget build(BuildContext context) {
Widget content = const SizedBox.shrink();
switch (code) {
case DefaultActivitySettings.parameterCodeLevel:
content = getLevelParameterItem();
break;
case DefaultActivitySettings.parameterCodeSize:
content = getSizeParameterItem();
break;
case DefaultGlobalSettings.parameterCodeSkin:
content = getSkinParameterItem();
break;
default:
printlog('Unknown parameter: $code/$value');
content = getUnknownParameterItem();
}
final Color buttonColor = isSelected ? buttonColorActive : buttonColorInactive;
return Container(
decoration: BoxDecoration(
color: buttonColor,
borderRadius: BorderRadius.circular(buttonBorderRadius),
border: Border.all(
color: buttonColor,
width: buttonBorderWidth,
),
),
child: content,
);
}
// "unknown" parameter -> simple block with text
Widget getUnknownParameterItem() {
return StyledButton.text(
caption: '$code / $value',
color: Colors.grey,
onPressed: null,
);
}
Widget getLevelParameterItem() {
Color backgroundColor = Colors.grey;
switch (value) {
case DefaultActivitySettings.levelValueEasy:
backgroundColor = Colors.green;
break;
case DefaultActivitySettings.levelValueMedium:
backgroundColor = Colors.orange;
break;
case DefaultActivitySettings.levelValueHard:
backgroundColor = Colors.red;
break;
case DefaultActivitySettings.levelValueNightmare:
backgroundColor = Colors.purple;
break;
default:
printlog('Wrong value for level parameter value: $value');
}
return StyledButton(
color: backgroundColor,
onPressed: onPressed,
child: CustomPaint(
size: Size(size, size),
willChange: false,
painter: ParameterPainter(
code: code,
value: value,
activitySettings: activitySettings,
globalSettings: globalSettings,
),
isComplex: true,
),
);
}
Widget getSizeParameterItem() {
Color backgroundColor = Colors.grey;
switch (value) {
case DefaultActivitySettings.sizeValueTiny:
backgroundColor = Colors.green;
break;
case DefaultActivitySettings.sizeValueSmall:
backgroundColor = Colors.orange;
break;
case DefaultActivitySettings.sizeValueStandard:
backgroundColor = Colors.red;
break;
case DefaultActivitySettings.sizeValueLarge:
backgroundColor = Colors.purple;
break;
default:
printlog('Wrong value for size parameter value: $value');
}
return StyledButton(
color: backgroundColor,
onPressed: onPressed,
child: CustomPaint(
size: Size(size, size),
willChange: false,
painter: ParameterPainter(
code: code,
value: value,
activitySettings: activitySettings,
globalSettings: globalSettings,
),
isComplex: true,
),
);
}
Widget getSkinParameterItem() {
return StyledButton(
color: Colors.green.shade800,
onPressed: onPressed,
child: Image(
image: AssetImage('assets/ui/${DefaultGlobalSettings.parameterCodeSkin}_$value.png'),
fit: BoxFit.fill,
),
);
}
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/common/cubit/nav/nav_cubit_pages.dart';
import 'package:sudoku/cubit/activity/activity_cubit.dart';
import 'package:sudoku/ui/parameters/parameter_painter_board_size.dart';
import 'package:sudoku/ui/parameters/parameter_painter_difficulty_level.dart';
class ApplicationConfig {
static const String appTitle = 'Sudoku';
static const String parameterCodeDifficultyLevel = 'activity.difficultyLevel';
static const String difficultyLevelValueEasy = 'easy';
static const String difficultyLevelValueMedium = 'medium';
static const String difficultyLevelValueHard = 'hard';
static const String difficultyLevelValueNightmare = 'nightmare';
static const String parameterCodeBoardSize = 'activity.boardSize';
static const String boardSizeValueTiny = '2x2';
static const String boardSizeValueSmall = '3x2';
static const String boardSizeValueStandard = '3x3';
static const String boardSizeValueLarge = '4x4';
static const String parameterCodeSkin = 'global.skin';
static const String skinValueDigits = 'digits';
static const String skinValueFood = 'food';
static const String skinValueNature = 'nature';
static const String skinValueMonsters = 'monsters';
static const List<String> shufflableSkins = [
skinValueFood,
skinValueNature,
skinValueMonsters,
];
static const int defaultTipCountDownValueInSeconds = 20;
static final ApplicationConfigDefinition config = ApplicationConfigDefinition(
appTitle: 'Sudoku',
activitySettings: [
// difficulty level
ApplicationSettingsParameter(
code: parameterCodeDifficultyLevel,
values: [
ApplicationSettingsParameterItemValue(
value: difficultyLevelValueEasy,
color: Colors.green,
),
ApplicationSettingsParameterItemValue(
value: difficultyLevelValueMedium,
color: Colors.orange,
isDefault: true,
),
ApplicationSettingsParameterItemValue(
value: difficultyLevelValueHard,
color: Colors.red,
),
ApplicationSettingsParameterItemValue(
value: difficultyLevelValueNightmare,
color: Colors.purple,
),
],
customPainter: (context, value) => ParameterPainterDifficultyLevel(
context: context,
value: value,
),
),
// board size
ApplicationSettingsParameter(
code: parameterCodeBoardSize,
values: [
ApplicationSettingsParameterItemValue(
value: boardSizeValueTiny,
color: Colors.green,
),
ApplicationSettingsParameterItemValue(
value: boardSizeValueSmall,
color: Colors.orange,
),
ApplicationSettingsParameterItemValue(
value: boardSizeValueStandard,
color: Colors.red,
isDefault: true,
),
ApplicationSettingsParameterItemValue(
value: boardSizeValueLarge,
color: Colors.purple,
),
],
customPainter: (context, value) => ParameterPainterBoardSize(
context: context,
value: value,
),
),
// skin
ApplicationSettingsParameter(
code: parameterCodeSkin,
values: [
ApplicationSettingsParameterItemValue(
value: skinValueDigits,
isDefault: true,
),
ApplicationSettingsParameterItemValue(
value: skinValueFood,
),
ApplicationSettingsParameterItemValue(
value: skinValueNature,
),
ApplicationSettingsParameterItemValue(
value: skinValueMonsters,
),
],
builder: ({
required context,
required itemValue,
required onPressed,
required size,
}) =>
StyledButton(
color: Colors.green.shade800,
onPressed: onPressed,
child: Image(
image: AssetImage('assets/ui/skin_${itemValue.value}.png'),
fit: BoxFit.fill,
),
),
),
],
startNewActivity: (BuildContext context) {
BlocProvider.of<ActivityCubit>(context).startNewActivity(context);
BlocProvider.of<NavCubitPage>(context).goToPageGame();
},
deleteCurrentActivity: (BuildContext context) {
BlocProvider.of<ActivityCubit>(context).deleteSavedActivity();
},
resumeActivity: (BuildContext context) {
BlocProvider.of<ActivityCubit>(context).resumeSavedActivity();
BlocProvider.of<NavCubitPage>(context).goToPageGame();
},
);
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class DefaultActivitySettings {
// available game parameters codes
static const String parameterCodeLevel = 'level';
static const String parameterCodeSize = 'size';
static const List<String> availableParameters = [
parameterCodeLevel,
parameterCodeSize,
];
// level: available values
static const String levelValueEasy = 'easy';
static const String levelValueMedium = 'medium';
static const String levelValueHard = 'hard';
static const String levelValueNightmare = 'nightmare';
static const List<String> allowedLevelValues = [
levelValueEasy,
levelValueMedium,
levelValueHard,
levelValueNightmare,
];
// level: default value
static const String defaultLevelValue = levelValueMedium;
// size: available values
static const String sizeValueTiny = '2x2';
static const String sizeValueSmall = '3x2';
static const String sizeValueStandard = '3x3';
static const String sizeValueLarge = '4x4';
static const List<String> allowedSizeValues = [
sizeValueTiny,
sizeValueSmall,
sizeValueStandard,
sizeValueLarge,
];
// size: default value
static const String defaultSizeValue = sizeValueStandard;
// available values from parameter code
static List<String> getAvailableValues(String parameterCode) {
switch (parameterCode) {
case parameterCodeLevel:
return DefaultActivitySettings.allowedLevelValues;
case parameterCodeSize:
return DefaultActivitySettings.allowedSizeValues;
}
printlog('Did not find any available value for game parameter "$parameterCode".');
return [];
}
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
class DefaultGlobalSettings {
// available global parameters codes
static const String parameterCodeSkin = 'skin';
static const List<String> availableParameters = [
parameterCodeSkin,
];
// skin: available values
static const String skinValueDigits = 'digits';
static const String skinValueFood = 'food';
static const String skinValueNature = 'nature';
static const String skinValueMonsters = 'monsters';
static const List<String> allowedSkinValues = [
skinValueDigits,
skinValueFood,
skinValueNature,
skinValueMonsters,
];
// skin: default value
static const String defaultSkinValue = skinValueDigits;
// skin: shufflable skins (without order in items)
static const List<String> shufflableSkins = [
skinValueFood,
skinValueNature,
skinValueMonsters,
];
// available values from parameter code
static List<String> getAvailableValues(String parameterCode) {
switch (parameterCode) {
case parameterCodeSkin:
return DefaultGlobalSettings.allowedSkinValues;
}
printlog('Did not find any available value for global parameter "$parameterCode".');
return [];
}
static const int defaultTipCountDownValueInSeconds = 20;
}
......@@ -4,12 +4,10 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/default_global_settings.dart';
import 'package:sudoku/config/application_config.dart';
import 'package:sudoku/models/activity/cell.dart';
import 'package:sudoku/models/activity/cell_location.dart';
import 'package:sudoku/models/activity/activity.dart';
import 'package:sudoku/models/settings/settings_activity.dart';
import 'package:sudoku/models/settings/settings_global.dart';
import 'package:sudoku/utils/board_animate.dart';
part 'activity_state.dart';
......@@ -30,7 +28,6 @@ class ActivityCubit extends HydratedCubit<ActivityState> {
final Activity activity = Activity(
// Settings
activitySettings: state.currentActivity.activitySettings,
globalSettings: state.currentActivity.globalSettings,
// State
isRunning: state.currentActivity.isRunning,
isStarted: state.currentActivity.isStarted,
......@@ -56,14 +53,13 @@ class ActivityCubit extends HydratedCubit<ActivityState> {
updateState(activity);
}
void startNewActivity({
required ActivitySettings activitySettings,
required GlobalSettings globalSettings,
}) {
void startNewActivity(BuildContext context) {
final ActivitySettingsCubit activitySettingsCubit =
BlocProvider.of<ActivitySettingsCubit>(context);
final Activity newActivity = Activity.createNew(
// Settings
activitySettings: activitySettings,
globalSettings: globalSettings,
activitySettings: activitySettingsCubit.state.settings,
);
newActivity.dump();
......@@ -113,7 +109,7 @@ class ActivityCubit extends HydratedCubit<ActivityState> {
void increaseGivenTipsCount() {
state.currentActivity.givenTipsCount++;
state.currentActivity.buttonTipsCountdown =
DefaultGlobalSettings.defaultTipCountDownValueInSeconds;
ApplicationConfig.defaultTipCountDownValueInSeconds;
refresh();
const Duration interval = Duration(milliseconds: 500);
......@@ -131,6 +127,10 @@ class ActivityCubit extends HydratedCubit<ActivityState> {
);
}
bool canBeResumed() {
return state.currentActivity.canBeResumed;
}
void quitActivity() {
state.currentActivity.isRunning = false;
refresh();
......
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/default_activity_settings.dart';
import 'package:sudoku/models/settings/settings_activity.dart';
part 'settings_activity_state.dart';
class ActivitySettingsCubit extends HydratedCubit<ActivitySettingsState> {
ActivitySettingsCubit()
: super(ActivitySettingsState(settings: ActivitySettings.createDefault()));
void setValues({
String? level,
String? size,
}) {
emit(
ActivitySettingsState(
settings: ActivitySettings(
level: level ?? state.settings.level,
size: size ?? state.settings.size,
),
),
);
}
String getParameterValue(String code) {
switch (code) {
case DefaultActivitySettings.parameterCodeLevel:
return ActivitySettings.getLevelValueFromUnsafe(state.settings.level);
case DefaultActivitySettings.parameterCodeSize:
return ActivitySettings.getSizeValueFromUnsafe(state.settings.size);
}
return '';
}
void setParameterValue(String code, String value) {
final String level = (code == DefaultActivitySettings.parameterCodeLevel)
? value
: getParameterValue(DefaultActivitySettings.parameterCodeLevel);
final String size = (code == DefaultActivitySettings.parameterCodeSize)
? value
: getParameterValue(DefaultActivitySettings.parameterCodeSize);
setValues(
level: level,
size: size,
);
}
@override
ActivitySettingsState? fromJson(Map<String, dynamic> json) {
final String level = json[DefaultActivitySettings.parameterCodeLevel] as String;
final String size = json[DefaultActivitySettings.parameterCodeSize] as String;
return ActivitySettingsState(
settings: ActivitySettings(
level: level,
size: size,
),
);
}
@override
Map<String, dynamic>? toJson(ActivitySettingsState state) {
return <String, dynamic>{
DefaultActivitySettings.parameterCodeLevel: state.settings.level,
DefaultActivitySettings.parameterCodeSize: state.settings.size,
};
}
}
part of 'settings_activity_cubit.dart';
@immutable
class ActivitySettingsState extends Equatable {
const ActivitySettingsState({
required this.settings,
});
final ActivitySettings settings;
@override
List<dynamic> get props => <dynamic>[
settings,
];
}
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/default_global_settings.dart';
import 'package:sudoku/models/settings/settings_global.dart';
part 'settings_global_state.dart';
class GlobalSettingsCubit extends HydratedCubit<GlobalSettingsState> {
GlobalSettingsCubit() : super(GlobalSettingsState(settings: GlobalSettings.createDefault()));
void setValues({
String? skin,
}) {
emit(
GlobalSettingsState(
settings: GlobalSettings(
skin: skin ?? state.settings.skin,
),
),
);
}
String getParameterValue(String code) {
switch (code) {
case DefaultGlobalSettings.parameterCodeSkin:
return GlobalSettings.getSkinValueFromUnsafe(state.settings.skin);
}
return '';
}
void setParameterValue(String code, String value) {
final String skin = (code == DefaultGlobalSettings.parameterCodeSkin)
? value
: getParameterValue(DefaultGlobalSettings.parameterCodeSkin);
setValues(
skin: skin,
);
}
@override
GlobalSettingsState? fromJson(Map<String, dynamic> json) {
final String skin = json[DefaultGlobalSettings.parameterCodeSkin] as String;
return GlobalSettingsState(
settings: GlobalSettings(
skin: skin,
),
);
}
@override
Map<String, dynamic>? toJson(GlobalSettingsState state) {
return <String, dynamic>{
DefaultGlobalSettings.parameterCodeSkin: state.settings.skin,
};
}
}
part of 'settings_global_cubit.dart';
@immutable
class GlobalSettingsState extends Equatable {
const GlobalSettingsState({
required this.settings,
});
final GlobalSettings settings;
@override
List<dynamic> get props => <dynamic>[
settings,
];
}
......@@ -8,10 +8,7 @@ import 'package:sudoku/common/cubit/nav/nav_cubit_pages.dart';
import 'package:sudoku/common/cubit/nav/nav_cubit_screens.dart';
import 'package:sudoku/config/application_config.dart';
import 'package:sudoku/config/default_global_settings.dart';
import 'package:sudoku/cubit/activity/activity_cubit.dart';
import 'package:sudoku/cubit/settings/settings_activity_cubit.dart';
import 'package:sudoku/cubit/settings/settings_global_cubit.dart';
import 'package:sudoku/ui/skeleton.dart';
void main() async {
......@@ -62,17 +59,14 @@ class MyApp extends StatelessWidget {
BlocProvider<ActivityCubit>(
create: (context) => ActivityCubit(),
),
BlocProvider<GlobalSettingsCubit>(
create: (context) => GlobalSettingsCubit(),
),
BlocProvider<ActivitySettingsCubit>(
create: (context) => ActivitySettingsCubit(),
create: (context) => ActivitySettingsCubit(appConfig: ApplicationConfig.config),
),
],
child: BlocBuilder<ApplicationThemeModeCubit, ApplicationThemeModeState>(
builder: (BuildContext context, ApplicationThemeModeState state) {
return MaterialApp(
title: ApplicationConfig.appTitle,
title: ApplicationConfig.config.appTitle,
home: const SkeletonScreen(),
// Theme stuff
......@@ -111,7 +105,9 @@ class MyApp extends StatelessWidget {
skinImages.add(value.toString());
}
for (String skin in DefaultGlobalSettings.allowedSkinValues) {
for (String skin in ApplicationConfig.config
.getFromCode(ApplicationConfig.parameterCodeSkin)
.allowedValues) {
assets.add('assets/ui/skin_$skin.png');
for (String image in skinImages) {
assets.add('assets/skins/${skin}_$image.png');
......
......@@ -2,21 +2,18 @@ import 'dart:math';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/default_global_settings.dart';
import 'package:sudoku/config/application_config.dart';
import 'package:sudoku/cubit/activity/activity_cubit.dart';
import 'package:sudoku/data/game_data.dart';
import 'package:sudoku/models/activity/board.dart';
import 'package:sudoku/models/activity/cell.dart';
import 'package:sudoku/models/activity/cell_location.dart';
import 'package:sudoku/models/activity/types.dart';
import 'package:sudoku/models/settings/settings_activity.dart';
import 'package:sudoku/models/settings/settings_global.dart';
class Activity {
Activity({
// Settings
required this.activitySettings,
required this.globalSettings,
// State
this.isRunning = false,
......@@ -43,7 +40,6 @@ class Activity {
// Settings
final ActivitySettings activitySettings;
final GlobalSettings globalSettings;
// State
bool isRunning;
......@@ -70,8 +66,7 @@ class Activity {
factory Activity.createEmpty() {
return Activity(
// Settings
activitySettings: ActivitySettings.createDefault(),
globalSettings: GlobalSettings.createDefault(),
activitySettings: ActivitySettings.createDefault(appConfig: ApplicationConfig.config),
// Base data
board: Board.createEmpty(),
solvedBoard: Board.createEmpty(),
......@@ -86,20 +81,21 @@ class Activity {
factory Activity.createNew({
ActivitySettings? activitySettings,
GlobalSettings? globalSettings,
}) {
final ActivitySettings newActivitySettings =
activitySettings ?? ActivitySettings.createDefault();
final GlobalSettings newGlobalSettings = globalSettings ?? GlobalSettings.createDefault();
final ActivitySettings newActivitySettings = activitySettings ??
ActivitySettings.createDefault(appConfig: ApplicationConfig.config);
final int blockSizeHorizontal = int.parse(newActivitySettings.size.split('x')[0]);
final int blockSizeVertical = int.parse(newActivitySettings.size.split('x')[1]);
final int blockSizeHorizontal = int.parse(
newActivitySettings.get(ApplicationConfig.parameterCodeBoardSize).split('x')[0]);
final int blockSizeVertical = int.parse(
newActivitySettings.get(ApplicationConfig.parameterCodeBoardSize).split('x')[1]);
final int boardSize = blockSizeHorizontal * blockSizeVertical;
const int maxCellValue = 16;
final List<int> shuffledCellValues = List<int>.generate(maxCellValue, (i) => i + 1);
if (DefaultGlobalSettings.shufflableSkins.contains(newGlobalSettings.skin)) {
if (ApplicationConfig.shufflableSkins
.contains(newActivitySettings.get(ApplicationConfig.parameterCodeSkin))) {
shuffledCellValues.shuffle();
printlog('Shuffled tiles values: $shuffledCellValues');
}
......@@ -115,7 +111,9 @@ class Activity {
}
final List<String> templates =
GameData.templates[newActivitySettings.size]?[newActivitySettings.level] ?? [];
GameData.templates[newActivitySettings.get(ApplicationConfig.parameterCodeBoardSize)]
?[newActivitySettings.get(ApplicationConfig.parameterCodeDifficultyLevel)] ??
[];
final String template = templates.elementAt(Random().nextInt(templates.length)).toString();
if (template.length != pow(blockSizeHorizontal * blockSizeVertical, 2)) {
......@@ -142,7 +140,6 @@ class Activity {
return Activity(
// Settings
activitySettings: newActivitySettings,
globalSettings: newGlobalSettings,
// State
isRunning: true,
boardAnimated: notAnimatedBoard,
......@@ -371,7 +368,6 @@ class Activity {
printlog('$Activity:');
printlog(' Settings');
activitySettings.dump();
globalSettings.dump();
printlog(' State');
printlog(' isRunning: $isRunning');
printlog(' isStarted: $isStarted');
......@@ -422,7 +418,6 @@ class Activity {
return <String, dynamic>{
// Settings
'activitySettings': activitySettings.toJson(),
'globalSettings': globalSettings.toJson(),
// State
'isRunning': isRunning,
'isStarted': isStarted,
......
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/default_activity_settings.dart';
class ActivitySettings {
final String level;
final String size;
ActivitySettings({
required this.level,
required this.size,
});
static String getLevelValueFromUnsafe(String level) {
if (DefaultActivitySettings.allowedLevelValues.contains(level)) {
return level;
}
return DefaultActivitySettings.defaultLevelValue;
}
static String getSizeValueFromUnsafe(String size) {
if (DefaultActivitySettings.allowedSizeValues.contains(size)) {
return size;
}
return DefaultActivitySettings.defaultSizeValue;
}
factory ActivitySettings.createDefault() {
return ActivitySettings(
level: DefaultActivitySettings.defaultLevelValue,
size: DefaultActivitySettings.defaultSizeValue,
);
}
void dump() {
printlog('$ActivitySettings:');
printlog(' ${DefaultActivitySettings.parameterCodeLevel}: $level');
printlog(' ${DefaultActivitySettings.parameterCodeSize}: $size');
printlog('');
}
@override
String toString() {
return '$ActivitySettings(${toJson()})';
}
Map<String, dynamic>? toJson() {
return <String, dynamic>{
DefaultActivitySettings.parameterCodeLevel: level,
DefaultActivitySettings.parameterCodeSize: size,
};
}
}
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/default_global_settings.dart';
class GlobalSettings {
String skin;
GlobalSettings({
required this.skin,
});
static String getSkinValueFromUnsafe(String skin) {
if (DefaultGlobalSettings.allowedSkinValues.contains(skin)) {
return skin;
}
return DefaultGlobalSettings.defaultSkinValue;
}
factory GlobalSettings.createDefault() {
return GlobalSettings(
skin: DefaultGlobalSettings.defaultSkinValue,
);
}
void dump() {
printlog('$GlobalSettings: ');
printlog(' ${DefaultGlobalSettings.parameterCodeSkin}: $skin');
printlog('');
}
@override
String toString() {
return '$GlobalSettings(${toJson()})';
}
Map<String, dynamic>? toJson() {
return <String, dynamic>{
DefaultGlobalSettings.parameterCodeSkin: skin,
};
}
}
File moved
......@@ -3,160 +3,39 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/default_activity_settings.dart';
import 'package:sudoku/models/settings/settings_activity.dart';
import 'package:sudoku/models/settings/settings_global.dart';
import 'package:sudoku/config/application_config.dart';
class ParameterPainter extends CustomPainter {
const ParameterPainter({
required this.code,
class ParameterPainterBoardSize extends CustomPainter {
const ParameterPainterBoardSize({
required this.context,
required this.value,
required this.activitySettings,
required this.globalSettings,
});
final String code;
final BuildContext context;
final String value;
final ActivitySettings activitySettings;
final GlobalSettings globalSettings;
@override
void paint(Canvas canvas, Size size) {
// force square
final double canvasSize = min(size.width, size.height);
// content
switch (code) {
case DefaultActivitySettings.parameterCodeLevel:
paintLevelParameterItem(canvas, canvasSize);
break;
case DefaultActivitySettings.parameterCodeSize:
paintSizeParameterItem(canvas, canvasSize);
break;
default:
printlog('Unknown parameter: $code/$value');
paintUnknownParameterItem(canvas, canvasSize);
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
// "unknown" parameter -> simple block with text
void paintUnknownParameterItem(
final Canvas canvas,
final double size,
) {
final paint = Paint();
paint.strokeJoin = StrokeJoin.round;
paint.strokeWidth = 3;
final textSpan = TextSpan(
text: '$code\n$value',
style: const TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.bold,
),
);
final textPainter = TextPainter(
text: textSpan,
textDirection: TextDirection.ltr,
textAlign: TextAlign.center,
);
textPainter.layout();
textPainter.paint(
canvas,
Offset(
(size - textPainter.width) * 0.5,
(size - textPainter.height) * 0.5,
),
);
}
void paintLevelParameterItem(
final Canvas canvas,
final double size,
) {
final List<dynamic> stars = [];
switch (value) {
case DefaultActivitySettings.levelValueEasy:
stars.add([0.5, 0.5]);
break;
case DefaultActivitySettings.levelValueMedium:
stars.add([0.3, 0.5]);
stars.add([0.7, 0.5]);
break;
case DefaultActivitySettings.levelValueHard:
stars.add([0.3, 0.3]);
stars.add([0.7, 0.3]);
stars.add([0.5, 0.7]);
break;
case DefaultActivitySettings.levelValueNightmare:
stars.add([0.3, 0.3]);
stars.add([0.7, 0.3]);
stars.add([0.3, 0.7]);
stars.add([0.7, 0.7]);
break;
default:
printlog('Wrong value for level parameter value: $value');
}
final paint = Paint();
paint.strokeJoin = StrokeJoin.round;
paint.strokeWidth = 3 / 100 * size;
// Stars
final textSpan = TextSpan(
text: '⭐',
style: TextStyle(
color: Colors.black,
fontSize: size / 3,
fontWeight: FontWeight.bold,
),
);
final textPainter = TextPainter(
text: textSpan,
textDirection: TextDirection.ltr,
textAlign: TextAlign.center,
);
textPainter.layout();
for (var center in stars) {
textPainter.paint(
canvas,
Offset(
size * center[0] - textPainter.width * 0.5,
size * center[1] - textPainter.height * 0.5,
),
);
}
}
void paintSizeParameterItem(
final Canvas canvas,
final double size,
) {
int gridWidth = 1;
int gridHeight = 1;
switch (value) {
case DefaultActivitySettings.sizeValueTiny:
case ApplicationConfig.boardSizeValueTiny:
gridWidth = 2;
gridHeight = 2;
break;
case DefaultActivitySettings.sizeValueSmall:
case ApplicationConfig.boardSizeValueSmall:
gridWidth = 3;
gridHeight = 2;
break;
case DefaultActivitySettings.sizeValueStandard:
case ApplicationConfig.boardSizeValueStandard:
gridWidth = 3;
gridHeight = 3;
break;
case DefaultActivitySettings.sizeValueLarge:
case ApplicationConfig.boardSizeValueLarge:
gridWidth = 4;
gridHeight = 4;
break;
......@@ -171,9 +50,9 @@ class ParameterPainter extends CustomPainter {
// Mini grid
final borderColor = Colors.grey.shade800;
final double cellSize = size / 6;
final double originX = (size - gridWidth * cellSize) / 2;
final double originY = (size - gridHeight * cellSize) / 2;
final double cellSize = canvasSize / 5;
final double originX = (canvasSize - gridWidth * cellSize) / 2;
final double originY = (canvasSize - gridHeight * cellSize) / 2;
for (int row = 0; row < gridHeight; row++) {
for (int col = 0; col < gridWidth; col++) {
......@@ -190,4 +69,9 @@ class ParameterPainter extends CustomPainter {
}
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
import 'package:sudoku/config/application_config.dart';
class ParameterPainterDifficultyLevel extends CustomPainter {
const ParameterPainterDifficultyLevel({
required this.context,
required this.value,
});
final BuildContext context;
final String value;
@override
void paint(Canvas canvas, Size size) {
// force square
final double canvasSize = min(size.width, size.height);
final List<dynamic> stars = [];
switch (value) {
case ApplicationConfig.difficultyLevelValueEasy:
stars.add([0.5, 0.5]);
break;
case ApplicationConfig.difficultyLevelValueMedium:
stars.add([0.3, 0.5]);
stars.add([0.7, 0.5]);
break;
case ApplicationConfig.difficultyLevelValueHard:
stars.add([0.3, 0.3]);
stars.add([0.7, 0.3]);
stars.add([0.5, 0.7]);
break;
case ApplicationConfig.difficultyLevelValueNightmare:
stars.add([0.3, 0.3]);
stars.add([0.7, 0.3]);
stars.add([0.3, 0.7]);
stars.add([0.7, 0.7]);
break;
default:
printlog('Wrong value for level parameter value: $value');
}
final paint = Paint();
paint.strokeJoin = StrokeJoin.round;
paint.strokeWidth = 3 / 100 * canvasSize;
// Stars
final textSpan = TextSpan(
text: '⭐',
style: TextStyle(
color: Colors.black,
fontSize: canvasSize / 3,
fontWeight: FontWeight.bold,
),
);
final textPainter = TextPainter(
text: textSpan,
textDirection: TextDirection.ltr,
textAlign: TextAlign.center,
);
textPainter.layout();
for (var center in stars) {
textPainter.paint(
canvas,
Offset(
canvasSize * center[0] - textPainter.width * 0.5,
canvasSize * center[1] - textPainter.height * 0.5,
),
);
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment