Skip to content

Commit

Permalink
Started getting item equipping working.
Browse files Browse the repository at this point in the history
  • Loading branch information
afritz1 committed Nov 15, 2024
1 parent f27438e commit 9c4b703
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 15 deletions.
19 changes: 18 additions & 1 deletion OpenTESArena/src/Interface/CharacterEquipmentPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ bool CharacterEquipmentPanel::init()
auto &pair = elements[i];
this->inventoryListBox.add(std::move(pair.first));
this->inventoryListBox.setOverrideColor(i, pair.second);
this->inventoryListBox.setCallback(i,
[this, &game, i]()
{
ItemInventory &playerInventory = game.player.inventory;
ItemInstance &itemInst = playerInventory.getSlot(i);
itemInst.isEquipped = !itemInst.isEquipped;

const Color &equipColor = InventoryUiView::getItemDisplayColor(itemInst);
this->inventoryListBox.setOverrideColor(i, equipColor);
});

this->addButtonProxy(MouseButtonType::Left, this->inventoryListBox.getItemGlobalRect(i),
[this, i]()
{
const int firstVisibleIndex = this->inventoryListBox.getFirstVisibleItemIndex();
this->inventoryListBox.getCallback(firstVisibleIndex + i)();
});
}

this->backToStatsButton = Button<Game&>(
Expand Down Expand Up @@ -107,7 +124,7 @@ bool CharacterEquipmentPanel::init()
this->addButtonProxy(MouseButtonType::Left, this->dropButton.getRect(),
[this, &game]()
{
// @todo: give the index of the clicked item instead.
// @todo: give the index of the currently selected item instead.
const int itemIndex = 0;
this->dropButton.click(game, itemIndex);
});
Expand Down
23 changes: 9 additions & 14 deletions OpenTESArena/src/Interface/InventoryUiModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,22 @@ Buffer<InventoryUiModel::ItemUiDefinition> InventoryUiModel::getPlayerInventoryI
{
const ItemLibrary &itemLibrary = ItemLibrary::getInstance();
const Player &player = game.player;

std::vector<const ItemDefinition*> itemDefs;
for (int i = 0; i < player.inventory.getTotalSlotCount(); i++)
const ItemInventory &playerInventory = player.inventory;
const int itemCount = playerInventory.getTotalSlotCount();
Buffer<ItemUiDefinition> buffer(itemCount);
for (int i = 0; i < itemCount; i++)
{
const ItemInstance &itemInst = player.inventory.getSlot(i);
if (itemInst.isValid())
const ItemInstance &itemInst = playerInventory.getSlot(i);
if (!itemInst.isValid())
{
const ItemDefinition &itemDef = itemLibrary.getDefinition(itemInst.defID);
itemDefs.emplace_back(&itemDef);
continue;
}
}

const int elementCount = static_cast<int>(itemDefs.size());
Buffer<ItemUiDefinition> buffer(elementCount);
for (int i = 0; i < elementCount; i++)
{
const ItemDefinition &itemDef = *itemDefs[i];
const ItemDefinition &itemDef = itemLibrary.getDefinition(itemInst.defID);

char itemDisplayName[64];
std::snprintf(std::begin(itemDisplayName), std::size(itemDisplayName), "%s (%.1fkg)", itemDef.getDisplayName().c_str(), itemDef.getWeight());
const Color &itemTextColor = InventoryUiView::PlayerInventoryEquipmentColor;
const Color &itemTextColor = InventoryUiView::getItemDisplayColor(itemInst);

ItemUiDefinition itemUiDef;
itemUiDef.init(itemDisplayName, itemTextColor);
Expand Down
15 changes: 15 additions & 0 deletions OpenTESArena/src/Interface/InventoryUiView.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "InventoryUiView.h"
#include "../Items/ItemInstance.h"
#include "../UI/ArenaFontName.h"
#include "../UI/FontLibrary.h"
#include "../UI/TextRenderUtils.h"
Expand Down Expand Up @@ -34,3 +35,17 @@ ListBox::Properties InventoryUiView::makePlayerInventoryListBoxProperties(const
return ListBox::Properties(fontDefIndex, &fontLibrary, textureGenInfo, fontDef.getCharacterHeight(),
InventoryUiView::PlayerInventoryEquipmentColor, scrollScale, rowSpacing);
}

const Color &InventoryUiView::getItemDisplayColor(const ItemInstance &itemInst)
{
// @todo magic items

// @todo: class non-equippable items

if (itemInst.isEquipped)
{
return InventoryUiView::PlayerInventoryEquipmentEquippedColor;
}

return InventoryUiView::PlayerInventoryEquipmentColor;
}
4 changes: 4 additions & 0 deletions OpenTESArena/src/Interface/InventoryUiView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class FontLibrary;

struct ItemInstance;

namespace InventoryUiView
{
const Rect PlayerInventoryRect(14, 50, 150, 75);
Expand All @@ -18,6 +20,8 @@ namespace InventoryUiView
const Color PlayerInventoryUnequipableColor(199, 32, 0);

ListBox::Properties makePlayerInventoryListBoxProperties(const FontLibrary &fontLibrary);

const Color &getItemDisplayColor(const ItemInstance &itemInst);
}

#endif
2 changes: 2 additions & 0 deletions OpenTESArena/src/Items/ItemInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
ItemInstance::ItemInstance()
{
this->defID = -1;
this->isEquipped = false;
}

void ItemInstance::init(ItemDefinitionID defID)
{
this->defID = defID;
this->isEquipped = false;
}

bool ItemInstance::isValid() const
Expand Down
1 change: 1 addition & 0 deletions OpenTESArena/src/Items/ItemInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
struct ItemInstance
{
ItemDefinitionID defID;
bool isEquipped;

ItemInstance();

Expand Down
5 changes: 5 additions & 0 deletions OpenTESArena/src/UI/ListBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ const ListBox::ItemCallback &ListBox::getCallback(int index) const
return this->items[index].callback;
}

int ListBox::getFirstVisibleItemIndex() const
{
return static_cast<int>(this->scrollPixelOffset) / (this->properties.itemHeight + this->properties.itemSpacing);
}

UiTextureID ListBox::getTextureID()
{
if (this->dirty)
Expand Down
2 changes: 2 additions & 0 deletions OpenTESArena/src/UI/ListBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class ListBox
// falls within the list box rect.
const ItemCallback &getCallback(int index) const;

int getFirstVisibleItemIndex() const;

UiTextureID getTextureID();

void insert(int index, std::string &&text);
Expand Down

0 comments on commit 9c4b703

Please sign in to comment.