Skip to content

Commit

Permalink
Fix merging and layer rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 24, 2024
1 parent 7e664f2 commit 6250fbf
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 48 deletions.
20 changes: 17 additions & 3 deletions app/lib/bloc/document_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -666,21 +666,35 @@ class DocumentBloc extends ReplayBloc<DocumentEvent, DocumentState> {
if (current is! DocumentLoadSuccess) return;
if (!(current.embedding?.editable ?? true)) return;
if (event.layers.length < 2) return;
final layers = current.page.layers;
final mainLayer = event.layers.first;
final mergedLayers = event.layers.skip(1).toList();
final mainLayerIndex =
layers.indexWhere((element) => element.id == mainLayer);
final mergedLayers = event.layers.skip(1).toList()
..sort((a, b) => layers
.indexWhere((e) => e.id == a)
.compareTo(layers.indexWhere((e) => e.id == b)));
final belowLayers = mergedLayers.where((element) =>
layers.indexWhere((e) => e.id == element) < mainLayerIndex);
final aboveLayers = mergedLayers.where((element) =>
layers.indexWhere((e) => e.id == element) > mainLayerIndex);
var layer = current.page.getLayer(mainLayer);
layer = layer.copyWith(content: [
...belowLayers.expand((e) => current.page.getLayer(e).content),
...layer.content,
...mergedLayers.expand((e) => current.page.getLayer(e).content)
...aboveLayers.expand((e) => current.page.getLayer(e).content),
]);
final newLayers = current.page.layers
final newLayers = layers
.where((e) => !mergedLayers.contains(e.id))
.map((e) => e.id == mainLayer ? layer : e)
.toList();
return _saveState(
emit,
state: current.copyWith(
page: current.page.copyWith(layers: newLayers),
currentLayer: mergedLayers.contains(current.currentLayer)
? mainLayer
: current.currentLayer,
),
);
});
Expand Down
5 changes: 4 additions & 1 deletion app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,10 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
.toList(),
visibleElements: visibleElements,
)
: cameraViewport.withoutLayers(),
: cameraViewport,
renderBackground: false,
renderBaked: !reset,
renderBakedLayers: false,
).paint(canvas, size);

var picture = recorder.endRecording();
Expand All @@ -586,8 +587,10 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
if (resetAllLayers) {
final belowLayerRecorder = ui.PictureRecorder();
final belowLayerCanvas = ui.Canvas(belowLayerRecorder);
belowLayerCanvas.scale(ratio);
final aboveLayerRecorder = ui.PictureRecorder();
final aboveLayerCanvas = ui.Canvas(aboveLayerRecorder);
aboveLayerCanvas.scale(ratio);
final belowLayers = [], aboveLayers = [];
bool above = false;
for (final layer in page.layers) {
Expand Down
7 changes: 4 additions & 3 deletions app/lib/view_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ViewPainter extends CustomPainter {
final DocumentPage page;
final DocumentInfo info;
final Area? currentArea;
final bool renderBackground, renderBaked;
final bool renderBackground, renderBaked, renderBakedLayers;
final CameraViewport cameraViewport;
final CameraTransform transform;
final ColorScheme? colorScheme;
Expand All @@ -108,6 +108,7 @@ class ViewPainter extends CustomPainter {
this.states = const {},
this.renderBackground = true,
this.renderBaked = true,
this.renderBakedLayers = true,
required this.cameraViewport,
this.colorScheme,
this.transform = const CameraTransform(),
Expand Down Expand Up @@ -143,7 +144,7 @@ class ViewPainter extends CustomPainter {
final bakedSize = cameraViewport.toSize();
final pos = transform.globalToLocal(cameraViewport.toOffset());
final bakedDst = pos & bakedSize * (1 + bakedSizeDiff);
if (renderBaked && belowLayerImage != null) {
if (renderBakedLayers && belowLayerImage != null) {
canvas.drawImageRect(
belowLayerImage,
Offset.zero &
Expand Down Expand Up @@ -214,7 +215,7 @@ class ViewPainter extends CustomPainter {
canvas.translate(transform.position.dx, transform.position.dy);
canvas.scale(1 / transform.size, 1 / transform.size);
final aboveLayerImage = cameraViewport.aboveLayerImage;
if (renderBaked && aboveLayerImage != null) {
if (renderBakedLayers && aboveLayerImage != null) {
canvas.drawImageRect(
aboveLayerImage,
Offset.zero &
Expand Down
8 changes: 4 additions & 4 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,10 @@ packages:
dependency: transitive
description:
name: file_selector_android
sha256: "00aafa9ae05a8663d0b4f17abd2a02316911ca0f46f9b9dacb9578b324d99590"
sha256: ec439df07c4999faad319ce8ad9e971795c2f1d7132ad5a793b9370a863c6128
url: "https://pub.dev"
source: hosted
version: "0.5.1+9"
version: "0.5.1+10"
file_selector_ios:
dependency: transitive
description:
Expand Down Expand Up @@ -1450,10 +1450,10 @@ packages:
dependency: transitive
description:
name: url_launcher_android
sha256: "8fc3bae0b68c02c47c5c86fa8bfa74471d42687b0eded01b78de87872db745e2"
sha256: "0dea215895a4d254401730ca0ba8204b29109a34a99fb06ae559a2b60988d2de"
url: "https://pub.dev"
source: hosted
version: "6.3.12"
version: "6.3.13"
url_launcher_ios:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
},
"packageManager": "[email protected]",
"devDependencies": {
"sass": "^1.80.3"
"sass": "^1.80.4"
}
}
78 changes: 42 additions & 36 deletions docs/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions metadata/en-US/changelogs/120.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Add layer merging
* Fix layer rendering

Read more here: https://linwood.dev/butterfly/2.3.0-beta.0

0 comments on commit 6250fbf

Please sign in to comment.