importStateHistory method
Imports state history and performs necessary recalculations.
If ImportStateHistory.configs.recalculateSizeAndPosition
is true
, it recalculates the position and size of layers.
It adjusts the scale and offset of each layer based on the image size and the editor's dimensions.
If ImportStateHistory.configs.mergeMode
is ImportEditorMergeMode.replace, it replaces the current state history with the imported one.
Otherwise, it merges the imported state history with the current one based on the merge mode.
After importing, it updates the UI by calling setState() and the optional onUpdateUI
callback.
Implementation
void importStateHistory(ImportStateHistory import) {
/// Recalculate position and size
if (import.configs.recalculateSizeAndPosition ||
import.version == ExportImportVersion.version_1_0_0) {
Size imgSize = import.imgSize / (_imageInfos?.pixelRatio ?? 1);
for (EditorStateHistory el in import.stateHistory) {
for (Layer layer in el.layers) {
if (import.configs.recalculateSizeAndPosition) {
// Calculate scaling factors for width and height
double scaleWidth =
sizesManager.decodedImageSize.width / imgSize.width;
double scaleHeight =
sizesManager.decodedImageSize.height / imgSize.height;
if (scaleWidth == 0 || scaleWidth.isInfinite) scaleWidth = 1;
if (scaleHeight == 0 || scaleHeight.isInfinite) scaleHeight = 1;
// Choose the middle value between scaleWidth and scaleHeight
double scale = (scaleWidth + scaleHeight) / 2;
// Adjust the scale
layer.scale *= scale;
// Adjust the offset
layer.offset = Offset(
layer.offset.dx * scaleWidth,
layer.offset.dy * scaleHeight,
);
}
if (import.version == ExportImportVersion.version_1_0_0) {
layer.offset -= Offset(
sizesManager.bodySize.width / 2 -
sizesManager.imageScreenGaps.left,
sizesManager.bodySize.height / 2 -
sizesManager.imageScreenGaps.top,
);
}
}
}
}
if (import.configs.mergeMode == ImportEditorMergeMode.replace) {
stateManager.screenshots = [];
stateManager.position =
import.editorPosition + (import.stateHistory.isEmpty ? 0 : 1);
stateManager.stateHistory = [
EditorStateHistory(
transformConfigs: TransformConfigs.empty(),
blur: 0,
filters: [],
layers: [],
),
...import.stateHistory
];
for (var i = 0; i < import.stateHistory.length; i++) {
if (i < import.stateHistory.length - 1) {
_controllers.screenshot
.addEmptyScreenshot(screenshots: stateManager.screenshots);
} else {
_controllers.screenshot
.addEmptyScreenshot(screenshots: stateManager.screenshots);
}
}
} else {
for (var el in stateManager.screenshots) {
el.broken = true;
}
for (var el in import.stateHistory) {
if (import.configs.mergeMode == ImportEditorMergeMode.merge) {
el.layers.insertAll(0, stateHistory.last.layers);
el.filters.insertAll(0, stateHistory.last.filters);
}
}
for (var i = 0; i < import.stateHistory.length; i++) {
stateHistory.add(import.stateHistory[i]);
if (i < import.stateHistory.length - 1) {
_controllers.screenshot
.addEmptyScreenshot(screenshots: stateManager.screenshots);
} else {
_takeScreenshot();
}
}
stateManager.position = stateHistory.length - 1;
}
setState(() {});
decodeImage(stateManager.transformConfigs);
mainEditorCallbacks?.handleUpdateUI();
}