-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollbar.go
326 lines (301 loc) · 15.7 KB
/
scrollbar.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package consolizer
import (
"github.com/supercom32/consolizer/constants"
"github.com/supercom32/consolizer/internal/memory"
"github.com/supercom32/consolizer/types"
"sort"
)
type ScrollbarInstanceType struct {
layerAlias string
scrollbarAlias string
}
type scrollbarType struct{}
var scrollbar scrollbarType
func (shared *ScrollbarInstanceType) Delete() *ScrollbarInstanceType {
if memory.IsScrollbarExists(shared.layerAlias, shared.scrollbarAlias) {
memory.DeleteScrollbar(shared.layerAlias, shared.scrollbarAlias)
}
return nil
}
/*
setScrollValue allows you set the current scroll value of a scroll bar. In addition, the following
information should be noted:
- The scroll bar handle position will automatically be updated to reflect your new scroll bar value.
- If the scroll bar value specified is out of range, then the closes minimum/maximum value will be selected instead.
- If the scroll bar instance does not exist, then the request is ignored.
*/
func (shared *ScrollbarInstanceType) setScrollValue(value int) {
// TODO: Add scroll value validation.
if memory.IsScrollbarExists(shared.layerAlias, shared.scrollbarAlias) {
scrollbarEntry := memory.GetScrollbar(shared.layerAlias, shared.scrollbarAlias)
scrollbarEntry.ScrollValue = value
scrollbar.computeScrollbarHandlePositionByScrollValue(shared.layerAlias, shared.scrollbarAlias)
}
}
/*
getScrollValue allows you to obtain the scroll bar value for a given scrollbar. If the scroll bar instance
no longer exists, then a result of 0 is always returned.
*/
func (shared *ScrollbarInstanceType) getScrollValue() int {
if memory.IsScrollbarExists(shared.layerAlias, shared.scrollbarAlias) {
scrollbarEntry := memory.GetScrollbar(shared.layerAlias, shared.scrollbarAlias)
return scrollbarEntry.ScrollValue
}
return 0
}
/*
setHandlePosition allows you to specify the location of where the scrollbar handle should be.
The scrollbar value is automatically updated to match the location of the scrollbar handle position.
*/
func (shared *ScrollbarInstanceType) setHandlePosition(positionIndex int) {
if memory.IsScrollbarExists(shared.layerAlias, shared.scrollbarAlias) {
scrollbarEntry := memory.GetScrollbar(shared.layerAlias, shared.scrollbarAlias)
scrollbarEntry.HandlePosition = positionIndex
scrollbar.computeScrollbarValueByHandlePosition(shared.layerAlias, shared.scrollbarAlias)
}
}
/*
Add allows you to add a scrollbar to a given text layer. Once called, an instance of your
control is returned which will allow you to read or manipulate the properties for it. The Style
of the scrollbar will be determined by the style entry passed in. If you wish to remove a scrollbar
from a text layer, simply call 'DeleteScrollbar'. In addition, the following information should be noted:
- Scrollbars are not drawn physically to the text layer provided. Instead,
they are rendered to the terminal at the same time when the text layer is
rendered. This allows you to create scrollbars without actually overwriting
the text layer data under it.
- If the scrollbar to be drawn falls outside the range of the provided layer,
then only the visible portion of the scrollbar will be drawn.
*/
func (shared *scrollbarType) Add(layerAlias string, scrollbarAlias string, styleEntry types.TuiStyleEntryType, xLocation int, yLocation int, length int, maxScrollValue int, scrollValue int, scrollIncrement int, isHorizontal bool) ScrollbarInstanceType {
// TODO: add validation and what happens if failed.
memory.AddScrollbar(layerAlias, scrollbarAlias, styleEntry, xLocation, yLocation, length, maxScrollValue, scrollValue, scrollIncrement, isHorizontal)
var ScrollbarInstance ScrollbarInstanceType
ScrollbarInstance.layerAlias = layerAlias
ScrollbarInstance.scrollbarAlias = scrollbarAlias
return ScrollbarInstance
}
/*
DeleteScrollbar allows you to remove a scrollbar from a text layer. In addition,
the following information should be noted:
- If you attempt to delete a scrollbar which does not exist, then the request
will simply be ignored.
*/
func (shared *scrollbarType) DeleteScrollbar(layerAlias string, scrollbarAlias string) {
memory.DeleteScrollbar(layerAlias, scrollbarAlias)
}
func (shared *scrollbarType) DeleteAllScrollbars(layerAlias string) {
memory.DeleteAllScrollbarsFromLayer(layerAlias)
}
/*
drawScrollbarsOnLayer allows you to draw all scrollbars on a given text layer.
*/
func (shared *scrollbarType) drawScrollbarsOnLayer(layerEntry types.LayerEntryType) {
layerAlias := layerEntry.LayerAlias
keyList := make([]string, 0)
for currentKey := range memory.ScrollBar.Entries[layerAlias] {
keyList = append(keyList, currentKey)
}
sort.Strings(keyList)
for currentKey := range keyList {
scrollbarEntry := memory.GetScrollbar(layerAlias, keyList[currentKey])
if scrollbarEntry.IsVisible {
shared.drawScrollbar(&layerEntry, keyList[currentKey], scrollbarEntry.StyleEntry, scrollbarEntry.XLocation, scrollbarEntry.YLocation, scrollbarEntry.Length, scrollbarEntry.HandlePosition, scrollbarEntry.IsHorizontal)
}
}
}
/*
drawScrollbar allows you to draw A scrollbar on a given text layer. The
Style of the scrollbar will be determined by the style entry passed in. In
addition, the following information should be noted:
- Scrollbars are not drawn physically to the text layer provided. Instead,
they are rendered to the terminal at the same time when the text layer is
rendered. This allows you to create scrollbars without actually overwriting
the text layer data under it.
- If the scrollbar to be drawn falls outside the range of the provided layer,
then only the visible portion of the scrollbar will be drawn.
*/
func (shared *scrollbarType) drawScrollbar(layerEntry *types.LayerEntryType, scrollbarAlias string, styleEntry types.TuiStyleEntryType, xLocation int, yLocation int, length int, handlePosition int, isHorizontal bool) {
attributeEntry := types.NewAttributeEntry()
attributeEntry.CellType = constants.CellTypeScrollbar
attributeEntry.CellControlAlias = scrollbarAlias
attributeEntry.ForegroundColor = styleEntry.ScrollbarForegroundColor
attributeEntry.BackgroundColor = styleEntry.ScrollbarBackgroundColor
scrollbarEntry := memory.GetScrollbar(layerEntry.LayerAlias, scrollbarAlias)
// numberOfScrollSegments := length - 2
// segmentPosition := math.RoundToEven(float64(currentValue) / float64(numberOfTicks) * float64(numberOfScrollSegments))
if isHorizontal {
for currentXLocation := 1; currentXLocation < length-1; currentXLocation++ {
attributeEntry.CellControlId = currentXLocation - 1
printLayer(layerEntry, attributeEntry, xLocation+currentXLocation, yLocation, []rune{styleEntry.ScrollbarTrackPattern})
}
attributeEntry.CellControlId = constants.CellControlIdUpScrollArrow
printLayer(layerEntry, attributeEntry, xLocation, yLocation, []rune{styleEntry.ScrollbarLeftArrow})
attributeEntry.CellControlId = constants.CellControlIdDownScrollArrow
printLayer(layerEntry, attributeEntry, xLocation+length-1, yLocation, []rune{styleEntry.ScrollbarRightArrow})
attributeEntry.ForegroundColor = styleEntry.ScrollbarHandleColor
attributeEntry.CellControlId = constants.CellControlIdScrollbarHandle
// Here we add 1 to the xLocation since handle bars cannot be drawn on scroll arrows.
if scrollbarEntry.IsEnabled {
printLayer(layerEntry, attributeEntry, xLocation+1+handlePosition, yLocation, []rune{styleEntry.ScrollbarHandle})
}
} else {
for currentYLocation := 1; currentYLocation < length-1; currentYLocation++ {
attributeEntry.CellControlId = currentYLocation - 1 // make all Ids 0 based.
printLayer(layerEntry, attributeEntry, xLocation, yLocation+currentYLocation, []rune{styleEntry.ScrollbarTrackPattern})
}
attributeEntry.CellControlId = constants.CellControlIdUpScrollArrow
printLayer(layerEntry, attributeEntry, xLocation, yLocation, []rune{styleEntry.ScrollbarUpArrow})
attributeEntry.CellControlId = constants.CellControlIdDownScrollArrow
printLayer(layerEntry, attributeEntry, xLocation, yLocation+length-1, []rune{styleEntry.ScrollbarDownArrow})
attributeEntry.ForegroundColor = styleEntry.ScrollbarHandleColor
attributeEntry.CellControlId = constants.CellControlIdScrollbarHandle
if scrollbarEntry.IsEnabled {
printLayer(layerEntry, attributeEntry, xLocation, yLocation+1+handlePosition, []rune{styleEntry.ScrollbarHandle})
}
}
}
/*
computeScrollbarValueByHandlePosition allows you to compute the scrollbar value based on the position of the
scrollbar handle.
*/
func (shared *scrollbarType) computeScrollbarValueByHandlePosition(layerAlias string, scrollbarAlias string) {
scrollbarEntry := memory.GetScrollbar(layerAlias, scrollbarAlias)
// If instructed not to draw scroll bars, do not compute values.
if scrollbarEntry.IsEnabled == false {
return
}
// Make sure the handle position is valid first. We minus 3 to the length to account for the two arrows
// and the handle itself.
if scrollbarEntry.HandlePosition >= scrollbarEntry.Length-3 {
scrollbarEntry.HandlePosition = scrollbarEntry.Length - 3
}
if scrollbarEntry.HandlePosition < 0 {
scrollbarEntry.HandlePosition = 0
}
// If you scroll to the last square of a scroll bar, set value to max since that's what a user
// expects to happen.
if scrollbarEntry.HandlePosition == scrollbarEntry.Length-3 {
scrollbarEntry.ScrollValue = scrollbarEntry.MaxScrollValue
return
}
percentScrolled := float64(scrollbarEntry.HandlePosition) / float64(scrollbarEntry.Length)
scrollbarEntry.ScrollValue = int(float64(scrollbarEntry.MaxScrollValue) * percentScrolled)
}
/*
computeScrollbarHandlePositionByScrollValue allows you to calculate the position of the scrollbar handle
based on the current scrollbar value.
*/
func (shared *scrollbarType) computeScrollbarHandlePositionByScrollValue(layerAlias string, scrollbarAlias string) {
scrollbarEntry := memory.GetScrollbar(layerAlias, scrollbarAlias)
// If instructed not to draw scroll bars, do not compute values.
if scrollbarEntry.IsEnabled == false {
return
}
// Make sure the scroll value is valid first.
if scrollbarEntry.ScrollValue >= scrollbarEntry.MaxScrollValue {
scrollbarEntry.ScrollValue = scrollbarEntry.MaxScrollValue
}
if scrollbarEntry.ScrollValue < 0 {
scrollbarEntry.ScrollValue = 0
}
percentScrolled := float64(0)
// Protect against divide by zero cases.
if scrollbarEntry.MaxScrollValue != 0 {
percentScrolled = float64(scrollbarEntry.ScrollValue) / float64(scrollbarEntry.MaxScrollValue)
}
scrollbarEntry.HandlePosition = int(float64(scrollbarEntry.Length-3) * percentScrolled)
// Protect in case drawing over the bar limit.
if scrollbarEntry.HandlePosition >= scrollbarEntry.Length {
scrollbarEntry.HandlePosition = scrollbarEntry.Length - 3
}
}
/*
updateKeyboardEventScrollbar allows you to update the state of all scrollbars according to the current keystroke event.
In the event that a screen update is required this method returns true.
*/
func (shared *scrollbarType) updateKeyboardEventScrollbar(keystroke []rune) bool {
keystrokeAsString := string(keystroke)
isScreenUpdateRequired := false
focusedLayerAlias := eventStateMemory.currentlyFocusedControl.layerAlias
focusedControlAlias := eventStateMemory.currentlyFocusedControl.controlAlias
focusedControlType := eventStateMemory.currentlyFocusedControl.controlType
if focusedControlType != constants.CellTypeScrollbar || !memory.IsScrollbarExists(focusedLayerAlias, focusedControlAlias) {
return isScreenUpdateRequired
}
// Check for scrollbar input only if the scroll bar is not disabled (not null).
scrollbarEntry := memory.GetScrollbar(focusedLayerAlias, focusedControlAlias)
if scrollbarEntry.IsEnabled {
if keystrokeAsString == "up" || keystrokeAsString == "left" {
scrollbarEntry.ScrollValue = scrollbarEntry.ScrollValue - scrollbarEntry.ScrollIncrement
shared.computeScrollbarHandlePositionByScrollValue(focusedLayerAlias, focusedControlAlias)
}
if keystrokeAsString == "down" || keystrokeAsString == "right" {
scrollbarEntry.ScrollValue = scrollbarEntry.ScrollValue + scrollbarEntry.ScrollIncrement
shared.computeScrollbarHandlePositionByScrollValue(focusedLayerAlias, focusedControlAlias)
}
if keystrokeAsString == "pgup" {
scrollbarEntry.ScrollValue = scrollbarEntry.ScrollValue - (scrollbarEntry.ScrollIncrement * 3)
shared.computeScrollbarHandlePositionByScrollValue(focusedLayerAlias, focusedControlAlias)
}
if keystrokeAsString == "pgdn" {
scrollbarEntry.ScrollValue = scrollbarEntry.ScrollValue + (scrollbarEntry.ScrollIncrement * 3)
shared.computeScrollbarHandlePositionByScrollValue(focusedLayerAlias, focusedControlAlias)
}
}
return isScreenUpdateRequired
}
/*
updateMouseEventScrollbar allows you to update the state of all scrollbars according to the current mouse event state.
In the event that a screen update is required this method returns true.
*/
func (shared *scrollbarType) updateMouseEventScrollbar() bool {
isScreenUpdateRequired := false
focusedLayerAlias := eventStateMemory.currentlyFocusedControl.layerAlias
focusedControlAlias := eventStateMemory.currentlyFocusedControl.controlAlias
focusedControlType := eventStateMemory.currentlyFocusedControl.controlType
mouseXLocation, mouseYLocation, buttonPressed, _ := memory.GetMouseStatus()
previousMouseXLocation, previousMouseYLocation, previousButtonPressed, _ := memory.GetPreviousMouseStatus()
if buttonPressed != 0 {
characterEntry := getCellInformationUnderMouseCursor(mouseXLocation, mouseYLocation)
if previousButtonPressed == 0 && characterEntry.AttributeEntry.CellType == constants.CellTypeScrollbar {
scrollbarEntry := memory.GetScrollbar(characterEntry.LayerAlias, characterEntry.AttributeEntry.CellControlAlias)
// Check for scrollbar input only if the scroll bar is not disabled (not null).
if scrollbarEntry.IsEnabled {
if characterEntry.AttributeEntry.CellControlId == constants.CellControlIdScrollbarHandle {
// If you click on a scroll bar handle, start the scrolling event.
eventStateMemory.stateId = constants.EventStateDragAndDropScrollbar
} else if characterEntry.AttributeEntry.CellControlId == constants.CellControlIdUpScrollArrow {
// If you click on the up scroll bar buttonType.
scrollbarEntry.ScrollValue = scrollbarEntry.ScrollValue - scrollbarEntry.ScrollIncrement
shared.computeScrollbarHandlePositionByScrollValue(characterEntry.LayerAlias, characterEntry.AttributeEntry.CellControlAlias)
} else if characterEntry.AttributeEntry.CellControlId == constants.CellControlIdDownScrollArrow {
// If you click on the down scroll bar buttonType.
scrollbarEntry.ScrollValue = scrollbarEntry.ScrollValue + scrollbarEntry.ScrollIncrement
shared.computeScrollbarHandlePositionByScrollValue(characterEntry.LayerAlias, characterEntry.AttributeEntry.CellControlAlias)
} else {
// If you click on the scroll bar area itself, jump the scroll bar to it.
scrollbarEntry.HandlePosition = characterEntry.AttributeEntry.CellControlId
shared.computeScrollbarValueByHandlePosition(characterEntry.LayerAlias, characterEntry.AttributeEntry.CellControlAlias)
}
}
setFocusedControl(characterEntry.LayerAlias, characterEntry.AttributeEntry.CellControlAlias, constants.CellTypeScrollbar)
isScreenUpdateRequired = true
} else if previousButtonPressed != 0 && eventStateMemory.stateId == constants.EventStateDragAndDropScrollbar {
xMove := mouseXLocation - previousMouseXLocation
yMove := mouseYLocation - previousMouseYLocation
if focusedControlType == constants.CellTypeScrollbar {
scrollbarEntry := memory.GetScrollbar(focusedLayerAlias, focusedControlAlias)
if scrollbarEntry.IsHorizontal {
scrollbarEntry.HandlePosition = scrollbarEntry.HandlePosition + xMove
} else {
scrollbarEntry.HandlePosition = scrollbarEntry.HandlePosition + yMove
}
shared.computeScrollbarValueByHandlePosition(focusedLayerAlias, focusedControlAlias)
isScreenUpdateRequired = true
}
}
} else {
eventStateMemory.stateId = constants.EventStateNone
}
return isScreenUpdateRequired
}