ManaPlus
Public Member Functions | Static Public Member Functions | Private Attributes
ItemShortcut Class Reference

#include <itemshortcut.h>

Public Member Functions

 ItemShortcut (const size_t number)
 
 ~ItemShortcut ()
 
void load ()
 
void save () const
 
int getItem (const size_t index) const
 
ItemColor getItemColor (const size_t index) const
 
void setItemData (const size_t index, const std::string &data)
 
std::string getItemData (const size_t index) const
 
int getItemSelected () const
 
void setItem (const size_t index)
 
void setItem (const size_t index, const int item, const ItemColor color)
 
void setItemFast (const size_t index, const int item, const ItemColor color)
 
void setItems (const size_t index, const int itemId, const ItemColor color)
 
void setItemSelected (const int itemId)
 
void setItemSelected (const Item *const item)
 
int getSelectedItem () const
 
bool isItemSelected () const
 
void removeItem (const size_t index)
 
void useItem (const size_t index) const
 
void equipItem (const size_t index) const
 
void unequipItem (const size_t index) const
 
void swap (const size_t index1, const size_t index2)
 
void clear ()
 
size_t getFreeIndex () const
 

Static Public Member Functions

static int getItemCount ()
 

Private Attributes

int mItems [SHORTCUT_ITEMS]
 
ItemColor mItemColors [SHORTCUT_ITEMS]
 
std::string mItemData [SHORTCUT_ITEMS]
 
size_t mNumber
 
int mItemSelected
 
ItemColor mItemColorSelected
 

Detailed Description

The class which keeps track of the item shortcuts.

Definition at line 40 of file itemshortcut.h.

Constructor & Destructor Documentation

◆ ItemShortcut()

ItemShortcut::ItemShortcut ( const size_t  number)
explicit

Constructor.

Definition at line 45 of file itemshortcut.cpp.

45  :
46  mItems(),
47  mItemColors(),
48  mItemData(),
49  mNumber(number),
50  mItemSelected(-1),
52 {
53  load();
54 }
size_t mNumber
Definition: itemshortcut.h:177
int mItems[SHORTCUT_ITEMS]
Definition: itemshortcut.h:174
ItemColor mItemColors[SHORTCUT_ITEMS]
Definition: itemshortcut.h:175
ItemColor mItemColorSelected
Definition: itemshortcut.h:179
std::string mItemData[SHORTCUT_ITEMS]
Definition: itemshortcut.h:176
const ItemColor ItemColor_one
Definition: itemcolor.h:30

References load().

◆ ~ItemShortcut()

ItemShortcut::~ItemShortcut ( )

Destructor.

Definition at line 56 of file itemshortcut.cpp.

57 {
58  logger->log1("ItemShortcut::~ItemShortcut");
59 }
void log1(const char *const log_text)
Definition: logger.cpp:238
Logger * logger
Definition: logger.cpp:89

References Logger::log1(), and logger.

Member Function Documentation

◆ clear()

void ItemShortcut::clear ( )

Definition at line 141 of file itemshortcut.cpp.

142 {
143  for (size_t i = 0; i < SHORTCUT_ITEMS; i++)
144  {
145  mItems[i] = 0;
147  mItemData[i].clear();
148  }
149 }
const unsigned int SHORTCUT_ITEMS
Definition: itemshortcut.h:27
const ItemColor ItemColor_zero
Definition: itemcolor.h:30

References ItemColor_zero, mItemColors, mItemData, mItems, and SHORTCUT_ITEMS.

Referenced by SkillDialog::update().

◆ equipItem()

void ItemShortcut::equipItem ( const size_t  index) const

Equip a item from the shortcut.

Definition at line 181 of file itemshortcut.cpp.

