Browse Source

Adds undo for drawn shapes, zoom scales rdp

main
Steve Ruiz 4 years ago
parent
commit
bd02ca4fd9

+ 0
- 1
lib/code/control.ts View File

5
   VectorCodeControl,
5
   VectorCodeControl,
6
 } from "types"
6
 } from "types"
7
 import { v4 as uuid } from "uuid"
7
 import { v4 as uuid } from "uuid"
8
-import Vector from "./vector"
9
 
8
 
10
 export const controls: Record<string, any> = {}
9
 export const controls: Record<string, any> = {}
11
 
10
 

+ 40
- 0
state/commands/draw.ts View File

1
+import Command from "./command"
2
+import history from "../history"
3
+import { Data } from "types"
4
+import { getPage } from "utils/utils"
5
+import { getShapeUtils } from "lib/shape-utils"
6
+import { current } from "immer"
7
+
8
+export default function drawCommand(
9
+  data: Data,
10
+  id: string,
11
+  before: number[][],
12
+  after: number[][]
13
+) {
14
+  const selectedIds = Array.from(data.selectedIds.values())
15
+  const restoreShape = current(getPage(data).shapes[id])
16
+  getShapeUtils(restoreShape).setPoints!(restoreShape, after)
17
+
18
+  history.execute(
19
+    data,
20
+    new Command({
21
+      name: "set_points",
22
+      category: "canvas",
23
+      manualSelection: true,
24
+      do(data, initial) {
25
+        if (!initial) {
26
+          getPage(data).shapes[id] = restoreShape
27
+        }
28
+
29
+        data.selectedIds.clear()
30
+      },
31
+      undo(data) {
32
+        delete getPage(data).shapes[id]
33
+        data.selectedIds.clear()
34
+        for (let id of selectedIds) {
35
+          data.selectedIds.add(id)
36
+        }
37
+      },
38
+    })
39
+  )
40
+}

+ 2
- 2
state/commands/index.ts View File

4
 import distribute from "./distribute"
4
 import distribute from "./distribute"
5
 import generate from "./generate"
5
 import generate from "./generate"
6
 import move from "./move"
6
 import move from "./move"
7
-import points from "./points"
7
+import draw from "./draw"
8
 import rotate from "./rotate"
8
 import rotate from "./rotate"
9
 import stretch from "./stretch"
9
 import stretch from "./stretch"
10
 import style from "./style"
10
 import style from "./style"
19
   distribute,
19
   distribute,
20
   generate,
20
   generate,
21
   move,
21
   move,
22
-  points,
22
+  draw,
23
   rotate,
23
   rotate,
24
   stretch,
24
   stretch,
25
   style,
25
   style,

+ 0
- 28
state/commands/points.ts View File

1
-import Command from "./command"
2
-import history from "../history"
3
-import { Data } from "types"
4
-import { getPage } from "utils/utils"
5
-import { getShapeUtils } from "lib/shape-utils"
6
-
7
-export default function pointsCommand(
8
-  data: Data,
9
-  id: string,
10
-  before: number[][],
11
-  after: number[][]
12
-) {
13
-  history.execute(
14
-    data,
15
-    new Command({
16
-      name: "set_points",
17
-      category: "canvas",
18
-      do(data) {
19
-        const shape = getPage(data).shapes[id]
20
-        getShapeUtils(shape).setPoints!(shape, after)
21
-      },
22
-      undo(data) {
23
-        const shape = getPage(data).shapes[id]
24
-        getShapeUtils(shape).setPoints!(shape, before)
25
-      },
26
-    })
27
-  )
28
-}

+ 8
- 4
state/sessions/draw-session.ts View File

8
 
8
 
9
 export default class BrushSession extends BaseSession {
9
 export default class BrushSession extends BaseSession {
10
   origin: number[]
10
   origin: number[]
11
+  previous: number[]
11
   points: number[][]
12
   points: number[][]
12
   snapshot: DrawSnapshot
13
   snapshot: DrawSnapshot
13
   shapeId: string
14
   shapeId: string
16
     super(data)
17
     super(data)
17
     this.shapeId = id
18
     this.shapeId = id
18
     this.origin = point
19
     this.origin = point
19
-    this.points = [[0, 0]]
20
+    this.previous = point
21
+    this.points = []
20
     this.snapshot = getDrawSnapshot(data, id)
22
     this.snapshot = getDrawSnapshot(data, id)
21
 
23
 
22
     const page = getPage(data)
24
     const page = getPage(data)
27
   update = (data: Data, point: number[]) => {
29
   update = (data: Data, point: number[]) => {
28
     const { shapeId } = this
30
     const { shapeId } = this
29
 
31
 
30
-    this.points.push(vec.sub(point, this.origin))
32
+    const lp = vec.med(this.previous, point)
33
+    this.points.push(vec.sub(lp, this.origin))
34
+    this.previous = lp
31
 
35
 
32
     const page = getPage(data)
36
     const page = getPage(data)
33
     const shape = page.shapes[shapeId]
37
     const shape = page.shapes[shapeId]
42
   }
46
   }
43
 
47
 
44
   complete = (data: Data) => {
48
   complete = (data: Data) => {
45
-    commands.points(
49
+    commands.draw(
46
       data,
50
       data,
47
       this.shapeId,
51
       this.shapeId,
48
       this.snapshot.points,
52
       this.snapshot.points,
49
-      simplify(this.points, 1).map(([x, y]) => [
53
+      simplify(this.points, 0.1 / data.camera.zoom).map(([x, y]) => [
50
         Math.trunc(x * 100) / 100,
54
         Math.trunc(x * 100) / 100,
51
         Math.trunc(y * 100) / 100,
55
         Math.trunc(y * 100) / 100,
52
       ])
56
       ])

+ 1
- 2
state/state.ts View File

31
   DistributeType,
31
   DistributeType,
32
   AlignType,
32
   AlignType,
33
   StretchType,
33
   StretchType,
34
-  DrawShape,
35
 } from "types"
34
 } from "types"
36
 
35
 
37
 const initialData: Data = {
36
 const initialData: Data = {
928
     },
927
     },
929
 
928
 
930
     restoreSavedData(data) {
929
     restoreSavedData(data) {
931
-      history.load(data)
930
+      // history.load(data)
932
     },
931
     },
933
 
932
 
934
     clearBoundsRotation(data) {
933
     clearBoundsRotation(data) {

Loading…
Cancel
Save