Browse Source

Transforming rotated items

main
Steve Ruiz 4 years ago
parent
commit
da8f812090

+ 2
- 0
lib/shapes/ellipse.tsx View File

@@ -73,6 +73,8 @@ const ellipse = createShape<EllipseShape>({
73 73
   },
74 74
 
75 75
   hitTestBounds(this, shape, brushBounds) {
76
+    // TODO: Account for rotation
77
+
76 78
     const shapeBounds = this.getBounds(shape)
77 79
 
78 80
     return (

+ 2
- 0
lib/shapes/index.tsx View File

@@ -61,11 +61,13 @@ export interface ShapeUtility<K extends Shape> {
61 61
     bounds: Bounds,
62 62
     info: {
63 63
       type: TransformEdge | TransformCorner
64
+      boundsRotation: number
64 65
       initialShape: K
65 66
       initialShapeBounds: BoundsSnapshot
66 67
       initialBounds: Bounds
67 68
       isFlippedX: boolean
68 69
       isFlippedY: boolean
70
+      isSingle: boolean
69 71
       anchor: TransformEdge | TransformCorner
70 72
     }
71 73
   ): K

+ 38
- 3
lib/shapes/rectangle.tsx View File

@@ -103,9 +103,44 @@ const rectangle = createShape<RectangleShape>({
103 103
     return shape
104 104
   },
105 105
 
106
-  transform(shape, bounds) {
107
-    shape.point = [bounds.minX, bounds.minY]
108
-    shape.size = [bounds.width, bounds.height]
106
+  transform(
107
+    shape,
108
+    shapeBounds,
109
+    { initialShape, isSingle, initialShapeBounds, isFlippedX, isFlippedY }
110
+  ) {
111
+    // TODO: Apply rotation to single-selection items
112
+
113
+    if (shape.rotation === 0 || isSingle) {
114
+      shape.size = [shapeBounds.width, shapeBounds.height]
115
+      shape.point = [shapeBounds.minX, shapeBounds.minY]
116
+    } else {
117
+      shape.size = vec.mul(
118
+        initialShape.size,
119
+        Math.min(
120
+          shapeBounds.width / initialShapeBounds.width,
121
+          shapeBounds.height / initialShapeBounds.height
122
+        )
123
+      )
124
+
125
+      const newCenter = [
126
+        shapeBounds.minX + shapeBounds.width / 2,
127
+        shapeBounds.minY + shapeBounds.height / 2,
128
+      ]
129
+
130
+      shape.point = vec.sub(newCenter, vec.div(shape.size, 2))
131
+    }
132
+
133
+    // Rotation for flipped shapes
134
+
135
+    shape.rotation = initialShape.rotation
136
+
137
+    if (isFlippedX) {
138
+      shape.rotation *= -1
139
+    }
140
+
141
+    if (isFlippedY) {
142
+      shape.rotation *= -1
143
+    }
109 144
 
110 145
     return shape
111 146
   },

+ 8
- 0
state/commands/transform.ts View File

@@ -22,6 +22,8 @@ export default function translateCommand(
22 22
           initialBounds,
23 23
           currentPageId,
24 24
           selectedIds,
25
+          isSingle,
26
+          boundsRotation,
25 27
         } = after
26 28
 
27 29
         const { shapes } = data.document.pages[currentPageId]
@@ -35,8 +37,10 @@ export default function translateCommand(
35 37
             initialShape,
36 38
             initialShapeBounds,
37 39
             initialBounds,
40
+            boundsRotation,
38 41
             isFlippedX: false,
39 42
             isFlippedY: false,
43
+            isSingle,
40 44
             anchor,
41 45
           })
42 46
         })
@@ -48,6 +52,8 @@ export default function translateCommand(
48 52
           initialBounds,
49 53
           currentPageId,
50 54
           selectedIds,
55
+          isSingle,
56
+          boundsRotation,
51 57
         } = before
52 58
 
53 59
         const { shapes } = data.document.pages[currentPageId]
@@ -61,8 +67,10 @@ export default function translateCommand(
61 67
             initialShape,
62 68
             initialShapeBounds,
63 69
             initialBounds,
70
+            boundsRotation,
64 71
             isFlippedX: false,
65 72
             isFlippedY: false,
73
+            isSingle,
66 74
             anchor: type,
67 75
           })
68 76
         })

