- 120
- 20
- Версия MoonLoader
- .026-beta
Здравствуйте.
Задался вопросом, можно ли как-то сделать несколько строк в JSON?
Суть: есть код, который сохраняет команду и текст, текст вводится через InputTextMultiline.
Я хочу вывести:
123
321
А в итоге получаю 123
Код ниже:
Задался вопросом, можно ли как-то сделать несколько строк в JSON?
Суть: есть код, который сохраняет команду и текст, текст вводится через InputTextMultiline.
Я хочу вывести:
123
321
А в итоге получаю 123
Код ниже:
Lua:
local imgui = require('imgui')
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8
local window = imgui.ImBool(false)
local list = {}
local json_file = getWorkingDirectory()..'\\config\\Test.json'
function style()
imgui.SwitchContext()
local style = imgui.GetStyle()
local colors = style.Colors
local clr = imgui.Col
local ImVec4 = imgui.ImVec4
colors[clr.Text] = ImVec4(0.00, 0.00, 0.00, 0.51)
colors[clr.TextDisabled] = ImVec4(0.24, 0.24, 0.24, 1.00)
colors[clr.WindowBg] = ImVec4(1.00, 1.00, 1.00, 1.00)
colors[clr.ChildWindowBg] = ImVec4(0.96, 0.96, 0.96, 1.00)
colors[clr.PopupBg] = ImVec4(0.92, 0.92, 0.92, 1.00)
colors[clr.Border] = ImVec4(0.86, 0.86, 0.86, 1.00)
colors[clr.BorderShadow] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[clr.FrameBg] = ImVec4(0.88, 0.88, 0.88, 1.00)
colors[clr.FrameBgHovered] = ImVec4(0.82, 0.82, 0.82, 1.00)
colors[clr.FrameBgActive] = ImVec4(0.76, 0.76, 0.76, 1.00)
colors[clr.TitleBg] = ImVec4(0.00, 0.45, 1.00, 0.82)
colors[clr.TitleBgCollapsed] = ImVec4(0.00, 0.45, 1.00, 0.82)
colors[clr.TitleBgActive] = ImVec4(0.00, 0.45, 1.00, 0.82)
colors[clr.MenuBarBg] = ImVec4(0.00, 0.37, 0.78, 1.00)
colors[clr.ScrollbarBg] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[clr.ScrollbarGrab] = ImVec4(0.00, 0.35, 1.00, 0.78)
colors[clr.ScrollbarGrabHovered] = ImVec4(0.00, 0.33, 1.00, 0.84)
colors[clr.ScrollbarGrabActive] = ImVec4(0.00, 0.31, 1.00, 0.88)
colors[clr.ComboBg] = ImVec4(0.92, 0.92, 0.92, 1.00)
colors[clr.CheckMark] = ImVec4(0.00, 0.49, 1.00, 0.59)
colors[clr.SliderGrab] = ImVec4(0.00, 0.49, 1.00, 0.59)
colors[clr.SliderGrabActive] = ImVec4(0.00, 0.39, 1.00, 0.71)
colors[clr.Button] = ImVec4(0.00, 0.49, 1.00, 0.59)
colors[clr.ButtonHovered] = ImVec4(0.00, 0.49, 1.00, 0.71)
colors[clr.ButtonActive] = ImVec4(0.00, 0.49, 1.00, 0.78)
colors[clr.Header] = ImVec4(0.00, 0.49, 1.00, 0.78)
colors[clr.HeaderHovered] = ImVec4(0.00, 0.49, 1.00, 0.71)
colors[clr.HeaderActive] = ImVec4(0.00, 0.49, 1.00, 0.78)
colors[clr.ResizeGrip] = ImVec4(0.00, 0.39, 1.00, 0.59)
colors[clr.ResizeGripHovered] = ImVec4(0.00, 0.27, 1.00, 0.59)
colors[clr.ResizeGripActive] = ImVec4(0.00, 0.25, 1.00, 0.63)
colors[clr.CloseButton] = ImVec4(0.00, 0.35, 0.96, 0.71)
colors[clr.CloseButtonHovered] = ImVec4(0.00, 0.31, 0.88, 0.69)
colors[clr.CloseButtonActive] = ImVec4(0.00, 0.25, 0.88, 0.67)
colors[clr.PlotLines] = ImVec4(0.00, 0.39, 1.00, 0.75)
colors[clr.PlotLinesHovered] = ImVec4(0.00, 0.39, 1.00, 0.75)
colors[clr.PlotHistogram] = ImVec4(0.00, 0.39, 1.00, 0.75)
colors[clr.PlotHistogramHovered] = ImVec4(0.00, 0.35, 0.92, 0.78)
colors[clr.TextSelectedBg] = ImVec4(0.00, 0.47, 1.00, 0.59)
colors[clr.ModalWindowDarkening] = ImVec4(0.20, 0.20, 0.20, 0.35)
end
style()
function jsonSave(jsonFilePath, t)
file = io.open(jsonFilePath, "w")
file:write(encodeJson(t))
file:flush()
file:close()
end
function jsonRead(jsonFilePath)
local file = io.open(jsonFilePath, "r+")
local jsonInString = file:read("*a")
file:close()
local jsonTable = decodeJson(jsonInString)
return jsonTable
end
function main()
while not isSampAvailable() do wait(200) end
if not doesDirectoryExist(getWorkingDirectory()..'\\config') then createDirectory(getWorkingDirectory()..'\\config') end
if not doesFileExist(json_file) then
jsonSave(json_file, {})
end
list = jsonRead(json_file)
sampRegisterChatCommand('tst', function()
window.v = not window.v
end)
imgui.Process = false
window.v = false --show window on start
while true do
wait(0)
imgui.Process = window.v
end
end
local selected = 0
local add_window = {
cmd = imgui.ImBuffer(128),
srv_cmd = imgui.ImBuffer(128),
}
local sampev = require 'lib.samp.events'
function sampev.onSendCommand(text)
if #list > 0 then
for i = 1, #list do
local d = list[i]
if text:find(d.cmd) then
sendLines(d.srv_cmd)
end
end
end
end
function imgui.OnDrawFrame()
if window.v then
local resX, resY = getScreenResolution()
local sizeX, sizeY = 500, 300 -- WINDOW SIZE
imgui.SetNextWindowPos(imgui.ImVec2(resX / 2 - sizeX / 2, resY / 2 - sizeY / 2), imgui.Cond.FirstUseEver)
imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
imgui.Begin('Test Binder', window)
imgui.SetCursorPos(imgui.ImVec2(5, 30))
imgui.BeginChild('list', imgui.ImVec2(sizeX - 10, sizeY - 110 + 25), true)
if #list > 0 then
imgui.Columns(2) imgui.Text(u8'Своя команда') imgui.NextColumn() imgui.Text(u8'Текст') imgui.Columns(1)
imgui.Separator()
for i = 1, #list do
if list[i] ~= nil then
local d = list[i]
imgui.Columns(2)
if imgui.Selectable(d.cmd, selected == i, 2) then selected = i end
imgui.NextColumn()
if imgui.Selectable(d.srv_cmd, selected == i, 2) then selected = i end
imgui.Columns(1)
imgui.Separator()
end
end
else
imgui.Text(u8'Список пуст :(')
end
imgui.EndChild()
imgui.SetCursorPos(imgui.ImVec2(5, sizeY - 25 - 25))
if imgui.Button(u8'Добавить', imgui.ImVec2(sizeX - 10, 20)) then
imgui.OpenPopup(u8'Добавление бинда')
end
imgui.SetCursorPos(imgui.ImVec2(5, sizeY - 25))
if imgui.Button(u8'Удалить', imgui.ImVec2(sizeX - 10, 20)) then
if list[selected] ~= nil then
table.remove(list, selected)
jsonSave(json_file, list)
end
end
if imgui.BeginPopupModal(u8'Добавление бинда', true, imgui.WindowFlags.NoTitleBar + imgui.WindowFlags.NoResize) and not sampIsChatInputActive() then
local sizeX, sizeY = 300, 200
imgui.SetWindowSize(imgui.ImVec2(sizeX, sizeY))
imgui.InputText(u8'Команда', add_window.cmd)
imgui.InputTextMultiline(u8'Текст', add_window.srv_cmd, imgui.ImVec2(50,50))
imgui.SetCursorPos(imgui.ImVec2(5, sizeY - 25 - 25))
if imgui.Button(u8'Добавить', imgui.ImVec2(sizeX - 10, 20)) then
table.insert(list, {cmd = add_window.cmd.v, srv_cmd = add_window.srv_cmd.v})
jsonSave(json_file, list)
add_window.cmd.v, add_window.srv_cmd.v = '', ''
imgui.CloseCurrentPopup()
end
imgui.SetCursorPos(imgui.ImVec2(5, sizeY - 25))
if imgui.Button(u8'Закрыть', imgui.ImVec2(sizeX - 10, 20)) then
add_window.cmd.v, add_window.srv_cmd.v = '', ''
imgui.CloseCurrentPopup()
end
imgui.EndPopup()
end
imgui.End()
end
end
function sendLines(text)
for line in text:gmatch('[^\n]+') do
sampSendChat(line)
end
end