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