Merge branch 'fix_load_quick_keys' into 'master'

Fix load quick keys (#7286)

Closes #7286

See merge request OpenMW/openmw!2850
depth-refraction
psi29a 2 years ago
commit c6d6747d67

@ -76,12 +76,12 @@ namespace MWGui
MWWorld::InventoryStore& store = player.getClass().getInventoryStore(player);
switch (mKey[index].type)
{
case Type_Unassigned:
case Type_HandToHand:
case Type_Magic:
case ESM::QuickKeys::Type::Unassigned:
case ESM::QuickKeys::Type::HandToHand:
case ESM::QuickKeys::Type::Magic:
break;
case Type_Item:
case Type_MagicItem:
case ESM::QuickKeys::Type::Item:
case ESM::QuickKeys::Type::MagicItem:
{
MWWorld::Ptr item = *mKey[index].button->getUserData<MWWorld::Ptr>();
// Make sure the item is available and is not broken
@ -133,7 +133,7 @@ namespace MWGui
if (key->index == 10)
{
key->type = Type_HandToHand;
key->type = ESM::QuickKeys::Type::HandToHand;
MyGUI::ImageBox* image = key->button->createWidget<MyGUI::ImageBox>(
"ImageBox", MyGUI::IntCoord(14, 13, 32, 32), MyGUI::Align::Default);
@ -143,7 +143,7 @@ namespace MWGui
}
else
{
key->type = Type_Unassigned;
key->type = ESM::QuickKeys::Type::Unassigned;
key->id = ESM::RefId();
key->name.clear();
@ -176,7 +176,7 @@ namespace MWGui
mSelected = &mKey[index];
// prevent reallocation of zero key from Type_HandToHand
// prevent reallocation of zero key from ESM::QuickKeys::Type::HandToHand
if (mSelected->index == 10)
return;
@ -236,7 +236,7 @@ namespace MWGui
while (mSelected->button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(mSelected->button->getChildAt(0));
mSelected->type = Type_Item;
mSelected->type = ESM::QuickKeys::Type::Item;
mSelected->id = item.getCellRef().getRefId();
mSelected->name = item.getClass().getName(item);
@ -260,7 +260,7 @@ namespace MWGui
while (mSelected->button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(mSelected->button->getChildAt(0));
mSelected->type = Type_MagicItem;
mSelected->type = ESM::QuickKeys::Type::MagicItem;
mSelected->id = item.getCellRef().getRefId();
mSelected->name = item.getClass().getName(item);
@ -290,7 +290,7 @@ namespace MWGui
const MWWorld::ESMStore& esmStore = MWBase::Environment::get().getWorld()->getStore();
const ESM::Spell* spell = esmStore.get<ESM::Spell>().find(spellId);
mSelected->type = Type_Magic;
mSelected->type = ESM::QuickKeys::Type::Magic;
mSelected->id = spellId;
mSelected->name = spell->mName;
@ -369,7 +369,7 @@ namespace MWGui
mActivated = nullptr;
}
if (key->type == Type_Item || key->type == Type_MagicItem)
if (key->type == ESM::QuickKeys::Type::Item || key->type == ESM::QuickKeys::Type::MagicItem)
{
MWWorld::Ptr item = *key->button->getUserData<MWWorld::Ptr>();
@ -396,7 +396,7 @@ namespace MWGui
}
}
if (key->type == Type_Item)
if (key->type == ESM::QuickKeys::Type::Item)
{
if (!store.isEquipped(item))
MWBase::Environment::get().getWindowManager()->useItem(item);
@ -408,7 +408,7 @@ namespace MWGui
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState::Weapon);
}
}
else if (key->type == Type_MagicItem)
else if (key->type == ESM::QuickKeys::Type::MagicItem)
{
// equip, if it can be equipped and isn't yet equipped
if (!item.getClass().getEquipmentSlots(item).first.empty() && !store.isEquipped(item))
@ -424,7 +424,7 @@ namespace MWGui
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState::Spell);
}
}
else if (key->type == Type_Magic)
else if (key->type == ESM::QuickKeys::Type::Magic)
{
const ESM::RefId& spellId = key->id;
@ -443,7 +443,7 @@ namespace MWGui
spellId, int(MWMechanics::getSpellSuccessChance(spellId, player)));
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState::Spell);
}
else if (key->type == Type_HandToHand)
else if (key->type == ESM::QuickKeys::Type::HandToHand)
{
store.unequipSlot(MWWorld::InventoryStore::Slot_CarriedRight);
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState::Weapon);
@ -499,24 +499,24 @@ namespace MWGui
{
ItemWidget* button = mKey[i].button;
int type = mKey[i].type;
const ESM::QuickKeys::Type type = mKey[i].type;
ESM::QuickKeys::QuickKey key;
key.mType = type;
switch (type)
{
case Type_Unassigned:
case Type_HandToHand:
case ESM::QuickKeys::Type::Unassigned:
case ESM::QuickKeys::Type::HandToHand:
break;
case Type_Item:
case Type_MagicItem:
case ESM::QuickKeys::Type::Item:
case ESM::QuickKeys::Type::MagicItem:
{
MWWorld::Ptr item = *button->getUserData<MWWorld::Ptr>();
key.mId = item.getCellRef().getRefId();
break;
}
case Type_Magic:
case ESM::QuickKeys::Type::Magic:
key.mId = ESM::RefId::stringRefId(button->getUserString("Spell"));
break;
}
@ -551,12 +551,12 @@ namespace MWGui
switch (quickKey.mType)
{
case Type_Magic:
case ESM::QuickKeys::Type::Magic:
if (MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(quickKey.mId))
onAssignMagic(quickKey.mId);
break;
case Type_Item:
case Type_MagicItem:
case ESM::QuickKeys::Type::Item:
case ESM::QuickKeys::Type::MagicItem:
{
// Find the item by id
MWWorld::Ptr item = store.findReplacement(quickKey.mId);
@ -565,16 +565,16 @@ namespace MWGui
unassign(mSelected);
else
{
if (quickKey.mType == Type_Item)
if (quickKey.mType == ESM::QuickKeys::Type::Item)
onAssignItem(item);
else // if (quickKey.mType == Type_MagicItem)
else // if (quickKey.mType == ESM::QuickKeys::Type::MagicItem)
onAssignMagicItem(item);
}
break;
}
case Type_Unassigned:
case Type_HandToHand:
case ESM::QuickKeys::Type::Unassigned:
case ESM::QuickKeys::Type::HandToHand:
unassign(mSelected);
break;
}

@ -3,6 +3,8 @@
#include <memory>
#include "components/esm3/quickkeys.hpp"
#include "itemselection.hpp"
#include "spellmodel.hpp"
#include "windowbase.hpp"
@ -38,16 +40,6 @@ namespace MWGui
void activateQuickKey(int index);
void updateActivatedQuickKey();
/// @note This enum is serialized, so don't move the items around!
enum QuickKeyType
{
Type_Item,
Type_Magic,
Type_MagicItem,
Type_Unassigned,
Type_HandToHand
};
void write(ESM::ESMWriter& writer);
void readRecord(ESM::ESMReader& reader, uint32_t type);
void clear() override;
@ -57,7 +49,7 @@ namespace MWGui
{
int index = -1;
ItemWidget* button = nullptr;
QuickKeysMenu::QuickKeyType type = Type_Unassigned;
ESM::QuickKeys::Type type = ESM::QuickKeys::Type::Unassigned;
ESM::RefId id;
std::string name;
};

@ -5,6 +5,7 @@
#include <components/esm3/loadregn.hpp>
#include <components/esm3/loadscpt.hpp>
#include <components/esm3/player.hpp>
#include <components/esm3/quickkeys.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@ -27,6 +28,11 @@ namespace ESM
{
return std::tie(value.mSound, value.mChance);
}
auto tie(const ESM::QuickKeys::QuickKey& value)
{
return std::tie(value.mType, value.mId);
}
}
inline bool operator==(const ESM::ContItem& lhs, const ESM::ContItem& rhs)
@ -50,6 +56,17 @@ namespace ESM
<< "}";
}
inline bool operator==(const ESM::QuickKeys::QuickKey& lhs, const ESM::QuickKeys::QuickKey& rhs)
{
return tie(lhs) == tie(rhs);
}
inline std::ostream& operator<<(std::ostream& stream, const ESM::QuickKeys::QuickKey& value)
{
return stream << "ESM::QuickKeys::QuickKey {.mType = '" << static_cast<std::uint32_t>(value.mType)
<< "', .mId = " << value.mId << "}";
}
namespace
{
using namespace ::testing;
@ -291,6 +308,25 @@ namespace ESM
EXPECT_EQ(result.mData.mNumShorts, record.mData.mNumShorts);
}
TEST_P(Esm3SaveLoadRecordTest, quickKeysShouldNotChange)
{
const QuickKeys record {
.mKeys = {
{
.mType = QuickKeys::Type::Magic,
.mId = generateRandomRefId(32),
},
{
.mType = QuickKeys::Type::MagicItem,
.mId = generateRandomRefId(32),
},
},
};
QuickKeys result;
saveAndLoadRecord(record, GetParam(), result);
EXPECT_EQ(result.mKeys, record.mKeys);
}
INSTANTIATE_TEST_SUITE_P(FormatVersions, Esm3SaveLoadRecordTest, ValuesIn(formats));
}
}

