Skip to content

Instantly share code, notes, and snippets.

@Tommy228
Created September 18, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tommy228/0e35e4148085ff4ebac5e0e7eea29de8 to your computer and use it in GitHub Desktop.
Save Tommy228/0e35e4148085ff4ebac5e0e7eea29de8 to your computer and use it in GitHub Desktop.
DComboBox with icons
local PANEL = {}
function PANEL:Init()
self.Icons = {}
self.IconMaterials = {}
end
function PANEL:SetText(value)
-- add some space for the icon
-- this is really not rigorous
self.BaseClass.SetText(self, " " .. value)
end
function PANEL:AddChoice(value, icon, data, select)
local choiceIndex = self.BaseClass.AddChoice(self, value, data, select)
self.Icons[choiceIndex] = icon or false
self.IconMaterials[choiceIndex] = icon and Material(icon) or false
return choiceIndex
end
function PANEL:PaintOver(w, h)
local selected = self:GetSelectedID()
local icon = self.IconMaterials[selected]
if not icon then return end
local iconSize = 16
local padding = h/2 - iconSize/2
surface.SetDrawColor(color_white)
surface.SetMaterial(icon)
surface.DrawTexturedRect(padding, padding, iconSize, iconSize)
end
function PANEL:OpenMenu()
self.BaseClass.OpenMenu(self)
local items = self.Menu:GetCanvas():GetChildren()
for index, item in ipairs(items) do
local icon = self.Icons[index]
if not icon then continue end
item:SetIcon(icon)
end
end
vgui.Register("DComboBoxIcons", PANEL, "DComboBox")
local function test()
local frame = vgui.Create("DFrame")
frame:SetSize(400, 200)
local comboBox = vgui.Create("DComboBoxIcons", frame)
comboBox:SetPos(20, 30)
comboBox:SetSize(100, 25)
comboBox:AddChoice("Test", "icon16/user.png")
comboBox:AddChoice("Test 2", "icon16/lightning.png")
comboBox:ChooseOptionID(1)
frame:MakePopup()
end
concommand.Add("testp", test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment