Alright, I’ve started a widgets system, similar to widgets on most OSes. I’ve never done this kind of thing, but I was wondering how I’d get this to work.
[lua]
Widget = {}
Widgets = {}
function Widget.Position(x,y)
return {x,y}
end
function Widget.Add(widgtable)
Widgets[widgtable.Name] = widgtable
return Widgets[widgtable.Name]
end
function Widget.GetByName(name)
return Widgets[name]
end
function Widget.GetByKValue(key)
return Widgets[k]
end
function Widget:RemoveWidget()
self = nil
end
function Widget:GetWidgetPos()
return {self.YPos, self.XPos}
end
function Widget:SetWidgetPos(pos)
if pos.x == 0 or pos.x == nil then pos.x = self:GetPos()[1] end
if pos.y == 0 or pos.y == nil then pos.y = self:GetPos()[2] end
self:SetWidgetXPos(pos.x)
self:SetWidgetYPos(pos.y)
end
function Widget:ScaleWidget(newscale)
self.Scale = newscale
end
function Widget:SetWidgetXPos(newpos)
self.Xpos = newpos
end
function Widget:SetSetWidgetYPos(newpos)
self.Ypos = newpos
end
function Widget:DrawWidget()
Scale = self.Scale
DrawYPos = self.Ypos
DrawXPos = self.Xpos
for k,v in pairs(self.Parts) do
print(k)
print(v)
v.Func(Scale,DrawYPos,DrawXPos)
end
end
function Widget.HUDThink()
for k,v in pairs(Widgets) do
print(v)
v:DrawWidget()
end
end
hook.Add("HUDPaint", "HUD_WIDGETS", Widget.HUDThink)
function Widget.DrawWidgetBox( RoundBy, PercX, PercY, PercWidth, PercHeight, BoxColor, Scalar, DrawYpos, DrawXPos, AdjustX, AdjustY)
-- Doing some checks
if PercWidth == 0 or PercHeight == 0 then
print("Percentages can't be 0!")
return
end
if DrawYPos == nil then DrawYPos = 0 end
if DrawXPos == nil then DrawXPos = 0 end
if AdjustX == nil then AdjustX = 0 end
if AdjustY == nil then AdjustY = 0 end
-- Done doing checks
-- Some simple math to decide how big the box will be
ScrHe = ScrH()
ScrWe = ScrW()
BoxHeight = ((ScrHe*PercHeight)/100)*Scalar
BoxWidth = ((ScrWe*PercWidth)/100)*Scalar
--
print(BoxHeight)
-- Some simple math to decide where the box will be placed
if DrawYPos != 0 and PercY != 0 then
BoxY = (ScrHe*(PercY-DrawYPos))/100
elseif DrawYPos != 0 then
BoxY = (ScrHe*DrawYPos)/100
elseif PercY != 0 then
BoxY = (ScrHe*PercY)/100
else
BoxY = 0
end
if DrawXPos != 0 and PercX != 0 then
BoxX = (ScrWe*(PercX-DrawXPos))/100
elseif DrawXPos != 0 then
BoxX = (ScrWe*DrawXPos)/100
elseif PercX != 0 then
BoxX = (ScrWe*PercX)/100
else
BoxX = 0
end
--
draw.RoundedBox( RoundBy, BoxX, BoxY, BoxWidth, BoxHeight, BoxColor)
end
-- This part is SUPER messy, so watch out!
WIDG = {}
WIDG.Name = "TestWidget" -- Almost all work on widgets is done with their names.
WIDG.Parts = {}
WIDG.Parts.One = {} -- Defind the parts, can be named anything
WIDG.Parts.Two = {}
function WIDG.Parts.One.Func(Scalar, DrawYPos, DrawXPos) -- Define the part one function
Widget.DrawWidgetBox( 8, 0, 0, 10, 2, Color(2,2,2,255), Scalar, DrawYpos, DrawXPos)
end
WIDG.Parts.One.Adjust = false
function WIDG.Parts.Two.Func(Scalar, DrawYPos, DrawXPos) -- Define the part two function
Widget.DrawWidgetBox( 8, 0, 2, 10, 2, Color(255,50,50,255), Scalar, DrawYpos, DrawXPos)
end
WIDG.Parts.Two.Adjust = true
WIDG.Parts.Two.X = 0
WIDG.Parts.Two.Y = 1 -- Parenting the Y axis placement of this object to the first part.
-- This makes it so part 1 and part 2 will NEVER overlap. This uhh isn't done yet.
WIDG.Scale = 1 -- The default scale of the boxes.
WIDG.YPos = 0 -- The default Y position modifier.
WIDG.XPos = 0 -- The default X position modifier.
testwidg = Widget.Add(WIDG) -- And tada we create our widget
[/lua]
None of the Widget:Function()s work. Do I have to create a metatable for them?