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

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

@@ -0,0 +1,40 @@
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,7 +4,7 @@ import direct from "./direct"
4 4
 import distribute from "./distribute"
5 5
 import generate from "./generate"
6 6
 import move from "./move"
7
-import points from "./points"
7
+import draw from "./draw"
8 8
 import rotate from "./rotate"
9 9
 import stretch from "./stretch"
10 10
 import style from "./style"
@@ -19,7 +19,7 @@ const commands = {
19 19
   distribute,
20 20
   generate,
21 21
   move,
22
-  points,
22
+  draw,
23 23
   rotate,
24 24
   stretch,
25 25
   style,

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

@@ -1,28 +0,0 @@
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,6 +8,7 @@ import commands from "state/commands"
8 8
 
9 9
 export default class BrushSession extends BaseSession {
10 10
   origin: number[]
11
+  previous: number[]
11 12
   points: number[][]
12 13
   snapshot: DrawSnapshot
13 14
   shapeId: string
@@ -16,7 +17,8 @@ export default class BrushSession extends BaseSession {
16 17
     super(data)
17 18
     this.shapeId = id
18 19
     this.origin = point
19
-    this.points = [[0, 0]]
20
+    this.previous = point
21
+    this.points = []
20 22
     this.snapshot = getDrawSnapshot(data, id)
21 23
 
22 24
     const page = getPage(data)
@@ -27,7 +29,9 @@ export default class BrushSession extends BaseSession {
27 29
   update = (data: Data, point: number[]) => {
28 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 36
     const page = getPage(data)
33 37
     const shape = page.shapes[shapeId]
@@ -42,11 +46,11 @@ export default class BrushSession extends BaseSession {
42 46
   }
43 47
 
44 48
   complete = (data: Data) => {
45
-    commands.points(
49
+    commands.draw(
46 50
       data,
47 51
       this.shapeId,
48 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 54
         Math.trunc(x * 100) / 100,
51 55
         Math.trunc(y * 100) / 100,
52 56
       ])

+ 1
- 2
state/state.ts View File

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

Loading…
Cancel
Save