From 0ca8c018b080c8761c56b987baeb88678ec5c2c6 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 18 Jan 2024 14:18:45 +0100 Subject: [PATCH 1/6] Add crosshair Lua HUD element and replace hardcoded crosshair --- builtin/game/features.lua | 1 + builtin/game/hud.lua | 13 ++ doc/lua_api.md | 27 ++- games/devtest/mods/testhud/init.lua | 198 +++++++++++++++++- .../testhud/textures/testhud_crosshair.png | Bin 0 -> 279 bytes .../textures/testhud_object_crosshair.png | Bin 0 -> 398 bytes src/client/game.cpp | 8 +- src/client/hud.cpp | 117 ++++++----- src/client/hud.h | 6 +- src/client/render/core.cpp | 4 +- src/client/render/core.h | 2 +- src/client/render/pipeline.h | 2 +- src/client/render/plain.cpp | 7 +- src/client/renderingengine.cpp | 4 +- src/client/renderingengine.h | 2 +- src/hud.cpp | 1 + src/hud.h | 3 +- 17 files changed, 324 insertions(+), 71 deletions(-) create mode 100644 games/devtest/mods/testhud/textures/testhud_crosshair.png create mode 100644 games/devtest/mods/testhud/textures/testhud_object_crosshair.png diff --git a/builtin/game/features.lua b/builtin/game/features.lua index 286c3f146..621cae29f 100644 --- a/builtin/game/features.lua +++ b/builtin/game/features.lua @@ -40,6 +40,7 @@ core.features = { lsystem_decoration_type = true, item_meta_range = true, node_interaction_actor = true, + crosshair_hud_element = true, } function core.has_feature(arg) diff --git a/builtin/game/hud.lua b/builtin/game/hud.lua index 4d531215d..8fe2adfff 100644 --- a/builtin/game/hud.lua +++ b/builtin/game/hud.lua @@ -259,3 +259,16 @@ register_builtin_hud_element("minimap", { core.get_player_information(player:get_player_name()).protocol_version >= 44 end, }) + +--- Crosshair + +register_builtin_hud_element("crosshair", { + elem_def = { + type = "crosshair", + position = {x = 0.5, y = 0.5}, + scale = {x = 1, y = 1}, + }, + show_elem = function(player, flags) + return flags.crosshair + end, +}) diff --git a/doc/lua_api.md b/doc/lua_api.md index 796a0f39e..ff8129edd 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -1812,6 +1812,26 @@ Displays a minimap on the HUD. * `alignment`: The alignment of the minimap. * `offset`: offset in pixels from position. +### `crosshair` + +Displays a crosshair on the HUD. + +* `scale`: The scale of the crosshair. + For image crosshairs it works the same as the scale of `image` HUD elements. + For line crosshairs the length is multiplied by the +* `alignment`: The alignment of the crosshair. +* `offset`: offset in pixels from position. +* `style`: The style of the crosshair: + * 0 - The default crosshair which when pointing at a node or nothing shows two + perpendicular lines of size 20, and when pointing at an object shows two + diagonal lines of size 16. The lines get replaced by images when textures + named `"crosshair.png"` and for objects `"object_crosshair.png"` exist. + * 1 - A crosshair that shows an image. + When pointing at nodes or nothing it uses as texture name `text`. + When pointing at objects it uses as texture name `text2`. +* `text`: Only relevent for `style` = 1. +* `text2`: Only relevent for `style` = 1. + Representations of simple things ================================ @@ -5435,6 +5455,9 @@ Utilities -- Allow passing an optional "actor" ObjectRef to the following functions: -- minetest.place_node, minetest.dig_node, minetest.punch_node (5.9.0) node_interaction_actor = true, + -- HUD elements of type crosshair exist and + -- the predefined crosshair is a Lua HUD elements. + crosshair_hud_element = true, } ``` @@ -7041,7 +7064,7 @@ Misc. (regardless of online status) * `minetest.hud_replace_builtin(name, hud_definition)` * Replaces definition of a builtin hud element - * `name`: `"breath"`, `"health"` or `"minimap"` + * `name`: `"breath"`, `"health"`, `"minimap"` or `"crosshair"` * `hud_definition`: definition to replace builtin definition * `minetest.parse_relative_number(arg, relative_to)`: returns number or nil * Helper function for chat commands. @@ -10528,7 +10551,7 @@ Used by `ObjectRef:hud_add`. Returned by `ObjectRef:hud_get`. { type = "image", -- Type of element, can be "image", "text", "statbar", "inventory", - -- "waypoint", "image_waypoint", "compass" or "minimap" + -- "waypoint", "image_waypoint", "compass", "minimap" or "crosshair" -- If undefined "text" will be used. hud_elem_type = "image", diff --git a/games/devtest/mods/testhud/init.lua b/games/devtest/mods/testhud/init.lua index 5fdae3d38..51ab6666a 100644 --- a/games/devtest/mods/testhud/init.lua +++ b/games/devtest/mods/testhud/init.lua @@ -206,9 +206,180 @@ minetest.register_chatcommand("zoomfov", { end, }) +-- Images + +local hud_image_defs = { + { + type = "image", + position = {x=0.5, y=0.5}, + scale = {x = 10, y = 10}, + text = "crosshair.png", + alignment = {x=0, y=-0}, + offset = {x=0, y=0}, + }, + { + type = "image", + position = {x=0.3, y=0.3}, + scale = {x = 10, y = 10}, + text = "default_cobble.png", + alignment = {x=0, y=-1}, + offset = {x=0, y=0}, + }, + { + type = "image", + position = {x=0.7, y=0.3}, + scale = {x = 10, y = 10}, + text = "default_lava.png", + alignment = {x=0, y=1}, + offset = {x=0, y=0}, + }, + { + type = "image", + position = {x=0.3, y=0.7}, + scale = {x = 10, y = 20}, + text = "default_gravel.png", + alignment = {x=0, y=0}, + offset = {x=0, y=0}, + }, + { + type = "image", + position = {x=0.7, y=0.7}, + scale = {x = 10, y = 10}, + text = "default_tree_top.png", + alignment = {x=0, y=0}, + offset = {x=160, y=0}, + }, +} + +local player_hud_images= {} +minetest.register_chatcommand("hudimages", { + description = "Shows some test Lua HUD elements of type image. (add: Adds elements (default). remove: Removes elements)", + params = "[ add | remove ]", + func = function(name, params) + local player = minetest.get_player_by_name(name) + if not player then + return false, "No player." + end + + local id_table = player_hud_images[name] + if not id_table then + id_table = {} + player_hud_images[name] = id_table + end + + if params == "remove" then + for _, id in ipairs(id_table) do + player:hud_remove(id) + end + return true, "Images removed." + end + + -- params == "add" or default + for _, def in ipairs(hud_image_defs) do + table.insert(id_table, player:hud_add(def)) + end + return true, #hud_image_defs .." images added." + end +}) + +-- Crosshairs + +local hud_crosshair_defs = { + { + type = "crosshair", + position = {x=0.45, y=0.5}, + scale = {x = 1, y = 1}, + alignment = {x=0, y=-1}, + offset = {x=0, y=0}, + }, + { + type = "crosshair", + position = {x=0.45, y=0.5}, + scale = {x = 1, y = 1}, + alignment = {x=0, y=1}, + offset = {x=0, y=0}, + }, + { + type = "crosshair", + position = {x=0.5, y=0.4}, + scale = {x = 1, y = 2}, + alignment = {x=0, y=1}, + offset = {x=100, y=0}, + }, + { + type = "crosshair", + position = {x=0.5, y=0.7}, + scale = {x = 5, y = 2}, + alignment = {x=0, y=-1}, + offset = {x=0, y=100}, + }, + { + type = "crosshair", + position = {x=0.5, y=0.65}, + scale = {x = 2, y = 2}, + alignment = {x=0, y=-1}, + offset = {x=0, y=0}, + }, + { + type = "crosshair", + position = {x=0.3, y=0.5}, + scale = {x = 1, y = 1}, + alignment = {x=0, y=0}, + offset = {x=0, y=0}, + style = 1, + text = "testhud_crosshair.png", + text2 = "testhud_object_crosshair.png", + }, + { + type = "crosshair", + position = {x=0.3, y=0.5}, + scale = {x = 2, y = 1}, + alignment = {x=0, y=2}, + offset = {x=0, y=0}, + style = 1, + text = "testhud_crosshair.png", + text2 = "testhud_object_crosshair.png", + }, +} + +local player_hud_crosshairs= {} +minetest.register_chatcommand("hudcrosshairs", { + description = "Shows some test Lua HUD elements of type crosshair. (add: Adds elements (default). remove: Removes elements)", + params = "[ add | remove ]", + func = function(name, params) + local player = minetest.get_player_by_name(name) + if not player then + return false, "No player." + end + + local id_table = player_hud_crosshairs[name] + if not id_table then + id_table = {} + player_hud_crosshairs[name] = id_table + end + + if params == "remove" then + for _, id in ipairs(id_table) do + player:hud_remove(id) + end + return true, "Crosshairs removed." + end + + -- params == "add" or default + for _, def in ipairs(hud_crosshair_defs) do + table.insert(id_table, player:hud_add(def)) + end + return true, #hud_crosshair_defs .." Crosshairs added." + end +}) + + minetest.register_on_leaveplayer(function(player) - player_font_huds[player:get_player_name()] = nil - player_waypoints[player:get_player_name()] = nil + local playername = player:get_player_name() + player_font_huds[playername] = nil + player_waypoints[playername] = nil + player_hud_images[playername] = nil + player_hud_crosshairs[playername] = nil end) minetest.register_chatcommand("hudprint", { @@ -230,3 +401,26 @@ minetest.register_chatcommand("hudprint", { return true, s end }) + +local hud_flags = {"hotbar", "healthbar", "crosshair", "wielditem", "breathbar", + "minimap", "minimap_radar", "basic_debug", "chat"} + +minetest.register_chatcommand("hudtoggleflag", { + description = "Toggles a hud flag.", + params = "[ ".. table.concat(hud_flags, " | ") .." ]", + func = function(name, params) + local player = minetest.get_player_by_name(name) + if not player then + return false, "No player." + end + + local flags = player:hud_get_flags() + if flags[params] == nil then + return false, "Unknown hud flag." + end + + flags[params] = not flags[params] + player:hud_set_flags(flags) + return true, "Flag \"".. params .."\" set to ".. tostring(flags[params]) .. "." + end +}) diff --git a/games/devtest/mods/testhud/textures/testhud_crosshair.png b/games/devtest/mods/testhud/textures/testhud_crosshair.png new file mode 100644 index 0000000000000000000000000000000000000000..f8a208c8d0de9ec7e083a4b621731c988fa3a66e GIT binary patch literal 279 zcmV+y0qFjTP)h?=RTX0fx55QHEESsO&nRhNHJ2tp8o5M&z=HLW)_%~1B)cj~im^@Kys dqZ-NpIRU;TFdoZ$V~YR)002ovPDHLkV1j5fZ-M{- literal 0 HcmV?d00001 diff --git a/games/devtest/mods/testhud/textures/testhud_object_crosshair.png b/games/devtest/mods/testhud/textures/testhud_object_crosshair.png new file mode 100644 index 0000000000000000000000000000000000000000..3ee9a995091c7ab063c623a7a500f4f21168f7f2 GIT binary patch literal 398 zcmV;90df9`P)AvrSw-n7K9Z_Ly5W_I&ZQFtk(_GRK z4MqYYI*cSlG#Fc8cU{K{@e4z`*%QE7c9Pn|BKEFQEH{jZJzGrBlADIOU~CZdoooij zj4QCu3_gKz=?qQ)KG}&A#z$8W%s7t61XqgXfVrc?grQIJ^)i>&b$xkGJb?+Bd&jq9 z!aUDj=R}A@Pkw+qzA1@)E)ieR%6ikm*F*R?nhk^trfHhb$Oa-+X(J9}uEKY9vVnjB zf!JCQ!48prAr8dXmM9QKBTN|&bFqTC>cTW1pxQxD4LYcnDO3|1>RBNM48*HdbO;wr sc8{UHsZ#y(TYJ*G3B+GGiWG6n4kPhXzgz9m_game_ui->m_flags.show_hud && (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE) && (this->camera->getCameraMode() == CAMERA_MODE_FIRST)); - bool draw_crosshair = ( - (player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) && - (this->camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT)); + bool draw_crosshairs = (this->camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT); if (g_touchscreengui && isTouchCrosshairDisabled()) draw_crosshair = false; this->m_rendering_engine->draw_scene(sky_color, this->m_game_ui->m_flags.show_hud, - draw_wield_tool, draw_crosshair); + draw_wield_tool, draw_crosshairs); /* Profiler graph diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 0c1b3e81f..a4f91d8af 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -340,7 +340,7 @@ bool Hud::calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *p return true; } -void Hud::drawLuaElements(const v3s16 &camera_offset) +void Hud::drawLuaElements(const v3s16 &camera_offset, bool draw_crosshairs) { const u32 text_height = g_fontengine->getTextHeight(); gui::IGUIFont *const font = g_fontengine->getFont(); @@ -465,22 +465,7 @@ void Hud::drawLuaElements(const v3s16 &camera_offset) continue; const video::SColor color(255, 255, 255, 255); - const video::SColor colors[] = {color, color, color, color}; - core::dimension2di imgsize(texture->getOriginalSize()); - v2s32 dstsize(imgsize.Width * e->scale.X * m_scale_factor, - imgsize.Height * e->scale.Y * m_scale_factor); - if (e->scale.X < 0) - dstsize.X = m_screensize.X * (e->scale.X * -0.01); - if (e->scale.Y < 0) - dstsize.Y = m_screensize.Y * (e->scale.Y * -0.01); - v2s32 offset((e->align.X - 1.0) * dstsize.X / 2, - (e->align.Y - 1.0) * dstsize.Y / 2); - core::rect rect(0, 0, dstsize.X, dstsize.Y); - rect += pos + offset + v2s32(e->offset.X * m_scale_factor, - e->offset.Y * m_scale_factor); - draw2DImageFilterScaled(driver, texture, rect, - core::rect(core::position2d(0,0), imgsize), - NULL, colors, true); + drawImage(pos, e->scale, e->align, e->offset, texture, color); break; } case HUD_ELEM_COMPASS: { video::ITexture *texture = tsrc->getTexture(e->text); @@ -545,6 +530,28 @@ void Hud::drawLuaElements(const v3s16 &camera_offset) e->offset.Y * m_scale_factor); client->getMinimap()->drawMinimap(rect); break; } + case HUD_ELEM_CROSSHAIR: { + // CAMERA_MODE_THIRD_FRONT and HAVE_TOUCHSCREENGUI may disallow crosshairs. + // In the future it may be good to add a feature for Lua HudElements to + // specify CAMERA_MODE and TOUCHSCREEN behavior. + if (!draw_crosshairs) + continue; + + if (e->style == 0) { + // Default Crosshair + drawCrosshair(pos, e->align, e->offset, e->scale); + } else if (e->style == 1) { + // Image Crosshair + const video::SColor color(255, 255, 255, 255); + if (pointing_at_object) { + drawImage(pos, e->scale, e->align, e->offset, + tsrc->getTexture(e->text2), color); + } else { + drawImage(pos, e->scale, e->align, e->offset, + tsrc->getTexture(e->text), color); + } + } + break; } default: infostream << "Hud::drawLuaElements: ignoring drawform " << e->type << " due to unrecognized type" << std::endl; @@ -794,55 +801,71 @@ void Hud::drawHotbar(u16 playeritem) } } - -void Hud::drawCrosshair() +void Hud::drawImage(const v2s32 &pos, const v2f &scale, const v2f &align, const v2f &offset, + video::ITexture *texture, const video::SColor &color) { - auto draw_image_crosshair = [this] (video::ITexture *tex) { - core::dimension2di orig_size(tex->getOriginalSize()); - // Integer scaling to avoid artifacts, floor instead of round since too - // small looks better than too large in this case. - core::dimension2di scaled_size = orig_size * std::max(std::floor(m_scale_factor), 1.0f); + const video::SColor colors[] = {color, color, color, color}; + core::dimension2di imgsize(texture->getOriginalSize()); + v2s32 dstsize(imgsize.Width * scale.X * m_scale_factor, + imgsize.Height * scale.Y * m_scale_factor); + if (scale.X < 0) + dstsize.X = m_screensize.X * (scale.X * -0.01); + if (scale.Y < 0) + dstsize.Y = m_screensize.Y * (scale.Y * -0.01); + v2s32 align_offset((align.X - 1.0) * dstsize.X / 2, (align.Y - 1.0) * dstsize.Y / 2); - core::rect src_rect(orig_size); - core::position2d pos(m_displaycenter.X - scaled_size.Width / 2, - m_displaycenter.Y - scaled_size.Height / 2); - core::rect dest_rect(pos, scaled_size); + core::rect rect(0, 0, dstsize.X, dstsize.Y); + rect += pos + align_offset + v2s32(offset.X * m_scale_factor, offset.Y * m_scale_factor); - video::SColor colors[] = { crosshair_argb, crosshair_argb, - crosshair_argb, crosshair_argb }; - - draw2DImageFilterScaled(driver, tex, dest_rect, src_rect, - nullptr, colors, true); - }; + draw2DImageFilterScaled(driver, texture, rect, +core::rect(core::position2d(0,0), imgsize), + NULL, colors, true); +} +void Hud::drawCrosshair(const v2s32 &pos, const v2f &align, const v2f &offset, const v2f &scale) +{ if (pointing_at_object) { if (use_object_crosshair_image) { - draw_image_crosshair(tsrc->getTexture("object_crosshair.png")); + drawImage(pos, scale, align, offset, + tsrc->getTexture("object_crosshair.png"), crosshair_argb); } else { - s32 line_size = core::round32(OBJECT_CROSSHAIR_LINE_SIZE * m_scale_factor); + s32 line_size_x = core::round32(m_scale_factor * OBJECT_CROSSHAIR_LINE_SIZE * scale.X); + s32 line_size_y = core::round32(m_scale_factor * OBJECT_CROSSHAIR_LINE_SIZE * scale.Y); + v2s32 center_pos( + pos.X + offset.X*m_scale_factor + align.X * line_size_x, + pos.Y + offset.Y*m_scale_factor + align.Y * line_size_y); driver->draw2DLine( - m_displaycenter - v2s32(line_size, line_size), - m_displaycenter + v2s32(line_size, line_size), + center_pos - v2s32(line_size_x, line_size_y), + center_pos + v2s32(line_size_x, line_size_y), crosshair_argb); driver->draw2DLine( - m_displaycenter + v2s32(line_size, -line_size), - m_displaycenter + v2s32(-line_size, line_size), + center_pos + v2s32(line_size_x, -line_size_y), + center_pos + v2s32(-line_size_x, line_size_y), crosshair_argb); } - return; } if (use_crosshair_image) { - draw_image_crosshair(tsrc->getTexture("crosshair.png")); + drawImage(pos, scale, align, offset, + tsrc->getTexture("crosshair.png"), crosshair_argb); } else { - s32 line_size = core::round32(CROSSHAIR_LINE_SIZE * m_scale_factor); + s32 line_size_x = core::round32(m_scale_factor * CROSSHAIR_LINE_SIZE * scale.X); + s32 line_size_y = core::round32(m_scale_factor * CROSSHAIR_LINE_SIZE * scale.Y); - driver->draw2DLine(m_displaycenter - v2s32(line_size, 0), - m_displaycenter + v2s32(line_size, 0), crosshair_argb); - driver->draw2DLine(m_displaycenter - v2s32(0, line_size), - m_displaycenter + v2s32(0, line_size), crosshair_argb); + v2s32 center_pos( + pos.X + offset.X*m_scale_factor + align.X * line_size_x, + pos.Y + offset.Y*m_scale_factor + align.Y * line_size_y); + + driver->draw2DLine( + center_pos - v2s32(line_size_x, 0), + center_pos + v2s32(line_size_x, 0), + crosshair_argb); + driver->draw2DLine( + center_pos - v2s32(0, line_size_y), + center_pos + v2s32(0, line_size_y), + crosshair_argb); } } diff --git a/src/client/hud.h b/src/client/hud.h index 746a1b33e..7d6e51ace 100644 --- a/src/client/hud.h +++ b/src/client/hud.h @@ -64,9 +64,11 @@ public: void disableBlockBounds(); void drawBlockBounds(); + void drawImage(const v2s32 &pos, const v2f &scale, const v2f &align, const v2f &offset, + video::ITexture *texture, const video::SColor &color); void drawHotbar(u16 playeritem); void resizeHotbar(); - void drawCrosshair(); + void drawCrosshair(const v2s32 &pos, const v2f &align, const v2f &offset, const v2f &scale); void drawSelectionMesh(); void updateSelectionMesh(const v3s16 &camera_offset); @@ -92,7 +94,7 @@ public: bool hasElementOfType(HudElementType type); - void drawLuaElements(const v3s16 &camera_offset); + void drawLuaElements(const v3s16 &camera_offset, bool draw_crosshairs); private: bool calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *pos); diff --git a/src/client/render/core.cpp b/src/client/render/core.cpp index dfd9dc02e..911015324 100644 --- a/src/client/render/core.cpp +++ b/src/client/render/core.cpp @@ -37,13 +37,13 @@ RenderingCore::~RenderingCore() } void RenderingCore::draw(video::SColor _skycolor, bool _show_hud, - bool _draw_wield_tool, bool _draw_crosshair) + bool _draw_wield_tool, bool _draw_crosshairs) { v2u32 screensize = device->getVideoDriver()->getScreenSize(); virtual_size = v2u32(screensize.X * virtual_size_scale.X, screensize.Y * virtual_size_scale.Y); PipelineContext context(device, client, hud, shadow_renderer, _skycolor, screensize); - context.draw_crosshair = _draw_crosshair; + context.draw_crosshairs = _draw_crosshairs; context.draw_wield_tool = _draw_wield_tool; context.show_hud = _show_hud; diff --git a/src/client/render/core.h b/src/client/render/core.h index c5617bcb2..20ad78f51 100644 --- a/src/client/render/core.h +++ b/src/client/render/core.h @@ -54,7 +54,7 @@ public: RenderingCore &operator=(RenderingCore &&) = delete; void draw(video::SColor _skycolor, bool _show_hud, - bool _draw_wield_tool, bool _draw_crosshair); + bool _draw_wield_tool, bool _draw_crosshairs); v2u32 getVirtualSize() const; diff --git a/src/client/render/pipeline.h b/src/client/render/pipeline.h index abb108652..671fe42e7 100644 --- a/src/client/render/pipeline.h +++ b/src/client/render/pipeline.h @@ -47,7 +47,7 @@ struct PipelineContext bool show_hud {true}; bool draw_wield_tool {true}; - bool draw_crosshair {true}; + bool draw_crosshairs {true}; }; /** diff --git a/src/client/render/plain.cpp b/src/client/render/plain.cpp index 60a732415..91d011cfb 100644 --- a/src/client/render/plain.cpp +++ b/src/client/render/plain.cpp @@ -57,12 +57,9 @@ void DrawHUD::run(PipelineContext &context) context.shadow_renderer->drawDebug(); context.hud->resizeHotbar(); - - if (context.draw_crosshair) - context.hud->drawCrosshair(); - context.hud->drawHotbar(context.client->getEnv().getLocalPlayer()->getWieldIndex()); - context.hud->drawLuaElements(context.client->getCamera()->getOffset()); + context.hud->drawLuaElements(context.client->getCamera()->getOffset(), + context.draw_crosshairs); context.client->getCamera()->drawNametags(); } context.device->getGUIEnvironment()->drawAll(); diff --git a/src/client/renderingengine.cpp b/src/client/renderingengine.cpp index 298ecadbd..4f9a07133 100644 --- a/src/client/renderingengine.cpp +++ b/src/client/renderingengine.cpp @@ -411,9 +411,9 @@ void RenderingEngine::finalize() } void RenderingEngine::draw_scene(video::SColor skycolor, bool show_hud, - bool draw_wield_tool, bool draw_crosshair) + bool draw_wield_tool, bool draw_crosshairs) { - core->draw(skycolor, show_hud, draw_wield_tool, draw_crosshair); + core->draw(skycolor, show_hud, draw_wield_tool, draw_crosshairs); } const VideoDriverInfo &RenderingEngine::getVideoDriverInfo(irr::video::E_DRIVER_TYPE type) diff --git a/src/client/renderingengine.h b/src/client/renderingengine.h index b8ae62fc4..1ee73d2b4 100644 --- a/src/client/renderingengine.h +++ b/src/client/renderingengine.h @@ -142,7 +142,7 @@ public: float dtime = 0, int percent = 0, bool sky = true); void draw_scene(video::SColor skycolor, bool show_hud, - bool draw_wield_tool, bool draw_crosshair); + bool draw_wield_tool, bool draw_crosshairs); void initialize(Client *client, Hud *hud); void finalize(); diff --git a/src/hud.cpp b/src/hud.cpp index 9b336cdef..7e1e66779 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -30,6 +30,7 @@ const struct EnumString es_HudElementType[] = {HUD_ELEM_IMAGE_WAYPOINT, "image_waypoint"}, {HUD_ELEM_COMPASS, "compass"}, {HUD_ELEM_MINIMAP, "minimap"}, + {HUD_ELEM_CROSSHAIR, "crosshair"}, {0, NULL}, }; diff --git a/src/hud.h b/src/hud.h index 5540828d4..9830ec490 100644 --- a/src/hud.h +++ b/src/hud.h @@ -67,7 +67,8 @@ enum HudElementType { HUD_ELEM_WAYPOINT = 4, HUD_ELEM_IMAGE_WAYPOINT = 5, HUD_ELEM_COMPASS = 6, - HUD_ELEM_MINIMAP = 7 + HUD_ELEM_MINIMAP = 7, + HUD_ELEM_CROSSHAIR = 8, }; enum HudElementStat : u8 { From 8c444baf8a9a45791e0ae1211a5787091d1ea75b Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 18 Jan 2024 15:31:59 +0100 Subject: [PATCH 2/6] Minor code style improvement and image test fix --- games/devtest/mods/testhud/init.lua | 8 -------- src/client/hud.cpp | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/games/devtest/mods/testhud/init.lua b/games/devtest/mods/testhud/init.lua index 51ab6666a..7539d7957 100644 --- a/games/devtest/mods/testhud/init.lua +++ b/games/devtest/mods/testhud/init.lua @@ -209,14 +209,6 @@ minetest.register_chatcommand("zoomfov", { -- Images local hud_image_defs = { - { - type = "image", - position = {x=0.5, y=0.5}, - scale = {x = 10, y = 10}, - text = "crosshair.png", - alignment = {x=0, y=-0}, - offset = {x=0, y=0}, - }, { type = "image", position = {x=0.3, y=0.3}, diff --git a/src/client/hud.cpp b/src/client/hud.cpp index a4f91d8af..89330843c 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -818,7 +818,7 @@ void Hud::drawImage(const v2s32 &pos, const v2f &scale, const v2f &align, const rect += pos + align_offset + v2s32(offset.X * m_scale_factor, offset.Y * m_scale_factor); draw2DImageFilterScaled(driver, texture, rect, -core::rect(core::position2d(0,0), imgsize), + core::rect(core::position2d(0,0),imgsize), NULL, colors, true); } From e8f92ba8252c3607432889a9c689d8bf28fed331 Mon Sep 17 00:00:00 2001 From: cx384 Date: Wed, 31 Jan 2024 12:13:23 +0100 Subject: [PATCH 3/6] Add better backward compatibility --- src/client/hud.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 89330843c..7276df7e9 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -369,6 +369,20 @@ void Hud::drawLuaElements(const v3s16 &camera_offset, bool draw_crosshairs) elems.insert(it, e); } + // Legacy (Remove this when version 5.8.0 and older is not supported anymore.) + // If a new client connects to a sever version 5.8.0 and older some HUD elements + // are not hard coded in the client code anymore, but handled in builtin. + // So the client must add them again. + + // Crosshair + if ((player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) && !hasElementOfType(HUD_ELEM_CROSSHAIR)) { + HudElement crosshair{HUD_ELEM_CROSSHAIR, v2f(0.5, 0.5), "", v2f(1, 1), "", 0 , 0, 0, v2f(), + v2f(), v3f(), v2s32(), 0, "", 0}; + elems.push_back(&crosshair); + } + + // End of legacy support code. + for (HudElement *e : elems) { v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5), From f24116ed018381afc6ee0b4f687631988f2ba9b0 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 1 Feb 2024 17:38:06 +0100 Subject: [PATCH 4/6] Replace hasElementOfType with protocol version --- src/client/hud.cpp | 11 ++--------- src/network/networkprotocol.h | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 7276df7e9..65f47a8a4 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -369,20 +369,13 @@ void Hud::drawLuaElements(const v3s16 &camera_offset, bool draw_crosshairs) elems.insert(it, e); } - // Legacy (Remove this when version 5.8.0 and older is not supported anymore.) - // If a new client connects to a sever version 5.8.0 and older some HUD elements - // are not hard coded in the client code anymore, but handled in builtin. - // So the client must add them again. - - // Crosshair - if ((player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) && !hasElementOfType(HUD_ELEM_CROSSHAIR)) { + // Add builtin cosshair if the server doesn't send it. + if (client->getProtoVersion() < 44 && (player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE)) { HudElement crosshair{HUD_ELEM_CROSSHAIR, v2f(0.5, 0.5), "", v2f(1, 1), "", 0 , 0, 0, v2f(), v2f(), v3f(), v2s32(), 0, "", 0}; elems.push_back(&crosshair); } - // End of legacy support code. - for (HudElement *e : elems) { v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5), diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index add80b3b2..105d82ecc 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -222,7 +222,7 @@ with this program; if not, write to the Free Software Foundation, Inc., PROTOCOL VERSION 44: AO_CMD_SET_BONE_POSITION extended Add TOCLIENT_MOVE_PLAYER_REL - Move default minimap from client-side C++ to server-side builtin Lua + Move default minimap and crosshair from client-side C++ to server-side builtin Lua [scheduled bump for 5.9.0] */ From 7de794388182eb572b05869353728ce1c31994c5 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 1 Feb 2024 18:00:53 +0100 Subject: [PATCH 5/6] Fix zindex behavior --- src/client/hud.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 65f47a8a4..2295b8d21 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -349,12 +349,21 @@ void Hud::drawLuaElements(const v3s16 &camera_offset, bool draw_crosshairs) std::vector elems; elems.reserve(player->maxHudId()); - // Add builtin minimap if the server doesn't send it. + // Add builtin elements if the server doesn't send them. + // Declared here such that they have the same lifetime as the elems vector HudElement minimap; - if (client->getProtoVersion() < 44 && (player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE)) { - minimap = {HUD_ELEM_MINIMAP, v2f(1, 0), "", v2f(), "", 0 , 0, 0, v2f(-1, 1), - v2f(-10, 10), v3f(), v2s32(256, 256), 0, "", 0}; - elems.push_back(&minimap); + HudElement crosshair; + if (client->getProtoVersion() < 44) { + if (player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE) { + minimap = {HUD_ELEM_MINIMAP, v2f(1, 0), "", v2f(), "", 0 , 0, 0, v2f(-1, 1), + v2f(-10, 10), v3f(), v2s32(256, 256), 0, "", 0}; + elems.push_back(&minimap); + } + if (player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) { + crosshair = {HUD_ELEM_CROSSHAIR, v2f(0.5, 0.5), "", v2f(1, 1), "", 0 , 0, 0, v2f(), + v2f(), v3f(), v2s32(), 0, "", 0}; + elems.push_back(&crosshair); + } } for (size_t i = 0; i != player->maxHudId(); i++) { @@ -369,13 +378,6 @@ void Hud::drawLuaElements(const v3s16 &camera_offset, bool draw_crosshairs) elems.insert(it, e); } - // Add builtin cosshair if the server doesn't send it. - if (client->getProtoVersion() < 44 && (player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE)) { - HudElement crosshair{HUD_ELEM_CROSSHAIR, v2f(0.5, 0.5), "", v2f(1, 1), "", 0 , 0, 0, v2f(), - v2f(), v3f(), v2s32(), 0, "", 0}; - elems.push_back(&crosshair); - } - for (HudElement *e : elems) { v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5), From bbec2ad0aaddac262837678503c140a6c5511a86 Mon Sep 17 00:00:00 2001 From: cx384 Date: Thu, 11 Apr 2024 22:51:37 +0200 Subject: [PATCH 6/6] Fix rebase change --- src/client/game.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/game.cpp b/src/client/game.cpp index 69c4f04fb..897657fc5 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -4305,7 +4305,7 @@ void Game::drawScene(ProfilerGraph *graph, RunStats *stats) bool draw_crosshairs = (this->camera->getCameraMode() != CAMERA_MODE_THIRD_FRONT); if (g_touchscreengui && isTouchCrosshairDisabled()) - draw_crosshair = false; + draw_crosshairs = false; this->m_rendering_engine->draw_scene(sky_color, this->m_game_ui->m_flags.show_hud, draw_wield_tool, draw_crosshairs);