182 {
183  const Inventory *const inv = PlayerInfo::getInventory();
184  if (inv == nullptr)
185  return;
186 
187  const int itemId = mItems[index];
188  if (itemId != 0)
189  {
190  const Item *const item = inv->findItem(itemId, mItemColors[index]);
191  if ((item != nullptr) && (item->getQuantity() != 0))
192  {
193  if (item->isEquipment() == Equipm_true)
194  {
195  if (item->isEquipped() == Equipped_false)
197  }
198  }
199  }
200 }
Item * findItem(const int itemId, const ItemColor color) const
Definition: inventory.cpp:94
Definition: item.h:50
Equipm isEquipment() const
Definition: item.h:117
int getQuantity() const
Definition: item.h:105
Equipped isEquipped() const
Definition: item.h:129
const bool Equipm_true
Definition: equipm.h:30
const bool Equipped_false
Definition: equipped.h:30
Inventory * getInventory()
Definition: playerinfo.cpp:195
void equipItem(const Item *const item, const Sfx sfx)
Definition: playerinfo.cpp:238
const bool Sfx_true
Definition: sfx.h:30

References PlayerInfo::equipItem(), Equipm_true, Equipped_false, Inventory::findItem(), PlayerInfo::getInventory(), Item::getQuantity(), Item::isEquipment(), Item::isEquipped(), mItemColors, mItems, and Sfx_true.

◆ getFreeIndex()

size_t ItemShortcut::getFreeIndex ( ) const

Definition at line 282 of file itemshortcut.cpp.

283 {
284  for (size_t i = 0; i < SHORTCUT_ITEMS; i++)
285  {
286  if (mItems[i] < 0)
287  return i;
288  }
289  return SHORTCUT_ITEMS;
290 }

References mItems, and SHORTCUT_ITEMS.

◆ getItem()

int ItemShortcut::getItem ( const size_t  index) const
inline

Returns the shortcut item ID specified by the index.

Parameters
indexIndex of the shortcut item.

Definition at line 70 of file itemshortcut.h.

71  { return mItems[index]; }

References mItems.

Referenced by ItemShortcutContainer::draw(), ItemShortcutContainer::mouseDragged(), ItemShortcutContainer::mouseMoved(), ItemShortcutContainer::mousePressed(), ItemShortcutContainer::mouseReleased(), and ItemShortcutContainer::safeDraw().

◆ getItemColor()

ItemColor ItemShortcut::getItemColor ( const size_t  index) const
inline

◆ getItemCount()

static int ItemShortcut::getItemCount ( )
inlinestatic

Returns the amount of shortcut items.

Definition at line 86 of file itemshortcut.h.

87  { return SHORTCUT_ITEMS; }

References SHORTCUT_ITEMS.

Referenced by ItemShortcutContainer::ItemShortcutContainer().

◆ getItemData()

std::string ItemShortcut::getItemData ( const size_t  index) const
inline

Definition at line 80 of file itemshortcut.h.

81  { return mItemData[index]; }

References mItemData.

Referenced by ItemShortcutContainer::mouseDragged(), and ItemShortcutContainer::mouseMoved().

◆ getItemSelected()

int ItemShortcut::getItemSelected ( ) const
inline

Returns the item ID that is currently selected.

Definition at line 92 of file itemshortcut.h.

93  { return mItemSelected; }

References mItemSelected.

◆ getSelectedItem()

int ItemShortcut::getSelectedItem ( ) const
inline

Returns selected shortcut item ID.

Definition at line 134 of file itemshortcut.h.

135  { return mItemSelected; }

References mItemSelected.

Referenced by ItemShortcutContainer::mousePressed().

◆ isItemSelected()

bool ItemShortcut::isItemSelected ( ) const
inline

A flag to check if the item is selected.

Definition at line 140 of file itemshortcut.h.

141  { return mItemSelected > -1; }

References mItemSelected.

Referenced by ItemShortcutContainer::mousePressed(), and ItemShortcutContainer::mouseReleased().

◆ load()

void ItemShortcut::load ( )

Load the configuration information.

Definition at line 61 of file itemshortcut.cpp.

