Skip to content

Commit

Permalink
feat(layer-scale): add ability to set minimum and maximum scale facto…
Browse files Browse the repository at this point in the history
…r for layers

This was requested in [#122](#122).
  • Loading branch information
hm21 committed Jun 19, 2024
1 parent 1fffb55 commit 7f19fbb
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
8 changes: 8 additions & 0 deletions lib/models/editor_configs/emoji_editor_configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ class EmojiEditorConfigs {
/// Custom emojis; if set, overrides default emojis provided by the library.
final List<CategoryEmoji> 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
/// reasonable defaults.
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');
Expand Down
8 changes: 8 additions & 0 deletions lib/models/editor_configs/paint_editor_configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions lib/models/editor_configs/sticker_editor_configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ 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
/// are set to reasonable defaults.
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');
}
Expand Down
8 changes: 8 additions & 0 deletions lib/models/editor_configs/text_editor_configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class TextEditorConfigs {
/// Allow users to select a different font style
final List<TextStyle>? 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
Expand All @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/main_editor/main_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ class ProImageEditorState extends State<ProImageEditor>
layerInteractionManager.freeStyleHighPerformanceScaling =
paintEditorConfigs.freeStyleHighPerformanceScaling ?? !isDesktop;
layerInteractionManager.calculateInteractiveButtonScaleRotate(
configs: configs,
activeLayer: _activeLayer!,
configEnabledHitVibration: helperLines.hitVibration,
details: details,
Expand Down Expand Up @@ -804,6 +805,7 @@ class ProImageEditorState extends State<ProImageEditor>
layerInteractionManager.freeStyleHighPerformanceScaling =
paintEditorConfigs.freeStyleHighPerformanceScaling ?? !isDesktop;
layerInteractionManager.calculateScaleRotate(
configs: configs,
activeLayer: _activeLayer!,
detail: details,
editorSize: sizesManager.editorSize,
Expand Down
31 changes: 30 additions & 1 deletion lib/modules/main_editor/utils/layer_interaction_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -268,6 +271,7 @@ class LayerInteractionManager {
_activeScale = true;

activeLayer.scale = baseScaleFactor * detail.scale;
_setMinMaxScaleFactor(configs, activeLayer);
activeLayer.rotation = baseAngleFactor + detail.rotation;

checkRotationLine(
Expand Down Expand Up @@ -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,
);
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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/
Expand Down

0 comments on commit 7f19fbb

Please sign in to comment.