Skip to content

Commit

Permalink
Improve zooming to be saved in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 22, 2024
1 parent 960ac5c commit 8f46f2c
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 174 deletions.
16 changes: 16 additions & 0 deletions app/lib/bloc/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class QuokkaSettings with QuokkaSettingsMappable implements LeapSettings {
final bool showConnectYour, showConnectNetwork;
final GameProperty gameProperty;
final List<ListGameServer> servers;
final double zoom;
final bool highContrast;

const QuokkaSettings({
Expand All @@ -36,6 +37,7 @@ class QuokkaSettings with QuokkaSettingsMappable implements LeapSettings {
this.gameProperty = const GameProperty(),
this.servers = const [],
this.highContrast = false,
this.zoom = 1,
});

Locale? get locale {
Expand Down Expand Up @@ -66,6 +68,7 @@ class QuokkaSettings with QuokkaSettingsMappable implements LeapSettings {
.toList() ??
[],
highContrast: prefs.getBool('highContrast') ?? false,
zoom: prefs.getDouble('zoom') ?? 1,
);

Future<void> save() async {
Expand All @@ -88,6 +91,7 @@ class QuokkaSettings with QuokkaSettingsMappable implements LeapSettings {
await prefs.setStringList(
'servers', servers.map((e) => e.toJson()).toList());
await prefs.setBool('highContrast', highContrast);
await prefs.setDouble('zoom', zoom);
}
}

Expand Down Expand Up @@ -179,4 +183,16 @@ class SettingsCubit extends Cubit<QuokkaSettings>
emit(state.copyWith(highContrast: value));
return save();
}

Future<void> zoomIn([double value = 0.1]) {
emit(state.copyWith(zoom: (state.zoom + value).clamp(0.4, 2)));
return save();
}

Future<void> zoomOut([double value = 0.1]) => zoomIn(-value);

Future<void> resetZoom([double value = 1]) {
emit(state.copyWith(zoom: value.clamp(0.4, 2)));
return save();
}
}
19 changes: 14 additions & 5 deletions app/lib/bloc/settings.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class QuokkaSettingsMapper extends ClassMapperBase<QuokkaSettings> {
static bool _$highContrast(QuokkaSettings v) => v.highContrast;
static const Field<QuokkaSettings, bool> _f$highContrast =
Field('highContrast', _$highContrast, opt: true, def: false);
static double _$zoom(QuokkaSettings v) => v.zoom;
static const Field<QuokkaSettings, double> _f$zoom =
Field('zoom', _$zoom, opt: true, def: 1);

@override
final MappableFields<QuokkaSettings> fields = const {
Expand All @@ -70,6 +73,7 @@ class QuokkaSettingsMapper extends ClassMapperBase<QuokkaSettings> {
#gameProperty: _f$gameProperty,
#servers: _f$servers,
#highContrast: _f$highContrast,
#zoom: _f$zoom,
};

static QuokkaSettings _instantiate(DecodingData data) {
Expand All @@ -84,7 +88,8 @@ class QuokkaSettingsMapper extends ClassMapperBase<QuokkaSettings> {
lastVersion: data.dec(_f$lastVersion),
gameProperty: data.dec(_f$gameProperty),
servers: data.dec(_f$servers),
highContrast: data.dec(_f$highContrast));
highContrast: data.dec(_f$highContrast),
zoom: data.dec(_f$zoom));
}

@override
Expand Down Expand Up @@ -154,7 +159,8 @@ abstract class QuokkaSettingsCopyWith<$R, $In extends QuokkaSettings, $Out>
String? lastVersion,
GameProperty? gameProperty,
List<ListGameServer>? servers,
bool? highContrast});
bool? highContrast,
double? zoom});
QuokkaSettingsCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
Expand Down Expand Up @@ -187,7 +193,8 @@ class _QuokkaSettingsCopyWithImpl<$R, $Out>
Object? lastVersion = $none,
GameProperty? gameProperty,
List<ListGameServer>? servers,
bool? highContrast}) =>
bool? highContrast,
double? zoom}) =>
$apply(FieldCopyWithData({
if (localeTag != null) #localeTag: localeTag,
if (theme != null) #theme: theme,
Expand All @@ -199,7 +206,8 @@ class _QuokkaSettingsCopyWithImpl<$R, $Out>
if (lastVersion != $none) #lastVersion: lastVersion,
if (gameProperty != null) #gameProperty: gameProperty,
if (servers != null) #servers: servers,
if (highContrast != null) #highContrast: highContrast
if (highContrast != null) #highContrast: highContrast,
if (zoom != null) #zoom: zoom
}));
@override
QuokkaSettings $make(CopyWithData data) => QuokkaSettings(
Expand All @@ -214,7 +222,8 @@ class _QuokkaSettingsCopyWithImpl<$R, $Out>
lastVersion: data.get(#lastVersion, or: $value.lastVersion),
gameProperty: data.get(#gameProperty, or: $value.gameProperty),
servers: data.get(#servers, or: $value.servers),
highContrast: data.get(#highContrast, or: $value.highContrast));
highContrast: data.get(#highContrast, or: $value.highContrast),
zoom: data.get(#zoom, or: $value.zoom));

@override
QuokkaSettingsCopyWith<$R2, QuokkaSettings, $Out2> $chain<$R2, $Out2>(
Expand Down
9 changes: 0 additions & 9 deletions app/lib/bloc/world/bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ class WorldBloc extends Bloc<PlayableWorldEvent, ClientWorldState> {
on<DrawerViewChanged>((event, emit) {
emit(state.copyWith(drawerView: event.view));
});
on<ZoomChanged>((event, emit) {
var zoom = event.zoom;
if (zoom != null) {
zoom += state.zoom;
zoom = zoom.clamp(0.5, 2.0);
}
zoom ??= 1;
emit(state.copyWith(zoom: zoom));
});
}

Future<void> save() async {
Expand Down
8 changes: 0 additions & 8 deletions app/lib/bloc/world/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,3 @@ final class DrawerViewChanged extends LocalWorldEvent

DrawerViewChanged(this.view);
}

@MappableClass()
final class ZoomChanged extends LocalWorldEvent with ZoomChangedMappable {
final double? zoom;

ZoomChanged(this.zoom);
ZoomChanged.reset() : zoom = null;
}
112 changes: 0 additions & 112 deletions app/lib/bloc/world/local.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -727,115 +727,3 @@ class _DrawerViewChangedCopyWithImpl<$R, $Out>
Then<$Out2, $R2> t) =>
_DrawerViewChangedCopyWithImpl($value, $cast, t);
}

class ZoomChangedMapper extends SubClassMapperBase<ZoomChanged> {
ZoomChangedMapper._();

static ZoomChangedMapper? _instance;
static ZoomChangedMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = ZoomChangedMapper._());
LocalWorldEventMapper.ensureInitialized().addSubMapper(_instance!);
}
return _instance!;
}

@override
final String id = 'ZoomChanged';

static double? _$zoom(ZoomChanged v) => v.zoom;
static const Field<ZoomChanged, double> _f$zoom = Field('zoom', _$zoom);

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

@override
final String discriminatorKey = 'type';
@override
final dynamic discriminatorValue = 'ZoomChanged';
@override
late final ClassMapperBase superMapper =
LocalWorldEventMapper.ensureInitialized();

static ZoomChanged _instantiate(DecodingData data) {
return ZoomChanged(data.dec(_f$zoom));
}

@override
final Function instantiate = _instantiate;

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

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

mixin ZoomChangedMappable {
String toJson() {
return ZoomChangedMapper.ensureInitialized()
.encodeJson<ZoomChanged>(this as ZoomChanged);
}

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

ZoomChangedCopyWith<ZoomChanged, ZoomChanged, ZoomChanged> get copyWith =>
_ZoomChangedCopyWithImpl(this as ZoomChanged, $identity, $identity);
@override
String toString() {
return ZoomChangedMapper.ensureInitialized()
.stringifyValue(this as ZoomChanged);
}

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

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

extension ZoomChangedValueCopy<$R, $Out>
on ObjectCopyWith<$R, ZoomChanged, $Out> {
ZoomChangedCopyWith<$R, ZoomChanged, $Out> get $asZoomChanged =>
$base.as((v, t, t2) => _ZoomChangedCopyWithImpl(v, t, t2));
}

abstract class ZoomChangedCopyWith<$R, $In extends ZoomChanged, $Out>
implements LocalWorldEventCopyWith<$R, $In, $Out> {
@override
$R call({double? zoom});
ZoomChangedCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}

class _ZoomChangedCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, ZoomChanged, $Out>
implements ZoomChangedCopyWith<$R, ZoomChanged, $Out> {
_ZoomChangedCopyWithImpl(super.value, super.then, super.then2);

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

@override
ZoomChangedCopyWith<$R2, ZoomChanged, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_ZoomChangedCopyWithImpl($value, $cast, t);
}
2 changes: 0 additions & 2 deletions app/lib/bloc/world/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ final class ClientWorldState with ClientWorldStateMappable {
final ItemLocation? selectedDeck;
final bool showHand, switchCellOnMove;
final DrawerView drawerView;
final double zoom;

const ClientWorldState({
required this.multiplayer,
Expand All @@ -42,7 +41,6 @@ final class ClientWorldState with ClientWorldStateMappable {
this.showHand = false,
this.switchCellOnMove = false,
this.drawerView = DrawerView.chat,
this.zoom = 1.0,
});

QuokkaFileSystem get fileSystem => assetManager.fileSystem;
Expand Down
19 changes: 5 additions & 14 deletions app/lib/bloc/world/state.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ class ClientWorldStateMapper extends ClassMapperBase<ClientWorldState> {
static DrawerView _$drawerView(ClientWorldState v) => v.drawerView;
static const Field<ClientWorldState, DrawerView> _f$drawerView =
Field('drawerView', _$drawerView, opt: true, def: DrawerView.chat);
static double _$zoom(ClientWorldState v) => v.zoom;
static const Field<ClientWorldState, double> _f$zoom =
Field('zoom', _$zoom, opt: true, def: 1.0);

@override
final MappableFields<ClientWorldState> fields = const {
Expand All @@ -158,7 +155,6 @@ class ClientWorldStateMapper extends ClassMapperBase<ClientWorldState> {
#showHand: _f$showHand,
#switchCellOnMove: _f$switchCellOnMove,
#drawerView: _f$drawerView,
#zoom: _f$zoom,
};

static ClientWorldState _instantiate(DecodingData data) {
Expand All @@ -171,8 +167,7 @@ class ClientWorldStateMapper extends ClassMapperBase<ClientWorldState> {
selectedDeck: data.dec(_f$selectedDeck),
showHand: data.dec(_f$showHand),
switchCellOnMove: data.dec(_f$switchCellOnMove),
drawerView: data.dec(_f$drawerView),
zoom: data.dec(_f$zoom));
drawerView: data.dec(_f$drawerView));
}

@override
Expand Down Expand Up @@ -242,8 +237,7 @@ abstract class ClientWorldStateCopyWith<$R, $In extends ClientWorldState, $Out>
ItemLocation? selectedDeck,
bool? showHand,
bool? switchCellOnMove,
DrawerView? drawerView,
double? zoom});
DrawerView? drawerView});
ClientWorldStateCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
Expand Down Expand Up @@ -276,8 +270,7 @@ class _ClientWorldStateCopyWithImpl<$R, $Out>
Object? selectedDeck = $none,
bool? showHand,
bool? switchCellOnMove,
DrawerView? drawerView,
double? zoom}) =>
DrawerView? drawerView}) =>
$apply(FieldCopyWithData({
if (multiplayer != null) #multiplayer: multiplayer,
if (colorScheme != null) #colorScheme: colorScheme,
Expand All @@ -287,8 +280,7 @@ class _ClientWorldStateCopyWithImpl<$R, $Out>
if (selectedDeck != $none) #selectedDeck: selectedDeck,
if (showHand != null) #showHand: showHand,
if (switchCellOnMove != null) #switchCellOnMove: switchCellOnMove,
if (drawerView != null) #drawerView: drawerView,
if (zoom != null) #zoom: zoom
if (drawerView != null) #drawerView: drawerView
}));
@override
ClientWorldState $make(CopyWithData data) => ClientWorldState(
Expand All @@ -301,8 +293,7 @@ class _ClientWorldStateCopyWithImpl<$R, $Out>
showHand: data.get(#showHand, or: $value.showHand),
switchCellOnMove:
data.get(#switchCellOnMove, or: $value.switchCellOnMove),
drawerView: data.get(#drawerView, or: $value.drawerView),
zoom: data.get(#zoom, or: $value.zoom));
drawerView: data.get(#drawerView, or: $value.drawerView));

@override
ClientWorldStateCopyWith<$R2, ClientWorldState, $Out2> $chain<$R2, $Out2>(
Expand Down
13 changes: 7 additions & 6 deletions app/lib/board/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flame/game.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:quokka/bloc/settings.dart';
import 'package:quokka/bloc/world/bloc.dart';
import 'package:quokka/bloc/world/local.dart';
import 'package:quokka/bloc/world/state.dart';
Expand All @@ -26,6 +27,7 @@ class BoardGame extends FlameGame
late final GameHand _hand;
late final BoardGrid grid;
final WorldBloc bloc;
final SettingsCubit settingsCubit;

bool _isShifting = false;
bool get isShifting => _isShifting;
Expand All @@ -34,14 +36,17 @@ class BoardGame extends FlameGame
required this.bloc,
required this.contextMenuController,
required this.onEscape,
required this.settingsCubit,
});

@override
FutureOr<void> onLoad() async {
add(ScreenHitbox());
final provider =
FlameBlocProvider provider =
FlameBlocProvider<WorldBloc, ClientWorldState>.value(value: bloc);
await add(provider);
provider = provider;
await add(FlameBlocProvider<SettingsCubit, QuokkaSettings>.value(
value: settingsCubit, children: [provider]));
provider.addAll([camera, world]);
selectionSprite = await Sprite.load('selection.png');
blankSprite = await Sprite.load('blank.png');
Expand All @@ -65,10 +70,6 @@ class BoardGame extends FlameGame
Localizations.localeOf(context).languageCode;
}

void clampZoom(double zoom) {
camera.viewfinder.zoom = zoom.clamp(0.3, 3.0);
}

static const zoomPerScrollUnit = 0.02;

@override
Expand Down
Loading

0 comments on commit 8f46f2c

Please sign in to comment.