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-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 
24 /* _______ __ __ __ ______ __ __ _______ __ __
25  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
26  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
27  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
28  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
29  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
30  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
31  *
32  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
33  *
34  *
35  * Per Larsson a.k.a finalman
36  * Olof Naessén a.k.a jansem/yakslem
37  *
38  * Visit: http://guichan.sourceforge.net
39  *
40  * License: (BSD)
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  * notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  * notice, this list of conditions and the following disclaimer in
48  * the documentation and/or other materials provided with the
49  * distribution.
50  * 3. Neither the name of Guichan nor the names of its contributors may
51  * be used to endorse or promote products derived from this software
52  * without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
60  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 #ifndef GUI_GUI_H
68 #define GUI_GUI_H
69 
70 #include "gui/color.h"
71 
74 
75 #include "enums/resources/cursor.h"
76 
77 #include <deque>
78 #include <list>
79 
80 #include "localconsts.h"
81 
82 class Event;
83 class FocusHandler;
84 class FocusListener;
85 class Graphics;
86 class GuiConfigListener;
87 class ImageSet;
88 class KeyEvent;
89 class KeyListener;
90 class MouseEvent;
91 class MouseInput;
92 class Font;
93 class SDLInput;
94 class Widget;
95 class Window;
96 
97 #ifdef USE_SDL2
98 #define MouseStateType uint32_t
99 #else // USE_SDL2
100 #define MouseStateType uint8_t
101 #endif // USE_SDL2
102 
116 class Gui final
117 {
118  public:
122  Gui();
123 
125 
126 
129  ~Gui();
130 
131  void postInit(Graphics *const graphics) A_NONNULL(2);
132 
137  void logic();
138 
139  void slowLogic();
140 
141  void clearFonts();
142 
147  void draw();
148 
152  void videoResized() const;
153 
155  { return mFocusHandler; }
156 
161  { return mGuiFont; }
162 
167  { return mHelpFont; }
168 
173  { return mSecureFont; }
174 
179  { return mNpcFont; }
180 
186  { return mInfoParticleFont; }
187 
191  void setUseCustomCursor(const bool customCursor);
192 
196  void setCursorType(const CursorT index)
197  { mCursorType = index; }
198 
199  void setDoubleClick(const bool b)
200  { mDoubleClick = b; }
201 
202  void updateFonts();
203 
204  bool handleInput();
205 
206  bool handleKeyInput();
207 
208  void resetClickCount();
209 
211 
212  static void getAbsolutePosition(Widget *restrict widget,
213  int &restrict x,
214  int &restrict y);
215 
216  void addGlobalFocusListener(FocusListener* focusListener);
217 
218  void removeGlobalFocusListener(FocusListener* focusListener);
219 
220  void distributeGlobalFocusGainedEvent(const Event &focusEvent);
221 
222  void removeDragged(const Widget *const widget);
223 
224  int getLastMouseX() const
225  { return mLastMouseX; }
226 
227  int getLastMouseY() const
228  { return mLastMouseY; }
229 
230  static MouseStateType getMouseState(int &x, int &y);
231 
240  void setTop(Widget *const top);
241 
249  { return mTop; }
250 
258  void setGraphics(Graphics *const graphics) A_NONNULL(2);
259 
269 
277  void setInput(SDLInput *const input) A_NONNULL(2);
278 
288 
298  void addGlobalKeyListener(KeyListener *const keyListener);
299 
307  void removeGlobalKeyListener(KeyListener *const keyListener);
308 
309  bool isLongPress() const
310  { return getMousePressLength() > 250; }
311 
312  int getMousePressLength() const;
313 
314  protected:
315  void handleMouseMoved(const MouseInput &mouseInput);
316 
317  void handleMouseReleased(const MouseInput &mouseInput);
318 
319  void handleMousePressed(const MouseInput &mouseInput);
320 
321  void handleMouseInput();
322 
323  void distributeMouseEvent(Widget *const source,
324  const MouseEventTypeT type,
325  const MouseButtonT button,
326  const int x, const int y,
327  const bool force,
328  const bool toSourceOnly);
329 
336  void handleMouseWheelMovedDown(const MouseInput& mouseInput);
337 
343  void handleMouseWheelMovedUp(const MouseInput& mouseInput);
344 
350  Widget* getWidgetAt(const int x, const int y) const A_WARN_UNUSED;
351 
357  Widget* getMouseEventSource(const int x,
358  const int y) const A_WARN_UNUSED;
359 
366 
373  void distributeKeyEvent(KeyEvent &event) const;
374 
382 
390 
397  void handleModalFocus();
398 
404  void handleModalFocusGained();
405 
413 
414  private:
419 
424 
429 
434 
438  typedef std::list<KeyListener*> KeyListenerList;
439 
444 
449 
454 
459 
464 
469 
475 
482 
488 
505 #ifdef ANDROID
506  uint16_t mLastMouseRealX;
507  uint16_t mLastMouseRealY;
508 #endif // ANDROID
509 
510  typedef std::list<FocusListener*> FocusListenerList;
511  typedef FocusListenerList::iterator FocusListenerIterator;
515  time_t mTime;
516  time_t mTime10;
519 };
520 
521 extern Gui *gui;
527 
528 #endif // GUI_GUI_H
Definition: color.h:76
Definition: event.h:79
Definition: font.h:90
Definition: gui.h:117
Font * mHelpFont
Definition: gui.h:495
bool isLongPress() const
Definition: gui.h:309
bool mCustomCursor
Definition: gui.h:517
std::deque< Widget * > mWidgetWithMouseQueue
Definition: gui.h:487
time_t mTime
Definition: gui.h:515
void removeGlobalKeyListener(KeyListener *const keyListener)
Definition: gui.cpp:1215
Color mForegroundColor2
Definition: gui.h:514
void distributeGlobalFocusGainedEvent(const Event &focusEvent)
Definition: gui.cpp:1152
Widget * getMouseEventSource(const int x, const int y) const
Definition: gui.cpp:1290
int getMousePressLength() const
Definition: gui.cpp:1530
SDLInput * mInput
Definition: gui.h:428
std::list< KeyListener * > KeyListenerList
Definition: gui.h:438
KeyListenerList::iterator KeyListenerListIterator
Definition: gui.h:443
void handleMouseMoved(const MouseInput &mouseInput)
Definition: gui.cpp:593
CursorT mCursorType
Definition: gui.h:504
int mMouseInactivityTimer
Definition: gui.h:503
MouseButtonT mLastMouseDragButton
Definition: gui.h:481
SDLInput * getInput() const
Definition: gui.cpp:1205
void handleModalFocusGained()
Definition: gui.cpp:1459
static uint8_t getMouseState(int &x, int &y)
Definition: gui.cpp:1171
Widget * getTop() const
Definition: gui.h:248
unsigned int mLastMousePressTimeStamp
Definition: gui.h:458
void setInput(SDLInput *const input)
Definition: gui.cpp:1200
void setUseCustomCursor(const bool customCursor)
Definition: gui.cpp:554
~Gui()
Definition: gui.cpp:268
void draw()
Definition: gui.cpp:470
Widget * getWidgetAt(const int x, const int y) const
Definition: gui.cpp:1268
int getLastMouseX() const
Definition: gui.h:224
Widget * mTop
Definition: gui.h:418
int getLastMouseY() const
Definition: gui.h:227
bool mDoubleClick
Definition: gui.h:518
void handleMouseInput()
Definition: gui.cpp:1023
Font * getNpcFont() const
Definition: gui.h:178
FocusListenerList::iterator FocusListenerIterator
Definition: gui.h:511
void removeGlobalFocusListener(FocusListener *focusListener)
Definition: gui.cpp:1147
int mLastMouseY
Definition: gui.h:468
void distributeMouseEvent(Widget *const source, const MouseEventTypeT type, const MouseButtonT button, const int x, const int y, const bool force, const bool toSourceOnly)
Definition: gui.cpp:854
void setCursorType(const CursorT index)
Definition: gui.h:196
std::list< FocusListener * > FocusListenerList
Definition: gui.h:510
void slowLogic()
Definition: gui.cpp:323
Font * mGuiFont
Definition: gui.h:491
Graphics * getGraphics() const
Definition: gui.cpp:1195
Font * mInfoParticleFont
Definition: gui.h:493
static void getAbsolutePosition(Widget *widget, int &x, int &y)
Definition: gui.cpp:1007
KeyListenerList mKeyListeners
Definition: gui.h:448
time_t mTime10
Definition: gui.h:516
void addGlobalKeyListener(KeyListener *const keyListener)
Definition: gui.cpp:1210
GuiConfigListener * mConfigListener
Definition: gui.h:489
FocusHandler * getFocusHandler() const
Definition: gui.h:154
void handleModalFocusReleased()
Definition: gui.cpp:1484
Font * getInfoParticleFont() const
Definition: gui.h:185
FocusHandler * mFocusHandler
Definition: gui.h:433
void logic()
Definition: gui.cpp:302
void videoResized() const
Definition: gui.cpp:536
void handleMousePressed(const MouseInput &mouseInput)
Definition: gui.cpp:774
void updateFonts()
Definition: gui.cpp:824
void handleModalMouseInputFocus()
Definition: gui.cpp:1410
Font * mSecureFont
Definition: gui.h:497
int mClickCount
Definition: gui.h:474
bool handleKeyInput()
Definition: gui.cpp:395
FocusListenerList mFocusListeners
Definition: gui.h:512
void setDoubleClick(const bool b)
Definition: gui.h:199
float mMouseCursorAlpha
Definition: gui.h:502
MouseEvent * createMouseEvent(Window *const widget)
Definition: gui.cpp:986
ImageSet * mMouseCursors
Definition: gui.h:501
Gui()
Definition: gui.cpp:114
Graphics * mGraphics
Definition: gui.h:423
void distributeKeyEventToGlobalKeyListeners(KeyEvent &event)
Definition: gui.cpp:1386
Font * getFont() const
Definition: gui.h:160
void resetClickCount()
Definition: gui.cpp:980
MouseButtonT mLastMousePressButton
Definition: gui.h:453
void handleMouseWheelMovedUp(const MouseInput &mouseInput)
Definition: gui.cpp:1244
void distributeKeyEvent(KeyEvent &event) const
Definition: gui.cpp:1320
int mLastMouseX
Definition: gui.h:463
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1162
void handleMouseReleased(const MouseInput &mouseInput)
Definition: gui.cpp:1081
void postInit(Graphics *const graphics)
Definition: gui.cpp:151
void clearFonts()
Definition: gui.cpp:372
Font * mNpcFont
Definition: gui.h:499
Font * getHelpFont() const
Definition: gui.h:166
void setGraphics(Graphics *const graphics)
Definition: gui.cpp:1190
void handleMouseWheelMovedDown(const MouseInput &mouseInput)
Definition: gui.cpp:1220
void handleModalFocus()
Definition: gui.cpp:1434
void addGlobalFocusListener(FocusListener *focusListener)
Definition: gui.cpp:1142
Font * getSecureFont() const
Definition: gui.h:172
void setTop(Widget *const top)
Definition: gui.cpp:1180
Widget * getKeyEventSource() const
Definition: gui.cpp:1306
Color mForegroundColor
Definition: gui.h:513
bool handleInput()
Definition: gui.cpp:388
Definition: widget.h:99
Definition: window.h:102
Cursor ::T CursorT
Definition: cursor.h:44
const Image *restrict const top
Gui * gui
Definition: gui.cpp:111
Font * boldFont
Definition: gui.cpp:112
#define MouseStateType
Definition: gui.h:100
#define restrict
Definition: localconsts.h:165
#define RETURNS_NONNULL
Definition: localconsts.h:167
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define A_NONNULL(...)
Definition: localconsts.h:168
#define A_NONNULLPOINTER
Definition: localconsts.h:266
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
MouseButton ::T MouseButtonT
Definition: mousebutton.h:78
MouseEventType ::T MouseEventTypeT