GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/itemcontainer.h Lines: 1 4 25.0 %
Date: 2021-03-17 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifndef GUI_WIDGETS_ITEMCONTAINER_H
25
#define GUI_WIDGETS_ITEMCONTAINER_H
26
27
#include "listeners/keylistener.h"
28
#include "listeners/mouselistener.h"
29
#include "listeners/widgetlistener.h"
30
31
#include "enums/simpletypes/forcequantity.h"
32
#include "enums/simpletypes/showemptyrows.h"
33
34
#include "gui/widgets/widget.h"
35
36
#include "localconsts.h"
37
38
class Image;
39
class ImageCollection;
40
class Inventory;
41
class Item;
42
class SelectionListener;
43
44
/**
45
 * An item container. Used to show items in inventory and trade dialog.
46
 *
47
 * \ingroup GUI
48
 */
49
class ItemContainer final : public Widget,
50
                            public KeyListener,
51
                            public MouseListener,
52
                            public WidgetListener
53
{
54
    public:
55
        ItemContainer(const Widget2 *const widget,
56
                      Inventory *const inventory,
57
                      const int maxColumns,
58
                      const ShowEmptyRows showEmptyRows,
59
                      const ForceQuantity forceQuantity);
60
61
        A_DELETE_COPY(ItemContainer)
62
63
        /**
64
         * Destructor.
65
         */
66
        ~ItemContainer() override final;
67
68
        /**
69
         * Necessary for checking how full the inventory is.
70
         */
71
        void logic() override final;
72
73
        /**
74
         * Draws the items.
75
         */
76
        void draw(Graphics *const graphics) override final A_NONNULL(2);
77
78
        void safeDraw(Graphics *const graphics) override final A_NONNULL(2);
79
80
        // KeyListener
81
        void keyPressed(KeyEvent &event) override final;
82
        void keyReleased(KeyEvent &event) override final;
83
84
        // MouseListener
85
        void mousePressed(MouseEvent &event) override final;
86
        void mouseDragged(MouseEvent &event) override final;
87
        void mouseReleased(MouseEvent &event) override final;
88
        void mouseMoved(MouseEvent &event) override final;
89
        void mouseExited(MouseEvent &event) override final;
90
91
        // WidgetListener
92
        void widgetResized(const Event &event) override final;
93
94
        void widgetMoved(const Event &event) override final;
95
96
        /**
97
         * Returns the selected item.
98
         */
99
        Item *getSelectedItem() const A_WARN_UNUSED;
100
101
        /**
102
         * Sets selected item to NULL.
103
         */
104
        void selectNone();
105
106
        /**
107
         * Adds a listener to the list that's notified each time a change to
108
         * the selection occurs.
109
         */
110
        void addSelectionListener(SelectionListener *listener)
111
6
        { mSelectionListeners.push_back(listener); }
112
113
        /**
114
         * Removes a listener from the list that's notified each time a change
115
         * to the selection occurs.
116
         */
117
        void removeSelectionListener(SelectionListener *listener)
118
        { mSelectionListeners.remove(listener); }
119
120
        void setFilter(const int tag);
121
122
        void setSortType(const int sortType);
123
124
        void setName(const std::string &str)
125
        { mName = str; }
126
127
        int updateMatrix();
128
129
        bool getClickCount() const noexcept2 A_WARN_UNUSED
130
        { return mClicks != 0; }
131
132
        void unsetInventory() noexcept2
133
        { mInventory = nullptr; }
134
135
        void setInventory(Inventory *const inventory) noexcept2
136
        { mInventory = inventory; }
137
138
        void setCellBackgroundImage(const std::string &xmlName);
139
140
        void setMaxColumns(const int maxColumns);
141
142
    private:
143
        enum Direction
144
        {
145
            Left = 0,
146
            Right,
147
            Up,
148
            Down
149
        };
150
151
        enum SelectionState
152
        {
153
            SEL_NONE = 0,
154
            SEL_SELECTED,
155
            SEL_SELECTING,
156
            SEL_DESELECTING,
157
            SEL_DRAGGING
158
        };
159
160
        /**
161
         * Sets the currently selected item.
162
         */
163
        void setSelectedIndex(const int index);
164
165
        /**
166
         * Determine and set the height of the container.
167
         */
168
        void adjustHeight();
169
170
        /**
171
         * Sends out selection events to the list of selection listeners.
172
         */
173
        void distributeValueChangedEvent();
174
175
        void updateSize();
176
177
        /**
178
         * Gets the inventory slot index based on the cursor position.
179
         *
180
         * @param x The X coordinate position.
181
         * @param y The Y coordinate position.
182
         * @return The slot index on success, -1 on failure.
183
         */
184
        int getSlotIndex(int x, int y) const;
185
186
        int getSlotByXY(int x, int y) const;
187
188
        Inventory *mInventory;
189
        Image *mSelImg;
190
        Image *mProtectedImg;
191
        Image *mCellBackgroundImg;
192
        std::string mName;
193
194
        int *mShowMatrix;
195
        Skin *mSkin;
196
        ImageCollection *mVertexes;
197
        Color mEquipedColor;
198
        Color mEquipedColor2;
199
        Color mUnEquipedColor;
200
        Color mUnEquipedColor2;
201
        typedef std::list<SelectionListener*> SelectionListenerList;
202
        typedef SelectionListenerList::iterator SelectionListenerIterator;
203
        SelectionListenerList mSelectionListeners;
204
        int mGridColumns;
205
        int mGridRows;
206
        int mDrawRows;
207
        int mSelectedIndex;
208
        int mLastUsedSlot;
209
        int mTag;
210
        int mSortType;
211
        int mClicks;
212
        int mBoxWidth;
213
        int mBoxHeight;
214
        int mEquippedTextPadding;
215
        int mPaddingItemX;
216
        int mPaddingItemY;
217
        int mMaxColumns;
218
        SelectionState mSelectionStatus;
219
        ForceQuantity mForceQuantity;
220
        ShowEmptyRows mShowEmptyRows;
221
        bool mDescItems;
222
};
223
224
#endif  // GUI_WIDGETS_ITEMCONTAINER_H