Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Jan 31, 2024
1 parent ac9da41 commit f8587ad
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Dev/Brightroom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
4B8CAF6925FFCD720075032A /* IsolatedEditingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8CAF6825FFCD720075032A /* IsolatedEditingView.swift */; };
4B8CAF6E25FFCE720075032A /* FullscreenIdentifiableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8CAF6D25FFCE720075032A /* FullscreenIdentifiableView.swift */; };
4B8CAF7325FFD13D0075032A /* BlurryMaskingViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8CAF7225FFD13D0075032A /* BlurryMaskingViewWrapper.swift */; };
4B9325832B6A923400DDABAC /* RotateScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B9325822B6A923400DDABAC /* RotateScrollView.swift */; };
4B9369E825F940E600B18571 /* nasa.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 4B9369E725F940E600B18571 /* nasa.jpg */; };
4B9369E925F940E600B18571 /* nasa.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 4B9369E725F940E600B18571 /* nasa.jpg */; };
4B98CCBC25EFF31300E4F61F /* SwiftUIDemoApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B98CCBB25EFF31300E4F61F /* SwiftUIDemoApp.swift */; };
Expand Down Expand Up @@ -607,6 +608,7 @@
4B8CAF6825FFCD720075032A /* IsolatedEditingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IsolatedEditingView.swift; sourceTree = "<group>"; };
4B8CAF6D25FFCE720075032A /* FullscreenIdentifiableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FullscreenIdentifiableView.swift; sourceTree = "<group>"; };
4B8CAF7225FFD13D0075032A /* BlurryMaskingViewWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlurryMaskingViewWrapper.swift; sourceTree = "<group>"; };
4B9325822B6A923400DDABAC /* RotateScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RotateScrollView.swift; sourceTree = "<group>"; };
4B9369E725F940E600B18571 /* nasa.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = nasa.jpg; sourceTree = "<group>"; };
4B98CCB925EFF31300E4F61F /* SwiftUIDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUIDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
4B98CCBB25EFF31300E4F61F /* SwiftUIDemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIDemoApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1235,6 +1237,7 @@
4B98CCC425EFF31400E4F61F /* Info.plist */,
4B98CCC125EFF31400E4F61F /* Preview Content */,
4BB9180B25F5460000C446B8 /* Launch Screen.storyboard */,
4B9325822B6A923400DDABAC /* RotateScrollView.swift */,
);
path = SwiftUIDemo;
sourceTree = "<group>";
Expand Down Expand Up @@ -2392,6 +2395,7 @@
4B7EE66D25F515B400E17AF1 /* XCAssets+Generated.swift in Sources */,
4B98CCBE25EFF31300E4F61F /* ContentView.swift in Sources */,
4B75114025F13276002D804A /* DemoCropView.swift in Sources */,
4B9325832B6A923400DDABAC /* RotateScrollView.swift in Sources */,
4B98CCBC25EFF31300E4F61F /* SwiftUIDemoApp.swift in Sources */,
4B98CCE725EFF4EF00E4F61F /* Mocks.swift in Sources */,
);
Expand Down
2 changes: 2 additions & 0 deletions Dev/Sources/SwiftUIDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ struct ContentView: View {
Form {
NavigationLink("Isolated", destination: IsolatedEditinView())

NavigationLink("Hoge", destination: BookRotateScrollView())

Section {
Button("Component: Crop - keepAlive") {
fullScreenView = .init { DemoCropView(editingStack: sharedStack) }
Expand Down
33 changes: 30 additions & 3 deletions Dev/Sources/SwiftUIDemo/DemoCropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct DemoCropView: View {
let editingStack: EditingStack

@State var rotation: EditingCrop.Rotation?
@State var adjustmentAngle: EditingCrop.AdjustmentAngle = .zero

var body: some View {
VStack {
Expand All @@ -37,17 +38,27 @@ struct DemoCropView: View {
})
)
.rotation(rotation)
.adjustmentAngle(adjustmentAngle)
.clipped()

HStack {
Button("rotate") {
Button("0") {
self.rotation = .angle_0
}
Button("90") {
self.rotation = .angle_90
}
Button("180") {
self.rotation = .angle_180
}
Button("270") {
self.rotation = .angle_270
}
Button("- 10") {
self.rotation = .angle_180
self.adjustmentAngle -= .degrees(10)
}
Button("+ 10") {
self.rotation = .angle_180
self.adjustmentAngle += .degrees(10)
}
}
}
Expand All @@ -66,3 +77,19 @@ struct DemoCropView: View {
#Preview {
DemoCropView(editingStack: Mocks.makeEditingStack(image: Mocks.imageHorizontal()))
}

#Preview {
Text("")
.onAppear {

let uiView = UIView(frame: .init(origin: .zero, size: .init(width: 100, height: 200)))
print(uiView.frame)

uiView.transform = .init(rotationAngle: Angle(degrees: 10).radians)
print(uiView.transform)

print(uiView.frame, uiView.bounds)
}
}


57 changes: 57 additions & 0 deletions Dev/Sources/SwiftUIDemo/RotateScrollView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import SwiftUI

struct BookRotateScrollView: View, PreviewProvider {
var body: some View {
ContentView()
}

static var previews: some View {
Self()
}

private struct ContentView: View {

var body: some View {
Represent()
}
}

class ContainerView: UIView {

override init(frame: CGRect) {
super.init(frame: frame)

let scrollView = UIScrollView()
scrollView.backgroundColor = .black
scrollView.frame = frame.insetBy(dx: 30, dy: 30)

addSubview(scrollView)

scrollView.transform = .init(rotationAngle: Angle(degrees: 40).radians)

}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

struct Represent: UIViewRepresentable {

func makeUIView(context: Context) -> BookRotateScrollView.ContainerView {
.init(frame: .init(origin: .zero, size: .init(width: 300, height: 300)))
}

func updateUIView(_ uiView: BookRotateScrollView.ContainerView, context: Context) {

}

typealias UIViewType = ContainerView



}

}

6 changes: 6 additions & 0 deletions Sources/BrightroomEngine/Core/EditingCrop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import UIKit
import Vision
import SwiftUI

/// A representation of cropping extent in Image.
public struct EditingCrop: Equatable {
Expand Down Expand Up @@ -64,6 +65,8 @@ public struct EditingCrop: Equatable {
}
}

public typealias AdjustmentAngle = SwiftUI.Angle

/// The dimensions in pixel for the image.
/// Applied image-orientation.
public var imageSize: CGSize
Expand All @@ -74,6 +77,9 @@ public struct EditingCrop: Equatable {
/// The angle that specifies rotation for the image.
public var rotation: Rotation = .angle_0

/// An angle to rotate in addition to the specified rotation.
public var adjustmentAngle: AdjustmentAngle = .zero

public private(set) var scaleToRestore: CGFloat

public init(from ciImage: CIImage) {
Expand Down
49 changes: 38 additions & 11 deletions Sources/BrightroomUI/Shared/Components/Crop/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ public final class CropView: UIView, UIScrollViewDelegate {
}
}

public func setAdjustmentAngle(_ angle: EditingCrop.AdjustmentAngle) {

store.commit {
$0.proposedCrop?.adjustmentAngle = angle
$0.layoutVersion += 1
}
}

public func setCrop(_ crop: EditingCrop) {
_pixeleditor_ensureMainThread()

Expand Down Expand Up @@ -522,33 +530,52 @@ extension CropView {
size = aspectRatio.sizeThatFitsWithRounding(in: bounds.size)
guideView.setLockedAspectRatio(preferredAspectRatio)
case .angle_90:
size = aspectRatio.swapped().sizeThatFitsWithRounding(in: bounds.size)
size = aspectRatio
.swapped()
.sizeThatFitsWithRounding(in: bounds.size)
guideView.setLockedAspectRatio(preferredAspectRatio?.swapped())
case .angle_180:
size = aspectRatio.sizeThatFitsWithRounding(in: bounds.size)
guideView.setLockedAspectRatio(preferredAspectRatio)
case .angle_270:
size = aspectRatio.swapped().sizeThatFitsWithRounding(in: bounds.size)
size = aspectRatio
.swapped()
.sizeThatFitsWithRounding(in: bounds.size)
guideView.setLockedAspectRatio(preferredAspectRatio?.swapped())
}

scrollView.transform = crop.rotation.transform

scrollView.frame = .init(
let scrollViewFrame = CGRect(
origin: .init(
x: contentInset.left + ((bounds.width - size.width) / 2) /* centering offset */,
y: contentInset.top + ((bounds.height - size.height) / 2) /* centering offset */
),
size: size
)

scrollBackdropView.frame = scrollView.frame
}

applyLayoutDescendants: do {
guideView.frame = scrollView.frame
// step 1 - rotate in right angle
do {

scrollView.transform = crop.rotation.transform
scrollView.frame = scrollViewFrame

scrollBackdropView.frame = scrollViewFrame
guideView.frame = scrollViewFrame

}

// step 2 - rotate in adjustment angle
do {
let frame = scrollView.frame
let rotatedFrame = frame.applying(.init(rotationAngle: crop.adjustmentAngle.radians))
scrollView.frame = rotatedFrame
scrollView.transform = crop.rotation.transform.rotated(by: crop.adjustmentAngle.radians)
}

// step 3 - lay the scroll view out on thne center.
scrollView.center = .init(x: bounds.midX, y: bounds.midY)

}

zoom: do {

let (min, max) = crop.calculateZoomScale(scrollViewSize: scrollView.bounds.size)
Expand Down
13 changes: 13 additions & 0 deletions Sources/BrightroomUI/Shared/Components/Crop/SwiftUICropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public struct SwiftUICropView: UIViewControllerRepresentable {
private let factory: () -> CropView

private var _rotation: EditingCrop.Rotation?
private var _adjustmentAngle: EditingCrop.AdjustmentAngle?

public init(
editingStack: EditingStack,
Expand Down Expand Up @@ -99,6 +100,10 @@ public struct SwiftUICropView: UIViewControllerRepresentable {
if let _rotation {
uiViewController.bodyView.setRotation(_rotation)
}

if let _adjustmentAngle {
uiViewController.bodyView.setAdjustmentAngle(_adjustmentAngle)
}
}

public func rotation(_ rotation: EditingCrop.Rotation?) -> Self {
Expand All @@ -108,4 +113,12 @@ public struct SwiftUICropView: UIViewControllerRepresentable {
return modified
}

public func adjustmentAngle(_ angle: EditingCrop.AdjustmentAngle?) -> Self {

var modified = self
modified._adjustmentAngle = angle
return modified

}

}

0 comments on commit f8587ad

Please sign in to comment.