forked from HACKERALERT/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressIndicator.go
105 lines (85 loc) · 2.58 KB
/
ProgressIndicator.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
package giu
import (
"image"
"math"
"time"
"github.com/Picocrypt/imgui-go"
)
var _ Disposable = &progressIndicatorState{}
type progressIndicatorState struct {
angle float64
stop bool
}
func (ps *progressIndicatorState) update() {
ticker := time.NewTicker(time.Second / 60)
for !ps.stop {
if ps.angle > 6.2 {
ps.angle = 0
}
ps.angle += 0.1
Update()
<-ticker.C
}
ticker.Stop()
}
// Dispose implements Disposable interface
func (ps *progressIndicatorState) Dispose() {
ps.stop = true
}
// static check to ensure if ProgressIndicatorWidget implements Widget interface
var _ Widget = &ProgressIndicatorWidget{}
// ProgressIndicatorWidget represents progress indicator widget
// see examples/extrawidgets/
type ProgressIndicatorWidget struct {
internalID string
width float32
height float32
radius float32
label string
}
// ProgressIndicator creates a new ProgressIndicatorWidget
func ProgressIndicator(label string, width, height, radius float32) *ProgressIndicatorWidget {
return &ProgressIndicatorWidget{
internalID: "###giu-progress-indicator",
width: width * Context.GetPlatform().GetContentScale(),
height: height * Context.GetPlatform().GetContentScale(),
radius: radius * Context.GetPlatform().GetContentScale(),
label: label,
}
}
// Build implements Widget interface
func (p *ProgressIndicatorWidget) Build() {
// State exists
if s := Context.GetState(p.internalID); s == nil {
// Register state and start go routine
ps := progressIndicatorState{angle: 0.0, stop: false}
Context.SetState(p.internalID, &ps)
go ps.update()
} else {
state := s.(*progressIndicatorState)
child := Child().Border(false).Size(p.width, p.height).Layout(Layout{
Custom(func() {
// Process width and height
width, height := GetAvailableRegion()
canvas := GetCanvas()
pos := GetCursorScreenPos()
centerPt := pos.Add(image.Pt(int(width/2), int(height/2)))
centerPt2 := image.Pt(
int(float64(p.radius)*math.Sin(state.angle)+float64(centerPt.X)),
int(float64(p.radius)*math.Cos(state.angle)+float64(centerPt.Y)),
)
color := imgui.CurrentStyle().GetColor(imgui.StyleColorText)
rgba := Vec4ToRGBA(color)
canvas.AddCircle(centerPt, p.radius, rgba, int(p.radius), p.radius/20.0)
canvas.AddCircleFilled(centerPt2, p.radius/5, rgba)
// Draw text
if len(p.label) > 0 {
labelWidth, _ := CalcTextSize(tStr(p.label))
labelPos := centerPt.Add(image.Pt(-1*int(labelWidth/2), int(p.radius+p.radius/5+8)))
canvas.AddText(labelPos, rgba, p.label)
}
}),
})
child.Build()
}
}