Files

180 lines
6.8 KiB
Plaintext

Function GetRandomMenuItemId (items() As MenuItem, count As Integer)
numVisibleItems = 0
Dim visibleItems(count) As Integer
For i = 1 To count
If items(i).isVisible and items(i).includeinrandom Then
visibleItems(numVisibleItems) = i
numVisibleItems = numVisibleItems + 1
End If
Next
i = GetRandomInt(0, numVisibleItems - 1)
GetRandomMenuItemId = visibleItems(i)
End Function
Sub BuildMenu (items() As MenuItem, labels() As String, count As Integer)
For i = 1 To count
Dim mi As MenuItem
Call NewMenuItem(mi, labels(i), i)
items(i) = mi
Next
End Sub
Sub BuildMenuWithValues (items() As MenuItem, labels() As String, values() As Integer, count As Integer)
For i = 1 To count
Dim mi As MenuItem
Call NewMenuItemWithValue(mi, labels(i), i, values(i))
items(i) = mi
Next
End Sub
Sub BuildMenuWithColors (items() As MenuItem, labels() As String, colors() As Integer)
' Check array bounds
If LBound(items) <> 1 Or LBound(colors) <> 1 Or UBound(items) <> UBound(colors) Then End
count = UBound(items)
For i = 1 To count
Dim mi As MenuItem
Call NewMenuItemWithColor(mi, labels(i), colors(i), i)
items(i) = mi
Next
End Sub
Sub AdjustMenuStyle (style As MenuStyle, items() As MenuItem, count As Integer, ignoreValue As Integer)
maxIdWidth = 0
maxItemWidth = 0
maxValueWidth = 0
For i = 1 To count
If items(i).isVisible Then
maxIdWidth = MaxI(maxIdWidth, Len(itos$(items(i).id)))
maxItemWidth = MaxI(maxItemWidth, Len(items(i).label + style.labelValueSeparator))
maxValueWidth = MaxI(maxValueWidth, Len(itos$(items(i).value)))
End If
Next
If style.showRandom Then
maxIdWidth = MaxI(maxIdWidth, Len(itos$(style.randomItemId)))
maxItemWidth = MaxI(maxItemWidth, Len(style.randomItemName))
End If
If style.showCancel Then
maxIdWidth = MaxI(maxIdWidth, Len(itos$(style.cancelItemId)))
maxItemWidth = MaxI(maxItemWidth, Len(style.cancelItemName))
End If
style.idWidth = maxIdWidth
style.labelWidth = maxItemWidth
If Not ignoreValue Then style.valueWidth = maxValueWidth Else style.valueWidth = 0
End Sub
Sub PrintMenu (items() As MenuItem, count As Integer, style As MenuStyle)
Dim randomItem As MenuItem
Call NewMenuItemWithColor(randomItem, style.randomItemName, style.randomItemColor, style.randomItemId)
Dim cancelItem As MenuItem
Call NewMenuItemWithColor(cancelItem, style.cancelItemName, style.cancelItemColor, style.cancelItemId)
actualCount = count
If style.showCancel = TRUE Then actualCount = actualCount + 1
If style.showRandom = TRUE Then actualCount = actualCount + 1
If actualCount <= 10 Then
For i = 1 To count
If items(i).isVisible Then
title$ = GetTitle$(items(i), style)
Call PrintWithMaybeColor(title$, items(i).color, style.useColors)
End If
Next
If style.showCancel Then
cancelLabel$ = GetTitleWithoutValue$(cancelItem, style)
Call PrintWithMaybeColor(cancelLabel$, cancelItem.color, style.useColors)
End If
If style.showRandom Then
randomLabel$ = GetTitleWithoutValue$(randomItem, style)
Call PrintWithMaybeColor(randomLabel$, randomItem.color, style.useColors)
End If
Else
Dim emptyItem As MenuItem
Call NewMenuItem(emptyItem, "", 0)
itemWidth = Len(GetTitle$(emptyItem, style))
itemsPerRow = style.screenWidth \ (itemWidth + Len(style.menuItemSpacer))
columnWidth = style.screenWidth \ itemsPerRow
column = 0
For i = 1 To count
If items(i).isVisible Then
itemText$ = GetTitle$(items(i), style)
If column <> (itemsPerRow - 1) Then
If i <> count Or style.showRandom Then
textLength = Len(itemText$)
itemText$ = MakeFitL$(RTrim$(itemText$) + style.menuItemSpacer, textLength + Len(style.menuItemSpacer), " ")
End If
End If
label$ = MakeFitC$(itemText$, columnWidth, " ")
Call PrintWithMaybeColor(label$, items(i).color, style.useColors)
End If
column = (column + 1) Mod itemsPerRow
If column = 0 Then Print ""
Next
If style.showCancel Then
cancelLabel$ = MakeFitC$(GetTitleWithoutValue$(cancelItem, style), columnWidth, " ")
Call PrintWithMaybeColor(cancelLabel$, cancelItem.color, style.useColors)
End If
If style.showRandom Then
randomLabel$ = MakeFitC$(GetTitleWithoutValue$(randomItem, style), columnWidth, " ")
Call PrintWithMaybeColor(randomLabel$, randomItem.color, style.useColors)
End If
End If
End Sub
Function GetTitle$ (mi As MenuItem, ms As MenuStyle)
id$ = itos$(mi.id)
label$ = mi.label
If ms.valueWidth > 0 Then label$ = label$ + ms.labelValueSeparator
value$ = itos$(mi.value)
GetTitle$ = MakeFitR$(id$, ms.idWidth, " ") + ms.idLabelSeparator + MakeFitL$(label$, ms.labelWidth, " ") + MakeFitR$(value$, ms.valueWidth, " ")
End Function
Function GetTitleWithoutValue$ (mi As MenuItem, ms As MenuStyle)
GetTitleWithoutValue$ = MakeFitR$(itos$(mi.id), ms.idWidth, " ") + ms.idLabelSeparator + MakeFitL$(mi.label, ms.labelWidth + ms.valueWidth + Len(ms.labelValueSeparator), " ")
End Function
Sub NewMenuStyle (ms As MenuStyle)
ms.idWidth = 0
ms.labelWidth = 0
ms.valueWidth = 0
ms.screenWidth = 80
ms.randomItemName = "Random"
ms.randomItemId = 0
ms.randomItemColor = COLOR_FOREGROUND_DEFAULT
ms.cancelItemName = "Cancel"
ms.cancelItemId = -1
ms.cancelItemColor = COLOR_FOREGROUND_DEFAULT
ms.idLabelSeparator = " = "
ms.labelValueSeparator = ": "
ms.menuItemSpacer = ", "
ms.showRandom = TRUE
ms.showCancel = false
ms.useColors = FALSE
End Sub
Sub NewMenuItem (mi As MenuItem, label As String, id As Integer)
mi.id = id
mi.label = label
mi.value = 0
mi.color = COLOR_DEFAULT
mi.isVisible = TRUE
mi.includeInRandom = true
End Sub
Sub NewMenuItemWithValue (mi As MenuItem, label As String, id As Integer, value As Integer)
mi.id = id
mi.label = label
mi.value = value
mi.color = COLOR_DEFAULT
mi.isVisible = TRUE
mi.includeInRandom = true
End Sub
Sub NewMenuItemWithColor (mi As MenuItem, label As String, textColor As Integer, id As Integer)
mi.id = id
mi.label = label
mi.value = 0
mi.color = textColor
mi.isVisible = TRUE
End Sub