/
textbutton.go
168 lines (140 loc) · 3.31 KB
/
textbutton.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
package gui
import (
"github.com/veandco/go-sdl2/sdl"
)
type TextButton struct {
X int32
Y int32
Width int32
Height int32
BackgroundColor sdl.Color
OutlineColor sdl.Color
MarginX int32
MarginY int32
PaddingX int32
PaddingY int32
Text string
IsHovering bool
IsHolding bool
Callback func(i *TextButton)
FontStyle string
FontSize int32
}
func NewTextButton(t string, x int32, y int32) *TextButton {
SetFont("normal", 14)
w, h := GetTextSize(t)
paddingX := int32(5)
paddingY := int32(0)
width := int32(w) + (paddingX * 2)
height := int32(h) + (paddingY * 2)
return &TextButton{
X: x,
Y: y,
Width: width,
Height: height,
BackgroundColor: BUTTON_PRIMARY,
OutlineColor: BUTTON_PRIMARY,
MarginX: 0,
MarginY: 0,
PaddingX: paddingX,
PaddingY: paddingY,
Text: t,
FontStyle: "normal",
FontSize: 14,
}
}
func (b *TextButton) SetFont(fontStyle string, fontSize int32) {
b.FontStyle = fontStyle
b.FontSize = fontSize
// update width/height
SetFont(fontStyle, fontSize)
w, h := GetTextSize(b.Text)
b.Width = int32(w) + b.PaddingX*2
b.Height = int32(h) + b.PaddingY*2
}
func (b *TextButton) MouseButtonEvent(event *sdl.MouseButtonEvent) {
if b.IsHovering {
// 0 here means UP. idk what it is set to in the library
if event.Button == sdl.BUTTON_LEFT {
if event.State == 0 {
// MOUSE UP
b.Trigger()
b.SetHolding(false)
} else if event.State == 1 {
// MOUSE DOWN
b.SetHolding(true)
}
}
}
}
func (b *TextButton) SetHolding(which bool) {
if which {
b.BackgroundColor = BUTTON_LIGHTER
b.OutlineColor = BUTTON_LIGHTER
} else {
b.BackgroundColor = BUTTON_PRIMARY
b.OutlineColor = BUTTON_PRIMARY
}
b.IsHolding = which
}
func (b *TextButton) SetCallback(f func(i *TextButton)) {
b.Callback = f
}
func (b *TextButton) Trigger() {
if b.Callback != nil {
b.Callback(b)
}
}
func (b *TextButton) SetMargin(x int32, y int32) {
b.MarginX = x
b.MarginY = y
}
func (b *TextButton) SetPadding(x int32, y int32) {
b.PaddingX = x
b.PaddingY = y
}
func (b *TextButton) SetHover(which bool) {
if which {
b.BackgroundColor = BUTTON_LIGHT
b.OutlineColor = BUTTON_LIGHT
} else {
b.BackgroundColor = BUTTON_PRIMARY
b.OutlineColor = BUTTON_PRIMARY
}
b.IsHovering = which
}
func (b *TextButton) Draw() {
originX := b.X + b.MarginX
originY := b.Y + b.MarginY
rect := sdl.Rect{X: originX, Y: originY, W: b.Width, H: b.Height}
// Background
SetColor(b.BackgroundColor)
DrawFilledRect(&rect)
// Outline
SetColor(b.OutlineColor)
DrawLineRect(&rect)
// Text
SetColor(COLOR_WHITE)
SetFont(b.FontStyle, b.FontSize)
textW, textH := GetTextSize(b.Text)
textX := b.X + (b.Width / 2) - (int32(textW) / 2)
textY := b.Y + (b.Height / 2) - (int32(textH) / 2)
Print(b.Text, textX, textY)
}
func (b *TextButton) GetPos() (int32, int32) {
return b.X, b.Y
}
func (b *TextButton) SetPos(x int32, y int32) {
b.X = x
b.Y = y
}
func (b *TextButton) GetSize() (int32, int32) {
return b.Width, b.Height
}
func (b *TextButton) SetSize(w int32, h int32) {
b.Width = w
b.Height = h
}
func (b *TextButton) KeyboardEvent(keyType uint32, key sdl.Keysym) {
return
}