/
progressbar.go
154 lines (137 loc) · 3.77 KB
/
progressbar.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
/*
* progressbar.go
*
* Copyright 2019 Dariusz Sikora <dev@isangeles.pl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
package mtk
import (
"fmt"
"image/color"
"github.com/gopxl/pixel"
)
// Struct for progress bars.
type ProgressBar struct {
value, max int
labelText string
hovered bool
bgSpr *pixel.Sprite
size pixel.Vec
color color.Color
label *Text
drawArea pixel.Rect
maxSize pixel.Vec
bounds pixel.Rect
}
// NewProgressBar creates new progress bar with IMDraw
// background bar with specified size, color and label text.
func NewProgressBar(size Size, color color.Color) *ProgressBar {
pb := new(ProgressBar)
pb.size = size.BarSize()
pb.color = color
pb.maxSize = size.BarSize()
labelParams := Params{
FontSize: size - 1,
}
pb.label = NewText(labelParams)
return pb
}
// Draw draws progress bar.
func (pb *ProgressBar) Draw(t pixel.Target, matrix pixel.Matrix) {
widthDiff := ConvSize(pb.maxSize.X - pb.Size().X)
barPos := pixel.V(-widthDiff/2, 0)
mx := matrix.Moved(barPos)
pb.drawArea = MatrixToDrawArea(mx, pb.Size())
// Background.
if pb.bgSpr != nil {
pb.bgSpr.Draw(t, mx)
} else {
DrawRectangle(t, pb.DrawArea(), pb.color)
}
// Label.
if pb.hovered {
pb.label.Draw(t, matrix)
}
}
// Update updates progress bar.
func (pb *ProgressBar) Update(win *Window) {
// On-hover.
if pb.DrawArea().Contains(win.MousePosition()) {
pb.hovered = true
} else {
pb.hovered = false
}
}
// SetBackground sets specified sprite as bar
// background, also removes current background color.
func (pb *ProgressBar) SetBackground(p pixel.Picture) {
bounds := pixel.R(0, p.Bounds().Min.Y, 0, p.Bounds().Max.Y)
pb.bgSpr = pixel.NewSprite(p, bounds)
pb.maxSize = pb.bgSpr.Picture().Bounds().Size()
pb.SetColor(nil)
}
// SetColor sets specified color as background color.
func (pb *ProgressBar) SetColor(c color.Color) {
pb.color = c
}
// SetLabel sets specified text as progress label.
func (pb *ProgressBar) SetLabel(t string) {
pb.labelText = t
}
// Size returns bar size.
func (pb *ProgressBar) Size() pixel.Vec {
return pb.bounds.Size()
}
// Value retruns current progress value.
func (pb *ProgressBar) Value() int {
return pb.value
}
// SetValue sets specified value as
// current progress value.
func (pb *ProgressBar) SetValue(val int) {
pb.value = val
pb.updateProgress()
}
// Max retruns maximal progress value.
func (pb *ProgressBar) Max() int {
return pb.max
}
// SetMax sets specified value as progress
// maximal value.
func (pb *ProgressBar) SetMax(max int) {
pb.max = max
pb.updateProgress()
}
// DrawArea returns current last draw area of
// this element.
func (pb *ProgressBar) DrawArea() pixel.Rect {
return pb.drawArea
}
// updateProgress updates bar progress to current
// progress value.
func (pb *ProgressBar) updateProgress() {
valPercent := float64(pb.Value()) * 1.0 / float64(pb.Max())
bgWidth := pb.maxSize.X * valPercent
pb.bounds = pixel.R(0, 0, bgWidth, pb.maxSize.Y)
if pb.bgSpr != nil {
pb.bgSpr = pixel.NewSprite(pb.bgSpr.Picture(), pb.bounds)
}
pb.label.SetText(fmt.Sprintf("%s:%d/%d", pb.labelText, pb.Value(),
pb.Max()))
}