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 ResizeMultipleTool class lets the user resize multiple objects at once.
|
17 | *
|
18 | * If you want to experiment with this extension, try the <a href="../../extensionsJSM/ResizeMultiple.html">Resize Multiple</a> sample.
|
19 | * @category Tool Extension
|
20 | */
|
21 | export class ResizeMultipleTool extends go.ResizingTool {
|
22 | /**
|
23 | * Constructs a ResizeMultipleTool and sets the name for the tool.
|
24 | */
|
25 | constructor() {
|
26 | super();
|
27 | this.name = 'ResizeMultiple';
|
28 | }
|
29 |
|
30 | /**
|
31 | * Overrides {@link ResizingTool#resize} to resize all selected objects to the same size.
|
32 | * @param {Rect} newr the intended new rectangular bounds for each Part's {@link Part#resizeObject}.
|
33 | */
|
34 | public resize(newr: go.Rect): void {
|
35 | const diagram = this.diagram;
|
36 | diagram.selection.each(function(part) {
|
37 | if (part instanceof go.Link) return; // only Nodes and simple Parts
|
38 | const obj = part.resizeObject;
|
39 |
|
40 | // calculate new location
|
41 | const pos = part.position.copy();
|
42 | const angle = obj.getDocumentAngle();
|
43 | const sc = obj.getDocumentScale();
|
44 |
|
45 | const radAngle = Math.PI * angle / 180;
|
46 | const angleCos = Math.cos(radAngle);
|
47 | const angleSin = Math.sin(radAngle);
|
48 |
|
49 | const deltaWidth = newr.width - obj.naturalBounds.width;
|
50 | const deltaHeight = newr.height - obj.naturalBounds.height;
|
51 |
|
52 | const angleRight = (angle > 270 || angle < 90) ? 1 : 0;
|
53 | const angleBottom = (angle > 0 && angle < 180) ? 1 : 0;
|
54 | const angleLeft = (angle > 90 && angle < 270) ? 1 : 0;
|
55 | const angleTop = (angle > 180 && angle < 360) ? 1 : 0;
|
56 |
|
57 | pos.x += sc * ((newr.x + deltaWidth * angleLeft) * angleCos - (newr.y + deltaHeight * angleBottom) * angleSin);
|
58 | pos.y += sc * ((newr.x + deltaWidth * angleTop) * angleSin + (newr.y + deltaHeight * angleLeft) * angleCos);
|
59 |
|
60 | obj.desiredSize = newr.size;
|
61 | part.position = pos;
|
62 | });
|
63 | }
|
64 |
|
65 | }
|