Classes
TDLib operates entirely around the basic idea of classes: flags which you can apply to your code to make it do different things. When you apply a class to a panel, a predefined function is ran on it. That's all they are. Classes in TDLib are everything, here are some examples of what you can do with minimal code:
- Set a panel's background color
- Send a net message when a button is clicked
- Give a panel neat transition effects on hover
- Turn a panel into a circle avatar
- Dock a panel, apply the same margin to all sides, and invalidate its layout
This is just a very basic idea of what classes do in TDLib. All available classes can be viewed on the Classes
page.
Applying Classes
There are two main ways to apply a class to a panel.
Generic
local panel = vgui.Create("DPanel")
panel:Class("Background", Color(60, 60, 60, 255))
Expicit
local panel = vgui.Create("DPanel")
panel:Background(Color(60, 60, 60, 255))
In these two examples, we apply the Background
class to the Panel
panel, with an argument of Color(60, 60, 60, 255)
. What this does is simple: Gives the panel a background color of 60, 60, 60, 255
.
Chainability
All TDLib functions can be chained, as each one returns the panel it was called on. This means that you can apply classes one after the other, without having to re-use the variable the panel is in.
Example
local but = vgui.Create("DButton")
but:Background(Color(255, 0, 0, 255)) --Apply red background
:Text("Buy a gun", "Trebuchet24", Color(0, 0, 255, 255)) --Set the button's text with Trebuchet24 font and make the color blue
:NetMessage("BuyGun") --Hook it up to send the "BuyGun" net message to the server when its clicked