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-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 
25 
26 #include "settings.h"
27 
28 #include "gui/gui.h"
29 #include "gui/skin.h"
30 
31 #include "utils/delete2.h"
32 
33 #include "resources/image/image.h"
34 
35 #include "render/graphics.h"
36 
38 
39 #include "debug.h"
40 
41 float ShortcutContainer::mAlpha = 1.0;
42 
44  Widget(widget),
46  MouseListener(),
47  mBackgroundImg(nullptr),
48  mSkin(nullptr),
49  mMaxItems(0),
50  mBoxWidth(1),
51  mBoxHeight(1),
52  mGridWidth(1),
53  mGridHeight(1),
54  mImageOffsetX(2),
55  mImageOffsetY(2),
56  mTextOffsetX(2),
57  mTextOffsetY(2),
58  mVertexes(new ImageCollection)
59 {
60  mAllowLogic = false;
61 
62  addMouseListener(this);
63  addWidgetListener(this);
64 
66  mForegroundColor2 = getThemeColor(ThemeColorId::TEXT_OUTLINE, 255U);
67 
69  "item_shortcut_background.xml", "background.xml");
70 
71  if (mBackgroundImg != nullptr)
72  {
73  mBackgroundImg->setAlpha(settings.guiAlpha);
74  mBoxHeight = mBackgroundImg->getHeight();
75  mBoxWidth = mBackgroundImg->getWidth();
76  }
77  else
78  {
79  mBoxHeight = 1;
80  mBoxWidth = 1;
81  }
82 }
83 
85 {
86  if (mBackgroundImg != nullptr)
87  {
88  mBackgroundImg->decRef();
89  mBackgroundImg = nullptr;
90  }
91 
92  if (gui != nullptr)
93  gui->removeDragged(this);
94 
96 }
97 
99 {
101 
102  if (mGridWidth < 1)
103  mGridWidth = 1;
104 
106 
107  if (mMaxItems % mGridWidth != 0 || mGridHeight < 1)
108  ++mGridHeight;
109 
111  mRedraw = true;
112 }
113 
115  const int pointY) const
116 {
117  const Rect tRect = Rect(0, 0,
119 
120  int index = ((pointY / mBoxHeight) * mGridWidth) + pointX / mBoxWidth;
121 
122  if (!tRect.isPointInRect(pointX, pointY) ||
123  index >= CAST_S32(mMaxItems) || index < 0)
124  {
125  index = -1;
126  }
127 
128  return index;
129 }
130 
132 {
133  if (mBackgroundImg != nullptr)
134  {
135  if (mRedraw)
136  {
137  mRedraw = false;
138  mVertexes->clear();
139  for (unsigned i = 0; i < mMaxItems; i ++)
140  {
142  (i % mGridWidth) * mBoxWidth,
143  (i / mGridWidth) * mBoxHeight);
144  }
145  g->finalize(mVertexes);
146  }
148  }
149 }
150 
152 {
153  if (mBackgroundImg != nullptr)
154  {
155  for (unsigned i = 0; i < mMaxItems; i ++)
156  {
158  (i / mGridWidth) * mBoxHeight);
159  }
160  }
161 }
162 
164 {
165  mRedraw = true;
166 }
167 
168 void ShortcutContainer::setSkin(const Widget2 *const widget,
169  Skin *const skin)
170 {
171  setWidget2(widget);
172  mSkin = skin;
173  if (mSkin)
174  {
175  mImageOffsetX = mSkin->getOption("imageOffsetX", 2);
176  mImageOffsetY = mSkin->getOption("imageOffsetY", 2);
177  mTextOffsetX = mSkin->getOption("textOffsetX", 2);
178  mTextOffsetY = mSkin->getOption("textOffsetY", 2);
179  }
180 }
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
Definition: event.h:79
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
virtual void calcTileCollection(ImageCollection *const vertCol, const Image *const image, int x, int y)=0
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:465
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1162
Definition: rect.h:74
int width
Definition: rect.h:219
bool isPointInRect(const int x_, const int y_) const
Definition: rect.h:197
float guiAlpha
Definition: settings.h:131
void widgetResized(const Event &event)
void widgetMoved(const Event &event)
virtual void setSkin(const Widget2 *const widget, Skin *const skin)
ImageCollection * mVertexes
ShortcutContainer(Widget2 *const widget)
void drawBackground(Graphics *const g)
void safeDrawBackground(Graphics *const g)
int getIndexFromGrid(const int pointX, const int pointY) const
Definition: skin.h:37
int getOption(const std::string &name) const
Definition: skin.h:106
static Image * getImageFromThemeXml(const std::string &name, const std::string &name2)
Definition: theme.cpp:926
Color mForegroundColor2
Definition: widget2.h:113
virtual void setWidget2(const Widget2 *const widget)
Definition: widget2.h:64
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
Definition: widget.h:99
Color mForegroundColor
Definition: widget.h:1086
Rect mDimension
Definition: widget.h:1101
bool mAllowLogic
Definition: widget.h:1160
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:302
void setHeight(const int height)
Definition: widget.cpp:140
bool mRedraw
Definition: widget.h:1164
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
Gui * gui
Definition: gui.cpp:111
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
Settings settings
Definition: settings.cpp:32