Skip to content

Commit

Permalink
Remove utilities renderer, use handler for manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 29, 2024
1 parent 417131d commit fd9476a
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 322 deletions.
10 changes: 2 additions & 8 deletions app/lib/bloc/document_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1079,13 +1079,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
final current = state;
if (current is! DocumentLoaded) return;
final cubit = current.currentIndexCubit;
final document = current.data;
final page = current.page;
final assetService = current.assetService;
cubit.setSaveState(saved: SaveState.saved);
final tool = UtilitiesRenderer(cubit.state.utilitiesState);
await tool.setup(document, assetService, page);
cubit.unbake(tool: tool);
cubit.loadElements(current);
cubit.init(this);
}
Expand Down Expand Up @@ -1158,7 +1152,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
if (state is! DocumentLoadSuccess) return {};
transform ??= state.currentIndexCubit.state.transformCubit.state;
final renderers = state.cameraViewport.visibleElements;
full ??= state.cameraViewport.utilities.element.fullSelection;
full ??= state.currentIndexCubit.state.utilities.fullSelection;
return compute(
_executeRayCast,
_RayCastParams(
Expand All @@ -1182,7 +1176,7 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
if (state is! DocumentLoadSuccess) return {};
final renderers = state.cameraViewport.visibleElements;
transform ??= state.currentIndexCubit.state.transformCubit.state;
full ??= state.cameraViewport.utilities.element.fullSelection;
full ??= state.currentIndexCubit.state.utilities.fullSelection;
return compute(
_executeRayCastPolygon,
_RayCastPolygonParams(
Expand Down
45 changes: 7 additions & 38 deletions app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CurrentIndex with _$CurrentIndex {
SettingsCubit settingsCubit,
TransformCubit transformCubit,
NetworkingService networkingService, {
@Default(UtilitiesState()) UtilitiesState utilities,
Handler<Tool>? temporaryHandler,
@Default([]) List<Renderer> foregrounds,
Selection? selection,
Expand Down Expand Up @@ -84,8 +85,6 @@ class CurrentIndex with _$CurrentIndex {

MouseCursor get currentCursor => temporaryCursor ?? cursor;

UtilitiesState get utilitiesState => cameraViewport.utilities.element;

Map<String, RendererState> get allRendererStates => {
...rendererStates,
...?temporaryRendererStates,
Expand Down Expand Up @@ -123,12 +122,6 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
}
}

Offset getGridPosition(
Offset position, DocumentPage page, DocumentInfo info) {
return state.cameraViewport.utilities
.getGridPosition(position, page, info, this);
}

Future<Handler?> changeTool(
DocumentBloc bloc, {
int? index,
Expand Down Expand Up @@ -529,7 +522,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
temporaryForegrounds: null,
temporaryCursor: null,
temporaryRendererStates: null,
cameraViewport: CameraViewport.unbaked(UtilitiesRenderer()),
cameraViewport: CameraViewport.unbaked(),
));
}

Expand Down Expand Up @@ -864,13 +857,10 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {

void unbake(
{List<Renderer<Background>>? backgrounds,
UtilitiesRenderer? tool,
List<Renderer<PadElement>>? unbakedElements}) {
emit(state.copyWith(
cameraViewport: state.cameraViewport.unbake(
unbakedElements: unbakedElements,
utilities: tool,
backgrounds: backgrounds)));
unbakedElements: unbakedElements, backgrounds: backgrounds)));
}

Future<void> loadElements(DocumentState docState) async {
Expand All @@ -893,17 +883,10 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
final backgrounds = page.backgrounds.map(Renderer.fromInstance).toList();
await Future.wait(backgrounds
.map((e) async => await e.setup(document, assetService, page)));
final utilities = UtilitiesRenderer(state.settingsCubit.state.utilities);
await utilities.setup(
docState.data,
docState.assetService,
docState.page,
);
emit(state.copyWith(
cameraViewport: state.cameraViewport.unbake(
unbakedElements: renderers,
backgrounds: backgrounds,
utilities: utilities,
)));
}

Expand Down Expand Up @@ -1048,24 +1031,10 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
Future<void> updateUtilities(
{UtilitiesState? utilities, ViewOption? view}) async {
var state = this.state;
final renderer = UtilitiesRenderer(
utilities ?? state.utilitiesState, view ?? state.viewOption);
if (utilities != null) {
var newSelection =
state.selection?.remove(state.cameraViewport.utilities.element);
if (newSelection == null && state.selection != null) {
newSelection = Selection.from(utilities);
} else if (newSelection != state.selection) {
newSelection = newSelection?.insert(renderer);
}
state = state.copyWith(selection: newSelection);
}
state = state.copyWith(
cameraViewport: state.cameraViewport.withUtilities(renderer),
utilities: utilities ?? state.utilities,
viewOption: view ?? state.viewOption,
);
if (view != null) {
state = state.copyWith(viewOption: view);
}
emit(state);
if (utilities != null) {
return state.settingsCubit.changeUtilities(utilities);
Expand All @@ -1075,7 +1044,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
void togglePin() => emit(state.copyWith(pinned: !state.pinned));

void move(Offset delta, [bool force = false]) {
final utilitiesState = state.utilitiesState;
final utilitiesState = state.utilities;
if (utilitiesState.lockHorizontal && !force) {
delta = Offset(0, delta.dy);
}
Expand All @@ -1089,7 +1058,7 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
}

void zoom(double delta, [Offset cursor = Offset.zero, bool force = false]) {
final utilitiesState = state.utilitiesState;
final utilitiesState = state.utilities;
if (utilitiesState.lockZoom && !force) {
delta = 1;
}
Expand Down
39 changes: 36 additions & 3 deletions app/lib/cubits/current_index.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mixin _$CurrentIndex {
SettingsCubit get settingsCubit => throw _privateConstructorUsedError;
TransformCubit get transformCubit => throw _privateConstructorUsedError;
NetworkingService get networkingService => throw _privateConstructorUsedError;
UtilitiesState get utilities => throw _privateConstructorUsedError;
Handler<Tool>? get temporaryHandler => throw _privateConstructorUsedError;
List<Renderer> get foregrounds => throw _privateConstructorUsedError;
Selection<dynamic>? get selection => throw _privateConstructorUsedError;
Expand Down Expand Up @@ -76,6 +77,7 @@ abstract class $CurrentIndexCopyWith<$Res> {
SettingsCubit settingsCubit,
TransformCubit transformCubit,
NetworkingService networkingService,
UtilitiesState utilities,
Handler<Tool>? temporaryHandler,
List<Renderer> foregrounds,
Selection<dynamic>? selection,
Expand Down Expand Up @@ -103,6 +105,7 @@ abstract class $CurrentIndexCopyWith<$Res> {
bool areaNavigatorExact,
bool areaNavigatorAsk});

$UtilitiesStateCopyWith<$Res> get utilities;
$ViewOptionCopyWith<$Res> get viewOption;
}

Expand All @@ -127,6 +130,7 @@ class _$CurrentIndexCopyWithImpl<$Res, $Val extends CurrentIndex>
Object? settingsCubit = null,
Object? transformCubit = null,
Object? networkingService = null,
Object? utilities = null,
Object? temporaryHandler = freezed,
Object? foregrounds = null,
Object? selection = freezed,
Expand Down Expand Up @@ -179,6 +183,10 @@ class _$CurrentIndexCopyWithImpl<$Res, $Val extends CurrentIndex>
? _value.networkingService
: networkingService // ignore: cast_nullable_to_non_nullable
as NetworkingService,
utilities: null == utilities
? _value.utilities
: utilities // ignore: cast_nullable_to_non_nullable
as UtilitiesState,
temporaryHandler: freezed == temporaryHandler
? _value.temporaryHandler
: temporaryHandler // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -286,6 +294,16 @@ class _$CurrentIndexCopyWithImpl<$Res, $Val extends CurrentIndex>
) as $Val);
}

/// Create a copy of CurrentIndex
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$UtilitiesStateCopyWith<$Res> get utilities {
return $UtilitiesStateCopyWith<$Res>(_value.utilities, (value) {
return _then(_value.copyWith(utilities: value) as $Val);
});
}

/// Create a copy of CurrentIndex
/// with the given fields replaced by the non-null parameter values.
@override
Expand All @@ -312,6 +330,7 @@ abstract class _$$CurrentIndexImplCopyWith<$Res>
SettingsCubit settingsCubit,
TransformCubit transformCubit,
NetworkingService networkingService,
UtilitiesState utilities,
Handler<Tool>? temporaryHandler,
List<Renderer> foregrounds,
Selection<dynamic>? selection,
Expand Down Expand Up @@ -339,6 +358,8 @@ abstract class _$$CurrentIndexImplCopyWith<$Res>
bool areaNavigatorExact,
bool areaNavigatorAsk});

@override
$UtilitiesStateCopyWith<$Res> get utilities;
@override
$ViewOptionCopyWith<$Res> get viewOption;
}
Expand All @@ -362,6 +383,7 @@ class __$$CurrentIndexImplCopyWithImpl<$Res>
Object? settingsCubit = null,
Object? transformCubit = null,
Object? networkingService = null,
Object? utilities = null,
Object? temporaryHandler = freezed,
Object? foregrounds = null,
Object? selection = freezed,
Expand Down Expand Up @@ -414,6 +436,10 @@ class __$$CurrentIndexImplCopyWithImpl<$Res>
? _value.networkingService
: networkingService // ignore: cast_nullable_to_non_nullable
as NetworkingService,
utilities: null == utilities
? _value.utilities
: utilities // ignore: cast_nullable_to_non_nullable
as UtilitiesState,
temporaryHandler: freezed == temporaryHandler
? _value.temporaryHandler
: temporaryHandler // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -527,7 +553,8 @@ class __$$CurrentIndexImplCopyWithImpl<$Res>
class _$CurrentIndexImpl extends _CurrentIndex {
const _$CurrentIndexImpl(this.index, this.handler, this.cameraViewport,
this.settingsCubit, this.transformCubit, this.networkingService,
{this.temporaryHandler,
{this.utilities = const UtilitiesState(),
this.temporaryHandler,
final List<Renderer> foregrounds = const [],
this.selection,
this.pinned = false,
Expand Down Expand Up @@ -576,6 +603,9 @@ class _$CurrentIndexImpl extends _CurrentIndex {
@override
final NetworkingService networkingService;
@override
@JsonKey()
final UtilitiesState utilities;
@override
final Handler<Tool>? temporaryHandler;
final List<Renderer> _foregrounds;
@override
Expand Down Expand Up @@ -704,7 +734,7 @@ class _$CurrentIndexImpl extends _CurrentIndex {

@override
String toString() {
return 'CurrentIndex(index: $index, handler: $handler, cameraViewport: $cameraViewport, settingsCubit: $settingsCubit, transformCubit: $transformCubit, networkingService: $networkingService, temporaryHandler: $temporaryHandler, foregrounds: $foregrounds, selection: $selection, pinned: $pinned, temporaryForegrounds: $temporaryForegrounds, toggleableHandlers: $toggleableHandlers, networkingForegrounds: $networkingForegrounds, toggleableForegrounds: $toggleableForegrounds, cursor: $cursor, temporaryCursor: $temporaryCursor, temporaryState: $temporaryState, lastPosition: $lastPosition, pointers: $pointers, buttons: $buttons, location: $location, embedding: $embedding, saved: $saved, toolbar: $toolbar, temporaryToolbar: $temporaryToolbar, rendererStates: $rendererStates, temporaryRendererStates: $temporaryRendererStates, viewOption: $viewOption, hideUi: $hideUi, areaNavigatorCreate: $areaNavigatorCreate, areaNavigatorExact: $areaNavigatorExact, areaNavigatorAsk: $areaNavigatorAsk)';
return 'CurrentIndex(index: $index, handler: $handler, cameraViewport: $cameraViewport, settingsCubit: $settingsCubit, transformCubit: $transformCubit, networkingService: $networkingService, utilities: $utilities, temporaryHandler: $temporaryHandler, foregrounds: $foregrounds, selection: $selection, pinned: $pinned, temporaryForegrounds: $temporaryForegrounds, toggleableHandlers: $toggleableHandlers, networkingForegrounds: $networkingForegrounds, toggleableForegrounds: $toggleableForegrounds, cursor: $cursor, temporaryCursor: $temporaryCursor, temporaryState: $temporaryState, lastPosition: $lastPosition, pointers: $pointers, buttons: $buttons, location: $location, embedding: $embedding, saved: $saved, toolbar: $toolbar, temporaryToolbar: $temporaryToolbar, rendererStates: $rendererStates, temporaryRendererStates: $temporaryRendererStates, viewOption: $viewOption, hideUi: $hideUi, areaNavigatorCreate: $areaNavigatorCreate, areaNavigatorExact: $areaNavigatorExact, areaNavigatorAsk: $areaNavigatorAsk)';
}

/// Create a copy of CurrentIndex
Expand All @@ -724,7 +754,8 @@ abstract class _CurrentIndex extends CurrentIndex {
final SettingsCubit settingsCubit,
final TransformCubit transformCubit,
final NetworkingService networkingService,
{final Handler<Tool>? temporaryHandler,
{final UtilitiesState utilities,
final Handler<Tool>? temporaryHandler,
final List<Renderer> foregrounds,
final Selection<dynamic>? selection,
final bool pinned,
Expand Down Expand Up @@ -765,6 +796,8 @@ abstract class _CurrentIndex extends CurrentIndex {
@override
NetworkingService get networkingService;
@override
UtilitiesState get utilities;
@override
Handler<Tool>? get temporaryHandler;
@override
List<Renderer> get foregrounds;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/handlers/eraser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class EraserHandler extends Handler<EraserTool> {
Future<void> _changeElement(Offset position, EventContext context) async {
final currentIndex = context.getCurrentIndex();
final transform = currentIndex.transformCubit.state;
final utilities = currentIndex.utilitiesState;
final utilities = currentIndex.utilities;
final globalPos = transform.localToGlobal(position);
final size = data.strokeWidth;
final shouldErase =
Expand Down
10 changes: 9 additions & 1 deletion app/lib/handlers/grid.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'handler.dart';

class GridHandler extends Handler<GridTool> {
class GridHandler extends Handler<GridTool> with PointerManipulationHandler {
GridHandler(super.data);

@override
Expand All @@ -13,6 +13,14 @@ class GridHandler extends Handler<GridTool> {
SelectState onSelected(BuildContext context, [bool wasAdded = true]) {
return SelectState.toggle;
}

@override
Offset getPointerPosition(Offset position, Size viewportSize) {
return Offset(
(position.dx / data.xSize).round() * data.xSize,
(position.dy / data.ySize).round() * data.ySize,
);
}
}

class GridRenderer extends Renderer<GridTool> {
Expand Down
16 changes: 16 additions & 0 deletions app/lib/handlers/handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,19 @@ abstract class PastingHandler<T> extends Handler<T> {

bool get currentlyPasting => _firstPos != null && _secondPos != null;
}

mixin PointerManipulationHandler<T> on Handler<T> {
Offset getPointerPosition(Offset position, Size viewportSize) {
return position;
}

static Offset calculatePointerPosition(
CurrentIndex index, Offset position, Size viewportSize) {
return index.toggleableHandlers.values
.whereType<PointerManipulationHandler>()
.map((e) => e.getPointerPosition(position, viewportSize))
.where((e) => e != position)
.firstOrNull ??
position;
}
}
4 changes: 2 additions & 2 deletions app/lib/handlers/laser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class LaserHandler extends Handler<LaserTool> with ColoredHandler {
final state = bloc.state as DocumentLoadSuccess;
final settings = context.read<SettingsCubit>().state;
final penOnlyInput = settings.penOnlyInput;
localPosition =
viewport.utilities.getPointerPosition(localPosition, currentIndexCubit);
localPosition = PointerManipulationHandler.calculatePointerPosition(
currentIndexCubit.state, localPosition, viewport.toSize());
if (penOnlyInput && kind != PointerDeviceKind.stylus) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/handlers/path_eraser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PathEraserHandler extends Handler<PathEraserTool> {
_currentPos = event.localPosition;
final currentIndex = context.getCurrentIndex();
final transform = currentIndex.transformCubit.state;
final utilities = currentIndex.utilitiesState;
final utilities = currentIndex.utilities;
final state = context.getState();
final globalPos = transform.localToGlobal(event.localPosition);
final size = data.strokeWidth;
Expand Down
4 changes: 2 additions & 2 deletions app/lib/handlers/pen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class PenHandler extends Handler<PenTool> with ColoredHandler {
final currentIndexCubit = context.read<CurrentIndexCubit>();
final viewport = currentIndexCubit.state.cameraViewport;
final transform = context.read<TransformCubit>().state;
localPos =
viewport.utilities.getPointerPosition(localPos, currentIndexCubit);
localPos = PointerManipulationHandler.calculatePointerPosition(
currentIndexCubit.state, localPos, viewport.toSize());
final globalPos = transform.localToGlobal(localPos);
if (!bloc.isInBounds(globalPos)) return;
final state = bloc.state as DocumentLoadSuccess;
Expand Down
Loading

0 comments on commit fd9476a

Please sign in to comment.