GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/dragdrop.h Lines: 12 100 12.0 %
Date: 2021-03-17 Branches: 5 62 8.1 %

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