Derma Basic Guide
Derma was created by Garry and TAD2020 and is the easiest way to create on-screen menus for your gamemode or addon.
Derma differs from vgui in that its code only needs to be run once, therefore you should NEVER put Derma code inside
GM:HUDPaint or similar function, as these are called each frame. To make your life easier, you should wrap your Derma code in a function and then call it whenever you need to create a menu and always use local variables. If you must use a global pointer, make it specific or a table member.
DLabel = vgui.Create( "DLabel" ) is bad coding practice and will likely override something.
The very first thing you should do is create a DFrame as this will be your canvas, so to speak, for placing Derma on.
As useful as Derma is for the Lua coder, its ugly as sin and sometimes you want a really nice looking menu. You can solve this by overriding the panel's Paint function which you can do on every single Derma element. Overriding the function means that the panel will be invisible until you draw something on it; if you would like to draw on the current panel you might try the PANEL:PaintOver function.
Your Derma menus might not be of much use if you don't know how to grab data from them; whether it would be user-typed text, a button click, or a checkbox.
Let's create a DTextEntry so the player can input their name and when they press enter! We can then assign the entered value to a variable to reference later or even use the net and send their name to the server for saving.
Now say we wanted the player to pick a color, which could be used for coloring their player or their name in chat, and then they would confirm it by pressing a button.
Not everyone who plays Garry's Mod has the same resolution or monitor size as you and as such you should create your Derma to work on nearly everyone's monitors. You can accomplish this by using the ScrH and ScrW functions which return their respective dimensions as integers. This is not necessarily something you have to worry about when you start your project as working with constant sizes is quicker, but this is very important if your Derma is quite large.
The first thing you need to do is figure out your monitor resolution, mine is 1920 x 1080 and I'll use that for this tutorial. The easiest way to find your resolution is just to use print and the screen size functions.
Now assume we created this frame and we want it to be in the center of the player's screen and be sized accordingly
Now this DFrame will be 400x300 on your resolution and it will size on any other resolution. You could then scale a DButton on this frame by dividing its size by the frame's size and it will scale according to it's parent.
It is recommended to replace static calculations later with the calculated values to reduce unnecessary operations in rendering code:
Unfortunately, due to the large amount of functions in Derma not all of them are documented yet, but you can search through the source code yourself and figure out how those functions work!
You can find an online repository here: https://github.com/garrynewman/garrysmod/tree/master/garrysmod/lua/vgui or look in
garrysmod\lua\vgui in your local
Garry's Mod directory.
A list of all Derma elements can be found here.