perfect-gui 4.11.4 → 4.11.5
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/dist/perfect-gui.mjs +64 -65
- package/dist/perfect-gui.umd.js +4 -4
- package/package.json +1 -1
- package/src/components/Slider.js +9 -8
package/dist/perfect-gui.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class
|
|
1
|
+
class k {
|
|
2
2
|
constructor(e, t = {}, i) {
|
|
3
3
|
if (this.parent = e, this.propReferences = [], typeof t != "object")
|
|
4
4
|
throw Error(`[GUI] slider() first parameter must be an object. Received: ${typeof t}.`);
|
|
@@ -6,8 +6,8 @@ class E {
|
|
|
6
6
|
this.isObject = !1;
|
|
7
7
|
let s = null;
|
|
8
8
|
this.obj = t.obj, this.prop = t.prop;
|
|
9
|
-
let
|
|
10
|
-
if (this.min = t.min ?? 0, this.max = t.max ?? 1, this.step = t.step || (this.max - this.min) / 100,
|
|
9
|
+
let n = typeof t.value == "number" ? t.value : null;
|
|
10
|
+
if (this.min = t.min ?? 0, this.max = t.max ?? 1, this.step = t.step || (this.max - this.min) / 100, this.callback = typeof i == "function" ? i : null, n !== null)
|
|
11
11
|
(this.prop != null || this.obj != null) && console.warn('[GUI] slider() "obj" and "prop" parameters are ignored when a "value" parameter is used.');
|
|
12
12
|
else if (this.prop != null && this.obj != null) {
|
|
13
13
|
if (typeof this.prop != "string")
|
|
@@ -16,15 +16,15 @@ class E {
|
|
|
16
16
|
throw Error(`[GUI] slider() "obj" parameter must be an object. Received: ${typeof this.obj}.`);
|
|
17
17
|
a == " " && (a = this.prop), s = this.propReferences.push(this.obj[this.prop]) - 1, this.isObject = !0;
|
|
18
18
|
} else
|
|
19
|
-
(this.prop != null && this.obj == null || this.prop == null && this.obj != null) && console.warn('[GUI] slider() "obj" and "prop" parameters must be used together.'),
|
|
20
|
-
const
|
|
19
|
+
(this.prop != null && this.obj == null || this.prop == null && this.obj != null) && console.warn('[GUI] slider() "obj" and "prop" parameters must be used together.'), n = (this.max - this.min) / 2;
|
|
20
|
+
const o = typeof t.tooltip == "string" ? t.tooltip : t.tooltip === !0 ? a : null;
|
|
21
21
|
this.imageContainer = null;
|
|
22
22
|
const r = document.createElement("div");
|
|
23
|
-
r.className = "p-gui__slider",
|
|
23
|
+
r.className = "p-gui__slider", o && r.setAttribute("title", o);
|
|
24
24
|
const p = document.createElement("div");
|
|
25
25
|
p.className = "p-gui__slider-name", p.textContent = a, r.append(p), this.ctrlDiv = document.createElement("div"), this.ctrlDiv.className = "p-gui__slider-ctrl", this.ctrlDiv.setAttribute("type", "range"), this.ctrlDiv.setAttribute("min", this.min), this.ctrlDiv.setAttribute("max", this.max), r.append(this.ctrlDiv);
|
|
26
26
|
const l = document.createElement("div");
|
|
27
|
-
return l.className = "p-gui__slider-bar", this.ctrlDiv.append(l), this.handle = document.createElement("div"), this.handle.className = "p-gui__slider-handle", this.ctrlDiv.append(this.handle), this.filling = document.createElement("div"), this.filling.className = "p-gui__slider-filling", l.append(this.filling), this.valueInput = document.createElement("input"), this.valueInput.className = "p-gui__slider-value", this.valueInput.value = this.isObject ? this.obj[this.prop] :
|
|
27
|
+
return l.className = "p-gui__slider-bar", this.ctrlDiv.append(l), this.handle = document.createElement("div"), this.handle.className = "p-gui__slider-handle", this.ctrlDiv.append(this.handle), this.filling = document.createElement("div"), this.filling.className = "p-gui__slider-filling", l.append(this.filling), this.valueInput = document.createElement("input"), this.valueInput.className = "p-gui__slider-value", this.valueInput.value = this.isObject ? this.obj[this.prop] : n, r.append(this.valueInput), setTimeout(() => {
|
|
28
28
|
const d = this.handle.offsetWidth;
|
|
29
29
|
this.handle.position = this._mapLinear(this.valueInput.value, this.min, this.max, d / 2, 88 - d / 2), this.handle.style.transform = `translate(-50%, -50%) translateX(${this.handle.position}px)`, this.filling.style.width = `${this.handle.position}px`;
|
|
30
30
|
}, 0), this.valueInput.addEventListener("change", () => {
|
|
@@ -37,15 +37,14 @@ class E {
|
|
|
37
37
|
this.ctrlDiv.pointerDown && (this.ctrlDiv.pointerDelta = d.clientX - this.ctrlDiv.prevPosition, this._updateHandlePositionFromPointer(d));
|
|
38
38
|
}), this.isObject && Object.defineProperty(this.obj, this.prop, {
|
|
39
39
|
set: (d) => {
|
|
40
|
-
this.propReferences[s] = d, this.valueInput.value = d, this._updateHandlePositionFromValue(),
|
|
40
|
+
this.propReferences[s] = d, this.valueInput.value = d, this._updateHandlePositionFromValue(), this.callback && this.callback(parseFloat(this.valueInput.value));
|
|
41
41
|
},
|
|
42
42
|
get: () => this.propReferences[s]
|
|
43
43
|
}), r;
|
|
44
44
|
}
|
|
45
45
|
_updateHandlePositionFromPointer(e, t = !1) {
|
|
46
|
-
const i = this.ctrlDiv.offsetWidth, a = this.handle.offsetWidth, s = e.clientX - this.ctrlDiv.prevPosition;
|
|
46
|
+
const i = this.ctrlDiv.offsetWidth, a = this.handle.offsetWidth, s = e.clientX - this.ctrlDiv.prevPosition, n = parseFloat(this.valueInput.value);
|
|
47
47
|
let o;
|
|
48
|
-
const n = parseFloat(this.valueInput.value);
|
|
49
48
|
t ? o = e.offsetX : o = this.handle.position + s, o = Math.max(a / 2, Math.min(o, i - a / 2));
|
|
50
49
|
let r = this.min + (this.max - this.min) * (o - a / 2) / (i - a);
|
|
51
50
|
r > n ? r = this._quantizeFloor(r, this.step) : r = this._quantizeCeil(r, this.step), r = parseFloat(r.toFixed(9));
|
|
@@ -65,7 +64,7 @@ class E {
|
|
|
65
64
|
i = Math.max(t / 2, Math.min(i, e - t / 2)), this.handle.style.transform = `translate(-50%, -50%) translateX(${i}px)`, this.handle.position = i, this.filling.style.width = this.handle.position + "px";
|
|
66
65
|
}
|
|
67
66
|
_triggerCallbacks() {
|
|
68
|
-
this.isObject ? this.obj[this.prop] = parseFloat(this.valueInput.value) :
|
|
67
|
+
this.isObject ? this.obj[this.prop] = parseFloat(this.valueInput.value) : this.callback && this.callback(parseFloat(this.valueInput.value)), this.parent.onUpdate ? this.parent.onUpdate() : this.parent.isFolder && this.parent.firstParent.onUpdate && this.parent.firstParent.onUpdate();
|
|
69
68
|
}
|
|
70
69
|
_mapLinear(e, t, i, a, s) {
|
|
71
70
|
return a + (e - t) * (s - a) / (i - t);
|
|
@@ -80,7 +79,7 @@ class E {
|
|
|
80
79
|
return t * Math.floor(e / t);
|
|
81
80
|
}
|
|
82
81
|
}
|
|
83
|
-
const
|
|
82
|
+
const E = (
|
|
84
83
|
/* css */
|
|
85
84
|
`
|
|
86
85
|
.p-gui__button {
|
|
@@ -617,7 +616,7 @@ function $(w) {
|
|
|
617
616
|
border-color: rgba(255,255,255,.2);
|
|
618
617
|
}
|
|
619
618
|
|
|
620
|
-
${
|
|
619
|
+
${E}
|
|
621
620
|
|
|
622
621
|
${P}
|
|
623
622
|
|
|
@@ -713,11 +712,11 @@ class A {
|
|
|
713
712
|
throw typeof e.path == null ? Error("[GUI] image() path must be provided.") : Error("[GUI] image() path must be a string.");
|
|
714
713
|
let a = i.replace(/^.*[\\\/]/, ""), s;
|
|
715
714
|
e.name == null ? s = a : s = typeof e.name == "string" && e.name || " ";
|
|
716
|
-
const
|
|
715
|
+
const n = typeof e.tooltip == "string" ? e.tooltip : e.tooltip === !0 ? s : null, o = e.selected === !0, r = e.selectionBorder !== !1;
|
|
717
716
|
let p = "";
|
|
718
717
|
e.width && (typeof e.width == "number" && (e.width += "px"), p += `flex: 0 0 calc(${e.width} - 5px); `), e.height && (typeof e.height == "number" && (e.height += "px"), p += `height: ${e.height}; `), this.imageContainer || (this.imageContainer = document.createElement("div"), this.imageContainer.className = "p-gui__image-container", this.wrapper.append(this.imageContainer));
|
|
719
718
|
const l = document.createElement("div");
|
|
720
|
-
l.className = "p-gui__image", l.style = "background-image: url(" + i + "); " + p,
|
|
719
|
+
l.className = "p-gui__image", l.style = "background-image: url(" + i + "); " + p, n && l.setAttribute("title", n), this.imageContainer.append(l), o && r && l.classList.add("p-gui__image--selected");
|
|
721
720
|
const d = document.createElement("div");
|
|
722
721
|
return d.className = "p-gui__image-text", d.textContent = s, l.append(d), l.addEventListener("click", () => {
|
|
723
722
|
let c = l.parentElement.querySelectorAll(".p-gui__image--selected");
|
|
@@ -727,34 +726,34 @@ class A {
|
|
|
727
726
|
}), l;
|
|
728
727
|
}
|
|
729
728
|
slider(e = {}, t) {
|
|
730
|
-
const i = new
|
|
729
|
+
const i = new k(this, e, t);
|
|
731
730
|
this.wrapper.append(i);
|
|
732
731
|
}
|
|
733
732
|
toggle(e = {}, t) {
|
|
734
733
|
if (typeof e != "object")
|
|
735
734
|
throw Error(`[GUI] toggle() first parameter must be an object. Received: ${typeof e}.`);
|
|
736
|
-
let i = typeof e.name == "string" && e.name || " ", a = !1, s = null,
|
|
735
|
+
let i = typeof e.name == "string" && e.name || " ", a = !1, s = null, n = e.obj, o = e.prop, r = typeof e.value == "boolean" ? e.value : null;
|
|
737
736
|
if (r !== null)
|
|
738
|
-
(
|
|
739
|
-
else if (
|
|
740
|
-
if (typeof
|
|
741
|
-
throw Error(`[GUI] toggle() "prop" parameter must be an string. Received: ${typeof
|
|
742
|
-
if (typeof
|
|
743
|
-
throw Error(`[GUI] toggle() "obj" parameter must be an object. Received: ${typeof
|
|
744
|
-
i == " " && (i =
|
|
737
|
+
(o != null || n != null) && console.warn('[GUI] toggle() "obj" and "prop" parameters are ignored when a "value" parameter is used.');
|
|
738
|
+
else if (o != null && n != null) {
|
|
739
|
+
if (typeof o != "string")
|
|
740
|
+
throw Error(`[GUI] toggle() "prop" parameter must be an string. Received: ${typeof o}.`);
|
|
741
|
+
if (typeof n != "object")
|
|
742
|
+
throw Error(`[GUI] toggle() "obj" parameter must be an object. Received: ${typeof n}.`);
|
|
743
|
+
i == " " && (i = o), s = this.propReferences.push(n[o]) - 1, a = !0;
|
|
745
744
|
} else
|
|
746
|
-
(
|
|
745
|
+
(o != null && n == null || o == null && n == null) && console.warn('[GUI] toggle() "obj" and "prop" parameters must be used together.');
|
|
747
746
|
const p = typeof e.tooltip == "string" ? e.tooltip : e.tooltip === !0 ? i : null;
|
|
748
747
|
this.imageContainer = null;
|
|
749
748
|
const l = document.createElement("div");
|
|
750
749
|
l.textContent = i, l.className = "p-gui__switch", p && l.setAttribute("title", p), this.wrapper.append(l), l.addEventListener("click", (f) => {
|
|
751
750
|
const u = f.target.childNodes[1];
|
|
752
751
|
let g = !0;
|
|
753
|
-
u.classList.contains("p-gui__switch-checkbox--active") && (g = !1), u.classList.toggle("p-gui__switch-checkbox--active"), a ? o
|
|
752
|
+
u.classList.contains("p-gui__switch-checkbox--active") && (g = !1), u.classList.toggle("p-gui__switch-checkbox--active"), a ? n[o] = g : typeof t == "function" && t(g), this.onUpdate ? this.onUpdate() : this.isFolder && this.firstParent.onUpdate && this.firstParent.onUpdate();
|
|
754
753
|
});
|
|
755
|
-
let d = (() => a ? o
|
|
754
|
+
let d = (() => a ? n[o] ? " p-gui__switch-checkbox--active" : "" : r ? " p-gui__switch-checkbox--active" : "")();
|
|
756
755
|
const c = document.createElement("div");
|
|
757
|
-
c.className = "p-gui__switch-checkbox" + d, l.append(c), a && Object.defineProperty(
|
|
756
|
+
c.className = "p-gui__switch-checkbox" + d, l.append(c), a && Object.defineProperty(n, o, {
|
|
758
757
|
set: (f) => {
|
|
759
758
|
this.propReferences[s] = f, f ? c.classList.add("p-gui__switch-checkbox--active") : c.classList.remove("p-gui__switch-checkbox--active"), typeof t == "function" && t(f);
|
|
760
759
|
},
|
|
@@ -764,10 +763,10 @@ class A {
|
|
|
764
763
|
list(e = {}, t) {
|
|
765
764
|
if (typeof e != "object")
|
|
766
765
|
throw Error(`[GUI] list() first parameter must be an object. Received: ${typeof e}.`);
|
|
767
|
-
let i = typeof e.name == "string" ? e.name : " ", a = !1, s = null,
|
|
766
|
+
let i = typeof e.name == "string" ? e.name : " ", a = !1, s = null, n = e.obj, o = e.prop, r = Array.isArray(e.values) ? e.values : null, p, l = typeof r[0] != "string";
|
|
768
767
|
const d = typeof e.tooltip == "string" ? e.tooltip : e.tooltip === !0 ? i : null;
|
|
769
|
-
if (t = typeof t == "function" ? t : null, e.value !== void 0 || e.value === void 0 &&
|
|
770
|
-
(
|
|
768
|
+
if (t = typeof t == "function" ? t : null, e.value !== void 0 || e.value === void 0 && n === void 0 && o === void 0)
|
|
769
|
+
(o != null || n != null) && console.warn('[GUI] list() "obj" and "prop" parameters are ignored when a "value" parameter is used.'), p = (() => {
|
|
771
770
|
if (!r)
|
|
772
771
|
return null;
|
|
773
772
|
if (typeof e.value == "string")
|
|
@@ -775,32 +774,32 @@ class A {
|
|
|
775
774
|
if (typeof e.value == "number")
|
|
776
775
|
return e.value;
|
|
777
776
|
})();
|
|
778
|
-
else if (
|
|
779
|
-
if (typeof
|
|
780
|
-
throw Error(`[GUI] list() "prop" parameter must be an string. Received: ${typeof
|
|
781
|
-
if (typeof
|
|
782
|
-
throw Error(`[GUI] list() "obj" parameter must be an object. Received: ${typeof
|
|
777
|
+
else if (o != null && n != null) {
|
|
778
|
+
if (typeof o != "string")
|
|
779
|
+
throw Error(`[GUI] list() "prop" parameter must be an string. Received: ${typeof o}.`);
|
|
780
|
+
if (typeof n != "object")
|
|
781
|
+
throw Error(`[GUI] list() "obj" parameter must be an object. Received: ${typeof n}.`);
|
|
783
782
|
p = (() => {
|
|
784
783
|
if (!r)
|
|
785
784
|
return null;
|
|
786
|
-
if (typeof o
|
|
787
|
-
return l ? r.find((u) => u.value === o
|
|
788
|
-
if (typeof o
|
|
789
|
-
return l ? r.find((u) => u.value === o
|
|
790
|
-
})(), s = this.propReferences.push(o
|
|
785
|
+
if (typeof n[o] == "string")
|
|
786
|
+
return l ? r.find((u) => u.value === n[o]).value : r.indexOf(n[o]);
|
|
787
|
+
if (typeof n[o] == "number")
|
|
788
|
+
return l ? r.find((u) => u.value === n[o]).value : n[o];
|
|
789
|
+
})(), s = this.propReferences.push(n[o]) - 1, a = !0;
|
|
791
790
|
} else
|
|
792
|
-
(
|
|
791
|
+
(o != null && n == null || o == null && n == null) && console.warn('[GUI] list() "obj" and "prop" parameters must be used together.');
|
|
793
792
|
this.imageContainer = null;
|
|
794
793
|
let c = document.createElement("div");
|
|
795
794
|
c.className = "p-gui__list", c.textContent = i, d && c.setAttribute("title", d), this.wrapper.append(c);
|
|
796
795
|
let f = document.createElement("select");
|
|
797
796
|
c.append(f), f.className = "p-gui__list-dropdown", f.addEventListener("change", (u) => {
|
|
798
|
-
a ? o
|
|
797
|
+
a ? n[o] = u.target.value : t && t(u.target.value), this.onUpdate ? this.onUpdate() : this.isFolder && this.firstParent.onUpdate && this.firstParent.onUpdate();
|
|
799
798
|
}), r && r.forEach((u, g) => {
|
|
800
799
|
const _ = l ? u.name : u, h = l ? u.value : u;
|
|
801
800
|
let x = document.createElement("option");
|
|
802
801
|
x.setAttribute("value", h), x.textContent = _, f.append(x), (!l && p == g || l && p == h) && x.setAttribute("selected", "");
|
|
803
|
-
}), a && Object.defineProperty(
|
|
802
|
+
}), a && Object.defineProperty(n, o, {
|
|
804
803
|
set: (u) => {
|
|
805
804
|
let g, _, h;
|
|
806
805
|
l ? (h = r.find((v) => v.value == u), _ = (h == null ? void 0 : h.value) || r[0].value, g = r.indexOf(h)) : (typeof u == "string" && (g = r.indexOf(u), _ = u), typeof u == "number" && (g = u, _ = r[u])), this.propReferences[s] = l ? _ : u;
|
|
@@ -814,7 +813,7 @@ class A {
|
|
|
814
813
|
if (typeof e != "object")
|
|
815
814
|
throw Error(`[GUI] vector2() first parameter must be an object. Received: ${typeof e}.`);
|
|
816
815
|
let i = typeof e.name == "string" && e.name || " ";
|
|
817
|
-
const a = e.x.min ?? 0, s = e.x.max ?? 1,
|
|
816
|
+
const a = e.x.min ?? 0, s = e.x.max ?? 1, n = e.y.min ?? 0, o = e.y.max ?? 1, r = e.x.obj, p = e.x.prop, l = this.propReferences.push(r[p]) - 1, d = e.y.obj, c = e.y.prop, f = this.propReferences.push(d[c]) - 1, u = typeof e.tooltip == "string" ? e.tooltip : e.tooltip === !0 ? i : null;
|
|
818
817
|
t = typeof t == "function" ? t : null, this.imageContainer = null;
|
|
819
818
|
const g = document.createElement("div");
|
|
820
819
|
g.className = "p-gui__vector2", g.textContent = i, u && g.setAttribute("title", u), this.wrapper.append(g);
|
|
@@ -822,7 +821,7 @@ class A {
|
|
|
822
821
|
_.className = "p-gui__vector-value", _.textContent = r[p] + ", " + d[c], g.append(_);
|
|
823
822
|
const h = document.createElement("div");
|
|
824
823
|
h.className = "p-gui__vector2-area", g.append(h), h.addEventListener("click", (b) => {
|
|
825
|
-
r[p] = parseFloat(this._mapLinear(b.offsetX, 0, h.clientWidth, a, s).toFixed(2)), d[c] = parseFloat(this._mapLinear(b.offsetY, 0, h.clientHeight,
|
|
824
|
+
r[p] = parseFloat(this._mapLinear(b.offsetX, 0, h.clientWidth, a, s).toFixed(2)), d[c] = parseFloat(this._mapLinear(b.offsetY, 0, h.clientHeight, o, n).toFixed(2)), t && t(r[p], r[c]), this.onUpdate ? this.onUpdate() : this.isFolder && this.firstParent.onUpdate && this.firstParent.onUpdate();
|
|
826
825
|
});
|
|
827
826
|
let x = !1;
|
|
828
827
|
h.addEventListener("pointerdown", (b) => {
|
|
@@ -830,21 +829,21 @@ class A {
|
|
|
830
829
|
}), h.addEventListener("pointerup", () => {
|
|
831
830
|
x = !1;
|
|
832
831
|
}), h.addEventListener("pointermove", (b) => {
|
|
833
|
-
x && (r[p] = parseFloat(this._mapLinear(b.offsetX, 0, h.clientWidth, a, s).toFixed(2)), d[c] = parseFloat(this._mapLinear(b.offsetY, 0, h.clientHeight,
|
|
832
|
+
x && (r[p] = parseFloat(this._mapLinear(b.offsetX, 0, h.clientWidth, a, s).toFixed(2)), d[c] = parseFloat(this._mapLinear(b.offsetY, 0, h.clientHeight, o, n).toFixed(2)), t && t(r[p], r[c]), this.onUpdate ? this.onUpdate() : this.isFolder && this.firstParent.onUpdate && this.firstParent.onUpdate());
|
|
834
833
|
});
|
|
835
834
|
const v = document.createElement("div");
|
|
836
835
|
v.className = "p-gui__vector2-line p-gui__vector2-line-x", h.append(v);
|
|
837
836
|
const y = document.createElement("div");
|
|
838
837
|
y.className = "p-gui__vector2-line p-gui__vector2-line-y", h.append(y);
|
|
839
838
|
const m = document.createElement("div");
|
|
840
|
-
m.className = "p-gui__vector2-dot", h.append(m), m.style.left = this._mapLinear(r[p], a, s, 0, h.clientWidth) + "px", m.style.top = this._mapLinear(d[c],
|
|
839
|
+
m.className = "p-gui__vector2-dot", h.append(m), m.style.left = this._mapLinear(r[p], a, s, 0, h.clientWidth) + "px", m.style.top = this._mapLinear(d[c], n, o, h.clientHeight, 0) + "px", Object.defineProperty(r, p, {
|
|
841
840
|
set: (b) => {
|
|
842
841
|
this.propReferences[l] = b, m.style.left = this._mapLinear(b, a, s, 0, h.clientWidth) + "px", _.textContent = String(b) + ", " + d[c];
|
|
843
842
|
},
|
|
844
843
|
get: () => this.propReferences[l]
|
|
845
844
|
}), Object.defineProperty(d, c, {
|
|
846
845
|
set: (b) => {
|
|
847
|
-
this.propReferences[f] = b, m.style.top = this._mapLinear(b,
|
|
846
|
+
this.propReferences[f] = b, m.style.top = this._mapLinear(b, n, o, h.clientHeight, 0) + "px", _.textContent = r[p] + ", " + String(b);
|
|
848
847
|
},
|
|
849
848
|
get: () => this.propReferences[f]
|
|
850
849
|
});
|
|
@@ -852,25 +851,25 @@ class A {
|
|
|
852
851
|
color(e = {}, t) {
|
|
853
852
|
if (typeof e != "object")
|
|
854
853
|
throw Error(`[GUI] color() first parameter must be an object. Received: ${typeof e}.`);
|
|
855
|
-
let i = typeof e.name == "string" && e.name || " ", a = !1, s = null,
|
|
854
|
+
let i = typeof e.name == "string" && e.name || " ", a = !1, s = null, n = e.obj, o = e.prop, r;
|
|
856
855
|
const p = typeof e.tooltip == "string" ? e.tooltip : e.tooltip === !0 ? i : null;
|
|
857
856
|
if (typeof e.value == "string" && (e.value.length != 7 || e.value[0] != "#" ? console.error(`[GUI] color() 'value' parameter must be an hexadecimal string in the format "#ffffff". Received: "${e.value}".`) : r = e.value), r || (r = "#000000"), e.value !== void 0)
|
|
858
|
-
(
|
|
859
|
-
else if (
|
|
860
|
-
if (typeof
|
|
861
|
-
throw Error(`[GUI] color() "prop" parameter must be an string. Received: ${typeof
|
|
862
|
-
if (typeof
|
|
863
|
-
throw Error(`[GUI] color() "obj" parameter must be an object. Received: ${typeof
|
|
864
|
-
i == " " && (i =
|
|
857
|
+
(o != null || n != null) && console.warn('[GUI] color() "obj" and "prop" parameters are ignored when a "value" parameter is used.');
|
|
858
|
+
else if (o != null && n != null) {
|
|
859
|
+
if (typeof o != "string")
|
|
860
|
+
throw Error(`[GUI] color() "prop" parameter must be an string. Received: ${typeof o}.`);
|
|
861
|
+
if (typeof n != "object")
|
|
862
|
+
throw Error(`[GUI] color() "obj" parameter must be an object. Received: ${typeof n}.`);
|
|
863
|
+
i == " " && (i = o), s = this.propReferences.push(n[o]) - 1, a = !0;
|
|
865
864
|
} else
|
|
866
|
-
(
|
|
865
|
+
(o != null && n == null || o == null && n == null) && console.warn('[GUI] color() "obj" and "prop" parameters must be used together.');
|
|
867
866
|
this.imageContainer = null;
|
|
868
867
|
const l = document.createElement("div");
|
|
869
868
|
l.className = "p-gui__color", l.textContent = i, p && l.setAttribute("title", p), this.wrapper.append(l);
|
|
870
869
|
const d = document.createElement("input");
|
|
871
870
|
d.className = "p-gui__color-picker", d.setAttribute("type", "color"), d.value = r, l.append(d), typeof t == "function" && d.addEventListener("input", () => {
|
|
872
|
-
a ? o
|
|
873
|
-
}), a && Object.defineProperty(
|
|
871
|
+
a ? n[o] = d.value : typeof t == "function" && t(d.value), this.onUpdate ? this.onUpdate() : this.isFolder && this.firstParent.onUpdate && this.firstParent.onUpdate();
|
|
872
|
+
}), a && Object.defineProperty(n, o, {
|
|
874
873
|
set: (c) => {
|
|
875
874
|
this.propReferences[s] = c, d.value = c, typeof t == "function" && t(c);
|
|
876
875
|
},
|
|
@@ -880,12 +879,12 @@ class A {
|
|
|
880
879
|
folder(e = {}) {
|
|
881
880
|
let t = typeof e.closed == "boolean" ? e.closed : !1, i = e.name || "", a = e.color || null, s = e.maxHeight || null;
|
|
882
881
|
this.imageContainer = null;
|
|
883
|
-
let
|
|
884
|
-
this.folders.length == 0 && (
|
|
885
|
-
let
|
|
886
|
-
|
|
882
|
+
let n = "p-gui__folder";
|
|
883
|
+
this.folders.length == 0 && (n += " p-gui__folder--first"), t && (n += " p-gui__folder--closed");
|
|
884
|
+
let o = a ? `background-color: ${a};` : "";
|
|
885
|
+
o += s ? `max-height: ${s}px;` : "";
|
|
887
886
|
const r = document.createElement("div");
|
|
888
|
-
r.className =
|
|
887
|
+
r.className = n, r.style = o, this.wrapper.append(r);
|
|
889
888
|
const p = document.createElement("div");
|
|
890
889
|
p.innerHTML = `<span class="p-gui__folder-arrow"></span>${i}`, p.className = "p-gui__folder-header", r.append(p), p.addEventListener("click", () => {
|
|
891
890
|
r.classList.toggle("p-gui__folder--closed");
|
package/dist/perfect-gui.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(v,w){typeof exports=="object"&&typeof module<"u"?module.exports=w():typeof define=="function"&&define.amd?define(w):(v=typeof globalThis<"u"?globalThis:v||self,v["Perfect GUI"]=w())})(this,function(){"use strict";class v{constructor(e,t={},i){if(this.parent=e,this.propReferences=[],typeof t!="object")throw Error(`[GUI] slider() first parameter must be an object. Received: ${typeof t}.`);let a=typeof t.name=="string"&&t.name||" ";this.isObject=!1;let s=null;this.obj=t.obj,this.prop=t.prop;let
|
|
1
|
+
(function(v,w){typeof exports=="object"&&typeof module<"u"?module.exports=w():typeof define=="function"&&define.amd?define(w):(v=typeof globalThis<"u"?globalThis:v||self,v["Perfect GUI"]=w())})(this,function(){"use strict";class v{constructor(e,t={},i){if(this.parent=e,this.propReferences=[],typeof t!="object")throw Error(`[GUI] slider() first parameter must be an object. Received: ${typeof t}.`);let a=typeof t.name=="string"&&t.name||" ";this.isObject=!1;let s=null;this.obj=t.obj,this.prop=t.prop;let n=typeof t.value=="number"?t.value:null;if(this.min=t.min??0,this.max=t.max??1,this.step=t.step||(this.max-this.min)/100,this.callback=typeof i=="function"?i:null,n!==null)(this.prop!=null||this.obj!=null)&&console.warn('[GUI] slider() "obj" and "prop" parameters are ignored when a "value" parameter is used.');else if(this.prop!=null&&this.obj!=null){if(typeof this.prop!="string")throw Error(`[GUI] slider() "prop" parameter must be an string. Received: ${typeof this.prop}.`);if(typeof this.obj!="object")throw Error(`[GUI] slider() "obj" parameter must be an object. Received: ${typeof this.obj}.`);a==" "&&(a=this.prop),s=this.propReferences.push(this.obj[this.prop])-1,this.isObject=!0}else(this.prop!=null&&this.obj==null||this.prop==null&&this.obj!=null)&&console.warn('[GUI] slider() "obj" and "prop" parameters must be used together.'),n=(this.max-this.min)/2;const o=typeof t.tooltip=="string"?t.tooltip:t.tooltip===!0?a:null;this.imageContainer=null;const r=document.createElement("div");r.className="p-gui__slider",o&&r.setAttribute("title",o);const p=document.createElement("div");p.className="p-gui__slider-name",p.textContent=a,r.append(p),this.ctrlDiv=document.createElement("div"),this.ctrlDiv.className="p-gui__slider-ctrl",this.ctrlDiv.setAttribute("type","range"),this.ctrlDiv.setAttribute("min",this.min),this.ctrlDiv.setAttribute("max",this.max),r.append(this.ctrlDiv);const l=document.createElement("div");return l.className="p-gui__slider-bar",this.ctrlDiv.append(l),this.handle=document.createElement("div"),this.handle.className="p-gui__slider-handle",this.ctrlDiv.append(this.handle),this.filling=document.createElement("div"),this.filling.className="p-gui__slider-filling",l.append(this.filling),this.valueInput=document.createElement("input"),this.valueInput.className="p-gui__slider-value",this.valueInput.value=this.isObject?this.obj[this.prop]:n,r.append(this.valueInput),setTimeout(()=>{const d=this.handle.offsetWidth;this.handle.position=this._mapLinear(this.valueInput.value,this.min,this.max,d/2,88-d/2),this.handle.style.transform=`translate(-50%, -50%) translateX(${this.handle.position}px)`,this.filling.style.width=`${this.handle.position}px`},0),this.valueInput.addEventListener("change",()=>{this._updateHandlePositionFromValue(),this._triggerCallbacks()}),this.ctrlDiv.addEventListener("pointerdown",d=>{this.ctrlDiv.pointerDown=!0,this.ctrlDiv.prevPosition=d.clientX,this._updateHandlePositionFromPointer(d,!0)}),window.addEventListener("pointerup",d=>{this.ctrlDiv.pointerDown=!1}),window.addEventListener("pointermove",d=>{this.ctrlDiv.pointerDown&&(this.ctrlDiv.pointerDelta=d.clientX-this.ctrlDiv.prevPosition,this._updateHandlePositionFromPointer(d))}),this.isObject&&Object.defineProperty(this.obj,this.prop,{set:d=>{this.propReferences[s]=d,this.valueInput.value=d,this._updateHandlePositionFromValue(),this.callback&&this.callback(parseFloat(this.valueInput.value))},get:()=>this.propReferences[s]}),r}_updateHandlePositionFromPointer(e,t=!1){const i=this.ctrlDiv.offsetWidth,a=this.handle.offsetWidth,s=e.clientX-this.ctrlDiv.prevPosition,n=parseFloat(this.valueInput.value);let o;t?o=e.offsetX:o=this.handle.position+s,o=Math.max(a/2,Math.min(o,i-a/2));let r=this.min+(this.max-this.min)*(o-a/2)/(i-a);r>n?r=this._quantizeFloor(r,this.step):r=this._quantizeCeil(r,this.step),r=parseFloat(r.toFixed(9));const p=parseFloat((n+this.step).toFixed(9)),l=parseFloat((n-this.step).toFixed(9));if(r>=p||r<=l){const d=this._countDecimals(this.step);r=r.toFixed(d),this.valueInput.value=r,this.ctrlDiv.prevPosition=e.clientX,this.handle.style.transform=`translate(-50%, -50%) translateX(${o}px)`,this.handle.position=o,this.filling.style.width=this.handle.position+"px",this._triggerCallbacks()}}_countDecimals(e){const t=e.toString(),i=t.indexOf(".");return i===-1?0:t.length-i-1}_updateHandlePositionFromValue(){const e=this.ctrlDiv.offsetWidth,t=this.handle.offsetWidth;let i=this._mapLinear(this.valueInput.value,this.min,this.max,t/2,e-t/2);i=Math.max(t/2,Math.min(i,e-t/2)),this.handle.style.transform=`translate(-50%, -50%) translateX(${i}px)`,this.handle.position=i,this.filling.style.width=this.handle.position+"px"}_triggerCallbacks(){this.isObject?this.obj[this.prop]=parseFloat(this.valueInput.value):this.callback&&this.callback(parseFloat(this.valueInput.value)),this.parent.onUpdate?this.parent.onUpdate():this.parent.isFolder&&this.parent.firstParent.onUpdate&&this.parent.firstParent.onUpdate()}_mapLinear(e,t,i,a,s){return a+(e-t)*(s-a)/(i-t)}_quantize(e,t){return t*Math.round(e/t)}_quantizeCeil(e,t){return t*Math.ceil(e/t)}_quantizeFloor(e,t){return t*Math.floor(e/t)}}const w=`
|
|
2
2
|
.p-gui__button {
|
|
3
3
|
background: var(--color-accent);
|
|
4
4
|
text-align: center;
|
|
@@ -377,7 +377,7 @@
|
|
|
377
377
|
.p-gui__folder--closed .p-gui__folder-arrow {
|
|
378
378
|
transform: rotate(0deg);
|
|
379
379
|
}
|
|
380
|
-
`;function $(
|
|
380
|
+
`;function $(k){return`
|
|
381
381
|
.p-gui {
|
|
382
382
|
--main-border-radius: 5px;
|
|
383
383
|
--color-bg: #121212;
|
|
@@ -389,7 +389,7 @@
|
|
|
389
389
|
--color-accent-hover: #218fda;
|
|
390
390
|
--transition: .1s linear;
|
|
391
391
|
|
|
392
|
-
position: ${
|
|
392
|
+
position: ${k};
|
|
393
393
|
top: 0;
|
|
394
394
|
left: 0;
|
|
395
395
|
transform: translate3d(0,0,0);
|
|
@@ -529,4 +529,4 @@
|
|
|
529
529
|
${this.screenCorner.y=="top"?"":"top: auto; bottom: 0;"}
|
|
530
530
|
${this.backgroundColor?"background: "+this.backgroundColor+";":""}
|
|
531
531
|
opacity: ${this.opacity};
|
|
532
|
-
}`)}_folderConstructor(e){this.wrapper=e.wrapper,this.isFolder=!0,this.parent=e.parent,this.firstParent=e.firstParent}_parseScreenCorner(e){let t={x:"right",y:"top"};return e==null||(typeof e!="string"&&console.error("[perfect-gui] Position must be a string."),e.includes("left")&&(t.x="left"),e.includes("bottom")&&(t.y="bottom")),t}_getScrollbarWidth(e){return e===document.body?window.innerWidth-document.documentElement.clientWidth:e.offsetWidth-e.clientWidth}_handleResize(){if(this.container==document.body?this.maxHeight=window.innerHeight:this.maxHeight=Math.min(this.container.clientHeight,window.innerHeight),this.initMaxHeight&&(this.maxHeight=Math.min(this.initMaxHeight,this.maxHeight)),this.wrapper.style.maxHeight=this.maxHeight+"px",this.hasBeenDragged)return;let e=this._getScrollbarWidth(this.container);if(this.xOffset=this.screenCorner.x=="left"?0:this.container.clientWidth-this.wrapperWidth-e,this.instanceId>0){let t=this.container.querySelectorAll(`.p-gui:not(#${this.wrapper.id}):not([data-dragged])`);for(let i=0;i<t.length&&!(parseInt(t[i].id.replace("p-gui-",""))>this.instanceId);i++)this.screenCorner.y==t[i].dataset.cornerY&&(this.screenCorner.x=="left"&&t[i].dataset.cornerX=="left"?this.xOffset+=t[i].offsetWidth:this.screenCorner.x=="right"&&t[i].dataset.cornerX=="right"&&(this.xOffset-=t[i].offsetWidth))}this.position={prevX:this.xOffset,prevY:this.yOffset,x:this.xOffset,y:this.yOffset},this.wrapper.style.transform=`translate3d(${this.position.x}px, ${this.position.y}px, 0)`}_addStyles(e){this.stylesheet.innerHTML+=e}_addWrapper(){this.wrapper=document.createElement("div"),this.wrapper.id="p-gui-"+this.instanceId,this.wrapper.className="p-gui",this.container.append(this.wrapper),this.header=document.createElement("div"),this.header.className="p-gui__header",this.header.textContent=this.name,this.header.style=`${this.backgroundColor?"border-color: "+this.backgroundColor+";":""}`,this.wrapper.append(this.header);const e=document.createElement("div");e.className="p-gui__header-close",e.addEventListener("click",this.toggleClose.bind(this)),this.header.append(e)}button(e,t){let i="";typeof e!="string"?typeof e=="object"&&(e!=null&&e.hasOwnProperty("name"))?i=e.name==""?" ":e.name:i=" ":i=e==""?" ":e;const a=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;this.imageContainer=null;const s=document.createElement("div");s.className="p-gui__button",s.textContent=i,a&&s.setAttribute("title",a),s.addEventListener("click",()=>{t&&t(),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),this.wrapper.append(s),typeof e.color=="string"&&(s.style.setProperty("--color-accent",e.color),s.style.setProperty("--color-accent-hover",e.hoverColor||e.color))}image(e={},t){if(typeof e!="object")throw Error(`[GUI] image() first parameter must be an object. Received: ${typeof e}.`);let i;if(typeof e.path=="string")i=e.path;else throw typeof e.path==null?Error("[GUI] image() path must be provided."):Error("[GUI] image() path must be a string.");let a=i.replace(/^.*[\\\/]/,""),s;e.name==null?s=a:s=typeof e.name=="string"&&e.name||" ";const o=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?s:null,n=e.selected===!0,r=e.selectionBorder!==!1;let p="";e.width&&(typeof e.width=="number"&&(e.width+="px"),p+=`flex: 0 0 calc(${e.width} - 5px); `),e.height&&(typeof e.height=="number"&&(e.height+="px"),p+=`height: ${e.height}; `),this.imageContainer||(this.imageContainer=document.createElement("div"),this.imageContainer.className="p-gui__image-container",this.wrapper.append(this.imageContainer));const l=document.createElement("div");l.className="p-gui__image",l.style="background-image: url("+i+"); "+p,o&&l.setAttribute("title",o),this.imageContainer.append(l),n&&r&&l.classList.add("p-gui__image--selected");const d=document.createElement("div");return d.className="p-gui__image-text",d.textContent=s,l.append(d),l.addEventListener("click",()=>{let c=l.parentElement.querySelectorAll(".p-gui__image--selected");for(let f=0;f<c.length;f++)c[f].classList.remove("p-gui__image--selected");r&&l.classList.add("p-gui__image--selected"),typeof t=="function"&&t({path:i,text:s}),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),l}slider(e={},t){const i=new v(this,e,t);this.wrapper.append(i)}toggle(e={},t){if(typeof e!="object")throw Error(`[GUI] toggle() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"&&e.name||" ",a=!1,s=null,o=e.obj,n=e.prop,r=typeof e.value=="boolean"?e.value:null;if(r!==null)(n!=null||o!=null)&&console.warn('[GUI] toggle() "obj" and "prop" parameters are ignored when a "value" parameter is used.');else if(n!=null&&o!=null){if(typeof n!="string")throw Error(`[GUI] toggle() "prop" parameter must be an string. Received: ${typeof n}.`);if(typeof o!="object")throw Error(`[GUI] toggle() "obj" parameter must be an object. Received: ${typeof o}.`);i==" "&&(i=n),s=this.propReferences.push(o[n])-1,a=!0}else(n!=null&&o==null||n==null&&o==null)&&console.warn('[GUI] toggle() "obj" and "prop" parameters must be used together.');const p=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;this.imageContainer=null;const l=document.createElement("div");l.textContent=i,l.className="p-gui__switch",p&&l.setAttribute("title",p),this.wrapper.append(l),l.addEventListener("click",f=>{const u=f.target.childNodes[1];let g=!0;u.classList.contains("p-gui__switch-checkbox--active")&&(g=!1),u.classList.toggle("p-gui__switch-checkbox--active"),a?o[n]=g:typeof t=="function"&&t(g),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()});let d=(()=>a?o[n]?" p-gui__switch-checkbox--active":"":r?" p-gui__switch-checkbox--active":"")();const c=document.createElement("div");c.className="p-gui__switch-checkbox"+d,l.append(c),a&&Object.defineProperty(o,n,{set:f=>{this.propReferences[s]=f,f?c.classList.add("p-gui__switch-checkbox--active"):c.classList.remove("p-gui__switch-checkbox--active"),typeof t=="function"&&t(f)},get:()=>this.propReferences[s]})}list(e={},t){if(typeof e!="object")throw Error(`[GUI] list() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"?e.name:" ",a=!1,s=null,o=e.obj,n=e.prop,r=Array.isArray(e.values)?e.values:null,p,l=typeof r[0]!="string";const d=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;if(t=typeof t=="function"?t:null,e.value!==void 0||e.value===void 0&&o===void 0&&n===void 0)(n!=null||o!=null)&&console.warn('[GUI] list() "obj" and "prop" parameters are ignored when a "value" parameter is used.'),p=(()=>{if(!r)return null;if(typeof e.value=="string")return r.indexOf(e.value);if(typeof e.value=="number")return e.value})();else if(n!=null&&o!=null){if(typeof n!="string")throw Error(`[GUI] list() "prop" parameter must be an string. Received: ${typeof n}.`);if(typeof o!="object")throw Error(`[GUI] list() "obj" parameter must be an object. Received: ${typeof o}.`);p=(()=>{if(!r)return null;if(typeof o[n]=="string")return l?r.find(u=>u.value===o[n]).value:r.indexOf(o[n]);if(typeof o[n]=="number")return l?r.find(u=>u.value===o[n]).value:o[n]})(),s=this.propReferences.push(o[n])-1,a=!0}else(n!=null&&o==null||n==null&&o==null)&&console.warn('[GUI] list() "obj" and "prop" parameters must be used together.');this.imageContainer=null;let c=document.createElement("div");c.className="p-gui__list",c.textContent=i,d&&c.setAttribute("title",d),this.wrapper.append(c);let f=document.createElement("select");c.append(f),f.className="p-gui__list-dropdown",f.addEventListener("change",u=>{a?o[n]=u.target.value:t&&t(u.target.value),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),r&&r.forEach((u,g)=>{const _=l?u.name:u,h=l?u.value:u;let x=document.createElement("option");x.setAttribute("value",h),x.textContent=_,f.append(x),(!l&&p==g||l&&p==h)&&x.setAttribute("selected","")}),a&&Object.defineProperty(o,n,{set:u=>{let g,_,h;l?(h=r.find(y=>y.value==u),_=(h==null?void 0:h.value)||r[0].value,g=r.indexOf(h)):(typeof u=="string"&&(g=r.indexOf(u),_=u),typeof u=="number"&&(g=u,_=r[u])),this.propReferences[s]=l?_:u;const x=f.querySelector("[selected]");x&&x.removeAttribute("selected"),f.querySelectorAll("option")[g].setAttribute("selected",""),typeof t=="function"&&t(l?h:_,g)},get:()=>this.propReferences[s]})}vector2(e={},t){if(typeof e!="object")throw Error(`[GUI] vector2() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"&&e.name||" ";const a=e.x.min??0,s=e.x.max??1,o=e.y.min??0,n=e.y.max??1,r=e.x.obj,p=e.x.prop,l=this.propReferences.push(r[p])-1,d=e.y.obj,c=e.y.prop,f=this.propReferences.push(d[c])-1,u=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;t=typeof t=="function"?t:null,this.imageContainer=null;const g=document.createElement("div");g.className="p-gui__vector2",g.textContent=i,u&&g.setAttribute("title",u),this.wrapper.append(g);const _=document.createElement("div");_.className="p-gui__vector-value",_.textContent=r[p]+", "+d[c],g.append(_);const h=document.createElement("div");h.className="p-gui__vector2-area",g.append(h),h.addEventListener("click",b=>{r[p]=parseFloat(this._mapLinear(b.offsetX,0,h.clientWidth,a,s).toFixed(2)),d[c]=parseFloat(this._mapLinear(b.offsetY,0,h.clientHeight,n,o).toFixed(2)),t&&t(r[p],r[c]),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()});let x=!1;h.addEventListener("pointerdown",b=>{x=!0}),h.addEventListener("pointerup",()=>{x=!1}),h.addEventListener("pointermove",b=>{x&&(r[p]=parseFloat(this._mapLinear(b.offsetX,0,h.clientWidth,a,s).toFixed(2)),d[c]=parseFloat(this._mapLinear(b.offsetY,0,h.clientHeight,n,o).toFixed(2)),t&&t(r[p],r[c]),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate())});const y=document.createElement("div");y.className="p-gui__vector2-line p-gui__vector2-line-x",h.append(y);const k=document.createElement("div");k.className="p-gui__vector2-line p-gui__vector2-line-y",h.append(k);const m=document.createElement("div");m.className="p-gui__vector2-dot",h.append(m),m.style.left=this._mapLinear(r[p],a,s,0,h.clientWidth)+"px",m.style.top=this._mapLinear(d[c],o,n,h.clientHeight,0)+"px",Object.defineProperty(r,p,{set:b=>{this.propReferences[l]=b,m.style.left=this._mapLinear(b,a,s,0,h.clientWidth)+"px",_.textContent=String(b)+", "+d[c]},get:()=>this.propReferences[l]}),Object.defineProperty(d,c,{set:b=>{this.propReferences[f]=b,m.style.top=this._mapLinear(b,o,n,h.clientHeight,0)+"px",_.textContent=r[p]+", "+String(b)},get:()=>this.propReferences[f]})}color(e={},t){if(typeof e!="object")throw Error(`[GUI] color() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"&&e.name||" ",a=!1,s=null,o=e.obj,n=e.prop,r;const p=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;if(typeof e.value=="string"&&(e.value.length!=7||e.value[0]!="#"?console.error(`[GUI] color() 'value' parameter must be an hexadecimal string in the format "#ffffff". Received: "${e.value}".`):r=e.value),r||(r="#000000"),e.value!==void 0)(n!=null||o!=null)&&console.warn('[GUI] color() "obj" and "prop" parameters are ignored when a "value" parameter is used.');else if(n!=null&&o!=null){if(typeof n!="string")throw Error(`[GUI] color() "prop" parameter must be an string. Received: ${typeof n}.`);if(typeof o!="object")throw Error(`[GUI] color() "obj" parameter must be an object. Received: ${typeof o}.`);i==" "&&(i=n),s=this.propReferences.push(o[n])-1,a=!0}else(n!=null&&o==null||n==null&&o==null)&&console.warn('[GUI] color() "obj" and "prop" parameters must be used together.');this.imageContainer=null;const l=document.createElement("div");l.className="p-gui__color",l.textContent=i,p&&l.setAttribute("title",p),this.wrapper.append(l);const d=document.createElement("input");d.className="p-gui__color-picker",d.setAttribute("type","color"),d.value=r,l.append(d),typeof t=="function"&&d.addEventListener("input",()=>{a?o[n]=d.value:typeof t=="function"&&t(d.value),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),a&&Object.defineProperty(o,n,{set:c=>{this.propReferences[s]=c,d.value=c,typeof t=="function"&&t(c)},get:()=>this.propReferences[s]})}folder(e={}){let t=typeof e.closed=="boolean"?e.closed:!1,i=e.name||"",a=e.color||null,s=e.maxHeight||null;this.imageContainer=null;let o="p-gui__folder";this.folders.length==0&&(o+=" p-gui__folder--first"),t&&(o+=" p-gui__folder--closed");let n=a?`background-color: ${a};`:"";n+=s?`max-height: ${s}px;`:"";const r=document.createElement("div");r.className=o,r.style=n,this.wrapper.append(r);const p=document.createElement("div");p.innerHTML=`<span class="p-gui__folder-arrow"></span>${i}`,p.className="p-gui__folder-header",r.append(p),p.addEventListener("click",()=>{r.classList.toggle("p-gui__folder--closed")});let l=new A({isFolder:!0,folderOptions:{wrapper:r,parent:this,firstParent:this.firstParent}});return this.folders.push(l),l}_makeDraggable(){var e=this;this.header.addEventListener("pointerdown",t),this.header.addEventListener("pointerup",a);function t(s){s.preventDefault(),e.position.initX=e.position.x,e.position.initY=e.position.y,e.position.prevX=s.clientX,e.position.prevY=s.clientY,document.addEventListener("pointermove",i)}function i(s){s.preventDefault(),e.hasBeenDragged||(e.hasBeenDragged=!0,e.wrapper.setAttribute("data-dragged","true")),e.position.x=e.position.initX+s.clientX-e.position.prevX,e.position.y=e.position.initY+s.clientY-e.position.prevY,e.wrapper.style.transform="translate3d("+e.position.x+"px,"+e.position.y+"px,0)"}function a(s){document.removeEventListener("pointermove",i)}}toggleClose(){this.closed=!this.closed,this.closed?(this.previousInnerScroll=this.wrapper.scrollTop,this.wrapper.scrollTo(0,0)):this.wrapper.scrollTo(0,this.previousInnerScroll),this.wrapper.classList.toggle("p-gui--collapsed")}kill(){this.wrapper.remove()}_mapLinear(e,t,i,a,s){return a+(e-t)*(s-a)/(i-t)}}return A});
|
|
532
|
+
}`)}_folderConstructor(e){this.wrapper=e.wrapper,this.isFolder=!0,this.parent=e.parent,this.firstParent=e.firstParent}_parseScreenCorner(e){let t={x:"right",y:"top"};return e==null||(typeof e!="string"&&console.error("[perfect-gui] Position must be a string."),e.includes("left")&&(t.x="left"),e.includes("bottom")&&(t.y="bottom")),t}_getScrollbarWidth(e){return e===document.body?window.innerWidth-document.documentElement.clientWidth:e.offsetWidth-e.clientWidth}_handleResize(){if(this.container==document.body?this.maxHeight=window.innerHeight:this.maxHeight=Math.min(this.container.clientHeight,window.innerHeight),this.initMaxHeight&&(this.maxHeight=Math.min(this.initMaxHeight,this.maxHeight)),this.wrapper.style.maxHeight=this.maxHeight+"px",this.hasBeenDragged)return;let e=this._getScrollbarWidth(this.container);if(this.xOffset=this.screenCorner.x=="left"?0:this.container.clientWidth-this.wrapperWidth-e,this.instanceId>0){let t=this.container.querySelectorAll(`.p-gui:not(#${this.wrapper.id}):not([data-dragged])`);for(let i=0;i<t.length&&!(parseInt(t[i].id.replace("p-gui-",""))>this.instanceId);i++)this.screenCorner.y==t[i].dataset.cornerY&&(this.screenCorner.x=="left"&&t[i].dataset.cornerX=="left"?this.xOffset+=t[i].offsetWidth:this.screenCorner.x=="right"&&t[i].dataset.cornerX=="right"&&(this.xOffset-=t[i].offsetWidth))}this.position={prevX:this.xOffset,prevY:this.yOffset,x:this.xOffset,y:this.yOffset},this.wrapper.style.transform=`translate3d(${this.position.x}px, ${this.position.y}px, 0)`}_addStyles(e){this.stylesheet.innerHTML+=e}_addWrapper(){this.wrapper=document.createElement("div"),this.wrapper.id="p-gui-"+this.instanceId,this.wrapper.className="p-gui",this.container.append(this.wrapper),this.header=document.createElement("div"),this.header.className="p-gui__header",this.header.textContent=this.name,this.header.style=`${this.backgroundColor?"border-color: "+this.backgroundColor+";":""}`,this.wrapper.append(this.header);const e=document.createElement("div");e.className="p-gui__header-close",e.addEventListener("click",this.toggleClose.bind(this)),this.header.append(e)}button(e,t){let i="";typeof e!="string"?typeof e=="object"&&(e!=null&&e.hasOwnProperty("name"))?i=e.name==""?" ":e.name:i=" ":i=e==""?" ":e;const a=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;this.imageContainer=null;const s=document.createElement("div");s.className="p-gui__button",s.textContent=i,a&&s.setAttribute("title",a),s.addEventListener("click",()=>{t&&t(),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),this.wrapper.append(s),typeof e.color=="string"&&(s.style.setProperty("--color-accent",e.color),s.style.setProperty("--color-accent-hover",e.hoverColor||e.color))}image(e={},t){if(typeof e!="object")throw Error(`[GUI] image() first parameter must be an object. Received: ${typeof e}.`);let i;if(typeof e.path=="string")i=e.path;else throw typeof e.path==null?Error("[GUI] image() path must be provided."):Error("[GUI] image() path must be a string.");let a=i.replace(/^.*[\\\/]/,""),s;e.name==null?s=a:s=typeof e.name=="string"&&e.name||" ";const n=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?s:null,o=e.selected===!0,r=e.selectionBorder!==!1;let p="";e.width&&(typeof e.width=="number"&&(e.width+="px"),p+=`flex: 0 0 calc(${e.width} - 5px); `),e.height&&(typeof e.height=="number"&&(e.height+="px"),p+=`height: ${e.height}; `),this.imageContainer||(this.imageContainer=document.createElement("div"),this.imageContainer.className="p-gui__image-container",this.wrapper.append(this.imageContainer));const l=document.createElement("div");l.className="p-gui__image",l.style="background-image: url("+i+"); "+p,n&&l.setAttribute("title",n),this.imageContainer.append(l),o&&r&&l.classList.add("p-gui__image--selected");const d=document.createElement("div");return d.className="p-gui__image-text",d.textContent=s,l.append(d),l.addEventListener("click",()=>{let c=l.parentElement.querySelectorAll(".p-gui__image--selected");for(let f=0;f<c.length;f++)c[f].classList.remove("p-gui__image--selected");r&&l.classList.add("p-gui__image--selected"),typeof t=="function"&&t({path:i,text:s}),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),l}slider(e={},t){const i=new v(this,e,t);this.wrapper.append(i)}toggle(e={},t){if(typeof e!="object")throw Error(`[GUI] toggle() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"&&e.name||" ",a=!1,s=null,n=e.obj,o=e.prop,r=typeof e.value=="boolean"?e.value:null;if(r!==null)(o!=null||n!=null)&&console.warn('[GUI] toggle() "obj" and "prop" parameters are ignored when a "value" parameter is used.');else if(o!=null&&n!=null){if(typeof o!="string")throw Error(`[GUI] toggle() "prop" parameter must be an string. Received: ${typeof o}.`);if(typeof n!="object")throw Error(`[GUI] toggle() "obj" parameter must be an object. Received: ${typeof n}.`);i==" "&&(i=o),s=this.propReferences.push(n[o])-1,a=!0}else(o!=null&&n==null||o==null&&n==null)&&console.warn('[GUI] toggle() "obj" and "prop" parameters must be used together.');const p=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;this.imageContainer=null;const l=document.createElement("div");l.textContent=i,l.className="p-gui__switch",p&&l.setAttribute("title",p),this.wrapper.append(l),l.addEventListener("click",f=>{const u=f.target.childNodes[1];let g=!0;u.classList.contains("p-gui__switch-checkbox--active")&&(g=!1),u.classList.toggle("p-gui__switch-checkbox--active"),a?n[o]=g:typeof t=="function"&&t(g),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()});let d=(()=>a?n[o]?" p-gui__switch-checkbox--active":"":r?" p-gui__switch-checkbox--active":"")();const c=document.createElement("div");c.className="p-gui__switch-checkbox"+d,l.append(c),a&&Object.defineProperty(n,o,{set:f=>{this.propReferences[s]=f,f?c.classList.add("p-gui__switch-checkbox--active"):c.classList.remove("p-gui__switch-checkbox--active"),typeof t=="function"&&t(f)},get:()=>this.propReferences[s]})}list(e={},t){if(typeof e!="object")throw Error(`[GUI] list() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"?e.name:" ",a=!1,s=null,n=e.obj,o=e.prop,r=Array.isArray(e.values)?e.values:null,p,l=typeof r[0]!="string";const d=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;if(t=typeof t=="function"?t:null,e.value!==void 0||e.value===void 0&&n===void 0&&o===void 0)(o!=null||n!=null)&&console.warn('[GUI] list() "obj" and "prop" parameters are ignored when a "value" parameter is used.'),p=(()=>{if(!r)return null;if(typeof e.value=="string")return r.indexOf(e.value);if(typeof e.value=="number")return e.value})();else if(o!=null&&n!=null){if(typeof o!="string")throw Error(`[GUI] list() "prop" parameter must be an string. Received: ${typeof o}.`);if(typeof n!="object")throw Error(`[GUI] list() "obj" parameter must be an object. Received: ${typeof n}.`);p=(()=>{if(!r)return null;if(typeof n[o]=="string")return l?r.find(u=>u.value===n[o]).value:r.indexOf(n[o]);if(typeof n[o]=="number")return l?r.find(u=>u.value===n[o]).value:n[o]})(),s=this.propReferences.push(n[o])-1,a=!0}else(o!=null&&n==null||o==null&&n==null)&&console.warn('[GUI] list() "obj" and "prop" parameters must be used together.');this.imageContainer=null;let c=document.createElement("div");c.className="p-gui__list",c.textContent=i,d&&c.setAttribute("title",d),this.wrapper.append(c);let f=document.createElement("select");c.append(f),f.className="p-gui__list-dropdown",f.addEventListener("change",u=>{a?n[o]=u.target.value:t&&t(u.target.value),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),r&&r.forEach((u,g)=>{const _=l?u.name:u,h=l?u.value:u;let x=document.createElement("option");x.setAttribute("value",h),x.textContent=_,f.append(x),(!l&&p==g||l&&p==h)&&x.setAttribute("selected","")}),a&&Object.defineProperty(n,o,{set:u=>{let g,_,h;l?(h=r.find(y=>y.value==u),_=(h==null?void 0:h.value)||r[0].value,g=r.indexOf(h)):(typeof u=="string"&&(g=r.indexOf(u),_=u),typeof u=="number"&&(g=u,_=r[u])),this.propReferences[s]=l?_:u;const x=f.querySelector("[selected]");x&&x.removeAttribute("selected"),f.querySelectorAll("option")[g].setAttribute("selected",""),typeof t=="function"&&t(l?h:_,g)},get:()=>this.propReferences[s]})}vector2(e={},t){if(typeof e!="object")throw Error(`[GUI] vector2() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"&&e.name||" ";const a=e.x.min??0,s=e.x.max??1,n=e.y.min??0,o=e.y.max??1,r=e.x.obj,p=e.x.prop,l=this.propReferences.push(r[p])-1,d=e.y.obj,c=e.y.prop,f=this.propReferences.push(d[c])-1,u=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;t=typeof t=="function"?t:null,this.imageContainer=null;const g=document.createElement("div");g.className="p-gui__vector2",g.textContent=i,u&&g.setAttribute("title",u),this.wrapper.append(g);const _=document.createElement("div");_.className="p-gui__vector-value",_.textContent=r[p]+", "+d[c],g.append(_);const h=document.createElement("div");h.className="p-gui__vector2-area",g.append(h),h.addEventListener("click",b=>{r[p]=parseFloat(this._mapLinear(b.offsetX,0,h.clientWidth,a,s).toFixed(2)),d[c]=parseFloat(this._mapLinear(b.offsetY,0,h.clientHeight,o,n).toFixed(2)),t&&t(r[p],r[c]),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()});let x=!1;h.addEventListener("pointerdown",b=>{x=!0}),h.addEventListener("pointerup",()=>{x=!1}),h.addEventListener("pointermove",b=>{x&&(r[p]=parseFloat(this._mapLinear(b.offsetX,0,h.clientWidth,a,s).toFixed(2)),d[c]=parseFloat(this._mapLinear(b.offsetY,0,h.clientHeight,o,n).toFixed(2)),t&&t(r[p],r[c]),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate())});const y=document.createElement("div");y.className="p-gui__vector2-line p-gui__vector2-line-x",h.append(y);const E=document.createElement("div");E.className="p-gui__vector2-line p-gui__vector2-line-y",h.append(E);const m=document.createElement("div");m.className="p-gui__vector2-dot",h.append(m),m.style.left=this._mapLinear(r[p],a,s,0,h.clientWidth)+"px",m.style.top=this._mapLinear(d[c],n,o,h.clientHeight,0)+"px",Object.defineProperty(r,p,{set:b=>{this.propReferences[l]=b,m.style.left=this._mapLinear(b,a,s,0,h.clientWidth)+"px",_.textContent=String(b)+", "+d[c]},get:()=>this.propReferences[l]}),Object.defineProperty(d,c,{set:b=>{this.propReferences[f]=b,m.style.top=this._mapLinear(b,n,o,h.clientHeight,0)+"px",_.textContent=r[p]+", "+String(b)},get:()=>this.propReferences[f]})}color(e={},t){if(typeof e!="object")throw Error(`[GUI] color() first parameter must be an object. Received: ${typeof e}.`);let i=typeof e.name=="string"&&e.name||" ",a=!1,s=null,n=e.obj,o=e.prop,r;const p=typeof e.tooltip=="string"?e.tooltip:e.tooltip===!0?i:null;if(typeof e.value=="string"&&(e.value.length!=7||e.value[0]!="#"?console.error(`[GUI] color() 'value' parameter must be an hexadecimal string in the format "#ffffff". Received: "${e.value}".`):r=e.value),r||(r="#000000"),e.value!==void 0)(o!=null||n!=null)&&console.warn('[GUI] color() "obj" and "prop" parameters are ignored when a "value" parameter is used.');else if(o!=null&&n!=null){if(typeof o!="string")throw Error(`[GUI] color() "prop" parameter must be an string. Received: ${typeof o}.`);if(typeof n!="object")throw Error(`[GUI] color() "obj" parameter must be an object. Received: ${typeof n}.`);i==" "&&(i=o),s=this.propReferences.push(n[o])-1,a=!0}else(o!=null&&n==null||o==null&&n==null)&&console.warn('[GUI] color() "obj" and "prop" parameters must be used together.');this.imageContainer=null;const l=document.createElement("div");l.className="p-gui__color",l.textContent=i,p&&l.setAttribute("title",p),this.wrapper.append(l);const d=document.createElement("input");d.className="p-gui__color-picker",d.setAttribute("type","color"),d.value=r,l.append(d),typeof t=="function"&&d.addEventListener("input",()=>{a?n[o]=d.value:typeof t=="function"&&t(d.value),this.onUpdate?this.onUpdate():this.isFolder&&this.firstParent.onUpdate&&this.firstParent.onUpdate()}),a&&Object.defineProperty(n,o,{set:c=>{this.propReferences[s]=c,d.value=c,typeof t=="function"&&t(c)},get:()=>this.propReferences[s]})}folder(e={}){let t=typeof e.closed=="boolean"?e.closed:!1,i=e.name||"",a=e.color||null,s=e.maxHeight||null;this.imageContainer=null;let n="p-gui__folder";this.folders.length==0&&(n+=" p-gui__folder--first"),t&&(n+=" p-gui__folder--closed");let o=a?`background-color: ${a};`:"";o+=s?`max-height: ${s}px;`:"";const r=document.createElement("div");r.className=n,r.style=o,this.wrapper.append(r);const p=document.createElement("div");p.innerHTML=`<span class="p-gui__folder-arrow"></span>${i}`,p.className="p-gui__folder-header",r.append(p),p.addEventListener("click",()=>{r.classList.toggle("p-gui__folder--closed")});let l=new A({isFolder:!0,folderOptions:{wrapper:r,parent:this,firstParent:this.firstParent}});return this.folders.push(l),l}_makeDraggable(){var e=this;this.header.addEventListener("pointerdown",t),this.header.addEventListener("pointerup",a);function t(s){s.preventDefault(),e.position.initX=e.position.x,e.position.initY=e.position.y,e.position.prevX=s.clientX,e.position.prevY=s.clientY,document.addEventListener("pointermove",i)}function i(s){s.preventDefault(),e.hasBeenDragged||(e.hasBeenDragged=!0,e.wrapper.setAttribute("data-dragged","true")),e.position.x=e.position.initX+s.clientX-e.position.prevX,e.position.y=e.position.initY+s.clientY-e.position.prevY,e.wrapper.style.transform="translate3d("+e.position.x+"px,"+e.position.y+"px,0)"}function a(s){document.removeEventListener("pointermove",i)}}toggleClose(){this.closed=!this.closed,this.closed?(this.previousInnerScroll=this.wrapper.scrollTop,this.wrapper.scrollTo(0,0)):this.wrapper.scrollTo(0,this.previousInnerScroll),this.wrapper.classList.toggle("p-gui--collapsed")}kill(){this.wrapper.remove()}_mapLinear(e,t,i,a,s){return a+(e-t)*(s-a)/(i-t)}}return A});
|
package/package.json
CHANGED
package/src/components/Slider.js
CHANGED
|
@@ -16,9 +16,10 @@ export default class Slider {
|
|
|
16
16
|
this.min = params.min ?? 0;
|
|
17
17
|
this.max = params.max ?? 1;
|
|
18
18
|
this.step = params.step || (this.max - this.min) / 100;
|
|
19
|
+
this.callback = typeof callback == 'function' ? callback : null;
|
|
19
20
|
|
|
20
21
|
// callback mode
|
|
21
|
-
if ( value !== null ) {
|
|
22
|
+
if ( value !== null ) {
|
|
22
23
|
if (this.prop != undefined || this.obj != undefined) {
|
|
23
24
|
console.warn(`[GUI] slider() "obj" and "prop" parameters are ignored when a "value" parameter is used.`);
|
|
24
25
|
}
|
|
@@ -110,7 +111,7 @@ export default class Slider {
|
|
|
110
111
|
});
|
|
111
112
|
|
|
112
113
|
window.addEventListener('pointermove', (evt) => {
|
|
113
|
-
if (this.ctrlDiv.pointerDown) {
|
|
114
|
+
if (this.ctrlDiv.pointerDown) {
|
|
114
115
|
this.ctrlDiv.pointerDelta = evt.clientX - this.ctrlDiv.prevPosition;
|
|
115
116
|
this._updateHandlePositionFromPointer(evt);
|
|
116
117
|
}
|
|
@@ -124,8 +125,8 @@ export default class Slider {
|
|
|
124
125
|
|
|
125
126
|
this._updateHandlePositionFromValue();
|
|
126
127
|
|
|
127
|
-
if (
|
|
128
|
-
callback(parseFloat(this.valueInput.value));
|
|
128
|
+
if (this.callback) {
|
|
129
|
+
this.callback(parseFloat(this.valueInput.value));
|
|
129
130
|
}
|
|
130
131
|
},
|
|
131
132
|
get: () => {
|
|
@@ -141,8 +142,8 @@ export default class Slider {
|
|
|
141
142
|
const sliderWidth = this.ctrlDiv.offsetWidth;
|
|
142
143
|
const handleWidth = this.handle.offsetWidth;
|
|
143
144
|
const pointerDelta = evt.clientX - this.ctrlDiv.prevPosition;
|
|
144
|
-
let handlePosition;
|
|
145
145
|
const currentValue = parseFloat(this.valueInput.value);
|
|
146
|
+
let handlePosition;
|
|
146
147
|
|
|
147
148
|
if (firstDown) {
|
|
148
149
|
handlePosition = evt.offsetX;
|
|
@@ -212,13 +213,13 @@ export default class Slider {
|
|
|
212
213
|
this.filling.style.width = this.handle.position + 'px';
|
|
213
214
|
}
|
|
214
215
|
|
|
215
|
-
_triggerCallbacks() {
|
|
216
|
+
_triggerCallbacks() {
|
|
216
217
|
if ( this.isObject ) {
|
|
217
218
|
this.obj[this.prop] = parseFloat(this.valueInput.value);
|
|
218
219
|
}
|
|
219
220
|
else {
|
|
220
|
-
if (
|
|
221
|
-
callback(parseFloat(this.valueInput.value));
|
|
221
|
+
if (this.callback) {
|
|
222
|
+
this.callback(parseFloat(this.valueInput.value));
|
|
222
223
|
}
|
|
223
224
|
}
|
|
224
225
|
|