Skip to content

Commit af2e6a6

Browse files
authoredApr 11, 2020
Improve waypoints and add image variant (#9480)
1 parent f780bae commit af2e6a6

File tree

7 files changed

+118
-47
lines changed

7 files changed

+118
-47
lines changed
 

‎doc/client_lua_api.txt

+24-1
Original file line numberDiff line numberDiff line change
@@ -1416,12 +1416,35 @@ Displays a horizontal bar made up of half-images.
14161416
* `offset`: offset in pixels from position.
14171417

14181418
### `waypoint`
1419+
14191420
Displays distance to selected world position.
14201421

14211422
* `name`: The name of the waypoint.
14221423
* `text`: Distance suffix. Can be blank.
1423-
* `number:` An integer containing the RGB value of the color used to draw the text.
1424+
* `precision`: Waypoint precision, integer >= 0. Defaults to 10.
1425+
If set to 0, distance is not shown. Shown value is `floor(distance*precision)/precision`.
1426+
When the precision is an integer multiple of 10, there will be `log_10(precision)` digits after the decimal point.
1427+
`precision = 1000`, for example, will show 3 decimal places (eg: `0.999`).
1428+
`precision = 2` will show multiples of `0.5`; precision = 5 will show multiples of `0.2` and so on:
1429+
`precision = n` will show multiples of `1/n`
1430+
* `number:` An integer containing the RGB value of the color used to draw the
1431+
text.
14241432
* `world_pos`: World position of the waypoint.
1433+
* `offset`: offset in pixels from position.
1434+
* `alignment`: The alignment of the waypoint.
1435+
1436+
### `image_waypoint`
1437+
1438+
Same as `image`, but does not accept a `position`; the position is instead determined by `world_pos`, the world position of the waypoint.
1439+
1440+
* `scale`: The scale of the image, with 1 being the original texture size.
1441+
Only the X coordinate scale is used (positive values).
1442+
Negative values represent that percentage of the screen it
1443+
should take; e.g. `x=-100` means 100% (width).
1444+
* `text`: The name of the texture that is displayed.
1445+
* `alignment`: The alignment of the image.
1446+
* `world_pos`: World position of the waypoint.
1447+
* `offset`: offset in pixels from position.
14251448

14261449
### Particle definition (`add_particle`)
14271450

‎doc/lua_api.txt

+20
Original file line numberDiff line numberDiff line change
@@ -1364,10 +1364,30 @@ Displays distance to selected world position.
13641364

13651365
* `name`: The name of the waypoint.
13661366
* `text`: Distance suffix. Can be blank.
1367+
* `precision`: Waypoint precision, integer >= 0. Defaults to 10.
1368+
If set to 0, distance is not shown. Shown value is `floor(distance*precision)/precision`.
1369+
When the precision is an integer multiple of 10, there will be `log_10(precision)` digits after the decimal point.
1370+
`precision = 1000`, for example, will show 3 decimal places (eg: `0.999`).
1371+
`precision = 2` will show multiples of `0.5`; precision = 5 will show multiples of `0.2` and so on:
1372+
`precision = n` will show multiples of `1/n`
13671373
* `number:` An integer containing the RGB value of the color used to draw the
13681374
text.
13691375
* `world_pos`: World position of the waypoint.
1376+
* `offset`: offset in pixels from position.
1377+
* `alignment`: The alignment of the waypoint.
1378+
1379+
### `image_waypoint`
1380+
1381+
Same as `image`, but does not accept a `position`; the position is instead determined by `world_pos`, the world position of the waypoint.
13701382

1383+
* `scale`: The scale of the image, with 1 being the original texture size.
1384+
Only the X coordinate scale is used (positive values).
1385+
Negative values represent that percentage of the screen it
1386+
should take; e.g. `x=-100` means 100% (width).
1387+
* `text`: The name of the texture that is displayed.
1388+
* `alignment`: The alignment of the image.
1389+
* `world_pos`: World position of the waypoint.
1390+
* `offset`: offset in pixels from position.
13711391

13721392

13731393

‎src/client/hud.cpp

+66-45
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
272272
}
273273
}
274274

