Кто может помочь пофиксить функцию Mimgui

Moorell

Участник
Автор темы
55
12
Версия MoonLoader
.026-beta
lua:
LastActiveTime = {}
LastActive  = {}
function imgui.ToggleButton(str_id, bool)
    local rBool = false
    local p = imgui.GetCursorScreenPos()
    local draw_list = imgui.GetWindowDrawList()
    local height = 20
    local width = height * 1.55
    local radius = height * 0.50

    if imgui.InvisibleButton(str_id, imgui.ImVec2(width, height)) then
        bool[0] = not bool[0]
        rBool = true
        LastActiveTime[tostring(str_id)] = imgui.GetTime()
        LastActive[tostring(str_id)] = true
    end

    local hovered = imgui.IsItemHovered()
    if str_id ~= "" then
        imgui.SameLine()
        imgui.SetCursorPosY(imgui.GetCursorPosY()+3)
        text = str_id:find("##") and str_id:match("(.*)##.*") or str_id
        if text ~= "" and text ~= nil then
            imgui.Text(text)
        end
    end

    local t = bool[0] and 1.0 or 0.0

    if LastActive[tostring(str_id)] then
        local time = imgui.GetTime() - LastActiveTime[tostring(str_id)]
        if time <= 0.13 then
            local t_anim = ImSaturate(time / 0.13)
            t = bool[0] and 1.0 + t_anim or 1.0 - t_anim
        else
            LastActive[tostring(str_id)] = false
        end
    end

    local col_bg = imgui.ColorConvertFloat4ToU32(imgui.ImVec4(100 / 255, 100 / 255, 100 / 255, hovered and 220 or 180 / 255))
  
    draw_list:AddRectFilled(
        imgui.ImVec2(p.x, p.y + (height / 8)),
        imgui.ImVec2(p.x + width, p.y + (height - (height / 8))),
        col_bg, 10
    )
    draw_list:AddRectFilled(
        imgui.ImVec2(p.x + (bool[0] and radius / 2 or 0) + t * (width - radius * 2.0), p.y + (height / 8)),
        imgui.ImVec2(p.x + (bool[0] and 30 or 15), p.y + (height - (height / 8))),
        imgui.ColorConvertFloat4ToU32(imgui.GetStyle().Colors[imgui.Col.Text]),
        6, 1 + (bool[0] and 9 or 4)
    )
  

    return rBool
end
Вообщем пытался сделать такой toggleButton(скрин закреплю) у меня по сути получилось, но переключение не плавное и много гавнокода. Помогите пофиксить пожалуйста)
 

Вложения

  • opera_EF666AlUTw.png
    opera_EF666AlUTw.png
    1.7 KB · Просмотры: 36
Последнее редактирование:

MLycoris

Режим чтения
Проверенный
1,820
1,860
local anim_speed = 0.1 - скорость анимки, чем выше значение, тем медленнее
Lua:
function imgui.ToggleButton(str_id, bool)
    local anim_speed = 0.1
    local function ImSaturate(f) return f < 0.0 and 0.0 or (f > 1.0 and 1.0 or f) end
    if LastActiveTime == nil then LastActiveTime = {} end
    if LastActive == nil then LastActive = {} end
    local rBool = false
    local p = imgui.GetCursorScreenPos()
    local height = 20
    local width = height * 1.55
    local radius = height * 0.50
    if imgui.InvisibleButton(str_id, imgui.ImVec2(width, height)) then
        bool[0] = not bool[0]
        rBool = true
        LastActiveTime[tostring(str_id)] = os.clock()
        LastActive[tostring(str_id)] = true
    end
    local t = bool[0] and 1.0 or 0.0
    if LastActive[str_id] then
        local time = os.clock() - LastActiveTime[tostring(str_id)]
        if time <= anim_speed then
           local t_anim = ImSaturate(time / anim_speed)
           t = bool[0] and t_anim or 1.0 - t_anim
        else
           LastActive[str_id] = false
        end
     end
    local col_bg = imgui.ColorConvertFloat4ToU32(imgui.ImVec4(100 / 255, 100 / 255, 100 / 255, imgui.IsItemHovered() and 220 or 180 / 255))
    imgui.GetWindowDrawList():AddRectFilled(
        imgui.ImVec2(p.x, p.y + (height / 8)),
        imgui.ImVec2(p.x + width, p.y + (height - (height / 8))),
        col_bg, 10
    )
    imgui.GetWindowDrawList():AddRectFilled(
        imgui.ImVec2(p.x + (bool[0] and radius / 2 or 0) + t * (width - radius * 2.0), p.y + (height / 8)),
        imgui.ImVec2(p.x + (bool[0] and 30 or 15), p.y + (height - (height / 8))),
        imgui.ColorConvertFloat4ToU32(imgui.GetStyle().Colors[imgui.Col.Text]),
        6, 1 + (bool[0] and 9 or 4)
    )
    return rBool
end
 
  • Нравится
Реакции: Sadow

Moorell

Участник
Автор темы
55
12
local anim_speed = 0.1 - скорость анимки, чем выше значение, тем медленнее
Lua:
function imgui.ToggleButton(str_id, bool)
    local anim_speed = 0.1
    local function ImSaturate(f) return f < 0.0 and 0.0 or (f > 1.0 and 1.0 or f) end
    if LastActiveTime == nil then LastActiveTime = {} end
    if LastActive == nil then LastActive = {} end
    local rBool = false
    local p = imgui.GetCursorScreenPos()
    local height = 20
    local width = height * 1.55
    local radius = height * 0.50
    if imgui.InvisibleButton(str_id, imgui.ImVec2(width, height)) then
        bool[0] = not bool[0]
        rBool = true
        LastActiveTime[tostring(str_id)] = os.clock()
        LastActive[tostring(str_id)] = true
    end
    local t = bool[0] and 1.0 or 0.0
    if LastActive[str_id] then
        local time = os.clock() - LastActiveTime[tostring(str_id)]
        if time <= anim_speed then
           local t_anim = ImSaturate(time / anim_speed)
           t = bool[0] and t_anim or 1.0 - t_anim
        else
           LastActive[str_id] = false
        end
     end
    local col_bg = imgui.ColorConvertFloat4ToU32(imgui.ImVec4(100 / 255, 100 / 255, 100 / 255, imgui.IsItemHovered() and 220 or 180 / 255))
    imgui.GetWindowDrawList():AddRectFilled(
        imgui.ImVec2(p.x, p.y + (height / 8)),
        imgui.ImVec2(p.x + width, p.y + (height - (height / 8))),
        col_bg, 10
    )
    imgui.GetWindowDrawList():AddRectFilled(
        imgui.ImVec2(p.x + (bool[0] and radius / 2 or 0) + t * (width - radius * 2.0), p.y + (height / 8)),
        imgui.ImVec2(p.x + (bool[0] and 30 or 15), p.y + (height - (height / 8))),
        imgui.ColorConvertFloat4ToU32(imgui.GetStyle().Colors[imgui.Col.Text]),
        6, 1 + (bool[0] and 9 or 4)
    )
    return rBool
end
Мне не скорость анимации надо исправить, а саму анимацию. Когда переключение идет влево все окей, но вправо она дергается.