62 {
63  std::string name;
64  std::string color;
65  std::string data;
67  return;
68 
69  const Configuration *cfg = &serverConfig;
70  if (mNumber != 0)
71  {
72  name = std::string("shortcut").append(
73  toString(CAST_S32(mNumber))).append("_");
74  color = std::string("shortcutColor").append(
75  toString(CAST_U32(mNumber))).append("_");
76  data = std::string("shortcutData").append(
77  toString(CAST_U32(mNumber))).append("_");
78  }
79  else
80  {
81  name = "shortcut";
82  color = "shortcutColor";
83  data = "shortcutData";
84  }
85  for (unsigned int i = 0; i < SHORTCUT_ITEMS; i++)
86  {
87  const int itemId = cfg->getValue(name + toString(i), -1);
88  const ItemColor itemColor = fromInt(
89  cfg->getValue(color + toString(i), 1),
90  ItemColor);
91 
92  mItems[i] = itemId;
93  mItemColors[i] = itemColor;
94  mItemData[i] = cfg->getValue(data + toString(i), std::string());
95  }
96 }
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
std::string getValue(const std::string &key, const std::string &deflt) const
Configuration serverConfig
const size_t SHORTCUT_AUTO_TAB
Definition: itemshortcut.h:29
#define fromInt(val, name)
Definition: intdefines.h:46
uint16_t ItemColor
Definition: itemcolor.h:30
uint32_t data
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774

References CAST_S32, CAST_U32, data, fromInt, ConfigurationObject::getValue(), mItemColors, mItemData, mItems, mNumber, serverConfig, SHORTCUT_AUTO_TAB, SHORTCUT_ITEMS, and Catch::toString().

Referenced by ItemShortcut().

◆ removeItem()

void ItemShortcut::removeItem ( const size_t  index)
inline

Remove a item from the shortcut.

Definition at line 146 of file itemshortcut.h.

147  { mItems[index] = -1; save(); }
void save() const

References mItems, and save().

Referenced by ItemShortcutContainer::mouseDragged().

◆ save()

void ItemShortcut::save ( ) const

Save the configuration information.

Definition at line 98 of file itemshortcut.cpp.

99 {
100  std::string name;
101  std::string color;
102  std::string data;
103  if (mNumber == SHORTCUT_AUTO_TAB)
104  return;
105  if (mNumber != 0)
106  {
107  name = std::string("shortcut").append(
108  toString(CAST_S32(mNumber))).append("_");
109  color = std::string("shortcutColor").append(
110  toString(CAST_U32(mNumber))).append("_");
111  data = std::string("shortcutData").append(
112  toString(CAST_U32(mNumber))).append("_");
113  }
114  else
115  {
116  name = "shortcut";
117  color = "shortcutColor";
118  data = "shortcutData";
119  }
120 
121  for (unsigned int i = 0; i < SHORTCUT_ITEMS; i++)
122  {
123  const int itemId = mItems[i] != 0 ? mItems[i] : -1;
124  if (itemId != -1)
125  {
126  const int itemColor = toInt(mItemColors[i], int);
127  const std::string itemData = mItemData[i];
128  serverConfig.setValue(name + toString(i), itemId);
129  serverConfig.setValue(color + toString(i), itemColor);
130  serverConfig.setValue(data + toString(i), itemData);
131  }
132  else
133  {
134  serverConfig.deleteKey(name + toString(i));
135  serverConfig.deleteKey(color + toString(i));
137  }
138  }
139 }
void deleteKey(const std::string &key)
void setValue(const std::string &key, const std::string &value)
#define toInt(val, name)
Definition: intdefines.h:47

References CAST_S32, CAST_U32, data, ConfigurationObject::deleteKey(), mItemColors, mItemData, mItems, mNumber, serverConfig, Configuration::setValue(), SHORTCUT_AUTO_TAB, SHORTCUT_ITEMS, toInt, and Catch::toString().

Referenced by removeItem(), setItem(), setItems(), and swap().

◆ setItem() [1/2]

void ItemShortcut::setItem ( const size_t  index)

Adds the selected item ID to the items specified by the index.

Parameters
indexIndex of the items.

Definition at line 236 of file itemshortcut.cpp.

237 {
238  mItems[index] = mItemSelected;
240  save();
241 }

References mItemColors, mItemColorSelected, mItems, mItemSelected, and save().

