script_name("Perfect Binder | Lua")
script_author("DonHomka")
script_description("ImGui Binder on lua")
local imgui = require 'imgui'
local vkeys = require 'vkeys'
local rkeys = require 'rkeys'
local encoding = require 'encoding'
local hk = require 'lib.imcustom.hotkey'
local wm = require 'lib.windows.message'
local gk = require 'game.keys'
encoding.default = 'CP1251'
u8 = encoding.UTF8
hk._SETTINGS.noKeysMessage = u8("Нет")
local file = getWorkingDirectory() .. "\\config\\binds.bind"
local tEditData = {
id = -1,
inputActive = false
}
local bMainWindow = imgui.ImBool(false)
local sInputEdit = imgui.ImBuffer(128)
local bIsEnterEdit = imgui.ImBool(false)
local tBindList = {}
if doesFileExist(file) then
local f = io.open(file, "r")
if f then
tBindList = decodeJson(f:read("a*"))
f:close()
end
else
tBindList = {
[1] = {
text = "/r [Взвод №1]: ",
v = {vkeys.VK_3}
},
[2] = {
text = "/rb ",
v = {vkeys.VK_4}
},
[3] = {
text = "/time[enter]",
v = {vkeys.VK_MENU, vkeys.VK_6}
},
[4] = {
text = "/clist 30[enter]",
v = {vkeys.VK_MENU, vkeys.VK_2}
},
[5] = {
text = "/clist 7[enter]",
v = {vkeys.VK_F2}
},
[6] = {
text = "/clist ",
v = {vkeys.VK_MENU, vkeys.VK_4}
},
[7] = {
text = "/lock[enter]",
v = {vkeys.VK_1}
},
[8] = {
text = "/members[enter]",
v = {vkeys.VK_MENU, vkeys.VK_3}
}
}
end
function main()
if not isSampLoaded() then
return
end
while not isSampAvailable() do
wait(0)
end
sampRegisterChatCommand("bind", function ()
bMainWindow.v = not bMainWindow.v
end)
rkeys.registerHotKey({vkeys.VK_F1}, true, function ()
bMainWindow.v = not bMainWindow.v
end)
for k, v in pairs(tBindList) do
rkeys.registerHotKey(v.v, true, onHotKey)
end
while true do
wait(0)
imgui.Process = bMainWindow.v
end
end
function apply_custom_style()
local style = imgui.GetStyle()
local colors = style.Colors
local clr = imgui.Col
local ImVec4 = imgui.ImVec4
style.WindowRounding = 1.5
style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
style.ChildWindowRounding = 1.5
style.FrameRounding = 1.0
style.ItemSpacing = imgui.ImVec2(5.0, 4.0)
style.ScrollbarSize = 13.0
style.ScrollbarRounding = 0
style.GrabMinSize = 8.0
style.GrabRounding = 1.0
-- style.Alpha =
style.WindowPadding = imgui.ImVec2(4.0, 4.0)
-- style.WindowMinSize =
style.FramePadding = imgui.ImVec2(2.5, 3.5)
-- style.ItemInnerSpacing =
-- style.TouchExtraPadding =
-- style.IndentSpacing =
-- style.ColumnsMinSpacing = ?
style.ButtonTextAlign = imgui.ImVec2(0.02, 0.4)
-- style.DisplayWindowPadding =
-- style.DisplaySafeAreaPadding =
-- style.AntiAliasedLines =
-- style.AntiAliasedShapes =
-- style.CurveTessellationTol =
colors[clr.Text] = ImVec4(1.00, 1.00, 1.00, 1.00)
colors[clr.TextDisabled] = ImVec4(0.50, 0.50, 0.50, 1.00)
colors[clr.WindowBg] = imgui.ImColor(0, 0, 0, 227):GetVec4()
colors[clr.ChildWindowBg] = ImVec4(1.00, 1.00, 1.00, 0.00)
colors[clr.PopupBg] = ImVec4(0.08, 0.08, 0.08, 0.94)
colors[clr.ComboBg] = colors[clr.PopupBg]
colors[clr.Border] = imgui.ImColor(40, 142, 110, 90):GetVec4()
colors[clr.BorderShadow] = ImVec4(0.00, 0.00, 0.00, 0.00)
colors[clr.FrameBg] = imgui.ImColor(40, 142, 110, 113):GetVec4()
colors[clr.FrameBgHovered] = imgui.ImColor(40, 142, 110, 164):GetVec4()
colors[clr.FrameBgActive] = imgui.ImColor(40, 142, 110, 255):GetVec4()
colors[clr.TitleBg] = imgui.ImColor(40, 142, 110, 236):GetVec4()
colors[clr.TitleBgActive] = imgui.ImColor(40, 142, 110, 236):GetVec4()
colors[clr.TitleBgCollapsed] = ImVec4(0.05, 0.05, 0.05, 0.79)
colors[clr.MenuBarBg] = ImVec4(0.14, 0.14, 0.14, 1.00)
colors[clr.ScrollbarBg] = ImVec4(0.02, 0.02, 0.02, 0.53)
colors[clr.ScrollbarGrab] = imgui.ImColor(40, 142, 110, 236):GetVec4()
colors[clr.ScrollbarGrabHovered] = ImVec4(0.41, 0.41, 0.41, 1.00)
colors[clr.ScrollbarGrabActive] = ImVec4(0.51, 0.51, 0.51, 1.00)
colors[clr.CheckMark] = ImVec4(1.00, 1.00, 1.00, 1.00)
colors[clr.SliderGrab] = ImVec4(0.28, 0.28, 0.28, 1.00)
colors[clr.SliderGrabActive] = ImVec4(0.35, 0.35, 0.35, 1.00)
colors[clr.Button] = imgui.ImColor(40, 142, 110, 255):GetVec4()
colors[clr.ButtonHovered] = imgui.ImColor(35, 121, 93, 174):GetVec4()
colors[clr.ButtonActive] = imgui.ImColor(44, 154, 119, 255):GetVec4()
colors[clr.Header] = imgui.ImColor(40, 142, 110, 255):GetVec4()
colors[clr.HeaderHovered] = ImVec4(0.34, 0.34, 0.35, 0.89)
colors[clr.HeaderActive] = ImVec4(0.12, 0.12, 0.12, 0.94)
colors[clr.Separator] = colors[clr.Border]
colors[clr.SeparatorHovered] = ImVec4(0.26, 0.59, 0.98, 0.78)
colors[clr.SeparatorActive] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[clr.ResizeGrip] = imgui.ImColor(40, 142, 110, 255):GetVec4()
colors[clr.ResizeGripHovered] = imgui.ImColor(35, 121, 93, 174):GetVec4()
colors[clr.ResizeGripActive] = imgui.ImColor(44, 154, 119, 255):GetVec4()
colors[clr.CloseButton] = ImVec4(0.41, 0.41, 0.41, 0.50)
colors[clr.CloseButtonHovered] = ImVec4(0.98, 0.39, 0.36, 1.00)
colors[clr.CloseButtonActive] = ImVec4(0.98, 0.39, 0.36, 1.00)
colors[clr.PlotLines] = ImVec4(0.61, 0.61, 0.61, 1.00)
colors[clr.PlotLinesHovered] = ImVec4(1.00, 0.43, 0.35, 1.00)
colors[clr.PlotHistogram] = ImVec4(0.90, 0.70, 0.00, 1.00)
colors[clr.PlotHistogramHovered] = ImVec4(1.00, 0.60, 0.00, 1.00)
colors[clr.TextSelectedBg] = ImVec4(0.26, 0.59, 0.98, 0.35)
colors[clr.ModalWindowDarkening] = ImVec4(0.10, 0.10, 0.10, 0.35)
end
apply_custom_style()
function imgui.OnDrawFrame()
local iScreenWidth, iScreenHeight = getScreenResolution()
local tLastKeys = {}
imgui.SetNextWindowPos(imgui.ImVec2(iScreenWidth / 2, iScreenHeight / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
imgui.SetNextWindowSize(imgui.ImVec2(800, 500), imgui.Cond.FirstUseEver)
imgui.Begin(u8("Perfect Binder | Главное меню##main"), bMainWindow, imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoResize)
imgui.BeginChild("##bindlist", imgui.ImVec2(795, 442))
for k, v in ipairs(tBindList) do
if hk.HotKey("##HK" .. k, v, tLastKeys, 100) then
if not rkeys.isHotKeyDefined(v.v) then
if rkeys.isHotKeyDefined(tLastKeys.v) then
rkeys.unRegisterHotKey(tLastKeys.v)
end
rkeys.registerHotKey(v.v, true, onHotKey)
end
end
imgui.SameLine()
if tEditData.id ~= k then
local sText = v.text:gsub("%[enter%]$", "")
imgui.BeginChild("##cliclzone" .. k, imgui.ImVec2(500, 21))
imgui.AlignTextToFramePadding()
if sText:len() > 0 then
imgui.Text(u8(sText))
else
imgui.TextDisabled(u8("Пустое сообщение ..."))
end
imgui.EndChild()
if imgui.IsItemClicked() then
sInputEdit.v = sText:len() > 0 and u8(sText) or ""
bIsEnterEdit.v = string.match(v.text, "(.)%[enter%]$") ~= nil
tEditData.id = k
tEditData.inputActve = true
end
else
imgui.PushAllowKeyboardFocus(false)
imgui.PushItemWidth(500)
local save = imgui.InputText("##Edit" .. k, sInputEdit, imgui.InputTextFlags.EnterReturnsTrue)
imgui.PopItemWidth()
imgui.PopAllowKeyboardFocus()
imgui.SameLine()
imgui.Checkbox(u8("Ввод") .. "##editCH" .. k, bIsEnterEdit)
if save then
tBindList[tEditData.id].text = u8:decode(sInputEdit.v) .. (bIsEnterEdit.v and "[enter]" or "")
tEditData.id = -1
end
if tEditData.inputActve then
tEditData.inputActve = false
imgui.SetKeyboardFocusHere(-1)
end
end
end
imgui.EndChild()
imgui.Separator()
if imgui.Button(u8"Добавить клавишу") then
tBindList[#tBindList + 1] = {text = "", v = {}}
end
imgui.End()
end
function rkeys.onHotKey(id, keys)
if sampIsChatInputActive() or sampIsDialogActive() or isSampfuncsConsoleActive() then
return false
end
end
function onHotKey(id, keys)
local sKeys = tostring(table.concat(keys, " "))
for k, v in pairs(tBindList) do
if sKeys == tostring(table.concat(v.v, " ")) then
if tostring(v.text):len() > 0 then
local bIsEnter = string.match(v.text, "(.)%[enter%]$") ~= nil
if bIsEnter then
sampSendChat(v.text:gsub("%[enter%]$", ""))
else
sampSetChatInputText(v.text)
sampSetChatInputEnabled(true)
end
end
end
end
end
function onScriptTerminate(scr)
if scr == script.this then
if doesFileExist(file) then
os.remove(file)
end
local f = io.open(file, "w")
if f then
f:write(encodeJson(tBindList))
f:close()
end
end
end
addEventHandler("onWindowMessage", function (msg, wparam, lparam)
if msg == wm.WM_KEYDOWN or msg == wm.WM_SYSKEYDOWN then
if tEditData.id > -1 then
if wparam == vkeys.VK_ESCAPE then
tEditData.id = -1
consumeWindowMessage(true, true)
elseif wparam == vkeys.VK_TAB then
bIsEnterEdit.v = not bIsEnterEdit.v
consumeWindowMessage(true, true)
end
end
end
end)