Помогите написать кнопку для изменения цвета тем скрипта.

Статус
В этой теме нельзя размещать новые ответы.

Larsson

Новичок
Автор темы
8
9
Здравстсвуйте, хотелось бы узнать или попросить помощи в написании скрипта.
Как сделать кнопку в скрипте "Цвета" и при переходе на неё, был бы выбор тем.
Рядом с кнопкой "Добавить бинд" Хотелось бы добавить кнопку "Настройки" или "Цвета", где можно было-бы изменять тему на другую.

XKbcqlb.png



lua:
script_name("Namalsk Binder")
script_author('VALK')
local limgui, imgui         = pcall(require, 'imgui')
local lencoding, encoding   = pcall(require, 'encoding')
assert(limgui, 'not found lib imgui')
assert(lencoding, 'not found lib encoding')
encoding.default = 'CP1251'
local u8 = encoding.UTF8

local vars = {
    menuselect  = 0,
    mainwindow  = imgui.ImBool(false),
    cmdbuf      = imgui.ImBuffer(256),
    cmdparams   = imgui.ImInt(0),
    cmdtext     = imgui.ImBuffer(20480)
}

local commands = {}

function scm(text)
    sampAddChatMessage(("[!] {FFFFFF}%s"):format(text), 0x8BC34A)
end

function apply_custom_style()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4

 style.WindowRounding = 9.0
    style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
    style.ChildWindowRounding = 9.0
    style.FrameRounding = 5.0
    style.ItemSpacing = imgui.ImVec2(5.0, 4.0)
    style.ScrollbarSize = 13.0
    style.ScrollbarRounding = 1
    style.GrabMinSize = 8.0
    style.GrabRounding = 3.0


 colors[clr.Text]                   = ImVec4(0.90, 0.90, 0.90, 1.00)
    colors[clr.TextDisabled]           = ImVec4(0.60, 0.60, 0.60, 1.00)
    colors[clr.WindowBg]               = ImVec4(0.08, 0.08, 0.08, 1.00)
    colors[clr.ChildWindowBg]          = ImVec4(0.10, 0.10, 0.10, 1.00)
    colors[clr.PopupBg]                = ImVec4(0.08, 0.08, 0.08, 1.00)
    colors[clr.Border]                 = ImVec4(0.70, 0.70, 0.70, 0.40)
    colors[clr.BorderShadow]           = ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[clr.FrameBg]                = ImVec4(0.15, 0.15, 0.15, 1.00)
    colors[clr.FrameBgHovered]         = ImVec4(0.19, 0.19, 0.19, 0.71)
    colors[clr.FrameBgActive]          = ImVec4(0.34, 0.34, 0.34, 0.79)
    colors[clr.TitleBg]                = ImVec4(0.00, 0.69, 0.33, 0.80)
    colors[clr.TitleBgActive]          = ImVec4(0.00, 0.74, 0.36, 1.00)
    colors[clr.TitleBgCollapsed]       = ImVec4(0.00, 0.69, 0.33, 0.50)
    colors[clr.MenuBarBg]              = ImVec4(0.00, 0.80, 0.38, 1.00)
    colors[clr.ScrollbarBg]            = ImVec4(0.16, 0.16, 0.16, 1.00)
    colors[clr.ScrollbarGrab]          = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.ScrollbarGrabHovered]   = ImVec4(0.00, 0.82, 0.39, 1.00)
    colors[clr.ScrollbarGrabActive]    = ImVec4(0.00, 1.00, 0.48, 1.00)
    colors[clr.ComboBg]                = ImVec4(0.20, 0.20, 0.20, 0.99)
    colors[clr.CheckMark]              = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.SliderGrab]             = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.SliderGrabActive]       = ImVec4(0.00, 0.77, 0.37, 1.00)
    colors[clr.Button]                 = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.ButtonHovered]          = ImVec4(0.00, 0.82, 0.39, 1.00)
    colors[clr.ButtonActive]           = ImVec4(0.00, 0.87, 0.42, 1.00)
    colors[clr.Header]                 = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.HeaderHovered]          = ImVec4(0.00, 0.76, 0.37, 0.57)
    colors[clr.HeaderActive]           = ImVec4(0.00, 0.88, 0.42, 0.89)
    colors[clr.Separator]              = ImVec4(1.00, 1.00, 1.00, 0.40)
    colors[clr.SeparatorHovered]       = ImVec4(1.00, 1.00, 1.00, 0.60)
    colors[clr.SeparatorActive]        = ImVec4(1.00, 1.00, 1.00, 0.80)
    colors[clr.ResizeGrip]             = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.ResizeGripHovered]      = ImVec4(0.00, 0.76, 0.37, 1.00)
    colors[clr.ResizeGripActive]       = ImVec4(0.00, 0.86, 0.41, 1.00)
    colors[clr.CloseButton]            = ImVec4(0.00, 0.82, 0.39, 1.00)
    colors[clr.CloseButtonHovered]     = ImVec4(0.00, 0.88, 0.42, 1.00)
    colors[clr.CloseButtonActive]      = ImVec4(0.00, 1.00, 0.48, 1.00)
    colors[clr.PlotLines]              = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.PlotLinesHovered]       = ImVec4(0.00, 0.74, 0.36, 1.00)
    colors[clr.PlotHistogram]          = ImVec4(0.00, 0.69, 0.33, 1.00)
    colors[clr.PlotHistogramHovered]   = ImVec4(0.00, 0.80, 0.38, 1.00)
    colors[clr.TextSelectedBg]         = ImVec4(0.00, 0.69, 0.33, 0.72)
    colors[clr.ModalWindowDarkening]   = ImVec4(0.17, 0.17, 0.17, 0.48)

