Как вставить заготовленный текст в input поле?

HavenS

Участник
Автор темы
76
19
Появился такой вопрос, как вставить заготовленный текст, который например хранится в переменной text, в поле input imgui, с которым можно будет взаимодействовать, стирать, добавлять к нему.
 
Последнее редактирование:
Решение
Код:
--local font_flag = require('moonloader').font_flag
--local font = renderCreateFont('Verdana', 12, font_flag.BOLD + font_flag.SHADOW)
--local ffi = require "ffi"
require "lib.moonloader"
--local memory = require 'memory'
local imgui = require "imgui"
--local notify = import "imgui_notf.lua"
local encoding = require "encoding"
local main_window_state = imgui.ImBool(false)
--local main_window_state1 = imgui.ImBool(false)
local sw, sh = getScreenResolution()
--local themes = import "resource/imgui_themes.lua"
--local fa = require 'faIcons'
--local fa_glyph_ranges = imgui.ImGlyphRanges({ fa.min_range, fa.max_range })
encoding.default = "CP1251"
u8 = encoding.UTF8

--[[local rkeys = require 'rkeys'
imgui.ToggleButton =...

Sidney31

Известный
1,132
385
mimgui:
Lua:
--вверху скрипт
local new, str, sizeof = imgui.new, ffi.string, ffi.sizeof
local text = new.char[128]()

--в теле имгуи
imgui.InputText("text", text, sizeof(text))
imgui.Text(text)


imgui:
Lua:
--вверху
local text = imgui.ImBuffer(128)

--в теле имгуи
imgui.InputText("text", text)
imgui.Text(text)
 

HavenS

Участник
Автор темы
76
19
Код:
local font_flag = require('moonloader').font_flag
local font = renderCreateFont('Verdana', 12, font_flag.BOLD + font_flag.SHADOW)
local ffi = require "ffi"
require "lib.moonloader"
local memory = require 'memory'
local imgui = require "imgui"
local notify = import "imgui_notf.lua"
local encoding = require "encoding"
local main_window_state = imgui.ImBool(false)
local main_window_state1 = imgui.ImBool(false)
local sw, sh = getScreenResolution()
local themes = import "resource/imgui_themes.lua"
local fa = require 'faIcons'
local fa_glyph_ranges = imgui.ImGlyphRanges({ fa.min_range, fa.max_range })
encoding.default = "CP1251"
u8 = encoding.UTF8

local rkeys = require 'rkeys'
imgui.ToggleButton = require('imgui_addons').ToggleButton
imgui.HotKey = require('imgui_addons').HotKey
imgui.Spinner = require('imgui_addons').Spinner
imgui.BufferingBar = require('imgui_addons').BufferingBar

main_window_state = imgui.ImBool(false)

function imgui.BeforeDrawFrame()
    if fa_font == nil then
        local font_config = imgui.ImFontConfig()
        font_config.MergeMode = true
        fa_font = imgui.GetIO().Fonts:AddFontFromFileTTF('moonloader/resource/fonts/fontawesome-webfont.ttf', 14.0, font_config, fa_glyph_ranges)
    end
end

function imgui.TextColoredRGB(text, render_text)
    local max_float = imgui.GetWindowWidth()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local ImVec4 = imgui.ImVec4

    local explode_argb = function(argb)
        local a = bit.band(bit.rshift(argb, 24), 0xFF)
        local r = bit.band(bit.rshift(argb, 16), 0xFF)
        local g = bit.band(bit.rshift(argb, 8), 0xFF)
        local b = bit.band(argb, 0xFF)
        return a, r, g, b
    end

    local getcolor = function(color)
        if color:sub(1, 6):upper() == 'SSSSSS' then
            local r, g, b = colors[1].x, colors[1].y, colors[1].z
            local a = tonumber(color:sub(7, 8), 16) or colors[1].w * 255
            return ImVec4(r, g, b, a / 255)
        end
        local color = type(color) == 'string' and tonumber(color, 16) or color
        if type(color) ~= 'number' then return end
        local r, g, b, a = explode_argb(color)
        return imgui.ImColor(r, g, b, a):GetVec4()
    end

    local render_text = function(text_)
        for w in text_:gmatch('[^\r\n]+') do
            local text, colors_, m = {}, {}, 1
            w = w:gsub('{(......)}', '{%1FF}')
            while w:find('{........}') do
                local n, k = w:find('{........}')
                local color = getcolor(w:sub(n + 1, k - 1))
                if color then
                    text[#text], text[#text + 1] = w:sub(m, n - 1), w:sub(k + 1, #w)
                    colors_[#colors_ + 1] = color
                    m = n
                end
                w = w:sub(1, n - 1) .. w:sub(k + 1, #w)
            end

            local length = imgui.CalcTextSize(w)
            if render_text == 2 then
                imgui.NewLine()
                imgui.SameLine(max_float / 2 - ( length.x / 2 ))
            elseif render_text == 3 then
                imgui.NewLine()
                imgui.SameLine(max_float - length.x - 5 )
            end
            if text[0] then
                for i = 0, #text do
                    imgui.TextColored(colors_[i] or colors[1], text[i])
                    imgui.SameLine(nil, 0)
                end
                imgui.NewLine()
            else imgui.Text(w) end


        end
    end

    render_text(text)
end

local text = imgui.ImBuffer(256)

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

    sampRegisterChatCommand("1", cmd_script)

    imgui.Process = false

    imgui.SwitchContext()
    themes.SwitchColorTheme(9)

    while true do
        wait(0)
        if main_window_state.v == false then
            imgui.Process = false
        end
    end
end

function cmd_script()
    main_window_state.v = not main_window_state.v
    imgui.Process = main_window_state.v
end

function imgui.OnDrawFrame()
    imgui.SetNextWindowSize(imgui.ImVec2(800, 700), imgui.Cond.FirstUseEver)
    imgui.SetNextWindowPos(imgui.ImVec2((sw / 2), sh / 1.8), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))

    imgui.Begin("Script", main_window_state, imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoResize + imgui.WindowFlags.NoMove)

    imgui.BeginChild("ChildWindow1", imgui.ImVec2(792, 567), true)
        imgui.TextColoredRGB("Text:", 2)
        imgui.InputTextMultiline("", text, imgui.ImVec2(800, 700)) -- вот здесь нужно чтобы был текст который я мог бы стерать или добавлять там что-то.
    imgui.SameLine()
    imgui.EndChild()

    function imgui.TextQuestion(label, description)
        imgui.TextDisabled(label)   
        if imgui.IsItemHovered() then
            imgui.BeginTooltip()
            imgui.PushTextWrapPos(600)
            imgui.TextUnformatted(description)
            imgui.PopTextWrapPos()
            imgui.EndTooltip()
        end
    end

    imgui.SameLine()
    imgui.End()
end
 

Sidney31

Известный
1,132
385
Код:
--local font_flag = require('moonloader').font_flag
--local font = renderCreateFont('Verdana', 12, font_flag.BOLD + font_flag.SHADOW)
--local ffi = require "ffi"
require "lib.moonloader"
--local memory = require 'memory'
local imgui = require "imgui"
--local notify = import "imgui_notf.lua"
local encoding = require "encoding"
local main_window_state = imgui.ImBool(false)
--local main_window_state1 = imgui.ImBool(false)
local sw, sh = getScreenResolution()
--local themes = import "resource/imgui_themes.lua"
--local fa = require 'faIcons'
--local fa_glyph_ranges = imgui.ImGlyphRanges({ fa.min_range, fa.max_range })
encoding.default = "CP1251"
u8 = encoding.UTF8

--[[local rkeys = require 'rkeys'
imgui.ToggleButton = require('imgui_addons').ToggleButton
imgui.HotKey = require('imgui_addons').HotKey
imgui.Spinner = require('imgui_addons').Spinner
imgui.BufferingBar = require('imgui_addons').BufferingBar]]



local buf_text = imgui.ImBuffer(256)
main_window_state = imgui.ImBool(false)

--[[function imgui.BeforeDrawFrame()
    if fa_font == nil then
        local font_config = imgui.ImFontConfig()
        font_config.MergeMode = true
        fa_font = imgui.GetIO().Fonts:AddFontFromFileTTF('moonloader/resource/fonts/fontawesome-webfont.ttf', 14.0, font_config, fa_glyph_ranges)
    end
end]]

function imgui.TextColoredRGB(text, render_text)
    local max_float = imgui.GetWindowWidth()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local ImVec4 = imgui.ImVec4

    local explode_argb = function(argb)
        local a = bit.band(bit.rshift(argb, 24), 0xFF)
        local r = bit.band(bit.rshift(argb, 16), 0xFF)
        local g = bit.band(bit.rshift(argb, 8), 0xFF)
        local b = bit.band(argb, 0xFF)
        return a, r, g, b
    end

    local getcolor = function(color)
        if color:sub(1, 6):upper() == 'SSSSSS' then
            local r, g, b = colors[1].x, colors[1].y, colors[1].z
            local a = tonumber(color:sub(7, 8), 16) or colors[1].w * 255
            return ImVec4(r, g, b, a / 255)
        end
        local color = type(color) == 'string' and tonumber(color, 16) or color
        if type(color) ~= 'number' then return end
        local r, g, b, a = explode_argb(color)
        return imgui.ImColor(r, g, b, a):GetVec4()
    end

    local render_text = function(text_)
        for w in text_:gmatch('[^\r\n]+') do
            local text, colors_, m = {}, {}, 1
            w = w:gsub('{(......)}', '{%1FF}')
            while w:find('{........}') do
                local n, k = w:find('{........}')
                local color = getcolor(w:sub(n + 1, k - 1))
                if color then
                    text[#text], text[#text + 1] = w:sub(m, n - 1), w:sub(k + 1, #w)
                    colors_[#colors_ + 1] = color
                    m = n
                end
                w = w:sub(1, n - 1) .. w:sub(k + 1, #w)
            end

            local length = imgui.CalcTextSize(w)
            if render_text == 2 then
                imgui.NewLine()
                imgui.SameLine(max_float / 2 - ( length.x / 2 ))
            elseif render_text == 3 then
                imgui.NewLine()
                imgui.SameLine(max_float - length.x - 5 )
            end
            if text[0] then
                for i = 0, #text do
                    imgui.TextColored(colors_[i] or colors[1], text[i])
                    imgui.SameLine(nil, 0)
                end
                imgui.NewLine()
            else imgui.Text(w) end


        end
    end

    render_text(text)
end

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

    sampRegisterChatCommand("test", cmd_script)

    imgui.Process = false

    --imgui.SwitchContext()
    --themes.SwitchColorTheme(9)

    while true do
        wait(0)
        if not main_window_state.v then imgui.Process = false end
    end
end

function cmd_script()
    main_window_state.v = not main_window_state.v
    imgui.Process = main_window_state.v
end

function imgui.OnDrawFrame()
    imgui.SetNextWindowSize(imgui.ImVec2(800, 700), imgui.Cond.FirstUseEver)
    imgui.SetNextWindowPos(imgui.ImVec2((sw / 2), sh / 1.8), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
    imgui.Begin("Script", main_window_state, imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoResize + imgui.WindowFlags.NoMove)
    imgui.BeginChild("ChildWindow1", imgui.ImVec2(792, 567), true)
    imgui.TextColoredRGB("Text:", 2)
    imgui.InputTextMultiline('##text', buf_text, imgui.ImVec2(800, 700)) -- вот здесь нужно чтобы был текст который я мог бы стерать или добавлять там что-то.
    imgui.Text(u8'Текст: '..buf_text.v)
    imgui.EndChild()
    imgui.End()
end

function imgui.TextQuestion(label, description)
    imgui.TextDisabled(label)
    if imgui.IsItemHovered() then
        imgui.BeginTooltip()
        imgui.PushTextWrapPos(600)
        imgui.TextUnformatted(description)
        imgui.PopTextWrapPos()
        imgui.EndTooltip()
    end
end
 

HavenS

Участник
Автор темы
76
19
Код:
--local font_flag = require('moonloader').font_flag
--local font = renderCreateFont('Verdana', 12, font_flag.BOLD + font_flag.SHADOW)
--local ffi = require "ffi"
require "lib.moonloader"
--local memory = require 'memory'
local imgui = require "imgui"
--local notify = import "imgui_notf.lua"
local encoding = require "encoding"
local main_window_state = imgui.ImBool(false)
--local main_window_state1 = imgui.ImBool(false)
local sw, sh = getScreenResolution()
--local themes = import "resource/imgui_themes.lua"
--local fa = require 'faIcons'
--local fa_glyph_ranges = imgui.ImGlyphRanges({ fa.min_range, fa.max_range })
encoding.default = "CP1251"
u8 = encoding.UTF8

--[[local rkeys = require 'rkeys'
imgui.ToggleButton = require('imgui_addons').ToggleButton
imgui.HotKey = require('imgui_addons').HotKey
imgui.Spinner = require('imgui_addons').Spinner
imgui.BufferingBar = require('imgui_addons').BufferingBar]]



local buf_text = imgui.ImBuffer(256)
main_window_state = imgui.ImBool(false)

--[[function imgui.BeforeDrawFrame()
    if fa_font == nil then
        local font_config = imgui.ImFontConfig()
        font_config.MergeMode = true
        fa_font = imgui.GetIO().Fonts:AddFontFromFileTTF('moonloader/resource/fonts/fontawesome-webfont.ttf', 14.0, font_config, fa_glyph_ranges)
    end
end]]

function imgui.TextColoredRGB(text, render_text)
    local max_float = imgui.GetWindowWidth()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local ImVec4 = imgui.ImVec4

    local explode_argb = function(argb)
        local a = bit.band(bit.rshift(argb, 24), 0xFF)
        local r = bit.band(bit.rshift(argb, 16), 0xFF)
        local g = bit.band(bit.rshift(argb, 8), 0xFF)
        local b = bit.band(argb, 0xFF)
        return a, r, g, b
    end

    local getcolor = function(color)
        if color:sub(1, 6):upper() == 'SSSSSS' then
            local r, g, b = colors[1].x, colors[1].y, colors[1].z
            local a = tonumber(color:sub(7, 8), 16) or colors[1].w * 255
            return ImVec4(r, g, b, a / 255)
        end
        local color = type(color) == 'string' and tonumber(color, 16) or color
        if type(color) ~= 'number' then return end
        local r, g, b, a = explode_argb(color)
        return imgui.ImColor(r, g, b, a):GetVec4()
    end

    local render_text = function(text_)
        for w in text_:gmatch('[^\r\n]+') do
            local text, colors_, m = {}, {}, 1
            w = w:gsub('{(......)}', '{%1FF}')
            while w:find('{........}') do
                local n, k = w:find('{........}')
                local color = getcolor(w:sub(n + 1, k - 1))
                if color then
                    text[#text], text[#text + 1] = w:sub(m, n - 1), w:sub(k + 1, #w)
                    colors_[#colors_ + 1] = color
                    m = n
                end
                w = w:sub(1, n - 1) .. w:sub(k + 1, #w)
            end

            local length = imgui.CalcTextSize(w)
            if render_text == 2 then
                imgui.NewLine()
                imgui.SameLine(max_float / 2 - ( length.x / 2 ))
            elseif render_text == 3 then
                imgui.NewLine()
                imgui.SameLine(max_float - length.x - 5 )
            end
            if text[0] then
                for i = 0, #text do
                    imgui.TextColored(colors_[i] or colors[1], text[i])
                    imgui.SameLine(nil, 0)
                end
                imgui.NewLine()
            else imgui.Text(w) end


        end
    end

    render_text(text)
end

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

    sampRegisterChatCommand("test", cmd_script)

    imgui.Process = false

    --imgui.SwitchContext()
    --themes.SwitchColorTheme(9)

    while true do
        wait(0)
        if not main_window_state.v then imgui.Process = false end
    end
end

function cmd_script()
    main_window_state.v = not main_window_state.v
    imgui.Process = main_window_state.v
end

function imgui.OnDrawFrame()
    imgui.SetNextWindowSize(imgui.ImVec2(800, 700), imgui.Cond.FirstUseEver)
    imgui.SetNextWindowPos(imgui.ImVec2((sw / 2), sh / 1.8), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
    imgui.Begin("Script", main_window_state, imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoResize + imgui.WindowFlags.NoMove)
    imgui.BeginChild("ChildWindow1", imgui.ImVec2(792, 567), true)
    imgui.TextColoredRGB("Text:", 2)
    imgui.InputTextMultiline('##text', buf_text, imgui.ImVec2(800, 700)) -- вот здесь нужно чтобы был текст который я мог бы стерать или добавлять там что-то.
    imgui.Text(u8'Текст: '..buf_text.v)
    imgui.EndChild()
    imgui.End()
end

function imgui.TextQuestion(label, description)
    imgui.TextDisabled(label)
    if imgui.IsItemHovered() then
        imgui.BeginTooltip()
        imgui.PushTextWrapPos(600)
        imgui.TextUnformatted(description)
        imgui.PopTextWrapPos()
        imgui.EndTooltip()
    end
end
а как можно сделать чтобы в input поле уже был введён текст переменной?
 

Sidney31

Известный
1,132
385
а как можно сделать чтобы в input поле уже был введён текст переменной?
Замени local buf_text = imgui.ImBuffer(256) на local buf_text = imgui.ImBuffer(256, u8"какой-то текст")
------
u8 для вывода кириллицы, если убрать, то будут вопросительные знаки вместо текста
 

HavenS

Участник
Автор темы
76
19
Замени local buf_text = imgui.ImBuffer(256) на local buf_text = imgui.ImBuffer(256, u8"какой-то текст")
------
u8 для вывода кириллицы, если убрать, то будут вопросительные знаки вместо текста
Замени local buf_text = imgui.ImBuffer(256) на local buf_text = imgui.ImBuffer(256, u8"какой-то текст")
------
u8 для вывода кириллицы, если убрать, то будут вопросительные знаки вместо текста
Не запускается скрипт:
[ML] (error) script.lua: E:\SAMM8ECLaq9zC4xgck6QUJACuAxDkZyouDAn3bqDP45PvN.lua:26: sol: no matching function call takes this number of arguments and the specified types
stack traceback:
[C]: in function 'ImBuffer'
E:\SAMM8ECLaq9zC4xgck6QUJACuAxDkZyouDAn3bqDP45PvN.lua:26: in main chunk