ManaPlus
shortcutbase.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 The Mana World Development Team
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2009-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "configuration.h"
26 
27 #include "resources/item/item.h"
28 
29 #include "debug.h"
30 
31 ShortcutBase::ShortcutBase(const std::string &itemName,
32  const std::string &colorName,
33  const int maxSize) :
34  mItems(new int[maxSize]),
35  mItemColors(new ItemColor[maxSize]),
36  mItemName(itemName),
37  mColorName(colorName),
38  mMaxSize(maxSize),
39  mItemSelected(-1),
40  mItemColorSelected(ItemColor_one)
41 {
42  clear(false);
43  load();
44 }
45 
47 {
48  delete [] mItems;
49  mItems = nullptr;
50  delete [] mItemColors;
51  mItemColors = nullptr;
52 }
53 
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 }
73 
74 void ShortcutBase::save() const
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 }
94 
95 void ShortcutBase::setItemSelected(const Item *const item)
96 {
97  if (item != nullptr)
98  {
99  mItemSelected = item->getId();
100  mItemColorSelected = item->getColor();
101  }
102  else
103  {
104  mItemSelected = -1;
106  }
107 }
108 
109 void ShortcutBase::setItem(const size_t index)
110 {
111  if (index >= mMaxSize)
112  return;
113 
114  mItems[index] = mItemSelected;
116  save();
117 }
118 
119 void ShortcutBase::clear(const bool isSave)
120 {
121  for (size_t i = 0; i < mMaxSize; i++)
122  {
123  mItems[i] = -1;
125  }
126  if (isSave)
127  save();
128 }
#define CAST_S32
Definition: cast.h:30
std::string getValue(const std::string &key, const std::string &deflt) const
void deleteKey(const std::string &key)
void setValue(const std::string &key, const std::string &value)
Definition: item.h:50
int getId() const
Definition: item.h:81
ItemColor getColor() const
Definition: item.h:181
ShortcutBase(const std::string &itemName, const std::string &colorName, const int maxSize)
void setItemSelected(const int itemId)
Definition: shortcutbase.h:112
ItemColor * mItemColors
Definition: shortcutbase.h:133
void clear(const bool isSave)
std::string mItemName
Definition: shortcutbase.h:134
virtual ~ShortcutBase()
void save() const
ItemColor mItemColorSelected
Definition: shortcutbase.h:138
size_t mMaxSize
Definition: shortcutbase.h:136
void setItem(const size_t index)
std::string mColorName
Definition: shortcutbase.h:135
Configuration serverConfig
#define new
Definition: debug_new.h:147
#define toInt(val, name)
Definition: intdefines.h:47
#define fromInt(val, name)
Definition: intdefines.h:46
const ItemColor ItemColor_one
Definition: itemcolor.h:30
uint16_t ItemColor
Definition: itemcolor.h:30
const ItemColor ItemColor_zero
Definition: itemcolor.h:30
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774