-
Notifications
You must be signed in to change notification settings - Fork 0
/
labeled_gauge.go
133 lines (112 loc) · 3.33 KB
/
labeled_gauge.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
package main
import (
"fmt"
"github.com/AllenDang/giu"
"github.com/AllenDang/imgui-go"
"image"
"image/color"
"math"
)
const (
DegToRad = 0.017453292519943295769236907684886127134428718885417 // N[Pi/180, 50]
RadToDeg = 57.295779513082320876798154814105170332405472466564 // N[180/Pi, 50]
)
var _ giu.Widget = &LabeledGaugeWidget{}
// LabeledGaugeWidget Renders a id and a gauge
type LabeledGaugeWidget struct {
id string
min float32
value float32
format string
max float32
}
// LabeledGauge creates LabeledGaugeWidget.
func LabeledGauge(id string) *LabeledGaugeWidget {
return &LabeledGaugeWidget{
id: id,
min: 0,
max: 1,
format: "%0.1f",
}
}
// Min Set minimum range for value
func (w *LabeledGaugeWidget) Min(min float32) *LabeledGaugeWidget {
w.min = min
return w
}
// Value Set value
func (w *LabeledGaugeWidget) Value(value float32) *LabeledGaugeWidget {
w.value = value
return w
}
// Value Set string to format value, e.g. "%0.1f%"
func (w *LabeledGaugeWidget) Format(format string) *LabeledGaugeWidget {
w.format = format
return w
}
// Max Set maximum range for value
func (w *LabeledGaugeWidget) Max(max float32) *LabeledGaugeWidget {
w.max = max
return w
}
func shortenText(width float32, text string) string {
cut := 2
newText := text
for {
w, _ := giu.CalcTextSize(newText)
if w < width {
return newText
}
if len(newText) <= cut {
return newText
}
s := len(text)/2 - cut/2
newText = text[:s] + "…" + text[s+cut:]
cut++
}
}
// Build implements Widget interface.
func (w *LabeledGaugeWidget) Build() {
width, height := giu.GetAvailableRegion()
defaultFonts := giu.GetDefaultFonts()
font := defaultFonts[0].SetSize(12)
if giu.PushFont(font) {
defer giu.PopFont()
}
label := shortenText(width, w.id)
labelWidth, labelHeight := giu.CalcTextSize(label)
textFg := giu.Vec4ToRGBA(imgui.CurrentStyle().GetColor(imgui.StyleColorText))
gaugeBg := color.RGBA{70, 70, 70, 255}
currentFg := color.RGBA{255, 255, 255, 255}
canvas := giu.GetCanvas()
topLeftPos := giu.GetCursorScreenPos()
gaugeHeight := height - labelHeight
gaugeCenterPos := topLeftPos.Add(image.Pt(0, int(labelHeight))).Add(image.Pt(int(width/2), int(gaugeHeight/2)))
var radius float32
if width > gaugeHeight {
radius = gaugeHeight / 1.8
} else {
radius = width / 2
}
radius *= 0.8
startAngle := 315 * DegToRad
endAngle := 45 * DegToRad
ratio := (w.value - w.min) / (w.max - w.min)
valueAngle := startAngle - math.Min(1, math.Max(0, float64(ratio)))*(startAngle-endAngle)
canvas.PathClear()
canvas.PathArcTo(gaugeCenterPos, radius, float32(startAngle+90*DegToRad), float32(endAngle+90*DegToRad), 64)
canvas.PathFillConvex(gaugeBg)
canvas.PathStroke(textFg, true, 2)
canvas.AddCircleFilled(gaugeCenterPos, 4, currentFg)
gaugeOtPt := image.Pt(
int(float64(radius)*math.Sin(valueAngle)+float64(gaugeCenterPos.X)),
int(float64(radius)*math.Cos(valueAngle)+float64(gaugeCenterPos.Y)),
)
canvas.AddLine(gaugeCenterPos, gaugeOtPt, currentFg, 2)
labelPos := image.Pt(gaugeCenterPos.X-int(labelWidth/2), topLeftPos.Y)
canvas.AddText(labelPos, textFg, label)
valueText := fmt.Sprintf(w.format, w.value)
valueWidth, valueHeight := giu.CalcTextSize(valueText)
valuePos := image.Pt(gaugeCenterPos.X-int(valueWidth/2), topLeftPos.Y+int(height-valueHeight*1.1))
canvas.AddText(valuePos, textFg, valueText)
}