Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMake/Assets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ set(devilutionx_assets
lua/mods/clock/init.lua
"lua/mods/Floating Numbers - Damage/init.lua"
"lua/mods/Floating Numbers - XP/init.lua"
lua/mods/Tooltips/init.lua
lua/repl_prelude.lua
plrgfx/warrior/whu/whufm.trn
plrgfx/warrior/whu/whulm.trn
Expand Down
2 changes: 2 additions & 0 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,15 @@ set(libdevilutionx_SRCS
lua/modules/dev/search.cpp
lua/modules/dev/towners.cpp
lua/modules/floatingnumbers.cpp
lua/modules/gamepad.cpp
lua/modules/i18n.cpp
lua/modules/items.cpp
lua/modules/log.cpp
lua/modules/monsters.cpp
lua/modules/player.cpp
lua/modules/render.cpp
lua/modules/system.cpp
lua/modules/infobox.cpp
lua/modules/towners.cpp
lua/repl.cpp

Expand Down
81 changes: 67 additions & 14 deletions Source/control/control_infobox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
#include "engine/render/primitive_render.hpp"
#include "inv.h"
#include "levels/trigs.h"
#include "lua/lua_global.hpp"
#include "lua/modules/infobox.hpp"
#include "panels/partypanel.hpp"
#include "qol/stash.h"
#include "qol/xpbar.h"
#include "towners.h"
#include "utils/algorithm/container.hpp"
#include "utils/format_int.hpp"
#include "utils/log.hpp"

Check warning on line 15 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:15:1 [misc-include-cleaner]

included header log.hpp is not used directly
#include "utils/screen_reader.hpp"
#include "utils/str_cat.hpp"
#include "utils/str_split.hpp"

namespace devilution {

StringOrView InfoString;

Check warning on line 22 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:22:1 [misc-include-cleaner]

no header providing "devilution::StringOrView" is directly included
StringOrView FloatingInfoString;

namespace {
Expand All @@ -28,12 +30,12 @@
return;

const int space[] = { 18, 12, 6, 3, 0 };
Rectangle infoBox = InfoBoxRect;

Check warning on line 33 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:33:2 [misc-include-cleaner]

no header providing "devilution::Rectangle" is directly included

SetPanelObjectPosition(UiPanels::Main, infoBox);

Check warning on line 35 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:35:25 [misc-include-cleaner]

no header providing "devilution::UiPanels" is directly included

const auto newLineCount = static_cast<int>(c_count(InfoString.str(), '\n'));
const int spaceIndex = std::min(4, newLineCount);

Check warning on line 38 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:38:30 [misc-include-cleaner]

no header providing "std::min" is directly included
const int spacing = space[spaceIndex];
const int lineHeight = 12 + spacing;

Expand All @@ -44,21 +46,23 @@

SpeakText(InfoString);

DrawString(out, InfoString, infoBox,
{
.flags = InfoColor | UiFlags::AlignCenter | UiFlags::VerticalCenter | UiFlags::KerningFitSpacing,
.spacing = 2,
.lineHeight = lineHeight,
});
if (!DrawInfoBoxTextWithColors(out, InfoString, infoBox, UiFlags::AlignCenter | UiFlags::VerticalCenter | UiFlags::KerningFitSpacing, 2, lineHeight)) {

Check warning on line 49 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:49:59 [misc-include-cleaner]

no header providing "devilution::UiFlags" is directly included
DrawString(out, InfoString, infoBox,

Check warning on line 50 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:50:3 [misc-include-cleaner]

no header providing "devilution::DrawString" is directly included
{
.flags = InfoColor | UiFlags::AlignCenter | UiFlags::VerticalCenter | UiFlags::KerningFitSpacing,
.spacing = 2,
.lineHeight = lineHeight,
});
}
}

Rectangle GetFloatingInfoRect(const int lineHeight, const int textSpacing)

Check warning on line 59 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:59:11 [readability-function-cognitive-complexity]

function 'GetFloatingInfoRect' has cognitive complexity of 45 (threshold 25)
{
// Calculate the width and height of the floating info box
const std::string txt = std::string(FloatingInfoString);

Check warning on line 62 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:62:13 [misc-include-cleaner]

no header providing "std::string" is directly included

auto lines = SplitByChar(txt, '\n');
const GameFontTables font = GameFont12;

Check warning on line 65 in Source/control/control_infobox.cpp

View workflow job for this annotation

GitHub Actions / tidy-check

Source/control/control_infobox.cpp:65:8 [misc-include-cleaner]

no header providing "devilution::GameFontTables" is directly included
int maxW = 0;

for (const auto &line : lines) {
Expand Down Expand Up @@ -211,6 +215,26 @@
return (yAbove >= 0) ? yAbove : yBelow;
}

const Item *GetHoveredInventoryOrStashItem()
{
if (pcursinvitem >= INVITEM_HEAD && pcursinvitem < INVITEM_INV_FIRST) {
const int slot = pcursinvitem - INVITEM_HEAD;
return &InspectPlayer->InvBody[slot];
}
if (pcursinvitem >= INVITEM_INV_FIRST && pcursinvitem < INVITEM_INV_FIRST + InventoryGridCells) {
const int itemIdx = pcursinvitem - INVITEM_INV_FIRST;
return &InspectPlayer->InvList[itemIdx];
}
if (pcursinvitem >= INVITEM_BELT_FIRST && pcursinvitem < INVITEM_BELT_FIRST + MaxBeltItems) {
const int itemIdx = pcursinvitem - INVITEM_BELT_FIRST;
return &InspectPlayer->SpdList[itemIdx];
}
if (pcursstashitem != StashStruct::EmptyCell) {
return &Stash.stashList[pcursstashitem];
}
return nullptr;
}

void PrintFloatingInfo(const Surface &out)
{
if (ChatFlag)
Expand All @@ -234,6 +258,7 @@

// Prevent the floating info box from going off-screen vertically
floatingInfoBox.position.y = ClampAboveOrBelow(anchorY, spriteH, floatingInfoBox.size.height, vPadding, verticalSpacing);
SetLastFloatingInfoBoxRect(floatingInfoBox);

SpeakText(FloatingInfoString);

Expand All @@ -244,12 +269,16 @@
DrawHalfTransparentHorizontalLine(out, { floatingInfoBox.position.x - hPadding, floatingInfoBox.position.y - vPadding - 1 }, floatingInfoBox.size.width + (hPadding * 2), PAL16_GRAY + 10);
DrawHalfTransparentHorizontalLine(out, { floatingInfoBox.position.x - hPadding, floatingInfoBox.position.y + vPadding + floatingInfoBox.size.height }, floatingInfoBox.size.width + (hPadding * 2), PAL16_GRAY + 10);

DrawString(out, FloatingInfoString, floatingInfoBox,
{
.flags = InfoColor | UiFlags::AlignCenter | UiFlags::VerticalCenter,
.spacing = textSpacing,
.lineHeight = lineHeight,
});
if (!DrawInfoBoxTextWithColors(out, FloatingInfoString, floatingInfoBox, UiFlags::AlignCenter | UiFlags::VerticalCenter, textSpacing, lineHeight)) {
DrawString(out, FloatingInfoString, floatingInfoBox,
{
.flags = InfoColor | UiFlags::AlignCenter | UiFlags::VerticalCenter,
.spacing = textSpacing,
.lineHeight = lineHeight,
});
}

LuaEvent("AfterFloatingInfoBoxDraw");
}

} // namespace
Expand Down Expand Up @@ -355,6 +384,8 @@

void DrawInfoBox(const Surface &out)
{
const Item *infoBoxItem = nullptr;

DrawPanelBox(out, MakeSdlRect(InfoBoxRect.position.x, InfoBoxRect.position.y + PanelPaddingHeight, InfoBoxRect.size.width, InfoBoxRect.size.height), GetMainPanel().position + Displacement { InfoBoxRect.position.x, InfoBoxRect.position.y });
if (!MainPanelFlag && !trigflag && pcursinvitem == -1 && pcursstashitem == StashStruct::EmptyCell && !SpellSelectFlag && pcurs != CURSOR_HOURGLASS) {
InfoString = StringOrView {};
Expand All @@ -364,6 +395,7 @@
if (SpellSelectFlag || trigflag || pcurs == CURSOR_HOURGLASS) {
InfoColor = UiFlags::ColorWhite;
} else if (!myPlayer.HoldItem.isEmpty()) {
infoBoxItem = &myPlayer.HoldItem;
if (myPlayer.HoldItem._itype == ItemType::Gold) {
const int nGold = myPlayer.HoldItem._ivalue;
InfoString = fmt::format(fmt::runtime(ngettext("{:s} gold piece", "{:s} gold pieces", nGold)), FormatInteger(nGold));
Expand All @@ -374,8 +406,10 @@
InfoColor = myPlayer.HoldItem.getTextColor();
}
} else {
if (pcursitem != -1)
if (pcursitem != -1) {
GetItemStr(Items[pcursitem]);
infoBoxItem = &Items[pcursitem];
}
else if (ObjectUnderCursor != nullptr)
GetObjectStr(*ObjectUnderCursor);
if (pcursmonst != -1) {
Expand Down Expand Up @@ -407,6 +441,17 @@
AddInfoBoxString(_("Right click to inspect"));
}
}
if (infoBoxItem == nullptr)
infoBoxItem = GetHoveredInventoryOrStashItem();

if (infoBoxItem != nullptr) {
PrepareInfoBoxColors(infoBoxItem, false);
if (FloatingInfoBoxColors.hasCustomColors())
InfoColor = FloatingInfoBoxColors.getLineColor(0);
} else {
FloatingInfoBoxColors.clear();
}

if (!InfoString.empty())
PrintInfo(out);
}
Expand All @@ -416,10 +461,18 @@
if (pcursinvitem == -1 && pcursstashitem == StashStruct::EmptyCell) {
FloatingInfoString = StringOrView {};
InfoColor = UiFlags::ColorWhite;
ClearLastFloatingInfoBoxRect();
}

if (!FloatingInfoString.empty())
if (!FloatingInfoString.empty()) {
const Item *hoveredItem = GetHoveredInventoryOrStashItem();
PrepareInfoBoxColors(hoveredItem, true);
if (FloatingInfoBoxColors.hasCustomColors())
InfoColor = FloatingInfoBoxColors.getLineColor(0);
PrintFloatingInfo(out);
} else {
ClearLastFloatingInfoBoxRect();
}
}

} // namespace devilution
4 changes: 3 additions & 1 deletion Source/engine/render/scrollrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,9 @@ void DrawView(const Surface &out, Point startPosition)
}
DrawLevelButton(out);
if (ShowUniqueItemInfoBox) {
DrawUniqueInfo(out);
LuaEvent("BeforeUniqueInfoBoxDraw");
if (ShowUniqueItemInfoBox)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always be truthy right?

DrawUniqueInfo(out);
}
if (qtextflag) {
DrawQText(out);
Expand Down
34 changes: 34 additions & 0 deletions Source/lua/lua_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "engine/assets.hpp"
#include "lua/modules/audio.hpp"
#include "lua/modules/floatingnumbers.hpp"
#include "lua/modules/gamepad.hpp"
#include "lua/modules/hellfire.hpp"
#include "lua/modules/i18n.hpp"
#include "lua/modules/items.hpp"
Expand All @@ -22,6 +23,7 @@
#include "lua/modules/player.hpp"
#include "lua/modules/render.hpp"
#include "lua/modules/system.hpp"
#include "lua/modules/infobox.hpp"
#include "lua/modules/towners.hpp"
#include "monster.h"
#include "options.h"
Expand Down Expand Up @@ -291,6 +293,8 @@ void LuaInitialize()
"devilutionx.towners", LuaTownersModule(lua),
"devilutionx.hellfire", LuaHellfireModule(lua),
"devilutionx.system", LuaSystemModule(lua),
"devilutionx.gamepad", LuaGamepadModule(lua),
"devilutionx.infobox", LuaInfoBoxModule(lua),
"devilutionx.floatingnumbers", LuaFloatingNumbersModule(lua),
"devilutionx.message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); },
// This package is loaded without a sandbox:
Expand Down Expand Up @@ -355,6 +359,36 @@ void LuaEvent(std::string_view name, const Player *player, uint32_t arg1)
CallLuaEvent(name, player, arg1);
}

void LuaEvent(std::string_view name, const Item *item)
{
CallLuaEvent(name, item);
}

void LuaEvent(std::string_view name, const Item *item, bool arg1)
{
CallLuaEvent(name, item, arg1);
}

bool LuaEventHasHandlers(std::string_view name)
{
if (!CurrentLuaState.has_value()) {
return false;
}

const auto hasHandlers = CurrentLuaState->events.traverse_get<std::optional<sol::object>>(name, "hasHandlers");
if (!hasHandlers.has_value() || !hasHandlers->is<sol::protected_function>()) {
return false;
}

const sol::protected_function fn = hasHandlers->as<sol::protected_function>();
sol::object result = SafeCallResult(fn(), /*optional=*/true);
if (!result.valid() || result.get_type() != sol::type::boolean) {
return false;
}

return result.as<bool>();
}

sol::state &GetLuaState()
{
return CurrentLuaState->sol;
Expand Down
4 changes: 4 additions & 0 deletions Source/lua/lua_global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace devilution {

struct Player;
struct Monster;
struct Item;

void LuaInitialize();
void LuaReloadActiveMods();
Expand All @@ -19,6 +20,9 @@ void LuaEvent(std::string_view name, std::string_view arg);
void LuaEvent(std::string_view name, const Player *player, int arg1, int arg2);
void LuaEvent(std::string_view name, const Monster *monster, int arg1, int arg2);
void LuaEvent(std::string_view name, const Player *player, uint32_t arg1);
void LuaEvent(std::string_view name, const Item *item);
void LuaEvent(std::string_view name, const Item *item, bool arg1);
bool LuaEventHasHandlers(std::string_view name);
sol::state &GetLuaState();
sol::environment CreateLuaSandbox();
sol::object SafeCallResult(sol::protected_function_result result, bool optional);
Expand Down
Loading
Loading