add keyData struct + general cleanup

pull/456/head
Finbar Crago 7 years ago
parent cac6d59140
commit 335e2c5897

@ -43,27 +43,21 @@ namespace MWGui
getWidget(mInstructionLabel, "InstructionLabel"); getWidget(mInstructionLabel, "InstructionLabel");
mMainWidget->setSize(mMainWidget->getWidth(), mMainWidget->setSize(mMainWidget->getWidth(),
mMainWidget->getHeight() + (mInstructionLabel->getTextSize().height - mInstructionLabel->getHeight())); mMainWidget->getHeight() +
(mInstructionLabel->getTextSize().height - mInstructionLabel->getHeight()));
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)
{ {
ItemWidget* button; mKey[i].index = i;
getWidget(button, "QuickKey" + MyGUI::utility::toString(i+1)); getWidget(mKey[i].button, "QuickKey" + MyGUI::utility::toString(i+1));
mKey[i].button->eventMouseButtonClick += MyGUI::newDelegate(this, &QuickKeysMenu::onQuickKeyButtonClicked);
button->eventMouseButtonClick += MyGUI::newDelegate(this, &QuickKeysMenu::onQuickKeyButtonClicked); unassign(&mKey[i]);
mQuickKeyButtons.push_back(button);
mAssigned.push_back(Type_Unassigned);
mAssignedId.push_back(std::string(""));
mAssignedName.push_back(std::string(""));
unassign(button, i);
} }
} }
@ -73,7 +67,7 @@ namespace MWGui
for (int i=0; i<10; ++i) for (int i=0; i<10; ++i)
{ {
unassign(mQuickKeyButtons[i], i); unassign(&mKey[i]);
} }
} }
@ -94,10 +88,7 @@ namespace MWGui
// Check if quick keys are still valid // Check if quick keys are still valid
for (int i=0; i<10; ++i) for (int i=0; i<10; ++i)
{ {
ItemWidget* button = mQuickKeyButtons[i]; switch (mKey[i].type)
int type = mAssigned[i];
switch (type)
{ {
case Type_Unassigned: case Type_Unassigned:
case Type_HandToHand: case Type_HandToHand:
@ -106,7 +97,7 @@ namespace MWGui
case Type_Item: case Type_Item:
case Type_MagicItem: case Type_MagicItem:
{ {
MWWorld::Ptr item = *button->getUserData<MWWorld::Ptr>(); MWWorld::Ptr item = *mKey[i].button->getUserData<MWWorld::Ptr>();
// Make sure the item is available and is not broken // Make sure the item is available and is not broken
if (item.getRefData().getCount() < 1 || if (item.getRefData().getCount() < 1 ||
(item.getClass().hasItemHealth(item) && (item.getClass().hasItemHealth(item) &&
@ -116,51 +107,53 @@ namespace MWGui
std::string id = item.getCellRef().getRefId(); std::string id = item.getCellRef().getRefId();
item = store.findReplacement(id); item = store.findReplacement(id);
button->setUserData(MWWorld::Ptr(item)); mKey[i].button->setUserData(MWWorld::Ptr(item));
break; break;
} }
} }
} }
} }
} }
void QuickKeysMenu::unassign(ItemWidget* key, int index) void QuickKeysMenu::unassign(struct keyData* key)
{ {
mAssignedName[index] = ""; key->button->clearUserStrings();
mAssignedId[index] = ""; key->button->setItem(MWWorld::Ptr());
key->clearUserStrings(); while(key->button->getChildCount()) // Destroy number label
key->setItem(MWWorld::Ptr()); MyGUI::Gui::getInstance().destroyWidget(key->button->getChildAt(0));
while (key->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(key->getChildAt(0));
if (index == 9) if (key->index == 9)
{ {
mAssigned[index] = Type_HandToHand; key->type = Type_HandToHand;
MyGUI::ImageBox* image = key->createWidget<MyGUI::ImageBox>("ImageBox", MyGUI::ImageBox* image = key->button->createWidget<MyGUI::ImageBox>("ImageBox",
MyGUI::IntCoord(14, 13, 32, 32), MyGUI::Align::Default); MyGUI::IntCoord(14, 13, 32, 32), MyGUI::Align::Default);
image->setImageTexture("icons\\k\\stealth_handtohand.dds"); image->setImageTexture("icons\\k\\stealth_handtohand.dds");
image->setNeedMouseFocus(false); image->setNeedMouseFocus(false);
} }
else else
{ {
mAssigned[index] = Type_Unassigned; key->type = Type_Unassigned;
key->id = "";
key->name = "";
MyGUI::TextBox* textBox = key->createWidgetReal<MyGUI::TextBox>("SandText", MyGUI::FloatCoord(0,0,1,1), MyGUI::Align::Default); MyGUI::TextBox* textBox = key->button->createWidgetReal<MyGUI::TextBox>("SandText",
textBox->setTextAlign (MyGUI::Align::Center); MyGUI::FloatCoord(0,0,1,1), MyGUI::Align::Default);
textBox->setCaption (MyGUI::utility::toString(index+1));
textBox->setNeedMouseFocus (false); textBox->setTextAlign(MyGUI::Align::Center);
textBox->setCaption(MyGUI::utility::toString(key->index + 1));
textBox->setNeedMouseFocus(false);
} }
} }
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 == mQuickKeyButtons[i] || sender->getParent () == mQuickKeyButtons[i]) if(sender == mKey[i].button || sender->getParent() == mKey[i].button)
{ {
index = i; index = i;
break; break;
@ -170,9 +163,10 @@ namespace MWGui
mSelectedIndex = index; mSelectedIndex = 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);
} }
void QuickKeysMenu::onOkButtonClicked (MyGUI::Widget *sender) void QuickKeysMenu::onOkButtonClicked (MyGUI::Widget *sender)
@ -180,7 +174,6 @@ namespace MWGui
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_QuickKeysMenu); MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_QuickKeysMenu);
} }
void QuickKeysMenu::onItemButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onItemButtonClicked(MyGUI::Widget* sender)
{ {
if (!mItemSelectionDialog ) if (!mItemSelectionDialog )
@ -198,42 +191,42 @@ namespace MWGui
void QuickKeysMenu::onMagicButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onMagicButtonClicked(MyGUI::Widget* sender)
{ {
if (!mMagicSelectionDialog ) if(!mMagicSelectionDialog)
{ {
mMagicSelectionDialog = new MagicSelectionDialog(this); mMagicSelectionDialog = new MagicSelectionDialog(this);
} }
mMagicSelectionDialog->setVisible(true); mMagicSelectionDialog->setVisible(true);
mAssignDialog->setVisible (false); mAssignDialog->setVisible(false);
} }
void QuickKeysMenu::onUnassignButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onUnassignButtonClicked(MyGUI::Widget* sender)
{ {
unassign(mQuickKeyButtons[mSelectedIndex], mSelectedIndex); unassign(&mKey[mSelectedIndex]);
mAssignDialog->setVisible (false); mAssignDialog->setVisible(false);
} }
void QuickKeysMenu::onCancelButtonClicked(MyGUI::Widget* sender) void QuickKeysMenu::onCancelButtonClicked(MyGUI::Widget* sender)
{ {
mAssignDialog->setVisible (false); mAssignDialog->setVisible(false);
} }
void QuickKeysMenu::onAssignItem(MWWorld::Ptr item) void QuickKeysMenu::onAssignItem(MWWorld::Ptr item)
{ {
assert (mSelectedIndex >= 0); assert(mSelectedIndex >= 0);
ItemWidget* button = mQuickKeyButtons[mSelectedIndex];
while (button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(button->getChildAt(0));
mAssigned[mSelectedIndex] = Type_Item; while(mKey[mSelectedIndex].button->getChildCount()) // Destroy number label
mAssignedId[mSelectedIndex] = item.getCellRef().getRefId(); MyGUI::Gui::getInstance().destroyWidget(mKey[mSelectedIndex].button->getChildAt(0));
mAssignedName[mSelectedIndex] = item.getClass().getName(item);
button->setItem(item, ItemWidget::Barter); mKey[mSelectedIndex].type = Type_Item;
button->setUserString ("ToolTipType", "ItemPtr"); mKey[mSelectedIndex].id = item.getCellRef().getRefId();
button->setUserData(item); mKey[mSelectedIndex].name = item.getClass().getName(item);
if (mItemSelectionDialog) mKey[mSelectedIndex].button->setItem(item, ItemWidget::Barter);
mKey[mSelectedIndex].button->setUserString("ToolTipType", "ItemPtr");
mKey[mSelectedIndex].button->setUserData(item);
if(mItemSelectionDialog)
mItemSelectionDialog->setVisible(false); mItemSelectionDialog->setVisible(false);
} }
@ -242,37 +235,37 @@ namespace MWGui
mItemSelectionDialog->setVisible(false); mItemSelectionDialog->setVisible(false);
} }
void QuickKeysMenu::onAssignMagicItem (MWWorld::Ptr item) void QuickKeysMenu::onAssignMagicItem(MWWorld::Ptr item)
{ {
assert (mSelectedIndex >= 0); assert(mSelectedIndex >= 0);
ItemWidget* button = mQuickKeyButtons[mSelectedIndex];
while (button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(button->getChildAt(0));
mAssigned[mSelectedIndex] = Type_MagicItem; while(mKey[mSelectedIndex].button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(mKey[mSelectedIndex].button->getChildAt(0));
button->setFrame("textures\\menu_icon_select_magic_magic.dds", MyGUI::IntCoord(2, 2, 40, 40)); mKey[mSelectedIndex].type = Type_MagicItem;
button->setIcon(item);
button->setUserString ("ToolTipType", "ItemPtr"); mKey[mSelectedIndex].button->setFrame("textures\\menu_icon_select_magic_magic.dds", MyGUI::IntCoord(2, 2, 40, 40));
button->setUserData(MWWorld::Ptr(item)); mKey[mSelectedIndex].button->setIcon(item);
if (mMagicSelectionDialog) mKey[mSelectedIndex].button->setUserString ("ToolTipType", "ItemPtr");
mKey[mSelectedIndex].button->setUserData(MWWorld::Ptr(item));
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(mSelectedIndex >= 0);
ItemWidget* button = mQuickKeyButtons[mSelectedIndex]; ItemWidget* button = mKey[mSelectedIndex].button;
while (button->getChildCount()) // Destroy number label while(mKey[mSelectedIndex].button->getChildCount()) // Destroy number label
MyGUI::Gui::getInstance().destroyWidget(button->getChildAt(0)); MyGUI::Gui::getInstance().destroyWidget(button->getChildAt(0));
mAssigned[mSelectedIndex] = Type_Magic; mKey[mSelectedIndex].type = Type_Magic;
button->setItem(MWWorld::Ptr()); mKey[mSelectedIndex].button->setItem(MWWorld::Ptr());
button->setUserString ("ToolTipType", "Spell"); mKey[mSelectedIndex].button->setUserString("ToolTipType", "Spell");
button->setUserString ("Spell", spellId); mKey[mSelectedIndex].button->setUserString("Spell", spellId);
const MWWorld::ESMStore &esmStore = const MWWorld::ESMStore &esmStore =
MWBase::Environment::get().getWorld()->getStore(); MWBase::Environment::get().getWorld()->getStore();
@ -311,10 +304,9 @@ namespace MWGui
void QuickKeysMenu::activateQuickKey(int index) void QuickKeysMenu::activateQuickKey(int index)
{ {
assert (index-1 >= 0); assert(index-1 >= 0);
ItemWidget* button = mQuickKeyButtons[index-1]; ItemWidget* button = mKey[index-1].button;
QuickKeyType type = mKey[index-1].type;
QuickKeyType type = mAssigned[index-1];
MWWorld::Ptr player = MWMechanics::getPlayer(); MWWorld::Ptr player = MWMechanics::getPlayer();
MWWorld::InventoryStore& store = player.getClass().getInventoryStore(player); MWWorld::InventoryStore& store = player.getClass().getInventoryStore(player);
@ -351,17 +343,17 @@ namespace MWGui
break; break;
} }
if (it == store.end()) if (it == store.end())
item = NULL; item = nullptr;
// check the item is available and not broken // check the item is available and not broken
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(mAssignedId[index-1]); item = store.findReplacement(mKey[index-1].id);
if (!item || item.getRefData().getCount() < 1) if (!item || item.getRefData().getCount() < 1)
{ {
MWBase::Environment::get().getWindowManager()->messageBox( MWBase::Environment::get().getWindowManager()->messageBox(
"#{sQuickMenu5} " + mAssignedName[index-1]); "#{sQuickMenu5} " + mKey[index-1].name);
return; return;
} }
@ -490,9 +482,9 @@ namespace MWGui
for (int i=0; i<10; ++i) for (int i=0; i<10; ++i)
{ {
ItemWidget* button = mQuickKeyButtons[i]; ItemWidget* button = mKey[i].button;
int type = mAssigned[i]; int type = mKey[i].type;
ESM::QuickKeys::QuickKey key; ESM::QuickKeys::QuickKey key;
key.mType = type; key.mType = type;
@ -525,7 +517,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;
@ -535,20 +527,19 @@ 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; mSelectedIndex = i;
int keyType = it->mType; int keyType = it->mType;
std::string id = it->mId; std::string id = it->mId;
ItemWidget* button = mQuickKeyButtons[i];
switch (keyType) switch (keyType)
{ {
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(id))
onAssignMagic(id); onAssignMagic(id);
break; break;
case Type_Item: case Type_Item:
@ -557,13 +548,13 @@ namespace MWGui
// Find the item by id // Find the item by id
MWWorld::Ptr item = store.findReplacement(id); MWWorld::Ptr item = store.findReplacement(id);
if (item.isEmpty()) if(item.isEmpty())
unassign(button, i); unassign(&mKey[i]);
else else
{ {
if (keyType == Type_Item) if(keyType == Type_Item)
onAssignItem(item); onAssignItem(item);
else if (keyType == Type_MagicItem) else if(keyType == Type_MagicItem)
onAssignMagicItem(item); onAssignMagicItem(item);
} }
@ -571,7 +562,7 @@ namespace MWGui
} }
case Type_Unassigned: case Type_Unassigned:
case Type_HandToHand: case Type_HandToHand:
unassign(button, i); unassign(&mKey[i]);
break; break;
} }

@ -2,7 +2,6 @@
#define MWGUI_QUICKKEYS_H #define MWGUI_QUICKKEYS_H
#include "../mwworld/ptr.hpp" #include "../mwworld/ptr.hpp"
#include "../mwworld/containerstore.hpp"
#include "windowbase.hpp" #include "windowbase.hpp"
@ -56,14 +55,21 @@ namespace MWGui
private: private:
struct keyData {
int index;
ItemWidget* button;
QuickKeysMenu::QuickKeyType type;
std::string id;
std::string name;
keyData(): index(-1), button(nullptr), type(Type_Unassigned), id(""), name("") {}
};
std::vector<struct keyData> mKey;
MyGUI::EditBox* mInstructionLabel; MyGUI::EditBox* mInstructionLabel;
MyGUI::Button* mOkButton; MyGUI::Button* mOkButton;
std::vector<ItemWidget*> mQuickKeyButtons;
std::vector<QuickKeyType> mAssigned;
std::vector<std::string> mAssignedId;
std::vector<std::string> mAssignedName;
QuickKeysMenuAssign* mAssignDialog; QuickKeysMenuAssign* mAssignDialog;
ItemSelectionDialog* mItemSelectionDialog; ItemSelectionDialog* mItemSelectionDialog;
MagicSelectionDialog* mMagicSelectionDialog; MagicSelectionDialog* mMagicSelectionDialog;
@ -74,7 +80,7 @@ namespace MWGui
void onQuickKeyButtonClicked(MyGUI::Widget* sender); void onQuickKeyButtonClicked(MyGUI::Widget* sender);
void onOkButtonClicked(MyGUI::Widget* sender); void onOkButtonClicked(MyGUI::Widget* sender);
void unassign(ItemWidget* key, int index); void unassign(struct keyData* key);
}; };
class QuickKeysMenuAssign : public WindowModal class QuickKeysMenuAssign : public WindowModal

Loading…
Cancel
Save