Scripting/Client/GUI
Constructors
| Call | Creates |
|---|
GUIWindow(pos, size, title) | A framed window with an optional titlebar; the only element that can hold children. |
GUILabel(pos, size, text) | Text with a transparent background. |
GUIButton(pos, size, text) | Clickable button. Needs a visible mouse cursor (ShowMouseCursor(true)). |
GUICheckbox(pos, size, checked) | checked is 0 or 1. |
GUIEditbox(pos, size) | Single-line text input. Read the content from .Text. |
GUIMemobox(pos, size) | Multi-line read-only text log (last 24 lines, up to 95 characters each). |
GUIProgressBar(pos, size) | A bar filled from Value to MaxValue. |
GUISprite(file, pos [, rotation, rotationCentre]) | An image. file is a <sprite> downloaded from the server (just the file name) or a local path. |
GUIListBox(pos, size) | (0.6) A scrollable list of text rows; a click selects a row. See 10.5. |
GUIDropdownBox(pos, size) | (0.6) One line showing the choice; a click opens the list of choices over everything else. size is the closed box. See 10.5. |
Layer functions
Element methods
Element properties
Colours accept a Colour(r,g,b,a), a {r,g,b,a} table, or an 0xAARRGGBB integer. Reading a colour returns a table.
| Property | Type | Access | Description |
|---|
ID | integer | r | Element id. |
Pos / Size | VectorScreen | rw | Position (relative to the parent window) and size in pixels. |
Text (TitleText) | string | rw | Label/button/window title/editbox content. |
Colour | colour | rw | Background colour. |
TextColour / TitlebarTextColour | colour | rw | |
StartColour / EndColour | colour | rw | Gradient ends of a progress bar. |
Alpha | integer | rw | 0-255 overall opacity. |
Visible | boolean | rw | Hide without removing. |
Transparent | boolean | rw | No background drawn. |
FontName / FontSize / FontTags | string / int / string | rw | Font face and size. FontName takes a PC font name or (0.5, PC and Android) a <font> file of the server: "pricedown.ttf", "pricedown" or its family name. FontTags is stored only. |
TextAlignment | integer | rw | ALIGN_* (0-8): the nine grid positions from ALIGN_TOP_LEFT to ALIGN_BOTTOM_RIGHT. |
Flags | integer | rw | Stored only (FLAG_*). |
Checked | boolean | rw | Checkbox state. |
Active | boolean | rw | Editbox has keyboard focus (set true to focus it). |
MaskInput | boolean | rw | Show * instead of characters (passwords). |
ReadOnly | boolean | rw | Editbox cannot be edited. |
Value / MaxValue | float | rw | Progress bar fill. |
Thickness | integer | rw | Progress bar border. |
Lines | integer | r | Number of lines in a memobox. |
Rotation / RotationCentre / Scale | float / VectorScreen / VectorScreen | rw | Sprite transform. |
Moveable / Titlebar | boolean | rw | Window can be dragged / shows its titlebar. |
Width | float | rw | Width in pixels. |
// Client.nut - a small login window
function onScriptLoad()
{
local w = ScreenWidth, h = ScreenHeight;
win <- GUIWindow(VectorScreen(w/2 - 150, h/2 - 70), ScreenSize(300, 140), "Login");
win.Moveable = true;
name <- GUILabel(VectorScreen(15, 30), ScreenSize(270, 20), "Enter the server password:");
pass <- GUIEditbox(VectorScreen(15, 55), ScreenSize(270, 24));
pass.MaskInput = true;
ok <- GUIButton(VectorScreen(100, 95), ScreenSize(100, 30), "Join");
ok.SetCallbackFunc("onJoinClicked");
win.AddChild(name); win.AddChild(pass); win.AddChild(ok);
AddGUILayer(win);
ShowMouseCursor(true);
ToggleCameraMovement(false);
}
function onJoinClicked()
{
CallServerFunc("Main/Server.nut", "tryPassword", FindLocalPlayer(), pass.Text);
win.Visible = false;
ShowMouseCursor(false);
ToggleCameraMovement(true);
}
function onGameWindowResize(w, h) { RescaleGUIElement(win); }
List boxes and dropdown boxes
(0.6, PC and Android) GUIListBox shows a list of text rows with a scrollbar when they do not fit; the mouse wheel scrolls it (on Android: drag it with a finger). GUIDropdownBox is one line that shows the chosen row (or Text as a hint while nothing is chosen) and opens its list over every other element when clicked; a click outside closes it. Rows are numbered from 0. Both take the usual properties (Pos, Size, Colour, TextColour, FontName, FontSize, Alpha, Visible ...) and can be children of a window.
| Property | Type | Access | Description |
|---|
Selected | integer | rw | Index of the selected row, -1 = none. Setting a row that does not exist gives -1. |
SelectedText | string | r | Text of the selected row, null when nothing is selected. |
ItemCount | integer | r | Number of rows. |
Scroll | integer | rw | First visible row (of the open list for a dropdown box). |
HighlightColour | colour | rw | Background of the selected row (default blue). |
Text | string | rw | Dropdown box: the hint shown while nothing is selected. |
MaxVisibleItems | integer | rw | Dropdown box: rows of the open list before it scrolls (1-30, default 8). |
Open | boolean | rw | Dropdown box: the list is showing. Set it to open / close the list from a script. |
// Client.nut - pick a car from a dropdown box, a list of saved garages
function onScriptLoad()
{
win <- GUIWindow(VectorScreen(200, 150), ScreenSize(420, 300), "Garage");
cars <- GUIDropdownBox(VectorScreen(15, 35), ScreenSize(190, 26));
cars.Text = "Choose a car...";
foreach (name in ["Banshee", "Infernus", "Cheetah", "Stinger"]) cars.AddItem(name);
cars.SetCallbackFunc("onCarPicked");
garages <- GUIListBox(VectorScreen(215, 35), ScreenSize(190, 240));
for (local i = 1; i <= 30; i++) garages.AddItem("Garage " + i);
garages.SetCallbackFunc(function(index, text) { Message("You chose " + text); });
win.AddChild(cars); win.AddChild(garages);
AddGUILayer(win);
ShowMouseCursor(true);
}
function onCarPicked(index, text)
{
CallServerFunc("Main/Server.nut", "spawnCar", FindLocalPlayer(), text);
}
GUI styles: colours, fonts, borders, rounded corners
(0.6, PC, Android and PS3) Every element type has a style: its default colours, border, corner rounding, font and a few sizes. Change a style once and every element of that type follows it, also the ones that already exist. A property the script set on an element itself (btn.Colour = ...) stays as it is. Without any style calls the GUI looks exactly as before. The styles go back to the built-in look when the player leaves the server.
| Property | Types | Meaning (built-in value) |
|---|
Colour | all | Background. Window 0xA0303050, button 0x90606060, edit / memo / list / dropdown 0xF0202020, others 0x90000000. |
TextColour | all | Text (checkbox: the check mark). White; dialog 0xFFE8E8E8. |
BorderColour | all | Border / outline. Black. |
BorderSize | all | Border width in pixels, 0 = none (0-16). 1; label 0; dialog 2. |
Rounding | all | Corner radius in pixels (0-64). 0 = square corners. |
FontName | all | Font. "Arial". |
FontSize | all | Font size (6-72). 12; dialog 0 = follows the screen height. |
TitlebarColour | window, dialog | Title bar. 0xFF404080; dialog 0xFF2D4A7A. |
TitlebarTextColour | window, dialog | Title text. White. |
TitlebarHeight | window | Title bar height in pixels (8-100). 18. |
PressedColour | button | Face while pressed. 0xFF3A3A42. |
HoverColour | button, listbox, dropdown, dialog | Under the mouse. Button: 0 = no hover look (PC only); lists 0x38FFFFFF. |
Bevel | button | 1 = the raised / sunken 3D edges (only with Rounding 0), 0 = flat with BorderColour. |
BevelLightColour, BevelDarkColour | button | The bevel edges. 0xFF9A9AA2 / black. |
BoxSize | checkbox | Box size in pixels, 0 = from the element height (up to 18). |
FocusColour | editbox, dropdown | Border while typing / while the list is open. White. |
SelectionColour, CaretColour | editbox, dialog | Selected text; the cursor (0 = the text colour). |
StartColour, EndColour | progressbar | The bar's gradient. Green to red. |
HighlightColour | listbox, dropdown, dialog | The selected row. 0xFF3C6EC8. |
ScrollbarColour, ScrollbarThumbColour | listbox, dropdown, dialog | Scrollbar track and thumb. |
ScrollbarDragColour | listbox, dropdown | The thumb while dragged. |
ScrollbarWidth, RowPadding | listbox, dropdown | Scrollbar width (4-40, 9); extra height of a row over the font (0-40, 6). |
BoxHoverColour, PopupColour, PopupBorderColour | dropdown | The closed box under the mouse; the open list and its border. |
DimColour, AccentColour | dialog | Over the game behind the dialog; the line under the title and the input border. |
HeaderColour, HeaderTextColour | dialog | The header row of a TABLIST_HEADERS dialog. |
InputColour, InputTextColour | dialog | The input box. |
ButtonColour, ButtonHoverColour, ButtonTextColour | dialog | The dialog's buttons. |
NoteOn Android the server dialogs are Android's own dialogs, so the "dialog" style is stored but not drawn there; buttons have no hover look on a touch screen. Rounded corners cost more drawing on Android and PS3 (each corner row is a rectangle): keep Rounding small on screens with many elements.
// Client.nut - a dark theme with rounded corners for every GUI element of the server
function onScriptLoad()
{
SetGUIStyle("all", "Rounding", 8);
SetGUIStyle("all", "BorderColour", Colour(58, 63, 75));
SetGUIStyle("all", "FontName", "Tahoma");
SetGUIStyle("window", "Colour", Colour(27, 30, 36, 240));
SetGUIStyle("window", "TitlebarColour", Colour(43, 108, 176));
SetGUIStyle("window", "TitlebarHeight", 26);
SetGUIStyle("button", "Colour", Colour(43, 108, 176));
SetGUIStyle("button", "HoverColour", Colour(59, 130, 208));
SetGUIStyle("button", "PressedColour", Colour(30, 78, 128));
SetGUIStyle("button", "BorderSize", 0);
SetGUIStyle("listbox", "HighlightColour", Colour(43, 108, 176));
SetGUIStyle("dialog", "TitlebarColour", Colour(43, 108, 176));
SetGUIStyle("dialog", "ButtonColour", Colour(43, 108, 176));
local win = GUIWindow(VectorScreen(200, 150), ScreenSize(300, 160), "Rounded");
local ok = GUIButton(VectorScreen(20, 100), ScreenSize(120, 30), "OK");
ok.Colour = Colour(40, 160, 80); // this one button stays green
win.AddChild(ok);
AddGUILayer(win);
}
- SetGUIStyle – Sets one property of a style.
- GetGUIStyle – Reads one property of a style (a colour comes back as a table with r, g, b, a).
- ResetGUIStyle – Back to the built-in look: all styles, or only the one of type.
Categories: Client GUI