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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the Bug Where Repeatedly Adjusting Angles Causes GuideView Size to Decrease #225

Merged
merged 4 commits into from Mar 14, 2024
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
5 changes: 5 additions & 0 deletions Dev/Sources/SwiftUIDemo/DemoCropView2.swift
Expand Up @@ -17,6 +17,7 @@ struct DemoCropView2: View {

@StateObject var editingStack: EditingStack
@State var resultImage: ResultImage?
@State var angle: EditingCrop.AdjustmentAngle = .zero

init(
editingStack: @escaping () -> EditingStack
Expand Down Expand Up @@ -52,11 +53,15 @@ struct DemoCropView2: View {
.fill(kind == nil ? Color.white : Color.white.opacity(0.6))
}
)
.adjustmentAngle(angle)
.frame(height: 500)
.clipped()
.background(Color.gray)

Spacer()

Slider(value: $angle.degrees, in: -45.0...45.0, step: 1)

}

VStack {
Expand Down
28 changes: 2 additions & 26 deletions Sources/BrightroomEngine/Core/EditingCrop.swift
Expand Up @@ -148,8 +148,6 @@ public struct EditingCrop: Equatable {

imageSize.width *= scale
imageSize.height *= scale
imageSize.width.round(.down)
imageSize.height.round(.down)

modified.cropExtent = Self.fittingRect(
rect: cropExtent,
Expand All @@ -169,7 +167,7 @@ public struct EditingCrop: Equatable {
*/
public mutating func updateCropExtent(toFitAspectRatio newAspectRatio: PixelAspectRatio) {

let maxSize = newAspectRatio.sizeThatFitsWithRounding(in: imageSize)
let maxSize = newAspectRatio.sizeThatFits(in: imageSize)

let proposed = CGRect(
origin: .init(
Expand Down Expand Up @@ -260,31 +258,10 @@ public struct EditingCrop: Equatable {

var fixed = rect

func containsFractionInCGFloat(_ value: CGFloat) -> Bool {
Int(exactly: value) == nil
}

func rectIsPixelPerfect(_ rect: CGRect) -> Bool {
guard containsFractionInCGFloat(rect.origin.x) == false else { return false }
guard containsFractionInCGFloat(rect.origin.y) == false else { return false }
guard containsFractionInCGFloat(rect.size.width) == false else { return false }
guard containsFractionInCGFloat(rect.size.height) == false else { return false }
return true
}

func clamp<T: Comparable>(value: T, lower: T, upper: T) -> T {
return min(max(value, lower), upper)
}

/*
Drops decimal fraction
*/

fixed.origin.x.round(.down)
fixed.origin.y.round(.down)
fixed.size.width.round(.down)
fixed.size.height.round(.down)

/*
Cuts the area off that out of maximum bounds

Expand Down Expand Up @@ -329,7 +306,7 @@ public struct EditingCrop: Equatable {
)
)

let newRect = aspectRatio.rectThatFitsWithRounding(in: maxRect)
let newRect = aspectRatio.rectThatFits(in: maxRect)

fixed = newRect

Expand All @@ -347,7 +324,6 @@ public struct EditingCrop: Equatable {
assert(fixed.width <= imageSize.width)
assert(fixed.height <= imageSize.height)

assert(rectIsPixelPerfect(fixed))
}

#if DEBUG
Expand Down
25 changes: 10 additions & 15 deletions Sources/BrightroomEngine/Library/Geometry.swift
Expand Up @@ -179,7 +179,7 @@ public struct PixelAspectRatio: Hashable, CustomReflectable, Identifiable {
.init(width: height, height: width)
}

public func sizeThatFillRounding(in boundingSize: CGSize) -> CGSize {
public func sizeThatFill(in boundingSize: CGSize) -> CGSize {
let widthRatio = boundingSize.width / width
let heightRatio = boundingSize.height / height
var size = boundingSize
Expand All @@ -191,12 +191,12 @@ public struct PixelAspectRatio: Hashable, CustomReflectable, Identifiable {
}

return CGSize(
width: size.width.rounded(.down),
height: size.height.rounded(.down)
width: size.width,
height: size.height
)
}

public func sizeThatFitsWithRounding(in boundingSize: CGSize) -> CGSize {
public func sizeThatFits(in boundingSize: CGSize) -> CGSize {

let widthRatio = boundingSize.width / width
let heightRatio = boundingSize.height / height
Expand All @@ -209,32 +209,27 @@ public struct PixelAspectRatio: Hashable, CustomReflectable, Identifiable {
}

return CGSize(
width: size.width.rounded(.down),
height: size.height.rounded(.down)
width: size.width,
height: size.height
)

}

public func rectThatFitsWithRounding(in boundingRect: CGRect) -> CGRect {
let size = sizeThatFitsWithRounding(in: boundingRect.size)
public func rectThatFits(in boundingRect: CGRect) -> CGRect {
let size = sizeThatFits(in: boundingRect.size)
var origin = boundingRect.origin
origin.x += (boundingRect.size.width - size.width) / 2.0
origin.y += (boundingRect.size.height - size.height) / 2.0

origin.x.round(.down)
origin.y.round(.down)

return CGRect(origin: origin, size: size)
}

public func rectThatFillWithRounding(in boundingRect: CGRect) -> CGRect {
let size = sizeThatFillRounding(in: boundingRect.size)
public func rectThatFill(in boundingRect: CGRect) -> CGRect {
let size = sizeThatFill(in: boundingRect.size)
var origin = CGPoint.zero
origin.x = (boundingRect.size.width - size.width) / 2.0
origin.y = (boundingRect.size.height - size.height) / 2.0

origin.x.round(.down)
origin.y.round(.down)
return CGRect(origin: origin, size: size)
}

Expand Down
Expand Up @@ -216,8 +216,8 @@ extension CropView {

super.layoutSubviews()

let width = (bounds.width / 3).rounded(.down)
let height = (bounds.height / 3).rounded(.down)
let width = (bounds.width / 3)
let height = (bounds.height / 3)

do {

Expand Down
4 changes: 2 additions & 2 deletions Sources/BrightroomUI/Shared/Components/Crop/CropView.swift
Expand Up @@ -699,7 +699,7 @@ extension CropView {
let bounds = self.bounds.inset(by: contentInset)

let size = PixelAspectRatio(crop.cropExtent.size)
.sizeThatFitsWithRounding(in: bounds.size)
.sizeThatFits(in: bounds.size)

return .init(
origin: .init(
Expand Down Expand Up @@ -746,7 +746,7 @@ extension CropView {
let bounds = self.bounds.inset(by: contentInset)

let size = PixelAspectRatio(crop.cropExtent.size)
.sizeThatFitsWithRounding(in: bounds.size)
.sizeThatFits(in: bounds.size)

return .init(
origin: .init(
Expand Down
Expand Up @@ -42,7 +42,7 @@ public final class BlurryMaskingView: PixelEditorCodeBasedView, UIScrollViewDele
}

let aspectRatio = PixelAspectRatio(proposedCrop.cropExtent.size)
let size = aspectRatio.sizeThatFitsWithRounding(in: bounds.size)
let size = aspectRatio.sizeThatFits(in: bounds.size)

let (min, _) = proposedCrop.calculateZoomScale(visibleSize: size)

Expand Down