swing-ui 0.1.25 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -4
- package/README.md +144 -7
- package/SwingUI.js +2611 -0
- package/classes/Arc.js +185 -0
- package/classes/Button.js +55 -0
- package/classes/CheckBox.js +33 -0
- package/classes/CheckBoxMenuItem.js +24 -0
- package/classes/ComboBox.js +329 -0
- package/classes/ComponentContainer.js +828 -0
- package/classes/ContextMenu.js +141 -0
- package/classes/DocumentEditor.js +168 -0
- package/classes/Ellipse.js +76 -0
- package/classes/InternalWindow.js +397 -0
- package/classes/InternalWindowContainer.js +93 -0
- package/classes/Label.js +52 -0
- package/classes/Line.js +105 -0
- package/classes/List.js +452 -0
- package/classes/MediaPlayer.js +504 -0
- package/classes/Menu.js +74 -0
- package/classes/MenuBar.js +56 -0
- package/classes/MenuItem.js +92 -0
- package/classes/Panel.js +50 -0
- package/classes/PasswordField.js +47 -0
- package/classes/Polygon.js +236 -0
- package/classes/Polyline.js +240 -0
- package/classes/ProgressBar.js +62 -0
- package/classes/RadioButton.js +110 -0
- package/classes/RadioButtonMenuItem.js +106 -0
- package/classes/Rectangle.js +199 -0
- package/classes/ScrollPane.js +53 -0
- package/classes/ScrollableComponent.js +141 -0
- package/classes/Separator.js +24 -0
- package/classes/Shape.js +584 -0
- package/classes/ShapeContainer.js +231 -0
- package/classes/Slider.js +172 -0
- package/classes/Spinner.js +172 -0
- package/classes/SplitView.js +82 -0
- package/classes/Stroke.js +115 -0
- package/classes/Table.js +982 -0
- package/classes/Tabs.js +228 -0
- package/classes/TextArea.js +125 -0
- package/classes/TextComponent.js +207 -0
- package/classes/TextField.js +128 -0
- package/classes/TextShape.js +123 -0
- package/classes/ToggleButton.js +147 -0
- package/classes/ToolBar.js +120 -0
- package/classes/TrayIcon.js +54 -0
- package/classes/Tree.js +569 -0
- package/classes/UIComponent.js +2154 -0
- package/classes/Window.js +609 -0
- package/enums.js +298 -0
- package/fonts.conf +32 -0
- package/functions.js +254 -0
- package/icon.png +0 -0
- package/index.js +109 -4
- package/jar/gui-11.jar +0 -0
- package/jar/lib/flatlaf.jar +0 -0
- package/jar/lib/jna.jar +0 -0
- package/package.json +58 -33
- package/typedefs.js +973 -0
- package/types/SwingUI.d.ts +727 -0
- package/types/classes/Arc.d.ts +111 -0
- package/types/classes/Button.d.ts +38 -0
- package/types/classes/CheckBox.d.ts +22 -0
- package/types/classes/CheckBoxMenuItem.d.ts +18 -0
- package/types/classes/ComboBox.d.ts +172 -0
- package/types/classes/ComponentContainer.d.ts +131 -0
- package/types/classes/ContextMenu.d.ts +74 -0
- package/types/classes/DocumentEditor.d.ts +95 -0
- package/types/classes/Ellipse.d.ts +34 -0
- package/types/classes/InternalWindow.d.ts +163 -0
- package/types/classes/InternalWindowContainer.d.ts +36 -0
- package/types/classes/Label.d.ts +33 -0
- package/types/classes/Line.d.ts +48 -0
- package/types/classes/List.d.ts +223 -0
- package/types/classes/MediaPlayer.d.ts +381 -0
- package/types/classes/Menu.d.ts +43 -0
- package/types/classes/MenuBar.d.ts +26 -0
- package/types/classes/MenuItem.d.ts +57 -0
- package/types/classes/Panel.d.ts +28 -0
- package/types/classes/PasswordField.d.ts +22 -0
- package/types/classes/Polygon.d.ts +93 -0
- package/types/classes/Polyline.d.ts +96 -0
- package/types/classes/ProgressBar.d.ts +36 -0
- package/types/classes/RadioButton.d.ts +70 -0
- package/types/classes/RadioButtonMenuItem.d.ts +71 -0
- package/types/classes/Rectangle.d.ts +82 -0
- package/types/classes/ScrollPane.d.ts +23 -0
- package/types/classes/ScrollableComponent.d.ts +81 -0
- package/types/classes/Separator.d.ts +14 -0
- package/types/classes/Shape.d.ts +223 -0
- package/types/classes/ShapeContainer.d.ts +92 -0
- package/types/classes/Slider.d.ts +107 -0
- package/types/classes/Spinner.d.ts +129 -0
- package/types/classes/SplitView.d.ts +47 -0
- package/types/classes/Stroke.d.ts +99 -0
- package/types/classes/Table.d.ts +652 -0
- package/types/classes/Tabs.d.ts +118 -0
- package/types/classes/TextArea.d.ts +45 -0
- package/types/classes/TextComponent.d.ts +136 -0
- package/types/classes/TextField.d.ts +94 -0
- package/types/classes/TextShape.d.ts +73 -0
- package/types/classes/ToggleButton.d.ts +84 -0
- package/types/classes/ToolBar.d.ts +82 -0
- package/types/classes/TrayIcon.d.ts +43 -0
- package/types/classes/Tree.d.ts +354 -0
- package/types/classes/UIComponent.d.ts +966 -0
- package/types/classes/Window.d.ts +334 -0
- package/types/enums.d.ts +437 -0
- package/types/functions.d.ts +109 -0
- package/types/index.d.ts +188 -0
- package/types/typedefs.d.ts +1626 -0
- package/ui-process-manager.js +317 -0
- package/video-devices-linux.sh +72 -0
- package/video-devices-mac.sh +33 -0
- package/CHANGES.md +0 -3
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @version 2025-09-23
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {UIComponent} from "./UIComponent.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Separator is used to create a separator line between menu items in a menu or between components in a ToolBar.
|
|
9
|
+
* Default orientation is horizontal, but a ToolBar will orient the orientation of this Separator making it in Opposite orientation of the ToolBar.
|
|
10
|
+
*
|
|
11
|
+
* @class
|
|
12
|
+
* @global
|
|
13
|
+
*/
|
|
14
|
+
export class Separator extends UIComponent
|
|
15
|
+
{
|
|
16
|
+
/**
|
|
17
|
+
* Separator constructor. Creates an instance of Separator UI component.
|
|
18
|
+
*/
|
|
19
|
+
constructor()
|
|
20
|
+
{
|
|
21
|
+
super(new.target.name, (new.target === Separator));
|
|
22
|
+
this.finalize(new.target);
|
|
23
|
+
}
|
|
24
|
+
}
|
package/classes/Shape.js
ADDED
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape object that can be added to Panel to be drawn on component Paint
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {Stroke} from "./Stroke.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Shape class is used to create shapes that can be drawn in the background of {@link Window} or {@link Panel} components. This class is usually extended by other shapes. It can be used by itself only if the ` path ` argument is provided.
|
|
9
|
+
*
|
|
10
|
+
* Path of the shape can be generated by adding, subtracting, or intersecting two shapes, then convert the Shape to JSON object using shape.toObject(). Then the resulting object will have a ` path ` property.
|
|
11
|
+
*
|
|
12
|
+
* Check out all the shapes that derive from this Shape class: {@link Arc}, {@link Ellipse}, {@link Line}, {@link Polygon}, {@link Polyline}, {@link Rectangle}, and a {@link Text} that can draw text as a shape.
|
|
13
|
+
*
|
|
14
|
+
* @class
|
|
15
|
+
* @global
|
|
16
|
+
*/
|
|
17
|
+
export class Shape
|
|
18
|
+
{
|
|
19
|
+
#prop = {
|
|
20
|
+
parent: undefined,
|
|
21
|
+
x: 0,
|
|
22
|
+
y: 0,
|
|
23
|
+
opacity: 1.0,
|
|
24
|
+
background: undefined,
|
|
25
|
+
stroke: new Stroke(0),
|
|
26
|
+
angle: 0,
|
|
27
|
+
anchor: {x: 0, y: 0},
|
|
28
|
+
path: undefined,
|
|
29
|
+
visible: true,
|
|
30
|
+
active:true,
|
|
31
|
+
update: true
|
|
32
|
+
};
|
|
33
|
+
_type = "path";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Shape constructor.
|
|
37
|
+
*
|
|
38
|
+
* @param {string|undefined} [path] - Path consisting of points. It can be auto-generated by performing operations of one shape to another, such as add() or subtract().
|
|
39
|
+
* @param {Stroke|null} [stroke] - A border of the shape.
|
|
40
|
+
* @param {string|null} [background] - Color, gradient, image, and background options like in CSS.
|
|
41
|
+
* @param {number} [opacity] - Opacity of the shape as a decimal between 0 and 1.
|
|
42
|
+
* @param {number} [x] - Position of the shape from left edge.
|
|
43
|
+
* @param {number} [y] - Position of the shape from the top.
|
|
44
|
+
* @throws {TypeError} When stroke, opacity or other argument types are invalid.
|
|
45
|
+
*/
|
|
46
|
+
constructor(path = undefined, stroke = undefined, background = undefined, opacity = 1, x = 0, y = 0)
|
|
47
|
+
{
|
|
48
|
+
if(stroke != null && !(stroke instanceof Stroke)) throw new TypeError(`Invalid stroke argument passed. Must be an instance of a Stroke or NULL.`);
|
|
49
|
+
if(!Number.isFinite(opacity)) throw new TypeError(`Invalid shape opacity argument type. Float from 0 to 1 is expected.`);
|
|
50
|
+
if(background != null && typeof background !== "string") throw new TypeError(`Invalid shape background argument type. String or NULL is expected.`);
|
|
51
|
+
|
|
52
|
+
this.#prop.path = path;
|
|
53
|
+
this.#prop.stroke = stroke;
|
|
54
|
+
this.#prop.background = background;
|
|
55
|
+
this.#prop.opacity = opacity;
|
|
56
|
+
this.#prop.x = Number.isFinite(x) ? x : 0;
|
|
57
|
+
this.#prop.y = Number.isFinite(y) ? y : 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Add this shape to a container.
|
|
62
|
+
*
|
|
63
|
+
* @param {ShapeContainer} container - Panel or Window component.
|
|
64
|
+
* @param {number} index - Optional index to insert the shape.
|
|
65
|
+
* @returns {Shape} Returns `this` Shape instance for chaining.
|
|
66
|
+
* @throws {TypeError} When a container is not Panel or Window component.
|
|
67
|
+
* @throws {Error} When specified index is out of bounds.
|
|
68
|
+
* @see ShapeComponent#addShape
|
|
69
|
+
* @see Shape#remove
|
|
70
|
+
*/
|
|
71
|
+
addTo(container, index = -1)
|
|
72
|
+
{
|
|
73
|
+
// validate new parent
|
|
74
|
+
if(!(container instanceof Panel || container instanceof Window)) throw new TypeError(`Invalid argument. Shape container must be a Panel or Window component.`);
|
|
75
|
+
if(index > container._shapes.length) throw new Error("Index is out of bounds");
|
|
76
|
+
|
|
77
|
+
// if already a child at same index, then skip it
|
|
78
|
+
if(index > -1 && this.#prop.parent === container && container.shape(index) === this) return this;
|
|
79
|
+
|
|
80
|
+
this.remove();
|
|
81
|
+
|
|
82
|
+
this.#prop.parent = container;
|
|
83
|
+
|
|
84
|
+
container.addShape(this, index);
|
|
85
|
+
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Removes this Shape from its container component.
|
|
91
|
+
*
|
|
92
|
+
* @returns {Shape} `this` Shape container for chaining.
|
|
93
|
+
* @see ShapeComponent#removeComponent
|
|
94
|
+
* @see Shape#addTo
|
|
95
|
+
*/
|
|
96
|
+
remove()
|
|
97
|
+
{
|
|
98
|
+
if(this.#prop.parent !== undefined)
|
|
99
|
+
{
|
|
100
|
+
const p = this.#prop.parent;
|
|
101
|
+
this.#prop.parent = undefined;
|
|
102
|
+
p.removeShape(this);
|
|
103
|
+
}
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Returns current parent component (Window or Panel), or `undefined` if the shape isn't added to any component.
|
|
109
|
+
*
|
|
110
|
+
* @readonly
|
|
111
|
+
* @type {Window|Panel|undefined}
|
|
112
|
+
*/
|
|
113
|
+
get parent()
|
|
114
|
+
{
|
|
115
|
+
return this.#prop.parent;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Sets or gets a {@link Stroke} of the shape.
|
|
120
|
+
*
|
|
121
|
+
* @param {Stroke} [stroke] - Stroke object
|
|
122
|
+
* @return {Stroke|this} When called without parameters, current `Stroke` is returned; otherwise this `Shape` is returned for method chaining.
|
|
123
|
+
*/
|
|
124
|
+
stroke(...args)
|
|
125
|
+
{
|
|
126
|
+
if(args.length > 0)
|
|
127
|
+
{
|
|
128
|
+
if(!(args[0] instanceof Stroke)) throw new Error(`Invalid stroke argument. It must be of Stroke type.`); // validate stroke input
|
|
129
|
+
|
|
130
|
+
this.#prop.stroke = args[0];
|
|
131
|
+
this.update("stroke");
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
else
|
|
135
|
+
{
|
|
136
|
+
return this.#prop.stroke;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Returns a JSON object that contains shape properties.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} [include="all"] - Specified what to include. "all" is default. Valid values are: "all", "stroke", "background", "points", "basic". Each shape that derives from this `Shape` class may also have it's own values.
|
|
144
|
+
* @return {Shape} Returns this `Shape` for method chaining.
|
|
145
|
+
*/
|
|
146
|
+
toObject(include = "all")
|
|
147
|
+
{
|
|
148
|
+
let shape = {};
|
|
149
|
+
|
|
150
|
+
if(include === "all") shape.type = this.#prop.path !== undefined ? "path" : this._type;
|
|
151
|
+
if(["all", "location"].includes(include))
|
|
152
|
+
{
|
|
153
|
+
shape.x = this.#prop.x;
|
|
154
|
+
shape.y = this.#prop.y;
|
|
155
|
+
}
|
|
156
|
+
if(["all", "opacity"].includes(include)) shape.opacity = this.#prop.opacity;
|
|
157
|
+
if(["all", "stroke"].includes(include) && this.#prop.stroke instanceof Stroke) shape.stroke = this.#prop.stroke.toObject();
|
|
158
|
+
if(["all", "rotation"].includes(include))
|
|
159
|
+
{
|
|
160
|
+
shape.angle = this.#prop.angle;
|
|
161
|
+
shape.anchor = this.#prop.anchor;
|
|
162
|
+
}
|
|
163
|
+
if(["all", "path"].includes(include) && this.#prop.path !== undefined) shape.path = this.#prop.path;
|
|
164
|
+
if(["all", "background"].includes(include)) shape.background = this.#prop.background;
|
|
165
|
+
if(["all", "visible"].includes(include)) shape.visible = this.#prop.visible;
|
|
166
|
+
if(["all", "active"].includes(include)) shape.active = this.#prop.active;
|
|
167
|
+
|
|
168
|
+
return shape;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Converts JSON object back to Shape. To convert Shape to object use Shape.toObject() function.
|
|
173
|
+
*
|
|
174
|
+
* @param {object} object - An object that has properties to recreate a Shape object.
|
|
175
|
+
* @returns {Shape} Returns a new instance of `Shape`.
|
|
176
|
+
*/
|
|
177
|
+
static fromObject(object)
|
|
178
|
+
{
|
|
179
|
+
const shape = new Shape(object.path ?? undefined, Stroke.fromObject(object.stroke) ?? new Stroke(0), object.background ?? undefined, object.opacity ?? 1, object.x ?? 0, object.y ?? 0);
|
|
180
|
+
|
|
181
|
+
shape._type = object.type ?? "path";
|
|
182
|
+
shape.#prop.angle = object.angle ?? 0;
|
|
183
|
+
shape.#prop.anchor = object.anchor ?? 0;
|
|
184
|
+
|
|
185
|
+
if(object._points !== undefined) object._points = object.points;
|
|
186
|
+
if(object.size !== undefined) shape._size = object.size;
|
|
187
|
+
if(object.hole_size !== undefined) shape._hole_size = object.hole_size;
|
|
188
|
+
if(object.start_angle !== undefined) shape._start_angle = object.start_angle;
|
|
189
|
+
if(object.extent !== undefined) shape._extent = object.extent;
|
|
190
|
+
if(object.arc_type !== undefined) shape._arc_type = object.arc_type;
|
|
191
|
+
if(object.arc_size !== undefined) shape._arc_size = object.arc_size;
|
|
192
|
+
if(object.text !== undefined) shape._text = object.text;
|
|
193
|
+
if(object.font !== undefined) shape._font = object.font;
|
|
194
|
+
|
|
195
|
+
return shape;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Updates properties of this Shape. It's called by other methods of Shape object when shape properties change.
|
|
200
|
+
*
|
|
201
|
+
* @param {string|boolean} [what="all"] - What to update: "all", "min", "stroke", "background", "points". Shapes that derive from this class may also have their own values. Pass a `boolean` to enable and disable this shape redraw. Before making multiple changes to the Shape it helps to disable redraw, make the changes, then enable redraw.
|
|
202
|
+
* @returns {void}
|
|
203
|
+
* @internal
|
|
204
|
+
* @ignore
|
|
205
|
+
*/
|
|
206
|
+
update(what = "all")
|
|
207
|
+
{
|
|
208
|
+
if(typeof what === "boolean")
|
|
209
|
+
{
|
|
210
|
+
if(what === this.#prop.update) return;
|
|
211
|
+
else
|
|
212
|
+
{
|
|
213
|
+
this.#prop.update = what;
|
|
214
|
+
if(what) what = "all";
|
|
215
|
+
else return;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if(this.#prop.parent === undefined) return;
|
|
220
|
+
|
|
221
|
+
let i = this.#prop.parent.shapeIndex(this);
|
|
222
|
+
|
|
223
|
+
if(i < 0) throw new Error(`This Shape "${this._type}" doesn't belongs to this component "${this.#prop.parent.constructor.name}".`);
|
|
224
|
+
|
|
225
|
+
let obj = this.toObject(what);
|
|
226
|
+
obj.index = i;
|
|
227
|
+
|
|
228
|
+
ui.sendToGUI({control: this.#prop.parent.id, set: "update_shape", value: obj});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Sets or gets location of the shape relative to top-left corner of its parent component.
|
|
233
|
+
*
|
|
234
|
+
* @param {number|Point} [x] - X position integer or a Point position object (e.g., {x:10,y:40}).
|
|
235
|
+
* @param {number} [y] - Y position integer.
|
|
236
|
+
* @return {Point|this} If called without arguments, current position object is returned; otherwise this shape is returned for method chaining.
|
|
237
|
+
* @see Point
|
|
238
|
+
*/
|
|
239
|
+
position(...args)
|
|
240
|
+
{
|
|
241
|
+
if(args.length > 0)
|
|
242
|
+
{
|
|
243
|
+
if(typeof args[0] === "object")
|
|
244
|
+
{
|
|
245
|
+
if(typeof args[0].x === "number" && Number.isFinite(args[0].x)) this.#prop.x = args[0].x;
|
|
246
|
+
if(typeof args[0].y === "number" && Number.isFinite(args[0].y)) this.#prop.y = args[0].y;
|
|
247
|
+
}
|
|
248
|
+
else
|
|
249
|
+
{
|
|
250
|
+
if(typeof args[0] === "number" && Number.isFinite(args[0])) this.#prop.x = args[0];
|
|
251
|
+
if(typeof args[1] === "number" && Number.isFinite(args[1])) this.#prop.y = args[1];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
this.update("location");
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
else
|
|
258
|
+
{
|
|
259
|
+
return {x: this.#prop.x, y: this.#prop.y};
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Moves the Shape by adding the specified x and y values to its current position.
|
|
265
|
+
* The function shifts the element's position relative to current position.
|
|
266
|
+
* If arguments are invalid integer values, the Shape will not be moved.
|
|
267
|
+
*
|
|
268
|
+
* @param {number} [dx=0] - Delta x (can be positive or negative number)
|
|
269
|
+
* @param {number} [dy=0] - Delta y (can be positive or negative number)
|
|
270
|
+
* @return {Shape} Returns `this` Shape instance.
|
|
271
|
+
*/
|
|
272
|
+
move(dx, dy)
|
|
273
|
+
{
|
|
274
|
+
if(!Number.isFinite(dx) || !Number.isFinite(dy)) return this;
|
|
275
|
+
return this.position(this.#prop.x + dx, this.#prop.y + dy);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Sets or gets X position of the shape relative to left edge of its parent component.
|
|
280
|
+
*
|
|
281
|
+
* @param {number} [x] - X position integer.
|
|
282
|
+
* @return {number|this} If called without arguments, current X position is returned; otherwise this shape is returned for method chaining.
|
|
283
|
+
* @see Shape#position
|
|
284
|
+
*/
|
|
285
|
+
x()
|
|
286
|
+
{
|
|
287
|
+
if(arguments.length > 0)
|
|
288
|
+
{
|
|
289
|
+
if(typeof arguments[0] === "number" && Number.isFinite(arguments[0]))
|
|
290
|
+
{
|
|
291
|
+
this.#prop.x = arguments[0];
|
|
292
|
+
this.update("location");
|
|
293
|
+
}
|
|
294
|
+
return this;
|
|
295
|
+
}
|
|
296
|
+
else
|
|
297
|
+
{
|
|
298
|
+
return this.#prop.x;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Sets or gets Y position of the shape relative to top edge of its parent component (for Window parent component gets Y position from the top of content area).
|
|
304
|
+
*
|
|
305
|
+
* @param {number} [y] - Y position integer.
|
|
306
|
+
* @return {number|this} If called without arguments, current Y position is returned; otherwise this shape is returned for method chaining.
|
|
307
|
+
* @see Shape#position
|
|
308
|
+
*/
|
|
309
|
+
y()
|
|
310
|
+
{
|
|
311
|
+
if(arguments.length > 0)
|
|
312
|
+
{
|
|
313
|
+
if(typeof arguments[0] === "number" && Number.isFinite(arguments[0]))
|
|
314
|
+
{
|
|
315
|
+
this.#prop.y = arguments[0];
|
|
316
|
+
this.update("location");
|
|
317
|
+
}
|
|
318
|
+
return this;
|
|
319
|
+
}
|
|
320
|
+
else
|
|
321
|
+
{
|
|
322
|
+
return this.#prop.y;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Gets or sets the opacity (transparency) of the shape.
|
|
328
|
+
*
|
|
329
|
+
* @param {number} [opacity] - Decimal value between 0 and 1 (inclusive).
|
|
330
|
+
* @returns {number|this} If called without arguments, returns the current opacity; otherwise, returns this shape for method chaining.
|
|
331
|
+
*/
|
|
332
|
+
opacity()
|
|
333
|
+
{
|
|
334
|
+
if(arguments.length > 0)
|
|
335
|
+
{
|
|
336
|
+
this.#prop.opacity = parseFloat(arguments[0]);
|
|
337
|
+
this.update("opacity");
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return this.#prop.opacity;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Gets or sets the rotation angle of the shape around its anchor.
|
|
346
|
+
*
|
|
347
|
+
* Use the `anchor()` method to specify the anchor position relative to the shape.
|
|
348
|
+
*
|
|
349
|
+
* The default anchor is set to the top-left corner of the shape's bounding rectangle (even for non-rectangular shapes).
|
|
350
|
+
*
|
|
351
|
+
* @param {number} [angle] - Rotation angle in degrees. Values below 0 or above 360 are normalized to the range 0–360 using the modulus operator (%).
|
|
352
|
+
* @return {number|this} If called without arguments, returns the current value; otherwise this shape is returned for method chaining.
|
|
353
|
+
* @see Shape#anchor
|
|
354
|
+
*/
|
|
355
|
+
angle(angle = undefined)
|
|
356
|
+
{
|
|
357
|
+
if(angle === undefined) return this.#prop.angle;
|
|
358
|
+
|
|
359
|
+
if(typeof angle !== "number") return this;
|
|
360
|
+
|
|
361
|
+
if(angle !== this.#prop.angle)
|
|
362
|
+
{
|
|
363
|
+
if(angle !== 0)
|
|
364
|
+
{
|
|
365
|
+
angle = angle % 360;
|
|
366
|
+
if(angle < 0) angle += 360; // for performance do this with IF
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
this.#prop.angle = angle;
|
|
370
|
+
|
|
371
|
+
this.update("rotation");
|
|
372
|
+
}
|
|
373
|
+
return this;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Sets or gets the anchor position relative to the shape, around which the shape can be rotated using the `angle()` method.
|
|
378
|
+
*
|
|
379
|
+
* The default anchor is at the top-left corner of the shape's bounding rectangle (even for non-rectangular shapes).
|
|
380
|
+
*
|
|
381
|
+
* @param {Point} [anchor] - Anchor point around which the shape rotates when the rotation angle is not 0.
|
|
382
|
+
* @returns {Point|this} If called without arguments, returns the current anchor; otherwise, returns this shape for method chaining.
|
|
383
|
+
* @see Point
|
|
384
|
+
* @see Shape#angle
|
|
385
|
+
*/
|
|
386
|
+
anchor()
|
|
387
|
+
{
|
|
388
|
+
if(arguments.length > 0)
|
|
389
|
+
{
|
|
390
|
+
let anchor = (arguments.length === 2) ? {x: arguments[0], y: arguments[1]} : arguments[0];
|
|
391
|
+
|
|
392
|
+
if(typeof anchor.x === "number") this.#prop.anchor.x = anchor.x;
|
|
393
|
+
if(typeof anchor.y === "number") this.#prop.anchor.y = anchor.y;
|
|
394
|
+
this.update("rotation");
|
|
395
|
+
return this;
|
|
396
|
+
}
|
|
397
|
+
else
|
|
398
|
+
{
|
|
399
|
+
return this.#prop.anchor;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Sets or gets the background of the shape.
|
|
405
|
+
*
|
|
406
|
+
* The `background` value can be a string that contains a mixture of background color, image path (relative or absolute), base64 encoded image string, linear-gradient, radial-gradient, like in CSS in web browsers. Some features may not be supported though.
|
|
407
|
+
* Background settings like position, image stretching or repeating, opacity and more are supported.
|
|
408
|
+
*
|
|
409
|
+
* See examples below.
|
|
410
|
+
*
|
|
411
|
+
* @param {string} [background] - Background string. If omitted, returns the current value.
|
|
412
|
+
* @returns {this|string} If called without parameters, returns this shape instance; otherwise returns current background.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* shape.background("#FF000055 url(images/bg.png) center no-repeat opacity=0.6 10px 15px linear-gradient(#DDDDDD55, #F8F8F855 10%) / auto);
|
|
416
|
+
* panel.background("url(data:image/png;base64,As8fE4fdt1r.....)")); // supports base64 encoded image string
|
|
417
|
+
*/
|
|
418
|
+
background()
|
|
419
|
+
{
|
|
420
|
+
if(arguments.length > 0)
|
|
421
|
+
{
|
|
422
|
+
this.#prop.background = arguments[0];
|
|
423
|
+
this.update("background");
|
|
424
|
+
return this;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return this.#prop.background;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Returns the size of a shape. Does not set a new size. Some shapes override this function and size can be set for those shapes. (e.g. Rectangle, Ellipse)
|
|
432
|
+
*
|
|
433
|
+
* @param {Size} [size] - Size formatted object, but only for shapes that allow setting new size, such as Rectangle or Ellipse.
|
|
434
|
+
* @returns {Size|this} If called without parameters, returns current size, or `null` if the shape is not added to a component. When setting size, this shape instance is returned for method chaining.
|
|
435
|
+
* @throws Error When the shape is not added to a container.
|
|
436
|
+
* @see Size
|
|
437
|
+
*/
|
|
438
|
+
size(...args)
|
|
439
|
+
{
|
|
440
|
+
if(args.length > 0) throw new Error(`${this.constructor.name} doesn't allow setting size(...). For ${this.constructor.name} you can only use size() to get current size of this shape.`);
|
|
441
|
+
if(this.#prop.parent === undefined) throw new Error(`Can't get shape size before the shape is added to a container.`);
|
|
442
|
+
|
|
443
|
+
if(args.length === 0)
|
|
444
|
+
{
|
|
445
|
+
const index = this.#prop.parent.shapeIndex(this);
|
|
446
|
+
return ui.sendToGUI({control: this.#prop.parent.id, get: "shapeSize", data: index});
|
|
447
|
+
}
|
|
448
|
+
else
|
|
449
|
+
{
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Sets or gets whether the shape is visible.
|
|
456
|
+
*
|
|
457
|
+
* @param {boolean} [visible] - `true` to show, `false` to hide.
|
|
458
|
+
* @return {boolean|Shape} When setting a value, this shape is returned; otherwise current value is returned.
|
|
459
|
+
*/
|
|
460
|
+
visible(...args)
|
|
461
|
+
{
|
|
462
|
+
if(args.length === 0) return this.#prop.visible;
|
|
463
|
+
|
|
464
|
+
args[0] = args[0] === true;
|
|
465
|
+
|
|
466
|
+
if(args[0] !== this.#prop.visible)
|
|
467
|
+
{
|
|
468
|
+
this.#prop.visible = args[0];
|
|
469
|
+
this.update("visible");
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Sets or gets whether this shape is active (`true` by default).
|
|
477
|
+
*
|
|
478
|
+
* An inactive shape (`active(false)`) does not produce events `mousedown`, `mouseup`, `click`, `mouseenter`, `mouseleave`, `mousemove` on {@link Panel#onShapeEvents}, and is skipped during mouse hit-testing. This means the mouse "passes through" the shape as if it weren't there — if another shape lies beneath it, that shape is considered to be under the mouse instead.
|
|
479
|
+
*
|
|
480
|
+
* Settings active to `false` may be useful for purely decorative shapes (e.g. background fills, watermarks, static overlays) that shouldn't intercept or respond to mouse interaction.
|
|
481
|
+
*
|
|
482
|
+
* @param {boolean} [active] - `false` to make the shape inactive (ignored by shape events and mouse hit-testing), `true` to restore normal behavior. Omit to get the current value.
|
|
483
|
+
* @return {boolean|Shape} When called without arguments, returns the current value; otherwise returns this `Shape` instance for method chaining.
|
|
484
|
+
* @default true
|
|
485
|
+
* @see Panel#onShapeEvents
|
|
486
|
+
* @see ShapeEvent
|
|
487
|
+
*/
|
|
488
|
+
active(...args)
|
|
489
|
+
{
|
|
490
|
+
if(args.length === 0) return this.#prop.active;
|
|
491
|
+
|
|
492
|
+
args[0] = args[0] === true;
|
|
493
|
+
|
|
494
|
+
if(args[0] !== this.#prop.active)
|
|
495
|
+
{
|
|
496
|
+
this.#prop.active = args[0];
|
|
497
|
+
this.update("active");
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return this;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Adds another shape to this shape. Stroke of this shape is kept (stroke of another shape is disregarded).
|
|
505
|
+
*
|
|
506
|
+
* @param {Shape} shape - Another shape object that would be added to this shape.
|
|
507
|
+
* @returns {this} Returns this shape for method chaining.
|
|
508
|
+
*/
|
|
509
|
+
add(shape)
|
|
510
|
+
{
|
|
511
|
+
if(!(shape instanceof Shape)) return false;
|
|
512
|
+
this.#prop.path = ui.sendToGUI({
|
|
513
|
+
java: "shapeOperation",
|
|
514
|
+
operation: "Add",
|
|
515
|
+
shape: this.toObject(),
|
|
516
|
+
otherShape: shape.toObject()
|
|
517
|
+
}, r => {return r.value;});
|
|
518
|
+
this._type = "path";
|
|
519
|
+
this.update("all");
|
|
520
|
+
return this;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Removes an area of a specified shape from this shape. Stroke of this shape is kept (stroke of another shape is disregarded).
|
|
525
|
+
*
|
|
526
|
+
* @param {Shape} shape - Another shape object that would be subtracted from this shape.
|
|
527
|
+
* @returns {this} Returns this shape for method chaining.
|
|
528
|
+
*/
|
|
529
|
+
subtract(shape)
|
|
530
|
+
{
|
|
531
|
+
if(!(shape instanceof Shape)) return false;
|
|
532
|
+
this.#prop.path = ui.sendToGUI({
|
|
533
|
+
java: "shapeOperation",
|
|
534
|
+
operation: "Subtract",
|
|
535
|
+
shape: this.toObject(),
|
|
536
|
+
otherShape: shape.toObject()
|
|
537
|
+
}, r => {return r.value;});
|
|
538
|
+
this._type = "path";
|
|
539
|
+
this.update("all");
|
|
540
|
+
return this;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Remove an area from this shape where this shape and provided shape don't intersect, leaving only area where these shapes do intersect. Stroke of this shape is kept (stroke of another shape is disregarded).
|
|
545
|
+
*
|
|
546
|
+
* @param {Shape} shape - Another shape object.
|
|
547
|
+
* @returns {this} Returns this shape for method chaining.
|
|
548
|
+
*/
|
|
549
|
+
intersect(shape)
|
|
550
|
+
{
|
|
551
|
+
if(!(shape instanceof Shape)) return false;
|
|
552
|
+
this.#prop.path = ui.sendToGUI({
|
|
553
|
+
java: "shapeOperation",
|
|
554
|
+
operation: "Intersect",
|
|
555
|
+
shape: this.toObject(),
|
|
556
|
+
otherShape: shape.toObject()
|
|
557
|
+
}, r => {return r.value;});
|
|
558
|
+
this._type = "path";
|
|
559
|
+
this.update("all");
|
|
560
|
+
return this;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Checks if this shape intersects (touches) with another shape. Both of these shapes must be added to the same component for this to work.
|
|
565
|
+
*
|
|
566
|
+
* @param {Shape} shape - Intercepting shape
|
|
567
|
+
* @return {boolean|null} Returns boolean on successful check or `null` if the check failed to be performed.
|
|
568
|
+
*/
|
|
569
|
+
intersects(shape)
|
|
570
|
+
{
|
|
571
|
+
if(this.#prop.parent === undefined || this.#prop.parent !== shape.parent) return false;
|
|
572
|
+
|
|
573
|
+
const i1 = this.#prop.parent.shapeIndex(this);
|
|
574
|
+
const i2 = this.#prop.parent.shapeIndex(shape);
|
|
575
|
+
|
|
576
|
+
if(i1 === -1 || i2 === -1) return false;
|
|
577
|
+
|
|
578
|
+
return ui.sendToGUI({
|
|
579
|
+
control: this.#prop.parent.id,
|
|
580
|
+
get: "shapeIntersects",
|
|
581
|
+
data: {i1: i1, i2: i2}
|
|
582
|
+
}, r => {return r.value;});
|
|
583
|
+
}
|
|
584
|
+
}
|