Skip to content

Commit 9649e47

Browse files
red-001nerzhul
authored andcommittedJan 20, 2018
[CSM] Add basic HUD manipulation. (#6067)
* [CSM] Add basic HUD manipulation. Workaround for on_connect not working right now.
1 parent d45e5da commit 9649e47

21 files changed

+1400
-1108
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ locale/
8686
*.gch
8787
cmake-build-debug/
8888
cmake-build-release/
89+
cmake_config.h
90+
cmake_config_githash.h
8991

9092
## Android build files
9193
build/android/src/main/assets

‎build/android/jni/Android.mk

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ LOCAL_SRC_FILES := \
270270
jni/src/settings.cpp \
271271
jni/src/wieldmesh.cpp \
272272
jni/src/client/clientlauncher.cpp \
273+
jni/src/client/hud.cpp \
273274
jni/src/client/inputhandler.cpp \
274275
jni/src/client/renderingengine.cpp \
275276
jni/src/client/tile.cpp \

‎clientmods/preview/init.lua

+20-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ dofile("preview:example.lua")
77
core.register_on_shutdown(function()
88
print("[PREVIEW] shutdown client")
99
end)
10-
10+
local id = 0
1111
core.register_on_connect(function()
1212
print("[PREVIEW] Player connection completed")
1313
local server_info = core.get_server_info()
1414
print("Server version: " .. server_info.protocol_version)
1515
print("Server ip: " .. server_info.ip)
1616
print("Server address: " .. server_info.address)
1717
print("Server port: " .. server_info.port)
18-
1918
mod_channel = core.mod_channel_join("experimental_preview")
2019

2120
core.after(4, function()
@@ -25,6 +24,19 @@ core.register_on_connect(function()
2524
end)
2625
end)
2726

27+
core.after(1, function()
28+
id = core.localplayer:hud_add({
29+
hud_elem_type = "text",
30+
name = "example",
31+
number = 0xff0000,
32+
position = {x=0, y=1},
33+
offset = {x=8, y=-8},
34+
text = "You are using the preview mod",
35+
scale = {x=200, y=60},
36+
alignment = {x=1, y=-1},
37+
})
38+
end)
39+
2840
core.register_on_modchannel_message(function(channel, sender, message)
2941
print("[PREVIEW][modchannels] Received message `" .. message .. "` on channel `"
3042
.. channel .. "` from sender `" .. sender .. "`")
@@ -184,3 +196,9 @@ core.register_chatcommand("privs", {
184196
return true, core.privs_to_string(minetest.get_privilege_list())
185197
end,
186198
})
199+
200+
core.register_chatcommand("text", {
201+
func = function(param)
202+
return core.localplayer:hud_change(id, "text", param)
203+
end,
204+
})

‎doc/client_lua_api.txt

+112
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,17 @@ Methods:
10251025
* returns last look vertical angle
10261026
* `get_key_pressed()`:
10271027
* returns last key typed by the player
1028+
* `hud_add(definition)`
1029+
* add a HUD element described by HUD def, returns ID number on success and `nil` on failure.
1030+
* See [`HUD definition`](#hud-definition-hud_add-hud_get)
1031+
* `hud_get(id)`
1032+
* returns the [`definition`](#hud-definition-hud_add-hud_get) of the HUD with that ID number or `nil`, if non-existent.
1033+
* `hud_remove(id)`
1034+
* remove the HUD element of the specified id, returns `true` on success
1035+
* `hud_change(id, stat, value)`
1036+
* change a value of a previously added HUD element
1037+
* element `stat` values: `position`, `name`, `scale`, `text`, `number`, `item`, `dir`
1038+
* Returns `true` on success, otherwise returns `nil`
10281039

10291040
### Settings
10301041
An interface to read config files in the format of `minetest.conf`.
@@ -1163,6 +1174,30 @@ Can be obtained via `minetest.get_meta(pos)`.
11631174
}
11641175
```
11651176

1177+
### HUD Definition (`hud_add`, `hud_get`)
1178+
```lua
1179+
{
1180+
hud_elem_type = "image", -- see HUD element types, default "text"
1181+
-- ^ type of HUD element, can be either of "image", "text", "statbar", or "inventory"
1182+
position = {x=0.5, y=0.5},
1183+
-- ^ Left corner position of element, default `{x=0,y=0}`.
1184+
name = "<name>", -- default ""
1185+
scale = {x=2, y=2}, -- default {x=0,y=0}
1186+
text = "<text>", -- default ""
1187+
number = 2, -- default 0
1188+
item = 3, -- default 0
1189+
-- ^ Selected item in inventory. 0 for no item selected.
1190+
direction = 0, -- default 0
1191+
-- ^ Direction: 0: left-right, 1: right-left, 2: top-bottom, 3: bottom-top
1192+
alignment = {x=0, y=0}, -- default {x=0, y=0}
1193+
-- ^ See "HUD Element Types"
1194+
offset = {x=0, y=0}, -- default {x=0, y=0}
1195+
-- ^ See "HUD Element Types"
1196+
size = { x=100, y=100 }, -- default {x=0, y=0}
1197+
-- ^ Size of element in pixels
1198+
}
1199+
```
1200+
11661201
Escape sequences
11671202
----------------
11681203
Most text can contain escape sequences, that can for example color the text.
@@ -1206,3 +1241,80 @@ value must (always) be two hexadecimal digits.
12061241
`Color`
12071242
-------------
12081243
`{a = alpha, r = red, g = green, b = blue}` defines an ARGB8 color.
1244+
1245+
HUD element types
1246+
-----------------
1247+
The position field is used for all element types.
1248+
1249+
To account for differing resolutions, the position coordinates are the percentage
1250+
of the screen, ranging in value from `0` to `1`.
1251+
1252+
The name field is not yet used, but should contain a description of what the
1253+
HUD element represents. The direction field is the direction in which something
1254+
is drawn.
1255+
1256+
`0` draws from left to right, `1` draws from right to left, `2` draws from
1257+
top to bottom, and `3` draws from bottom to top.
1258+
1259+
The `alignment` field specifies how the item will be aligned. It ranges from `-1` to `1`,
1260+
with `0` being the center, `-1` is moved to the left/up, and `1` is to the right/down.
1261+
Fractional values can be used.
1262+
1263+
The `offset` field specifies a pixel offset from the position. Contrary to position,
1264+
the offset is not scaled to screen size. This allows for some precisely-positioned
1265+
items in the HUD.
1266+
1267+
**Note**: `offset` _will_ adapt to screen DPI as well as user defined scaling factor!
1268+
1269+
Below are the specific uses for fields in each type; fields not listed for that type are ignored.
1270+
1271+
**Note**: Future revisions to the HUD API may be incompatible; the HUD API is still
1272+
in the experimental stages.
1273+
1274+
### `image`
1275+
Displays an image on the HUD.
1276+
1277+
* `scale`: The scale of the image, with 1 being the original texture size.
1278+
Only the X coordinate scale is used (positive values).
1279+
Negative values represent that percentage of the screen it
1280+
should take; e.g. `x=-100` means 100% (width).
1281+
* `text`: The name of the texture that is displayed.
1282+
* `alignment`: The alignment of the image.
1283+
* `offset`: offset in pixels from position.
1284+
1285+
### `text`
1286+
Displays text on the HUD.
1287+
1288+
* `scale`: Defines the bounding rectangle of the text.
1289+
A value such as `{x=100, y=100}` should work.
1290+
* `text`: The text to be displayed in the HUD element.
1291+
* `number`: An integer containing the RGB value of the color used to draw the text.
1292+
Specify `0xFFFFFF` for white text, `0xFF0000` for red, and so on.
1293+
* `alignment`: The alignment of the text.
1294+
* `offset`: offset in pixels from position.
1295+
1296+
### `statbar`
1297+
Displays a horizontal bar made up of half-images.
1298+
1299+
* `text`: The name of the texture that is used.
1300+
* `number`: The number of half-textures that are displayed.
1301+
If odd, will end with a vertically center-split texture.
1302+
* `direction`
1303+
* `offset`: offset in pixels from position.
1304+
* `size`: If used, will force full-image size to this value (override texture pack image size)
1305+
1306+
### `inventory`
1307+
* `text`: The name of the inventory list to be displayed.
1308+
* `number`: Number of items in the inventory to be displayed.
1309+
* `item`: Position of item that is selected.
1310+
* `direction`
1311+
* `offset`: offset in pixels from position.
1312+
1313+
### `waypoint`
1314+
Displays distance to selected world position.
1315+
1316+
* `name`: The name of the waypoint.
1317+
* `text`: Distance suffix. Can be blank.
1318+
* `number:` An integer containing the RGB value of the color used to draw the text.
1319+
* `world_pos`: World position of the waypoint.
1320+

‎src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ set(common_SRCS
448448
version.cpp
449449
voxel.cpp
450450
voxelalgorithms.cpp
451+
hud.cpp
451452
${common_network_SRCS}
452453
${JTHREAD_SRCS}
453454
${common_SCRIPT_SRCS}

‎src/client.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030
#include "gamedef.h"
3131
#include "inventorymanager.h"
3232
#include "localplayer.h"
33-
#include "hud.h"
33+
#include "client/hud.h"
3434
#include "particles.h"
3535
#include "mapnode.h"
3636
#include "tileanimation.h"
@@ -554,6 +554,10 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
554554
// And relations to objects
555555
std::unordered_map<int, u16> m_sounds_to_objects;
556556

557+
// HUD
558+
// Mapping from server hud ids to internal hud ids
559+
std::unordered_map<u32, u32> m_hud_server_to_client;
560+
557561
// Privileges
558562
std::unordered_set<std::string> m_privileges;
559563

‎src/client/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ set(client_SRCS
1313
${CMAKE_CURRENT_SOURCE_DIR}/inputhandler.cpp
1414
${CMAKE_CURRENT_SOURCE_DIR}/tile.cpp
1515
${CMAKE_CURRENT_SOURCE_DIR}/joystick_controller.cpp
16+
${CMAKE_CURRENT_SOURCE_DIR}/hud.cpp
1617
PARENT_SCOPE
1718
)

0 commit comments

Comments
 (0)
Please sign in to comment.