Mimgui обновить состояние кнопки

  • Автор темы deleted-user-182194
  • Дата начала
D

deleted-user-182194

Гость
Автор темы
Версия MoonLoader
.027.0-preview
У меня есть функция
Код:
function imgui.ToggleButton(str_id, bool, a_speed)

    local p         = imgui.GetCursorScreenPos()
    local DL        = imgui.GetWindowDrawList()

    local label     = str_id:gsub('##.+', '') or ""
    local h         = imgui.GetTextLineHeightWithSpacing()
    local w         = h * 2
    local r         = h - 6
    local s         = a_speed or 0.2

    local function bringFloatTo(from, to, start_time, duration)
        local timer = os.clock() - start_time
        if timer >= 0.00 and timer <= duration then
            local count = timer / (duration / 100)
            return from + (count * (to - from) / 100), true
        end
        return (timer > duration) and to or from, false
    end
    local function bringVec4To(from, to, start_time, duration)
        local timer = os.clock() - start_time
        if timer >= 0.00 and timer <= duration then
            local count = timer / (duration / 100)
            return imgui.ImVec4(
                from.x + (count * (to.x - from.x) / 100),
                from.y + (count * (to.y - from.y) / 100),
                from.z + (count * (to.z - from.z) / 100),
                from.w + (count * (to.w - from.w) / 100)
            ), true
        end
        return (timer > duration) and to or from, false
    end

    if UI_CUSTOM_TOGGLEBUTTON == nil then UI_CUSTOM_TOGGLEBUTTON = {} end
    if UI_CUSTOM_TOGGLEBUTTON[str_id] == nil then
        UI_CUSTOM_TOGGLEBUTTON[str_id] = {
            start = 0,
            c_from = bool[0] and 0.0 or 1.0,
            c_to = bool[0] and 1.0 or 0.0,
            active = bool[0],
            h_start = 0
        }
    end

    local poolbutton = UI_CUSTOM_TOGGLEBUTTON[str_id]

    imgui.BeginGroup()
        imgui.InvisibleButton(str_id, imgui.ImVec2(w, h))
        imgui.SameLine()
        local pp = imgui.GetCursorPos()
        imgui.SetCursorPos(imgui.ImVec2(pp.x, pp.y + h/2 - imgui.CalcTextSize(label).y/2))
        imgui.Text(label)
    imgui.EndGroup()

    local clicked = imgui.IsItemClicked()
    if imgui.IsItemClicked() then
        bool[0] = not bool[0]
        poolbutton.active = bool[0]
        local timer = os.clock() - poolbutton.start
        if timer >= 0 and timer <= s then
            poolbutton.start = os.clock() - (s - timer)
        else
            poolbutton.start = os.clock()
        end
        poolbutton.c_from = bool[0] and 0.0 or 1.0
        poolbutton.c_to =  bool[0] and 1.0 or 0.0
    end

    local f = bringFloatTo(poolbutton.c_from, poolbutton.c_to, poolbutton.start, s)

    local colorButton = imgui.GetStyle().Colors[imgui.Col.Button]
    local colorButtonActive = imgui.GetStyle().Colors[imgui.Col.ButtonActive]

    local color = bringVec4To(
        poolbutton.active and colorButton or colorButtonActive,
        poolbutton.active and colorButtonActive or colorButton,
        poolbutton.start, s
    )

    DL:AddRect(imgui.ImVec2(p.x, p.y), imgui.ImVec2(p.x + w, p.y + h), imgui.GetColorU32Vec4(color), 3, 15, 1.5)
    DL:AddRectFilled(imgui.ImVec2(p.x + 3 + f * (w - 6 - r), p.y + 3), imgui.ImVec2(p.x + 3 + r + f * (w - 6 - r), p.y + 3 + r), imgui.GetColorU32Vec4(color), 3, 15)

    return clicked
end

Проблема в том, что когда я изменяю переменную bool из другой части кода(не кликом) кнопка не обновляется визуально. Как можно это исправить?
 
Решение
проигрывать анимацию при изменении статуса, а не по нажатию кнопки, примерно так:
Lua:
function(...)
    -- code

    if imgui.IsItemClicked() then
        bool[0] = not bool[0]
        -- остальное перенести
    end

    if poolbutton.active ~= bool[0] then
        -- активировать анимацию
    end

    -- code
end

wojciech?

Известный
Проверенный
404
360
проигрывать анимацию при изменении статуса, а не по нажатию кнопки, примерно так:
Lua:
function(...)
    -- code

    if imgui.IsItemClicked() then
        bool[0] = not bool[0]
        -- остальное перенести
    end

    if poolbutton.active ~= bool[0] then
        -- активировать анимацию
    end

    -- code
end