-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_menu.go
692 lines (646 loc) · 30.1 KB
/
input_menu.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
package dosktop
import (
"fmt"
"github.com/supercom32/dosktop/constants"
"github.com/supercom32/dosktop/internal/memory"
"github.com/supercom32/dosktop/internal/stringformat"
)
/*
drawProportionalHorizontalMenu allows you to draw a horizontal menu with
proportional spacing between menu options. In addition, the following
information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The viewport position represents the first menu item that needs to be
drawn. This is useful for menus sizes which are significantly larger than
the visible display area allocated for the menu.
*/
func drawProportionalHorizontalMenu(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, menuWidth int, numberOfItemsOnRow int, viewportPosition int, itemSelected int) {
layerEntry := memory.GetLayer(layerAlias)
menuAttributeEntry := memory.NewAttributeEntry()
menuAttributeEntry.ForegroundColor = styleEntry.MenuForegroundColor
menuAttributeEntry.BackgroundColor = styleEntry.MenuBackgroundColor
highlightAttributeEntry := memory.NewAttributeEntry()
highlightAttributeEntry.ForegroundColor = styleEntry.HighlightForegroundColor
highlightAttributeEntry.BackgroundColor = styleEntry.HighlightBackgroundColor
menuItemWidth := int(float64(menuWidth) / float64(numberOfItemsOnRow))
menuWidthRemainder := menuWidth % menuItemWidth
for currentMenuItemIndex := 0; currentMenuItemIndex < numberOfItemsOnRow; currentMenuItemIndex++ {
if currentMenuItemIndex >= len(selectionEntry.SelectionValue) {
menuItem := stringformat.GetFilledString(menuWidth, " ")
arrayOfRunes := stringformat.GetRunesFromString(menuItem)
printLayer(layerEntry, menuAttributeEntry, xLocation+(currentMenuItemIndex*menuItemWidth), yLocation, arrayOfRunes)
continue
}
attributeEntry := menuAttributeEntry
if currentMenuItemIndex == itemSelected {
attributeEntry = highlightAttributeEntry
}
menuItemName := stringformat.GetFormattedString(" "+selectionEntry.SelectionValue[viewportPosition+currentMenuItemIndex]+" ", menuItemWidth, styleEntry.MenuTextAlignment)
if currentMenuItemIndex == numberOfItemsOnRow-1 {
menuItemName = stringformat.GetFormattedString(" "+selectionEntry.SelectionValue[viewportPosition+currentMenuItemIndex]+" ", menuItemWidth + menuWidthRemainder, styleEntry.MenuTextAlignment)
}
arrayOfRunes := stringformat.GetRunesFromString(menuItemName)
attributeEntry.CellId = currentMenuItemIndex
printLayer(layerEntry, attributeEntry, xLocation+(currentMenuItemIndex*menuItemWidth), yLocation, arrayOfRunes)
}
}
/*
drawHorizontalMenu allows you to draw a horizontal menu with no padded spacing
between menu options. In addition, the following information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The viewport position represents the first menu item that needs to be
drawn. This is useful for menus sizes which are significantly larger than
the visible display area allocated for the menu.
*/
func drawHorizontalMenu(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, itemSelected int) {
layerEntry := memory.GetLayer(layerAlias)
menuAttributeEntry := memory.NewAttributeEntry()
menuAttributeEntry.ForegroundColor = styleEntry.MenuForegroundColor
menuAttributeEntry.BackgroundColor = styleEntry.MenuBackgroundColor
highlightAttributeEntry := memory.NewAttributeEntry()
highlightAttributeEntry.ForegroundColor = styleEntry.HighlightForegroundColor
highlightAttributeEntry.BackgroundColor = styleEntry.HighlightBackgroundColor
currentXLocationOffset := 0
for currentMenuItemIndex := 0; currentMenuItemIndex < len(selectionEntry.SelectionValue); currentMenuItemIndex++ {
attributeEntry := menuAttributeEntry
if currentMenuItemIndex == itemSelected {
attributeEntry = highlightAttributeEntry
}
menuItemName := " " + selectionEntry.SelectionValue[currentMenuItemIndex] + " "
arrayOfRunes := stringformat.GetRunesFromString(menuItemName)
if xLocation+currentXLocationOffset+len(arrayOfRunes) > layerEntry.Width {
return // Stop rendering if the menu item is larger than the layer supports.
//panic(fmt.Sprintf("The menu item '%s' on layer '%s' cannot be drawn since it would be longer than the Width of the layer!", selectionEntry.selectionValue[currentMenuItemIndex], LayerAlias))
}
attributeEntry.CellId = currentMenuItemIndex
printLayer(layerEntry, attributeEntry, xLocation+currentXLocationOffset, yLocation, arrayOfRunes)
// Here we need to check how many wide characters we have printed so we can add double the amount
currentXLocationOffset += len(arrayOfRunes) + stringformat.GetNumberOfWideCharacters(arrayOfRunes)
}
}
/*
clearMouseMenuItemIdentifiers allows you to remove all mouse menu item
identifiers from a given text layer area. Mouse menu item identifiers are
attribute markers that are tagged to text cells which enable the easy
identification of which menu item is currently being selected.
*/
func clearMouseMenuItemIdentifiers(layerAlias string, xLocation int, yLocation int, width int, height int) {
layerEntry := memory.GetLayer(layerAlias)
for currentYLocation := 0; currentYLocation < yLocation+height; currentYLocation++ {
for currentXLocation := 0; currentXLocation < xLocation+width; currentXLocation++ {
if yLocation+currentYLocation < len(layerEntry.CharacterMemory) && xLocation+currentXLocation < len(layerEntry.CharacterMemory[0]) {
characterEntry := &layerEntry.CharacterMemory[yLocation+currentYLocation][xLocation+currentXLocation]
characterEntry.AttributeEntry.CellId = constants.NullCellId
}
}
}
}
/*
GetSelectionFromProportionalHorizontalMenu allows you to obtain a user selection
from a proportional horizontal menu. This differs from a regular menu since all
menu selections are evenly spaced out. If you would like to have menu items
match the size of the selection, use 'GetSelectionFromHorizontalMenu' instead.
In addition, the following information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The returned value is the selection alias for the item that was
chosen.
*/
func GetSelectionFromProportionalHorizontalMenu(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, menuWidth int, numberOfItemsOnRow int, defaultItemSelected int) string {
selectionIndex := GetSelectionFromProportionalHorizontalMenuByIndex(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, menuWidth, numberOfItemsOnRow, defaultItemSelected)
if selectionIndex == constants.NullSelectionIndex {
return ""
}
return selectionEntry.SelectionAlias[selectionIndex]
}
/*
GetSelectionFromProportionalHorizontalMenuByIndex allows you to obtain a user
selection from a proportional horizontal menu. If you would like to have menu
items match the size of the selection, use
'GetSelectionFromHorizontalMenuByIndex' instead. In addition, the following
information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The returned value is the index number of your selection, where 0 is the
first item on your selection list.
*/
func GetSelectionFromProportionalHorizontalMenuByIndex(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, menuWidth int, numberOfItemsOnRow int, defaultItemSelected int) int {
if menuWidth <= 0 {
panic(fmt.Sprintf("The specified menu width of '%d' is invalid!", menuWidth))
}
if numberOfItemsOnRow <= 0 {
panic(fmt.Sprintf("The specified number of menu items on row of '%d' is invalid!", numberOfItemsOnRow))
}
if defaultItemSelected < 0 || defaultItemSelected >= len(selectionEntry.SelectionValue) {
panic(fmt.Sprintf("The specified default item selected of '%d' is invalid for a selection range of 0 to %d!", defaultItemSelected, len(selectionEntry.SelectionValue)))
}
layerEntry := memory.GetLayer(layerAlias)
isItemSelected := false
selectedItem := defaultItemSelected
viewportPosition := 0
// If your default item selected is greater than the number of items allowed on a line, set the viewport
// and default selected item to be your selected item.
if defaultItemSelected + 1 > numberOfItemsOnRow {
viewportPosition = defaultItemSelected
selectedItem = 0
// If your selected item is greater than the last viewport position possible, set your viewport back
// to the last possible position and adjust the selected item to match the default chosen.
if len(selectionEntry.SelectionValue) - viewportPosition < numberOfItemsOnRow {
selectedItem = len(selectionEntry.SelectionValue) - viewportPosition
viewportPosition = len(selectionEntry.SelectionValue) - numberOfItemsOnRow
}
}
returnValue := 0
previouslySelectedItem := selectedItem
drawProportionalHorizontalMenu(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, menuWidth, numberOfItemsOnRow, viewportPosition, selectedItem)
UpdateDisplay()
for isItemSelected == false {
currentKeyPressed := Inkey()
mouseXLocation, mouseYLocation, mouseButtonPressed, _ := memory.MouseMemory.GetMouseStatus()
mouseCellIdentifier := getCellIdByLayerAlias(layerAlias, mouseXLocation, mouseYLocation)
if mouseCellIdentifier != constants.NullCellId {
selectedItem = mouseCellIdentifier
if mouseButtonPressed == 1 {
returnValue = selectedItem
isItemSelected = true
}
}
if currentKeyPressed == "left" {
selectedItem--
if selectedItem < 0 {
selectedItem = 0
viewportPosition--
if viewportPosition < 0 {
viewportPosition = 0
}
}
previouslySelectedItem = -1
memory.MouseMemory.ClearMouseMemory()
}
if currentKeyPressed == "right" {
selectedItem++
if selectedItem >= numberOfItemsOnRow {
selectedItem = numberOfItemsOnRow - 1
viewportPosition++
if viewportPosition + numberOfItemsOnRow > len(selectionEntry.SelectionValue) {
viewportPosition = len(selectionEntry.SelectionValue) - numberOfItemsOnRow
}
}
previouslySelectedItem = -1
memory.MouseMemory.ClearMouseMemory()
}
if currentKeyPressed == "enter" {
returnValue = selectedItem
isItemSelected = true
}
if currentKeyPressed == "esc" {
returnValue = constants.NullSelectionIndex
isItemSelected = true
}
if previouslySelectedItem != selectedItem {
if selectedItem >= len(selectionEntry.SelectionValue) {
selectedItem = len(selectionEntry.SelectionValue) - 1
}
drawProportionalHorizontalMenu(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, menuWidth, numberOfItemsOnRow, viewportPosition, selectedItem)
UpdateDisplay()
previouslySelectedItem = selectedItem
}
}
clearMouseMenuItemIdentifiers(layerAlias, xLocation, yLocation, layerEntry.Width, 1)
memory.MouseMemory.WaitForClickRelease()
return returnValue
}
/*
GetSelectionFromHorizontalMenu allows you to obtain a user selection from a
horizontal menu. This is different from a proportional menu since all menu
items are drawn to the size of the current selection. If you want each
menu item to be of equal width, consider using
'GetSelectionFromProportionalHorizontalMenu' instead. In addition, the
following information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The returned value is the selection alias for the item that was
chosen.
*/
func GetSelectionFromHorizontalMenu(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, defaultItemSelected int) string {
selectionIndex := GetSelectionFromHorizontalMenuByIndex(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, defaultItemSelected)
if selectionIndex == constants.NullSelectionIndex {
return ""
}
return selectionEntry.SelectionAlias[selectionIndex]
}
/*
GetSelectionFromHorizontalMenuByIndex allows you to obtain a user selection
from a horizontal menu. This is different from a proportional menu since all
menu items are drawn to the size of the current selection. If you want each
menu item to be of equal width, consider using
'GetSelectionFromProportionalHorizontalMenuByIndex' instead. In addition, the
following information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The returned value is the index number of your selection, where 0 is the
first item on your selection list.
*/
func GetSelectionFromHorizontalMenuByIndex(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, defaultItemSelected int) int {
if defaultItemSelected < 0 || defaultItemSelected >= len(selectionEntry.SelectionValue) {
panic(fmt.Sprintf("The specified default item selected of '%d' is invalid for a selection range of 0 to %d!", defaultItemSelected, len(selectionEntry.SelectionValue)))
}
layerEntry := memory.GetLayer(layerAlias)
isItemSelected := false
selectedItem := defaultItemSelected
returnValue := 0
previouslySelectedItem := 0
numberOfMenuItems := len(selectionEntry.SelectionValue)
drawHorizontalMenu(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, selectedItem)
UpdateDisplay()
for isItemSelected == false {
currentKeyPressed := Inkey()
mouseXLocation, mouseYLocation, mouseButtonPressed, _ := memory.MouseMemory.GetMouseStatus()
mouseCellIdentifier := getCellIdByLayerAlias(layerAlias, mouseXLocation, mouseYLocation)
if mouseCellIdentifier != constants.NullCellId {
selectedItem = mouseCellIdentifier
if mouseButtonPressed == 1 {
returnValue = selectedItem
isItemSelected = true
}
}
if currentKeyPressed == "left" {
selectedItem--
if selectedItem < 0 {
selectedItem = 0
}
previouslySelectedItem = -1
memory.MouseMemory.ClearMouseMemory()
}
if currentKeyPressed == "right" {
selectedItem++
if selectedItem >= numberOfMenuItems {
selectedItem = numberOfMenuItems - 1
}
previouslySelectedItem = -1
memory.MouseMemory.ClearMouseMemory()
}
if currentKeyPressed == "enter" {
returnValue = selectedItem
isItemSelected = true
}
if currentKeyPressed == "esc" {
returnValue = constants.NullSelectionIndex
isItemSelected = true
}
if previouslySelectedItem != selectedItem {
if selectedItem >= len(selectionEntry.SelectionValue) {
selectedItem = len(selectionEntry.SelectionValue) - 1
}
drawHorizontalMenu(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, selectedItem)
UpdateDisplay()
previouslySelectedItem = selectedItem
}
}
clearMouseMenuItemIdentifiers(layerAlias, xLocation, yLocation, layerEntry.Width, 1)
memory.MouseMemory.WaitForClickRelease()
return returnValue
}
/*
DrawVerticalMenu allows you to obtain a user selection from a horizontal menu.
In addition, the following information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The viewport position represents the first menu item that needs to be
drawn. This is useful for menus sizes which are significantly larger than
the visible display area allocated for the menu.
*/
func DrawVerticalMenu(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, menuWidth int, menuHeight int, viewportPosition int, itemSelected int) {
layerEntry := memory.GetLayer(layerAlias)
menuAttributeEntry := memory.NewAttributeEntry()
menuAttributeEntry.ForegroundColor = styleEntry.MenuForegroundColor
menuAttributeEntry.BackgroundColor = styleEntry.MenuBackgroundColor
highlightAttributeEntry := memory.NewAttributeEntry()
highlightAttributeEntry.ForegroundColor = styleEntry.HighlightForegroundColor
highlightAttributeEntry.BackgroundColor = styleEntry.HighlightBackgroundColor
for currentMenuItemIndex := 0; currentMenuItemIndex < menuHeight; currentMenuItemIndex++ {
if currentMenuItemIndex >= len(selectionEntry.SelectionValue) {
menuItem := stringformat.GetFilledString(menuWidth, " ")
arrayOfRunes := stringformat.GetRunesFromString(menuItem)
printLayer(layerEntry, menuAttributeEntry, xLocation, yLocation+currentMenuItemIndex, arrayOfRunes)
continue
}
attributeEntry := menuAttributeEntry
if currentMenuItemIndex == itemSelected {
attributeEntry = highlightAttributeEntry
}
menuItemName := stringformat.GetFormattedString(selectionEntry.SelectionValue[viewportPosition+currentMenuItemIndex], menuWidth, styleEntry.MenuTextAlignment)
arrayOfRunes := stringformat.GetRunesFromString(menuItemName)
attributeEntry.CellId = currentMenuItemIndex
printLayer(layerEntry, attributeEntry, xLocation, yLocation+currentMenuItemIndex, arrayOfRunes)
}
}
/*
GetSelectionFromVerticalMenu allows you to obtain a user selection
from a vertical menu. In addition, the following information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The returned value is the selection alias for the item that was
chosen.
*/
func GetSelectionFromVerticalMenu (layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, menuWidth int, menuHeight int, defaultItemSelected int) string {
selectionIndex := GetSelectionFromVerticalMenuByIndex(layerAlias, styleEntry , selectionEntry, xLocation, yLocation, menuWidth, menuHeight, defaultItemSelected)
if selectionIndex == constants.NullSelectionIndex {
return ""
}
return selectionEntry.SelectionAlias[selectionIndex]
}
/*
GetSelectionFromVerticalMenuByIndex allows you to obtain a user selection
from a vertical menu. In addition, the following information should be noted:
- If the location to draw a menu item falls outside of the range of the text
layer, then only the visible portion of your menu item will be drawn.
- The returned value is the index number of your selection, where 0 is the
first item on your selection list.
*/
func GetSelectionFromVerticalMenuByIndex(layerAlias string, styleEntry memory.TuiStyleEntryType, selectionEntry memory.SelectionEntryType, xLocation int, yLocation int, menuWidth int, menuHeight int, defaultItemSelected int ) int {
isItemSelected := false
selectedItem := defaultItemSelected
viewportPosition := 0
returnValue := 0
previouslySelectedItem := 0
if defaultItemSelected < 0 || defaultItemSelected >= len(selectionEntry.SelectionValue) {
panic(fmt.Sprintf("The specified default item selected of '%d' is invalid for a selection range of 0 to %d!", defaultItemSelected, len(selectionEntry.SelectionValue)))
}
DrawVerticalMenu(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, menuWidth, menuHeight, viewportPosition, selectedItem)
UpdateDisplay()
for isItemSelected == false {
currentKeyPressed := Inkey()
mouseXLocation, mouseYLocation, mouseButtonPressed, _ := memory.MouseMemory.GetMouseStatus()
mouseCellIdentifier := getCellIdByLayerAlias(layerAlias, mouseXLocation, mouseYLocation)
if mouseCellIdentifier != constants.NullCellId {
selectedItem = mouseCellIdentifier
if mouseButtonPressed == 1 {
returnValue = selectedItem
isItemSelected = true
}
}
if currentKeyPressed == "up" {
selectedItem--
if selectedItem < 0 {
selectedItem = 0
viewportPosition--
if viewportPosition < 0 {
viewportPosition = 0
}
}
previouslySelectedItem = -1
// When the mouse is off screen, the last known location becomes static. This can effect
// Keyboard commands if the mouse's last position appears to be over menu items. So
// We clear the mouse memory here so that it appears to be off screen until it is used again.
memory.MouseMemory.ClearMouseMemory()
}
if currentKeyPressed == "down" {
selectedItem++
if selectedItem >= menuHeight {
selectedItem = menuHeight - 1
viewportPosition++
if viewportPosition+menuHeight > len(selectionEntry.SelectionValue) {
viewportPosition = len(selectionEntry.SelectionValue) - menuHeight
}
}
previouslySelectedItem = -1
memory.MouseMemory.ClearMouseMemory()
}
if currentKeyPressed == "enter" {
returnValue = viewportPosition + selectedItem
isItemSelected = true
}
if currentKeyPressed == "esc" {
returnValue = constants.NullSelectionIndex
isItemSelected = true
}
if previouslySelectedItem != selectedItem {
if selectedItem >= len(selectionEntry.SelectionValue) {
selectedItem = len(selectionEntry.SelectionValue) - 1
}
DrawVerticalMenu(layerAlias, styleEntry, selectionEntry, xLocation, yLocation, menuWidth, menuHeight, viewportPosition, selectedItem)
UpdateDisplay()
previouslySelectedItem = selectedItem
}
}
clearMouseMenuItemIdentifiers(layerAlias, xLocation, yLocation, menuWidth, menuHeight)
memory.MouseMemory.WaitForClickRelease()
return returnValue
}
/*
GetInput allows you to obtain keyboard input from the user. This is useful for
letting applications accept configurations, settings, or other options in
an interactive way at runtime. In addition, the following information
should be noted:
- If the location specified for the input field falls outside of the range
of the text layer, then only the visible portion of your input field will be
drawn.
- If the max length of your input field is less than or equal to 0, a panic
will be generated to fail as fast as possible.
- Password protection will echo back '*' characters to the terminal instead
of the actual characters entered.
- Specifying a default value will simply pre-populate the input field with
the value specified.
- If the cursor position moves outside of the visible display area of the
field, then the entire input field will shift to ensure the cursor is always
visible.
*/
func GetInput(layerAlias string, styleEntry memory.TuiStyleEntryType, xLocation int, yLocation int, width int, maxLengthAllowed int, IsPasswordProtected bool, defaultValue string) string {
layerEntry := memory.GetLayer(layerAlias)
inputString := defaultValue
isScreenUpdateRequired := true
var currentKeyPressed string
var cursorPosition int
var viewportPosition int
if maxLengthAllowed <= 0 {
panic(fmt.Sprintf("The specified maximum input length of '%d' is invalid!", maxLengthAllowed))
}
widthOfViewport := width - 1
if len(inputString) > 0 {
memory.KeyboardMemory.AddKeystrokeToKeyboardBuffer("end")
}
for currentKeyPressed != "enter" {
mouseXLocation, mouseYLocation, mouseButtonPressed, _ := memory.MouseMemory.GetMouseStatus()
mouseCellIdentifier := getCellIdByLayerAlias(layerAlias, mouseXLocation, mouseYLocation)
if mouseCellIdentifier != constants.NullCellId {
if mouseButtonPressed == 1 {
cursorPosition = mouseCellIdentifier
isScreenUpdateRequired = true
}
}
currentKeyPressed = Inkey()
if currentKeyPressed != "" {
// If character is pressed.
if len(currentKeyPressed) == 1 {
if len(inputString) < maxLengthAllowed {
inputString = inputString[:viewportPosition+cursorPosition] + currentKeyPressed + inputString[viewportPosition+cursorPosition:]
if cursorPosition < widthOfViewport {
cursorPosition++
} else {
viewportPosition++
}
isScreenUpdateRequired = true
}
}
if currentKeyPressed == "delete" {
if inputString != "" {
// Protect if nothing else to delete left of string
if viewportPosition+cursorPosition+1 <= len(inputString) {
inputString = inputString[:viewportPosition+cursorPosition] + inputString[viewportPosition+cursorPosition+1:]
isScreenUpdateRequired = true
}
}
}
if currentKeyPressed == "home" {
cursorPosition = 0
viewportPosition = 0
isScreenUpdateRequired = true
}
if currentKeyPressed == "end" {
// If your current viewport shows the end of the input string, just move the cursor to the end of the string.
if viewportPosition > len(inputString)-widthOfViewport {
cursorPosition = len(inputString) - viewportPosition
} else {
// Otherwise advance viewport to end of input string.
viewportPosition = len(inputString) - widthOfViewport
if viewportPosition < 0 {
// If input string is smaller than even one viewport block, just set cursor to end.
viewportPosition = 0
cursorPosition = len(inputString)
} else {
// Otherwise place cursor at end of viewport / string
cursorPosition = widthOfViewport
}
}
isScreenUpdateRequired = true
}
if currentKeyPressed == "backspace" || currentKeyPressed == "backspace2" {
if inputString != "" {
// Protect if nothing else to delete left of string
if viewportPosition+cursorPosition-1 >= 0 {
inputString = inputString[:viewportPosition+cursorPosition-1] + inputString[viewportPosition+cursorPosition:]
cursorPosition--
if cursorPosition < 1 {
if len(inputString) < widthOfViewport {
cursorPosition = viewportPosition + cursorPosition
viewportPosition = 0
} else {
if viewportPosition != 0 { // If your not at the start of the input string
if cursorPosition == 0 {
viewportPosition = viewportPosition - widthOfViewport + 1
} else {
viewportPosition = viewportPosition - widthOfViewport
}
if viewportPosition < 0 {
viewportPosition = 0
}
cursorPosition = widthOfViewport - 1
}
}
}
isScreenUpdateRequired = true
}
}
}
if currentKeyPressed == "left" {
cursorPosition--
if cursorPosition < 0 {
if viewportPosition == 0 {
cursorPosition = 0
} else {
viewportPosition = viewportPosition - widthOfViewport
if viewportPosition < 0 {
viewportPosition = 0
}
cursorPosition = widthOfViewport
}
}
isScreenUpdateRequired = true
}
if currentKeyPressed == "right" {
cursorPosition++
if viewportPosition+cursorPosition >= len(inputString)+1 {
cursorPosition--
} else {
if cursorPosition > widthOfViewport {
viewportPosition += widthOfViewport
cursorPosition = 0
}
}
isScreenUpdateRequired = true
}
}
if isScreenUpdateRequired {
drawInputString(layerEntry, styleEntry, xLocation, yLocation, width, 0, false, stringformat.GetFilledString(width, " "))
if IsPasswordProtected {
passwordProtectedString := stringformat.GetFilledString(len(inputString), "*")
drawInputString(layerEntry, styleEntry, xLocation, yLocation, widthOfViewport, viewportPosition, true, passwordProtectedString)
} else {
drawInputString(layerEntry, styleEntry, xLocation, yLocation, widthOfViewport, viewportPosition, true, inputString)
}
drawCursor(layerEntry, styleEntry, xLocation, yLocation, cursorPosition, false)
UpdateDisplay()
isScreenUpdateRequired = false
}
}
return inputString
}
/*
drawCursor allows you to draw a cursor at the appropriate location for a
text field. In addition, the following information should be noted:
- If the location specified for the cursor range falls outside of the text
layer, then the cursor will only be rendered on the visible portion.
- The cursor position indicates how many spaces to the right of the starting x
and y location your cursor should be drawn at.
- If it is indicated that your cursor is moving backwards, then the space in
which the cursor was previously located will be automatically cleared.
*/
func drawCursor(layerEntry *memory.LayerEntryType, styleEntry memory.TuiStyleEntryType, xLocation int, yLocation int, cursorPosition int, isMovementBackwards bool) {
attributeEntry := memory.NewAttributeEntry()
attributeEntry.ForegroundColor = styleEntry.CursorForegroundColor
attributeEntry.BackgroundColor = styleEntry.CursorBackgroundColor
arrayOfRunes := stringformat.GetRunesFromString(string(styleEntry.CursorCharacter))
printLayer(layerEntry, attributeEntry, xLocation+cursorPosition, yLocation, arrayOfRunes)
if isMovementBackwards {
arrayOfRunes = stringformat.GetRunesFromString(" ")
printLayer(layerEntry, attributeEntry, xLocation+cursorPosition+1, yLocation, arrayOfRunes)
}
}
/*
drawInputString allows you to draw a string for an input field. This is
different than regular printing, since input fields are usually
constrained for space and have the possibility of not being able to
show the entire string. In addition, the following information should be
noted:
- If the location specified for the input string falls outside the range of
the text layer, then only the visible portion will be displayed.
- Width indicates how large the visible area of your input string should be.
- String position indicates the location in your string where printing should
start. If the remainder of your string is too long for the specified width,
then only the visible portion will be displayed.
*/
func drawInputString(layerEntry *memory.LayerEntryType, styleEntry memory.TuiStyleEntryType, xLocation int, yLocation int, width int, stringPosition int, isCellIdsRequired bool, inputString string) {
attributeEntry := memory.NewAttributeEntry()
attributeEntry.ForegroundColor = styleEntry.TextInputForegroundColor
attributeEntry.BackgroundColor = styleEntry.TextInputBackgroundColor
attributeEntry.CellType = constants.CellTypeTextInput
runeSlice := []rune(inputString)
var safeSubstring string
if stringPosition+width <= len(inputString) {
safeSubstring = string(runeSlice[stringPosition : stringPosition+width])
} else {
safeSubstring = string(runeSlice[stringPosition : stringPosition+len(inputString)-stringPosition])
}
arrayOfRunes := stringformat.GetRunesFromString(safeSubstring)
// Here we loop over each character to draw since we need to accommodate for unique
// cell IDs (if required for mouse location detection).
for currentRuneIndex := 0; currentRuneIndex < len(arrayOfRunes); currentRuneIndex ++ {
if isCellIdsRequired {
attributeEntry.CellId = currentRuneIndex
}
printLayer(layerEntry, attributeEntry, xLocation + currentRuneIndex, yLocation, []rune{arrayOfRunes[currentRuneIndex]})
}
}