From 7484ce6c2d1718cabee70027ddc01de3030cfe66 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Wed, 25 Mar 2026 19:05:10 -0700 Subject: [PATCH] refactor: extract getInventorySlotName and renderBindingType into shared UI utils Add getInventorySlotName() and renderBindingType() to ui_colors.hpp, replacing 3 copies of the 26-case slot name switch (2 inventory_screen + 1 game_screen) and 2 copies of the binding type switch. Removes ~80 lines of duplicate tooltip code. --- include/ui/ui_colors.hpp | 45 ++++++++++++++++++++++ src/ui/game_screen.cpp | 29 +------------- src/ui/inventory_screen.cpp | 76 ++----------------------------------- 3 files changed, 50 insertions(+), 100 deletions(-) diff --git a/include/ui/ui_colors.hpp b/include/ui/ui_colors.hpp index ef1e02f0..d387ad11 100644 --- a/include/ui/ui_colors.hpp +++ b/include/ui/ui_colors.hpp @@ -60,4 +60,49 @@ inline void renderCoinsFromCopper(uint64_t copper) { static_cast(copper % 100)); } +// ---- Inventory slot name from WoW inventory type ---- +inline const char* getInventorySlotName(uint32_t inventoryType) { + switch (inventoryType) { + case 1: return "Head"; + case 2: return "Neck"; + case 3: return "Shoulder"; + case 4: return "Shirt"; + case 5: return "Chest"; + case 6: return "Waist"; + case 7: return "Legs"; + case 8: return "Feet"; + case 9: return "Wrist"; + case 10: return "Hands"; + case 11: return "Finger"; + case 12: return "Trinket"; + case 13: return "One-Hand"; + case 14: return "Shield"; + case 15: return "Ranged"; + case 16: return "Back"; + case 17: return "Two-Hand"; + case 18: return "Bag"; + case 19: return "Tabard"; + case 20: return "Robe"; + case 21: return "Main Hand"; + case 22: return "Off Hand"; + case 23: return "Held In Off-hand"; + case 25: return "Thrown"; + case 26: return "Ranged"; + case 28: return "Relic"; + default: return ""; + } +} + +// ---- Binding type display ---- +inline void renderBindingType(uint32_t bindType) { + constexpr ImVec4 kBindColor = {1.0f, 0.82f, 0.0f, 1.0f}; + switch (bindType) { + case 1: ImGui::TextColored(kBindColor, "Binds when picked up"); break; + case 2: ImGui::TextColored(kBindColor, "Binds when equipped"); break; + case 3: ImGui::TextColored(kBindColor, "Binds when used"); break; + case 4: ImGui::TextColored(kBindColor, "Quest Item"); break; + default: break; + } +} + } // namespace wowee::ui diff --git a/src/ui/game_screen.cpp b/src/ui/game_screen.cpp index 6cafa6ed..f45a1e5a 100644 --- a/src/ui/game_screen.cpp +++ b/src/ui/game_screen.cpp @@ -1396,34 +1396,7 @@ void GameScreen::renderChatWindow(game::GameHandler& gameHandler) { // Slot type if (info->inventoryType > 0) { - const char* slotName = ""; - switch (info->inventoryType) { - case 1: slotName = "Head"; break; - case 2: slotName = "Neck"; break; - case 3: slotName = "Shoulder"; break; - case 4: slotName = "Shirt"; break; - case 5: slotName = "Chest"; break; - case 6: slotName = "Waist"; break; - case 7: slotName = "Legs"; break; - case 8: slotName = "Feet"; break; - case 9: slotName = "Wrist"; break; - case 10: slotName = "Hands"; break; - case 11: slotName = "Finger"; break; - case 12: slotName = "Trinket"; break; - case 13: slotName = "One-Hand"; break; - case 14: slotName = "Shield"; break; - case 15: slotName = "Ranged"; break; - case 16: slotName = "Back"; break; - case 17: slotName = "Two-Hand"; break; - case 18: slotName = "Bag"; break; - case 19: slotName = "Tabard"; break; - case 20: slotName = "Robe"; break; - case 21: slotName = "Main Hand"; break; - case 22: slotName = "Off Hand"; break; - case 23: slotName = "Held In Off-hand"; break; - case 25: slotName = "Thrown"; break; - case 26: slotName = "Ranged"; break; - } + const char* slotName = ui::getInventorySlotName(info->inventoryType); if (slotName[0]) { if (!info->subclassName.empty()) ImGui::TextColored(ui::colors::kLightGray, "%s %s", slotName, info->subclassName.c_str()); diff --git a/src/ui/inventory_screen.cpp b/src/ui/inventory_screen.cpp index 4f087bd1..e3495c1d 100644 --- a/src/ui/inventory_screen.cpp +++ b/src/ui/inventory_screen.cpp @@ -2562,13 +2562,7 @@ void InventoryScreen::renderItemTooltip(const game::ItemDef& item, const game::I } // Binding type - switch (item.bindType) { - case 1: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Binds when picked up"); break; - case 2: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Binds when equipped"); break; - case 3: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Binds when used"); break; - case 4: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Quest Item"); break; - default: break; - } + ui::renderBindingType(item.bindType); if (item.itemId == 6948 && gameHandler_) { uint32_t mapId = 0; @@ -2600,35 +2594,7 @@ void InventoryScreen::renderItemTooltip(const game::ItemDef& item, const game::I // Slot type if (item.inventoryType > 0) { - const char* slotName = ""; - switch (item.inventoryType) { - case 1: slotName = "Head"; break; - case 2: slotName = "Neck"; break; - case 3: slotName = "Shoulder"; break; - case 4: slotName = "Shirt"; break; - case 5: slotName = "Chest"; break; - case 6: slotName = "Waist"; break; - case 7: slotName = "Legs"; break; - case 8: slotName = "Feet"; break; - case 9: slotName = "Wrist"; break; - case 10: slotName = "Hands"; break; - case 11: slotName = "Finger"; break; - case 12: slotName = "Trinket"; break; - case 13: slotName = "One-Hand"; break; - case 14: slotName = "Shield"; break; - case 15: slotName = "Ranged"; break; - case 16: slotName = "Back"; break; - case 17: slotName = "Two-Hand"; break; - case 18: slotName = "Bag"; break; - case 19: slotName = "Tabard"; break; - case 20: slotName = "Robe"; break; - case 21: slotName = "Main Hand"; break; - case 22: slotName = "Off Hand"; break; - case 23: slotName = "Held In Off-hand"; break; - case 25: slotName = "Thrown"; break; - case 26: slotName = "Ranged"; break; - default: slotName = ""; break; - } + const char* slotName = ui::getInventorySlotName(item.inventoryType); if (slotName[0]) { if (!item.subclassName.empty()) { ImGui::TextColored(ui::colors::kLightGray, "%s %s", slotName, item.subclassName.c_str()); @@ -3240,45 +3206,11 @@ void InventoryScreen::renderItemTooltip(const game::ItemQueryResponseData& info, } // Binding type - switch (info.bindType) { - case 1: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Binds when picked up"); break; - case 2: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Binds when equipped"); break; - case 3: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Binds when used"); break; - case 4: ImGui::TextColored(ImVec4(1.0f, 0.82f, 0.0f, 1.0f), "Quest Item"); break; - default: break; - } + ui::renderBindingType(info.bindType); // Slot / subclass if (info.inventoryType > 0) { - const char* slotName = ""; - switch (info.inventoryType) { - case 1: slotName = "Head"; break; - case 2: slotName = "Neck"; break; - case 3: slotName = "Shoulder"; break; - case 4: slotName = "Shirt"; break; - case 5: slotName = "Chest"; break; - case 6: slotName = "Waist"; break; - case 7: slotName = "Legs"; break; - case 8: slotName = "Feet"; break; - case 9: slotName = "Wrist"; break; - case 10: slotName = "Hands"; break; - case 11: slotName = "Finger"; break; - case 12: slotName = "Trinket"; break; - case 13: slotName = "One-Hand"; break; - case 14: slotName = "Shield"; break; - case 15: slotName = "Ranged"; break; - case 16: slotName = "Back"; break; - case 17: slotName = "Two-Hand"; break; - case 18: slotName = "Bag"; break; - case 19: slotName = "Tabard"; break; - case 20: slotName = "Robe"; break; - case 21: slotName = "Main Hand"; break; - case 22: slotName = "Off Hand"; break; - case 23: slotName = "Held In Off-hand"; break; - case 25: slotName = "Thrown"; break; - case 26: slotName = "Ranged"; break; - default: break; - } + const char* slotName = ui::getInventorySlotName(info.inventoryType); if (slotName[0]) { if (!info.subclassName.empty()) ImGui::TextColored(ui::colors::kLightGray, "%s %s", slotName, info.subclassName.c_str());