ManaPlus
gui.h
Go to the documentation of this file.
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 /* _______ __ __ __ ______ __ __ _______ __ __
24  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
25  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
26  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
27  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
28  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
29  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
30  *
31  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
32  *
33  *
34  * Per Larsson a.k.a finalman
35  * Olof Naessén a.k.a jansem/yakslem
36  *
37  * Visit: http://guichan.sourceforge.net
38  *
39  * License: (BSD)
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  * notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  * notice, this list of conditions and the following disclaimer in
47  * the documentation and/or other materials provided with the
48  * distribution.
49  * 3. Neither the name of Guichan nor the names of its contributors may
50  * be used to endorse or promote products derived from this software
51  * without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
59  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #ifndef GUI_GUI_H
67 #define GUI_GUI_H
68 
69 #include "gui/color.h"
70 
73 
74 #include "enums/resources/cursor.h"
75 
76 #include <deque>
77 #include <list>
78 
79 #include "localconsts.h"
80 
81 class Event;
82 class FocusHandler;
83 class FocusListener;
84 class Graphics;
85 class GuiConfigListener;
86 class ImageSet;
87 class KeyEvent;
88 class KeyListener;
89 class MouseEvent;
90 class MouseInput;
91 class Font;
92 class SDLInput;
93 class Widget;
94 class Window;
95 
96 #ifdef USE_SDL2
97 #define MouseStateType uint32_t
98 #else // USE_SDL2
99 #define MouseStateType uint8_t
100 #endif // USE_SDL2
101 
115 class Gui final
116 {
117  public:
121  Gui();
122 
124 
125 
128  ~Gui();
129 
130  void postInit(Graphics *const graphics) A_NONNULL(2);
131 
136  void logic();
137 
138  void slowLogic();
139 
140  void clearFonts();
141 
146  void draw();
147 
151  void videoResized() const;
152 
154  { return mFocusHandler; }
155 
160  { return mGuiFont; }
161 
166  { return mHelpFont; }
167 
172  { return mSecureFont; }
173 
178  { return mNpcFont; }
179 
185  { return mInfoParticleFont; }
186 
190  void setUseCustomCursor(const bool customCursor);
191 
195  void setCursorType(const CursorT index)
196  { mCursorType = index; }
197 
198  void setDoubleClick(const bool b)
199  { mDoubleClick = b; }
200 
201  void updateFonts();
202 
203  bool handleInput();
204 
205  bool handleKeyInput();
206 
207  void resetClickCount();
208 
210 
211  static void getAbsolutePosition(Widget *restrict widget,
212  int &restrict x,
213  int &restrict y);
214 
215  void addGlobalFocusListener(FocusListener* focusListener);
216 
217  void removeGlobalFocusListener(FocusListener* focusListener);
218 
219  void distributeGlobalFocusGainedEvent(const Event &focusEvent);
220 
221  void removeDragged(const Widget *const widget);
222 
223  int getLastMouseX() const
224  { return mLastMouseX; }
225 
226  int getLastMouseY() const
227  { return mLastMouseY; }
228 
229  static MouseStateType getMouseState(int &x, int &y);
230 
239  void setTop(Widget *const top);
240 
247  Widget* getTop() const noexcept2 A_WARN_UNUSED
248  { return mTop; }
249 
257  void setGraphics(Graphics *const graphics) A_NONNULL(2);
258 
268 
276  void setInput(SDLInput *const input) A_NONNULL(2);
277 
287 
297  void addGlobalKeyListener(KeyListener *const keyListener);
298 
306  void removeGlobalKeyListener(KeyListener *const keyListener);
307 
308  bool isLongPress() const
309  { return getMousePressLength() > 250; }
310 
311  int getMousePressLength() const;
312 
313  protected:
314  void handleMouseMoved(const MouseInput &mouseInput);
315 
316  void handleMouseReleased(const MouseInput &mouseInput);
317 
318  void handleMousePressed(const MouseInput &mouseInput);
319 
320  void handleMouseInput();
321 
322  void distributeMouseEvent(Widget *const source,
323  const MouseEventTypeT type,
324  const MouseButtonT button,
325  const int x, const int y,
326  const bool force = false,
327  const bool toSourceOnly = false);
328 
335  void handleMouseWheelMovedDown(const MouseInput& mouseInput);
336 
342  void handleMouseWheelMovedUp(const MouseInput& mouseInput);
343 
349  Widget* getWidgetAt(const int x, const int y) const A_WARN_UNUSED;
350 
356  Widget* getMouseEventSource(const int x,
357  const int y) const A_WARN_UNUSED;
358 
365 
372  void distributeKeyEvent(KeyEvent &event) const;
373 
381 
389 
396  void handleModalFocus();
397 
403  void handleModalFocusGained();
404 
412 
413  private:
418 
423 
428 
433 
437  typedef std::list<KeyListener*> KeyListenerList;
438 
442  typedef KeyListenerList::iterator KeyListenerListIterator;
443 
447  KeyListenerList mKeyListeners;
448 
453 
458 
463 
468 
474 
481 
486  std::deque<Widget*> mWidgetWithMouseQueue;
487 
504 #ifdef ANDROID
505  uint16_t mLastMouseRealX;
506  uint16_t mLastMouseRealY;
507 #endif // ANDROID
508 
509  typedef std::list<FocusListener*> FocusListenerList;
510  typedef FocusListenerList::iterator FocusListenerIterator;
511  FocusListenerList mFocusListeners;
514  time_t mTime;
515  time_t mTime10;
518 };
519 
520 extern Gui *gui;
526 
527 #endif // GUI_GUI_H
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
CursorT mCursorType
Definition: gui.h:503
void distributeKeyEvent(KeyEvent &event) const
Definition: gui.cpp:1297
Color mForegroundColor
Definition: gui.h:512
void removeGlobalFocusListener(FocusListener *focusListener)
Definition: gui.cpp:1129
MouseEvent * createMouseEvent(Window *const widget)
Definition: gui.cpp:974
int mLastMouseY
Definition: gui.h:467
void slowLogic()
Definition: gui.cpp:322
void updateFonts()
Definition: gui.cpp:812
void logic()
Definition: gui.cpp:301
Font * getHelpFont() const
Definition: gui.h:165
void videoResized() const
Definition: gui.cpp:535
#define A_NONNULLPOINTER
Definition: localconsts.h:294
#define MouseStateType
Definition: gui.h:99
Definition: font.h:88
Graphics * getGraphics() const
Definition: gui.cpp:1177
void setDoubleClick(const bool b)
Definition: gui.h:198
FocusListenerList mFocusListeners
Definition: gui.h:511
FocusListenerList::iterator FocusListenerIterator
Definition: gui.h:510
void handleModalFocusReleased()
Definition: gui.cpp:1461
void clearFonts()
Definition: gui.cpp:371
void removeGlobalKeyListener(KeyListener *const keyListener)
Definition: gui.cpp:1197
#define A_WARN_UNUSED
Definition: localconsts.h:172
Definition: window.h:98
ImageSet * mMouseCursors
Definition: gui.h:500
Color mForegroundColor2
Definition: gui.h:513
#define final
Definition: localconsts.h:45
Widget * mTop
Definition: gui.h:417
Font * getInfoParticleFont() const
Definition: gui.h:184
void handleModalFocusGained()
Definition: gui.cpp:1436
bool isLongPress() const
Definition: gui.h:308
void addGlobalKeyListener(KeyListener *const keyListener)
Definition: gui.cpp:1192
Font * getFont() const
Definition: gui.h:159
void draw()
Definition: gui.cpp:469
const Image *restrict const top
Widget * getMouseEventSource(const int x, const int y) const
Definition: gui.cpp:1267
Definition: gui.h:115
#define A_NONNULL(...)
Definition: localconsts.h:179
GuiConfigListener * mConfigListener
Definition: gui.h:488
MouseButtonT mLastMousePressButton
Definition: gui.h:452
void addGlobalFocusListener(FocusListener *focusListener)
Definition: gui.cpp:1124
int mMouseInactivityTimer
Definition: gui.h:502
void handleMouseMoved(const MouseInput &mouseInput)
Definition: gui.cpp:592
Graphics * mGraphics
Definition: gui.h:422
void postInit(Graphics *const graphics)
Definition: gui.cpp:150
void handleMouseWheelMovedUp(const MouseInput &mouseInput)
Definition: gui.cpp:1224
void distributeGlobalFocusGainedEvent(const Event &focusEvent)
Definition: gui.cpp:1134
Gui * gui
Definition: gui.cpp:110
int getLastMouseY() const
Definition: gui.h:226
bool handleInput()
Definition: gui.cpp:387
Font * mInfoParticleFont
Definition: gui.h:492
Gui()
Definition: gui.cpp:113
void setInput(SDLInput *const input)
Definition: gui.cpp:1182
unsigned int mLastMousePressTimeStamp
Definition: gui.h:457
void handleModalMouseInputFocus()
Definition: gui.cpp:1387
void handleMousePressed(const MouseInput &mouseInput)
Definition: gui.cpp:768
~Gui()
Definition: gui.cpp:267
void resetClickCount()
Definition: gui.cpp:968
FocusHandler * mFocusHandler
Definition: gui.h:432
Font * mNpcFont
Definition: gui.h:498
std::deque< Widget * > mWidgetWithMouseQueue
Definition: gui.h:486
Widget * getKeyEventSource() const
Definition: gui.cpp:1283
void setCursorType(const CursorT index)
Definition: gui.h:195
Widget * getWidgetAt(const int x, const int y) const
Definition: gui.cpp:1246
static uint8_t getMouseState(int &x, int &y)
Definition: gui.cpp:1153
void handleMouseInput()
Definition: gui.cpp:1011
time_t mTime10
Definition: gui.h:515
KeyListenerList::iterator KeyListenerListIterator
Definition: gui.h:442
void handleMouseWheelMovedDown(const MouseInput &mouseInput)
Definition: gui.cpp:1202
int mClickCount
Definition: gui.h:473
void setTop(Widget *const top)
Definition: gui.cpp:1162
void setGraphics(Graphics *const graphics)
Definition: gui.cpp:1172
Cursor ::T CursorT
Definition: cursor.h:43
static void getAbsolutePosition(Widget *widget, int &x, int &y)
Definition: gui.cpp:995
FocusHandler * getFocusHandler() const
Definition: gui.h:153
Font * getSecureFont() const
Definition: gui.h:171
SDLInput * mInput
Definition: gui.h:427
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1144
Widget * getTop() const
Definition: gui.h:247
Definition: widget.h:97
void setUseCustomCursor(const bool customCursor)
Definition: gui.cpp:553
Definition: event.h:77
std::list< FocusListener * > FocusListenerList
Definition: gui.h:509
Font * mGuiFont
Definition: gui.h:490
MouseEventType ::T MouseEventTypeT
time_t mTime
Definition: gui.h:514
void handleMouseReleased(const MouseInput &mouseInput)
Definition: gui.cpp:1069
Definition: color.h:74
#define restrict
Definition: localconsts.h:176
Font * getNpcFont() const
Definition: gui.h:177
float mMouseCursorAlpha
Definition: gui.h:501
int getMousePressLength() const
Definition: gui.cpp:1507
int mLastMouseX
Definition: gui.h:462
Font * mHelpFont
Definition: gui.h:494
int getLastMouseX() const
Definition: gui.h:223
bool mDoubleClick
Definition: gui.h:517
bool handleKeyInput()
Definition: gui.cpp:394
void distributeKeyEventToGlobalKeyListeners(KeyEvent &event)
Definition: gui.cpp:1363
MouseButton ::T MouseButtonT
Definition: mousebutton.h:77
SDLInput * getInput() const
Definition: gui.cpp:1187
void handleModalFocus()
Definition: gui.cpp:1411
Font * boldFont
Definition: gui.cpp:111
#define noexcept2
Definition: localconsts.h:49
std::list< KeyListener * > KeyListenerList
Definition: gui.h:437
void distributeMouseEvent(Widget *const source, const MouseEventTypeT type, const MouseButtonT button, const int x, const int y, const bool force=false, const bool toSourceOnly=false)
Definition: gui.cpp:842
MouseButtonT mLastMouseDragButton
Definition: gui.h:480
bool mCustomCursor
Definition: gui.h:516
Font * mSecureFont
Definition: gui.h:496
KeyListenerList mKeyListeners
Definition: gui.h:447
#define RETURNS_NONNULL
Definition: localconsts.h:178