1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-15 18:57:08 +00:00

Apply suggestions

This commit is contained in:
cx384 2025-03-20 21:25:26 +01:00
parent 6248707548
commit 69a2d0a511
9 changed files with 69 additions and 72 deletions

View file

@ -229,7 +229,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
client, selected ? IT_ROT_SELECTED : IT_ROT_NONE);
}
void Hud::drawItems(const v2s32& pos, s32 inv_size, s32 inv_offset, InventoryList *mainlist,
void Hud::drawItems(v2s32 pos, s32 inv_size, s32 inv_offset, InventoryList *mainlist,
u16 selectitem, u16 direction, bool is_hotbar, u16 hotbar_touchcontrol_offset)
{
// Store hotbar_selected_image in member variable, used by drawItem()
@ -255,8 +255,8 @@ void Hud::drawItems(const v2s32& pos, s32 inv_size, s32 inv_offset, InventoryLis
}
// Returns the total width, height and pos to draw a HUD inventory
void Hud::getInventoryDimensions(v2s32 screen_pos, const v2s32& screen_offset, s32 inv_length,
v2f alignment, u16 direction, v2s32& pos, s32& width, s32& height)
void Hud::getInventoryDimensions(v2s32 screen_pos, v2s32 screen_offset, s32 inv_length,
v2f alignment, u16 direction, v2s32 &pos, s32 &width, s32 &height) const
{
height = m_hotbar_imagesize + m_padding * 2;
width = inv_length * (m_hotbar_imagesize + m_padding * 2);
@ -276,7 +276,7 @@ void Hud::getInventoryDimensions(v2s32 screen_pos, const v2s32& screen_offset, s
// Returns an inventory position offset depending on the direction
// arguments are the number of items before and the remaining number of items
v2s32 Hud::getInventoryPosOffset(u16 direction, s32 before, s32 remainder)
v2s32 Hud::getInventoryPosOffset(u16 direction, s32 before, s32 remainder) const
{
s32 fullimglen = m_hotbar_imagesize + m_padding * 2;
@ -299,7 +299,7 @@ v2s32 Hud::getInventoryPosOffset(u16 direction, s32 before, s32 remainder)
return steppos;
}
void Hud::drawInventoryBackground(const v2s32& pos, s32 width, s32 height)
void Hud::drawInventoryBackground(v2s32 pos, s32 width, s32 height)
{
// Store hotbar_image in member variable
if (hotbar_image != player->hotbar_image) {
@ -322,7 +322,7 @@ void Hud::drawInventoryBackground(const v2s32& pos, s32 width, s32 height)
// NOTE: selectitem = 0 -> no selected; selectitem is 1-based
// mainlist can be NULL, but draw the frame anyway.
void Hud::drawInventory(const v2s32& screen_pos, const v2f& offset, s32 itemcount, v2f alignment,
void Hud::drawInventory(v2s32 screen_pos, v2f offset, s32 itemcount, v2f alignment,
InventoryList *mainlist, u16 selectitem, u16 direction)
{
v2s32 screen_offset(offset.X, offset.Y);
@ -336,12 +336,9 @@ void Hud::drawInventory(const v2s32& screen_pos, const v2f& offset, s32 itemcoun
}
// If max_inv_length == 0 do not clamp size
void Hud::drawHotbar(const v2s32 &screen_pos, const v2f &offset, u16 direction,
void Hud::drawHotbar(v2s32 screen_pos, v2f offset, u16 direction,
const v2f &alignment, s32 max_inv_length, s32 inv_offset)
{
if (g_touchcontrols)
g_touchcontrols->resetHotbarRects();
auto &sources = player->hotbar_source.getSources();
u16 wield_index = player->getWieldIndex() + 1;
v2s32 screen_offset(offset.X, offset.Y);
@ -364,7 +361,7 @@ void Hud::drawHotbar(const v2s32 &screen_pos, const v2f &offset, u16 direction,
for (s32 i_offset = inv_offset; source_index < sources.size(); source_index++) {
const HotbarSource::Source& source = sources[source_index];
if (i_offset < source.length) {
s32 inv_length = MYMIN(source.length - i_offset, max_inv_length);
s32 inv_length = std::min<s32>(source.length - i_offset, max_inv_length);
drawItems(pos + getInventoryPosOffset(direction, 0, max_inv_length - inv_length),
inv_length + source.offset + i_offset,
@ -381,7 +378,7 @@ void Hud::drawHotbar(const v2s32 &screen_pos, const v2f &offset, u16 direction,
for (; source_index < sources.size(); source_index++) {
const HotbarSource::Source &source = sources[source_index];
s32 inv_length = MYMIN(source.length, max_inv_length - length_before);
s32 inv_length = std::min<s32>(source.length, max_inv_length - length_before);
if(inv_length <= 0)
break;
@ -394,6 +391,25 @@ void Hud::drawHotbar(const v2s32 &screen_pos, const v2f &offset, u16 direction,
}
}
void Hud::drawHotbarElement(v2s32 pos, HudElement *e)
{
if (g_touchcontrols) // FIXME probably breaks if more then one hotbar element exists
g_touchcontrols->resetHotbarRects();
// Handle splitting caused by hud_hotbar_max_width
u16 hotbar_itemcount = player->hotbar_source.getMaxLength();
float width = hotbar_itemcount * (m_hotbar_imagesize + m_padding * 2);
const v2u32 &window_size = RenderingEngine::getWindowSize();
if (width / window_size.X > g_settings->getFloat("hud_hotbar_max_width")) {
v2s32 upper_pos = pos - v2s32(0, m_hotbar_imagesize + m_padding);
u16 upper_itemcount = hotbar_itemcount / 2;
drawHotbar(upper_pos, e->offset, e->dir, e->align, upper_itemcount, 0);
drawHotbar(pos, e->offset, e->dir, e->align, 0, upper_itemcount);
} else {
drawHotbar(pos, e->offset, e->dir, e->align);
}
}
bool Hud::hasElementOfType(HudElementType type)
{
for (size_t i = 0; i != player->maxHudId(); i++) {
@ -654,23 +670,9 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
e->offset.Y * m_scale_factor);
client->getMinimap()->drawMinimap(rect);
break; }
case HUD_ELEM_HOTBAR: {
// Handle splitting caused by hud_hotbar_max_width
u16 hotbar_itemcount = player->hotbar_source.getMaxLength();
s32 width = hotbar_itemcount * (m_hotbar_imagesize + m_padding * 2);
const v2u32 &window_size = RenderingEngine::getWindowSize();
if ((float) width / (float) window_size.X >
g_settings->getFloat("hud_hotbar_max_width")) {
v2s32 upper_pos = pos - v2s32(0, m_hotbar_imagesize + m_padding);
u16 upper_itemcount = hotbar_itemcount/2;
drawHotbar(upper_pos, e->offset, e->dir, e->align, upper_itemcount, 0);
drawHotbar(pos, e->offset, e->dir, e->align,
hotbar_itemcount - upper_itemcount, upper_itemcount);
case HUD_ELEM_HOTBAR:
drawHotbarElement(pos, e);
break;
} else {
drawHotbar(pos, e->offset, e->dir, e->align);
}
break; }
default:
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type
<< " due to unrecognized type" << std::endl;

View file

@ -104,19 +104,20 @@ private:
s32 count, s32 maxcount, v2s32 offset, v2s32 size = v2s32());
void drawItem(const ItemStack &item, const core::rect<s32> &rect, bool selected);
void drawItems(const v2s32& pos, s32 inv_size, s32 inv_offset, InventoryList *mainlist,
void drawItems(v2s32 pos, s32 inv_size, s32 inv_offset, InventoryList *mainlist,
u16 selectitem, u16 direction, bool is_hotbar = false,
u16 hotbar_touchcontrol_offset = 0);
v2s32 getInventoryPosOffset(u16 direction, s32 before, s32 remainder);
void getInventoryDimensions(v2s32 screen_pos, const v2s32& screen_offset, s32 inv_length,
v2f alignment, u16 direction, v2s32& pos, s32& width, s32& height);
void drawInventoryBackground(const v2s32& pos, s32 width, s32 height);
void drawInventory(const v2s32& screen_pos, const v2f& offset, s32 itemcount,
v2s32 getInventoryPosOffset(u16 direction, s32 before, s32 remainder) const;
void getInventoryDimensions(v2s32 screen_pos, v2s32 screen_offset, s32 inv_length,
v2f alignment, u16 direction, v2s32 &pos, s32 &width, s32 &height) const;
void drawInventoryBackground(v2s32 pos, s32 width, s32 height);
void drawInventory(v2s32 screen_pos, v2f offset, s32 itemcount,
v2f alignment, InventoryList *mainlist, u16 selectitem, u16 direction);
void drawHotbar(const v2s32 &pos, const v2f &offset, u16 direction, const v2f &alignment,
void drawHotbar(v2s32 pos, v2f offset, u16 direction, const v2f &alignment,
s32 max_inv_length = 0, s32 inv_offset = 0);
void drawHotbarElement(v2s32 pos, HudElement *e);
void drawCompassTranslate(HudElement *e, video::ITexture *texture,
const core::rect<s32> &rect, int way);

View file

@ -82,12 +82,12 @@ Player::~Player()
void Player::setWieldIndex(u16 index)
{
m_wield_index = MYMIN(index, hotbar_source.getMaxLength() - 1);
m_wield_index = std::min<u16>(index, hotbar_source.getMaxLength() - 1);
}
u16 Player::getWieldIndex()
{
return MYMIN(m_wield_index, hotbar_source.getMaxLength() - 1);
return std::min<u16>(m_wield_index, hotbar_source.getMaxLength() - 1);
}
ItemStack &Player::getWieldedItem(ItemStack *selected, ItemStack *hand) const

View file

@ -3461,7 +3461,7 @@ bool Server::hudSetHotbarItemcountLegacy(RemotePlayer *player, s32 hotbar_itemco
return false;
InventoryList *mainlist = player->inventory.getList("main");
hotbar_itemcount = mainlist ? MYMIN(mainlist->getSize(), (u32) hotbar_itemcount) : 0;
hotbar_itemcount = mainlist ? std::min<u32>(mainlist->getSize(), hotbar_itemcount) : 0;
player->hotbar_source.setHotbarItemcountLegacy(hotbar_itemcount);

View file

@ -17,7 +17,6 @@
#include "util/thread.h"
#include "util/basic_macros.h"
#include "util/metricsbackend.h"
#include "util/hotbar_source.h"
#include "serverenvironment.h"
#include "server/clientiface.h"
#include "threading/ordered_mutex.h"
@ -67,6 +66,7 @@ class ServerInventoryManager;
struct PackedValue;
struct ParticleParameters;
struct ParticleSpawnerParameters;
struct HotbarSource;
// Anticheat flags
enum {

View file

@ -5,10 +5,13 @@ set(util_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/colorize.cpp
${CMAKE_CURRENT_SOURCE_DIR}/directiontables.cpp
${CMAKE_CURRENT_SOURCE_DIR}/enriched_string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/enum_string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/hashing.cpp
${CMAKE_CURRENT_SOURCE_DIR}/hotbar_source.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ieee_float.cpp
${CMAKE_CURRENT_SOURCE_DIR}/metricsbackend.cpp
${CMAKE_CURRENT_SOURCE_DIR}/numeric.cpp
${CMAKE_CURRENT_SOURCE_DIR}/png.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pointedthing.cpp
${CMAKE_CURRENT_SOURCE_DIR}/pointabilities.cpp
${CMAKE_CURRENT_SOURCE_DIR}/quicktune.cpp
@ -17,7 +20,4 @@ set(util_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp
${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp
${CMAKE_CURRENT_SOURCE_DIR}/png.cpp
${CMAKE_CURRENT_SOURCE_DIR}/enum_string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/hotbar_source.cpp
PARENT_SCOPE)

View file

@ -3,21 +3,18 @@
// Copyright (C) 2025 cx384
#include "hotbar_source.h"
#include "serialize.h"
void HotbarSource::setHotbarItemcountLegacy(s32 count)
#include "serialize.h"
#include "numeric.h"
void HotbarSource::setHotbarItemcountLegacy(u16 count)
{
sources.clear();
sources.push_back({HOTBAR_INVENTORY_LIST_DEFAULT, (u16) count, 0});
}
u16 HotbarSource::getMaxLength() const
{
u16 length = 0;
for (auto& source : sources) {
length += source.length;
}
return length ;
sources.push_back({
HOTBAR_INVENTORY_LIST_DEFAULT,
rangelim(count, 0, HOTBAR_ITEMCOUNT_MAX),
0
});
}
bool HotbarSource::getInventoryFromWieldIndex(u16 wield_index, std::string &list, u16 &index) const
@ -33,18 +30,17 @@ bool HotbarSource::getInventoryFromWieldIndex(u16 wield_index, std::string &list
return false;
}
u16 HotbarSource::getLengthBefore(std::size_t index) const {
u16 length_before = 0;
for (std::size_t i = 0; i < index && i < sources.size(); i++) {
length_before += sources[i].length;
}
return length_before;
u16 HotbarSource::getLengthBefore(std::size_t index) const
{
index = std::min(index, sources.size());
u16 length = 0;
while (index-- > 0)
length += sources[index].length;
return length;
}
void HotbarSource::serialize(std::ostream &os) const
{
writeU8(os, 0); // version
writeU16(os, sources.size());
for (const auto &source : sources) {
os << serializeString16(source.list);
@ -55,10 +51,6 @@ void HotbarSource::serialize(std::ostream &os) const
void HotbarSource::deSerialize(std::istream &is)
{
int version = readU8(is);
if (version != 0)
throw SerializationError("unsupported HotbarSource version");
sources.clear();
u16 size = readU16(is);
for (u16 i = 0; i < size; i++) {
@ -66,6 +58,6 @@ void HotbarSource::deSerialize(std::istream &is)
source.list = deSerializeString16(is);
source.length = readU16(is);
source.offset = readU16(is);
sources.push_back(source);
sources.push_back(std::move(source));
}
}

View file

@ -24,10 +24,12 @@ struct HotbarSource {
};
// Old functionality which could only use the "main" list
void setHotbarItemcountLegacy(s32 count);
void setHotbarItemcountLegacy(u16 count);
// Returns the total length of all sources
u16 getMaxLength() const;
u16 getMaxLength() const {
return getLengthBefore(sources.size());
}
// Returns list and index of the inventory if it exists
bool getInventoryFromWieldIndex(u16 wield_index, std::string &list, u16 &index) const;