+ 41
- 11
state/sessions/transform-session.ts View File

@@ -32,6 +32,18 @@ export default class TransformSession extends BaseSession {
32 32
     super(data)
33 33
     this.origin = point
34 34
     this.transformType = transformType
35
+
36
+    // if (data.selectedIds.size === 1) {
37
+    //   const shape =
38
+    //     data.document.pages[data.currentPageId].shapes[
39
+    //       Array.from(data.selectedIds.values())[0]
40
+    //     ]
41
+
42
+    //   if (shape.rotation > 0) {
43
+
44
+    //   }
45
+    // }
46
+
35 47
     this.snapshot = getTransformSnapshot(data, transformType)
36 48
 
37 49
     const { minX, minY, maxX, maxY } = this.snapshot.initialBounds
@@ -43,21 +55,26 @@ export default class TransformSession extends BaseSession {
43 55
   }
44 56
 
45 57
   update(data: Data, point: number[]) {
46
-    const { shapeBounds, initialBounds, currentPageId, selectedIds } =
47
-      this.snapshot
48
-
49
-    const { shapes } = data.document.pages[currentPageId]
50
-
51
-    const delta = vec.vec(this.origin, point)
52
-
53 58
     const {
54 59
       corners: { a, b },
55 60
       transformType,
56 61
     } = this
57 62
 
58
-    // Edge Transform
63
+    const {
64
+      boundsRotation,
65
+      shapeBounds,
66
+      initialBounds,
67
+      currentPageId,
68
+      selectedIds,
69
+      isSingle,
70
+    } = this.snapshot
71
+
72
+    const { shapes } = data.document.pages[currentPageId]
73
+
74
+    const delta = vec.vec(this.origin, point)
75
+
59 76
     /*
60
-    Edge transform
77
+    Transforms
61 78
     
62 79
     Corners a and b are the original top-left and bottom-right corners of the
63 80
     bounding box. Depending on what the user is dragging, change one or both
@@ -153,8 +170,10 @@ export default class TransformSession extends BaseSession {
153 170
         initialShape,
154 171
         initialShapeBounds,
155 172
         initialBounds,
173
+        boundsRotation,
156 174
         isFlippedX: this.isFlippedX,
157 175
         isFlippedY: this.isFlippedY,
176
+        isSingle,
158 177
         anchor: getTransformAnchor(
159 178
           this.transformType,
160 179
           this.isFlippedX,
@@ -165,8 +184,14 @@ export default class TransformSession extends BaseSession {
165 184
   }
166 185
 
167 186
   cancel(data: Data) {
168
-    const { shapeBounds, initialBounds, currentPageId, selectedIds } =
169
-      this.snapshot
187
+    const {
188
+      shapeBounds,
189
+      boundsRotation,
190
+      initialBounds,
191
+      currentPageId,
192
+      selectedIds,
193
+      isSingle,
194
+    } = this.snapshot
170 195
 
171 196
     const { shapes } = data.document.pages[currentPageId]
172 197
 
@@ -180,8 +205,10 @@ export default class TransformSession extends BaseSession {
180 205
         initialShape,
181 206
         initialShapeBounds,
182 207
         initialBounds,
208
+        boundsRotation,
183 209
         isFlippedX: false,
184 210
         isFlippedY: false,
211
+        isSingle,
185 212
         anchor: getTransformAnchor(this.transformType, false, false),
186 213
       })
187 214
     })
@@ -205,6 +232,7 @@ export function getTransformSnapshot(
205 232
     document: { pages },
206 233
     selectedIds,
207 234
     currentPageId,
235
+    boundsRotation,
208 236
   } = current(data)
209 237
 
210 238
   const pageShapes = pages[currentPageId].shapes
@@ -226,6 +254,8 @@ export function getTransformSnapshot(
226 254
     currentPageId,
227 255
     type: transformType,
228 256
     initialBounds: bounds,
257
+    boundsRotation,
258
+    isSingle: selectedIds.size === 1,
229 259
     selectedIds: new Set(selectedIds),
230 260
     shapeBounds: Object.fromEntries(
231 261
       Array.from(selectedIds.values()).map((id) => {

Loading…
Cancel
Save