Scripting/Client/GUI

Constructors

CallCreates
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.

PropertyTypeAccessDescription
IDintegerrElement id.
Pos / SizeVectorScreenrwPosition (relative to the parent window) and size in pixels.
Text (TitleText)stringrwLabel/button/window title/editbox content.
ColourcolourrwBackground colour.
TextColour / TitlebarTextColourcolourrw
StartColour / EndColourcolourrwGradient ends of a progress bar.
Alphaintegerrw0-255 overall opacity.
VisiblebooleanrwHide without removing.
TransparentbooleanrwNo background drawn.
FontName / FontSize / FontTagsstring / int / stringrwFont 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.
TextAlignmentintegerrwALIGN_* (0-8): the nine grid positions from ALIGN_TOP_LEFT to ALIGN_BOTTOM_RIGHT.
FlagsintegerrwStored only (FLAG_*).
CheckedbooleanrwCheckbox state.
ActivebooleanrwEditbox has keyboard focus (set true to focus it).
MaskInputbooleanrwShow * instead of characters (passwords).
ReadOnlybooleanrwEditbox cannot be edited.
Value / MaxValuefloatrwProgress bar fill.
ThicknessintegerrwProgress bar border.
LinesintegerrNumber of lines in a memobox.
Rotation / RotationCentre / Scalefloat / VectorScreen / VectorScreenrwSprite transform.
Moveable / TitlebarbooleanrwWindow can be dragged / shows its titlebar.
WidthfloatrwWidth 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.

PropertyTypeAccessDescription
SelectedintegerrwIndex of the selected row, -1 = none. Setting a row that does not exist gives -1.
SelectedTextstringrText of the selected row, null when nothing is selected.
ItemCountintegerrNumber of rows.
ScrollintegerrwFirst visible row (of the open list for a dropdown box).
HighlightColourcolourrwBackground of the selected row (default blue).
TextstringrwDropdown box: the hint shown while nothing is selected.
MaxVisibleItemsintegerrwDropdown box: rows of the open list before it scrolls (1-30, default 8).
OpenbooleanrwDropdown 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.

PropertyTypesMeaning (built-in value)
ColourallBackground. Window 0xA0303050, button 0x90606060, edit / memo / list / dropdown 0xF0202020, others 0x90000000.
TextColourallText (checkbox: the check mark). White; dialog 0xFFE8E8E8.
BorderColourallBorder / outline. Black.
BorderSizeallBorder width in pixels, 0 = none (0-16). 1; label 0; dialog 2.
RoundingallCorner radius in pixels (0-64). 0 = square corners.
FontNameallFont. "Arial".
FontSizeallFont size (6-72). 12; dialog 0 = follows the screen height.
TitlebarColourwindow, dialogTitle bar. 0xFF404080; dialog 0xFF2D4A7A.
TitlebarTextColourwindow, dialogTitle text. White.
TitlebarHeightwindowTitle bar height in pixels (8-100). 18.
PressedColourbuttonFace while pressed. 0xFF3A3A42.
HoverColourbutton, listbox, dropdown, dialogUnder the mouse. Button: 0 = no hover look (PC only); lists 0x38FFFFFF.
Bevelbutton1 = the raised / sunken 3D edges (only with Rounding 0), 0 = flat with BorderColour.
BevelLightColour, BevelDarkColourbuttonThe bevel edges. 0xFF9A9AA2 / black.
BoxSizecheckboxBox size in pixels, 0 = from the element height (up to 18).
FocusColoureditbox, dropdownBorder while typing / while the list is open. White.
SelectionColour, CaretColoureditbox, dialogSelected text; the cursor (0 = the text colour).
StartColour, EndColourprogressbarThe bar's gradient. Green to red.
HighlightColourlistbox, dropdown, dialogThe selected row. 0xFF3C6EC8.
ScrollbarColour, ScrollbarThumbColourlistbox, dropdown, dialogScrollbar track and thumb.
ScrollbarDragColourlistbox, dropdownThe thumb while dragged.
ScrollbarWidth, RowPaddinglistbox, dropdownScrollbar width (4-40, 9); extra height of a row over the font (0-40, 6).
BoxHoverColour, PopupColour, PopupBorderColourdropdownThe closed box under the mouse; the open list and its border.
DimColour, AccentColourdialogOver the game behind the dialog; the line under the title and the input border.
HeaderColour, HeaderTextColourdialogThe header row of a TABLIST_HEADERS dialog.
InputColour, InputTextColourdialogThe input box.
ButtonColour, ButtonHoverColour, ButtonTextColourdialogThe 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);
}
Categories: Client GUI