1
0
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:
alexvins
2012-10-06 20:35:04 +00:00
parent 3f0c225880
commit ff3da291a1
6 changed files with 73 additions and 189 deletions

View File

@ -599,7 +599,7 @@ void CAdvMapInt::activate()
{ {
CIntObject::activate(); CIntObject::activate();
if (!(active & KEYBOARD)) if (!(active & KEYBOARD))
activateKeys(); CIntObject::activate(KEYBOARD);
screenBuf = screen; screenBuf = screen;
GH.statusbar = &statusbar; GH.statusbar = &statusbar;

View File

@ -123,7 +123,7 @@ CPlayerInterface::~CPlayerInterface()
if(adventureInt) if(adventureInt)
{ {
if(adventureInt->active & CIntObject::KEYBOARD) if(adventureInt->active & CIntObject::KEYBOARD)
adventureInt->deactivateKeys(); adventureInt->deactivateKeyboard();
delete adventureInt; delete adventureInt;
adventureInt = NULL; adventureInt = NULL;
} }

View File

@ -43,6 +43,43 @@ SSetCaptureState::~SSetCaptureState()
GH.defActionsDef = prevActions; 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 ) void CGuiHandler::popInt( IShowActivatable *top )
{ {
assert(listInt.front() == top); assert(listInt.front() == top);

View File

@ -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) 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; IStatusBar * statusbar;
private:
typedef std::list<CIntObject*> CIntObjectList;
//active GUI elements (listening for events //active GUI elements (listening for events
std::list<CIntObject*> lclickable; CIntObjectList lclickable,
std::list<CIntObject*> rclickable; rclickable,
std::list<CIntObject*> hoverable; hoverable,
std::list<CIntObject*> keyinterested; keyinterested,
std::list<CIntObject*> motioninterested; motioninterested,
std::list<CIntObject*> timeinterested; timeinterested,
std::list<CIntObject*> wheelInterested; wheelInterested,
std::list<CIntObject*> doubleClickInterested; 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 //objs to blit
std::vector<IShowable*> objsToBlit; std::vector<IShowable*> objsToBlit;

View File

@ -3,118 +3,6 @@
#include "CGuiHandler.h" #include "CGuiHandler.h"
#include "SDL_Extensions.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_): CIntObject::CIntObject(int used_, Point pos_):
parent_m(nullptr), parent_m(nullptr),
active_m(0), active_m(0),
@ -139,7 +27,7 @@ CIntObject::CIntObject(int used_, Point pos_):
void CIntObject::setTimer(int msToTrigger) void CIntObject::setTimer(int msToTrigger)
{ {
if (!(active & TIME)) if (!(active & TIME))
activateTimer(); activate(TIME);
toNextTick = timerDelay = msToTrigger; toNextTick = timerDelay = msToTrigger;
used |= TIME; used |= TIME;
} }
@ -197,22 +85,7 @@ void CIntObject::activate()
void CIntObject::activate(ui16 what) void CIntObject::activate(ui16 what)
{ {
if(what & LCLICK) GH.handleElementActivate(this, what);
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();
} }
void CIntObject::deactivate() void CIntObject::deactivate()
@ -233,22 +106,7 @@ void CIntObject::deactivate()
void CIntObject::deactivate(ui16 what) void CIntObject::deactivate(ui16 what)
{ {
if(what & LCLICK) GH.handleElementDeActivate(this, what);
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();
} }
CIntObject::~CIntObject() CIntObject::~CIntObject()

View File

@ -69,45 +69,27 @@ public:
virtual std::string getCurrent()=0; //returns currently displayed text virtual std::string getCurrent()=0; //returns currently displayed text
}; };
//typedef ui16 ActivityFlag;
// Base UI element // Base UI element
class CIntObject : public IShowActivatable //interface object 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 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 //time handling
int toNextTick; int toNextTick;
int timerDelay; int timerDelay;
void activateTimer();
void deactivateTimer();
void onTimer(int timePassed); void onTimer(int timePassed);
//non-const versions of fields to allow changing them in CIntObject //non-const versions of fields to allow changing them in CIntObject
CIntObject *parent_m; //parent object CIntObject *parent_m; //parent object
ui16 active_m; ui16 active_m;
protected:
//activate or deactivate specific action (LCLICK, RCLICK...)
void activate(ui16 what);
void deactivate(ui16 what);
public: public:
/* /*
@ -115,14 +97,12 @@ public:
* Don't use them unless you really know what they are for * Don't use them unless you really know what they are for
*/ */
std::vector<CIntObject *> children; std::vector<CIntObject *> children;
//keyboard handling //FIXME: workaround
void activateKeys(); void deactivateKeyboard()
void deactivateKeys(); {
deactivate(KEYBOARD);
//mouse movement handling }
void activateMouseMove();
void deactivateMouseMove();
/* /*
* Public interface * Public interface