Listboxes

The listbox item is a type that holds a list of items. The user can scroll through the list and select an item in the list. The following code is from the friendslist menu:

    -- Create friendslist
    friends = UI.Item.new()
    friends:SetRect(-267, -135, 534, 280, 2, 2)     -- the size of the listbox item
    friends:SetTextScale(0.3)
    friends:SetFont(UI.Fonts.BigFont)
    friends:SetType(UI.ItemType.ListBox)            -- tell the lua menu engine that we're dealing with a listbox
    friends:SetBorder(false)
    friends:SetBorderSize(0.5)
    friends:SetBorderColor(1, 1, 1, 0)
    friends:SetItemSize(14)                         -- the size of the listbox entries
    friends:OnClick(
        function (menu, friends)

        end
    )
    friends:OnDoubleClick(
        function (menu, friends)
            Game.JoinFriend(                        -- the action that is executed when pressing an item in the listbox
                friends:GetSelectedItemIndex()      -- the selected index in the listbox item
            )
        end
    )
    friends:GetColumnText(
        function (row, col)
            if col == 0 then                        -- column 0 is used for the friend names
                return Game.GetFriendName(row)
            end
            if col == 1 then                        -- column 1 is used for the friend statuses
                return Game.GetFriendStatus(row)
            end
        end
    )
    friends:GetColumnOffset(
        function (column)
            if column == 0 then
                return 0                            -- column 1 text draws at offset 0 from the left side of the menu
            end
            if column == 1 then
                return 200                          -- column 1 text draws at offset +200 from the left side of the menu
            end
        end
    )
    friends:GetColumnCount(
        function ()
            return 2                                -- the amount of columns we have in the listbox
        end
    )
    friends:GetRowCount(
        function ()
            return Game.GetNumFriends()             -- the amount of rows we have in the listbox
        end
    )
    menu:AddItem(friends)

The type of the item is UI.ItemType.ListBox. This tells the lua menu engine that we're dealing with a list of items. The ListBox can be populated with data using the GetColumnText, GetColumnCount and GetRowCount callbacks. GetRowCount should return the amount of rows that should be displayed, GetColumnCount should return the amount of columns that should be displayed, and finally, GetColumnText should return the text per row/column index. The offset of the column text can be adjusted by using the GetColumnOffset callback.

Listboxes can execute actions by either double or single clicking on an item, by using either the OnDoubleClick or OnClick callback. The second argument in those callbacks link to a handle of the listbox. To obtain the index of the clicked item, use the GetSelectedItemIndex function.

For example:

    listbox:OnDoubleClick(
        function (menu, listbox)
            clickedIndex = listbox:GetSelectedItemIndex()
        end
    )
Last updated on 28th Oct 2017