ScrollableMenuPanel

Package: Libraries.Interface.Controls · Inherits from: Libraries.Interface.Controls.Control, Libraries.Game.Graphics.Drawable, Libraries.Interface.Controls.Icon, Libraries.Interface.Item, Libraries.Interface.Controls.MenuPanelBlueprint, Libraries.Interface.Item2D, Libraries.Language.Object, Libraries.Interface.Events.TextureLoadListener, Libraries.Game.Graphics.TextureRegion, Libraries.Game.Disposable

The ScrollableMenuPanel class is a menu panel that shows only some of its items at a time. It is useful for a menu with a long list of items, which might otherwise run off the edge of the screen. A scroll bar on the right side of the panel shows which part of the list is in view, and the user can drag it, use the mouse wheel, or move through the items with the keyboard to bring other items into view. The easiest way to get one is to call SetScrollable(true) on a PopupMenu.

Example Code

use Libraries.Game.Game
use Libraries.Interface.Controls.PopupMenu
use Libraries.Interface.Controls.MenuItem

class Main is Game
    action Main
        StartGame()
    end

    action CreateGame
        PopupMenu menu
        // Use a ScrollableMenuPanel that shows 5 items at a time.
        menu:SetScrollable(true)
        menu:SetItemsInView(5)

        integer counter = 1
        repeat while counter <= 20
            MenuItem item
            item:SetName("Item " + counter)
            menu:Add(item)
            counter = counter + 1
        end

        menu:OpenAt(100, 400)
    end
end

Actions

DragScrollControlTo(number x, number y)

This action scrolls the panel to follow the mouse while the user drags the scroll bar. It moves the view by how far the mouse has moved up or down since the last drag point, then records the new point. Dragging past the top of the bar shows the start of the list, and dragging past the bottom shows the end. It does nothing unless IsDragging returns true. The panel's mouse behaviors call it, so most users shouldn't need to call it directly.

Parameters

  • x number: The x coordinate of the mouse, in pixels. Only the y coordinate affects the scrolling.
  • y number: The y coordinate of the mouse, in pixels.

Draw(Libraries.Game.Graphics.Painter2D painter)

This action draws the panel. It draws the panel's background, then only the items that are in view, then the scroll bar. Items that are scrolled out of view are skipped. This is called automatically by the Game engine each frame, and most users shouldn't need to call it directly.

Parameters

GetItemsInView()

This action returns how many items the panel shows at once.

Returns

number: How many items are shown at once.

GetScrollIndex()

This action returns the index of the first MenuItem visible in the scroll pane. This can return non-integer values to indicate the first item is partially offscreen -- for example, a returned value of 8.4 would indicate that the first visible item in the scroll pane is the 8th indexed item, and it is 40% of the way off of the top of the pane.

Returns

number: The index of the first item visible in the scroll pane, with a decimal component describing how much of it is off the top of the panel (if any).

IsDragging()

This action returns true if the user is currently dragging the scroll bar with the mouse, or false otherwise.

Returns

boolean: Whether or not the scroll bar is being dragged with the mouse.

IsScrollable()

This action tells whether this panel scrolls through its items. A ScrollableMenuPanel always scrolls, so this always returns true.

Returns

boolean: true, because a ScrollableMenuPanel always scrolls.

LoadGraphics(Libraries.Interface.Layouts.LayoutProperties properties)

This action is used to load the graphical components of the panel, including the views for the scroll bar and the part of it that is dragged, and colors them from the panel's foreground and accent colors. This is handled automatically by the Game engine as needed, and most users shouldn't need to use this action directly.

Parameters

Resize()

This action works out the size and position of the panel and its items. Items outside the current view are hidden and the ones in view are shown, each item is placed from the bottom of the view upward, every item is given the width of the widest one, and the scroll bar is sized to show how much of the list is in view. The panel does this itself instead of using a Layout, so that it only lays out the items in view. It is called automatically as needed, and most users shouldn't need to call it directly.

ScrollToView(Libraries.Interface.Controls.MenuItem item)

This action scrolls the panel just far enough to bring the given item into view. If the item is above the view, it becomes the first item shown. If it is below the view, it becomes the last item shown. If it is already in view, or is not in this panel, nothing changes. A PopupMenu calls this when the selection moves, so the selected item is always visible.

Parameters

Example

use Libraries.Game.Game
use Libraries.Interface.Controls.ScrollableMenuPanel
use Libraries.Interface.Controls.MenuItem

class Main is Game
    action Main
        StartGame()
    end

    action CreateGame
        ScrollableMenuPanel panel
        panel:SetItemsInView(3)

        MenuItem last = undefined
        integer counter = 1
        repeat while counter <= 8
            MenuItem item
            item:SetName("Item " + counter)
            panel:Add(item)
            last = item
            counter = counter + 1
        end

        panel:ScrollToView(last)
    end
end

SetDragging(boolean drag)

This action sets whether the user is dragging the scroll bar with the mouse. The panel's mouse behaviors set it to true when the user presses on the panel and to false when they let go, and DragScrollControlTo only scrolls while it is true.

Parameters

  • drag boolean: true if the scroll bar is being dragged, or false if not.

SetItemsInView(number itemCount)

This action sets how many items the panel shows at once. The panel's height fits this many items, and the rest can be reached by scrolling. If the panel has fewer items than this, it shows all of them and hides its scroll bar. The default is 10.

Parameters

  • itemCount number: How many items to show at once.

Example

use Libraries.Game.Game
use Libraries.Interface.Controls.ScrollableMenuPanel
use Libraries.Interface.Controls.MenuItem

class Main is Game
    action Main
        StartGame()
    end

    action CreateGame
        ScrollableMenuPanel panel
        panel:SetItemsInView(3)

        integer counter = 1
        repeat while counter <= 8
            MenuItem item
            item:SetName("Item " + counter)
            panel:Add(item)
            counter = counter + 1
        end
    end
end

SetScrollClickPoint(number x, number y)

This action records the point where the user pressed the mouse to start dragging the scroll bar. DragScrollControlTo measures each mouse movement from this point.

Parameters

  • x number: The x coordinate of the point, in pixels.
  • y number: The y coordinate of the point, in pixels.

SetScrollIndex(number index)

This action scrolls the panel so the item at the given index is the first one in view, then lays the panel out again. Index 0 is the top of the list. A value with a decimal part, such as 2.5, leaves the first visible item partly scrolled off the top. A value below 0 is treated as 0, and a value too large to fill the panel is pulled back so the last item sits at the bottom.

Parameters

  • index number: The index of the item to show first.

Example

use Libraries.Game.Game
use Libraries.Interface.Controls.ScrollableMenuPanel
use Libraries.Interface.Controls.MenuItem

class Main is Game
    action Main
        StartGame()
    end

    action CreateGame
        ScrollableMenuPanel panel
        panel:SetItemsInView(3)

        integer counter = 1
        repeat while counter <= 8
            MenuItem item
            item:SetName("Item " + counter)
            panel:Add(item)
            counter = counter + 1
        end

        // Show Item 5, Item 6 and Item 7.
        panel:SetScrollIndex(4)
    end
end

Inherited Actions

59 actions from Libraries.Game.Graphics.Drawable
27 actions from Libraries.Game.Graphics.TextureRegion
262 actions from Libraries.Interface.Controls.Control
3 actions from Libraries.Interface.Controls.Icon
17 actions from Libraries.Interface.Controls.MenuPanelBlueprint
60 actions from Libraries.Interface.Item
193 actions from Libraries.Interface.Item2D
3 actions from Libraries.Language.Object