Skip to content

Commit

Permalink
Add colorscheme system to states
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Aug 9, 2024
1 parent 32c3c29 commit 35ef78d
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 26 deletions.
4 changes: 4 additions & 0 deletions app/lib/bloc/board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class BoardBloc extends Bloc<BoardEvent, BoardState> {
data: data ?? QuokkaData.empty(),
table: table ?? data?.getTable() ?? const GameTable(),
)) {
on<ColorSchemeChanged>((event, emit) {
emit(state.copyWith(colorScheme: event.colorScheme));
return save();
});
on<HandChanged>((event, emit) {
emit(state.copyWith(
showHand: event.show ?? (!state.showHand),
Expand Down
9 changes: 9 additions & 0 deletions app/lib/bloc/board_event.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:dart_mappable/dart_mappable.dart';
import 'package:flutter/material.dart';
import 'package:quokka/models/table.dart';
import 'package:quokka/models/vector.dart';

Expand All @@ -15,6 +16,14 @@ final class CellSwitched extends BoardEvent with CellSwitchedMappable {
CellSwitched(this.cell, {this.toggle = false});
}

@MappableClass()
final class ColorSchemeChanged extends BoardEvent
with ColorSchemeChangedMappable {
final ColorScheme? colorScheme;

ColorSchemeChanged(this.colorScheme);
}

@MappableClass()
final class HandChanged extends BoardEvent with HandChangedMappable {
final ItemLocation? deck;
Expand Down
111 changes: 111 additions & 0 deletions app/lib/bloc/board_event.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BoardEventMapper extends ClassMapperBase<BoardEvent> {
if (_instance == null) {
MapperContainer.globals.use(_instance = BoardEventMapper._());
CellSwitchedMapper.ensureInitialized();
ColorSchemeChangedMapper.ensureInitialized();
HandChangedMapper.ensureInitialized();
ObjectsSpawnedMapper.ensureInitialized();
ObjectsMovedMapper.ensureInitialized();
Expand Down Expand Up @@ -171,6 +172,116 @@ class _CellSwitchedCopyWithImpl<$R, $Out>
_CellSwitchedCopyWithImpl($value, $cast, t);
}

class ColorSchemeChangedMapper extends ClassMapperBase<ColorSchemeChanged> {
ColorSchemeChangedMapper._();

static ColorSchemeChangedMapper? _instance;
static ColorSchemeChangedMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = ColorSchemeChangedMapper._());
BoardEventMapper.ensureInitialized();
}
return _instance!;
}

@override
final String id = 'ColorSchemeChanged';

static ColorScheme? _$colorScheme(ColorSchemeChanged v) => v.colorScheme;
static const Field<ColorSchemeChanged, ColorScheme> _f$colorScheme =
Field('colorScheme', _$colorScheme);

@override
final MappableFields<ColorSchemeChanged> fields = const {
#colorScheme: _f$colorScheme,
};

static ColorSchemeChanged _instantiate(DecodingData data) {
return ColorSchemeChanged(data.dec(_f$colorScheme));
}

@override
final Function instantiate = _instantiate;

static ColorSchemeChanged fromMap(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<ColorSchemeChanged>(map);
}

static ColorSchemeChanged fromJson(String json) {
return ensureInitialized().decodeJson<ColorSchemeChanged>(json);
}
}

mixin ColorSchemeChangedMappable {
String toJson() {
return ColorSchemeChangedMapper.ensureInitialized()
.encodeJson<ColorSchemeChanged>(this as ColorSchemeChanged);
}

Map<String, dynamic> toMap() {
return ColorSchemeChangedMapper.ensureInitialized()
.encodeMap<ColorSchemeChanged>(this as ColorSchemeChanged);
}

ColorSchemeChangedCopyWith<ColorSchemeChanged, ColorSchemeChanged,
ColorSchemeChanged>
get copyWith => _ColorSchemeChangedCopyWithImpl(
this as ColorSchemeChanged, $identity, $identity);
@override
String toString() {
return ColorSchemeChangedMapper.ensureInitialized()
.stringifyValue(this as ColorSchemeChanged);
}

@override
bool operator ==(Object other) {
return ColorSchemeChangedMapper.ensureInitialized()
.equalsValue(this as ColorSchemeChanged, other);
}

@override
int get hashCode {
return ColorSchemeChangedMapper.ensureInitialized()
.hashValue(this as ColorSchemeChanged);
}
}

extension ColorSchemeChangedValueCopy<$R, $Out>
on ObjectCopyWith<$R, ColorSchemeChanged, $Out> {
ColorSchemeChangedCopyWith<$R, ColorSchemeChanged, $Out>
get $asColorSchemeChanged =>
$base.as((v, t, t2) => _ColorSchemeChangedCopyWithImpl(v, t, t2));
}

abstract class ColorSchemeChangedCopyWith<$R, $In extends ColorSchemeChanged,
$Out> implements BoardEventCopyWith<$R, $In, $Out> {
@override
$R call({ColorScheme? colorScheme});
ColorSchemeChangedCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}

class _ColorSchemeChangedCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, ColorSchemeChanged, $Out>
implements ColorSchemeChangedCopyWith<$R, ColorSchemeChanged, $Out> {
_ColorSchemeChangedCopyWithImpl(super.value, super.then, super.then2);

@override
late final ClassMapperBase<ColorSchemeChanged> $mapper =
ColorSchemeChangedMapper.ensureInitialized();
@override
$R call({Object? colorScheme = $none}) => $apply(
FieldCopyWithData({if (colorScheme != $none) #colorScheme: colorScheme}));
@override
ColorSchemeChanged $make(CopyWithData data) =>
ColorSchemeChanged(data.get(#colorScheme, or: $value.colorScheme));

@override
ColorSchemeChangedCopyWith<$R2, ColorSchemeChanged, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_ColorSchemeChangedCopyWithImpl($value, $cast, t);
}

class HandChangedMapper extends ClassMapperBase<HandChanged> {
HandChangedMapper._();

Expand Down
3 changes: 3 additions & 0 deletions app/lib/bloc/board_state.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:dart_mappable/dart_mappable.dart';
import 'package:flutter/material.dart';
import 'package:quokka/models/data.dart';
import 'package:quokka/models/table.dart';
import 'package:quokka/models/vector.dart';
Expand All @@ -8,6 +9,7 @@ part 'board_state.mapper.dart';

@MappableClass()
class BoardState with BoardStateMappable {
final ColorScheme? colorScheme;
final QuokkaFileSystem fileSystem;
final GameTable table;
final VectorDefinition? selectedCell;
Expand All @@ -19,6 +21,7 @@ class BoardState with BoardStateMappable {
const BoardState({
required this.fileSystem,
this.name,
this.colorScheme,
this.table = const GameTable(),
this.selectedCell,
this.selectedDeck,
Expand Down
9 changes: 9 additions & 0 deletions app/lib/bloc/board_state.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class BoardStateMapper extends ClassMapperBase<BoardState> {
static String? _$name(BoardState v) => v.name;
static const Field<BoardState, String> _f$name =
Field('name', _$name, opt: true);
static ColorScheme? _$colorScheme(BoardState v) => v.colorScheme;
static const Field<BoardState, ColorScheme> _f$colorScheme =
Field('colorScheme', _$colorScheme, opt: true);
static GameTable _$table(BoardState v) => v.table;
static const Field<BoardState, GameTable> _f$table =
Field('table', _$table, opt: true, def: const GameTable());
Expand All @@ -48,6 +51,7 @@ class BoardStateMapper extends ClassMapperBase<BoardState> {
final MappableFields<BoardState> fields = const {
#fileSystem: _f$fileSystem,
#name: _f$name,
#colorScheme: _f$colorScheme,
#table: _f$table,
#selectedCell: _f$selectedCell,
#selectedDeck: _f$selectedDeck,
Expand All @@ -59,6 +63,7 @@ class BoardStateMapper extends ClassMapperBase<BoardState> {
return BoardState(
fileSystem: data.dec(_f$fileSystem),
name: data.dec(_f$name),
colorScheme: data.dec(_f$colorScheme),
table: data.dec(_f$table),
selectedCell: data.dec(_f$selectedCell),
selectedDeck: data.dec(_f$selectedDeck),
Expand Down Expand Up @@ -124,6 +129,7 @@ abstract class BoardStateCopyWith<$R, $In extends BoardState, $Out>
$R call(
{QuokkaFileSystem? fileSystem,
String? name,
ColorScheme? colorScheme,
GameTable? table,
VectorDefinition? selectedCell,
ItemLocation? selectedDeck,
Expand Down Expand Up @@ -154,6 +160,7 @@ class _BoardStateCopyWithImpl<$R, $Out>
$R call(
{QuokkaFileSystem? fileSystem,
Object? name = $none,
Object? colorScheme = $none,
GameTable? table,
Object? selectedCell = $none,
Object? selectedDeck = $none,
Expand All @@ -162,6 +169,7 @@ class _BoardStateCopyWithImpl<$R, $Out>
$apply(FieldCopyWithData({
if (fileSystem != null) #fileSystem: fileSystem,
if (name != $none) #name: name,
if (colorScheme != $none) #colorScheme: colorScheme,
if (table != null) #table: table,
if (selectedCell != $none) #selectedCell: selectedCell,
if (selectedDeck != $none) #selectedDeck: selectedDeck,
Expand All @@ -172,6 +180,7 @@ class _BoardStateCopyWithImpl<$R, $Out>
BoardState $make(CopyWithData data) => BoardState(
fileSystem: data.get(#fileSystem, or: $value.fileSystem),
name: data.get(#name, or: $value.name),
colorScheme: data.get(#colorScheme, or: $value.colorScheme),
table: data.get(#table, or: $value.table),
selectedCell: data.get(#selectedCell, or: $value.selectedCell),
selectedDeck: data.get(#selectedDeck, or: $value.selectedDeck),
Expand Down
4 changes: 1 addition & 3 deletions app/lib/board/cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ class GameCell extends PositionComponent
final controller = EffectController(
duration: 0.1,
);
final context = game.buildContext;
final color =
context == null ? Colors.green : Theme.of(context).colorScheme.primary;
final color = state.colorScheme?.primary ?? Colors.green.withOpacity(0.5);
if (selected) {
_updateEffects([
OpacityEffect.to(1, controller),
Expand Down
7 changes: 6 additions & 1 deletion app/lib/board/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BoardGame extends FlameGame with ScrollDetector, KeyboardEvents {
}

@override
void onMount() {
void onAttach() {
_updateLocale();
}

Expand Down Expand Up @@ -82,6 +82,11 @@ class BoardGame extends FlameGame with ScrollDetector, KeyboardEvents {
if (!_currentCameraVelocity.isZero()) {
camera.moveBy(_currentCameraVelocity * dt * 60);
}
final nextColorScheme =
buildContext != null ? Theme.of(buildContext!).colorScheme : null;
if (nextColorScheme != bloc.state.colorScheme) {
bloc.add(ColorSchemeChanged(nextColorScheme));
}
}

@override
Expand Down
37 changes: 27 additions & 10 deletions app/lib/board/hand/item.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/material.dart' show Colors;
import 'package:flutter/painting.dart';
import 'package:quokka/bloc/board.dart';
import 'package:quokka/bloc/board_state.dart';
Expand All @@ -16,9 +17,10 @@ abstract class HandItem<T> extends PositionComponent
HasGameRef<BoardGame>,
DragCallbacks,
LongDragCallbacks,
FlameBlocReader<BoardBloc, BoardState> {
FlameBlocListenable<BoardBloc, BoardState> {
final T item;
late final SpriteComponent _sprite;
late final TextComponent<TextPaint> _label;
Vector2 _lastPos = Vector2.zero();

HandItem({required this.item}) : super(size: Vector2(100, 0));
Expand All @@ -36,15 +38,6 @@ abstract class HandItem<T> extends PositionComponent
@override
Future<void> onLoad() async {
super.onLoad();
add(TextComponent(
text: label,
size: Vector2(0, labelHeight),
position: Vector2(50, 0),
anchor: Anchor.topCenter,
textRenderer: TextPaint(
style: const TextStyle(fontSize: 14),
),
));
_sprite = SpriteComponent(
position: Vector2(0, labelHeight),
size: Vector2(100, 0),
Expand All @@ -53,6 +46,30 @@ abstract class HandItem<T> extends PositionComponent
add(_sprite);
}

@override
bool listenWhen(BoardState previousState, BoardState newState) =>
previousState.colorScheme != newState.colorScheme;

@override
void onInitialState(BoardState state) {
add(_label = TextComponent(
text: label,
size: Vector2(0, labelHeight),
position: Vector2(50, 0),
anchor: Anchor.topCenter,
textRenderer: _buildPaint(state)));
}

_buildPaint([BoardState? state]) => TextPaint(
style: TextStyle(
fontSize: 14, color: state?.colorScheme?.onSurface ?? Colors.white),
);

@override
void onNewState(BoardState state) {
_label.textRenderer = _buildPaint(state);
}

@override
void onParentResize(Vector2 maxSize) {
height = maxSize.y;
Expand Down
Loading

0 comments on commit 35ef78d

Please sign in to comment.