275+
// Calculates screen position of waypoint. Returns true if waypoint is visible (in front of the player), else false.
276+
bool Hud::calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *pos)
277+
{
278+
v3f w_pos = e->world_pos * BS;
279+
scene::ICameraSceneNode* camera =
280+
RenderingEngine::get_scene_manager()->getActiveCamera();
281+
w_pos -= intToFloat(camera_offset, BS);
282+
core::matrix4 trans = camera->getProjectionMatrix();
283+
trans *= camera->getViewMatrix();
284+
f32 transformed_pos[4] = { w_pos.X, w_pos.Y, w_pos.Z, 1.0f };
285+
trans.multiplyWith1x4Matrix(transformed_pos);
286+
if (transformed_pos[3] < 0)
287+
return false;
288+
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
289+
core::reciprocal(transformed_pos[3]);
290+
pos->X = m_screensize.X * (0.5 * transformed_pos[0] * zDiv + 0.5);
291+
pos->Y = m_screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5);
292+
return true;
293+
}
275294

276295
void Hud::drawLuaElements(const v3s16 &camera_offset)
277296
{
@@ -299,28 +318,6 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
299318
v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
300319
floor(e->pos.Y * (float) m_screensize.Y + 0.5));
301320
switch (e->type) {
302-
case HUD_ELEM_IMAGE: {
303-
video::ITexture *texture = tsrc->getTexture(e->text);
304-
if (!texture)
305-
continue;
306-
307-
const video::SColor color(255, 255, 255, 255);
308-
const video::SColor colors[] = {color, color, color, color};
309-
core::dimension2di imgsize(texture->getOriginalSize());
310-
v2s32 dstsize(imgsize.Width * e->scale.X,
311-
imgsize.Height * e->scale.Y);
312-
if (e->scale.X < 0)
313-
dstsize.X = m_screensize.X * (e->scale.X * -0.01);
314-
if (e->scale.Y < 0)
315-
dstsize.Y = m_screensize.Y * (e->scale.Y * -0.01);
316-
v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
317-
(e->align.Y - 1.0) * dstsize.Y / 2);
318-
core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
319-
rect += pos + offset + v2s32(e->offset.X, e->offset.Y);
320-
draw2DImageFilterScaled(driver, texture, rect,
321-
core::rect<s32>(core::position2d<s32>(0,0), imgsize),
322-
NULL, colors, true);
323-
break; }
324321
case HUD_ELEM_TEXT: {
325322
video::SColor color(255, (e->number >> 16) & 0xFF,
326323
(e->number >> 8) & 0xFF,
@@ -343,34 +340,58 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
343340
inv, e->item, e->dir);
344341
break; }
345342
case HUD_ELEM_WAYPOINT: {
346-
v3f p_pos = player->getPosition() / BS;
347-
v3f w_pos = e->world_pos * BS;
348-
float distance = std::floor(10 * p_pos.getDistanceFrom(e->world_pos)) /
349-
10.0f;
350-
scene::ICameraSceneNode* camera =
351-
RenderingEngine::get_scene_manager()->getActiveCamera();
352-
w_pos -= intToFloat(camera_offset, BS);
353-
core::matrix4 trans = camera->getProjectionMatrix();
354-
trans *= camera->getViewMatrix();
355-
f32 transformed_pos[4] = { w_pos.X, w_pos.Y, w_pos.Z, 1.0f };
356-
trans.multiplyWith1x4Matrix(transformed_pos);
357-
if (transformed_pos[3] < 0)
343+
if (!calculateScreenPos(camera_offset, e, &pos))
358344
break;
359-
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
360-
core::reciprocal(transformed_pos[3]);
361-
pos.X = m_screensize.X * (0.5 * transformed_pos[0] * zDiv + 0.5);
362-
pos.Y = m_screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5);
345+
v3f p_pos = player->getPosition() / BS;
346+
pos += v2s32(e->offset.X, e->offset.Y);
363347
video::SColor color(255, (e->number >> 16) & 0xFF,
364348
(e->number >> 8) & 0xFF,
365349
(e->number >> 0) & 0xFF);
366-
core::rect<s32> size(0, 0, 200, 2 * text_height);
367350
std::wstring text = unescape_translate(utf8_to_wide(e->name));
368-
font->draw(text.c_str(), size + pos, color);
369-
std::ostringstream os;
370-
os << distance << e->text;
371-
text = unescape_translate(utf8_to_wide(os.str()));
372-
pos.Y += text_height;
373-
font->draw(text.c_str(), size + pos, color);
351+
const std::string &unit = e->text;
352+
// waypoints reuse the item field to store precision, item = precision + 1
353+
u32 item = e->item;
354+
float precision = (item == 0) ? 10.0f : (item - 1.f);
355+
bool draw_precision = precision > 0;
356+
357+
core::rect<s32> bounds(0, 0, font->getDimension(text.c_str()).Width, (draw_precision ? 2:1) * text_height);
358+
pos.Y += (e->align.Y - 1.0) * bounds.getHeight() / 2;
359+
bounds += pos;
360+
font->draw(text.c_str(), bounds + v2s32((e->align.X - 1.0) * bounds.getWidth() / 2, 0), color);
361+
if (draw_precision) {
362+
std::ostringstream os;
363+
float distance = std::floor(precision * p_pos.getDistanceFrom(e->world_pos)) / precision;
364+
os << distance << unit;
365+
text = unescape_translate(utf8_to_wide(os.str()));
366+
bounds.LowerRightCorner.X = bounds.UpperLeftCorner.X + font->getDimension(text.c_str()).Width;
367+
font->draw(text.c_str(), bounds + v2s32((e->align.X - 1.0f) * bounds.getWidth() / 2, text_height), color);
368+
}
369+
break; }
370+
case HUD_ELEM_IMAGE_WAYPOINT: {
371+
if (!calculateScreenPos(camera_offset, e, &pos))
372+
break;
373+
}
374+
case HUD_ELEM_IMAGE: {
375+
video::ITexture *texture = tsrc->getTexture(e->text);
376+
if (!texture)
377+
continue;
378+
379+
const video::SColor color(255, 255, 255, 255);
380+
const video::SColor colors[] = {color, color, color, color};
381+
core::dimension2di imgsize(texture->getOriginalSize());
382+
v2s32 dstsize(imgsize.Width * e->scale.X,
383+
imgsize.Height * e->scale.Y);
384+
if (e->scale.X < 0)
385+
dstsize.X = m_screensize.X * (e->scale.X * -0.01);
386+
if (e->scale.Y < 0)
387+
dstsize.Y = m_screensize.Y * (e->scale.Y * -0.01);
388+
v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
389+
(e->align.Y - 1.0) * dstsize.Y / 2);
390+
core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
391+
rect += pos + offset + v2s32(e->offset.X, e->offset.Y);
392+
draw2DImageFilterScaled(driver, texture, rect,
393+
core::rect<s32>(core::position2d<s32>(0,0), imgsize),
394+
NULL, colors, true);
374395
break; }
375396
default:
376397
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type <<

