From 7f19fbb950865ade8749cbe423e9af4ef69cccf9 Mon Sep 17 00:00:00 2001 From: hm21 Date: Wed, 19 Jun 2024 22:59:46 +0200 Subject: [PATCH] feat(layer-scale): add ability to set minimum and maximum scale factor for layers This was requested in [#122](https://github.com/hm21/pro_image_editor/issues/122). --- CHANGELOG.md | 3 ++ .../editor_configs/emoji_editor_configs.dart | 8 +++++ .../editor_configs/paint_editor_configs.dart | 8 +++++ .../sticker_editor_configs.dart | 8 +++++ .../editor_configs/text_editor_configs.dart | 8 +++++ lib/modules/main_editor/main_editor.dart | 2 ++ .../utils/layer_interaction_manager.dart | 31 ++++++++++++++++++- pubspec.yaml | 2 +- 8 files changed, 68 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8466221..84a879d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## Version 4.0.6 +- **feat(layer-scale)**: Add ability to set minimum and maximum scale factor for layers. This was requsted in [#122](https://github.com/hm21/pro_image_editor/issues/122) + ## Version 4.0.5 - **fix(text-editor)**: Resolve misapplication of secondary color. This resolve issue [#105](https://github.com/hm21/pro_image_editor/discussions/105). - **fix(text-editor)**: Resolve issue where text styles (bold/italic/underline) are not saved in history. This resolves issue [#118](https://github.com/hm21/pro_image_editor/discussions/118). diff --git a/lib/models/editor_configs/emoji_editor_configs.dart b/lib/models/editor_configs/emoji_editor_configs.dart index 94c0e7d9..5851f6b2 100644 --- a/lib/models/editor_configs/emoji_editor_configs.dart +++ b/lib/models/editor_configs/emoji_editor_configs.dart @@ -33,6 +33,12 @@ class EmojiEditorConfigs { /// Custom emojis; if set, overrides default emojis provided by the library. final List emojiSet; + /// The minimum scale factor from the layer. + final double minScale; + + /// The maximum scale factor from the layer. + final double maxScale; + /// Creates an instance of EmojiEditorConfigs with optional settings. /// /// By default, the editor is enabled, and other properties are set to @@ -40,6 +46,8 @@ class EmojiEditorConfigs { const EmojiEditorConfigs({ this.enabled = true, this.initScale = 5.0, + this.minScale = double.negativeInfinity, + this.maxScale = double.infinity, this.checkPlatformCompatibility = true, this.emojiSet = defaultEmojiSet, }) : assert(initScale > 0, 'initScale must be positive'); diff --git a/lib/models/editor_configs/paint_editor_configs.dart b/lib/models/editor_configs/paint_editor_configs.dart index 41507af2..1d840e0d 100644 --- a/lib/models/editor_configs/paint_editor_configs.dart +++ b/lib/models/editor_configs/paint_editor_configs.dart @@ -81,6 +81,12 @@ class PaintEditorConfigs { /// Indicates the initial paint mode. final PaintModeE initialPaintMode; + /// The minimum scale factor from the layer. + final double minScale; + + /// The maximum scale factor from the layer. + final double maxScale; + /// Creates an instance of PaintEditorConfigs with optional settings. /// /// By default, the editor is enabled, and most drawing tools are enabled. @@ -97,6 +103,8 @@ class PaintEditorConfigs { this.canToggleFill = true, this.canChangeLineWidth = true, this.initialFill = false, + this.minScale = double.negativeInfinity, + this.maxScale = double.infinity, this.freeStyleHighPerformanceScaling, this.freeStyleHighPerformanceMoving, this.freeStyleHighPerformanceHero = false, diff --git a/lib/models/editor_configs/sticker_editor_configs.dart b/lib/models/editor_configs/sticker_editor_configs.dart index c76001bb..ee6b7148 100644 --- a/lib/models/editor_configs/sticker_editor_configs.dart +++ b/lib/models/editor_configs/sticker_editor_configs.dart @@ -38,6 +38,12 @@ class StickerEditorConfigs { /// and behavior of stickers in the editor. final BuildStickers buildStickers; + /// The minimum scale factor from the layer. + final double minScale; + + /// The maximum scale factor from the layer. + final double maxScale; + /// Creates an instance of StickerEditorConfigs with optional settings. /// /// By default, the editor is disabled (if not specified), and other properties @@ -45,6 +51,8 @@ class StickerEditorConfigs { const StickerEditorConfigs({ required this.buildStickers, this.initWidth = 100, + this.minScale = double.negativeInfinity, + this.maxScale = double.infinity, this.enabled = false, }) : assert(initWidth > 0, 'initWidth must be positive'); } diff --git a/lib/models/editor_configs/text_editor_configs.dart b/lib/models/editor_configs/text_editor_configs.dart index 3651ee98..108384fb 100644 --- a/lib/models/editor_configs/text_editor_configs.dart +++ b/lib/models/editor_configs/text_editor_configs.dart @@ -57,6 +57,12 @@ class TextEditorConfigs { /// Allow users to select a different font style final List? customTextStyles; + /// The minimum scale factor from the layer. + final double minScale; + + /// The maximum scale factor from the layer. + final double maxScale; + /// Creates an instance of TextEditorConfigs with optional settings. /// /// By default, the text editor is enabled, and most text formatting options @@ -72,6 +78,8 @@ class TextEditorConfigs { this.initFontScale = 1.0, this.maxFontScale = 3.0, this.minFontScale = 0.3, + this.minScale = double.negativeInfinity, + this.maxScale = double.infinity, this.customTextStyles, this.initialBackgroundColorMode = LayerBackgroundMode.backgroundAndColor, }) : assert(initFontSize > 0, 'initFontSize must be positive'); diff --git a/lib/modules/main_editor/main_editor.dart b/lib/modules/main_editor/main_editor.dart index 8d575c8a..3bddbd0f 100644 --- a/lib/modules/main_editor/main_editor.dart +++ b/lib/modules/main_editor/main_editor.dart @@ -772,6 +772,7 @@ class ProImageEditorState extends State layerInteractionManager.freeStyleHighPerformanceScaling = paintEditorConfigs.freeStyleHighPerformanceScaling ?? !isDesktop; layerInteractionManager.calculateInteractiveButtonScaleRotate( + configs: configs, activeLayer: _activeLayer!, configEnabledHitVibration: helperLines.hitVibration, details: details, @@ -804,6 +805,7 @@ class ProImageEditorState extends State layerInteractionManager.freeStyleHighPerformanceScaling = paintEditorConfigs.freeStyleHighPerformanceScaling ?? !isDesktop; layerInteractionManager.calculateScaleRotate( + configs: configs, activeLayer: _activeLayer!, detail: details, editorSize: sizesManager.editorSize, diff --git a/lib/modules/main_editor/utils/layer_interaction_manager.dart b/lib/modules/main_editor/utils/layer_interaction_manager.dart index db34413e..9ddfcd65 100644 --- a/lib/modules/main_editor/utils/layer_interaction_manager.dart +++ b/lib/modules/main_editor/utils/layer_interaction_manager.dart @@ -125,6 +125,7 @@ class LayerInteractionManager { /// Calculates scaling and rotation based on user interactions. calculateInteractiveButtonScaleRotate({ + required ProImageEditorConfigs configs, required ScaleUpdateDetails details, required Layer activeLayer, required Size editorSize, @@ -157,7 +158,8 @@ class LayerInteractionManager { ) / rotateScaleLayerScaleHelper!; - activeLayer.scale = newDistance / realSize.distance; + activeLayer.scale = (newDistance / realSize.distance); + _setMinMaxScaleFactor(configs, activeLayer); activeLayer.rotation = touchPositionFromCenter.direction - atan(1 / activeSize.aspectRatio); @@ -259,6 +261,7 @@ class LayerInteractionManager { /// Calculates scaling and rotation of a layer based on user interactions. calculateScaleRotate({ + required ProImageEditorConfigs configs, required ScaleUpdateDetails detail, required Layer activeLayer, required Size editorSize, @@ -268,6 +271,7 @@ class LayerInteractionManager { _activeScale = true; activeLayer.scale = baseScaleFactor * detail.scale; + _setMinMaxScaleFactor(configs, activeLayer); activeLayer.rotation = baseAngleFactor + detail.rotation; checkRotationLine( @@ -475,4 +479,29 @@ class LayerInteractionManager { }); } } + + void _setMinMaxScaleFactor(ProImageEditorConfigs configs, Layer layer) { + if (layer is PaintingLayerData) { + layer.scale = layer.scale.clamp( + configs.paintEditorConfigs.minScale, + configs.paintEditorConfigs.maxScale, + ); + } else if (layer is TextLayerData) { + layer.scale = layer.scale.clamp( + configs.textEditorConfigs.minScale, + configs.textEditorConfigs.maxScale, + ); + } else if (layer is EmojiLayerData) { + layer.scale = layer.scale.clamp( + configs.emojiEditorConfigs.minScale, + configs.emojiEditorConfigs.maxScale, + ); + } else if (layer is StickerLayerData && + configs.stickerEditorConfigs != null) { + layer.scale = layer.scale.clamp( + configs.stickerEditorConfigs!.minScale, + configs.stickerEditorConfigs!.maxScale, + ); + } + } } diff --git a/pubspec.yaml b/pubspec.yaml index 3e74bd22..20f07cdd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: pro_image_editor description: "A Flutter image editor: Seamlessly enhance your images with user-friendly editing features." -version: 4.0.5 +version: 4.0.6 homepage: https://github.com/hm21/pro_image_editor/ repository: https://github.com/hm21/pro_image_editor/ issue_tracker: https://github.com/hm21/pro_image_editor/issues/