end
apply_custom_style()

function main()
    if not doesDirectoryExist("moonloader/config") then createDirectory("moonloader/config") end
    repeat wait(0) until isSampAvailable()
    sampRegisterChatCommand("cbind", cbind)
    if doesFileExist("moonloader/config/cmdbinder.json") then
        local file = io.open('moonloader/config/cmdbinder.json', 'r')
        if file then
            commands = decodeJson(file:read('*a'))
        end
    end
    saveData(commands, "moonloader/config/cmdbinder.json")
    registerCommands()
    while true do wait(0)
        imgui.Process = vars.mainwindow.v
    end
end

function registerCommands()
    for k, v in pairs(commands) do
        if sampIsChatCommandDefined(v.cmd) then sampUnregisterChatCommand(v.cmd) end
        sampRegisterChatCommand(v.cmd, function(pam)
            lua_thread.create(function()
                local params = string.split(pam, " ", v.params)
                local cmdtext = v.text
                if #params < v.params then
                    local paramtext = ""
                    for i = 1, v.params do
                        paramtext = paramtext .. "[параметр"..i.."] "
                    end
                    scm("Введите: /"..v.cmd.." "..paramtext, -1)
                else
                    local keys = {}
                    for i = 1, v.params do
                        keys["{param:"..i.."}"] = params[i]
                    end
                    for k1, v1 in pairs(keys) do
                        cmdtext = cmdtext:gsub(k1, v1)
                    end
                    for line in cmdtext:gmatch('[^\r\n]+') do
                        if line:match("{wait:%d+}") then
                            wait(line:match("{wait:(%d+)}"))
                        else
                            sampSendChat(line)
                        end
                    end
                end
            end)
        end)
    end
end

function cbind()
    vars.mainwindow.v = not vars.mainwindow.v
end

