1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-30 21:15:33 +00:00

switch mSelectedIndex/mActivatedIndex int to mSelected/mActivated keyData pointers

This commit is contained in:
Finbar Crago 2018-06-28 17:02:25 +10:00
parent 335e2c5897
commit 80a3f0a0d4
2 changed files with 90 additions and 89 deletions

View file

@ -33,11 +33,13 @@ namespace MWGui
QuickKeysMenu::QuickKeysMenu() QuickKeysMenu::QuickKeysMenu()
: WindowBase("openmw_quickkeys_menu.layout") : WindowBase("openmw_quickkeys_menu.layout")
, mKey(std::vector<struct keyData>(10))
, mSelected(nullptr)
, mActivated(nullptr)
, mAssignDialog(0) , mAssignDialog(0)
, mItemSelectionDialog(0) , mItemSelectionDialog(0)
, mMagicSelectionDialog(0) , mMagicSelectionDialog(0)
, mSelectedIndex(-1)
, mActivatedIndex(-1)
{ {
getWidget(mOkButton, "OKButton"); getWidget(mOkButton, "OKButton");
getWidget(mInstructionLabel, "InstructionLabel"); getWidget(mInstructionLabel, "InstructionLabel");
@ -49,8 +51,6 @@ namespace MWGui
mOkButton->eventMouseButtonClick += MyGUI::newDelegate(this, &QuickKeysMenu::onOkButtonClicked); mOkButton->eventMouseButtonClick += MyGUI::newDelegate(this, &QuickKeysMenu::onOkButtonClicked);
center(); center();
mKey = std::vector<struct keyData>(10);
for (int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
mKey[i].index = i; mKey[i].index = i;
@ -63,7 +63,7 @@ namespace MWGui
void QuickKeysMenu::clear() void QuickKeysMenu::clear()
{ {
mActivatedIndex = -1; mActivated = nullptr;
for (int i=0; i<10; ++i) for (int i=0; i<10; ++i)
{ {
@ -120,7 +120,7 @@ namespace MWGui
key->button->clearUserStrings(); key->button->clearUserStrings();
key->button->setItem(MWWorld::Ptr()); key->button->setItem(MWWorld::Ptr());
while(key->button->getChildCount()) // Destroy number label while (key->button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(key->button->getChildAt(0)); MyGUI::Gui::getInstance().destroyWidget(key->button->getChildAt(0));
if (key->index == 9) if (key->index == 9)
@ -151,19 +151,19 @@ namespace MWGui
void QuickKeysMenu::onQuickKeyButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onQuickKeyButtonClicked(MyGUI::Widget* sender)
{ {
int index = -1; int index = -1;
for(int i = 0; i < 10; ++i) for (int i = 0; i < 10; ++i)
{ {
if(sender == mKey[i].button || sender->getParent() == mKey[i].button) if (sender == mKey[i].button || sender->getParent() == mKey[i].button)
{ {
index = i; index = i;
break; break;
} }
} }
assert(index != -1); assert(index != -1);
mSelectedIndex = index; mSelected = &mKey[index];
// open assign dialog // open assign dialog
if(!mAssignDialog) if (!mAssignDialog)
mAssignDialog = new QuickKeysMenuAssign(this); mAssignDialog = new QuickKeysMenuAssign(this);
mAssignDialog->setVisible(true); mAssignDialog->setVisible(true);
@ -176,7 +176,7 @@ namespace MWGui
void QuickKeysMenu::onItemButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onItemButtonClicked(MyGUI::Widget* sender)
{ {
if (!mItemSelectionDialog ) if (!mItemSelectionDialog)
{ {
mItemSelectionDialog = new ItemSelectionDialog("#{sQuickMenu6}"); mItemSelectionDialog = new ItemSelectionDialog("#{sQuickMenu6}");
mItemSelectionDialog->eventItemSelected += MyGUI::newDelegate(this, &QuickKeysMenu::onAssignItem); mItemSelectionDialog->eventItemSelected += MyGUI::newDelegate(this, &QuickKeysMenu::onAssignItem);
@ -186,12 +186,12 @@ namespace MWGui
mItemSelectionDialog->openContainer(MWMechanics::getPlayer()); mItemSelectionDialog->openContainer(MWMechanics::getPlayer());
mItemSelectionDialog->setFilter(SortFilterItemModel::Filter_OnlyUsableItems); mItemSelectionDialog->setFilter(SortFilterItemModel::Filter_OnlyUsableItems);
mAssignDialog->setVisible (false); mAssignDialog->setVisible(false);
} }
void QuickKeysMenu::onMagicButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onMagicButtonClicked(MyGUI::Widget* sender)
{ {
if(!mMagicSelectionDialog) if (!mMagicSelectionDialog)
{ {
mMagicSelectionDialog = new MagicSelectionDialog(this); mMagicSelectionDialog = new MagicSelectionDialog(this);
} }
@ -202,7 +202,7 @@ namespace MWGui
void QuickKeysMenu::onUnassignButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onUnassignButtonClicked(MyGUI::Widget* sender)
{ {
unassign(&mKey[mSelectedIndex]); unassign(mSelected);
mAssignDialog->setVisible(false); mAssignDialog->setVisible(false);
} }
@ -213,20 +213,20 @@ namespace MWGui
void QuickKeysMenu::onAssignItem(MWWorld::Ptr item) void QuickKeysMenu::onAssignItem(MWWorld::Ptr item)
{ {
assert(mSelectedIndex >= 0); assert(mSelected);
while(mKey[mSelectedIndex].button->getChildCount()) // Destroy number label while (mSelected->button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(mKey[mSelectedIndex].button->getChildAt(0)); MyGUI::Gui::getInstance().destroyWidget(mSelected->button->getChildAt(0));
mKey[mSelectedIndex].type = Type_Item; mSelected->type = Type_Item;
mKey[mSelectedIndex].id = item.getCellRef().getRefId(); mSelected->id = item.getCellRef().getRefId();
mKey[mSelectedIndex].name = item.getClass().getName(item); mSelected->name = item.getClass().getName(item);
mKey[mSelectedIndex].button->setItem(item, ItemWidget::Barter); mSelected->button->setItem(item, ItemWidget::Barter);
mKey[mSelectedIndex].button->setUserString("ToolTipType", "ItemPtr"); mSelected->button->setUserString("ToolTipType", "ItemPtr");
mKey[mSelectedIndex].button->setUserData(item); mSelected->button->setUserData(item);
if(mItemSelectionDialog) if (mItemSelectionDialog)
mItemSelectionDialog->setVisible(false); mItemSelectionDialog->setVisible(false);
} }
@ -237,38 +237,37 @@ namespace MWGui
void QuickKeysMenu::onAssignMagicItem(MWWorld::Ptr item) void QuickKeysMenu::onAssignMagicItem(MWWorld::Ptr item)
{ {
assert(mSelectedIndex >= 0); assert(mSelected);
while(mKey[mSelectedIndex].button->getChildCount()) // Destroy number label while (mSelected->button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(mKey[mSelectedIndex].button->getChildAt(0)); MyGUI::Gui::getInstance().destroyWidget(mSelected->button->getChildAt(0));
mKey[mSelectedIndex].type = Type_MagicItem; mSelected->type = Type_MagicItem;
mKey[mSelectedIndex].button->setFrame("textures\\menu_icon_select_magic_magic.dds", MyGUI::IntCoord(2, 2, 40, 40)); mSelected->button->setFrame("textures\\menu_icon_select_magic_magic.dds", MyGUI::IntCoord(2, 2, 40, 40));
mKey[mSelectedIndex].button->setIcon(item); mSelected->button->setIcon(item);
mKey[mSelectedIndex].button->setUserString ("ToolTipType", "ItemPtr"); mSelected->button->setUserString("ToolTipType", "ItemPtr");
mKey[mSelectedIndex].button->setUserData(MWWorld::Ptr(item)); mSelected->button->setUserData(MWWorld::Ptr(item));
if(mMagicSelectionDialog) if (mMagicSelectionDialog)
mMagicSelectionDialog->setVisible(false); mMagicSelectionDialog->setVisible(false);
} }
void QuickKeysMenu::onAssignMagic(const std::string& spellId) void QuickKeysMenu::onAssignMagic(const std::string& spellId)
{ {
assert(mSelectedIndex >= 0); assert(mSelected);
ItemWidget* button = mKey[mSelectedIndex].button; while (mSelected->button->getChildCount()) // Destroy number label
while(mKey[mSelectedIndex].button->getChildCount()) // Destroy number label MyGUI::Gui::getInstance().destroyWidget(mSelected->button->getChildAt(0));
MyGUI::Gui::getInstance().destroyWidget(button->getChildAt(0));
mKey[mSelectedIndex].type = Type_Magic; mSelected->type = Type_Magic;
mSelected->id = spellId;
mKey[mSelectedIndex].button->setItem(MWWorld::Ptr()); mSelected->button->setItem(MWWorld::Ptr());
mKey[mSelectedIndex].button->setUserString("ToolTipType", "Spell"); mSelected->button->setUserString("ToolTipType", "Spell");
mKey[mSelectedIndex].button->setUserString("Spell", spellId); mSelected->button->setUserString("Spell", spellId);
const MWWorld::ESMStore &esmStore = const MWWorld::ESMStore &esmStore = MWBase::Environment::get().getWorld()->getStore();
MWBase::Environment::get().getWorld()->getStore();
// use the icon of the first effect // use the icon of the first effect
const ESM::Spell* spell = esmStore.get<ESM::Spell>().find(spellId); const ESM::Spell* spell = esmStore.get<ESM::Spell>().find(spellId);
@ -281,14 +280,14 @@ namespace MWGui
path.insert(slashPos+1, "b_"); path.insert(slashPos+1, "b_");
path = MWBase::Environment::get().getWindowManager()->correctIconPath(path); path = MWBase::Environment::get().getWindowManager()->correctIconPath(path);
button->setFrame("textures\\menu_icon_select_magic.dds", MyGUI::IntCoord(2, 2, 40, 40)); mSelected->button->setFrame("textures\\menu_icon_select_magic.dds", MyGUI::IntCoord(2, 2, 40, 40));
button->setIcon(path); mSelected->button->setIcon(path);
if (mMagicSelectionDialog) if (mMagicSelectionDialog)
mMagicSelectionDialog->setVisible(false); mMagicSelectionDialog->setVisible(false);
} }
void QuickKeysMenu::onAssignMagicCancel () void QuickKeysMenu::onAssignMagicCancel()
{ {
mMagicSelectionDialog->setVisible(false); mMagicSelectionDialog->setVisible(false);
} }
@ -296,17 +295,16 @@ namespace MWGui
void QuickKeysMenu::updateActivatedQuickKey() void QuickKeysMenu::updateActivatedQuickKey()
{ {
// there is no delayed action, nothing to do. // there is no delayed action, nothing to do.
if (mActivatedIndex < 0) if (!mActivated)
return; return;
activateQuickKey(mActivatedIndex); activateQuickKey(mActivated->index);
} }
void QuickKeysMenu::activateQuickKey(int index) void QuickKeysMenu::activateQuickKey(int index)
{ {
assert(index-1 >= 0); assert(index > 0);
ItemWidget* button = mKey[index-1].button; struct keyData *key = &mKey[index-1];
QuickKeyType type = mKey[index-1].type;
MWWorld::Ptr player = MWMechanics::getPlayer(); MWWorld::Ptr player = MWMechanics::getPlayer();
MWWorld::InventoryStore& store = player.getClass().getInventoryStore(player); MWWorld::InventoryStore& store = player.getClass().getInventoryStore(player);
@ -319,22 +317,23 @@ namespace MWGui
|| playerStats.getHitRecovery(); || playerStats.getHitRecovery();
bool isReturnNeeded = playerStats.isParalyzed() || playerStats.isDead(); bool isReturnNeeded = playerStats.isParalyzed() || playerStats.isDead();
if (isReturnNeeded && type != Type_Item)
if (isReturnNeeded && key->type != Type_Item)
{ {
return; return;
} }
else if(isDelayNeeded && key->type != Type_Item)
if (isDelayNeeded && type != Type_Item)
{ {
mActivatedIndex = index; mActivated = key;
return; return;
} }
else else
mActivatedIndex = -1; mActivated = nullptr;
if (type == Type_Item || type == Type_MagicItem)
if (key->type == Type_Item || key->type == Type_MagicItem)
{ {
MWWorld::Ptr item = *button->getUserData<MWWorld::Ptr>(); MWWorld::Ptr item = *key->button->getUserData<MWWorld::Ptr>();
MWWorld::ContainerStoreIterator it = store.begin(); MWWorld::ContainerStoreIterator it = store.begin();
for (; it != store.end(); ++it) for (; it != store.end(); ++it)
@ -349,17 +348,18 @@ namespace MWGui
if (!item || item.getRefData().getCount() < 1 || if (!item || item.getRefData().getCount() < 1 ||
(item.getClass().hasItemHealth(item) && item.getClass().getItemHealth(item) <= 0)) (item.getClass().hasItemHealth(item) && item.getClass().getItemHealth(item) <= 0))
{ {
item = store.findReplacement(mKey[index-1].id); item = store.findReplacement(key->id);
if (!item || item.getRefData().getCount() < 1) if (!item || item.getRefData().getCount() < 1)
{ {
MWBase::Environment::get().getWindowManager()->messageBox( MWBase::Environment::get().getWindowManager()->messageBox(
"#{sQuickMenu5} " + mKey[index-1].name); "#{sQuickMenu5} " + key->name);
return; return;
} }
} }
if (type == Type_Item) if (key->type == Type_Item)
{ {
bool isWeapon = item.getTypeName() == typeid(ESM::Weapon).name(); bool isWeapon = item.getTypeName() == typeid(ESM::Weapon).name();
bool isTool = item.getTypeName() == typeid(ESM::Probe).name() || bool isTool = item.getTypeName() == typeid(ESM::Probe).name() ||
@ -368,12 +368,11 @@ namespace MWGui
// delay weapon switching if player is busy // delay weapon switching if player is busy
if (isDelayNeeded && (isWeapon || isTool)) if (isDelayNeeded && (isWeapon || isTool))
{ {
mActivatedIndex = index; mActivated = key;
return; return;
} }
// disable weapon switching if player is dead or paralyzed // disable weapon switching if player is dead or paralyzed
if (isReturnNeeded && (isWeapon || isTool)) else if (isReturnNeeded && (isWeapon || isTool))
{ {
return; return;
} }
@ -386,7 +385,7 @@ namespace MWGui
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Weapon); MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Weapon);
} }
} }
else if (type == Type_MagicItem) else if (key->type == Type_MagicItem)
{ {
// equip, if it can be equipped // equip, if it can be equipped
if (!item.getClass().getEquipmentSlots(item).first.empty()) if (!item.getClass().getEquipmentSlots(item).first.empty())
@ -402,25 +401,28 @@ namespace MWGui
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Spell); MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Spell);
} }
} }
else if (type == Type_Magic) else if (key->type == Type_Magic)
{ {
std::string spellId = button->getUserString("Spell"); std::string spellId = key->id;
// Make sure the player still has this spell // Make sure the player still has this spell
MWMechanics::CreatureStats& stats = player.getClass().getCreatureStats(player); MWMechanics::CreatureStats& stats = player.getClass().getCreatureStats(player);
MWMechanics::Spells& spells = stats.getSpells(); MWMechanics::Spells& spells = stats.getSpells();
if (!spells.hasSpell(spellId)) if (!spells.hasSpell(spellId))
{ {
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(spellId); const ESM::Spell* spell =
MWBase::Environment::get().getWindowManager()->messageBox ( MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(spellId);
"#{sQuickMenu5} " + spell->mName); MWBase::Environment::get().getWindowManager()->messageBox("#{sQuickMenu5} " + spell->mName);
return; return;
} }
store.setSelectedEnchantItem(store.end()); store.setSelectedEnchantItem(store.end());
MWBase::Environment::get().getWindowManager()->setSelectedSpell(spellId, int(MWMechanics::getSpellSuccessChance(spellId, player))); MWBase::Environment::get().getWindowManager()
->setSelectedSpell(spellId, int(MWMechanics::getSpellSuccessChance(spellId, player)));
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Spell); MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Spell);
} }
else if (type == Type_HandToHand) else if (key->type == Type_HandToHand)
{ {
store.unequipSlot(MWWorld::InventoryStore::Slot_CarriedRight, player); store.unequipSlot(MWWorld::InventoryStore::Slot_CarriedRight, player);
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Weapon); MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Weapon);
@ -517,7 +519,7 @@ namespace MWGui
void QuickKeysMenu::readRecord(ESM::ESMReader &reader, uint32_t type) void QuickKeysMenu::readRecord(ESM::ESMReader &reader, uint32_t type)
{ {
if(type != ESM::REC_KEYS) if (type != ESM::REC_KEYS)
return; return;
ESM::QuickKeys keys; ESM::QuickKeys keys;
@ -527,34 +529,34 @@ namespace MWGui
MWWorld::InventoryStore& store = player.getClass().getInventoryStore(player); MWWorld::InventoryStore& store = player.getClass().getInventoryStore(player);
int i=0; int i=0;
for(std::vector<ESM::QuickKeys::QuickKey>::const_iterator it = keys.mKeys.begin(); it != keys.mKeys.end(); ++it) for (std::vector<ESM::QuickKeys::QuickKey>::const_iterator it = keys.mKeys.begin(); it != keys.mKeys.end(); ++it)
{ {
if(i >= 10) if (i >= 10)
return; return;
mSelectedIndex = i; mSelected = &mKey[i];
int keyType = it->mType; mSelected->type = (QuickKeysMenu::QuickKeyType) it->mType;
std::string id = it->mId; mSelected->id = it->mId;
switch (keyType) switch (mSelected->type)
{ {
case Type_Magic: case Type_Magic:
if(MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(id)) if (MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().search(mSelected->id))
onAssignMagic(id); onAssignMagic(mSelected->id);
break; break;
case Type_Item: case Type_Item:
case Type_MagicItem: case Type_MagicItem:
{ {
// Find the item by id // Find the item by id
MWWorld::Ptr item = store.findReplacement(id); MWWorld::Ptr item = store.findReplacement(mSelected->id);
if(item.isEmpty()) if (item.isEmpty())
unassign(&mKey[i]); unassign(&mKey[i]);
else else
{ {
if(keyType == Type_Item) if (mSelected->type == Type_Item)
onAssignItem(item); onAssignItem(item);
else if(keyType == Type_MagicItem) else if (mSelected->type == Type_MagicItem)
onAssignMagicItem(item); onAssignMagicItem(item);
} }

View file

@ -66,6 +66,8 @@ namespace MWGui
}; };
std::vector<struct keyData> mKey; std::vector<struct keyData> mKey;
struct keyData* mSelected;
struct keyData* mActivated;
MyGUI::EditBox* mInstructionLabel; MyGUI::EditBox* mInstructionLabel;
MyGUI::Button* mOkButton; MyGUI::Button* mOkButton;
@ -74,9 +76,6 @@ namespace MWGui
ItemSelectionDialog* mItemSelectionDialog; ItemSelectionDialog* mItemSelectionDialog;
MagicSelectionDialog* mMagicSelectionDialog; MagicSelectionDialog* mMagicSelectionDialog;
int mSelectedIndex;
int mActivatedIndex;
void onQuickKeyButtonClicked(MyGUI::Widget* sender); void onQuickKeyButtonClicked(MyGUI::Widget* sender);
void onOkButtonClicked(MyGUI::Widget* sender); void onOkButtonClicked(MyGUI::Widget* sender);