Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added support to set the rotation limits #148

Merged
merged 4 commits into from Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions Mantis.xcodeproj/project.pbxproj
Expand Up @@ -62,7 +62,6 @@
OBJ_93 /* CropVisualEffectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_40 /* CropVisualEffectView.swift */; };
OBJ_94 /* RatioOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_41 /* RatioOptions.swift */; };
OBJ_95 /* CGAngle.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_43 /* CGAngle.swift */; };
OBJ_96 /* DialConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_44 /* DialConfig.swift */; };
OBJ_97 /* RotationCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_45 /* RotationCalculator.swift */; };
OBJ_98 /* RotationDial+Touches.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_46 /* RotationDial+Touches.swift */; };
OBJ_99 /* RotationDial.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_47 /* RotationDial.swift */; };
Expand Down Expand Up @@ -144,7 +143,6 @@
OBJ_40 /* CropVisualEffectView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CropVisualEffectView.swift; sourceTree = "<group>"; };
OBJ_41 /* RatioOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RatioOptions.swift; sourceTree = "<group>"; };
OBJ_43 /* CGAngle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CGAngle.swift; sourceTree = "<group>"; };
OBJ_44 /* DialConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DialConfig.swift; sourceTree = "<group>"; };
OBJ_45 /* RotationCalculator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RotationCalculator.swift; sourceTree = "<group>"; };
OBJ_46 /* RotationDial+Touches.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RotationDial+Touches.swift"; sourceTree = "<group>"; };
OBJ_47 /* RotationDial.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RotationDial.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -263,7 +261,6 @@
isa = PBXGroup;
children = (
OBJ_43 /* CGAngle.swift */,
OBJ_44 /* DialConfig.swift */,
OBJ_45 /* RotationCalculator.swift */,
OBJ_46 /* RotationDial+Touches.swift */,
OBJ_47 /* RotationDial.swift */,
Expand Down Expand Up @@ -567,7 +564,6 @@
OBJ_93 /* CropVisualEffectView.swift in Sources */,
OBJ_94 /* RatioOptions.swift in Sources */,
OBJ_95 /* CGAngle.swift in Sources */,
OBJ_96 /* DialConfig.swift in Sources */,
OBJ_97 /* RotationCalculator.swift in Sources */,
OBJ_98 /* RotationDial+Touches.swift in Sources */,
OBJ_99 /* RotationDial.swift in Sources */,
Expand Down
16 changes: 7 additions & 9 deletions Sources/Mantis/CropView/CropView.swift
Expand Up @@ -37,6 +37,9 @@ let hotAreaUnit: CGFloat = 32
let cropViewPadding:CGFloat = 14.0

