lua 矩阵算法实现,按钮排列

69 阅读1分钟

记录,以防忘记

function renderDom()
    local list = {
        [1] = {
            type = '1-1',
            mapid = "zyt1-1",
            x = 69,
            y = 71,
            range = 10
        },
        [2] = {
            type = '1-1',
            mapid = "zyt1-2",
            x = 70,
            y = 72,
            range = 10
        },
        [3] = {
            type = '1-1',
            mapid = "zyt3",
            x = 71,
            y = 73,
            range = 10
        },
        [4] = {
            type = '1-1',
            mapid = "zyt4",
            x = 72,
            y = 74,
            range = 10
        },
        [5] = {
            type = '1-1',
            mapid = "zyt5",
            x = 73,
            y = 75,
            range = 10
        },
        [6] = {
            type = '1-1',
            mapid = "zyt6",
            x = 74,
            y = 76,
            range = 10
        }
    }
    
    
    --一行显示几个
    local lineCount = 4;   
    -- 初始化坐标及按钮宽高
    local startX, startY, btnW, btnH = 0, 0, 110, 45;

    for index, value in ipairs(list) do
        -- 获取当前是第几行  
        local currRow = math.ceil(index / lineCount);

        -- 计算当前按钮在行内的索引(从1开始)  
        local colIndex = (index - 1) % lineCount + 1;

        -- 计算按钮的x坐标  
        local x = startX + (colIndex - 1) * btnW;

        -- 计算按钮的y坐标  
        local y = startY + (currRow - 1) * btnH;

        local btnClone = GUI:Clone(_uiDelegate.mapBtn);
        GUI:Button_setTitleText(btnClone, value.mapid);
        GUI:setPosition(btnClone, x, y);
        GUI:addChild(layout, btnClone);
        GUI:setVisible(btnClone, true);

        SL:Print('--------当前是第几行', colIndex, index, currRow, '-------x' .. x .. '----y' .. y)
    end
end

效果图

image.png