perfect-gui 4.12.9 → 5.0.0

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.
@@ -1,89 +1,152 @@
1
1
  export default class Vector2 {
2
- constructor(parent, params = {}, callback) {
2
+ constructor(parent, arg1, arg2, arg3, arg4) {
3
3
  this.parent = parent;
4
+ this.callback = null;
4
5
 
5
- if (typeof params != 'object') {
6
- throw Error(`[GUI] vector2() first parameter must be an object. Received: ${typeof params}.`);
6
+ let params = {};
7
+ let objectX, objectY, propX, propY;
8
+
9
+ if (
10
+ arg1 &&
11
+ typeof arg1 === 'object' &&
12
+ typeof arg2 === 'string' &&
13
+ typeof arg3 === 'string'
14
+ ) {
15
+ objectX = arg1;
16
+ objectY = arg1;
17
+ propX = arg2;
18
+ propY = arg3;
19
+ params = arg4 || {};
20
+ } else if (arg1 && typeof arg1 === 'object' && arg1.x && arg1.x.obj) {
21
+ // Backwards compatibility for the old verbose { x: { obj, prop }, y: { obj, prop } } structure
22
+ // incase it's heavily used, since Vector2 doesn't have a 'simple' mode.
23
+ params = arg1;
24
+ objectX = params.x.obj;
25
+ propX = params.x.prop;
26
+ objectY = params.y.obj;
27
+ propY = params.y.prop;
28
+ } else {
29
+ throw Error(
30
+ `[GUI] vector2() invalid parameters. Use: gui.vector2(obj, 'propX', 'propY', params)`,
31
+ );
7
32
  }
8
-
33
+
9
34
  let label = typeof params.label == 'string' ? params.label || ' ' : ' ';
10
-
11
- const minX = params.x.min ?? 0;
12
- const maxX = params.x.max ?? 1;
13
- const minY = params.y.min ?? 0;
14
- const maxY = params.y.max ?? 1;
15
- const stepX = params.x.step || (maxX - minX) / 100;
16
- const stepY = params.y.step || (maxY - minY) / 100;
35
+ if (label === ' ') label = propX + ' / ' + propY;
36
+
37
+ const safeParamsX = params.x || {};
38
+ const safeParamsY = params.y || {};
39
+
40
+ const minX = safeParamsX.min ?? params.min ?? 0;
41
+ const maxX = safeParamsX.max ?? params.max ?? 1;
42
+ const minY = safeParamsY.min ?? params.min ?? 0;
43
+ const maxY = safeParamsY.max ?? params.max ?? 1;
44
+ const stepX = safeParamsX.step || params.step || (maxX - minX) / 100;
45
+ const stepY = safeParamsY.step || params.step || (maxY - minY) / 100;
17
46
  const decimalsX = this.parent._countDecimals(stepX);
18
47
  const decimalsY = this.parent._countDecimals(stepY);
19
-
20
- const objectX = params.x.obj;
21
- const propX = params.x.prop;
22
- const propXReferenceIndex = this.parent.propReferences.push(objectX[propX]) - 1;
23
-
24
- const objectY = params.y.obj;
25
- const propY = params.y.prop;
26
- const propYReferenceIndex = this.parent.propReferences.push(objectY[propY]) - 1;
27
-
28
- const tooltip = (typeof params.tooltip === 'string') ? params.tooltip : (params.tooltip === true ? label : null);
29
-
30
- callback = typeof callback == 'function' ? callback : null;
31
-
48
+
49
+ const propXReferenceIndex =
50
+ this.parent.propReferences.push(objectX[propX]) - 1;
51
+ const propYReferenceIndex =
52
+ this.parent.propReferences.push(objectY[propY]) - 1;
53
+
54
+ const tooltip =
55
+ typeof params.tooltip === 'string'
56
+ ? params.tooltip
57
+ : params.tooltip === true
58
+ ? label
59
+ : null;
60
+
32
61
  const container = document.createElement('div');
33
62
  container.className = 'p-gui__vector2';
34
63
  container.textContent = label;
35
- if ( tooltip ) {
64
+ if (tooltip) {
36
65
  container.setAttribute('title', tooltip);
37
66
  }
38
67
  this.parent.wrapper.append(container);
39
-
68
+
40
69
  const vector_value = document.createElement('div');
41
70
  vector_value.className = 'p-gui__vector-value';
42
71
  vector_value.textContent = objectX[propX] + ', ' + objectY[propY];
43
72
  container.append(vector_value);
44
-
73
+
45
74
  const area = document.createElement('div');
46
75
  area.className = 'p-gui__vector2-area';
47
76
  container.append(area);
48
- area.addEventListener('click', evt => {
49
- const newX = parseFloat(this.parent._mapLinear(evt.offsetX, 0, area.clientWidth, minX, maxX));
50
- const newY = parseFloat(this.parent._mapLinear(evt.offsetY, 0, area.clientHeight, maxY, minY));
77
+ area.addEventListener('click', (evt) => {
78
+ const newX = parseFloat(
79
+ this.parent._mapLinear(
80
+ evt.offsetX,
81
+ 0,
82
+ area.clientWidth,
83
+ minX,
84
+ maxX,
85
+ ),
86
+ );
87
+ const newY = parseFloat(
88
+ this.parent._mapLinear(
89
+ evt.offsetY,
90
+ 0,
91
+ area.clientHeight,
92
+ maxY,
93
+ minY,
94
+ ),
95
+ );
51
96
  objectX[propX] = newX.toFixed(decimalsX);
52
97
  objectY[propY] = newY.toFixed(decimalsY);
53
-
54
- if (callback) {
55
- callback(objectX[propX], objectX[propY]);
98
+
99
+ if (this.callback) {
100
+ this.callback(objectX[propX], objectX[propY]);
56
101
  }
57
-
102
+
58
103
  if (this.parent.onUpdate) {
59
104
  this.parent.onUpdate();
60
- } else if (this.parent.isFolder && this.parent.firstParent.onUpdate) {
105
+ } else if (
106
+ this.parent.isFolder &&
107
+ this.parent.firstParent.onUpdate
108
+ ) {
61
109
  this.parent.firstParent.onUpdate();
62
110
  }
63
111
  });
64
-
112
+
65
113
  const handlePointerMove = (evt) => {
66
114
  const rect = area.getBoundingClientRect();
67
115
  const offsetX = evt.clientX - rect.left;
68
116
  const offsetY = evt.clientY - rect.top;
69
-
117
+
70
118
  // Calculate new values and clamp them within min/max bounds
71
- const mappedX = this.parent._mapLinear(offsetX, 0, area.clientWidth, minX, maxX);
72
- const mappedY = this.parent._mapLinear(offsetY, 0, area.clientHeight, maxY, minY);
73
-
119
+ const mappedX = this.parent._mapLinear(
120
+ offsetX,
121
+ 0,
122
+ area.clientWidth,
123
+ minX,
124
+ maxX,
125
+ );
126
+ const mappedY = this.parent._mapLinear(
127
+ offsetY,
128
+ 0,
129
+ area.clientHeight,
130
+ maxY,
131
+ minY,
132
+ );
133
+
74
134
  const clampedX = Math.max(minX, Math.min(maxX, mappedX));
75
135
  const clampedY = Math.max(minY, Math.min(maxY, mappedY));
76
-
136
+
77
137
  objectX[propX] = parseFloat(clampedX.toFixed(decimalsX));
78
138
  objectY[propY] = parseFloat(clampedY.toFixed(decimalsY));
79
139
 
80
- if (callback) {
81
- callback(objectX[propX], objectY[propY]);
140
+ if (this.callback) {
141
+ this.callback(objectX[propX], objectY[propY]);
82
142
  }
83
143
 
84
144
  if (this.parent.onUpdate) {
85
145
  this.parent.onUpdate();
86
- } else if (this.parent.isFolder && this.parent.firstParent.onUpdate) {
146
+ } else if (
147
+ this.parent.isFolder &&
148
+ this.parent.firstParent.onUpdate
149
+ ) {
87
150
  this.parent.firstParent.onUpdate();
88
151
  }
89
152
  };
@@ -91,67 +154,89 @@ export default class Vector2 {
91
154
  area.addEventListener('pointerdown', (evt) => {
92
155
  // Call handlePointerMove immediately to update position on click
93
156
  handlePointerMove(evt);
94
-
157
+
95
158
  // Attach pointermove to document to capture movements everywhere
96
159
  document.addEventListener('pointermove', handlePointerMove);
97
160
 
98
161
  // Clean up on pointerup
99
- document.addEventListener('pointerup', () => {
100
- document.removeEventListener('pointermove', handlePointerMove);
101
- }, {once: true});
162
+ document.addEventListener(
163
+ 'pointerup',
164
+ () => {
165
+ document.removeEventListener(
166
+ 'pointermove',
167
+ handlePointerMove,
168
+ );
169
+ },
170
+ { once: true },
171
+ );
102
172
  });
103
-
104
-
105
-
173
+
106
174
  const line_x = document.createElement('div');
107
175
  line_x.className = 'p-gui__vector2-line p-gui__vector2-line-x';
108
176
  area.append(line_x);
109
-
177
+
110
178
  const line_y = document.createElement('div');
111
179
  line_y.className = 'p-gui__vector2-line p-gui__vector2-line-y';
112
180
  area.append(line_y);
113
-
181
+
114
182
  const dot = document.createElement('div');
115
183
  dot.className = 'p-gui__vector2-dot';
116
184
  area.append(dot);
117
-
185
+
118
186
  // Function to update dot position based on current values
119
187
  const updateDotPosition = () => {
120
- dot.style.left = this.parent._mapLinear(objectX[propX], minX, maxX, 0, area.clientWidth) + 'px';
121
- dot.style.top = this.parent._mapLinear(objectY[propY], minY, maxY, area.clientHeight, 0) + 'px';
188
+ dot.style.left =
189
+ this.parent._mapLinear(
190
+ objectX[propX],
191
+ minX,
192
+ maxX,
193
+ 0,
194
+ area.clientWidth,
195
+ ) + 'px';
196
+ dot.style.top =
197
+ this.parent._mapLinear(
198
+ objectY[propY],
199
+ minY,
200
+ maxY,
201
+ area.clientHeight,
202
+ 0,
203
+ ) + 'px';
122
204
  };
123
-
205
+
124
206
  // Initial position
125
207
  updateDotPosition();
126
-
208
+
127
209
  // Observe area resize (e.g., when scrollbars appear/disappear)
128
210
  const resizeObserver = new ResizeObserver(() => {
129
211
  updateDotPosition();
130
212
  });
131
213
  resizeObserver.observe(area);
132
-
133
- Object.defineProperty( objectX, propX, {
134
- set: val => {
214
+
215
+ Object.defineProperty(objectX, propX, {
216
+ set: (val) => {
135
217
  this.parent.propReferences[propXReferenceIndex] = val;
136
218
  updateDotPosition();
137
- vector_value.textContent = String( val ) + ', ' + objectY[propY];
219
+ vector_value.textContent = String(val) + ', ' + objectY[propY];
138
220
  },
139
- get: () => {
221
+ get: () => {
140
222
  return this.parent.propReferences[propXReferenceIndex];
141
- }
223
+ },
142
224
  });
143
-
144
- Object.defineProperty( objectY, propY, {
145
- set: val => {
225
+
226
+ Object.defineProperty(objectY, propY, {
227
+ set: (val) => {
146
228
  this.parent.propReferences[propYReferenceIndex] = val;
147
229
  updateDotPosition();
148
- vector_value.textContent = objectX[propX] + ', ' + String( val );
230
+ vector_value.textContent = objectX[propX] + ', ' + String(val);
149
231
  },
150
- get: () => {
232
+ get: () => {
151
233
  return this.parent.propReferences[propYReferenceIndex];
152
- }
234
+ },
153
235
  });
236
+ }
154
237
 
155
- return container;
238
+ onChange(callback) {
239
+ this.callback = callback;
240
+ return this;
156
241
  }
157
- }
242
+ }
package/src/index.js CHANGED
@@ -81,8 +81,8 @@ export default class GUI {
81
81
  this._styleInstance();
82
82
 
83
83
  this._addWrapper();
84
- this.wrapper.setAttribute('data-corner-x', this.screenCorner.x);
85
- this.wrapper.setAttribute('data-corner-y', this.screenCorner.y);
84
+ this.domElement.setAttribute('data-corner-x', this.screenCorner.x);
85
+ this.domElement.setAttribute('data-corner-y', this.screenCorner.y);
86
86
 
87
87
  if (options.autoRepositioning != false) {
88
88
  window.addEventListener('resize', this._handleResize.bind(this));
@@ -268,50 +268,49 @@ export default class GUI {
268
268
  content.append(this.wrapper);
269
269
  }
270
270
 
271
- button(params = {}, callback) {
271
+ button(params = {}) {
272
272
  this.imageContainer = null;
273
- const el = new Button(this, params, callback);
273
+ const el = new Button(this, params);
274
274
  return el;
275
275
  }
276
276
 
277
- image(params = {}, callback) {
277
+ image(params = {}) {
278
278
  if (!this.imageContainer) {
279
279
  this.imageContainer = document.createElement('div');
280
280
  this.imageContainer.className = 'p-gui__image-container';
281
281
  this.wrapper.append(this.imageContainer);
282
282
  }
283
- const el = new Image(this, params, callback);
283
+ const el = new Image(this, params);
284
284
  return el;
285
285
  }
286
286
 
287
- slider(params = {}, callback) {
287
+ slider(arg1, arg2, arg3) {
288
288
  this.imageContainer = null;
289
- const el = new Slider(this, params, callback);
289
+ const el = new Slider(this, arg1, arg2, arg3);
290
290
  return el;
291
291
  }
292
292
 
293
- toggle(params = {}, callback) {
293
+ toggle(arg1, arg2, arg3) {
294
294
  this.imageContainer = null;
295
- const el = new Toggle(this, params, callback);
296
- this.wrapper.append(el);
295
+ const el = new Toggle(this, arg1, arg2, arg3);
297
296
  return el;
298
297
  }
299
298
 
300
- list(params = {}, callback) {
299
+ list(arg1, arg2, arg3) {
301
300
  this.imageContainer = null;
302
- const el = new List(this, params, callback);
301
+ const el = new List(this, arg1, arg2, arg3);
303
302
  return el;
304
303
  }
305
304
 
306
- color(params = {}, callback) {
305
+ color(arg1, arg2, arg3) {
307
306
  this.imageContainer = null;
308
- const el = new Color(this, params, callback);
307
+ const el = new Color(this, arg1, arg2, arg3);
309
308
  return el;
310
309
  }
311
310
 
312
- vector2(params = {}, callback) {
311
+ vector2(arg1, arg2, arg3, arg4) {
313
312
  this.imageContainer = null;
314
- const el = new Vector2(this, params, callback);
313
+ const el = new Vector2(this, arg1, arg2, arg3, arg4);
315
314
  return el;
316
315
  }
317
316
 
@@ -38,6 +38,7 @@ export default function (position_type) {
38
38
  border: 1px solid var(--color-border);
39
39
  line-height: normal;
40
40
  transition: var(--transition) opacity;
41
+ overflow: hidden;
41
42
  }
42
43
 
43
44
  .p-gui__content {