Skip to content

Instantly share code, notes, and snippets.

@imring
Last active December 29, 2019 17:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imring/831bbe4e30ac0d99475dcfecabc913df to your computer and use it in GitHub Desktop.
Save imring/831bbe4e30ac0d99475dcfecabc913df to your computer and use it in GitHub Desktop.
script_name('ScreenManager')
script_author('imring')
local imgui = require 'imgui'
local encoding = require 'encoding'
local requests = require 'requests'
local folder = require 'folder'
local memory = require 'memory'
local imagesize = require 'imagesize'
local u8 = encoding.UTF8
encoding.default = 'CP!251'
local style = imgui.GetStyle()
local colors = style.Colors
local clr = imgui.Col
local ImVec4 = imgui.ImVec4
local function apply_custom_style()
imgui.SwitchContext()
style.WindowRounding = 2.0
style.WindowTitleAlign = imgui.ImVec2(0.5, 0.84)
style.ChildWindowRounding = 2.0
style.FrameRounding = 2.0
style.ItemSpacing = imgui.ImVec2(5.0, 4.0)
style.ScrollbarSize = 13.0
style.ScrollbarRounding = 0
style.GrabMinSize = 8.0
style.GrabRounding = 1.0
colors[clr.FrameBg] = ImVec4(0.16, 0.29, 0.48, 0.54)
colors[clr.FrameBgHovered] = ImVec4(0.26, 0.59, 0.98, 0.40)
colors[clr.FrameBgActive] = ImVec4(0.26, 0.59, 0.98, 0.67)
colors[clr.TitleBg] = ImVec4(0.04, 0.04, 0.04, 1.00)
colors[clr.TitleBgActive] = ImVec4(0.16, 0.29, 0.48, 1.00)
colors[clr.TitleBgCollapsed] = ImVec4(0.00, 0.00, 0.00, 0.51)
colors[clr.CheckMark] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[clr.SliderGrab] = ImVec4(0.24, 0.52, 0.88, 1.00)
colors[clr.SliderGrabActive] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[clr.Button] = ImVec4(0.26, 0.59, 0.98, 0.40)
colors[clr.ButtonHovered] = ImVec4(0.26, 0.59, 0.98, 1.00)
colors[clr.ButtonActive] = ImVec4(0.06, 0.53, 0.98, 1.00)
colors[clr.Header] = ImVec4(0.26, 0.59, 0.98, 0.31)
colors[clr.HeaderHovered] = ImVec4(0.26, 0.59, 0.98, 0.80)
colors[clr.HeaderActive] = ImVec4(0.26, 0.59, 0.98, 1.00)
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] = ImVec4(0.26, 0.59, 0.98, 0.25)
colors[clr.ResizeGripHovered] = ImVec4(0.26, 0.59, 0.98, 0.67)
colors[clr.ResizeGripActive] = ImVec4(0.26, 0.59, 0.98, 0.95)
colors[clr.TextSelectedBg] = ImVec4(0.26, 0.59, 0.98, 0.35)
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] = ImVec4(0.06, 0.06, 0.06, 0.94)
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] = ImVec4(0.43, 0.43, 0.50, 0.50)
colors[clr.BorderShadow] = ImVec4(0.00, 0.00, 0.00, 0.00)
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] = ImVec4(0.31, 0.31, 0.31, 1.00)
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.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.ModalWindowDarkening] = ImVec4(0.80, 0.80, 0.80, 0.35)
end
apply_custom_style()
local imgs
local loadimgs = {}
local info = {}
local selected = {}
local link = {}
local album = {}
local lalbum = {}
local function char_to_hex(str)
return string.format("%%%02X", string.byte(str))
end
local function url_encode(str)
local str = string.gsub(str, "\\", "\\")
local str = string.gsub(str, "([^%w])", char_to_hex)
return str
end
local function http_build_query(query)
local buff=""
for k, v in pairs(query) do
if type(v) == 'table' then
for _, m in ipairs(v) do
buff = buff.. string.format("%s=%s&", k, url_encode(m))
end
else buff = buff.. string.format("%s=%s&", k, url_encode(v)) end
end
local buff = string.reverse(string.gsub(string.reverse(buff), "&", "", 1))
return buff
end
local function send_to_site(params)
local res, response = pcall(requests.request, 'POST', 'http://api.fishlake-scripts.ru/imgur.php', {
headers = {
["content-type"] = "application/x-www-form-urlencoded"
},
data = http_build_query(params)
})
return res, response.text
end
function main()
if not isSampLoaded() or not isSampfuncsLoaded() then return end
while not isSampAvailable() do wait(0) end
local path = memory.tostring(sampGetBase() + 0x219F88) .. '\\screens'
imgs = folder.new(path)
sampRegisterChatCommand('mscr', function() imgui.Process = not imgui.Process end)
while true do wait(0)
imgs:submit('*')
info = {}
local files = imgs:files()
for i = 2, #files do
if files[i]:type() == 'file' then
info[#info + 1] = files[i]
end
end
end
end
function imgui.OnDrawFrame()
local x, y = getScreenResolution()
imgui.SetNextWindowSize(imgui.ImVec2(685, 410), imgui.Cond.FirstUseEver)
imgui.SetNextWindowPos(imgui.ImVec2(x / 2, y / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
local title = 'ScreenManager'
if #album > 0 then title = title .. ' | Album'
elseif #selected ~= 0 then title = title .. ' | ' .. selected[2]:get_name() end
local flags = 0
if #selected == 0 or #album > 0 then flags = flags + imgui.WindowFlags.NoResize end
imgui.Begin(title, _, flags)
local height = 100
if #album > 0 then
imgui.BeginChild('##scrs', imgui.ImVec2(0, imgui.GetWindowHeight() - ( imgui.GetFontSize() * 4 + style.ItemSpacing.y )))
local size = imgui.CalcTextSize('sa-mp-000.png').x
local a = 0
for i = 1, #album do
imgui.Checkbox(album[i][2]:get_name(), album[i][1])
if a + size * ( imgui.GetWindowWidth() / 200 ) < imgui.GetWindowWidth() then
imgui.SameLine()
a = a + size + style.ItemSpacing.x
else a = 0 end
end
imgui.EndChild()
imgui.Separator()
imgui.SetCursorPosX((imgui.GetWindowWidth() - imgui.CalcTextSize('Cancel Upload').x) / 2)
if imgui.Button('Cancel') then album = {} end
imgui.SameLine()
if imgui.Button('Upload') then
local res, id
local r, t = send_to_site{ type = 3 }
while not r do r, t = send_to_site{ type = 3 } end
if t and t:sub(1, 1) == '{' then
local json = decodeJson(t)
if json and json.success then
res = json.data.deletehash
id = json.data.id
end
end
local b = {}
local c = {}
for i = 1, #album do
if album[i][1].v then
table.insert(c, album[i][2]:get_name())
local file = album[i][2]:open('rb')
if file then
local text = file:read('*a')
file:close()
local r, t = send_to_site{ img = text, type = 1 }
while not r do r, t = send_to_site{ img = text, type = 1 } end
if t and t:sub(1, 1) == '{' then
local json = decodeJson(t)
if json and json.success then
table.insert(b, json.data.deletehash)
end
end
end
end
end
local r, t = send_to_site{ ['img[]'] = b, type = 2, dh = res }
while not r do r, t = send_to_site{ ['img[]'] = b, type = 2, dh = res } end
if t and t:sub(1, 1) == '{' then
local json = decodeJson(t)
if json and json.success then
for i = 1, #c do
lalbum[c[i]] = imgui.ImBuffer('https://imgur.com/a/' .. id, 100)
end
end
end
album = {}
end
elseif #selected == 0 then
local size = imgui.CalcTextSize('sa-mp-000.png').x
local a = 0
for i = 1, #info do
if imgui.Button(info[i]:get_name()) then
local width, height = imagesize.imgsize(info[i]:full_path_name())
selected = { i, info[i], imgui.CreateTextureFromFile(info[i]:full_path_name()), width, height }
end
if a + size * ( imgui.GetWindowWidth() / 300 ) < imgui.GetWindowWidth() then
imgui.SameLine()
a = a + size + style.ItemSpacing.x
else a = 0 end
end
else
local a = selected[4] / selected[5]
local b = 4
if link[selected[2]:get_name()] then b = b + 2 end
if lalbum[selected[2]:get_name()] then b = b + 2 end
local height = imgui.GetWindowHeight() - ( imgui.GetFontSize() * b + style.ItemSpacing.y )
--[[if tostring(selected[3]):find('NULL') then
selected[3] = imgui.CreateTextureFromFile(selected[2]:full_path_name())
end]]
imgui.BeginChild('##scr', imgui.ImVec2(-1, height))
local c = imgui.GetCursorPosY()
if selected[1] > 1 then
imgui.SetCursorPosY((imgui.GetWindowHeight() - imgui.GetFontSize()) / 2)
if imgui.Button('<<') then
local i = selected[1] - 1
local width, height = imagesize.imgsize(info[i]:full_path_name())
selected = { i, info[i], imgui.CreateTextureFromFile(info[i]:full_path_name()), width, height }
end
imgui.SetCursorPosY(c)
end
local c = imgui.GetCursorPos()
if selected[1] < #info then
imgui.SetCursorPosY((imgui.GetWindowHeight() - imgui.GetFontSize()) / 2)
imgui.SetCursorPosX(imgui.GetWindowWidth() - imgui.CalcTextSize('>>').x - style.ItemSpacing.x * 1.7)
if imgui.Button('>>') then
local i = selected[1] + 1
local width, height = imagesize.imgsize(info[i]:full_path_name())
selected = { i, info[i], imgui.CreateTextureFromFile(info[i]:full_path_name()), width, height }
end
imgui.SetCursorPos(c)
end
height = height - 5
imgui.SetCursorPosX((imgui.GetWindowWidth() - a * height) / 2)
imgui.Image(selected[3], imgui.ImVec2(a * height, height))
imgui.EndChild()
imgui.Separator()
imgui.SetCursorPosX((imgui.GetWindowWidth() - imgui.CalcTextSize('Back Delete Upload to Imgur Upload album to Imgur').x) / 2)
if imgui.Button('Back') then
selected = {}
end
imgui.SameLine()
if imgui.Button('Delete') then
selected[2]:remove()
selected = {}
end
imgui.SameLine()
if imgui.Button('Upload to Imgur') then
if not link[selected[2]:get_name()] then
local file = selected[2]:open('rb')
if file then
local text = file:read('*a')
file:close()
local r, t = send_to_site{ img = text, type = 1 }
while not r do r, t = send_to_site{ img = text, type = 1 } end
if t and t:sub(1, 1) == '{' then
local json = decodeJson(t)
if json and json.success then
link[selected[2]:get_name()] = imgui.ImBuffer(json.data.link, 100)
end
end
end
end
end
imgui.SameLine()
if imgui.Button('Upload album to Imgur') then
for i = 1, #info do
local b = false
if info[i]:full_path_name() == selected[2]:full_path_name() then b = true end
album[i] = { imgui.ImBool(b), info[i] }
end
end
if #selected > 0 then
local a = link[selected[2]:get_name()]
if a then
local size = imgui.CalcTextSize('Link: ' .. a.v).x
imgui.SetCursorPosX((imgui.GetWindowWidth() - size) / 2)
imgui.Text('Link:')
imgui.SameLine()
imgui.PushItemWidth(imgui.CalcTextSize(a.v).x + style.ItemSpacing.x)
imgui.InputText('##link', a, imgui.InputTextFlags.ReadOnly)
imgui.PopItemWidth()
end
local b = lalbum[selected[2]:get_name()]
if b then
local size = imgui.CalcTextSize('Album: ' .. b.v).x
imgui.SetCursorPosX((imgui.GetWindowWidth() - size) / 2)
imgui.Text('Album:')
imgui.SameLine()
imgui.PushItemWidth(imgui.CalcTextSize(b.v).x + style.ItemSpacing.x)
imgui.InputText('##link', b, imgui.InputTextFlags.ReadOnly)
imgui.PopItemWidth()
end
end
end
imgui.End()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment