ManaPlus
Public Member Functions | Private Attributes
ShortcutBase Class Reference

#include <shortcutbase.h>

Inheritance diagram for ShortcutBase:
DropShortcut

Public Member Functions

 ShortcutBase (const std::string &itemName, const std::string &colorName, const int maxSize)
 
virtual ~ShortcutBase ()
 
void load ()
 
void save () const
 
int getItem (const size_t index) const
 
ItemColor getItemColor (const size_t index) const
 
int getItemCount () const
 
int getItemSelected () const
 
void setItem (const size_t index)
 
void setItems (const size_t index, const int itemId, const ItemColor color)
 
void setItemSelected (const int itemId)
 
void setItemSelected (const Item *const item)
 
bool isItemSelected () const
 
void removeItem (const size_t index)
 
void clear (const bool isSave)
 

Private Attributes

int * mItems
 
ItemColormItemColors
 
std::string mItemName
 
std::string mColorName
 
size_t mMaxSize
 
int mItemSelected
 
ItemColor mItemColorSelected
 

Detailed Description

The class which keeps track of the item shortcuts.

Definition at line 39 of file shortcutbase.h.

Constructor & Destructor Documentation

◆ ShortcutBase()

ShortcutBase::ShortcutBase ( const std::string &  itemName,
const std::string &  colorName,
const int  maxSize 
)

Constructor.

Definition at line 31 of file shortcutbase.cpp.

33  :
34  mItems(new int[maxSize]),
35  mItemColors(new ItemColor[maxSize]),
36  mItemName(itemName),
37  mColorName(colorName),
38  mMaxSize(maxSize),
39  mItemSelected(-1),
41 {
42  clear(false);
43  load();
44 }
ItemColor * mItemColors
Definition: shortcutbase.h:133
void clear(const bool isSave)
std::string mItemName
Definition: shortcutbase.h:134
ItemColor mItemColorSelected
Definition: shortcutbase.h:138
size_t mMaxSize
Definition: shortcutbase.h:136
std::string mColorName
Definition: shortcutbase.h:135
const ItemColor ItemColor_one
Definition: itemcolor.h:30
uint16_t ItemColor
Definition: itemcolor.h:30

References clear(), and load().

◆ ~ShortcutBase()

ShortcutBase::~ShortcutBase ( )
virtual

Destructor.

Definition at line 46 of file shortcutbase.cpp.

47 {
48  delete [] mItems;
49  mItems = nullptr;
50  delete [] mItemColors;
51  mItemColors = nullptr;
52 }

References mItemColors, and mItems.

Member Function Documentation

◆ clear()

void ShortcutBase::clear ( const bool  isSave)

Definition at line 119 of file shortcutbase.cpp.

120 {
121  for (size_t i = 0; i < mMaxSize; i++)
122  {
123  mItems[i] = -1;
125  }
126  if (isSave)
127  save();
128 }
void save() const

References ItemColor_one, mItemColors, mItems, mMaxSize, and save().

Referenced by DropShortcut::DropShortcut(), and ShortcutBase().

◆ getItem()

int ShortcutBase::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 71 of file shortcutbase.h.

72  { return mItems[index]; }

References mItems.

Referenced by VirtShortcutContainer::draw(), DropShortcut::dropFirst(), DropShortcut::dropItem(), VirtShortcutContainer::mouseDragged(), VirtShortcutContainer::mouseMoved(), VirtShortcutContainer::mousePressed(), and VirtShortcutContainer::safeDraw().

◆ getItemColor()

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

◆ getItemCount()

int ShortcutBase::getItemCount ( ) const
inline

Returns the amount of shortcut items.

Definition at line 80 of file shortcutbase.h.

81  { return CAST_S32(mMaxSize); }
#define CAST_S32
Definition: cast.h:30

References CAST_S32, and mMaxSize.

Referenced by VirtShortcutContainer::VirtShortcutContainer().

◆ getItemSelected()

int ShortcutBase::getItemSelected ( ) const
inline

Returns the item ID that is currently selected.

Definition at line 86 of file shortcutbase.h.

87  { return mItemSelected; }

References mItemSelected.

◆ isItemSelected()

bool ShortcutBase::isItemSelected ( ) const
inline

A flag to check if the item is selected.

Definition at line 120 of file shortcutbase.h.

121  { return mItemSelected > -1; }

References mItemSelected.

Referenced by VirtShortcutContainer::mouseReleased().

◆ load()

void ShortcutBase::load ( )

Load the configuration information.

Definition at line 54 of file shortcutbase.cpp.

55 {
56  const Configuration *cfg = &serverConfig;
57 
58  for (size_t i = 0; i < mMaxSize; i++)
59  {
60  const std::string num = toString(CAST_S32(i));
61  const int itemId = cfg->getValue(mItemName + num, -1);
62  const ItemColor itemColor = fromInt(
63  cfg->getValue(mColorName + num, -1),
64  ItemColor);
65 
66  if (itemId != -1)
67  {
68  mItems[i] = itemId;
69  mItemColors[i] = itemColor;
70  }
71  }
72 }
std::string getValue(const std::string &key, const std::string &deflt) const
Configuration serverConfig
#define fromInt(val, name)
Definition: intdefines.h:46
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774

References CAST_S32, fromInt, ConfigurationObject::getValue(), mColorName, mItemColors, mItemName, mItems, mMaxSize, serverConfig, and Catch::toString().

Referenced by DropShortcut::DropShortcut(), and ShortcutBase().

◆ removeItem()

void ShortcutBase::removeItem ( const size_t  index)
inline

Remove a item from the shortcut.

Definition at line 126 of file shortcutbase.h.

127  { mItems[index] = -1; save(); }

References mItems, and save().

Referenced by VirtShortcutContainer::mouseDragged().

◆ save()

void ShortcutBase::save ( ) const

Save the configuration information.

Definition at line 74 of file shortcutbase.cpp.

75 {
76  for (size_t i = 0; i < mMaxSize; i++)
77  {
78  const int itemId = mItems[i] != 0 ? mItems[i] : -1;
79  const std::string num = toString(CAST_S32(i));
80  if (itemId != -1)
81  {
82  const int itemColor = (mItemColors[i] != ItemColor_zero)
83  ? toInt(mItemColors[i], int) : 1;
84  serverConfig.setValue(mItemName + num, itemId);
85  serverConfig.setValue(mColorName + num, itemColor);
86  }
87  else
88  {
91  }
92  }
93 }
void deleteKey(const std::string &key)
void setValue(const std::string &key, const std::string &value)
#define toInt(val, name)
Definition: intdefines.h:47
const ItemColor ItemColor_zero
Definition: itemcolor.h:30

References CAST_S32, ConfigurationObject::deleteKey(), ItemColor_zero, mColorName, mItemColors, mItemName, mItems, mMaxSize, serverConfig, Configuration::setValue(), toInt, and Catch::toString().

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

◆ setItem()

void ShortcutBase::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 109 of file shortcutbase.cpp.

110 {
111  if (index >= mMaxSize)
112  return;
113 
114  mItems[index] = mItemSelected;
116  save();
117 }

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

◆ setItems()

void ShortcutBase::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 102 of file shortcutbase.h.

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

References mItemColors, mItems, and save().

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

◆ setItemSelected() [1/2]

void ShortcutBase::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 112 of file shortcutbase.h.

113  { mItemSelected = itemId; }

References mItemSelected.

Referenced by VirtShortcutContainer::mouseReleased().

◆ setItemSelected() [2/2]

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

Definition at line 95 of file shortcutbase.cpp.

96 {
97  if (item != nullptr)
98  {
99  mItemSelected = item->getId();
100  mItemColorSelected = item->getColor();
101  }
102  else
103  {
104  mItemSelected = -1;
106  }
107 }
int getId() const
Definition: item.h:81
ItemColor getColor() const
Definition: item.h:181

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

Field Documentation

◆ mColorName

std::string ShortcutBase::mColorName
private

Definition at line 135 of file shortcutbase.h.

Referenced by load(), and save().

◆ mItemColors

ItemColor* ShortcutBase::mItemColors
private

Definition at line 133 of file shortcutbase.h.

Referenced by clear(), getItemColor(), load(), save(), setItem(), setItems(), and ~ShortcutBase().

◆ mItemColorSelected

ItemColor ShortcutBase::mItemColorSelected
private

Definition at line 138 of file shortcutbase.h.

Referenced by setItem(), and setItemSelected().

◆ mItemName

std::string ShortcutBase::mItemName
private

Definition at line 134 of file shortcutbase.h.

Referenced by load(), and save().

◆ mItems

int* ShortcutBase::mItems
private

Definition at line 132 of file shortcutbase.h.

Referenced by clear(), getItem(), load(), removeItem(), save(), setItem(), setItems(), and ~ShortcutBase().

◆ mItemSelected

int ShortcutBase::mItemSelected
private

Definition at line 137 of file shortcutbase.h.

Referenced by getItemSelected(), isItemSelected(), setItem(), and setItemSelected().

◆ mMaxSize

size_t ShortcutBase::mMaxSize
private

Definition at line 136 of file shortcutbase.h.

Referenced by clear(), getItemCount(), load(), save(), and setItem().


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