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 all 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
8 changes: 4 additions & 4 deletions Mantis.xcodeproj/project.pbxproj
Expand Up @@ -21,6 +21,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
269AF94227437EE400F7FAF6 /* DialConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269AF94127437EE400F7FAF6 /* DialConfig.swift */; };
5F17E40A253535F300A3EB7D /* Orientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F17E409253535F300A3EB7D /* Orientation.swift */; };
5F69CC5E26C0629400568B75 /* definitions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F69CC5D26C0629400568B75 /* definitions.swift */; };
5F7D22AE245BCA8D0015A0D5 /* CropToolbarProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F7D22AD245BCA8D0015A0D5 /* CropToolbarProtocol.swift */; };
Expand Down Expand Up @@ -62,7 +63,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 @@ -93,6 +93,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
269AF94127437EE400F7FAF6 /* DialConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DialConfig.swift; sourceTree = "<group>"; };
5F17E409253535F300A3EB7D /* Orientation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Orientation.swift; sourceTree = "<group>"; };
5F69CC5D26C0629400568B75 /* definitions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = definitions.swift; sourceTree = "<group>"; };
5F7D22AD245BCA8D0015A0D5 /* CropToolbarProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropToolbarProtocol.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -144,7 +145,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 +263,7 @@
isa = PBXGroup;
children = (
OBJ_43 /* CGAngle.swift */,
OBJ_44 /* DialConfig.swift */,
269AF94127437EE400F7FAF6 /* DialConfig.swift */,
OBJ_45 /* RotationCalculator.swift */,
OBJ_46 /* RotationDial+Touches.swift */,
OBJ_47 /* RotationDial.swift */,
Expand Down Expand Up @@ -565,9 +565,9 @@
FEDAAD8C25205DE900D95667 /* RatioItemView.swift in Sources */,
FEDAAD8625205CC300D95667 /* RatioSelector.swift in Sources */,
OBJ_93 /* CropVisualEffectView.swift in Sources */,
269AF94227437EE400F7FAF6 /* DialConfig.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
17 changes: 12 additions & 5 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,13 @@ public enum FixRatiosShowType {
case vetical
}

// 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 +163,15 @@ public struct CropToolbarConfig {
var includeFixedRatioSettingButton = true
}

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

// 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
96 changes: 47 additions & 49 deletions Sources/Mantis/RotationDial/DialConfig.swift
Expand Up @@ -2,77 +2,75 @@
// DialConfig.swift
// Mantis
//
// Created by Echo on 5/24/19.
// Created by Echo on 5/22/19.
// Copyright © 2019 Echo. All rights reserved.
//

import Foundation
import UIKit

enum DialConfig {
struct Config {
public init() {}

public var margin: Double = 10
public var interactable = false
public var rotationLimitType: RotationLimitType = .noLimit
public var angleShowLimitType: AnglehowLimitType = .noLimit
public var rotationCenterType: RotationCenterType = .useDefault
public var numberShowSpan = 2
public var orientation: Orientation = .normal

public var backgroundColor: UIColor = .black
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: Theme = .dark {
didSet {
switch theme {
case .dark:
backgroundColor = .black
bigScaleColor = .lightGray
smallScaleColor = .lightGray
indicatorColor = .lightGray
numberColor = .lightGray
centerAxisColor = .lightGray
case .light:
backgroundColor = .white
bigScaleColor = .darkGray
smallScaleColor = .darkGray
indicatorColor = .darkGray
numberColor = .darkGray
centerAxisColor = .darkGray
}
// MARK: - DialConfig
public struct DialConfig {
public init() {}

public var margin: Double = 10
public var interactable = false
public var rotationLimitType: RotationLimitType = .limit(angle: CGAngle(degrees: 45))
public var angleShowLimitType: AngleShowLimitType = .limit(angle: CGAngle(degrees: 40))
public var rotationCenterType: RotationCenterType = .useDefault
public var numberShowSpan = 1
public var orientation: Orientation = .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: Theme = .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
}
}
}
enum RotationCenterType {

public enum RotationCenterType {
case useDefault
case custom(CGPoint)
}
enum RotationLimitType {

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

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

public enum Orientation {
case normal
case right
case left
case upsideDown
}
enum Theme {

public enum Theme {
case dark
case light
}
Expand Down