-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui-node.js
381 lines (346 loc) · 9.43 KB
/
ui-node.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
let all_ui_points = [] // all UINodes automatically add themself to this list
//Draggable implementation: https://github.com/awillats/dynamics-visualizer-p5/blob/main/dragPoints.js
function add_ui_node(node){
all_ui_points.push(node)
}
function update_all_ui( force_param_update = false ){
all_ui_points.forEach( function(item,index){ item.update( force_param_update );item.over();} );
}
function show_all_ui(){
all_ui_points.forEach( function(item,index){ item.show(); } );
}
function press_all_ui(){
all_ui_points.forEach( function(item,index){ item.pressed(); } );
}
function release_all_ui(){
all_ui_points.forEach( function(item,index){ item.released(); } );
}
// misc helper function
function get_ui_colors(){
var style = window.getComputedStyle(document.body)
//var style = document.querySelector(':root').style;
let color_spec = {
'face':style.getPropertyValue('--ui-face-color'),
'edge':style.getPropertyValue('--ui-edge-color'),
'light_edge':style.getPropertyValue('--ui-light-edge-color'),
'back':style.getPropertyValue('--ui-back-color'),
//'light_edge':style.getPropertyValue('--ui-edge-color'),
}
return color_spec
}
function fnum(num) {
return num.toFixed(1);
}
function round_by(val,step){
dv = val%step
if (dv>step/2.0){
dv -= step
}
return val-dv
}
function int_range(min_v,max_v=false){
//akin to numpy's range function, generates integers between min and max val
if (!max_v)
{
max_v = min_v
min_v = 0
}
return Array(max_v-min_v+1).fill().map((_,idx)=>min_v+idx)
}
////
class LinearConstraint {
constructor(min,max,step=false)
{
this.min=min
this.max=max
this.step=step
}
shift(dv)
{
this.min+=dv
this.max+=dv
}
expand(dc)
{
this.min-=dv
this.max+=dv
}
scale(s){
this.min*=s
this.max*=s
if (this.step){
this.step*=s
}
}
range(){ return this.max-this.min }
center(){ return this.range()/2.0+this.min }
mapval(v,to_min=0.0, to_max=1.0){
return map(v, this.min, this.max, to_min, to_max)
}
dv(v) { return v-this.min }
bound(v){ return constrain(v, this.min, this.max) }
round_to_step(v)
{
if (!this.step) { return v }
return this.min + round_by( this.dv(v), this.step )
}
constrain(v){
return this.bound(this.round_to_step(v));
//return this.bound(v)
}
n_steps(){
if (!this.step) { return 0; }
return floor(this.range()/this.step);
}
index_to_step_val(idx){
return idx*this.step+this.min;
}
step_vals(){
if (!this.step) { return []; }
let step_indices = int_range(this.n_steps());
return step_indices.map( v => this.index_to_step_val(v) );
}
horiz_line(y) { line(this.min, y, this.max, y); }
vert_line(x) { line(x, this.min, x, this.max); }
}
////
class BoxConstraint {
constructor(xcon, ycon)
{
this.xcon=xcon;
this.ycon=ycon;
}
constrain(x, y){
x = this.xcon.constrain(x)
y = this.ycon.constrain(y)
return [x,y];
}
minmin(){
return new p5.Vector(this.xcon.min,this.ycon.min)
}
maxmax(){
return new p5.Vector(this.xcon.max,this.ycon.max)
}
center(){
let diff_v = this.maxmax().sub(this.minmin()).div(2)
return diff_v.add(this.minmin())
}
midleft(){return new p5.Vector(this.xcon.min, this.ycon.center())}
width() {return this.xcon.range() }
height() {return this.ycon.range() }
show(){
push()
push()
noFill()
stroke(color(get_ui_colors().light_edge))
// draw gridlines
this.xcon.step_vals().forEach(v => line(v, this.ycon.min, v, this.ycon.max ) )
this.ycon.step_vals().forEach(v => line(this.xcon.min, v, this.xcon.max, v ) )
pop()
//stroke(get_ui_colors().edge)
rectMode(CORNERS)
rect(this.xcon.min, this.ycon.min, this.xcon.max, this.ycon.max)
pop()
}
//likely useless, want to think about constraining outer edge of circle to within bounds
expand_constraints(dx,dy){
this.xcon.expand(dx);
this.ycon.expand(dy);
}
shift(dx=0.0, dy=0.0){
this.xcon.add(dx);
this.xcon.add(dy);
}
scale(xScale=1.0, yScale=1.0){
this.xcon.scale(xScale)
this.ycon.scale(yScale)
}
scaled_copy(xScale,yScale){
var scaled_clone = Object.assign({}, this);
scaled_clone.scale(xScale, yScale)
return scaled_clone
}
mapfrom_xy(x,y){
mx = this.xcon.map(x, 0.0, 1.0)
my = this.ycon.map(y, 0.0, 1.0)
}
}
/////
class UINode extends Draggable {
constructor(x,y, node_color = get_ui_colors().face){
super(x, y, node_color)
this.hw = 20;
this.hh = this.hw;
this.color = color(node_color);
add_ui_node( this );
this.ondrag_callbacks = [];
}
// drawCircle() {
// ellipseMode(RADIUS)
// ellipse(this.x, this.y, this.hh)
// }
_draw_node_()
{
super.show();
}
draw_text(str){
push()
//rectMode(CENTER)
textAlign(CENTER, CENTER)
fill( get_style_prop('--ui-text-color'))
let size_str = get_style_prop('--ui-text-size')
textSize( parseFloat(size_str) );
text(str, this.x, this.y)
pop()
noStroke();
}
show() {
push()
super.show()
let val = this.getValue()
this.draw_text( `${fnum(val.x)}, ${fnum(val.y)}` )
pop();
}
update( force_param_update = false)
{
super.update()
if ((this.dragging) || (force_param_update))
{
// some alternate implementations here:
// modeled after Alter the register function to take the object: http://www.bitstructures.com/2007/11/javascript-method-callbacks.html
// https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback
this.ondrag_callbacks.forEach( f => f() );
}
}
}
class Slider1D extends UINode {
constructor(x, y,
wh = 50,
is_vertical = true,
node_color = get_ui_colors().face
) {
super (x,y, node_color)
this.is_vertical = is_vertical
let v = this.pos(x,y);// (is_vertical ? y : x)
let pos_con = new LinearConstraint( v-wh, v+wh );
this.set_constraints(new LinearConstraint(0, 1), pos_con);
}
set_constraints( val_con, pos_con){
this.value_constraints = val_con;
if (pos_con) {
this.position_constraints = pos_con;
}
//let con_min = this.position_constraints.max;
let xo = (this.is_vertical ? this.x : this.position_constraints.min );
let yo = (this.is_vertical ? this.position_constraints.max : this.y );
this.origin = new p5.Vector(xo, yo)
this.xScale = this.value_constraints.range()/this.position_constraints.range()
this.yScale = -this.value_constraints.range()/this.position_constraints.range()
if (!pos_con){
this.position_constraints.step = this.value_constraints.step / this.pos(this.xScale, this.yScale)
}
}
set_origin(x=null, y=null)
{
if (x==null) { x = this.x; }
if (y==null) { y = this.y; }
let xo = (this.is_vertical ? this.x : x);
let yo = (this.is_vertical ? y : this.y );
this.origin = new p5.Vector(xo, yo)
}
pos(x=null, y=null){
if (x instanceof p5.Vector) {
let xy = x.copy();
x = xy.x;
y = xy.y;
}
else{
x = (x==null ? this.x : x);
y = (y==null ? this.y : y);
}
return (this.is_vertical ? y : x)
}
update(){
super.update()
this.constrain_pos();
}
constrain_pos()
{
if ( this.is_vertical ){
this.x = this.origin.x;
this.y = this.position_constraints.constrain(this.y);
}
else{
this.x = this.position_constraints.constrain(this.x);
this.y = this.origin.y;
}
}
getValue(){
this.constrain_pos();
return super.getValue();
}
set_value( v )
{
//certainly has to be a more concise way to write this
// should be a general method for mapping between two ranges (if doesnt already exist)
if (this.is_vertical)
{
this.y = this.value_constraints.mapval(v, this.position_constraints.min, this.position_constraints.max);
}
else
{
this.x = this.value_constraints.mapval(v, this.position_constraints.min, this.position_constraints.max);
}
}
show(){
push()
noFill();
strokeWeight(2);
stroke(get_ui_colors().edge);
if ( this.is_vertical ){
this.position_constraints.vert_line(this.origin.x);
}
else{
this.position_constraints.horiz_line(this.origin.y);
}
pop()
super._draw_node_();
//super.show();
let val = this.getValue()
let relevant_val = (this.is_vertical ? val.y : val.x)
this.draw_text( `${fnum(relevant_val)}` )
}
}
//
class Slider2D extends UINode {
constructor(x,y, node_color = get_ui_colors().face) {
super (x,y, node_color)
this.value_constraints = new BoxConstraint(
new LinearConstraint(0,10,1),
new LinearConstraint(-5,5,.1));
this.position_constraints = new BoxConstraint(
new LinearConstraint(x, x+100),
new LinearConstraint(y-50, y+50));
if (this.position_constraints){
this.origin = this.position_constraints.midleft()
this.xScale = this.value_constraints.width()/this.position_constraints.width()
this.yScale = -this.value_constraints.height()/this.position_constraints.height()
}
}
update() {
super.update();
[this.x,this.y] = this.position_constraints.constrain(this.x,this.y)
}
show() {
noFill()
stroke(get_ui_colors().light_edge)
this.position_constraints.show()
push()
strokeWeight(2)
stroke(get_ui_colors().edge)
this.position_constraints.xcon.horiz_line(this.origin.y)
this.position_constraints.ycon.vert_line(this.origin.x)
pop()
super.show();
}
}