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-2018 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 #include "gui/skin.h"
29 
30 #include "utils/delete2.h"
31 
32 #include "resources/image/image.h"
33 
34 #include "render/graphics.h"
35 
37 
38 #include "debug.h"
39 
40 float ShortcutContainer::mAlpha = 1.0;
41 
43  Widget(widget),
45  MouseListener(),
46  mBackgroundImg(nullptr),
47  mSkin(nullptr),
48  mMaxItems(0),
49  mBoxWidth(1),
50  mBoxHeight(1),
51  mGridWidth(1),
52  mGridHeight(1),
53  mImageOffsetX(2),
54  mImageOffsetY(2),
55  mTextOffsetX(2),
56  mTextOffsetY(2),
57  mVertexes(new ImageCollection)
58 {
59  mAllowLogic = false;
60 
61  addMouseListener(this);
62  addWidgetListener(this);
63 
66 
68  "item_shortcut_background.xml", "background.xml");
69 
70  if (mBackgroundImg != nullptr)
71  {
75  }
76  else
77  {
78  mBoxHeight = 1;
79  mBoxWidth = 1;
80  }
81 }
82 
84 {
85  if (mBackgroundImg != nullptr)
86  {
88  mBackgroundImg = nullptr;
89  }
90 
91  if (gui != nullptr)
92  gui->removeDragged(this);
93 
95 }
96 
98 {
100 
101  if (mGridWidth < 1)
102  mGridWidth = 1;
103 
105 
106  if (mMaxItems % mGridWidth != 0 || mGridHeight < 1)
107  ++mGridHeight;
108 
110  mRedraw = true;
111 }
112 
114  const int pointY) const
115 {
116  const Rect tRect = Rect(0, 0,
118 
119  int index = ((pointY / mBoxHeight) * mGridWidth) + pointX / mBoxWidth;
120 
121  if (!tRect.isPointInRect(pointX, pointY) ||
122  index >= CAST_S32(mMaxItems) || index < 0)
123  {
124  index = -1;
125  }
126 
127  return index;
128 }
129 
131 {
132  if (mBackgroundImg != nullptr)
133  {
134  if (mRedraw)
135  {
136  mRedraw = false;
137  mVertexes->clear();
138  for (unsigned i = 0; i < mMaxItems; i ++)
139  {
141  (i % mGridWidth) * mBoxWidth,
142  (i / mGridWidth) * mBoxHeight);
143  }
144  g->finalize(mVertexes);
145  }
147  }
148 }
149 
151 {
152  if (mBackgroundImg != nullptr)
153  {
154  for (unsigned i = 0; i < mMaxItems; i ++)
155  {
157  (i / mGridWidth) * mBoxHeight);
158  }
159  }
160 }
161 
163 {
164  mRedraw = true;
165 }
166 
167 void ShortcutContainer::setSkin(const Widget2 *const widget,
168  Skin *const skin)
169 {
170  setWidget2(widget);
171  mSkin = skin;
172  if (mSkin)
173  {
174  mImageOffsetX = mSkin->getOption("imageOffsetX", 2);
175  mImageOffsetY = mSkin->getOption("imageOffsetY", 2);
176  mTextOffsetX = mSkin->getOption("textOffsetX", 2);
177  mTextOffsetY = mSkin->getOption("textOffsetY", 2);
178  }
179 }
#define CAST_U32
Definition: cast.h:30
int width
Definition: rect.h:218
Definition: skin.h:35
ShortcutContainer(Widget2 *const widget)
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
ImageCollection * mVertexes
Gui * gui
Definition: gui.cpp:110
static Image * getImageFromThemeXml(const std::string &name, const std::string &name2)
Definition: theme.cpp:925
Definition: rect.h:72
virtual void setWidget2(const Widget2 *const widget)
Definition: widget2.h:63
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 getOption(const std::string &name) const
Definition: skin.h:105
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:286
#define CAST_S32
Definition: cast.h:29
virtual void setSkin(const Widget2 *const widget, Skin *const skin)
#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:159
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:1161
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
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
void widgetResized(const Event &event)
float guiAlpha
Definition: settings.h:129
Rect mDimension
Definition: widget.h:1100
void decRef()
Definition: image.cpp:522
int getHeight() const A_INLINE
Definition: image.h:122