2015-01-01 19:36:52 +03:00
|
|
|
---
|
|
|
|
title: HUD
|
|
|
|
layout: default
|
2017-08-26 18:40:30 +03:00
|
|
|
root: ../../
|
2015-01-01 19:36:52 +03:00
|
|
|
---
|
|
|
|
|
2015-02-22 13:28:37 +03:00
|
|
|
## Introduction
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
Heads Up Display (HUD) elements allow you to show text, images, and other graphical elements.
|
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
The HUD doesn't accept user input. For that, you should use a [Formspec](formspecs.html).
|
2015-01-01 19:36:52 +03:00
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
* [Basic Interface](#basic-interface)
|
|
|
|
* [Positioning](#positioning)
|
|
|
|
* [Text Elements](#text-elements)
|
|
|
|
* [Image Elements](#image-elements)
|
|
|
|
* [Other Elements](#other-elements)
|
2015-01-01 19:36:52 +03:00
|
|
|
|
2015-02-22 13:28:37 +03:00
|
|
|
## Basic Interface
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
HUD elements are created using a player object.
|
2017-09-21 19:07:32 +03:00
|
|
|
You can get the player object from a username:
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
local player = minetest.get_player_by_name("username")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
Once you have the player object, you can create a HUD element:
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
local idx = player:hud_add({
|
|
|
|
hud_elem_type = "text",
|
|
|
|
position = {x = 1, y = 0},
|
|
|
|
offset = {x=-100, y = 20},
|
|
|
|
scale = {x = 100, y = 100},
|
|
|
|
text = "My Text"
|
|
|
|
})
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
The attributes in the HUD element table and what they do vary depending on
|
2015-01-01 19:36:52 +03:00
|
|
|
the `hud_elem_type`.\\
|
2017-09-21 19:07:32 +03:00
|
|
|
The `hud_add` function returns a number which is needed to identify the HUD element
|
|
|
|
if you wanted to change or delete it.
|
2015-01-01 19:36:52 +03:00
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
You can change an attribute after creating a HUD element. For example, you can change
|
|
|
|
the text:
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
player:hud_change(idx, "text", "New Text")
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
You can also delete the element:
|
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
player:hud_remove(idx)
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2015-02-22 13:28:37 +03:00
|
|
|
## Positioning
|
2015-01-01 19:36:52 +03:00
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
Screens come in different sizes, and HUD elements need to work well on all screens.
|
2015-01-01 19:36:52 +03:00
|
|
|
You locate an element using a combination of a position and an offset.
|
|
|
|
|
|
|
|
The position is a co-ordinate between (0, 0) and (1, 1) which determines where,
|
2017-09-21 19:07:32 +03:00
|
|
|
relative to the screen width and height, the element is located.
|
2015-01-01 19:36:52 +03:00
|
|
|
For example, an element with a position of (0.5, 0.5) will be in the center of the screen.
|
|
|
|
|
|
|
|
The offset applies a pixel offset to the position.\\
|
2017-09-21 19:07:32 +03:00
|
|
|
For example, an element with a position of (0, 0) and an offset of (10, 10) will be at the screen
|
2015-01-01 19:36:52 +03:00
|
|
|
co-ordinates (0 * width + 10, 0 * height + 10).
|
|
|
|
|
|
|
|
Please note that offset scales to DPI and a user defined factor.
|
|
|
|
|
2015-02-22 13:28:37 +03:00
|
|
|
## Text Elements
|
2015-01-01 19:36:52 +03:00
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
A text element is the simplest type of HUD element.\\
|
|
|
|
Here is the earlier example, but with comments to explain each part:
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
local idx = player:hud_add({
|
2017-08-26 21:01:51 +03:00
|
|
|
hud_elem_type = "text", -- This is a text element
|
|
|
|
position = {x = 1, y = 0},
|
|
|
|
offset = {x=-100, y = 20},
|
2017-09-21 19:07:32 +03:00
|
|
|
scale = {x = 100, y = 100}, -- Maximum size of text, text outside these bounds is cropped
|
2017-08-26 21:01:51 +03:00
|
|
|
text = "My Text" -- The actual text shown
|
2015-01-01 19:36:52 +03:00
|
|
|
})
|
|
|
|
{% endhighlight %}
|
|
|
|
|
|
|
|
### Colors
|
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
Use the `number` attribute to apply colors to the text.
|
2015-01-01 19:36:52 +03:00
|
|
|
Colors are in [Hexadecimal form](http://www.colorpicker.com/).
|
2017-09-21 19:07:32 +03:00
|
|
|
For example:
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
{% highlight lua %}
|
|
|
|
local idx = player:hud_add({
|
2017-08-26 21:01:51 +03:00
|
|
|
hud_elem_type = "text",
|
|
|
|
position = {x = 1, y = 0},
|
|
|
|
offset = {x=-100, y = 20},
|
|
|
|
scale = {x = 100, y = 100},
|
|
|
|
text = "My Text",
|
|
|
|
number = 0xFF0000 -- Red
|
2015-01-01 19:36:52 +03:00
|
|
|
})
|
|
|
|
{% endhighlight %}
|
|
|
|
|
2015-02-22 13:28:37 +03:00
|
|
|
## Image Elements
|
2015-01-01 19:36:52 +03:00
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
Image elements display an image on the HUD.
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
The X co-ordinate of the `scale` attribute is the scale of the image, with 1 being the original texture size.
|
2017-09-21 19:07:32 +03:00
|
|
|
Negative values represent the percentage of the screen the image should use. For example, x=-100 means 100% (width).
|
2015-01-01 19:36:52 +03:00
|
|
|
|
|
|
|
Use `text` to specify the name of the texture.
|
|
|
|
|
2015-02-22 13:28:37 +03:00
|
|
|
## Other Elements
|
2015-01-01 19:36:52 +03:00
|
|
|
|
2017-09-21 19:07:32 +03:00
|
|
|
Read [lua_api.txt]({{ page.root }}lua_api.html#hud-element-types) for a complete list of HUD elements.
|