@ -13,14 +13,9 @@ namespace ESM
while (esm.isNextSub("TYPE"))
{
int keyType;
esm.getHT(keyType);
std::string id;
id = esm.getHNString("ID__");
QuickKey key;
key.mType = keyType;
key.mId = ESM::RefId::stringRefId(id);
esm.getHT(key.mType);
key.mId = esm.getHNRefId("ID__");
mKeys.push_back(key);
@ -31,10 +26,10 @@ namespace ESM
void QuickKeys::save(ESMWriter& esm) const
{
for (std::vector<QuickKey>::const_iterator it = mKeys.begin(); it != mKeys.end(); ++it)
for (const QuickKey& key : mKeys)
{
esm.writeHNT("TYPE", it->mType);
esm.writeHNRefId("ID__", it->mId);
esm.writeHNT("TYPE", key.mType);
esm.writeHNRefId("ID__", key.mId);
}
}

@ -2,6 +2,7 @@
#define OPENMW_COMPONENTS_ESM_QUICKKEYS_H
#include "components/esm/refid.hpp"
#include <string>
#include <vector>
@ -12,9 +13,18 @@ namespace ESM
struct QuickKeys
{
enum class Type : std::uint32_t
{
Item = 0,
Magic = 1,
MagicItem = 2,
Unassigned = 3,
HandToHand = 4,
};
struct QuickKey
{
int mType;
Type mType;
RefId mId; // Spell or Item ID
};

Loading…
Cancel
Save