GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/gui.h Lines: 6 10 60.0 %
Date: 2017-11-29 Branches: 0 0 0.0 %

Line Branch Exec Source
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
71
#include "enums/events/mousebutton.h"
72
#include "enums/events/mouseeventtype.h"
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
102
/**
103
 * \defgroup GUI Core GUI related classes (widgets)
104
 */
105
106
/**
107
 * \defgroup Interface User interface related classes (windows, dialogs)
108
 */
109
110
/**
111
 * Main GUI class.
112
 *
113
 * \ingroup GUI
114
 */
115
class Gui final
116
{
117
    public:
118
        /**
119
         * Constructor.
120
         */
121
        Gui();
122
123
        A_DELETE_COPY(Gui)
124
125
        /**
126
         * Destructor.
127
         */
128
        ~Gui();
129
130
        void postInit(Graphics *const graphics) A_NONNULL(2);
131
132
        /**
133
         * Performs logic of the GUI. Overridden to track mouse pointer
134
         * activity.
135
         */
136
        void logic();
137
138
        void slowLogic();
139
140
        void clearFonts();
141
142
        /**
143
         * Draws the whole Gui by calling draw functions down in the
144
         * Gui hierarchy. It also draws the mouse pointer.
145
         */
146
        void draw();
147
148
        /**
149
         * Called when the application window has been resized.
150
         */
151
        void videoResized() const;
152
153
        FocusHandler *getFocusHandler() const noexcept2 A_WARN_UNUSED
154
        { return mFocusHandler; }
155
156
        /**
157
         * Return game font.
158
         */
159
        Font *getFont() const RETURNS_NONNULL A_WARN_UNUSED
160
62
        { return mGuiFont; }
161
162
        /**
163
         * Return help font.
164
         */
165
        Font *getHelpFont() const RETURNS_NONNULL A_WARN_UNUSED
166
14
        { return mHelpFont; }
167
168
        /**
169
         * Return secure font.
170
         */
171
        Font *getSecureFont() const RETURNS_NONNULL A_WARN_UNUSED
172
        { return mSecureFont; }
173
174
        /**
175
         * Return npc font.
176
         */
177
        Font *getNpcFont() const RETURNS_NONNULL A_WARN_UNUSED
178
6
        { return mNpcFont; }
179
180
        /**
181
         * Return the Font used for "Info Particles", i.e. ones showing, what
182
         * you picked up, etc.
183
         */
184
        Font *getInfoParticleFont() const RETURNS_NONNULL A_WARN_UNUSED
185
        { return mInfoParticleFont; }
186
187
        /**
188
         * Sets whether a custom cursor should be rendered.
189
         */
190
        void setUseCustomCursor(const bool customCursor);
191
192
        /**
193
         * Sets which cursor should be used.
194
         */
195
        void setCursorType(const CursorT index)
196
11
        { mCursorType = index; }
197
198
        void setDoubleClick(const bool b)
199
248
        { mDoubleClick = b; }
200
201
        void updateFonts();
202
203
        bool handleInput();
204
205
        bool handleKeyInput();
206
207
        void resetClickCount();
208
209
        MouseEvent *createMouseEvent(Window *const widget) A_WARN_UNUSED;
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
231
        /**
232
         * Sets the top widget. The top widget is the root widget
233
         * of the GUI. If you want a GUI to be able to contain more
234
         * than one widget the top widget should be a container.
235
         *
236
         * @param top The top widget.
237
         * @see Container
238
         */
239
        void setTop(Widget *const top);
240
241
        /**
242
         * Gets the top widget. The top widget is the root widget
243
         * of the GUI.
244
         *
245
         * @return The top widget. NULL if no top widget has been set.
246
         */
247
        Widget* getTop() const noexcept2 A_WARN_UNUSED
248
132
        { return mTop; }
249
250
        /**
251
         * Sets the graphics object to use for drawing.
252
         *
253
         * @param graphics The graphics object to use for drawing.
254
         * @see getGraphics, AllegroGraphics, HGEGraphics,
255
         *      OpenLayerGraphics, OpenGLGraphics, SDLGraphics
256
         */
257
        void setGraphics(Graphics *const graphics) A_NONNULL(2);
258
259
        /**
260
         * Gets the graphics object used for drawing.
261
         *
262
         *  @return The graphics object used for drawing. NULL if no
263
         *          graphics object has been set.
264
         * @see setGraphics, AllegroGraphics, HGEGraphics,
265
         *      OpenLayerGraphics, OpenGLGraphics, SDLGraphics
266
         */
267
        Graphics* getGraphics() const RETURNS_NONNULL A_WARN_UNUSED;
268
269
        /**
270
         * Sets the input object to use for input handling.
271
         *
272
         * @param input The input object to use for input handling.
273
         * @see getInput, AllegroInput, HGEInput, OpenLayerInput,
274
         *      SDLInput
275
         */
276
        void setInput(SDLInput *const input) A_NONNULL(2);
277
278
        /**
279
         * Gets the input object being used for input handling.
280
         *
281
         *  @return The input object used for handling input. NULL if no
282
         *          input object has been set.
283
         * @see setInput, AllegroInput, HGEInput, OpenLayerInput,
284
         *      SDLInput
285
         */
286
        SDLInput* getInput() const A_WARN_UNUSED;
287
288
        /**
289
         * Adds a global key listener to the Gui. A global key listener
290
         * will receive all key events generated from the GUI and global
291
         * key listeners will receive the events before key listeners
292
         * of widgets.
293
         *
294
         * @param keyListener The key listener to add.
295
         * @see removeGlobalKeyListener
296
         */
297
        void addGlobalKeyListener(KeyListener *const keyListener);
298
299
        /**
300
         * Removes global key listener from the Gui.
301
         *
302
         * @param keyListener The key listener to remove.
303
         * @throws Exception if the key listener hasn't been added.
304
         * @see addGlobalKeyListener
305
         */
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
329
        /**
330
         *
331
         * Handles mouse wheel moved down input.
332
         *
333
         * @param mouseInput The mouse input to handle.
334
         */
335
        void handleMouseWheelMovedDown(const MouseInput& mouseInput);
336
337
        /**
338
         * Handles mouse wheel moved up input.
339
         *
340
         * @param mouseInput The mouse input to handle.
341
         */
342
        void handleMouseWheelMovedUp(const MouseInput& mouseInput);
343
344
        /**
345
         * Gets the widget at a certain position.
346
         *
347
         * @return The widget at a certain position.
348
         */
349
        Widget* getWidgetAt(const int x, const int y) const A_WARN_UNUSED;
350
351
        /**
352
         * Gets the source of the mouse event.
353
         *
354
         * @return The source widget of the mouse event.
355
         */
356
        Widget* getMouseEventSource(const int x,
357
                                    const int y) const A_WARN_UNUSED;
358
359
        /**
360
         * Gets the source of the key event.
361
         *
362
         * @return The source widget of the key event.
363
         */
364
        Widget* getKeyEventSource() const A_WARN_UNUSED;
365
366
        /**
367
         * Distributes a key event.
368
         *
369
         * @param event The key event to distribute.
370
371
         */
372
        void distributeKeyEvent(KeyEvent &event) const;
373
374
        /**
375
         * Distributes a key event to the global key listeners.
376
         *
377
         * @param event The key event to distribute.
378
         *
379
         */
380
        void distributeKeyEventToGlobalKeyListeners(KeyEvent& event);
381
382
        /**
383
         * Handles modal mouse input focus. Modal mouse input focus needs
384
         * to be checked at each logic iteration as it might be necessary to
385
         * distribute mouse entered or mouse exited events.
386
         *
387
         */
388
        void handleModalMouseInputFocus();
389
390
        /**
391
         * Handles modal focus. Modal focus needs to be checked at
392
         * each logic iteration as it might be necessary to distribute
393
         * mouse entered or mouse exited events.
394
         *
395
         */
396
        void handleModalFocus();
397
398
        /**
399
         * Handles modal focus gained. If modal focus has been gained it might
400
         * be necessary to distribute mouse entered or mouse exited events.
401
         *
402
         */
403
        void handleModalFocusGained();
404
405
        /**
406
         * Handles modal mouse input focus gained. If modal focus has been
407
         * gained it might be necessary to distribute mouse entered or mouse
408
         * exited events.
409
         *
410
         */
411
        void handleModalFocusReleased();
412
413
    private:
414
        /**
415
         * Holds the top widget.
416
         */
417
        Widget* mTop;
418
419
        /**
420
         * Holds the graphics implementation used.
421
         */
422
        Graphics* mGraphics A_NONNULLPOINTER;
423
424
        /**
425
         * Holds the input implementation used.
426
         */
427
        SDLInput* mInput A_NONNULLPOINTER;
428
429
        /**
430
         * Holds the focus handler for the Gui.
431
         */
432
        FocusHandler* mFocusHandler A_NONNULLPOINTER;
433
434
        /**
435
         * Typedef.
436
         */
437
        typedef std::list<KeyListener*> KeyListenerList;
438
439
        /**
440
         * Typedef.
441
         */
442
        typedef KeyListenerList::iterator KeyListenerListIterator;
443
444
        /**
445
         * Holds the global key listeners of the Gui.
446
         */
447
        KeyListenerList mKeyListeners;
448
449
        /**
450
         * Holds the last mouse button pressed.
451
         */
452
        MouseButtonT mLastMousePressButton;
453
454
        /**
455
         * Holds the last mouse press time stamp.
456
         */
457
        unsigned int mLastMousePressTimeStamp;
458
459
        /**
460
         * Holds the last mouse x coordinate.
461
         */
462
        int mLastMouseX;
463
464
        /**
465
         * Holds the last mouse y coordinate.
466
         */
467
        int mLastMouseY;
468
469
        /**
470
         * Holds the current click count. Used to keep track
471
         * of clicks for a the last pressed button.
472
         */
473
        int mClickCount;
474
475
        /**
476
         * Holds the last button used when a drag of a widget
477
         * was initiated. Used to be able to release a drag
478
         * when the same button is released.
479
         */
480
        MouseButtonT mLastMouseDragButton;
481
482
        /**
483
         * Holds a stack with all the widgets with the mouse.
484
         * Used to properly distribute mouse events.
485
         */
486
        std::deque<Widget*> mWidgetWithMouseQueue;
487
488
        GuiConfigListener *mConfigListener;
489
        /** The global GUI font */
490
        Font *mGuiFont A_NONNULLPOINTER;
491
        /** Font for Info Particles */
492
        Font *mInfoParticleFont A_NONNULLPOINTER;
493
        /** Font for Help Window */
494
        Font *mHelpFont A_NONNULLPOINTER;
495
        /** Font for secure labels */
496
        Font *mSecureFont A_NONNULLPOINTER;
497
        /** Font for npc text */
498
        Font *mNpcFont A_NONNULLPOINTER;
499
        /** Mouse cursor images */
500
        ImageSet *mMouseCursors;
501
        float mMouseCursorAlpha;
502
        int mMouseInactivityTimer;
503
        CursorT mCursorType;
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;
512
        Color mForegroundColor;
513
        Color mForegroundColor2;
514
        time_t mTime;
515
        time_t mTime10;
516
        bool mCustomCursor;                 /**< Show custom cursor */
517
        bool mDoubleClick;
518
};
519
520
extern Gui *gui;                            /**< The GUI system */
521
522
/**
523
 * Bolded text font
524
 */
525
extern Font *boldFont A_NONNULLPOINTER;
526
527
#endif  // GUI_GUI_H