-
Notifications
You must be signed in to change notification settings - Fork 2
/
ButtonWidget.go
182 lines (152 loc) · 5.52 KB
/
ButtonWidget.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
package tbuikit
import (
"github.com/nsf/termbox-go"
)
// A widget which acts like a button. It can be selected and then "pressed"
// via a key binding.
type ButtonWidget struct {
buttonText string
textPosition ScreenPosition
defaultTextColor termbox.Attribute
selectedTextColor termbox.Attribute
defaultBorderColor termbox.Attribute
selectedBorderColor termbox.Attribute
defaultBgColor termbox.Attribute
selectedBgColor termbox.Attribute
calcFunction CalcFunction
rect *Rectangle
isSelectable bool
selected bool
widgetKeyBindings map[interface{}]EventCallback
}
// Draw the button every iteration of the main loop. Figure out where to put the button text within the button,
// make sure the borders get drawn and then draw the text in the figured out location.
func (this *ButtonWidget) Draw() {
if this.rect == nil {
this.CalculateSize()
}
this.drawBorderAndBg()
// Decide where the button text should be drawn. Keep in mind
// the Termboxprintf function still prints a string left-to-right,
// so the X coord is the first rune of the string and the it advances
// to the right.
var x, y int
if this.textPosition == TOP_LEFT {
y = this.rect.Y1 + 1
x = this.rect.X1 + 1
} else if this.textPosition == TOP_RIGHT {
y = this.rect.Y1 + 1
x = this.rect.X2 - len(this.buttonText) // - 1?
} else if this.textPosition == BOTTOM_LEFT {
y = this.rect.Y2 - 1
x = this.rect.X1 + 1
} else if this.textPosition == BOTTOM_RIGHT {
y = this.rect.Y2 - 1
x = this.rect.X2 - len(this.buttonText)
} else {
// default to center
y = this.rect.Y2 - (this.rect.Height() / 2)
x = this.rect.X2 - (this.rect.Width() / 2) - (len(this.buttonText) / 2)
}
if this.selected {
TermboxPrintf(x, y, this.selectedTextColor, this.selectedBgColor, this.buttonText)
termbox.HideCursor()
} else {
TermboxPrintf(x, y, this.defaultTextColor, this.defaultBgColor, this.buttonText)
}
}
// This draws the border lines around the widget
//
// TODO: probably paint BG colors, thought this may need to be
// in tandem with the normal Draw function as well.
func (this *ButtonWidget) drawBorderAndBg() {
var borderColor termbox.Attribute
if this.selected {
borderColor = this.selectedBorderColor | termbox.AttrBold
} else {
borderColor = this.defaultBorderColor
}
var bgColor termbox.Attribute
if this.selected {
bgColor = this.selectedBgColor
} else {
bgColor = this.defaultBgColor
}
// Draw corners
termbox.SetCell(this.rect.X1, this.rect.Y1, 0x250C, borderColor, bgColor)
termbox.SetCell(this.rect.X2, this.rect.Y1, 0x2510, borderColor, bgColor)
termbox.SetCell(this.rect.X1, this.rect.Y2, 0x2514, borderColor, bgColor)
termbox.SetCell(this.rect.X2, this.rect.Y2, 0x2518, borderColor, bgColor)
for i := this.rect.X1 + 1; i < this.rect.X2; i++ {
termbox.SetCell(i, this.rect.Y1, 0x2500, borderColor, bgColor)
termbox.SetCell(i, this.rect.Y2, 0x2500, borderColor, bgColor)
}
for i := this.rect.Y1 + 1; i < this.rect.Y2; i++ {
termbox.SetCell(this.rect.X1, i, 0x2502, borderColor, bgColor)
termbox.SetCell(this.rect.X2, i, 0x2502, borderColor, bgColor)
}
}
// Meant to be called when the terminal dimensions are resized, it calls the callback function and
// refigures out the sizing and positioning of the rectacngle.
func (this *ButtonWidget) CalculateSize() {
rect := CreateRectangle(this.calcFunction())
this.rect = rect
}
// Check if this widget should be flaggable as selected.
func (this *ButtonWidget) IsSelectable() bool {
return this.isSelectable
}
// Check if this widget is flagged as selected. Accessor
// because eventually want to implement logic to test for isSelectable
func (this *ButtonWidget) IsSelected() bool {
return this.selected
}
// Selects this widget - it'd probably make sense
// to return an error if this widget isn't selectable
func (this *ButtonWidget) Select() {
if this.isSelectable {
this.selected = true
}
}
// Unset selection status
func (this *ButtonWidget) Unselect() {
this.selected = false
}
// Take widget level printable-key (rune) handler function
func (this *ButtonWidget) AddCharKeyCallback(char rune, callback EventCallback) {
this.widgetKeyBindings[char] = callback
}
// Take widget level meta-key (termbox.Key) handler function
func (this *ButtonWidget) AddSpecialKeyCallback(key termbox.Key, callback EventCallback) {
this.widgetKeyBindings[key] = callback
}
// If this widget is selected, handle key inputs based on mapped keys
func (this *ButtonWidget) HandleEvents(event interface{}) {
if this.widgetKeyBindings[event] != nil {
this.widgetKeyBindings[event](this, event)
}
}
// Setter for the button's displayed text.
func (this *ButtonWidget) SetText(btnText string) {
//TODO: Maybe do something about too-long text by
// looking at the width of the rect?
this.buttonText = btnText
}
// A "constructor" function to create new widgets.
func CreateButtonWidget(buttonText string, textPos ScreenPosition, defTextCol, selTextCol, defBgCol, selBgCol, defBorCol, selBorCol termbox.Attribute,
calcFunction CalcFunction, selectable, selected bool) *ButtonWidget {
widget := new(ButtonWidget)
widget.buttonText = buttonText
widget.textPosition = textPos
widget.defaultTextColor = defTextCol
widget.selectedTextColor = selTextCol
widget.defaultBgColor = defBgCol
widget.selectedBgColor = selBgCol
widget.defaultBorderColor = defBorCol
widget.selectedBorderColor = selBorCol
widget.calcFunction = calcFunction
widget.selected = selected
widget.isSelectable = selectable
widget.widgetKeyBindings = make(map[interface{}]EventCallback)
return widget
}