Траблы с отображением imgui инпутов

Anti...

Активный
Автор темы
267
31
Версия MoonLoader
.026-beta
Нуждаюсь в помощи, потому что сильно заебался с кодом и решить проблему не могу.
В скрипте можно создавать группы, а в этих группах инпуты(при этом в imgui.Combo выбирая какой именно), каждый со своим буфером. Сделал сохранение в json
1714599474638.png


но после обновления скрипта, инпуты не отображаются, отображается только группа:
1714599525143.png

В json буфера сохраняются:
JSON:
[
    {
        "inputs": [
            {
                "type": "Special",
                "inputs": [
                    {
                        "type": "InputText",
                        "buffer": "TEXT"
                    },
                    {
                        "type": "InputInt",
                        "buffer": 5
                    }
                ]
            }
        ]
    }
]
А если нажать SAVE после обновления, то json сбивается
JSON:
[
    {
        "inputs": [
            {
                "type": "S",
                "inputs": [
                    {
                        "type": "I",
                        "buffer": "T"
                    },
                    {
                        "type": "I",
                        "buffer": 5
                    }
                ]
            }
        ]
    }
]
Чтобы так сильно не ебаться с буферами, сделал 2 функции конвертации с имгуи таблицы в луашную и наоборот(да, есть сниппет от чапо, но он мне что-то не подошёл и решил переделать под себя.)
Помогите пожалуйста решить проблему 🥲