Referenced by ItemShortcutContainer::mousePressed(), and ItemShortcutContainer::mouseReleased().

◆ setItem() [2/2]

void ItemShortcut::setItem ( const size_t  index,
const int  item,
const ItemColor  color 
)

Definition at line 243 of file itemshortcut.cpp.

246 {
247  mItems[index] = item;
248  mItemColors[index] = color;
249  save();
250 }

References mItemColors, mItems, and save().

◆ setItemData()

void ItemShortcut::setItemData ( const size_t  index,
const std::string &  data 
)
inline

Definition at line 76 of file itemshortcut.h.

78  { mItemData[index] = data; }

References data, and mItemData.

Referenced by ItemShortcutContainer::mouseReleased(), and SkillDialog::update().

◆ setItemFast()

void ItemShortcut::setItemFast ( const size_t  index,
const int  item,
const ItemColor  color 
)

Definition at line 252 of file itemshortcut.cpp.

255 {
256  mItems[index] = item;
257  mItemColors[index] = color;
258 }

References mItemColors, and mItems.

Referenced by SkillDialog::update().

◆ setItems()

void ItemShortcut::setItems ( const size_t  index,
const int  itemId,
const ItemColor  color 
)
inline

Adds an item to the items store specified by the index.

Parameters
indexIndex of the item.
itemIdID of the item.

Definition at line 116 of file itemshortcut.h.

119  { mItems[index] = itemId; mItemColors[index] = color; save(); }

References mItemColors, mItems, and save().

◆ setItemSelected() [1/2]

void ItemShortcut::setItemSelected ( const int  itemId)
inline

Set the item that is selected.

Parameters
itemIdThe ID of the item that is to be assigned.

Definition at line 126 of file itemshortcut.h.

127  { mItemSelected = itemId; }

References mItemSelected.

Referenced by SkillDialog::action(), ItemContainer::mousePressed(), ItemShortcutContainer::mousePressed(), ItemShortcutContainer::mouseReleased(), and SpellShortcutContainer::mouseReleased().

◆ setItemSelected() [2/2]

void ItemShortcut::setItemSelected ( const Item *const  item)

Definition at line 222 of file itemshortcut.cpp.

223 {
224  if (item != nullptr)
225  {
226  mItemSelected = item->getId();
227  mItemColorSelected = item->getColor();
228  }
229  else
230  {
231  mItemSelected = -1;
233  }
234 }
int getId() const
Definition: item.h:81
ItemColor getColor() const
Definition: item.h:181

References Item::getColor(), Item::getId(), ItemColor_one, mItemColorSelected, and mItemSelected.

◆ swap()

void ItemShortcut::swap ( const size_t  index1,
const size_t  index2 
)

Definition at line 260 of file itemshortcut.cpp.

262 {
263  if (CAST_U32(index1) >= SHORTCUT_ITEMS ||
264  CAST_U32(index2) >= SHORTCUT_ITEMS)
265  {
266  return;
267  }
268 
269  const int tmpItem = mItems[index1];
270  mItems[index1] = mItems[index2];
271  mItems[index2] = tmpItem;
272  const ItemColor tmpColor = mItemColors[index1];
273  mItemColors[index1] = mItemColors[index2];
274  mItemColors[index2] = tmpColor;
275 
276  const std::string tmpData = mItemData[index1];
277  mItemData[index1] = mItemData[index2];
278  mItemData[index2] = tmpData;
279  save();
280 }

References CAST_U32, mItemColors, mItemData, mItems, save(), and SHORTCUT_ITEMS.

Referenced by ItemShortcutContainer::mouseReleased().

◆ unequipItem()

void ItemShortcut::unequipItem ( const size_t  index) const

UnEquip a item from the shortcut.

Definition at line 201 of file itemshortcut.cpp.