function imgui.OnDrawFrame()
    if vars.mainwindow.v then
        local sX, sY = getScreenResolution()
        imgui.SetNextWindowPos(imgui.ImVec2(sX/2, sY/2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(891, 380), imgui.Cond.FirstUseEver)
        imgui.Begin(u8 "Список биндов", vars.mainwindow, imgui.WindowFlags.NoResize)
        imgui.BeginChild("##commandlist", imgui.ImVec2(170 ,320), true)
        for k, v in pairs(commands) do
            if imgui.Selectable(u8(("%s. /%s##%s"):format(k, v.cmd, k)), vars.menuselect == k) then
                vars.menuselect     = k
                vars.cmdbuf.v       = u8(v.cmd)
                vars.cmdparams.v    = v.params
                vars.cmdtext.v      = u8(v.text)
            end
        end
        imgui.EndChild()
        imgui.SameLine()
        imgui.BeginChild("##commandsetting", imgui.ImVec2(700, 320), true)
        for k, v in pairs(commands) do
            if vars.menuselect == k then
                imgui.InputText(u8 "Введите сам бинд", vars.cmdbuf)
                imgui.InputInt(u8 "Введите кол-во параметров бинда", vars.cmdparams, 0)
                imgui.InputTextMultiline(u8 "##cmdtext", vars.cmdtext, imgui.ImVec2(678, 200))
                imgui.TextWrapped(u8 "Ключи параметров: {param:1}, {param:2} и т.д (Использовать в тексте на месте параметра)\nКлюч задержки: {wait:кол-во миллисекунд} (Использовать на новой строке)")
                if imgui.Button(u8 "Сохранить бинд") then
                    sampUnregisterChatCommand(v.cmd)
                    v.cmd = u8:decode(vars.cmdbuf.v)
                    v.params = vars.cmdparams.v
                    v.text = u8:decode(vars.cmdtext.v)
                    saveData(commands, "moonloader/config/cmdbinder.json")
                    registerCommands()
                    scm("Бинд сохранен")
                end
                imgui.SameLine()
                if imgui.Button(u8 "Удалить бинд") then
                    imgui.OpenPopup(u8 "Удаление бинда##"..k)
                end
                if imgui.BeginPopupModal(u8 "Удаление бинда##"..k, _, imgui.WindowFlags.NoResize) then
                    imgui.SetCursorPosX(imgui.GetWindowWidth()/2 - imgui.CalcTextSize(u8 "Вы действительно хотите удалить бинд?").x / 2)
                    imgui.Text(u8 "Вы действительно хотите удалить бинд?")
                    if imgui.Button(u8 "Удалить##"..k, imgui.ImVec2(150, 20)) then
                        sampUnregisterChatCommand(v.cmd)
                        vars.menuselect     = 0
                        vars.cmdbuf.v       = ""
                        vars.cmdparams.v    = 0
                        vars.cmdtext.v      = ""
                        table.remove(commands, k)
                        saveData(commands, "moonloader/config/cmdbinder.json")
                        registerCommands()
                        scm("Бинд удалена")
                        imgui.CloseCurrentPopup()

                    end
                    imgui.SameLine()
                    if imgui.Button(u8 "Отмена##"..k, imgui.ImVec2(170, 20)) then
                        imgui.CloseCurrentPopup()
                    end
                    imgui.EndPopup()
                end
            end
        end
        imgui.EndChild()
        if imgui.Button(u8 "Добавить бинд", imgui.ImVec2(170, 20)) then
            table.insert(commands, {cmd = "", params = 0, text = ""})
            saveData(commands, "moonloader/config/cmdbinder.json")
        end
        imgui.End()
    end
end

function string.split(inputstr, sep, limit)
    if limit == nil then limit = 0 end
    if sep == nil then sep = "%s" end
    local t={} ; i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        if i >= limit and limit > 0 then
            if t[i] == nil then
                t[i] = ""..str
            else
                t[i] = t[i]..sep..str
            end
        else
            t[i] = str
            i = i + 1
        end
    end
    return t
end

function saveData(table, path)
    if doesFileExist(path) then os.remove(path) end
    local sfa = io.open(path, "w")
    if sfa then
        sfa:write(encodeJson(table))
        sfa:close()
    end
end
 
  • Нравится
Реакции: kolimans

cort

Активный
275
79
Lua:
local menu = 1
Это вверх, перед меин, но после имгуи.
Lua:
if imgui.Button('theme') then
menu = 1
end
А потом в какой-то чилд, или кула сам всунешь делай так;
Код:
if menu == 1 then

-- и тут твой код для тем, кнопки всякие
end


Посмотри это, поможет
 
Статус
В этой теме нельзя размещать новые ответы.