GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/dragdrop.h Lines: 12 100 12.0 %
Date: 2017-11-29 Branches: 5 60 8.3 %

Line Branch Exec Source
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
33
#include "resources/skill/skilldata.h"
34
#include "resources/skill/skillinfo.h"
35
36
#include "localconsts.h"
37
38
class DragDrop final
39
{
40
    public:
41
2
        DragDrop(Item *const item,
42
2
                 const DragDropSourceT source) :
43
2
            mItemImage(item != nullptr ? item->getImage() : nullptr),
44
            mText(),
45
            mItemData(),
46
            mSource(source),
47
2
            mItem(item != nullptr ? item->getId() : 0),
48
            mSelItem(0),
49
            mTag(-1),
50
2
            mItemColor(item != nullptr ? item->getColor() : ItemColor_one),
51
10
            mSelItemColor(ItemColor_one)
52
        {
53
2
            if (mItemImage != nullptr)
54
                mItemImage->incRef();
55
2
        }
56
57
        A_DELETE_COPY(DragDrop)
58
59
2
        ~DragDrop()
60
6
        {
61
2
            if (mItemImage != nullptr)
62
                mItemImage->decRef();
63
2
        }
64
65
        int getItem() const
66
        { return mItem; }
67
68
        ItemColor getItemColor() const
69
        { return mItemColor; }
70
71
        void setItemColor(const ItemColor color)
72
        { mItemColor = color; }
73
74
        Image *getItemImage()
75
        { return mItemImage; }
76
77
        void setItemData(const std::string &data)
78
        { mItemData = data; }
79
80
        std::string getItemData()
81
        { return mItemData; }
82
83
        DragDropSourceT getSource() const
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;
104
                ItemSoundManager::playSfx(item, ItemSoundEvent::TAKE);
105
            }
106
            else
107
            {
108
                mItem = 0;
109
                mItemColor = ItemColor_one;
110
                mItemImage = nullptr;
111
                mSource = DragDropSource::Empty;
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;
123
            mItemColor = ItemColor_one;
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;
158
            mItemColor = ItemColor_zero;
159
            mText.clear();
160
            mItemImage = nullptr;
161
            mSource = DragDropSource::Empty;
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;
177
                mItemColor = fromInt(info->customSelectedLevel,
178
                    ItemColor);
179
            }
180
        }
181
182
        void clear()
183
        {
184
            if (mItemImage != nullptr)
185
                mItemImage->decRef();
186
            mItem = 0;
187
            mItemColor = ItemColor_one;
188
            mItemImage = nullptr;
189
            mSource = DragDropSource::Empty;
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;
208
                mSelItemColor = ItemColor_one;
209
            }
210
        }
211
212
        void deselect()
213
        {
214
            mSelItem = 0;
215
            mSelItemColor = ItemColor_one;
216
        }
217
218
        int getSelected() const
219
        { return mSelItem; }
220
221
        ItemColor getSelectedColor() const
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
240
        bool isSourceItemContainer() const
241
        {
242
            return mSource == DragDropSource::Inventory
243
                || mSource == DragDropSource::Storage
244
                || mSource == DragDropSource::Cart
245
                || mSource == DragDropSource::Craft
246
                || mSource == DragDropSource::Trade
247
                || mSource == DragDropSource::Outfit
248
                || mSource == DragDropSource::Ground
249
                || mSource == DragDropSource::Drop;
250
        }
251
252
    private:
253
        Image *mItemImage;
254
        std::string mText;
255
        std::string mItemData;
256
        DragDropSourceT mSource;
257
        int mItem;
258
        int mSelItem;
259
        int mTag;
260
        ItemColor mItemColor;
261
        ItemColor mSelItemColor;
262
};
263
264
extern DragDrop dragDrop;
265
266
#endif  // DRAGDROP_H