ManaPlus
dragdrop.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef DRAGDROP_H
23 #define DRAGDROP_H
24 
25 #include "resources/item/item.h"
26 
27 #include "itemsoundmanager.h"
28 #include "textcommand.h"
29 
30 #include "enums/dragdropsource.h"
31 
32 #include "resources/image/image.h"
33 
36 
37 #include "localconsts.h"
38 
40 {
41  public:
42  DragDrop(Item *const item,
43  const DragDropSourceT source) :
44  mItemImage(item != nullptr ? item->getImage() : nullptr),
45  mText(),
46  mItemData(),
47  mSource(source),
48  mItem(item != nullptr ? item->getId() : 0),
49  mSelItem(0),
50  mTag(-1),
51  mItemColor(item != nullptr ? item->getColor() : ItemColor_one),
53  {
54  if (mItemImage != nullptr)
55  mItemImage->incRef();
56  }
57 
59 
61  {
62  if (mItemImage != nullptr)
63  mItemImage->decRef();
64  }
65 
66  int getItem() const
67  { return mItem; }
68 
70  { return mItemColor; }
71 
72  void setItemColor(const ItemColor color)
73  { mItemColor = color; }
74 
76  { return mItemImage; }
77 
78  void setItemData(const std::string &data)
79  { mItemData = data; }
80 
81  std::string getItemData()
82  { return mItemData; }
83 
85  { return mSource; }
86 
87  void dragItem(const Item *const item,
88  const DragDropSourceT source,
89  const int tag)
90  {
91  if (mItemImage != nullptr)
92  mItemImage->decRef();
93 
94  mItemData.clear();
95  mText.clear();
96  if (item != nullptr)
97  {
98  mItem = item->getId();
99  mItemColor = item->getColor();
100  mItemImage = item->getImage();
101  if (mItemImage != nullptr)
102  mItemImage->incRef();
103  mSource = source;
104  mTag = tag;
106  }
107  else
108  {
109  mItem = 0;
111  mItemImage = nullptr;
113  mTag = -1;
114  }
115  }
116 
117  void dragCommand(const TextCommand *const command,
118  const DragDropSourceT source,
119  const int tag)
120  {
121  if (mItemImage != nullptr)
122  mItemImage->decRef();
123  mItem = 0;
125  mItemData.clear();
126 
127  if (command != nullptr)
128  {
129  mText = command->getSymbol();
130  mItemImage = command->getImage();
131  if (mItemImage != nullptr)
132  {
133  mItemImage->incRef();
134  }
135  else if (mText.empty())
136  {
137  mSource = source;
138  mTag = -1;
139  return;
140  }
141  mItem = command->getId();
142  }
143  else
144  {
145  mText.clear();
146  mItemImage = nullptr;
147  }
148  mSource = source;
149  mTag = tag;
150  }
151 
152  void dragSkill(const SkillInfo *const info,
153  const DragDropSourceT source,
154  const int tag)
155  {
156  if (mItemImage != nullptr)
157  mItemImage->decRef();
158  mItem = 0;
160  mText.clear();
161  mItemImage = nullptr;
163  mTag = -1;
164  mItemData.clear();
165  if (info != nullptr)
166  {
167  const SkillData *const data = info->data;
168  if (data != nullptr)
169  {
170  mText = data->name;
171  mItemImage = data->icon;
172  if (mItemImage != nullptr)
173  mItemImage->incRef();
174  mSource = source;
175  mTag = tag;
176  }
177  mItem = info->id;
178  mItemColor = fromInt(info->customSelectedLevel,
179  ItemColor);
180  }
181  }
182 
183  void clear()
184  {
185  if (mItemImage != nullptr)
186  mItemImage->decRef();
187  mItem = 0;
189  mItemImage = nullptr;
191  mText.clear();
192  mItemData.clear();
193  mTag = -1;
194  }
195 
196  bool isEmpty() const
197  { return mSource == DragDropSource::Empty; }
198 
199  void select(const Item *const item)
200  {
201  if (item != nullptr)
202  {
203  mSelItem = item->getId();
204  mSelItemColor = item->getColor();
205  }
206  else
207  {
208  mSelItem = 0;
210  }
211  }
212 
213  void deselect()
214  {
215  mSelItem = 0;
217  }
218 
219  int getSelected() const
220  { return mSelItem; }
221 
223  { return mSelItemColor; }
224 
225  bool isSelected() const
226  { return mSelItem > 0; }
227 
228  void clearItem(const Item *const item A_UNUSED)
229  {
230  }
231 
232  const std::string &getText() const
233  { return mText; }
234 
235  int getTag() const
236  { return mTag; }
237 
238  void setItem(const int item)
239  { mItem = item; }
240 
242  {
251  }
252 
253  private:
255  std::string mText;
256  std::string mItemData;
258  int mItem;
259  int mSelItem;
260  int mTag;
263 };
264 
265 extern DragDrop dragDrop;
266 
267 #endif // DRAGDROP_H
void dragItem(const Item *const item, const DragDropSourceT source, const int tag)
Definition: dragdrop.h:87
DragDropSourceT getSource() const
Definition: dragdrop.h:84
DragDropSourceT mSource
Definition: dragdrop.h:257
ItemColor getSelectedColor() const
Definition: dragdrop.h:222
bool isSelected() const
Definition: dragdrop.h:225
void deselect()
Definition: dragdrop.h:213
Image * getItemImage()
Definition: dragdrop.h:75
ItemColor mSelItemColor
Definition: dragdrop.h:262
~DragDrop()
Definition: dragdrop.h:60
int getSelected() const
Definition: dragdrop.h:219
void setItem(const int item)
Definition: dragdrop.h:238
void dragCommand(const TextCommand *const command, const DragDropSourceT source, const int tag)
Definition: dragdrop.h:117
Image * mItemImage
Definition: dragdrop.h:254
std::string getItemData()
Definition: dragdrop.h:81
void dragSkill(const SkillInfo *const info, const DragDropSourceT source, const int tag)
Definition: dragdrop.h:152
int mItem
Definition: dragdrop.h:258
DragDrop(Item *const item, const DragDropSourceT source)
Definition: dragdrop.h:42
void setItemColor(const ItemColor color)
Definition: dragdrop.h:72
bool isEmpty() const
Definition: dragdrop.h:196
void clearItem(const Item *const item)
Definition: dragdrop.h:228
bool isSourceItemContainer() const
Definition: dragdrop.h:241
ItemColor mItemColor
Definition: dragdrop.h:261
int getTag() const
Definition: dragdrop.h:235
void select(const Item *const item)
Definition: dragdrop.h:199
void clear()
Definition: dragdrop.h:183
int mSelItem
Definition: dragdrop.h:259
int getItem() const
Definition: dragdrop.h:66
std::string mText
Definition: dragdrop.h:255
int mTag
Definition: dragdrop.h:260
const std::string & getText() const
Definition: dragdrop.h:232
ItemColor getItemColor() const
Definition: dragdrop.h:69
std::string mItemData
Definition: dragdrop.h:256
void setItemData(const std::string &data)
Definition: dragdrop.h:78
static void playSfx(const int item, const ItemSoundEvent::Type sound)
Definition: item.h:50
int getId() const
Definition: item.h:81
Image * getImage() const
Definition: item.h:87
ItemColor getColor() const
Definition: item.h:181
int getId() const
Definition: textcommand.h:94
std::string getSymbol() const
Definition: textcommand.h:91
Image * getImage() const
Definition: textcommand.h:156
DragDrop dragDrop
DragDropSource ::T DragDropSourceT
#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
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
uint32_t data
bool info(InputEvent &event)
Definition: commands.cpp:57
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:86
const DyeColor * getColor(const std::string &name)
Definition: palettedb.cpp:109