Skip to content
Download

Interface

Interfaces are built in the editor’s interface tab. In code, an interface and its elements are referred to by name:

Ui::Element loja = Ui::get("Loja");
loja["Moedas"].setText("Moedas: 12");
loja["Fechar"].onClick(this, [] { Ui::hide("Loja"); });
loja.show();

Ui::Element is just an address: it is cheap to copy and to keep, and each call looks the element up again. A name that doesn’t exist logs an error to the Output (once) and the call does nothing.

Function What it does Example
Ui::Element Ui::get(std::string_view ui) The interface with that name. Ui::Element hud = Ui::get("Hud");
void Ui::show(std::string_view ui) Shows the interface, on top of the ones already on screen. Ui::show("Pausa");
void Ui::hide(std::string_view ui) Hides the interface. What the code changed in it still holds. Ui::hide("Pausa");
bool Ui::visible(std::string_view ui) true if the interface is on screen. if (Ui::visible("Loja")) ...
void Ui::reset(std::string_view ui) Puts the interface back the way it is in the editor and removes the onClick handlers. Ui::reset("Loja");
bool Ui::mouseOver() true if the mouse is over a button or a panel of the interface. if (!Ui::mouseOver()) atirar = true;

The methods of Ui::Element. The ones that start with set can be chained: botao.setText("Comprado").setEnabled(false);

Function What it does Example
Element operator[](std::string_view name) An element inside, by name: direct children first, then the ones inside them. hud["Moedas"]
bool exists() true if the interface or the element exists. if (loja["Item3"].exists()) ...
setText(std::string_view text) e text() The text of a Texto or a Botão. hud["Moedas"].setText("12");
setValue(float value) e value() How full a Barra is, from 0 to 1. hud["Vida"].setValue(0.5f);
setVisible(bool visible), show(), hide() e visible() Shows or hides the whole interface or one element (with what is inside it). hud["Chave"].show();
setEnabled(bool enabled) e enabled() Button: disabled, it is dimmed and does not respond. botao.setEnabled(moedas >= 10);
setSprite(std::string_view sprite, int frame = 0) e setFrame(int frame) Changes the sprite of an Imagem (or the background of a Painel or Botão) and the frame. linha["Icone"].setSprite("espada");
setColor(Color color) The main color: the background of a Painel and a Botão, the filled part of a Barra, the letters of a Texto. hud["Vida"].setColor(colors::red);
setTextColor(Color color) e setBorderColor(Color color) The color of the letters and of the border. botao.setTextColor(colors::yellow);
setFont(std::string_view font, int size = 0) e setTextSize(int size) The font (a project font; "" is the built-in one) and the size in pixels. titulo.setFont("Titulos", 24);
setPosition(float x, float y) e setSize(float width, float height) The position (from the anchor) and the size, in game pixels. hud["Seta"].setPosition(10, 40);
Element clone(std::string_view name) Copies the element (with what is inside it) into the same panel, at the end of the list, visible. With a name that already exists, it rebuilds the copy. modelo.clone("Item0");
void remove() Removes the element from the interface. loja["Itens"]["Item0"].remove();
onClick(const Entity* owner, std::function<void()> callback) Runs callback when the button is pressed. With owner (pass this), the handler goes away when the instance is destroyed. botao.onClick(this, [this] { restart(); });
setScroll(float pixels) and scroll() How far the list of a Panel with Rolar (scroll) is scrolled, in pixels (0 = at the start). The mouse wheel already scrolls it by itself. lista.setScroll(0);
onMouseEnter(const Entity* owner, std::function<void()> callback) and onMouseLeave(...) Runs callback when the mouse enters the element and when it leaves it. linha.onMouseEnter(this, [linha] { linha.setColor(colors::yellow); });
Function What it does Example
void setPaused(bool paused) Pauses or resumes the game. While paused, instances don’t run timers, onUpdate, movement or collisions. setPaused(true);
bool paused() true if the game is paused. if (paused()) ...
void onPausedUpdate() Instance event: once per step while the game is paused, instead of onUpdate. void onPausedUpdate() override;
  • Each interface exists only once while the game runs. Hiding and showing doesn’t undo what the code changed; for that, use Ui::reset.
  • The interfaces checked under Interfaces, in the room Inspector, open with it and close when leaving it. The ones the code shows stay until the code hides them.
  • With the mouse over a button or a panel with a background, mousePressed, mouseHeld and mouseReleased return false to the game.
  • The mouse works on the buttons of every visible interface; what is in front covers what is behind. The keyboard moves through the buttons of the front interface, with the cima, baixo, esquerda, direita and confirmar actions.
  • Changing rooms (or restarting) removes the pause.
  • onClick without owner also exists, and holds until a Ui::reset. Prefer passing this.

See HUD and pause menu and Shop with a list of items.