class CropView: UIView {

public var dialConfig = Mantis.Config().dialConfig
7blueXY marked this conversation as resolved.
Show resolved Hide resolved

var cropShapeType: CropShapeType = .rect
var cropVisualEffectType: CropVisualEffectType = .blurDark
var angleDashboardHeight: CGFloat = 60
Expand Down Expand Up @@ -76,9 +79,10 @@ class CropView: UIView {
print("CropView deinit.")
}

init(image: UIImage, viewModel: CropViewModel = CropViewModel()) {
init(image: UIImage, viewModel: CropViewModel = CropViewModel(), dialConfig: DialConfig = Mantis.Config().dialConfig) {
self.image = image
self.viewModel = viewModel
self.dialConfig = dialConfig

imageContainer = ImageContainer()
gridOverlayView = CropOverlayView()
Expand Down Expand Up @@ -247,15 +251,9 @@ class CropView: UIView {
if rotationDial != nil {
rotationDial?.removeFromSuperview()
}

var config = DialConfig.Config()
config.backgroundColor = .clear
config.angleShowLimitType = .limit(angle: CGAngle(degrees: 40))
config.rotationLimitType = .limit(angle: CGAngle(degrees: 45))
config.numberShowSpan = 1


let boardLength = min(bounds.width, bounds.height) * 0.6
let rotationDial = RotationDial(frame: CGRect(x: 0, y: 0, width: boardLength, height: angleDashboardHeight), config: config)
let rotationDial = RotationDial(frame: CGRect(x: 0, y: 0, width: boardLength, height: angleDashboardHeight), dialConfig: dialConfig)
self.rotationDial = rotationDial
rotationDial.isUserInteractionEnabled = true
addSubview(rotationDial)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mantis/CropViewController/CropViewController.swift
Expand Up @@ -59,7 +59,7 @@ public class CropViewController: UIViewController {
public var config = Mantis.Config()

private var orientation: UIInterfaceOrientation = .unknown
private lazy var cropView = CropView(image: image, viewModel: CropViewModel())
private lazy var cropView = CropView(image: image, viewModel: CropViewModel(), dialConfig: config.dialConfig)
private var cropToolbar: CropToolbarProtocol
private var ratioPresenter: RatioPresenter?
private var ratioSelector: RatioSelector?
Expand All @@ -79,7 +79,7 @@ public class CropViewController: UIViewController {
self.image = image

self.config = config

switch config.cropShapeType {
case .circle, .square, .heart:
self.config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: 1)
Expand Down
81 changes: 78 additions & 3 deletions Sources/Mantis/Mantis.swift
Expand Up @@ -30,6 +30,7 @@ private(set) var bundle: Bundle? = {

internal var localizationConfig = LocalizationConfig()

// MARK: - Functions
public func cropViewController(image: UIImage,
config: Mantis.Config = Mantis.Config(),
cropToolbar: CropToolbarProtocol = CropToolbar(frame: CGRect.zero)) -> CropViewController {
Expand All @@ -56,6 +57,7 @@ public func getCroppedImage(byCropInfo info: CropInfo, andImage image: UIImage)
return image.getCroppedImage(byCropInfo: info)
}

// MARK: - Type Aliases
public typealias Transformation = (
offset: CGPoint,
rotation: CGFloat,
Expand All @@ -68,6 +70,7 @@ public typealias Transformation = (

public typealias CropInfo = (translation: CGPoint, rotation: CGFloat, scale: CGFloat, cropSize: CGSize, imageViewSize: CGSize)

// MARK: - Enums
public enum PresetTransformationType {
case none
case presetInfo(info: Transformation)
Expand Down Expand Up @@ -139,6 +142,40 @@ public enum FixRatiosShowType {
case vetical
}

public enum DialRotationCenterType {
case useDefault
case custom(CGPoint)
}

public enum DialAngleShowLimitType {
case noLimit
case limit(angle: CGAngle)
}
7blueXY marked this conversation as resolved.
Show resolved Hide resolved

public enum DialOrientation {
case normal
case right
case left
case upsideDown
}

public enum DialTheme {
case dark
case light
}

public enum DialRotationLimitType {
case noLimit
case limit(angle: CGAngle)
}

// MARK: - Localization
public class LocalizationConfig {
public var bundle: Bundle? = Mantis.Config.bundle
public var tableName = "MantisLocalizable"
}

// MARK: - CropToolbarConfig
public struct CropToolbarConfig {
public var optionButtonFontSize: CGFloat = 14
public var optionButtonFontSizeForPad: CGFloat = 20
Expand All @@ -153,18 +190,56 @@ public struct CropToolbarConfig {
var includeFixedRatioSettingButton = true
}

public class LocalizationConfig {
public var bundle: Bundle? = Mantis.Config.bundle
public var tableName = "MantisLocalizable"
// MARK: - DialConfig
public struct DialConfig {
7blueXY marked this conversation as resolved.
Show resolved Hide resolved
public init() {}

public var margin: Double = 10
public var interactable = false
public var rotationLimitType: DialRotationLimitType = .limit(angle: CGAngle(degrees: 45))
public var angleShowLimitType: DialAngleShowLimitType = .limit(angle: CGAngle(degrees: 40))
public var rotationCenterType: DialRotationCenterType = .useDefault
public var numberShowSpan = 1
public var orientation: DialOrientation = .normal

public var backgroundColor: UIColor = .clear
public var bigScaleColor: UIColor = .lightGray
public var smallScaleColor: UIColor = .lightGray
public var indicatorColor: UIColor = .lightGray
public var numberColor: UIColor = .lightGray
public var centerAxisColor: UIColor = .lightGray

public var theme: DialTheme = .dark {
didSet {
switch theme {
case .dark:
backgroundColor = .clear
bigScaleColor = .lightGray
smallScaleColor = .lightGray
indicatorColor = .lightGray
numberColor = .lightGray
centerAxisColor = .lightGray
case .light:
backgroundColor = .clear
bigScaleColor = .darkGray
smallScaleColor = .darkGray
indicatorColor = .darkGray
numberColor = .darkGray
centerAxisColor = .darkGray
}
}
}
}

// MARK: - Config
public struct Config {
public var presetTransformationType: PresetTransformationType = .none
public var cropShapeType: CropShapeType = .rect
public var cropVisualEffectType: CropVisualEffectType = .blurDark
public var ratioOptions: RatioOptions = .all
public var presetFixedRatioType: PresetFixedRatioType = .canUseMultiplePresetFixedRatio()
public var showRotationDial = true
public var dialConfig = DialConfig()
public var cropToolbarConfig = CropToolbarConfig()
public private(set) var localizationConfig = Mantis.localizationConfig

Expand Down
79 changes: 0 additions & 79 deletions Sources/Mantis/RotationDial/DialConfig.swift

This file was deleted.