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