-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
105 lines (93 loc) · 2.82 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function initPanel() {
gPanel = QuickSettings.create(60, 110, 'Sketch Parameter Range Helper');
for (let s of settings) {
gPanel.addRange(s.title, s.min, s.max, s.value, s.step, function (value) {});
}
gPanel
.addButton('Randomize', randomize)
.addButton('Copy Current Settings', copySettings)
.addButton('Update Min Bounds', updateMin)
.addButton('Update Max Bounds', updateMax)
.addButton('Reset', resetPanel)
.setGlobalChangeHandler(updateAll);
}
function updateAll() {
updateDefault();
createLines();
}
function updatePanel() {
for (let s of settings) {
gPanel.setRangeParameters(s.title, s.min, s.max, s.step);
}
}
function updateMax() {
for (let s of settings) {
let val = int(gPanel.getValue(s.title));
s.value = val;
s.max = val;
}
updatePanel();
}
function updateMin() {
for (let s of settings) {
let val = int(gPanel.getValue(s.title));
s.value = val;
s.min = val;
}
updatePanel();
}
function randomize() {
for (let s of settings) {
s.value = floor(random(s.min, s.max));
gPanel.setValue(s.title, s.value);
}
}
function updateDefault() {
for (let s of settings) {
s.value = int(gPanel.getValue(s.title));
}
}
function copySettings() {
let stringSettings = 'let settings = ' + JSON.stringify(settings, null, 4);
copyToClipboard(stringSettings);
}
function resetPanel() {
settings = gOGSettings.map((obj) => deepCopy(obj));
for (let s of settings) {
gPanel.setValue(s.title, s.value);
}
updatePanel();
}
// I did not write this function, but copied it from Aaron Reuland (a_ soluble_fish) who copied it from user Greg Lowe on Stack Overflow
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
// Internet Explorer-specific code path to prevent textarea being shown while dialog is visible.
return window.clipboardData.setData('Text', text);
} else if (document.queryCommandSupported && document.queryCommandSupported('copy')) {
var textarea = document.createElement('textarea');
textarea.textContent = text;
textarea.style.position = 'fixed'; // Prevent scrolling to bottom of page in Microsoft Edge.
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand('copy'); // Security exception may be thrown by some browsers.
} catch (ex) {
console.warn('Copy to clipboard failed.', ex);
return prompt('Copy to clipboard: Ctrl+C, Enter', text);
} finally {
document.body.removeChild(textarea);
}
}
}
// I did not write this function either, ChatGPT did it for me
function deepCopy(obj) {
let newObj = {};
for (let key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
newObj[key] = deepCopy(obj[key]);
} else {
newObj[key] = obj[key];
}
}
return newObj;
}