2011-12-17 21:59:59 +03:00
# pragma once
# include "../../lib/CStopWatch.h"
2011-12-22 16:05:19 +03:00
# include "Geometries.h"
2014-05-21 19:04:34 +03:00
# include "SDL_Extensions.h"
2011-12-17 21:59:59 +03:00
2011-12-22 16:05:19 +03:00
class CFramerateManager ;
2012-12-22 23:05:08 +03:00
class CGStatusBar ;
2011-12-17 21:59:59 +03:00
class CIntObject ;
class IUpdateable ;
class IShowActivatable ;
class IShowable ;
/*
* CGuiHandler . h , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
2011-12-22 16:05:19 +03:00
// A fps manager which holds game updates at a constant rate
class CFramerateManager
{
private :
double rateticks ;
ui32 lastticks , timeElapsed ;
int rate ;
public :
int fps ; // the actual fps value
CFramerateManager ( int rate ) ; // initializes the manager with a given fps rate
void init ( ) ; // needs to be called directly before the main game loop to reset the internal timer
void framerateDelay ( ) ; // needs to be called every game update cycle
2012-06-13 16:04:06 +03:00
ui32 getElapsedMilliseconds ( ) const { return this - > timeElapsed ; }
2011-12-22 16:05:19 +03:00
} ;
2011-12-17 21:59:59 +03:00
// Handles GUI logic and drawing
class CGuiHandler
{
public :
2011-12-22 16:05:19 +03:00
CFramerateManager * mainFPSmng ; //to keep const framerate
2011-12-17 21:59:59 +03:00
std : : list < IShowActivatable * > listInt ; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
2012-12-22 23:05:08 +03:00
CGStatusBar * statusbar ;
2011-12-17 21:59:59 +03:00
2012-10-06 23:35:04 +03:00
private :
typedef std : : list < CIntObject * > CIntObjectList ;
2011-12-17 21:59:59 +03:00
//active GUI elements (listening for events
2012-10-06 23:35:04 +03:00
CIntObjectList lclickable ,
rclickable ,
hoverable ,
keyinterested ,
motioninterested ,
timeinterested ,
wheelInterested ,
doubleClickInterested ;
void processLists ( const ui16 activityFlag , std : : function < void ( std : : list < CIntObject * > * ) > cb ) ;
public :
void handleElementActivate ( CIntObject * elem , ui16 activityFlag ) ;
void handleElementDeActivate ( CIntObject * elem , ui16 activityFlag ) ;
public :
2011-12-17 21:59:59 +03:00
//objs to blit
std : : vector < IShowable * > objsToBlit ;
2013-06-26 14:18:27 +03:00
SDL_Event * current ; //current event - can be set to nullptr to stop handling event
2011-12-17 21:59:59 +03:00
IUpdateable * curInt ;
2011-12-22 16:05:19 +03:00
Point lastClick ;
2011-12-17 21:59:59 +03:00
unsigned lastClickTime ;
bool terminate ;
CGuiHandler ( ) ;
~ CGuiHandler ( ) ;
void run ( ) ; // holds the main loop for the whole program after initialization and manages the update/rendering system
void totalRedraw ( ) ; //forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
void simpleRedraw ( ) ; //update only top interface and draw background from buffer, sets a flag, method gets called at the end of the rendering
void popInt ( IShowActivatable * top ) ; //removes given interface from the top and activates next
void popIntTotally ( IShowActivatable * top ) ; //deactivates, deletes, removes given interface from the top and activates next
void pushInt ( IShowActivatable * newInt ) ; //deactivate old top interface, activates this one and pushes to the top
void popInts ( int howMany ) ; //pops one or more interfaces - deactivates top, deletes and removes given number of interfaces, activates new front
IShowActivatable * topInt ( ) ; //returns top interface
void updateTime ( ) ; //handles timeInterested
void handleEvents ( ) ; //takes events from queue and calls interested objects
void handleEvent ( SDL_Event * sEvent ) ;
void handleMouseMotion ( SDL_Event * sEvent ) ;
void handleMoveInterested ( const SDL_MouseMotionEvent & motion ) ;
void fakeMouseMove ( ) ;
void breakEventHandling ( ) ; //current event won't be propagated anymore
void drawFPSCounter ( ) ; // draws the FPS to the upper left corner of the screen
ui8 defActionsDef ; //default auto actions
ui8 captureChildren ; //all newly created objects will get their parents from stack and will be added to parents children list
std : : list < CIntObject * > createdObj ; //stack of objs being created
2012-09-15 22:16:16 +03:00
2011-12-17 21:59:59 +03:00
static SDLKey arrowToNum ( SDLKey key ) ; //converts arrow key to according numpad key
static SDLKey numToDigit ( SDLKey key ) ; //converts numpad digit key to normal digit key
static bool isNumKey ( SDLKey key , bool number = true ) ; //checks if key is on numpad (numbers - check only for numpad digits)
2012-09-15 22:16:16 +03:00
static bool isArrowKey ( SDLKey key ) ;
2012-04-08 04:15:18 +03:00
static bool amIGuiThread ( ) ;
2012-09-11 17:25:19 +03:00
static void pushSDLEvent ( int type , int usercode = 0 ) ;
2011-12-17 21:59:59 +03:00
} ;
extern CGuiHandler GH ; //global gui handler
template < typename T > void pushIntT ( )
{
GH . pushInt ( new T ( ) ) ;
}
struct SObjectConstruction
{
CIntObject * myObj ;
SObjectConstruction ( CIntObject * obj ) ;
~ SObjectConstruction ( ) ;
} ;
struct SSetCaptureState
{
bool previousCapture ;
ui8 prevActions ;
SSetCaptureState ( bool allow , ui8 actions ) ;
~ SSetCaptureState ( ) ;
} ;
# define OBJ_CONSTRUCTION SObjectConstruction obj__i(this)
# define OBJ_CONSTRUCTION_CAPTURING_ALL defActions = 255; SSetCaptureState obj__i1(true, 255); SObjectConstruction obj__i(this)
# define BLOCK_CAPTURING SSetCaptureState obj__i(false, 0)
# define BLOCK_CAPTURING_DONT_TOUCH_REC_ACTIONS SSetCaptureState obj__i(false, GH.defActionsDef)