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