forked from aarzilli/nucular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
1045 lines (938 loc) · 25.6 KB
/
context.go
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
package nucular
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
"time"
"github.com/aarzilli/nucular/command"
"github.com/aarzilli/nucular/rect"
nstyle "github.com/aarzilli/nucular/style"
"github.com/golang/freetype/raster"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"golang.org/x/mobile/event/mouse"
)
type context struct {
mw MasterWindow
Input Input
Style nstyle.Style
Windows []*Window
DockedWindows dockedTree
changed int32
activateEditor *TextEditor
cmds []command.Command
trashFrame bool
autopos image.Point
finalCmds command.Buffer
dockedWindowFocus int
floatWindowFocus int
scrollwheelFocus int
dockedCnt int
cmdstim []time.Duration // contains timing for all commands
}
func contextAllCommands(ctx *context) {
ctx.cmds = ctx.cmds[:0]
for i, w := range ctx.Windows {
ctx.cmds = append(ctx.cmds, w.cmds.Commands...)
if i == 0 {
ctx.DockedWindows.Walk(func(w *Window) *Window {
ctx.cmds = append(ctx.cmds, w.cmds.Commands...)
return w
})
}
}
ctx.cmds = append(ctx.cmds, ctx.finalCmds.Commands...)
return
}
func (ctx *context) setupMasterWindow(layout *panel, updatefn UpdateFn) {
ctx.Windows = append(ctx.Windows, createWindow(ctx, ""))
ctx.Windows[0].idx = 0
ctx.Windows[0].layout = layout
ctx.Windows[0].flags = layout.Flags | WindowNonmodal
ctx.Windows[0].updateFn = updatefn
}
func (ctx *context) Update() {
for count := 0; count < 2; count++ {
contextBegin(ctx, ctx.Windows[0].layout)
for i := 0; i < len(ctx.Windows); i++ {
ctx.Windows[i].began = false
}
ctx.Restack()
ctx.FindFocus()
for i := 0; i < len(ctx.Windows); i++ { // this must not use range or tooltips won't work
ctx.updateWindow(ctx.Windows[i])
if i == 0 {
t := ctx.DockedWindows.Update(ctx.Windows[0].Bounds, ctx.Style.Scaling)
if t != nil {
ctx.DockedWindows = *t
}
}
}
contextEnd(ctx)
if !ctx.trashFrame {
break
} else {
ctx.Reset()
}
}
}
func (ctx *context) updateWindow(win *Window) {
if win.updateFn != nil {
win.specialPanelBegin()
win.updateFn(win)
}
if !win.began {
win.close = true
return
}
if win.title == tooltipWindowTitle {
win.close = true
}
if win.flags&windowPopup != 0 {
panelEnd(ctx, win)
}
}
func contextBegin(ctx *context, layout *panel) {
for _, w := range ctx.Windows {
w.usingSub = false
w.curNode = w.rootNode
w.close = false
w.widgets.reset()
w.cmds.Reset()
}
ctx.finalCmds.Reset()
ctx.DockedWindows.Walk(func(w *Window) *Window {
w.usingSub = false
w.curNode = w.rootNode
w.close = false
w.widgets.reset()
w.cmds.Reset()
return w
})
ctx.trashFrame = false
ctx.Windows[0].layout = layout
panelBegin(ctx, ctx.Windows[0], "")
layout.Offset = &ctx.Windows[0].Scrollbar
}
func contextEnd(ctx *context) {
panelEnd(ctx, ctx.Windows[0])
}
func (ctx *context) Reset() {
prevNumWindows := len(ctx.Windows)
for i := 0; i < len(ctx.Windows); i++ {
if ctx.Windows[i].close {
if i != len(ctx.Windows)-1 {
copy(ctx.Windows[i:], ctx.Windows[i+1:])
i--
}
ctx.Windows = ctx.Windows[:len(ctx.Windows)-1]
}
}
for i := range ctx.Windows {
ctx.Windows[i].idx = i
}
if prevNumWindows == 2 && len(ctx.Windows) == 1 && ctx.Input.Mouse.valid {
ctx.DockedWindows.Walk(func(w *Window) *Window {
if w.flags&windowDocked == 0 {
return w
}
for _, b := range []mouse.Button{mouse.ButtonLeft, mouse.ButtonRight, mouse.ButtonMiddle} {
btn := ctx.Input.Mouse.Buttons[b]
if btn.Clicked && w.Bounds.Contains(btn.ClickedPos) {
ctx.dockedWindowFocus = w.idx
return w
}
}
return w
})
}
ctx.activateEditor = nil
in := &ctx.Input
in.Mouse.Buttons[mouse.ButtonLeft].Clicked = false
in.Mouse.Buttons[mouse.ButtonMiddle].Clicked = false
in.Mouse.Buttons[mouse.ButtonRight].Clicked = false
in.Mouse.ScrollDelta = 0
in.Mouse.Prev.X = in.Mouse.Pos.X
in.Mouse.Prev.Y = in.Mouse.Pos.Y
in.Mouse.Delta = image.Point{}
in.Keyboard.Keys = in.Keyboard.Keys[0:0]
}
func (ctx *context) Restack() {
clicked := false
for _, b := range []mouse.Button{mouse.ButtonLeft, mouse.ButtonRight, mouse.ButtonMiddle} {
if ctx.Input.Mouse.Buttons[b].Clicked && ctx.Input.Mouse.Buttons[b].Down {
clicked = true
break
}
}
if !clicked {
return
}
ctx.dockedWindowFocus = 0
nonmodalToplevel := false
var toplevelIdx int
for i := len(ctx.Windows) - 1; i >= 0; i-- {
if ctx.Windows[i].flags&windowTooltip == 0 {
toplevelIdx = i
nonmodalToplevel = ctx.Windows[i].flags&WindowNonmodal != 0
break
}
}
if !nonmodalToplevel {
return
}
// toplevel window is non-modal, proceed to change the stacking order if
// the user clicked outside of it
restacked := false
found := false
for i := len(ctx.Windows) - 1; i > 0; i-- {
if ctx.Windows[i].flags&windowTooltip != 0 {
continue
}
if ctx.restackClick(ctx.Windows[i]) {
found = true
if toplevelIdx != i {
newToplevel := ctx.Windows[i]
copy(ctx.Windows[i:toplevelIdx], ctx.Windows[i+1:toplevelIdx+1])
ctx.Windows[toplevelIdx] = newToplevel
restacked = true
}
break
}
}
if restacked {
for i := range ctx.Windows {
ctx.Windows[i].idx = i
}
}
if found {
return
}
ctx.DockedWindows.Walk(func(w *Window) *Window {
if ctx.restackClick(w) && (w.flags&windowDocked != 0) {
ctx.dockedWindowFocus = w.idx
}
return w
})
}
func (ctx *context) FindFocus() {
ctx.floatWindowFocus = 0
for i := len(ctx.Windows) - 1; i >= 0; i-- {
if ctx.Windows[i].flags&windowTooltip == 0 {
ctx.floatWindowFocus = i
break
}
}
ctx.scrollwheelFocus = 0
for i := len(ctx.Windows) - 1; i > 0; i-- {
if ctx.Windows[i].Bounds.Contains(ctx.Input.Mouse.Pos) {
ctx.scrollwheelFocus = i
break
}
}
if ctx.scrollwheelFocus == 0 {
ctx.DockedWindows.Walk(func(w *Window) *Window {
if w.Bounds.Contains(ctx.Input.Mouse.Pos) {
ctx.scrollwheelFocus = w.idx
}
return w
})
}
}
func (ctx *context) Walk(fn WindowWalkFn) {
fn(ctx.Windows[0].title, ctx.Windows[0].Data, false, 0, ctx.Windows[0].Bounds)
ctx.DockedWindows.walkExt(func(t *dockedTree) {
switch t.Type {
case dockedNodeHoriz:
fn("", nil, true, t.Split.Size, rect.Rect{})
case dockedNodeVert:
fn("", nil, true, -t.Split.Size, rect.Rect{})
case dockedNodeLeaf:
if t.W == nil {
fn("", nil, true, 0, rect.Rect{})
} else {
fn(t.W.title, t.W.Data, true, 0, t.W.Bounds)
}
}
})
for _, win := range ctx.Windows[1:] {
if win.flags&WindowNonmodal != 0 {
fn(win.title, win.Data, false, 0, win.Bounds)
}
}
}
func (ctx *context) restackClick(w *Window) bool {
if !ctx.Input.Mouse.valid {
return false
}
for _, b := range []mouse.Button{mouse.ButtonLeft, mouse.ButtonRight, mouse.ButtonMiddle} {
btn := ctx.Input.Mouse.Buttons[b]
if btn.Clicked && btn.Down && w.Bounds.Contains(btn.ClickedPos) {
return true
}
}
return false
}
var cnt = 0
var ln, frect, frectover, brrect, frrect, ftri, circ, fcirc, txt int
func (ctx *context) Draw(wimg *image.RGBA) int {
var txttim, tritim, brecttim, frecttim, frectovertim, frrecttim time.Duration
var t0 time.Time
img := wimg
var painter *myRGBAPainter
var rasterizer *raster.Rasterizer
roundAngle := func(cx, cy int, radius uint16, startAngle, angle float64, c color.Color) {
rasterizer.Clear()
rasterizer.Start(fixed.P(cx, cy))
traceArc(rasterizer, float64(cx), float64(cy), float64(radius), float64(radius), startAngle, angle, false)
rasterizer.Add1(fixed.P(cx, cy))
painter.SetColor(c)
rasterizer.Rasterize(painter)
}
setupRasterizer := func() {
rasterizer = raster.NewRasterizer(img.Bounds().Dx(), img.Bounds().Dy())
painter = &myRGBAPainter{Image: img}
}
if ctx.cmdstim != nil {
ctx.cmdstim = ctx.cmdstim[:0]
}
transparentBorderOptimization := false
for i := range ctx.cmds {
if perfUpdate {
t0 = time.Now()
}
icmd := &ctx.cmds[i]
switch icmd.Kind {
case command.ScissorCmd:
img = wimg.SubImage(icmd.Rectangle()).(*image.RGBA)
painter = nil
rasterizer = nil
case command.LineCmd:
cmd := icmd.Line
colimg := image.NewUniform(cmd.Color)
op := draw.Over
if cmd.Color.A == 0xff {
op = draw.Src
}
h1 := int(cmd.LineThickness / 2)
h2 := int(cmd.LineThickness) - h1
if cmd.Begin.X == cmd.End.X {
// draw vertical line
r := image.Rect(cmd.Begin.X-h1, cmd.Begin.Y, cmd.Begin.X+h2, cmd.End.Y)
drawFill(img, r, colimg, r.Min, op)
} else if cmd.Begin.Y == cmd.End.Y {
// draw horizontal line
r := image.Rect(cmd.Begin.X, cmd.Begin.Y-h1, cmd.End.X, cmd.Begin.Y+h2)
drawFill(img, r, colimg, r.Min, op)
} else {
if rasterizer == nil {
setupRasterizer()
}
unzw := rasterizer.UseNonZeroWinding
rasterizer.UseNonZeroWinding = true
var p raster.Path
p.Start(fixed.P(cmd.Begin.X-img.Bounds().Min.X, cmd.Begin.Y-img.Bounds().Min.Y))
p.Add1(fixed.P(cmd.End.X-img.Bounds().Min.X, cmd.End.Y-img.Bounds().Min.Y))
rasterizer.Clear()
rasterizer.AddStroke(p, fixed.I(int(cmd.LineThickness)), nil, nil)
painter.SetColor(cmd.Color)
rasterizer.Rasterize(painter)
rasterizer.UseNonZeroWinding = unzw
}
ln++
case command.RectFilledCmd:
cmd := icmd.RectFilled
if i == 0 {
// first command draws the background, insure that it's always fully opaque
cmd.Color.A = 0xff
}
if transparentBorderOptimization {
transparentBorderOptimization = false
prevcmd := ctx.cmds[i-1].RectFilled
const m = 1<<16 - 1
sr, sg, sb, sa := cmd.Color.RGBA()
a := (m - sa) * 0x101
cmd.Color.R = uint8((uint32(prevcmd.Color.R)*a/m + sr) >> 8)
cmd.Color.G = uint8((uint32(prevcmd.Color.G)*a/m + sg) >> 8)
cmd.Color.B = uint8((uint32(prevcmd.Color.B)*a/m + sb) >> 8)
cmd.Color.A = uint8((uint32(prevcmd.Color.A)*a/m + sa) >> 8)
}
colimg := image.NewUniform(cmd.Color)
op := draw.Over
if cmd.Color.A == 0xff {
op = draw.Src
}
body := icmd.Rectangle()
var lwing, rwing image.Rectangle
// rounding is true if rounding has been requested AND we can draw it
rounding := cmd.Rounding > 0 && int(cmd.Rounding*2) < icmd.W && int(cmd.Rounding*2) < icmd.H
if rounding {
body.Min.X += int(cmd.Rounding)
body.Max.X -= int(cmd.Rounding)
lwing = image.Rect(icmd.X, icmd.Y+int(cmd.Rounding), icmd.X+int(cmd.Rounding), icmd.Y+icmd.H-int(cmd.Rounding))
rwing = image.Rect(icmd.X+icmd.W-int(cmd.Rounding), lwing.Min.Y, icmd.X+icmd.W, lwing.Max.Y)
}
bordopt := false
if ok, border := borderOptimize(icmd, ctx.cmds, i+1); ok {
// only draw parts of body if this command can be optimized to a border with the next command
bordopt = true
if ctx.cmds[i+1].RectFilled.Color.A != 0xff {
transparentBorderOptimization = true
}
border += int(ctx.cmds[i+1].RectFilled.Rounding)
top := image.Rect(body.Min.X, body.Min.Y, body.Max.X, body.Min.Y+border)
bot := image.Rect(body.Min.X, body.Max.Y-border, body.Max.X, body.Max.Y)
drawFill(img, top, colimg, top.Min, op)
drawFill(img, bot, colimg, bot.Min, op)
if border < int(cmd.Rounding) {
// wings need shrinking
d := int(cmd.Rounding) - border
lwing.Max.Y -= d
rwing.Min.Y += d
} else {
// display extra wings
d := border - int(cmd.Rounding)
xlwing := image.Rect(top.Min.X, top.Max.Y, top.Min.X+d, bot.Min.Y)
xrwing := image.Rect(top.Max.X-d, top.Max.Y, top.Max.X, bot.Min.Y)
drawFill(img, xlwing, colimg, xlwing.Min, op)
drawFill(img, xrwing, colimg, xrwing.Min, op)
}
brrect++
} else {
drawFill(img, body, colimg, body.Min, op)
if cmd.Rounding == 0 {
if op == draw.Src {
frect++
} else {
frectover++
}
} else {
frrect++
}
}
if rounding {
drawFill(img, lwing, colimg, lwing.Min, op)
drawFill(img, rwing, colimg, rwing.Min, op)
rangle := math.Pi / 2
if rasterizer == nil {
setupRasterizer()
}
minx := img.Bounds().Min.X
miny := img.Bounds().Min.Y
roundAngle(icmd.X+icmd.W-int(cmd.Rounding)-minx, icmd.Y+int(cmd.Rounding)-miny, cmd.Rounding, -math.Pi/2, rangle, cmd.Color)
roundAngle(icmd.X+icmd.W-int(cmd.Rounding)-minx, icmd.Y+icmd.H-int(cmd.Rounding)-miny, cmd.Rounding, 0, rangle, cmd.Color)
roundAngle(icmd.X+int(cmd.Rounding)-minx, icmd.Y+icmd.H-int(cmd.Rounding)-miny, cmd.Rounding, math.Pi/2, rangle, cmd.Color)
roundAngle(icmd.X+int(cmd.Rounding)-minx, icmd.Y+int(cmd.Rounding)-miny, cmd.Rounding, math.Pi, rangle, cmd.Color)
}
if perfUpdate {
if bordopt {
brecttim += time.Now().Sub(t0)
} else {
if cmd.Rounding > 0 {
frrecttim += time.Now().Sub(t0)
} else {
d := time.Now().Sub(t0)
if op == draw.Src {
frecttim += d
} else {
if d > 8*time.Millisecond {
fmt.Printf("outstanding rect")
}
frectovertim += d
}
}
}
}
case command.TriangleFilledCmd:
cmd := icmd.TriangleFilled
if rasterizer == nil {
setupRasterizer()
}
minx := img.Bounds().Min.X
miny := img.Bounds().Min.Y
rasterizer.Clear()
rasterizer.Start(fixed.P(cmd.A.X-minx, cmd.A.Y-miny))
rasterizer.Add1(fixed.P(cmd.B.X-minx, cmd.B.Y-miny))
rasterizer.Add1(fixed.P(cmd.C.X-minx, cmd.C.Y-miny))
rasterizer.Add1(fixed.P(cmd.A.X-minx, cmd.A.Y-miny))
painter.SetColor(cmd.Color)
rasterizer.Rasterize(painter)
ftri++
if perfUpdate {
tritim += time.Now().Sub(t0)
}
case command.CircleFilledCmd:
if rasterizer == nil {
setupRasterizer()
}
rasterizer.Clear()
startp := traceArc(rasterizer, float64(icmd.X-img.Bounds().Min.X)+float64(icmd.W/2), float64(icmd.Y-img.Bounds().Min.Y)+float64(icmd.H/2), float64(icmd.W/2), float64(icmd.H/2), 0, -math.Pi*2, true)
rasterizer.Add1(startp) // closes path
painter.SetColor(icmd.CircleFilled.Color)
rasterizer.Rasterize(painter)
fcirc++
case command.ImageCmd:
draw.Draw(img, icmd.Rectangle(), icmd.Image.Img, image.Point{}, draw.Src)
case command.TextCmd:
dstimg := wimg.SubImage(img.Bounds().Intersect(icmd.Rectangle())).(*image.RGBA)
d := font.Drawer{
Dst: dstimg,
Src: image.NewUniform(icmd.Text.Foreground),
Face: icmd.Text.Face,
Dot: fixed.P(icmd.X, icmd.Y+icmd.Text.Face.Metrics().Ascent.Ceil())}
start := 0
for i := range icmd.Text.String {
if icmd.Text.String[i] == '\n' {
d.DrawString(icmd.Text.String[start:i])
d.Dot.X = fixed.I(icmd.X)
d.Dot.Y += fixed.I(FontHeight(icmd.Text.Face))
start = i + 1
}
}
if start < len(icmd.Text.String) {
d.DrawString(icmd.Text.String[start:])
}
txt++
if perfUpdate {
txttim += time.Now().Sub(t0)
}
default:
panic(UnknownCommandErr)
}
if dumpFrame {
ctx.cmdstim = append(ctx.cmdstim, time.Since(t0))
}
}
if perfUpdate {
fmt.Printf("triangle: %0.4fms text: %0.4fms brect: %0.4fms frect: %0.4fms frectover: %0.4fms frrect %0.4f\n", tritim.Seconds()*1000, txttim.Seconds()*1000, brecttim.Seconds()*1000, frecttim.Seconds()*1000, frectovertim.Seconds()*1000, frrecttim.Seconds()*1000)
}
cnt++
if perfUpdate /*&& (cnt%100) == 0*/ {
fmt.Printf("ln %d, frect %d, frectover %d, frrect %d, brrect %d, ftri %d, circ %d, fcirc %d, txt %d\n", ln, frect, frectover, frrect, brrect, ftri, circ, fcirc, txt)
ln, frect, frectover, frrect, brrect, ftri, circ, fcirc, txt = 0, 0, 0, 0, 0, 0, 0, 0, 0
}
return len(ctx.cmds)
}
// Returns true if cmds[idx] is a shrunk version of CommandFillRect and its
// color is not semitransparent and the border isn't greater than 128
func borderOptimize(cmd *command.Command, cmds []command.Command, idx int) (ok bool, border int) {
if idx >= len(cmds) {
return false, 0
}
if cmd.Kind != command.RectFilledCmd || cmds[idx].Kind != command.RectFilledCmd {
return false, 0
}
cmd2 := cmds[idx]
if cmd.RectFilled.Color.A != 0xff && cmd2.RectFilled.Color.A != 0xff {
return false, 0
}
border = cmd2.X - cmd.X
if border <= 0 || border > 128 {
return false, 0
}
if shrinkRect(cmd.Rect, border) != cmd2.Rect {
return false, 0
}
return true, border
}
func floatP(x, y float64) fixed.Point26_6 {
return fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}
}
// TraceArc trace an arc using a Liner
func traceArc(t *raster.Rasterizer, x, y, rx, ry, start, angle float64, first bool) fixed.Point26_6 {
end := start + angle
clockWise := true
if angle < 0 {
clockWise = false
}
if !clockWise {
for start < end {
start += math.Pi * 2
}
end = start + angle
}
ra := (math.Abs(rx) + math.Abs(ry)) / 2
da := math.Acos(ra/(ra+0.125)) * 2
//normalize
if !clockWise {
da = -da
}
angle = start
var curX, curY float64
var startX, startY float64
for {
if (angle < end-da/4) != clockWise {
curX = x + math.Cos(end)*rx
curY = y + math.Sin(end)*ry
t.Add1(floatP(curX, curY))
return floatP(startX, startY)
}
curX = x + math.Cos(angle)*rx
curY = y + math.Sin(angle)*ry
angle += da
if first {
first = false
startX, startY = curX, curY
t.Start(floatP(curX, curY))
} else {
t.Add1(floatP(curX, curY))
}
}
}
type myRGBAPainter struct {
Image *image.RGBA
// cr, cg, cb and ca are the 16-bit color to paint the spans.
cr, cg, cb, ca uint32
}
// SetColor sets the color to paint the spans.
func (r *myRGBAPainter) SetColor(c color.Color) {
r.cr, r.cg, r.cb, r.ca = c.RGBA()
}
func (r *myRGBAPainter) Paint(ss []raster.Span, done bool) {
b := r.Image.Bounds()
cr8 := uint8(r.cr >> 8)
cg8 := uint8(r.cg >> 8)
cb8 := uint8(r.cb >> 8)
for _, s := range ss {
s.Y += b.Min.Y
s.X0 += b.Min.X
s.X1 += b.Min.X
if s.Y < b.Min.Y {
continue
}
if s.Y >= b.Max.Y {
return
}
if s.X0 < b.Min.X {
s.X0 = b.Min.X
}
if s.X1 > b.Max.X {
s.X1 = b.Max.X
}
if s.X0 >= s.X1 {
continue
}
// This code mimics drawGlyphOver in $GOROOT/src/image/draw/draw.go.
ma := s.Alpha
const m = 1<<16 - 1
i0 := (s.Y-r.Image.Rect.Min.Y)*r.Image.Stride + (s.X0-r.Image.Rect.Min.X)*4
i1 := i0 + (s.X1-s.X0)*4
if ma != m || r.ca != m {
for i := i0; i < i1; i += 4 {
dr := uint32(r.Image.Pix[i+0])
dg := uint32(r.Image.Pix[i+1])
db := uint32(r.Image.Pix[i+2])
da := uint32(r.Image.Pix[i+3])
a := (m - (r.ca * ma / m)) * 0x101
r.Image.Pix[i+0] = uint8((dr*a + r.cr*ma) / m >> 8)
r.Image.Pix[i+1] = uint8((dg*a + r.cg*ma) / m >> 8)
r.Image.Pix[i+2] = uint8((db*a + r.cb*ma) / m >> 8)
r.Image.Pix[i+3] = uint8((da*a + r.ca*ma) / m >> 8)
}
} else {
for i := i0; i < i1; i += 4 {
r.Image.Pix[i+0] = cr8
r.Image.Pix[i+1] = cg8
r.Image.Pix[i+2] = cb8
r.Image.Pix[i+3] = 0xff
}
}
}
}
type dockedNodeType uint8
const (
dockedNodeLeaf dockedNodeType = iota
dockedNodeVert
dockedNodeHoriz
)
type dockedTree struct {
Type dockedNodeType
Split ScalableSplit
Child [2]*dockedTree
W *Window
}
func (t *dockedTree) Update(bounds rect.Rect, scaling float64) *dockedTree {
if t == nil {
return nil
}
switch t.Type {
case dockedNodeVert:
b0, b1, _ := t.Split.verticalnw(bounds, scaling)
t.Child[0] = t.Child[0].Update(b0, scaling)
t.Child[1] = t.Child[1].Update(b1, scaling)
case dockedNodeHoriz:
b0, b1, _ := t.Split.horizontalnw(bounds, scaling)
t.Child[0] = t.Child[0].Update(b0, scaling)
t.Child[1] = t.Child[1].Update(b1, scaling)
case dockedNodeLeaf:
if t.W != nil {
t.W.Bounds = bounds
t.W.ctx.updateWindow(t.W)
if t.W == nil {
return nil
}
if t.W.close {
t.W = nil
return nil
}
return t
}
return nil
}
if t.Child[0] == nil {
return t.Child[1]
}
if t.Child[1] == nil {
return t.Child[0]
}
return t
}
func (t *dockedTree) walkExt(fn func(t *dockedTree)) {
if t == nil {
return
}
switch t.Type {
case dockedNodeVert, dockedNodeHoriz:
fn(t)
t.Child[0].walkExt(fn)
t.Child[1].walkExt(fn)
case dockedNodeLeaf:
fn(t)
}
}
func (t *dockedTree) Walk(fn func(t *Window) *Window) {
t.walkExt(func(t *dockedTree) {
if t.Type == dockedNodeLeaf && t.W != nil {
t.W = fn(t.W)
}
})
}
func newDockedLeaf(win *Window) *dockedTree {
r := &dockedTree{Type: dockedNodeLeaf, W: win}
r.Split.MinSize = 40
return r
}
func (t *dockedTree) Dock(win *Window, pos image.Point, bounds rect.Rect, scaling float64) (bool, rect.Rect) {
if t == nil {
return false, rect.Rect{}
}
switch t.Type {
case dockedNodeVert:
b0, b1, _ := t.Split.verticalnw(bounds, scaling)
canDock, r := t.Child[0].Dock(win, pos, b0, scaling)
if canDock {
return canDock, r
}
canDock, r = t.Child[1].Dock(win, pos, b1, scaling)
if canDock {
return canDock, r
}
case dockedNodeHoriz:
b0, b1, _ := t.Split.horizontalnw(bounds, scaling)
canDock, r := t.Child[0].Dock(win, pos, b0, scaling)
if canDock {
return canDock, r
}
canDock, r = t.Child[1].Dock(win, pos, b1, scaling)
if canDock {
return canDock, r
}
case dockedNodeLeaf:
v := percentages(bounds, 0.03)
for i := range v {
if v[i].Contains(pos) {
if t.W == nil {
if win != nil {
t.W = win
win.ctx.dockWindow(win)
}
return true, bounds
}
w := percentages(bounds, 0.5)
if win != nil {
if i < 2 {
// horizontal split
t.Type = dockedNodeHoriz
t.Split.Size = int(float64(w[0].H) / scaling)
t.Child[i] = newDockedLeaf(win)
t.Child[-i+1] = newDockedLeaf(t.W)
} else {
// vertical split
t.Type = dockedNodeVert
t.Split.Size = int(float64(w[2].W) / scaling)
t.Child[i-2] = newDockedLeaf(win)
t.Child[-(i-2)+1] = newDockedLeaf(t.W)
}
t.W = nil
win.ctx.dockWindow(win)
}
return true, w[i]
}
}
}
return false, rect.Rect{}
}
func (ctx *context) dockWindow(win *Window) {
win.undockedSz = image.Point{win.Bounds.W, win.Bounds.H}
win.flags |= windowDocked
win.layout.Flags |= windowDocked
ctx.dockedCnt--
win.idx = ctx.dockedCnt
for i := range ctx.Windows {
if ctx.Windows[i] == win {
if i+1 < len(ctx.Windows) {
copy(ctx.Windows[i:], ctx.Windows[i+1:])
}
ctx.Windows = ctx.Windows[:len(ctx.Windows)-1]
return
}
}
}
func (t *dockedTree) Undock(win *Window) {
t.Walk(func(w *Window) *Window {
if w == win {
return nil
}
return w
})
win.flags &= ^windowDocked
win.layout.Flags &= ^windowDocked
win.Bounds.H = win.undockedSz.Y
win.Bounds.W = win.undockedSz.X
win.idx = len(win.ctx.Windows)
win.ctx.Windows = append(win.ctx.Windows, win)
}
func (t *dockedTree) Scale(win *Window, delta image.Point, scaling float64) image.Point {
if t == nil || (delta.X == 0 && delta.Y == 0) {
return image.ZP
}
switch t.Type {
case dockedNodeVert:
d0 := t.Child[0].Scale(win, delta, scaling)
if d0.X != 0 {
t.Split.Size += int(float64(d0.X) / scaling)
if t.Split.Size <= t.Split.MinSize {
t.Split.Size = t.Split.MinSize
}
d0.X = 0
}
if d0 != image.ZP {
return d0
}
return t.Child[1].Scale(win, delta, scaling)
case dockedNodeHoriz:
d0 := t.Child[0].Scale(win, delta, scaling)
if d0.Y != 0 {
t.Split.Size += int(float64(d0.Y) / scaling)
if t.Split.Size <= t.Split.MinSize {
t.Split.Size = t.Split.MinSize
}
d0.Y = 0
}
if d0 != image.ZP {
return d0
}
return t.Child[1].Scale(win, delta, scaling)
case dockedNodeLeaf:
if t.W == win {
return delta
}
}
return image.ZP
}
func (ctx *context) ResetWindows() *DockSplit {
ctx.DockedWindows = dockedTree{}
ctx.Windows = ctx.Windows[:1]
ctx.dockedCnt = 0
return &DockSplit{ctx, &ctx.DockedWindows}
}
type DockSplit struct {
ctx *context
node *dockedTree
}
func (ds *DockSplit) Split(horiz bool, size int) (left, right *DockSplit) {
if horiz {
ds.node.Type = dockedNodeHoriz
} else {
ds.node.Type = dockedNodeVert
}
ds.node.Split.Size = size
ds.node.Child[0] = &dockedTree{Type: dockedNodeLeaf, Split: ScalableSplit{MinSize: 40}}
ds.node.Child[1] = &dockedTree{Type: dockedNodeLeaf, Split: ScalableSplit{MinSize: 40}}
return &DockSplit{ds.ctx, ds.node.Child[0]}, &DockSplit{ds.ctx, ds.node.Child[1]}
}
func (ds *DockSplit) Open(title string, flags WindowFlags, rect rect.Rect, scale bool, updateFn UpdateFn) {
ds.ctx.popupOpen(title, flags, rect, scale, updateFn)
ds.node.Type = dockedNodeLeaf
ds.node.W = ds.ctx.Windows[len(ds.ctx.Windows)-1]
ds.ctx.dockWindow(ds.node.W)
}
func percentages(bounds rect.Rect, f float64) (r [4]rect.Rect) {
pw := int(float64(bounds.W) * f)
ph := int(float64(bounds.H) * f)
// horizontal split
r[0] = bounds
r[0].H = ph
r[1] = bounds
r[1].Y += r[1].H - ph
r[1].H = ph
// vertical split
r[2] = bounds
r[2].W = pw
r[3] = bounds
r[3].X += r[3].W - pw
r[3].W = pw
return
}
func clip(dst *image.RGBA, r *image.Rectangle, src image.Image, sp *image.Point) {
orig := r.Min
*r = r.Intersect(dst.Bounds())
*r = r.Intersect(src.Bounds().Add(orig.Sub(*sp)))
dx := r.Min.X - orig.X
dy := r.Min.Y - orig.Y
if dx == 0 && dy == 0 {