mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-13 01:20:34 +02:00
[refactor] reduce some code duplication. no functional changes
This commit is contained in:
@ -599,7 +599,7 @@ void CAdvMapInt::activate()
|
||||
{
|
||||
CIntObject::activate();
|
||||
if (!(active & KEYBOARD))
|
||||
activateKeys();
|
||||
CIntObject::activate(KEYBOARD);
|
||||
|
||||
screenBuf = screen;
|
||||
GH.statusbar = &statusbar;
|
||||
|
@ -123,7 +123,7 @@ CPlayerInterface::~CPlayerInterface()
|
||||
if(adventureInt)
|
||||
{
|
||||
if(adventureInt->active & CIntObject::KEYBOARD)
|
||||
adventureInt->deactivateKeys();
|
||||
adventureInt->deactivateKeyboard();
|
||||
delete adventureInt;
|
||||
adventureInt = NULL;
|
||||
}
|
||||
|
@ -43,6 +43,43 @@ SSetCaptureState::~SSetCaptureState()
|
||||
GH.defActionsDef = prevActions;
|
||||
}
|
||||
|
||||
static inline void
|
||||
processList(const ui16 mask, const ui16 flag, std::list<CIntObject*> *lst, std::function<void (std::list<CIntObject*> *)> cb)
|
||||
{
|
||||
if (mask & flag)
|
||||
cb(lst);
|
||||
}
|
||||
|
||||
void CGuiHandler::processLists(const ui16 activityFlag, std::function<void (std::list<CIntObject*> *)> cb)
|
||||
{
|
||||
processList(CIntObject::LCLICK,activityFlag,&lclickable,cb);
|
||||
processList(CIntObject::RCLICK,activityFlag,&rclickable,cb);
|
||||
processList(CIntObject::HOVER,activityFlag,&hoverable,cb);
|
||||
processList(CIntObject::MOVE,activityFlag,&motioninterested,cb);
|
||||
processList(CIntObject::KEYBOARD,activityFlag,&keyinterested,cb);
|
||||
processList(CIntObject::TIME,activityFlag,&timeinterested,cb);
|
||||
processList(CIntObject::WHEEL,activityFlag,&wheelInterested,cb);
|
||||
processList(CIntObject::DOUBLECLICK,activityFlag,&doubleClickInterested,cb);
|
||||
}
|
||||
|
||||
void CGuiHandler::handleElementActivate(CIntObject * elem, ui16 activityFlag)
|
||||
{
|
||||
processLists(activityFlag,[&](CIntObjectList * lst){
|
||||
lst->push_front(elem);
|
||||
});
|
||||
elem->active_m |= activityFlag;
|
||||
}
|
||||
|
||||
void CGuiHandler::handleElementDeActivate(CIntObject * elem, ui16 activityFlag)
|
||||
{
|
||||
processLists(activityFlag,[&](CIntObjectList * lst){
|
||||
CIntObjectList::iterator hlp = std::find(lst->begin(),lst->end(),elem);
|
||||
assert(hlp != lst->end());
|
||||
lst->erase(hlp);
|
||||
});
|
||||
elem->active_m &= ~activityFlag;
|
||||
}
|
||||
|
||||
void CGuiHandler::popInt( IShowActivatable *top )
|
||||
{
|
||||
assert(listInt.front() == top);
|
||||
|
@ -45,16 +45,25 @@ public:
|
||||
std::list<IShowActivatable *> listInt; //list of interfaces - front=foreground; back = background (includes adventure map, window interfaces, all kind of active dialogs, and so on)
|
||||
IStatusBar * statusbar;
|
||||
|
||||
//active GUI elements (listening for events
|
||||
std::list<CIntObject*> lclickable;
|
||||
std::list<CIntObject*> rclickable;
|
||||
std::list<CIntObject*> hoverable;
|
||||
std::list<CIntObject*> keyinterested;
|
||||
std::list<CIntObject*> motioninterested;
|
||||
std::list<CIntObject*> timeinterested;
|
||||
std::list<CIntObject*> wheelInterested;
|
||||
std::list<CIntObject*> doubleClickInterested;
|
||||
private:
|
||||
typedef std::list<CIntObject*> CIntObjectList;
|
||||
|
||||
//active GUI elements (listening for events
|
||||
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:
|
||||
//objs to blit
|
||||
std::vector<IShowable*> objsToBlit;
|
||||
|
||||
|
@ -3,118 +3,6 @@
|
||||
#include "CGuiHandler.h"
|
||||
#include "SDL_Extensions.h"
|
||||
|
||||
void CIntObject::activateLClick()
|
||||
{
|
||||
GH.lclickable.push_front(this);
|
||||
active_m |= LCLICK;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateLClick()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.lclickable.begin(),GH.lclickable.end(),this);
|
||||
assert(hlp != GH.lclickable.end());
|
||||
GH.lclickable.erase(hlp);
|
||||
active_m &= ~LCLICK;
|
||||
}
|
||||
|
||||
void CIntObject::activateRClick()
|
||||
{
|
||||
GH.rclickable.push_front(this);
|
||||
active_m |= RCLICK;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateRClick()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.rclickable.begin(),GH.rclickable.end(),this);
|
||||
assert(hlp != GH.rclickable.end());
|
||||
GH.rclickable.erase(hlp);
|
||||
active_m &= ~RCLICK;
|
||||
}
|
||||
|
||||
void CIntObject::activateHover()
|
||||
{
|
||||
GH.hoverable.push_front(this);
|
||||
active_m |= HOVER;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateHover()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.hoverable.begin(),GH.hoverable.end(),this);
|
||||
assert(hlp != GH.hoverable.end());
|
||||
GH.hoverable.erase(hlp);
|
||||
active_m &= ~HOVER;
|
||||
}
|
||||
|
||||
void CIntObject::activateKeys()
|
||||
{
|
||||
GH.keyinterested.push_front(this);
|
||||
active_m |= KEYBOARD;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateKeys()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.keyinterested.begin(),GH.keyinterested.end(),this);
|
||||
assert(hlp != GH.keyinterested.end());
|
||||
GH.keyinterested.erase(hlp);
|
||||
active_m &= ~KEYBOARD;
|
||||
}
|
||||
|
||||
void CIntObject::activateMouseMove()
|
||||
{
|
||||
GH.motioninterested.push_front(this);
|
||||
active_m |= MOVE;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateMouseMove()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.motioninterested.begin(),GH.motioninterested.end(),this);
|
||||
assert(hlp != GH.motioninterested.end());
|
||||
GH.motioninterested.erase(hlp);
|
||||
active_m &= ~MOVE;
|
||||
}
|
||||
|
||||
void CIntObject::activateWheel()
|
||||
{
|
||||
GH.wheelInterested.push_front(this);
|
||||
active_m |= WHEEL;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateWheel()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.wheelInterested.begin(),GH.wheelInterested.end(),this);
|
||||
assert(hlp != GH.wheelInterested.end());
|
||||
GH.wheelInterested.erase(hlp);
|
||||
active_m &= ~WHEEL;
|
||||
}
|
||||
|
||||
void CIntObject::activateDClick()
|
||||
{
|
||||
GH.doubleClickInterested.push_front(this);
|
||||
active_m |= DOUBLECLICK;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateDClick()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.doubleClickInterested.begin(),GH.doubleClickInterested.end(),this);
|
||||
assert(hlp != GH.doubleClickInterested.end());
|
||||
GH.doubleClickInterested.erase(hlp);
|
||||
active_m &= ~DOUBLECLICK;
|
||||
}
|
||||
|
||||
void CIntObject::activateTimer()
|
||||
{
|
||||
GH.timeinterested.push_back(this);
|
||||
active_m |= TIME;
|
||||
}
|
||||
|
||||
void CIntObject::deactivateTimer()
|
||||
{
|
||||
std::list<CIntObject*>::iterator hlp = std::find(GH.timeinterested.begin(),GH.timeinterested.end(),this);
|
||||
assert(hlp != GH.timeinterested.end());
|
||||
GH.timeinterested.erase(hlp);
|
||||
active_m &= ~TIME;
|
||||
}
|
||||
|
||||
CIntObject::CIntObject(int used_, Point pos_):
|
||||
parent_m(nullptr),
|
||||
active_m(0),
|
||||
@ -139,7 +27,7 @@ CIntObject::CIntObject(int used_, Point pos_):
|
||||
void CIntObject::setTimer(int msToTrigger)
|
||||
{
|
||||
if (!(active & TIME))
|
||||
activateTimer();
|
||||
activate(TIME);
|
||||
toNextTick = timerDelay = msToTrigger;
|
||||
used |= TIME;
|
||||
}
|
||||
@ -197,22 +85,7 @@ void CIntObject::activate()
|
||||
|
||||
void CIntObject::activate(ui16 what)
|
||||
{
|
||||
if(what & LCLICK)
|
||||
activateLClick();
|
||||
if(what & RCLICK)
|
||||
activateRClick();
|
||||
if(what & HOVER)
|
||||
activateHover();
|
||||
if(what & MOVE)
|
||||
activateMouseMove();
|
||||
if(what & KEYBOARD)
|
||||
activateKeys();
|
||||
if(what & TIME)
|
||||
activateTimer();
|
||||
if(what & WHEEL)
|
||||
activateWheel();
|
||||
if(what & DOUBLECLICK)
|
||||
activateDClick();
|
||||
GH.handleElementActivate(this, what);
|
||||
}
|
||||
|
||||
void CIntObject::deactivate()
|
||||
@ -233,22 +106,7 @@ void CIntObject::deactivate()
|
||||
|
||||
void CIntObject::deactivate(ui16 what)
|
||||
{
|
||||
if(what & LCLICK)
|
||||
deactivateLClick();
|
||||
if(what & RCLICK)
|
||||
deactivateRClick();
|
||||
if(what & HOVER)
|
||||
deactivateHover();
|
||||
if(what & MOVE)
|
||||
deactivateMouseMove();
|
||||
if(what & KEYBOARD)
|
||||
deactivateKeys();
|
||||
if(what & TIME)
|
||||
deactivateTimer();
|
||||
if(what & WHEEL)
|
||||
deactivateWheel();
|
||||
if(what & DOUBLECLICK)
|
||||
deactivateDClick();
|
||||
GH.handleElementDeActivate(this, what);
|
||||
}
|
||||
|
||||
CIntObject::~CIntObject()
|
||||
|
@ -69,45 +69,27 @@ public:
|
||||
virtual std::string getCurrent()=0; //returns currently displayed text
|
||||
};
|
||||
|
||||
//typedef ui16 ActivityFlag;
|
||||
|
||||
// Base UI element
|
||||
class CIntObject : public IShowActivatable //interface object
|
||||
{
|
||||
//activate or deactivate specific action (LCLICK, RCLICK...)
|
||||
void activate(ui16 what);
|
||||
void deactivate(ui16 what);
|
||||
|
||||
ui16 used;//change via addUsed() or delUsed
|
||||
|
||||
//l-clicks handling
|
||||
void activateLClick();
|
||||
void deactivateLClick();
|
||||
|
||||
//r-clicks handling
|
||||
void activateRClick();
|
||||
void deactivateRClick();
|
||||
|
||||
//hover handling
|
||||
void activateHover();
|
||||
void deactivateHover();
|
||||
|
||||
//double click
|
||||
void activateDClick();
|
||||
void deactivateDClick();
|
||||
|
||||
//mouse wheel
|
||||
void activateWheel();
|
||||
void deactivateWheel();
|
||||
|
||||
//time handling
|
||||
int toNextTick;
|
||||
int timerDelay;
|
||||
void activateTimer();
|
||||
void deactivateTimer();
|
||||
|
||||
void onTimer(int timePassed);
|
||||
|
||||
//non-const versions of fields to allow changing them in CIntObject
|
||||
CIntObject *parent_m; //parent object
|
||||
ui16 active_m;
|
||||
protected:
|
||||
//activate or deactivate specific action (LCLICK, RCLICK...)
|
||||
void activate(ui16 what);
|
||||
void deactivate(ui16 what);
|
||||
|
||||
public:
|
||||
/*
|
||||
@ -116,13 +98,11 @@ public:
|
||||
*/
|
||||
std::vector<CIntObject *> children;
|
||||
|
||||
//keyboard handling
|
||||
void activateKeys();
|
||||
void deactivateKeys();
|
||||
|
||||
//mouse movement handling
|
||||
void activateMouseMove();
|
||||
void deactivateMouseMove();
|
||||
//FIXME: workaround
|
||||
void deactivateKeyboard()
|
||||
{
|
||||
deactivate(KEYBOARD);
|
||||
}
|
||||
|
||||
/*
|
||||
* Public interface
|
||||
|
Reference in New Issue
Block a user