dofile("toriui/uielement.lua")
-- Creating a global posShift table - this will store the last scrollbar position between script runs within one game session
POS_SHIFT = POS_SHIFT or { 0 }
-- Creating the list
local list = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" }
-- Spawning the primary parent object
viewElement = UIElement:new({
pos = { 0, 0 },
size = { 400, 300 },
bgColor = { 1, 0.4, 0, 1 }
})
-- Spawning a parent element for any object that needs to be reloaded every frame
local toReload = UIElement:new({
parent = viewElement,
pos = { 0, 0 },
size = { viewElement.size.w, viewElement.size.h }
})
-- Adding toReload children objects which will overlay scrollable list elements on top and bottom.
-- In case any of the scrollable list elements are interactive, these should be interactive aswell to prevent accidental clicking.
-- Bars' height should be at least same as scrollable list's elements' height.
local listElementHeight = 40
local topBar = UIElement:new({
parent = toReload,
pos = { 0, 0 },
size = { viewElement.size.w, listElementHeight },
bgColor = { 0, 0, 0, 1 },
interactive = true
})
local botBar = UIElement:new({
parent = toReload,
pos = { 0, -listElementHeight },
size = { viewElement.size.w, listElementHeight },
bgColor = { 0, 0, 0, 1 },
interactive = true
})
-- Adding quit button
local quitButton = UIElement:new({
parent = topBar,
pos = { -40, 0 },
size = { 40, 40 },
interactive = true,
bgColor = { 1, 1, 1, 0.2 },
hoverColor = { 1, 0.4, 1, 0.2 },
pressedColor = { 1, 0, 0, 1 }
})
quitButton:addMouseHandlers(nil, function()
viewElement:kill()
remove_hooks("uiVisuals")
end)
-- Spawning main view for the scrollable list.
-- Height should be equal to parent element's height minus the sum of bars' height.
local listMainView = UIElement:new({
parent = viewElement,
pos = { 0, topBar.size.h },
size = { viewElement.size.w, viewElement.size.h - topBar.size.h - botBar.size.h }
})
-- Spawning scrollable list holder element.
-- Can have same size as its' parent, in this example width is smaller to leave place for a scrollbar.
local scrollableListHolder = UIElement:new({
parent = listMainView,
pos = { 0, 0 },
size = { listMainView.size.w - 30, listMainView.size.h }
})
-- Calculating scrollbar scale
-- When dealing with dynamically generated tables, you may need to run additional checks.
local scrollScale = (listMainView.size.h) / (#list * listElementHeight)
-- Creating the scroll bar
-- First creating a holder object, then attaching a scrollbar object to it.
local listScrollView = UIElement:new({
parent = listMainView,
pos = { -30, 0 },
size = { 30, listMainView.size.h }
})
local listScrollBar = UIElement:new({
parent = listScrollView,
pos = { 0, 0 },
size = { listScrollView.size.w, listScrollView.size.h * scrollScale },
interactive = true,
bgColor = { 0, 0, 0, 0.3 },
hoverColor = { 0, 0, 0, 0.5 },
pressedColor = { 0, 0, 0, 0.2 },
scrollEnabled = true
})
-- Populating the scrollable list with objects
-- First, creating a table to store all list elements, then spawning them one-by-one and adding to the table
local listElements = {}
for i,v in pairs(list) do
local listElement = UIElement:new({
parent = scrollableListHolder,
pos = { 0, (i - 1) * listElementHeight },
size = { scrollableListHolder.size.w, listElementHeight },
interactive = true,
bgColor = { 0, 0, 0, i % 2 == 0 and 0 or 0.1 },
hoverColor = { 0, 0, 0, 0.3 },
pressedColor = { 0, 0, 0, 0.4 },
})
listElement:addCustomDisplay(false, function()
listElement:uiText(v)
end)
-- Hiding every object, ones that need to be made visible will be activated upon running makeScrollBar(...)
listElement:hide()
table.insert(listElements, listElement)
end
-- Calling makeScrollBar() method to make scrollable list
listScrollBar:makeScrollBar(scrollableListHolder, listElements, toReload, POS_SHIFT, 0.4)
-- Adding standard drawVisuals() function and hooks
local function drawVisuals()
for i, v in pairs(UIElementManager) do
v:updatePos()
end
for i, v in pairs(UIVisualManager) do
v:display()
end
end
add_hook("mouse_button_down", "uiMouseHandler", function(s, x, y) UIElement:handleMouseDn(s, x, y) end)
add_hook("mouse_button_up", "uiMouseHandler", function(s, x, y) UIElement:handleMouseUp(s, x, y) end)
add_hook("mouse_move", "uiMouseHandler", function(x, y) UIElement:handleMouseHover(x, y) end)
add_hook("draw2d", "uiVisuals", drawVisuals)