202 {
203  const Inventory *const inv = PlayerInfo::getInventory();
204  if (inv == nullptr)
205  return;
206 
207  const int itemId = mItems[index];
208  if (itemId != 0)
209  {
210  const Item *const item = inv->findItem(itemId, mItemColors[index]);
211  if ((item != nullptr) && (item->getQuantity() != 0))
212  {
213  if (item->isEquipment() == Equipm_true)
214  {
215  if (item->isEquipped() == Equipped_true)
217  }
218  }
219  }
220 }
const bool Equipped_true
Definition: equipped.h:30
void unequipItem(const Item *const item, const Sfx sfx)
Definition: playerinfo.cpp:246

References Equipm_true, Equipped_true, Inventory::findItem(), PlayerInfo::getInventory(), Item::getQuantity(), Item::isEquipment(), Item::isEquipped(), mItemColors, mItems, Sfx_true, and PlayerInfo::unequipItem().

◆ useItem()

void ItemShortcut::useItem ( const size_t  index) const

Try to use the item specified by the index.

Parameters
indexIndex of the item shortcut.

Definition at line 151 of file itemshortcut.cpp.

152 {
153  const Inventory *const inv = PlayerInfo::getInventory();
154  if (inv == nullptr)
155  return;
156 
157  const int itemId = mItems[index];
158  const ItemColor itemColor = mItemColors[index];
159  if (itemId >= 0)
160  {
161  if (itemId < SPELL_MIN_ID)
162  {
163  const Item *const item = inv->findItem(itemId, itemColor);
164  if (item != nullptr && item->getQuantity() != 0)
166  }
167  else if (itemId < SKILL_MIN_ID && (spellManager != nullptr))
168  {
169  spellManager->useItem(itemId);
170  }
171  else if (skillDialog != nullptr)
172  {
173  skillDialog->useItem(itemId,
174  fromBool(config.getBoolValue("skillAutotarget"), AutoTarget),
175  toInt(itemColor, int),
176  mItemData[index]);
177  }
178  }
179 }
bool AutoTarget
Definition: autotarget.h:30
#define fromBool(val, name)
Definition: booldefines.h:49
bool getBoolValue(const std::string &key) const
void useItem(const int itemId, const AutoTarget autoTarget, const int level, const std::string &data) const
void useItem(const int itemId) const
Configuration config
void useEquipItem(const Item *const item, const int16_t useType, const Sfx sfx)
Definition: playerinfo.cpp:262
const int SKILL_MIN_ID
Definition: skill.h:25
SkillDialog * skillDialog
Definition: skilldialog.cpp:66
SpellManager * spellManager
const int SPELL_MIN_ID
Definition: spells.h:27

References config, Inventory::findItem(), fromBool, Configuration::getBoolValue(), PlayerInfo::getInventory(), Item::getQuantity(), mItemColors, mItemData, mItems, Sfx_true, SKILL_MIN_ID, skillDialog, SPELL_MIN_ID, spellManager, toInt, PlayerInfo::useEquipItem(), SpellManager::useItem(), and SkillDialog::useItem().

Referenced by ItemShortcutContainer::mouseReleased().

Field Documentation

◆ mItemColors

ItemColor ItemShortcut::mItemColors[SHORTCUT_ITEMS]
private

The item colors.

Definition at line 175 of file itemshortcut.h.

Referenced by clear(), equipItem(), getItemColor(), load(), save(), setItem(), setItemFast(), setItems(), swap(), unequipItem(), and useItem().

◆ mItemColorSelected

ItemColor ItemShortcut::mItemColorSelected
private

Definition at line 179 of file itemshortcut.h.

Referenced by setItem(), and setItemSelected().

◆ mItemData

std::string ItemShortcut::mItemData[SHORTCUT_ITEMS]
private

Definition at line 176 of file itemshortcut.h.

Referenced by clear(), getItemData(), load(), save(), setItemData(), swap(), and useItem().

◆ mItems

int ItemShortcut::mItems[SHORTCUT_ITEMS]
private

◆ mItemSelected

int ItemShortcut::mItemSelected
private

◆ mNumber

size_t ItemShortcut::mNumber
private

Definition at line 177 of file itemshortcut.h.

Referenced by load(), and save().


The documentation for this class was generated from the following files: