Калькуляция текста. ImGui.

A S K I T

Активный
Автор темы
200
69
Версия MoonLoader
.026-beta
Необходимо подсчитать кол-во символов, отнять их от размера окна и разделить на 2. Как это сделать? Что-то туплю.
Lua:
imgui.SetCursorPos((imgui.GetWindowWidth() - imgui.CalcTextSize(separator('$'..tostring(getPlayerMoney(player)))).x)/2)

-- Запятые в деньгах.
function separator(text)
    if text:find("$") then
        for S in string.gmatch(text, "%d+%$") do
            S = string.sub(S, 0, #S-1)
            local replace = comma_value(S)
            text = string.gsub(text, S, replace)
        end
    end
    return text
end
function comma_value(n)
    local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

Пытался сделать как тут::
imgui.SetCursorPosX((imgui.GetWindowWidth() - imgui.CalcTextSize(u8'Окей').x)/2-20)
 
Решение
У тебя функции comma_value и separator нерабочие были.
Lua:
-- Использование:
-- В OnDrawFrame()
imgui.CenterText(separator('$'..tostring(getPlayerMoney(player))))


--
function imgui.CenterText(text)
    imgui.SetCursorPosX((imgui.GetWindowWidth() - imgui.CalcTextSize(u8(text)).x) / 2)
    imgui.Text(u8(text))
end

function comma_value(n)
    local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

function separator(text)
    if text:find("$") then
        for S in string.gmatch(text, "%$%d+") do
            local replace = comma_value(S)
            text = string.gsub(text, S, replace)
        end
        for S in string.gmatch(text, "%d+%$") do...

Dmitriy Makarov

25.05.2021
Проверенный
2,513
1,140
Тебе обычное центрирование текста нужно, что-ли?
Lua:
function imgui.CenterText(text)
    imgui.SetCursorPosX((imgui.GetWindowWidth() - imgui.CalcTextSize(u8(text)).x) / 2)
    imgui.Text(u8(text))
end


-- Использование:
imgui.CenterText("Текст")

1633899438738.png
 

A S K I T

Активный
Автор темы
200
69
Тебе обычное центрирование текста нужно, что-ли?
Lua:
function imgui.CenterText(text)
    imgui.SetCursorPosX((imgui.GetWindowWidth() - imgui.CalcTextSize(u8(text)).x) / 2)
    imgui.Text(u8(text))
end


-- Использование:
imgui.CenterText("Текст")

Посмотреть вложение 117491
Мне нужно центрировать деньги игрока, разделённые запятыми с помощью функции. Я написал код.
 

Dmitriy Makarov

25.05.2021
Проверенный
2,513
1,140
У тебя функции comma_value и separator нерабочие были.
Lua:
-- Использование:
-- В OnDrawFrame()
imgui.CenterText(separator('$'..tostring(getPlayerMoney(player))))


--
function imgui.CenterText(text)
    imgui.SetCursorPosX((imgui.GetWindowWidth() - imgui.CalcTextSize(u8(text)).x) / 2)
    imgui.Text(u8(text))
end

function comma_value(n)
    local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
    return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

function separator(text)
    if text:find("$") then
        for S in string.gmatch(text, "%$%d+") do
            local replace = comma_value(S)
            text = string.gsub(text, S, replace)
        end
        for S in string.gmatch(text, "%d+%$") do
            S = string.sub(S, 0, #S-1)
            local replace = comma_value(S)
            text = string.gsub(text, S, replace)
        end
    end
    return text
end

1633901032133.png
 
  • Нравится
Реакции: A S K I T