Сохранение id стиля в ini для Imgui

NotFound

Участник
Автор темы
74
22
Версия MoonLoader
.026-beta
Сделал меню для выбора одной из нескольких тем. Создал ini файл, в котором записываю id выбранной темы.
По команде, открывается меню, с помощью RadioButton выбирается тема и по нажатию на кнопку, выбранную тему надо записать в ini.
Пробовал много разных способов записать, но мунлоадер постоянно ругался по разным причинам. Как это можно реализовать?

Примерно такая выборка из кода по этой ситуации:
require "lib.moonloader"
local keys = require "vkeys"
local imgui = require 'imgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local themes = import "resource/imgui_themes.lua"
local directIni = 'moonloader\\settings.ini'
local mainIni = inicfg.load(nil, directIni)
main_window_state = imgui.ImBool(false)

local checked_radio = imgui.ImInt()

function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(100) end
    sampRegisterChatCommand("themes", cmd_themes)
    imgui.Process = false
    imgui.SwitchContext()
    themes.SwitchColorTheme(mainIni.config.id)
    while true do
        wait(0)
end

function cmd_themes()
    main_window_state.v = not main_window_state.v
    imgui.Process = main_window_state.v

end

function imgui.OnDrawFrame()

    if not main_window_state.v then
        imgui.Process = false
    end

    if main_window_state.v then
        local sw, sh = getScreenResolution()
        imgui.SetNextWindowPos(imgui.ImVec2(sw / 2, sh / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(510, 600), imgui.Cond.FirstUseEver)
        imgui.Begin('Imgui Themes', main_window_state)
        if imgui.Button(u8'Кнопка') then
            themes.SwitchColorTheme(2)
        end
            for id, value in ipairs(themes.colorThemes) do
                if imgui.RadioButton(value, checked_radio, id) then
                    themes.SwitchColorTheme(id)
                end
            end
                        if imgui.Button(u8'Запомнить') then
                  if inicfg.save(NewIni, directIni) then
                    sampAddChatMessage('Изменения сохранены. Перезагрузите скприпт, нажав CTRL + R', -1)
                  end
                end
                        NewIni = {
                            config = {
                                id = --???
                            }
                        }
        imgui.End()
    end
end
 
Решение
Lua:
local imgui = require 'imgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local inicfg = require 'inicfg'
local directIni = 'xz.ini'
ini = inicfg.load({
    settings = {
        theme = 1,
    },
}, directIni)
inicfg.save(ini, directIni)

local themes = import "resource/imgui_themes.lua"
local window_state = imgui.ImBool(false)
local checked_radio = imgui.ImInt(ini.settings.theme)

function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(100) end

    sampRegisterChatCommand("themes", function() window_state.v = not window_state.v end)
    
    imgui.SwitchContext()
    themes.SwitchColorTheme(ini.settings.theme)
    while true...

Nicolas

Активный
114
66
Lua:
local imgui = require 'imgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local inicfg = require 'inicfg'
local directIni = 'xz.ini'
ini = inicfg.load({
    settings = {
        theme = 1,
    },
}, directIni)
inicfg.save(ini, directIni)

local themes = import "resource/imgui_themes.lua"
local window_state = imgui.ImBool(false)
local checked_radio = imgui.ImInt(ini.settings.theme)

function main()
    if not isSampLoaded() or not isSampfuncsLoaded() then return end
    while not isSampAvailable() do wait(100) end

    sampRegisterChatCommand("themes", function() window_state.v = not window_state.v end)
    
    imgui.SwitchContext()
    themes.SwitchColorTheme(ini.settings.theme)
    while true do wait(0)
        imgui.Process = window_state.v
    end
end

function imgui.OnDrawFrame()
    if window_state.v then
        local sw, sh = getScreenResolution()
        imgui.SetNextWindowPos(imgui.ImVec2(sw / 2, sh / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(400, 400), imgui.Cond.FirstUseEver)
        imgui.Begin('Imgui Themes', window_state)

        if imgui.Button(u8'Стандартная тема') then
            themes.SwitchColorTheme(1)
            checked_radio = imgui.ImInt(1)
            ini.settings.theme = 1
            inicfg.save(ini, directIni)
        end

        for id, value in ipairs(themes.colorThemes) do
            if imgui.RadioButton(u8(value), checked_radio, id) then
                themes.SwitchColorTheme(id)
                ini.settings.theme = id
                inicfg.save(ini, directIni)
            end
        end
        imgui.End()
    end
end
 
  • Нравится
Реакции: NotFound