Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate images from Imgur #47

Merged
merged 12 commits into from
Aug 23, 2023
39 changes: 32 additions & 7 deletions lua/pixelui/core/cl_images.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ local useProxy = false

file.CreateDir(PIXEL.DownloadPath)

local function endsWithExtension(str)
local fileName = str:match(".+/(.-)$")
if not fileName then
return false
end
local extractedExtension = fileName and fileName:match("^.+(%..+)$")

return extractedExtension and string.sub(str, -#extractedExtension) == extractedExtension or false
end

local function processQueue()
if queue[1] then
local url, filePath, matSettings, callback = unpack(queue[1])
Expand All @@ -31,8 +41,13 @@ local function processQueue()
if len > 2097152 then
materials[filePath] = Material("nil")
else
file.Write(filePath, body)
materials[filePath] = Material("../data/" .. filePath, matSettings or "noclamp smooth mips")
local writeFilePath = filePath
if not endsWithExtension(filePath) then
writeFilePath = filePath .. ".png"
end

file.Write(writeFilePath, body)
materials[filePath] = Material("../data/" .. writeFilePath, matSettings or "noclamp smooth mips")
end

callback(materials[filePath])
Expand All @@ -52,7 +67,12 @@ end

function PIXEL.GetImage(url, callback, matSettings)
local protocol = url:match("^([%a]+://)")
local urlWithoutProtocol = string.gsub(url, protocol, "")
local urlWithoutProtocol = url
if not protocol then
protocol = "http://"
else
urlWithoutProtocol = string.gsub(url, protocol, "")
end

local fileNameStart = url:find("[^/]+$")
if not fileNameStart then
Expand All @@ -66,10 +86,15 @@ function PIXEL.GetImage(url, callback, matSettings)

file.CreateDir(dirPath)

local readFilePath = filePath
if not endsWithExtension(filePath) and file.Exists(filePath .. ".png", "DATA") then
readFilePath = filePath .. ".png"
end

if materials[filePath] then
callback(materials[filePath])
elseif file.Exists(filePath, "DATA") then
materials[filePath] = Material("../data/" .. filePath, matSettings or "noclamp smooth mips")
elseif file.Exists(readFilePath, "DATA") then
materials[filePath] = Material("../data/" .. readFilePath, matSettings or "noclamp smooth mips")
callback(materials[filePath])
else
table.insert(queue, {
Expand All @@ -91,6 +116,6 @@ end


function PIXEL.GetImgur(id, callback, _, matSettings)
local url = "i.imgur.com/" .. id .. ".png"
local url = "https://i.imgur.com/" .. id .. ".png"
PIXEL.GetImage(url, callback, matSettings)
end
end
20 changes: 10 additions & 10 deletions lua/pixelui/drawing/cl_circle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

do
local materials = {
"4c5f5nk", --8
"mONPuyy", --16
"icx1Qbq", --32
"TpwrpKe", --64
"E8QbV5i", --128
"wAr5H1x", --256
"g52zxtK", --512
"9tHAUp6", --1024
"XAYX2uH" --2048
"https://pixel-cdn.lythium.dev/i/srlt7tk7m", --8
"https://pixel-cdn.lythium.dev/i/l2km82zi", --16
"https://pixel-cdn.lythium.dev/i/5mqrguuxd", --32
"https://pixel-cdn.lythium.dev/i/yxh641f2a", --64
"https://pixel-cdn.lythium.dev/i/yz2n2neu", --128
"https://pixel-cdn.lythium.dev/i/v4sxyjdd8", --256
"https://pixel-cdn.lythium.dev/i/nmp8368j", --512
"https://pixel-cdn.lythium.dev/i/e425w7lrj", --1024
"https://pixel-cdn.lythium.dev/i/iinrlgj5b" --2048
}

local max = math.max
Expand All @@ -40,7 +40,7 @@ do
curSize = curSize + curSize
end

PIXEL.DrawImgur(x, y, w, h, id, col)
PIXEL.DrawImage(x, y, w, h, id, col)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lua/pixelui/drawing/cl_images.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local materials = {}
local grabbingMaterials = {}

local getImage = PIXEL.GetImage
PIXEL.GetImgur(PIXEL.ProgressImageID, function(mat)
getImage(PIXEL.ProgressImageURL, function(mat)
progressMat = mat
end)

Expand Down Expand Up @@ -86,11 +86,11 @@ function PIXEL.DrawImageRotated(x, y, w, h, rot, url, col)
end

function PIXEL.DrawImgur(x, y, w, h, imgurId, col)
local url = "i.imgur.com/" .. id .. ".png"
local url = "https://i.imgur.com/" .. imgurId .. ".png"
PIXEL.DrawImage(x, y, w, h, url, col)
end

function PIXEL.DrawImgurRotated(x, y, w, h, rot, imgurId, col)
local url = "i.imgur.com/" .. id .. ".png"
local url = "https://i.imgur.com/" .. imgurId .. ".png"
PIXEL.DrawImageRotated(x, y, w, h, rot, url, col)
end
6 changes: 5 additions & 1 deletion lua/pixelui/drawing/cl_overheads.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ local function drawOverhead(ent, pos, text, ang, scale)
PIXEL.DrawRoundedBox(12, x, y, h, h, PIXEL.Colors.Primary)
PIXEL.DrawRoundedBoxEx(12, x + (h - 12), y + h - 20, w + 15, 20, PIXEL.Colors.Primary, false, false, false, true)
PIXEL.DrawText(text, "UI.Overhead", x + h + 15, y + 8, PIXEL.Colors.PrimaryText)
PIXEL.DrawImgur(x + 10, y + 10, h - 20, h - 20, Icon, color_white)
PIXEL.DrawImage(x + 10, y + 10, h - 20, h - 20, Icon, color_white)
end
end3d2d()

Expand Down Expand Up @@ -95,6 +95,10 @@ end

function PIXEL.EnableIconOverheads(new)
local oldIcon = Icon
local imgurMatch = (new or ""):match("^[a-zA-Z0-9]+$")
if imgurMatch then
new = "https://i.imgur.com/" .. new .. ".png"
end
Icon = new
return oldIcon
end
2 changes: 1 addition & 1 deletion lua/pixelui/elements/cl_category.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function PANEL:Paint(w, h)
self.ArrowRotation = lerp(FrameTime() * 10, self.ArrowRotation, self:GetParent():GetExpanded() and 0 or 90)

local arrowSize = h * .45
PIXEL.DrawImgurRotated(w - h * .3 - PIXEL.Scale(4), h / 2, arrowSize, arrowSize, self.ArrowRotation, "30Bvuwi", PIXEL.Colors.PrimaryText)
PIXEL.DrawImageRotated(w - h * .3 - PIXEL.Scale(4), h / 2, arrowSize, arrowSize, self.ArrowRotation, "https://pixel-cdn.lythium.dev/i/5r7ovslav", PIXEL.Colors.PrimaryText)
end

vgui.Register("PIXEL.CategoryHeader", PANEL, "PIXEL.Button")
Expand Down
4 changes: 2 additions & 2 deletions lua/pixelui/elements/cl_check_box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function PANEL:Init()
local boxSize = PIXEL.Scale(20)
self:SetSize(boxSize, boxSize)

self:SetImgurID("YvG7VI6")
self:SetImageURL("https://pixel-cdn.lythium.dev/i/7u6uph3x6g")

self:SetNormalColor(PIXEL.Colors.Transparent)
self:SetHoverColor(PIXEL.Colors.PrimaryText)
Expand Down Expand Up @@ -54,4 +54,4 @@ function PANEL:PaintBackground(w, h)
PIXEL.DrawRoundedBox(PIXEL.Scale(4), 0, 0, w, h, self.BackgroundCol)
end

vgui.Register("PIXEL.Checkbox", PANEL, "PIXEL.ImgurButton")
vgui.Register("PIXEL.Checkbox", PANEL, "PIXEL.ImageButton")
6 changes: 3 additions & 3 deletions lua/pixelui/elements/cl_color_picker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
local PANEL = {}

local gradientMat = Material("nil")
PIXEL.GetImgur("i0xcO1R", function(mat)
PIXEL.GetImage("https://pixel-cdn.lythium.dev/i/zxlflz5vp", function(mat)
gradientMat = mat
end)

local colorWheelMat = Material("nil")
PIXEL.GetImgur("k5mtok6", function(mat)
PIXEL.GetImage("https://pixel-cdn.lythium.dev/i/4lsnfph3b", function(mat)
colorWheelMat = mat
end)

local pickerMat = Material("nil")
PIXEL.GetImgur("t0k86qy", function(mat)
PIXEL.GetImage("https://pixel-cdn.lythium.dev/i/rhz6llj2", function(mat)
pickerMat = mat
end)

Expand Down
2 changes: 1 addition & 1 deletion lua/pixelui/elements/cl_combo_box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ end

function PANEL:PaintOver(w, h)
local dropBtnSize = PIXEL.Scale(8)
PIXEL.DrawImgur(w - dropBtnSize - PIXEL.Scale(8), h / 2 - dropBtnSize / 2, dropBtnSize, dropBtnSize, "30Bvuwi", PIXEL.Colors.PrimaryText)
PIXEL.DrawImage(w - dropBtnSize - PIXEL.Scale(8), h / 2 - dropBtnSize / 2, dropBtnSize, dropBtnSize, "https://pixel-cdn.lythium.dev/i/5r7ovslav", PIXEL.Colors.PrimaryText)
end

vgui.Register("PIXEL.ComboBox", PANEL, "PIXEL.TextButton")
38 changes: 28 additions & 10 deletions lua/pixelui/elements/cl_frame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ AccessorFunc(PANEL, "ScreenLock", "ScreenLock", FORCE_BOOL)
AccessorFunc(PANEL, "RemoveOnClose", "RemoveOnClose", FORCE_BOOL)

AccessorFunc(PANEL, "Title", "Title", FORCE_STRING)
AccessorFunc(PANEL, "ImgurID", "ImgurID", FORCE_STRING)
AccessorFunc(PANEL, "ImgurID", "ImgurID", FORCE_STRING) -- Deprecated
AccessorFunc(PANEL, "ImageURL", "ImageURL", FORCE_STRING)

function PANEL:SetImgurID(id)
self:SetImageURL("https://i.imgur.com/" .. id .. ".png")
self.ImgurID = id
end

function PANEL:GetImgurID()
return self:GetImageURL():match("https://i.imgur.com/(.-).png")
end

PIXEL.RegisterFont("UI.FrameTitle", "Open Sans Bold", 22)

function PANEL:Init()
self.CloseButton = vgui.Create("PIXEL.ImgurButton", self)
self.CloseButton:SetImgurID("z1uAU0b")
self.CloseButton = vgui.Create("PIXEL.ImageButton", self)
self.CloseButton:SetImageURL("https://pixel-cdn.lythium.dev/i/fh640z2o")
self.CloseButton:SetNormalColor(PIXEL.Colors.PrimaryText)
self.CloseButton:SetHoverColor(PIXEL.Colors.Negative)
self.CloseButton:SetClickColor(PIXEL.Colors.Negative)
Expand Down Expand Up @@ -142,7 +152,7 @@ function PANEL:OnMouseReleased()
self:MouseCapture(false)
end

function PANEL:CreateSidebar(defaultItem, imgurID, imgurScale, imgurYOffset, buttonYOffset)
function PANEL:CreateSidebar(defaultItem, imageURL, imageScale, imageYOffset, buttonYOffset)
if IsValid(self.SideBar) then return end
self.SideBar = vgui.Create("PIXEL.Sidebar", self)

Expand All @@ -153,9 +163,17 @@ function PANEL:CreateSidebar(defaultItem, imgurID, imgurScale, imgurYOffset, but
end)
end

if imgurID then self.SideBar:SetImgurID(imgurID) end
if imgurScale then self.SideBar:SetImgurScale(imgurScale) end
if imgurYOffset then self.SideBar:SetImgurOffset(imgurYOffset) end
if imageURL then
local imgurMatch = (imageURL or ""):match("^[a-zA-Z0-9]+$")
if imgurMatch then
imageURL = "https://i.imgur.com/" .. imageURL .. ".png"
end

self.SideBar:SetImageURL(imageURL)
end

if imageScale then self.SideBar:SetImageScale(imageScale) end
if imageYOffset then self.SideBar:SetImageOffset(imageYOffset) end
if buttonYOffset then self.SideBar:SetButtonOffset(buttonYOffset) end

return self.SideBar
Expand Down Expand Up @@ -219,10 +237,10 @@ function PANEL:OnClose() end
function PANEL:PaintHeader(x, y, w, h)
PIXEL.DrawRoundedBoxEx(PIXEL.Scale(6), x, y, w, h, PIXEL.Colors.Header, true, true)

local imgurID = self:GetImgurID()
if imgurID then
local imageURL = self:GetImageURL()
if imageURL then
local iconSize = h * .6
PIXEL.DrawImgur(PIXEL.Scale(6), x + (h - iconSize) / 2, y + iconSize, iconSize, imgurID, color_white)
PIXEL.DrawImage(PIXEL.Scale(6), x + (h - iconSize) / 2, y + iconSize, iconSize, imageURL, color_white)
PIXEL.DrawSimpleText(self:GetTitle(), "UI.FrameTitle", x + PIXEL.Scale(12) + iconSize, y + h / 2, PIXEL.Colors.PrimaryText, nil, TEXT_ALIGN_CENTER)
return
end
Expand Down
67 changes: 67 additions & 0 deletions lua/pixelui/elements/cl_image_button.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--[[
PIXEL UI - Copyright Notice
© 2023 Thomas O'Sullivan - All rights reserved

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
--]]

local PANEL = {}

AccessorFunc(PANEL, "ImageURL", "ImageURL", FORCE_STRING)
AccessorFunc(PANEL, "ImageSize", "ImageSize", FORCE_NUMBER)
AccessorFunc(PANEL, "NormalColor", "NormalColor")
AccessorFunc(PANEL, "HoverColor", "HoverColor")
AccessorFunc(PANEL, "ClickColor", "ClickColor")
AccessorFunc(PANEL, "DisabledColor", "DisabledColor")

function PANEL:Init()
self.ImageCol = PIXEL.CopyColor(color_white)
self:SetImageURL(PIXEL.ProgressImageURL)

self:SetNormalColor(color_white)
self:SetHoverColor(color_white)
self:SetClickColor(color_white)
self:SetDisabledColor(color_white)

self:SetImageSize(1)
end

function PANEL:PaintBackground(w, h) end

function PANEL:Paint(w, h)
self:PaintBackground(w, h)

local imageSize = h * self:GetImageSize()
local imageOffset = (h - imageSize) / 2

if not self:IsEnabled() then
PIXEL.DrawImage(imageOffset, imageOffset, imageSize, imageSize, self:GetImageURL(), self:GetDisabledColor())
return
end

local col = self:GetNormalColor()

if self:IsHovered() then
col = self:GetHoverColor()
end

if self:IsDown() or self:GetToggle() then
col = self:GetClickColor()
end

self.ImageCol = PIXEL.LerpColor(FrameTime() * 12, self.ImageCol, col)

PIXEL.DrawImage(imageOffset, imageOffset, imageSize, imageSize, self:GetImageURL(), self.ImageCol)
end

vgui.Register("PIXEL.ImageButton", PANEL, "PIXEL.Button")
Loading