Lua:
local imgui = require 'mimgui'
local ffi = require 'ffi'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local new = imgui.new
local groups = {}
local renderWindow = new.bool(true)
local elementType = new.int()
local item_list = {u8'InputText', u8'InputInt', u8'Special'}
local ImItems = new['const char*'][#item_list](item_list)
local slot_list = {u8'Slot 1', u8'Slot 2', u8'Slot 3', u8'Slot 4', u8'Slot 5', u8'Slot 6', u8'Slot 7', u8'Slot 8', u8'Slot 9', u8'Slot 10', u8'Slot 11', u8'Slot 12', u8'Slot 13', u8'Slot 14', u8'Slot 15'}
local ImSlots = new['const char*'][#slot_list](slot_list)


local fileName = 'INPUT.json'
function json(filePath)
    local filePath = getWorkingDirectory()..'\\config\\'..(filePath:find('(.+).json') and filePath or filePath..'.json')
    local class = {}
    if not doesDirectoryExist(getWorkingDirectory()..'\\config') then
        createDirectory(getWorkingDirectory()..'\\config')
    end
  
    function class:Save(tbl)
        if tbl then
            local F = io.open(filePath, 'w')
            F:write(encodeJson(tbl) or {})
            F:close()
            return true, 'ok'
        end
        return false, 'table = nil'
    end

    function class:Load(defaultTable)
        if not doesFileExist(filePath) then
            class:Save(defaultTable or {})
        end
        local F = io.open(filePath, 'r+')
        local TABLE = decodeJson(F:read() or {})
        F:close()
        for def_k, def_v in next, defaultTable do
            if TABLE[def_k] == nil then
                TABLE[def_k] = def_v
            end
        end
        return TABLE
    end
    return class
end
local cfg = json(fileName):Load({})

imgui.OnInitialize(function()
    imgui.GetIO().IniFilename = nil
end)

local newFrame = imgui.OnFrame(
    function() return renderWindow[0] end,
    function(player)
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 500, 100
        imgui.SetNextWindowPos(imgui.ImVec2(resX, resY), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Main Window', renderWindow)
            if imgui.Button(u8'Create Group') then
                CreateNewGroup()
            end
            if imgui.Button('SAVE') then
                saveConfig()
            end
            imgui.NewLine()
            for i, group in ipairs(groups) do
                imgui.PushIDInt(i)
                imgui.BeginGroup()
                --imgui.Text(u8'Group ' .. i)
              
                for j, input in ipairs(group.inputs) do
                    imgui.PushIDInt(j)
                    imgui.SameLine()
                    imgui.BeginChild(u8'Input ' .. j, imgui.ImVec2(400, 60), true)
                        imgui.PushItemWidth(150)
                        if input.type == 'InputText' then
                            imgui.InputTextWithHint(u8'##input' .. j, u8'Input ' .. j, input.buffer, 256)
                            imgui.SameLine()
                            imgui.Combo(u8'Slot', input.slotType, ImSlots, #slot_list)
                            input.slot = input.slotType[0] + 1
                        elseif input.type == 'InputInt' then
                            imgui.InputInt(u8'##input' .. j, input.buffer)
                            imgui.SameLine()
                            imgui.Combo(u8'Slot', input.slotType, ImSlots, #slot_list)
                            input.slot = input.slotType[0] + 1
                        elseif input.type == 'Special' then
                            --imgui.BeginChild(u8'Special Input ' .. j, imgui.ImVec2(200, 100), true)
                                for k, specialInput in ipairs(input.inputs) do
                                    if specialInput.type == 'InputText' then
                                        imgui.InputTextWithHint(u8'##specialInput' .. k, u8'Special Input ' .. k, specialInput.buffer, 256)
                                    elseif specialInput.type == 'InputInt' then
                                        imgui.InputInt(u8'##specialInput' .. k, specialInput.buffer)
                                    end
                                end
                            --imgui.EndChild()
                        end
                        imgui.PopItemWidth()
                        imgui.SameLine()
                        if j == 1 then
                            if imgui.Button(u8'Remove Group') then
                                table.remove(groups, i)
                            end
                        else
                            if imgui.Button(u8'Remove') then
                                table.remove(group.inputs, j)
                            end
                        end
                    imgui.EndChild()
                    imgui.PopID()
                end
                imgui.SameLine()
                if imgui.Button(u8'Create Element') then
                    imgui.OpenPopup(u8'Create Element Popup')
                end
                if imgui.BeginPopupModal(u8'Create Element Popup') then
                    imgui.Text(u8'Select element type:')
                    imgui.Combo(u8'Element Type', elementType, ImItems, #item_list)
                    --if elementType[0] == 0 then -- InputText
                    --    imgui.Text(u8'Create InputText')
                    --elseif elementType[0] == 1 then -- InputInt
                    --    imgui.Text(u8'Create InputInt')
                    --end
                    --imgui.Separator()
                    if imgui.Button(u8'Create Element', imgui.ImVec2(120, 0)) then
                        if elementType[0] == 0 then -- InputText
                            table.insert(group.inputs, {type = 'InputText', buffer = new.char[256](), slot = 1, slotType = new.int()})
                        elseif elementType[0] == 1 then -- InputInt
                            table.insert(group.inputs, {type = 'InputInt', buffer = new.int(), slot = 1, slotType = new.int()})
                        end
                        imgui.CloseCurrentPopup()
                    end
                    imgui.SameLine()
                    if imgui.Button(u8'Cancel', imgui.ImVec2(120, 0)) then
                        imgui.CloseCurrentPopup()
                    end
                    imgui.EndPopup()
                end
                imgui.EndGroup()
                imgui.PopID()
                imgui.NewLine()
            end
        imgui.End()
    end
)

function CreateNewGroup()
    table.insert(groups, {inputs = {
        {
            type = 'Special',
            inputs = {
                {type = 'InputText', buffer = new.char[256]()},
                {type = 'InputInt', buffer = new.int()}
            }
        }
    }})
end

function saveConfig()
    local jsonConfig = MimguiToTable(groups)
    json(fileName):Save(jsonConfig)
end

function loadConfig()
    local jsonConfig = json(fileName):Load({})
    groups = TableToMimgui(jsonConfig)
end

function MimguiToTable(imguiTable)
    local result = {}
    for field, value in pairs(imguiTable) do
        if type(value) == 'cdata' then
            local cType, cSize = tostring(ffi.typeof(value)):match('ctype<(.+) %[(%d+)%]>')
            if cType == 'float' and (tonumber(cSize) or 1) > 1 then
                local floatAsTable = {}
                for i = 0, cSize - 1 do table.insert(floatAsTable, value[i]) end
                result[field] = floatAsTable
            elseif cType == 'char' then
                result[field] = ffi.string(value)
            elseif cType == 'char' and not cSize then
                result[field] = ffi.string(value)
            else
                result[field] = value[0]
            end
        elseif type(value) == 'table' then
            result[field] = MimguiToTable(value)
        else
            result[field] = value
        end
    end
    return result
end

function TableToMimgui(luaTable)
    local result = {}
    for field, value in pairs(luaTable) do
        if type(value) == 'table' then
            result[field] = TableToMimgui(value)
        elseif type(value) == 'number' then
            result[field] = imgui.new.float(value)
        elseif type(value) == 'string' then
            result[field] = imgui.new.char(value)
        elseif type(value) == 'boolean' then
            result[field] = imgui.new.bool(value)
        else
            result[field] = value
        end
    end
    return result
end


function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('mimgui1', function()
        renderWindow[0] = not renderWindow[0]
    end)
    loadConfig()
    while true do
        wait(0)
    end
end

help

help

help
 
Последнее редактирование: