ManaPlus
shortcutwindow.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 "enums/gui/layouttype.h"
27 
29 #include "gui/widgets/tabbedarea.h"
30 
31 #include "gui/widgets/button.h"
33 #include "gui/widgets/layout.h"
34 #include "gui/widgets/scrollarea.h"
36 
38 
39 #include "utils/delete2.h"
40 #include "utils/foreach.h"
41 
42 #include "debug.h"
43 
48 static const int SCROLL_PADDING = 0;
49 
51 
52 ShortcutWindow::ShortcutWindow(const std::string &restrict title,
53  ShortcutContainer *restrict const content,
54  const std::string &restrict skinFile,
55  int width, int height) :
56  Window("Window", Modal_false, nullptr, skinFile),
57  mItems(content),
58  mScrollArea(new ScrollArea(this, mItems, Opaque_false, std::string())),
59  mTabs(nullptr),
60  mPages(),
61  mButtonIndex(0)
62 {
63  setWindowName(title);
65 
66  setShowTitle(false);
67  setResizable(true);
68  setDefaultVisible(false);
69  setSaveVisible(true);
70  setAllowClose(true);
71 
72  mDragOffsetX = 0;
73  mDragOffsetY = 0;
74 
75  if (content != nullptr)
76  content->setSkin(this, mSkin);
77  if (setupWindow != nullptr)
79 
80  setMinWidth(32);
81  setMinHeight(32);
82  if (mItems != nullptr)
83  {
84  const int border = SCROLL_PADDING * 2 + getPadding() * 2;
85  const int bw = mItems->getBoxWidth();
86  const int bh = mItems->getBoxHeight();
87  const int maxItems = mItems->getMaxItems();
88  setMaxWidth(bw * maxItems + border);
89  setMaxHeight(bh * maxItems + border);
90 
91  if (width == 0)
92  width = bw + border;
93  if (height == 0)
94  height = bh * maxItems + border;
95 
96  setDefaultSize(width, height, ImagePosition::LOWER_RIGHT, 0, 0);
97 
98  mBoxesWidth += bw + border;
99  }
100 
103 
104  place(0, 0, mScrollArea, 5, 5).setPadding(0);
105 
106  Layout &layout = getLayout();
107  layout.setRowHeight(0, LayoutType::SET);
108  layout.setMargin(0);
109 
110  loadWindowState();
111  enableVisibleSound(true);
112 }
113 
114 ShortcutWindow::ShortcutWindow(const std::string &restrict title,
115  const std::string &restrict skinFile,
116  const int width, const int height) :
117  Window("Window", Modal_false, nullptr, skinFile),
118  mItems(nullptr),
119  mScrollArea(nullptr),
120  mTabs(CREATEWIDGETR(TabbedArea, this)),
121  mPages(),
122  mButtonIndex(0)
123 {
124  setWindowName(title);
126  setShowTitle(false);
127  setResizable(true);
128  setDefaultVisible(false);
129  setSaveVisible(true);
130  setAllowClose(true);
131 
132  mDragOffsetX = 0;
133  mDragOffsetY = 0;
134 
135  if (setupWindow != nullptr)
137 
138  if ((width != 0) && (height != 0))
139  setDefaultSize(width, height, ImagePosition::LOWER_RIGHT, 0, 0);
140 
141  setMinWidth(32);
142  setMinHeight(32);
143 
144  place(0, 0, mTabs, 5, 5);
145 
146  Layout &layout = getLayout();
147  layout.setRowHeight(0, LayoutType::SET);
148  layout.setMargin(0);
149 
150  loadWindowState();
151  enableVisibleSound(true);
152 }
153 
155 {
156  if (mTabs != nullptr)
157  mTabs->removeAll(true);
158  delete2(mTabs)
159  delete2(mItems)
160 }
161 
162 void ShortcutWindow::addButton(const std::string &text,
163  const std::string &eventName,
164  ActionListener *const listener)
165 {
167  5,
168  new Button(this, text, eventName, BUTTON_SKIN, listener),
169  1,
170  1);
171  Window::widgetResized(Event(nullptr));
172 }
173 
174 void ShortcutWindow::addTab(const std::string &name,
175  ShortcutContainer *const content)
176 {
177  if ((content == nullptr) || (mTabs == nullptr))
178  return;
179  ScrollArea *const scroll = new ScrollArea(this,
180  content,
181  Opaque_false,
182  std::string());
185  content->setSkin(this, mSkin);
186  Tab *const tab = new ShortcutTab(this, name, content);
187  mTabs->addTab(tab, scroll);
188  mPages.push_back(content);
189 }
190 
192 {
193  if (mTabs == nullptr)
194  return 0;
195  return mTabs->getSelectedTabIndex();
196 }
197 
199 {
200  Window::widgetHidden(event);
201  if (mItems != nullptr)
202  mItems->widgetHidden(event);
203  if (mTabs != nullptr)
204  {
205  ScrollArea *const scroll = static_cast<ScrollArea *>(
207  if (scroll != nullptr)
208  {
209  ShortcutContainer *const content = static_cast<ShortcutContainer*>(
210  scroll->getContent());
211 
212  if (content != nullptr)
213  content->widgetHidden(event);
214  }
215  }
216 }
217 
219 {
220  Window::mousePressed(event);
221 
222  if (event.isConsumed())
223  return;
224 
225  if (event.getButton() == MouseButton::LEFT)
226  {
227  mDragOffsetX = event.getX();
228  mDragOffsetY = event.getY();
229  }
230 }
231 
233 {
234  Window::mouseDragged(event);
235 
236  if (event.isConsumed())
237  return;
238 
239  if (canMove() && isMovable() && mMoved)
240  {
241  int newX = std::max(0, getX() + event.getX() - mDragOffsetX);
242  int newY = std::max(0, getY() + event.getY() - mDragOffsetY);
243  newX = std::min(mainGraphics->mWidth - getWidth(), newX);
244  newY = std::min(mainGraphics->mHeight - getHeight(), newY);
245  setPosition(newX, newY);
246  }
247 }
248 
250 {
251  Window::widgetMoved(event);
252  if (mItems != nullptr)
253  mItems->setRedraw(true);
254  FOR_EACH (STD_VECTOR<ShortcutContainer*>::iterator, it, mPages)
255  (*it)->setRedraw(true);
256 }
257 
259 {
260  if (mTabs != nullptr)
261  mTabs->selectNextTab();
262 }
263 
265 {
266  if (mTabs != nullptr)
267  mTabs->selectPrevTab();
268 }
269 
270 #ifdef USE_PROFILER
272 {
273  BLOCK_START("ShortcutWindow::logicChildren")
276 }
277 #endif // USE_PROFILER
const std::string BUTTON_SKIN
Definition: button.h:89
virtual void logicChildren()
Definition: button.h:102
Definition: event.h:79
int mWidth
Definition: graphics.h:484
int mHeight
Definition: graphics.h:485
bool isConsumed() const
LayoutCell & setPadding(int p)
Definition: layoutcell.h:60
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:128
Definition: layout.h:45
void setMargin(int m)
Definition: layout.h:54
MouseButtonT getButton() const
Definition: mouseevent.h:116
int getX() const
Definition: mouseevent.h:127
int getY() const
Definition: mouseevent.h:138
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
Widget * getContent()
void registerWindowForReset(Window *const window)
virtual void setSkin(const Widget2 *const widget, Skin *const skin)
int getBoxHeight() const
int getMaxItems() const
int getBoxWidth() const
TabbedArea * mTabs
void addTab(const std::string &name, ShortcutContainer *const content)
ScrollArea * mScrollArea
void mousePressed(MouseEvent &event)
void widgetHidden(const Event &event)
void widgetMoved(const Event &event)
std::vector< ShortcutContainer * > mPages
ShortcutContainer * mItems
int getTabIndex() const
static int mBoxesWidth
void mouseDragged(MouseEvent &event)
void addButton(const std::string &text, const std::string &eventName, ActionListener *const listener)
Definition: tab.h:88
Widget * getCurrentWidget() const
Definition: tabbedarea.cpp:229
void selectPrevTab()
Definition: tabbedarea.cpp:828
void selectNextTab()
Definition: tabbedarea.cpp:819
void removeAll(const bool del)
Definition: tabbedarea.cpp:730
int getSelectedTabIndex() const
Definition: tabbedarea.cpp:449
void addTab(Tab *const tab, Widget *const widget)
Definition: tabbedarea.cpp:238
virtual void widgetHidden(const Event &event)
void setRedraw(const bool b)
Definition: widget.h:942
int getY() const
Definition: widget.h:288
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getX() const
Definition: widget.h:269
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
int mDragOffsetY
Definition: window.h:642
void setSaveVisible(const bool save)
Definition: window.h:300
void setResizable(const bool resize)
Definition: window.cpp:627
void widgetHidden(const Event &event)
Definition: window.cpp:725
void setMaxWidth(const int width)
Definition: window.cpp:617
void setAllowClose(const bool b)
Definition: window.h:189
Skin * mSkin
Definition: window.h:649
int getTitlePadding() const
Definition: window.h:602
Layout & getLayout()
Definition: window.cpp:1365
void setWindowName(const std::string &name)
Definition: window.h:355
bool mMoved
Definition: window.h:647
void mouseDragged(MouseEvent &event)
Definition: window.cpp:982
int mDragOffsetX
Definition: window.h:635
bool canMove() const
Definition: window.cpp:977
bool isMovable() const
Definition: window.h:576
void setMinHeight(const int height)
Definition: window.cpp:604
void setMaxHeight(const int height)
Definition: window.cpp:622
void setMinWidth(const int width)
Definition: window.cpp:591
void mousePressed(MouseEvent &event)
Definition: window.cpp:836
int getPadding() const
Definition: window.h:504
void enableVisibleSound(bool b)
Definition: window.h:481
void widgetResized(const Event &event)
Definition: window.cpp:655
void widgetMoved(const Event &event)
Definition: window.cpp:720
void setShowTitle(bool flag)
Definition: window.h:235
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setDefaultVisible(const bool save)
Definition: window.h:288
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
void setTitleBarHeight(unsigned int height)
Definition: window.h:513
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Graphics * mainGraphics
Definition: graphics.cpp:109
#define restrict
Definition: localconsts.h:165
#define nullptr
Definition: localconsts.h:45
const bool Modal_false
Definition: modal.h:30
std::vector< std::string > mPages
Definition: statdb.cpp:41
const bool Opaque_false
Definition: opaque.h:30
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
ShortcutWindow * dropShortcutWindow
static const int SCROLL_PADDING
ShortcutWindow * spellShortcutWindow
ShortcutWindow * emoteShortcutWindow
ShortcutWindow * itemShortcutWindow