ManaPlus
shortcutcontainer.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2007-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 
24 
25 #include "settings.h"
26 
27 #include "gui/gui.h"
28 
29 #include "utils/delete2.h"
30 
31 #include "resources/image/image.h"
32 
33 #include "render/graphics.h"
34 
36 
37 #include "debug.h"
38 
39 float ShortcutContainer::mAlpha = 1.0;
40 
42  Widget(widget),
44  MouseListener(),
45  mBackgroundImg(nullptr),
46  mMaxItems(0),
47  mBoxWidth(1),
48  mBoxHeight(1),
49  mGridWidth(1),
50  mGridHeight(1),
51  mVertexes(new ImageCollection)
52 {
53  mAllowLogic = false;
54 
55  addMouseListener(this);
56  addWidgetListener(this);
57 
60 
62  "item_shortcut_background.xml", "background.xml");
63 
64  if (mBackgroundImg != nullptr)
65  {
69  }
70  else
71  {
72  mBoxHeight = 1;
73  mBoxWidth = 1;
74  }
75 }
76 
78 {
79  if (mBackgroundImg != nullptr)
80  {
82  mBackgroundImg = nullptr;
83  }
84 
85  if (gui != nullptr)
86  gui->removeDragged(this);
87 
89 }
90 
92 {
94 
95  if (mGridWidth < 1)
96  mGridWidth = 1;
97 
99 
100  if (mMaxItems % mGridWidth != 0 || mGridHeight < 1)
101  ++mGridHeight;
102 
104  mRedraw = true;
105 }
106 
108  const int pointY) const
109 {
110  const Rect tRect = Rect(0, 0,
112 
113  int index = ((pointY / mBoxHeight) * mGridWidth) + pointX / mBoxWidth;
114 
115  if (!tRect.isPointInRect(pointX, pointY) ||
116  index >= CAST_S32(mMaxItems) || index < 0)
117  {
118  index = -1;
119  }
120 
121  return index;
122 }
123 
125 {
126  if (mBackgroundImg != nullptr)
127  {
128  if (mRedraw)
129  {
130  mRedraw = false;
131  mVertexes->clear();
132  for (unsigned i = 0; i < mMaxItems; i ++)
133  {
135  (i % mGridWidth) * mBoxWidth,
136  (i / mGridWidth) * mBoxHeight);
137  }
138  g->finalize(mVertexes);
139  }
141  }
142 }
143 
145 {
146  if (mBackgroundImg != nullptr)
147  {
148  for (unsigned i = 0; i < mMaxItems; i ++)
149  {
151  (i / mGridWidth) * mBoxHeight);
152  }
153  }
154 }
155 
157 {
158  mRedraw = true;
159 }
#define CAST_U32
Definition: cast.h:30
int width
Definition: rect.h:218
ShortcutContainer(Widget2 *const widget)
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
ImageCollection * mVertexes
Gui * gui
Definition: gui.cpp:110
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha=255U) const A_INLINE
Definition: widget2.h:44
static Image * getImageFromThemeXml(const std::string &name, const std::string &name2)
Definition: theme.cpp:1129
Definition: rect.h:72
bool isPointInRect(const int x_, const int y_) const
Definition: rect.h:196
#define delete2(var)
Definition: delete2.h:24
Color mForegroundColor
Definition: widget.h:1085
int getIndexFromGrid(const int pointX, const int pointY) const
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:464
#define new
Definition: debug_new.h:147
void widgetMoved(const Event &event)
Settings settings
Definition: settings.cpp:31
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
#define CAST_S32
Definition: cast.h:29
#define nullptr
Definition: localconsts.h:44
bool mAllowLogic
Definition: widget.h:1159
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
void drawBackground(Graphics *const g)
void setHeight(const int height)
Definition: widget.cpp:139
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
#define A_UNUSED
Definition: localconsts.h:171
Color mForegroundColor2
Definition: widget2.h:112
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1144
Definition: widget.h:97
Definition: event.h:77
bool mRedraw
Definition: widget.h:1163
virtual void calcTileCollection(ImageCollection *const vertCol, const Image *const image, int x, int y)=0
void safeDrawBackground(Graphics *const g)
int getWidth() const A_INLINE
Definition: image.h:116
void widgetResized(const Event &event)
float guiAlpha
Definition: settings.h:129
Rect mDimension
Definition: widget.h:1100
void decRef()
Definition: image.cpp:521
int getHeight() const A_INLINE
Definition: image.h:122