1 | /*
|
2 | * Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
|
3 | */
|
4 |
|
5 | /*
|
6 | * This is an extension and not part of the main GoJS library.
|
7 | * Note that the API for this class may change with any version, even point releases.
|
8 | * If you intend to use an extension in production, you should copy the code to your own source directory.
|
9 | * Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
|
10 | * See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
|
11 | */
|
12 |
|
13 | import * as go from '../release/go-module.js';
|
14 |
|
15 | /**
|
16 | * The FreehandDrawingTool allows the user to draw a shape using the mouse.
|
17 | * It collects all of the points from a mouse-down, all mouse-moves, until a mouse-up,
|
18 | * and puts all of those points in a {@link Geometry} used by a {@link Shape}.
|
19 | * It then adds a node data object to the diagram's model.
|
20 | *
|
21 | * This tool may be installed as the first mouse down tool:
|
22 | * ```js
|
23 | * myDiagram.toolManager.mouseDownTools.insertAt(0, new FreehandDrawingTool());
|
24 | * ```
|
25 | *
|
26 | * The Shape used during the drawing operation can be customized by setting {@link #temporaryShape}.
|
27 | * The node data added to the model can be customized by setting {@link #archetypePartData}.
|
28 | *
|
29 | * If you want to experiment with this extension, try the <a href="../../extensionsJSM/FreehandDrawing.html">Freehand Drawing</a> sample.
|
30 | * @category Tool Extension
|
31 | */
|
32 | export class FreehandDrawingTool extends go.Tool {
|
33 | // this is the Shape that is shown during a drawing operation
|
34 | private _temporaryShape: go.GraphObject = go.GraphObject.make(go.Shape, { name: 'SHAPE', fill: null, strokeWidth: 1.5 });
|
35 | private _archetypePartData: go.ObjectData = {}; // the data to copy for a new polyline Part
|
36 | private _isBackgroundOnly: boolean = true; // affects canStart()
|
37 |
|
38 | // the Shape has to be inside a temporary Part that is used during the drawing operation
|
39 | private temp: go.GraphObject = go.GraphObject.make(go.Part, { layerName: 'Tool' }, this._temporaryShape);
|
40 |
|
41 | constructor() {
|
42 | super();
|
43 | this.name = 'FreehandDrawing';
|
44 | }
|
45 |
|
46 | /**
|
47 | * Gets or sets the Shape that is used to hold the line as it is being drawn.
|
48 | *
|
49 | * The default value is a simple Shape drawing an unfilled open thin black line.
|
50 | */
|
51 | get temporaryShape(): go.Shape { return this._temporaryShape as go.Shape; }
|
52 | set temporaryShape(val: go.Shape) {
|
53 | if (this._temporaryShape !== val && val !== null) {
|
54 | val.name = 'SHAPE';
|
55 | const panel = this._temporaryShape.panel;
|
56 | if (panel !== null) {
|
57 | panel.remove(this._temporaryShape);
|
58 | this._temporaryShape = val;
|
59 | panel.add(this._temporaryShape);
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 | /**
|
65 | * Gets or sets the node data object that is copied and added to the model
|
66 | * when the freehand drawing operation completes.
|
67 | */
|
68 | get archetypePartData(): go.ObjectData { return this._archetypePartData; }
|
69 | set archetypePartData(val: go.ObjectData) { this._archetypePartData = val; }
|
70 |
|
71 | /**
|
72 | * Gets or sets whether this tool can only run if the user starts in the diagram's background
|
73 | * rather than on top of an existing Part.
|
74 | *
|
75 | * The default value is true.
|
76 | */
|
77 | get isBackgroundOnly(): boolean { return this._isBackgroundOnly; }
|
78 | set isBackgroundOnly(val: boolean) { this._isBackgroundOnly = val; }
|
79 |
|
80 | /**
|
81 | * Only start if the diagram is modifiable and allows insertions.
|
82 | * OPTIONAL: if the user is starting in the diagram's background, not over an existing Part.
|
83 | */
|
84 | public canStart(): boolean {
|
85 | if (!this.isEnabled) return false;
|
86 | const diagram = this.diagram;
|
87 | if (diagram.isReadOnly || diagram.isModelReadOnly) return false;
|
88 | if (!diagram.allowInsert) return false;
|
89 | // don't include the following check when this tool is running modally
|
90 | if (diagram.currentTool !== this && this.isBackgroundOnly) {
|
91 | // only operates in the background, not on some Part
|
92 | const part = diagram.findPartAt(diagram.lastInput.documentPoint, true);
|
93 | if (part !== null) return false;
|
94 | }
|
95 | return true;
|
96 | }
|
97 |
|
98 | /**
|
99 | * Capture the mouse and use a "crosshair" cursor.
|
100 | */
|
101 | public doActivate(): void {
|
102 | super.doActivate();
|
103 | this.diagram.isMouseCaptured = true;
|
104 | this.diagram.currentCursor = 'crosshair';
|
105 | }
|
106 |
|
107 | /**
|
108 | * Release the mouse and reset the cursor.
|
109 | */
|
110 | public doDeactivate(): void {
|
111 | super.doDeactivate();
|
112 | if (this.temporaryShape !== null && this.temporaryShape.part !== null) {
|
113 | this.diagram.remove(this.temporaryShape.part);
|
114 | }
|
115 | this.diagram.currentCursor = '';
|
116 | this.diagram.isMouseCaptured = false;
|
117 | }
|
118 |
|
119 | /**
|
120 | * This adds a Point to the {@link #temporaryShape}'s geometry.
|
121 | *
|
122 | * If the Shape is not yet in the Diagram, its geometry is initialized and
|
123 | * its parent Part is added to the Diagram.
|
124 | *
|
125 | * If the point is less than half a pixel away from the previous point, it is ignored.
|
126 | */
|
127 | public addPoint(p: go.Point): void {
|
128 | const shape = this.temporaryShape;
|
129 | if (shape === null) return;
|
130 | const part = shape.part;
|
131 | if (part === null) return;
|
132 |
|
133 | // for the temporary Shape, normalize the geometry to be in the viewport
|
134 | const viewpt = this.diagram.viewportBounds.position;
|
135 | const q = new go.Point(p.x - viewpt.x, p.y - viewpt.y);
|
136 |
|
137 | if (part.diagram === null) {
|
138 | const f = new go.PathFigure(q.x, q.y, true); // possibly filled, depending on Shape.fill
|
139 | const g = new go.Geometry().add(f); // the Shape.geometry consists of a single PathFigure
|
140 | shape.geometry = g;
|
141 | // position the Shape's Part, accounting for the strokeWidth
|
142 | part.position = new go.Point(viewpt.x - shape.strokeWidth / 2, viewpt.y - shape.strokeWidth / 2);
|
143 | this.diagram.add(part);
|
144 | }
|
145 |
|
146 | // only add a point if it isn't too close to the last one
|
147 | const geo = shape.geometry;
|
148 | if (geo !== null) {
|
149 | const fig = geo.figures.first();
|
150 | if (fig !== null) {
|
151 | const segs = fig.segments;
|
152 | const idx = segs.count - 1;
|
153 | if (idx >= 0) {
|
154 | const last = segs.elt(idx);
|
155 | if (Math.abs(q.x - last.endX) < 0.5 && Math.abs(q.y - last.endY) < 0.5) return;
|
156 | }
|
157 |
|
158 | // must copy whole Geometry in order to add a PathSegment
|
159 | const geo2 = geo.copy();
|
160 | const fig2 = geo2.figures.first();
|
161 | if (fig2 !== null) {
|
162 | fig2.add(new go.PathSegment(go.PathSegment.Line, q.x, q.y));
|
163 | shape.geometry = geo2;
|
164 | }
|
165 | }
|
166 | }
|
167 | }
|
168 |
|
169 | /**
|
170 | * Start drawing the line by starting to accumulate points in the {@link #temporaryShape}'s geometry.
|
171 | */
|
172 | public doMouseDown(): void {
|
173 | if (!this.isActive) {
|
174 | this.doActivate();
|
175 | // the first point
|
176 | this.addPoint(this.diagram.lastInput.documentPoint);
|
177 | }
|
178 | }
|
179 |
|
180 | /**
|
181 | * Keep accumulating points in the {@link #temporaryShape}'s geometry.
|
182 | */
|
183 | public doMouseMove(): void {
|
184 | if (this.isActive) {
|
185 | this.addPoint(this.diagram.lastInput.documentPoint);
|
186 | }
|
187 | }
|
188 |
|
189 | /**
|
190 | * Finish drawing the line by adding a node data object holding the
|
191 | * geometry string and the node position that the node template can bind to.
|
192 | * This copies the {@link #archetypePartData} and adds it to the model.
|
193 | */
|
194 | public doMouseUp(): void {
|
195 | const diagram = this.diagram;
|
196 | let started = false;
|
197 | if (this.isActive) {
|
198 | started = true;
|
199 | // the last point
|
200 | this.addPoint(diagram.lastInput.documentPoint);
|
201 | // normalize geometry and node position
|
202 | const viewpt = diagram.viewportBounds.position;
|
203 | if (this.temporaryShape.geometry !== null) {
|
204 | const geo = this.temporaryShape.geometry.copy();
|
205 | const pos = geo.normalize();
|
206 | pos.x = viewpt.x - pos.x;
|
207 | pos.y = viewpt.y - pos.y;
|
208 |
|
209 | diagram.startTransaction(this.name);
|
210 | // create the node data for the model
|
211 | const d = diagram.model.copyNodeData(this.archetypePartData);
|
212 | if (d !== null) {
|
213 | // adding data to model creates the actual Part
|
214 | diagram.model.addNodeData(d);
|
215 | const part = diagram.findPartForData(d);
|
216 | if (part !== null) {
|
217 | // assign the location
|
218 | part.location = new go.Point(pos.x + geo.bounds.width / 2, pos.y + geo.bounds.height / 2);
|
219 | // assign the Shape.geometry
|
220 | const shape = part.findObject('SHAPE') as go.Shape;
|
221 | if (shape !== null) shape.geometry = geo;
|
222 | }
|
223 | }
|
224 | }
|
225 | }
|
226 | this.stopTool();
|
227 | if (started) diagram.commitTransaction(this.name);
|
228 | }
|
229 | }
|