‎src/client/hud.h

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Hud
8181
void drawLuaElements(const v3s16 &camera_offset);
8282

8383
private:
84+
bool calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *pos);
8485
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, const std::string &texture,
8586
s32 count, v2s32 offset, v2s32 size = v2s32());
8687

‎src/hud.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const struct EnumString es_HudElementType[] =
2727
{HUD_ELEM_STATBAR, "statbar"},
2828
{HUD_ELEM_INVENTORY, "inventory"},
2929
{HUD_ELEM_WAYPOINT, "waypoint"},
30+
{HUD_ELEM_IMAGE_WAYPOINT, "image_waypoint"},
3031
{0, NULL},
3132
};
3233

‎src/hud.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ enum HudElementType {
6161
HUD_ELEM_STATBAR = 2,
6262
HUD_ELEM_INVENTORY = 3,
6363
HUD_ELEM_WAYPOINT = 4,
64+
HUD_ELEM_IMAGE_WAYPOINT = 5
6465
};
6566

6667
enum HudElementStat {

‎src/script/common/c_content.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,11 @@ void read_hud_element(lua_State *L, HudElement *elem)
18501850
elem->name = getstringfield_default(L, 2, "name", "");
18511851
elem->text = getstringfield_default(L, 2, "text", "");
18521852
elem->number = getintfield_default(L, 2, "number", 0);
1853-
elem->item = getintfield_default(L, 2, "item", 0);
1853+
if (elem->type == HUD_ELEM_WAYPOINT)
1854+
// waypoints reuse the item field to store precision, item = precision + 1
1855+
elem->item = getintfield_default(L, 2, "precision", -1) + 1;
1856+
else
1857+
elem->item = getintfield_default(L, 2, "item", 0);
18541858
elem->dir = getintfield_default(L, 2, "direction", 0);
18551859
elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX,
18561860
getintfield_default(L, 2, "z_index", 0)));

0 commit comments

Comments
 (0)
Please sign in to comment.