-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc2.html
1249 lines (1027 loc) · 27 KB
/
ccc2.html
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html theme=dark>
<body>
<base href="www2/">
<link rel="preload" href="fa-regular-400.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="fa-solid-900.woff2" as="font" type="font/woff2" crossorigin>
<script src=glue.js global extend></script>
<script src=ui.js></script>
<script src=ui_validation.js></script>
<script src=ui_nav.js></script>
<script src=ui_grid.js></script>
<script>
(function () {
"use strict"
const G = window
let EPSILON = 1e-5
function near(a, b) { return abs(a - b) < EPSILON }
function pix(x, scale) {
return (scale ?? 1) == 1 ? x : round(x * scale)
}
let comp = obj()
// walls ---------------------------------------------------------------------
function hit_wall_middle(xs, xi1, xi2, x, px) {
let x1 = xs[xi1]
let x2 = xs[xi2]
let xc = (x1+x2) / 2
let d = min(5*px, (x2-x1)/3)
return x >= xc-d && x <= xc+d
}
function point_type(lv, invert, xi, yi) {
if (invert) {
let xi0 = xi
xi = yi
yi = xi0
}
let r = lv.walls_h[yi][xi]
let b = lv.walls_v[xi][yi]
let l = lv.walls_h[yi][xi-1] ?? 0
let t = lv.walls_v[xi][yi-1] ?? 0
let ends = r + b + l + t
if (!ends)
return
if (ends == 1)
return 'end'
if (r && l && !(t || b))
return // point along h-wall with no joint
if (t && b && !(r || l))
return // point along v-wall with no joint
return 'joint'
}
// sides: l,b,r,t == 1,2,-1,-2
function hit_point_side(plan, xi, yi, mx, my) {
let dx = mx - plan.xs[xi]
let dy = my - plan.ys[yi]
return abs(dx) > abs(dy) ? (dx > 0 ? 1 : -1) : (dy > 0 ? 2 : -2)
}
function point_end_side(lv, xi, yi) {
let r = lv.walls_h[yi][xi]
let b = lv.walls_v[xi][yi]
let l = lv.walls_h[yi][xi-1]
let t = lv.walls_v[xi][yi-1]
return r ? 1 : b ? 2 : l ? -1 : t ? -2 : 0
}
function xs_add(plan, invert, lv, x) {
// add a vertical line to the grid.
let xs = invert ? plan.ys : plan.xs
let i = binsearch(xs, x)
insert(xs, i, x)
// add an empty vertical wall-line at the added grid line.
let wall_lines = invert ? lv.walls_h : lv.walls_v
let wall_line = []
wall_line.length = wall_lines[0].length
insert(wall_lines, i, wall_line.fill(0))
// reconnect _|_ walls that are split by the new point.
let splits_walls = i > 0 && i < xs.length-1
for (let lv of plan.levels) {
let wall_lines = invert ? lv.walls_v : lv.walls_h
if (wall_lines)
for (let walls of wall_lines)
insert(walls, i, splits_walls && walls[i-1] ? 1 : 0)
}
return i
}
function xs_del(plan, invert, lv, xi) {
// remove the vertical line from the grid.
let xs = invert ? plan.ys : plan.xs
remove(xs, xi)
// remove the vertical wall-line at the removed grid line.
let wall_lines = invert ? lv.walls_h : lv.walls_v
remove(wall_lines, xi)
// remove the point from all _|_ walls.
for (let lv of plan.levels) {
let wall_lines = invert ? lv.walls_v : lv.walls_h
if (wall_lines)
for (let walls of wall_lines)
remove(walls, xi)
}
}
// NOTE: the moved line should not be shared with other levels!
function xs_move(plan, invert, lv, xi0, xi1) {
// move the vertical line from the grid.
let xs = invert ? plan.ys : plan.xs
let xi2 = array_move(xs, xi0, 1, xi1, true)
// move the vertical wall-line at the moved grid line.
let wall_lines_v = invert ? lv.walls_h : lv.walls_v
array_move(wall_lines_v, xi0, 1, xi1, true)
// move the point from all _|_ walls.
let wall_lines_h = invert ? lv.walls_v : lv.walls_h
if (wall_lines_h) {
let is_last = xi2 == xs.length-1
let splits_walls = xi2 > 0 && !is_last
for (let walls of wall_lines_h) {
array_move(walls, xi0, 1, xi1, true)
// reconnect the _|_ walls that are split by the moved point.
walls[xi2] = splits_walls && walls[xi2-1] ? 1 : 0
// disconnect the _|_ walls that
if (is_last)
walls[xi2-1] = 0
}
}
return xi2
}
function wall_v_start_move(plan, invert, lv, xi0, yi1, yi2) {
let xs = invert ? plan.ys : plan.xs
let x = xs[xi0]
let xi1 = xs_add(plan, invert, lv, x)
assert(xi1 == xi0)
xi0++
let wall_lines = invert ? lv.walls_h : lv.walls_v
// move moving wall to the new mobile grid line.
for (let yi = yi1; yi < yi2; yi++) {
wall_lines[xi0][yi] = 0
wall_lines[xi1][yi] = 1
}
return xi1
}
function wall_v_move(plan, invert, lv, xi0, x1) {
let xs = invert ? plan.ys : plan.xs
if (x1 == xs[xi0]) // x didn't change
return xi0
let xi1 = binsearch(xs, x1)
// NOTE: result can be to the left or to the right of xi0 hence xi0+1.
if (xi1 == xi0 || xi1 == xi0+1) { // index in grid didn't change
assert((xs[xi0-1] ?? -1/0) <= x1)
assert((xs[xi0+1] ?? 1/0) >= x1)
xs[xi0] = x1
return xi0
}
xs[xi0] = x1
return xs_move(plan, invert, lv, xi0, xi1)
}
function wall_end_move() {
}
// find the longest horizontal wall segment without joints that intersects x.
function wall_intersecting(plan, invert, lv, yi, x) {
let walls_h = invert ? lv.walls_v : lv.walls_h
let walls_v = invert ? lv.walls_h : lv.walls_v
let xs = invert ? plan.ys : plan.xs
if (x < xs[0])
return [null, null]
let xi = binsearch(xs, x)
if (xi == xs.length)
return [null, null]
let xi2 = xi
let xi1 = xi-1
if (!walls_h[yi][xi1])
return [null, null]
while (walls_h[yi][xi2] && !walls_v[xi2][yi] && !walls_v[xi2][yi-1]) xi2++
while (walls_h[yi][xi1-1] && !walls_v[xi1][yi] && !walls_v[xi1][yi-1]) xi1--
return [xi1, xi2]
}
function hit_plan(plan, lv, id, x0, y0) {
if (!(lv.walls_h || lv.walls_v))
return
let scale = plan.scale
let mx = (ui.mx - x0 - plan.x) / scale
let my = (ui.my - y0 - plan.y) / scale
let px = 1 / scale
let min_x = plan.xs[0]
let max_x = plan.xs[plan.xs.length-1]
let min_y = plan.ys[0]
let max_y = plan.ys[plan.ys.length-1]
// find the range of close-enough points.
let xi1 = binsearch(plan.xs, mx - 5*px)
let xi2 = binsearch(plan.xs, mx + 5*px)
let yi1 = binsearch(plan.ys, my - 5*px)
let yi2 = binsearch(plan.ys, my + 5*px)
// find closest point from the range of close-enough points with walls on it on lv.
let min_d2 = 1/0
let xi, yi, pt
for (let i = xi1; i < xi2; i++) {
let x = plan.xs[i]
for (let j = yi1; j < yi2; j++) {
let y = plan.ys[j]
let d2 = (x - mx)**2 + (y - my)**2
if (d2 < min_d2) {
let t = point_type(lv, 0, i, j)
if (t) { // point has walls on it on lv
min_d2 = d2
xi = i
yi = j
pt = t
}
}
}
}
if (pt) {
let hs = ui.hover(id)
if (pt == 'end') { // resize or add
let hit_side = hit_point_side(plan, xi, yi, mx, my)
let end_side = point_end_side(lv, xi, yi)
let inline = abs(hit_side) == abs(end_side)
hs.set('action', inline ? 'resize' : 'add')
}
if (pt == 'joint') { // detach or add
let hit_side = hit_point_side(plan, xi, yi, mx, my)
let r = lv.walls_h[yi][xi-0] ? 1 : 0
let b = lv.walls_v[xi][yi-0] ? 2 : 0
let l = lv.walls_h[yi][xi-1] ? -1 : 0
let t = lv.walls_v[xi][yi-1] ? 2 : 0
let detach = hit_side == r || hit_side == b || hit_side == l || hit_side == t
hs.set('action', detach ? 'detach' : 'add')
}
hs.set('xi', xi)
hs.set('yi', yi)
pr(hs.get('action'), xi, yi)
return
}
// find the closest wall from the range of close-enough walls on both directions.
for (let vert = 0; vert <= 1; vert++) {
let _xi1 = vert ? yi1 : xi1
let _xi2 = vert ? yi2 : xi2
let xs = vert ? plan.ys : plan.xs
let ys = vert ? plan.xs : plan.ys
let _mx = vert ? my : mx
let _my = vert ? mx : my
let wall_lines = vert ? lv.walls_h : lv.walls_v
let min_d = 1/0
let c_xi
let c_yi1
let c_yi2
for (let xi = _xi1; xi < _xi2; xi++) {
let [yi1, yi2] = wall_intersecting(plan, !vert, lv, xi, _my)
if (yi1 != null) {
let x = xs[xi]
let d = abs(x - _mx)
if (d < min_d) {
min_d = d
c_xi = xi
c_yi1 = yi1
c_yi2 = yi2
}
}
}
if (c_xi != null) {
let move = hit_wall_middle(ys, c_yi1, c_yi2, _my, px)
let hs = ui.hover(id)
hs.set('action', move ? 'move_wall_v' : 'add_wall_h')
hs.set('invert', vert)
hs.set('xi' , c_xi )
hs.set('yi1', c_yi1)
hs.set('yi2', c_yi2)
hs.set('x' , _mx)
hs.set('y' , _my)
pr(hs.get('action'), c_xi, c_yi1, c_yi2, _mx, _my)
}
}
}
function drag_plan(id, level) {
let [dstate, dx, dy] = ui.drag(id)
let hs = ui.hovers(id)
let cs = ui.captured(id)
let lv = plan.levels[level]
let x0 = plan.x
let y0 = plan.y
let scale = plan.scale
dx = dx / scale
dy = dy / scale
let px = 1 / scale
if (dstate == 'hover') {
let action = hs.get('action')
if (action == 'move_wall_v')
ui.set_cursor(hs.get('invert') ? 'row-resize' : 'col-resize')
/*
let opening = hs.get('opening')
if (opening) {
let wr = hs.get('wall_run')
let i = hs.get('pi')
let v = wall_vert(wr, i)
ui.set_cursor(cursors[v ? 'y' : 'x'])
}
*/
}
if (dstate == 'drag') {
let action = hs.get('action')
if (action) {
cs.set('action', action)
if (action == 'move_wall_v') {
let xi = hs.get('xi' )
let yi1 = hs.get('yi1')
let yi2 = hs.get('yi2')
let invert = hs.get('invert')
let xi1 = wall_v_start_move(plan, invert, lv, xi, yi1, yi2)
cs.set('xi' , xi1)
cs.set('yi1' , yi1)
cs.set('yi2' , yi2)
cs.set('invert' , invert)
// add any movable _|_ walls.
let t1 = point_type(lv, vert, c_xi, c_yi1)
let t2 = point_type(lv, vert, c_xi, c_yi2)
if (t1 == 'joint')
cs.set('')
}
}
/*
let op = hs.get('opening')
if (op) {
let wr = hs.get('wall_run')
let i = hs.get('pi')
let v = wall_vert(wr, i)
let ranges = opening_move_ranges(op, plan)
cs.set('opening' , op)
cs.set('wall_run' , wr)
cs.set('pi' , i)
cs.set('d0' , op.d)
cs.set('move_ranges' , ranges)
ui.set_cursor(cursors[v ? 'y' : 'x'])
}
*/
}
if (dstate == 'drag' || dstate == 'dragging' || dstate == 'drop') {
let action = cs.get('action')
if (action == 'move_wall_v') {
let xi = cs.get('xi' )
let yi1 = cs.get('yi1' )
let yi2 = cs.get('yi2' )
let x = cs.get('x' )
let invert = cs.get('invert' )
let x1 = x + (invert ? dy : dx)
let xi2 = wall_v_move(plan, invert, lv, xi, x1)
cs.set('xi', xi2)
}
/*
let op = cs.get('opening')
if (op) {
let wr = cs.get('wall_run')
let i = cs.get('pi')
let v = wall_vert(wr, i)
let s = wall_sign(wr, i)
let d0 = cs.get('d0')
let ranges = cs.get('move_ranges')
ui.set_cursor(cursors[v ? 'y' : 'x'])
let scale = 1 // TODO
let d = d0 + s * (v ? dy : dx) / scale
for (let [wr1, i1, min_d, max_d] of ranges) {
if (wr1 == wr && i1 == i) {
op.d = clamp(d, min_d, max_d)
break
}
}
}
*/
}
}
function plan_bb(plan) {
let bbx1 = plan.xs[0]
let bby1 = plan.ys[0]
let bbx2 = plan.xs[plan.xs.length-1]
let bby2 = plan.ys[plan.ys.length-1]
return [bbx1, bby1, bbx2, bby2]
}
function face_bb(plan, vert) { // returns (x1, y1, x2, y2, min_depth, max_depth)
let [x1, y1, x2, y2] = plan_bb(plan)
let h = 0
for (let lv of plan.levels)
h += lv.h
if (vert)
return [y1, 0, y2, h, x1, x2]
else
return [x1, 0, x2, h, y1, y2]
}
function bb_scale(w, h, bb) {
let [x1, y1, x2, y2, d1, d2] = bb
let bw = x2 - x1
let bh = y2 - y1
let scale = min(
w / bw,
h / bh
)
return [
scale,
pix(x1, scale),
pix(y1, scale),
d1, d2,
]
}
function plan_scale(plan, w, h) {
return bb_scale(w, h, plan_bb(plan))
}
function face_scale(plan, vert, w, h) {
return bb_scale(w, h, face_bb(plan, vert))
}
// plan view widget ----------------------------------------------------------
let PLAN_VIEW_ID = ui.S-1
let PLAN_VIEW_PLAN = ui.S+0
let PLAN_VIEW_LEVEL = ui.S+1
ui.box_widget('plan_view', {
create: function(cmd, id, plan, level, fr, align, valign, min_w, min_h) {
level = level ?? 0
drag_plan(id, level)
return ui.cmd_box(cmd, fr, align, valign, min_w, min_h,
id, plan, level,
)
},
after_position: function(a, i, axis) {
let plan = a[i+PLAN_VIEW_PLAN]
if (!axis) {
plan.scale = 1/0
return
}
let w = a[i+2]
let h = a[i+3]
let [scale, x, y] = plan_scale(plan, w, h)
plan.scale = min(plan.scale, scale)
plan.x = x
plan.y = y
},
draw: function(a, i) {
let x = a[i+0]
let y = a[i+1]
let w = a[i+2]
let h = a[i+3]
let id = a[i+PLAN_VIEW_ID]
let plan = a[i+PLAN_VIEW_PLAN]
let level = a[i+PLAN_VIEW_LEVEL]
// let [scale, plan_x, plan_y] = plan_scale(plan, w, h)
let scale = plan.scale
let hs = ui.hit(id)
let cx = ui.cx
cx.save()
cx.translate(x - plan.x, y - plan.y)
// draw grid walls
cx.beginPath()
let lv = plan.levels[level]
for (let i = 0; i <= 1; i++) {
let xs = i ? plan.ys : plan.xs
let ys = i ? plan.xs : plan.ys
let walls_h = i ? lv.walls_v : lv.walls_h
if (walls_h) {
for (let yi = 0, lines = walls_h, n = lines.length; yi < n; yi++) {
let y = pix(ys[yi], scale)
for (let xi = 0, a = lines[yi], n = a.length; xi < n; xi++) {
if (a[xi]) {
let x1 = pix(xs[xi+0], scale)
let x2 = pix(xs[xi+1], scale)
if (i) {
cx.moveTo(y, x1)
cx.lineTo(y, x2)
} else {
cx.moveTo(x1, y)
cx.lineTo(x2, y)
}
}
}
}
}
}
cx.strokeStyle = ui.fg_color('text')
cx.stroke()
// draw walls
for (let wr of plan.wall_runs) {
if ((wr.level ?? 0) != level)
continue
draw_wall_openings(cx, scale, wr, hs)
}
let furniture = plan.levels[level].furniture
if (furniture)
for (let e of furniture) {
let e_class = comp[e.type]
let [x, y, w, h] = e_class.plan_rect(e, scale)
cx.save()
cx.translate(x, y)
e_class.draw_plan(cx, e, w, h)
cx.restore()
}
cx.restore()
},
hit: function(a, i) {
let x = a[i+0]
let y = a[i+1]
let w = a[i+2]
let h = a[i+3]
let id = a[i+PLAN_VIEW_ID]
let plan = a[i+PLAN_VIEW_PLAN]
let level = a[i+PLAN_VIEW_LEVEL]
let hs = ui.hovers(id)
let cs = ui.captured(id)
if (cs)
return
let cx = ui.cx
//cx.save()
//cx.translate(x00, y00)
// cx.translate(-plan_x, -plan_y)
//ui.update_mouse()
let lv = plan.levels[level]
hit_plan(plan, lv, id, x, y)
//cx.restore()
//ui.update_mouse()
},
})
// face view widget ----------------------------------------------------------
// sort walls by depth.
function sort_face_walls(plan, face_vert, face_sign) {
let walls = [] // [[wr1, i1], ...]
for (let wr of plan.wall_runs) {
if (!wr.exterior)
continue
for (let i = 0, n = wr.walls.length; i < n; i++) {
if (wall_vert(wr, i) != face_vert)
continue
if (wall_sign(wr, i) != face_sign)
continue
walls.push([wr, i])
}
}
walls.sort(function(e1, e2) {
let y1 = wall_Y(e1[0], e1[1])
let y2 = wall_Y(e2[0], e2[1])
return y1 < y2 ? face_sign : y1 > y2 ? -face_sign : 0
})
return walls
}
function wall_face_rect(plan, lv, wr, i, scale) {
let h = pix(lv.h, scale)
let v = wall_vert(wr, i)
let p1 = wall_p1(wr, i+0, true, null, scale)
let p2 = wall_p1(wr, i+1, true, null, scale)
let X = v ? 1 : 0
let Y = v ? 0 : 1
let x1 = p1[X]
let x2 = p2[X]
let x = min(x1, x2)
let w = max(x1, x2) - x
let y = 0
return [x, y, w, h, p1[Y]] // (x, y, w, h, z)
}
function hit_opening_face(plan, lv, wr, i, id, scale) {
let wall = wr.walls[i]
if (!wall.openings)
return
for (let op of wall.openings) {
let [x, y, w, h, rotation] = opening_face_rect(op, wr, i, lv, scale)
let cx = ui.cx
cx.save()
cx.translate(x, y)
cx.rotate(rotation)
ui.update_mouse()
let hit = ui.hit_rect(- 5, - 5, w + 10, h + 10)
cx.restore()
if (hit) {
let hs = ui.hover(id)
hs.set('opening', op)
return true
}
}
}
function hit_wall_face(plan, lv, wr, i, id, scale) {
let [x, y, w, h] = wall_face_rect(plan, lv, wr, i, scale)
if (ui.hit_rect(x, y, w, h)) {
let hs = ui.hover(id)
//hs.set('wall_run', wr)
//hs.set('pi', i)
//hs.set('side', h ? 'y' : 'x')
return true
}
}
function hit_face(plan, lv, level_num, walls, id, scale, transparent_walls) {
for (let [wr, i] of walls) {
if ((wr.level ?? 1) != level_num)
continue
if (hit_opening_face(plan, lv, wr, i, id, scale))
return true
if (!transparent_walls && hit_wall_face(plan, lv, wr, i, id, scale))
return true
}
}
let FACE_VIEW_ID = ui.S-1
let FACE_VIEW_PLAN = ui.S+0
let FACE_VIEW_FACE = ui.S+1
ui.box_widget('face_view', {
create: function(cmd, id, plan, face, fr, align, valign, min_w, min_h) {
let [dstate, dx, dy] = ui.drag(id)
return ui.cmd_box(cmd, fr, align, valign, min_w, min_h,
id, plan, face
)
},
draw: function(a, i) {
let x00 = a[i+0]
let y00 = a[i+1]
let w = a[i+2]
let h = a[i+3]
let id = a[i+FACE_VIEW_ID]
let plan = a[i+FACE_VIEW_PLAN]
let face = a[i+FACE_VIEW_FACE]
return
// face is 1,2,3,4 going clockwise from the left-to-right face.
let face_vert = (face-1) & 1
let face_sign = face <= 2 ? 1 : -1
let [scale, face_x, face_y, min_depth, max_depth] = face_scale(plan, face_vert, w, h)
let hs = ui.hit(id)
let cx = ui.cx
cx.beginPath()
cx.save()
cx.translate(x00, y00)
cx.translate(-face_x, -face_y)
let walls = sort_face_walls(plan, face_vert, face_sign)
for (let level_num = plan.levels.length-1; level_num >= 0; level_num--) {
let lv = plan.levels[level_num]
let h = pix(lv.h, scale)
// draw walls (offset face) in depth order.
let X = face_vert ? 1 : 0
let Y = face_vert ? 0 : 1
for (let [wr, i] of walls) {
if ((wr.level ?? 0) != level_num)
continue
let [x, y, w, h, z] = wall_face_rect(plan, lv, wr, i, scale)
let depth = lerp(z, min_depth * scale, max_depth * scale, 0, 1)
cx.beginPath()
cx.rect(x, y, w, h)
cx.fillStyle = ui.hsl_adjust(ui.bg_color_hsl('bg3'), 1, 1, depth, 1 - depth/4)
cx.fill()
cx.beginPath()
cx.rect(x + .5, .5, w, h)
cx.strokeStyle = ui.fg_color('text')
cx.stroke()
// draw openings.
let wall = wr.walls[i]
let ops = wall.openings
if (ops) {
for (let op of ops) {
let [x, y, w, h] = opening_face_rect(op, wr, i, lv, scale)
cx.save()
cx.translate(x, y)
cx.beginPath()
cx.rect(0, 0, w, h)
cx.fillStyle = ui.bg_color('bg')
cx.fill()
let op_class = comp[op.type]
op_class.draw_face(cx, op, w, h, hs)
cx.restore()
}
}
}
cx.translate(0, h)
}
cx.restore()
},
hit: function(a, i) {
let x00 = a[i+0]
let y00 = a[i+1]
let w = a[i+2]
let h = a[i+3]
let id = a[i+FACE_VIEW_ID]
let plan = a[i+FACE_VIEW_PLAN]
let face = a[i+FACE_VIEW_FACE]
return
// face is 1,2,3,4 going clockwise from the left-to-right face.
let face_vert = (face-1) & 1
let face_sign = face <= 2 ? 1 : -1
let hs = ui.hovers(id)
let cs = ui.captured(id)
if (!cs) {
let [scale, face_x, face_y] = face_scale(plan, face_vert, w, h)
let cx = ui.cx
cx.save()
cx.translate(x00, y00)
cx.translate(-face_x, -face_y)
ui.update_mouse()
let walls = sort_face_walls(plan, face_vert, face_sign)
walls.reverse()
let y = 0
for (let level_num = plan.levels.length-1; level_num >= 0; level_num--) {
let lv = plan.levels[level_num]
if (hit_face(plan, lv, level_num, walls, id, scale, true))
break
y += pix(lv.h, scale)
cx.translate(0, y)
ui.update_mouse()
}
cx.restore()
ui.update_mouse()
}
},
})
// openings ------------------------------------------------------------------
function draw_wall_openings(cx, scale, wr, hs) {
return
let i = -1
for (let wall of wr.walls) {
i++
if (wall.openings) {
for (let op of wall.openings) {
let op_class = comp[op.type]
let [x, y, w, h, rotation] = opening_plan_rect(op, wr, i, scale)
cx.save()
cx.translate(x, y)
cx.rotate(rotation)
cx.beginPath()
cx.rect(0, 0, w, h)
cx.fillStyle = ui.bg_color('bg')
cx.fill()
op_class.draw_plan(cx, op, w, h, hs)
cx.restore()
}
}
}
}
function hit_opening_plan(wr, id, scale) {
let i = -1
for (let wall of wr.walls) {
i++
if (!wall.openings)
continue
for (let op of wall.openings) {
let op_class = comp[op.type]
let [x, y, w, h, rotation] = opening_plan_rect(op, wr, i, scale)
let cx = ui.cx
cx.save()
cx.translate(x, y)
cx.rotate(rotation)
ui.update_mouse()
let hit = ui.hit_rect(- 5, - 5, w + 10, h + 10)
cx.restore()
ui.update_mouse()
if (hit) {
let hs = ui.hover(id)
hs.set('opening' , op)
hs.set('wall_run', wr)
hs.set('pi' , i)
return true
}
}
}
}
function opening_move_ranges(op, plan) {
let w = op.w
let op_class = comp[op.type]
let exterior = op_class.exterior_only
let ranges = []
for (let wr of plan.wall_runs) {
if (exterior && !wr.exterior)
continue
let i = -1
for (let wall of wr.walls) {
i++
// find the min and max distances on an empty wall.
let X1m = wall_X1(wr, i)
let X1o = wall_X1(wr, i, true)
let X2m = wall_X2(wr, i)
let X2o = wall_X2(wr, i, true)
let o1, o2
if (wall_sign(wr, i) > 0) {
o1 = max(0, X1o - X1m)
o2 = max(0, X2m - X2o)
} else {
o1 = max(0, X1m - X1o)
o2 = max(0, X2o - X2m)
}
let min_d = o1
let max_d = abs(X2m - X1m) - op.w - o2
if (max_d - min_d < op.w)
continue // opening won't fit the free space.
// find the actual spaces in between other openings.
if (wall.openings) {
let op0
for (let j = 0, n = wall.openings.length; j <= n; j++) {
let op1 = j < n ? wall.openings[j] : null
if (op1 == op)
continue
let d0 = op0 ? op0.d + op0.w : min_d
let d1 = op1 ? op1.d : max_d
if (d1 - d0 < 0)
continue // opening won't fit this space.
ranges.push([wr, i, d0, d1])
op0 = op1
}
} else {
ranges.push([wr, i, min_d, max_d])
}
}
}
return ranges
}
comp.door = {}
comp.window = {}
comp.window.exterior_only = true // can only be put on exterior walls
// in pixels, relative to widget, aligned to plan walls.
function opening_plan_rect(e, wr, i, scale) {
let t = pix(wr.thickness, scale)
let r = [0, 0, 0, 0, 0] // (x, y, w, h, rotation)
r[0] = pix(e.sx || 0, scale)
r[1] = pix(e.sy || 0, scale)
r[2] = pix(e.w, scale)
r[3] = t
if (i != null) {
let p1 = wall_p1 (wr, i, null, null, scale)
let vert = wall_vert (wr, i)
let sign = wall_sign (wr, i)
let osign = wall_offset_sign (wr, i)
let X = vert ? 1 : 0
let Y = vert ? 0 : 1
r[0] = p1[0]
r[1] = p1[1]
let d = pix(e.d, scale)
if (!vert) {
r[0] += sign * d
r[1] -= sign * t
r[4] = sign < 0 ? -180 * rad : 0
} else if (vert) {
r[0] += sign * t
r[1] += sign * d
r[4] = sign * 90 * rad
}
}
return r
}