• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

OTClient How to reorder TextList widgets?

kuhi

Premium User
Premium User
Joined
Aug 26, 2012
Messages
200
Solutions
1
Reaction score
81
Location
x64dbg
GitHub
Kuhicop
Hi :)

I'm trying to create the classic "Up" & "Down" buttons to reorganize this TextList widget:
1709362329151.png

Once clicking the "Up" button, the selected/focused list item, if it's not already the first one, it should raise 1 position in the list

I've tryed with this functions but none worked:
C++:
void raiseChild(UIWidgetPtr child);
void moveChildToIndex(const UIWidgetPtr& child, int index);
void reorderChildren(const std::vector<UIWidgetPtr>& childrens);

How I can achieve this one? :) Do I really need to destroy and create the widget manually? :( and how to create it on X position/index of the list?

PS: I'm doing this shit right now which is working, but there must be a better way to do it:
Lua:
-- this table will hold a copy of the widgets
-- we will use this table as a reference and replicate it's data to the UI
waypointWidgetList = {}

-- finds a widget by it's "custom index"
-- required to assign index manually to the children object
-- example: mychildren.index = 1
function findChildByIndex(control, index)
    for i, v in pairs(control:getChildren()) do
        if v.index == index then
            return v
        end
    end

    return nil
end

-- escalate 1 position up a waypoint in the TextList
function moveWaypointUp()
    local focusedChild = waypointList:getFocusedChild()
    if focusedChild then
        local index = focusedChild.index
        print("index: " .. index)
        if index > 1 then                      
            local newpos = index - 1
            local aux = waypointWidgetList[newpos]
            waypointWidgetList[newpos] = waypointWidgetList[index]
            waypointWidgetList[index] = aux
            redrawWaypointList()      
            childToFocus = findChildByIndex(waypointList, newpos)
            if childToFocus then
                waypointList:focusChild(childToFocus)
            end
        end
    end
end

-- redraw the entire TextList from memory
function redrawWaypointList()  
    waypointList:destroyChildren()
    for i, waypoint in ipairs(waypointWidgetList) do
        local waypointLabel = g_ui.createWidget('WaypointLabel', waypointList)
        waypointLabel:setId(i)
        waypointLabel.index = i
        waypointLabel.description = waypoint.description
        waypointLabel:setText(waypoint.description)
        waypointLabel:setVisible(true)
    end  
end

Thanks <3
 
Last edited:
Adding some up and down with module manager using getFocusedChild, getChildIndex and moveChildToIndex
Lua:
function move(offset) -- down is positive
    local focusedChild = moduleList:getFocusedChild()
    if focusedChild then
        local index=moduleList:getChildIndex(focusedChild)
        local newIndex=index+offset
        if newIndex < 0 then return end -- trying to move up when we are at the top
        moduleList:moveChildToIndex(focusedChild,newIndex)
    end
end

modulemanager.otui
Code:
  Button
    id: moduleMoveDownButton
    anchors.top: moduleReloadButton.bottom
    anchors.right: moduleReloadButton.right
    margin-top: 8
    !text: tr('Move down')
    enabled: true
    width: 90
    @onClick: modules.client_modulemanager.move(1)

  Button
    id: moduleMoveUpButton
    anchors.top: moduleReloadButton.bottom
    anchors.right: moduleUnloadButton.right
    margin-left: 10
    margin-top: 8
    !text: tr('Move Up')
    enabled: true
    width: 90
    @onClick: modules.client_modulemanager.move(-1)
Untitled.png
 
Back
Top