1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-07 00:58:39 +02:00

- first part of CIntObject API clean-up.

- - mostly remove usage of (de)activateSomething functions
- - CIntObject's can be safely deleted in active state or without removing from parent first
- - Added CWindowObject to use as base of all windows (will be required for such features as shadows)

Report any crashes or glitches - it should not cause any issues apart from more console output. 

TODO:
- remove redundant (de)activate and show(All) calls
- decrease usage of blitAtLoc\printAtLoc methods
- switch all windows to new base
This commit is contained in:
Ivan Savenko
2012-06-02 15:16:54 +00:00
parent 3df1ddbf86
commit d60f2d57a0
29 changed files with 582 additions and 745 deletions

View File

@ -6,7 +6,7 @@
void CIntObject::activateLClick()
{
GH.lclickable.push_front(this);
active |= LCLICK;
active_m |= LCLICK;
}
void CIntObject::deactivateLClick()
@ -14,17 +14,13 @@ 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 &= ~LCLICK;
}
void CIntObject::clickLeft(tribool down, bool previousState)
{
active_m &= ~LCLICK;
}
void CIntObject::activateRClick()
{
GH.rclickable.push_front(this);
active |= RCLICK;
active_m |= RCLICK;
}
void CIntObject::deactivateRClick()
@ -32,17 +28,13 @@ 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 &= ~RCLICK;
}
void CIntObject::clickRight(tribool down, bool previousState)
{
active_m &= ~RCLICK;
}
void CIntObject::activateHover()
{
GH.hoverable.push_front(this);
active |= HOVER;
active_m |= HOVER;
}
void CIntObject::deactivateHover()
@ -50,17 +42,13 @@ 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 &= ~HOVER;
}
void CIntObject::hover( bool on )
{
active_m &= ~HOVER;
}
void CIntObject::activateKeys()
{
GH.keyinterested.push_front(this);
active |= KEYBOARD;
active_m |= KEYBOARD;
}
void CIntObject::deactivateKeys()
@ -68,17 +56,13 @@ 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 &= ~KEYBOARD;
}
void CIntObject::keyPressed( const SDL_KeyboardEvent & key )
{
active_m &= ~KEYBOARD;
}
void CIntObject::activateMouseMove()
{
GH.motioninterested.push_front(this);
active |= MOVE;
active_m |= MOVE;
}
void CIntObject::deactivateMouseMove()
@ -86,17 +70,41 @@ 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 &= ~MOVE;
active_m &= ~MOVE;
}
void CIntObject::mouseMoved( const SDL_MouseMotionEvent & sEvent )
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 |= TIME;
active_m |= TIME;
}
void CIntObject::deactivateTimer()
@ -104,39 +112,46 @@ 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 &= ~TIME;
active_m &= ~TIME;
}
void CIntObject::tick()
{
}
CIntObject::CIntObject()
CIntObject::CIntObject(int used_, Point pos_):
parent_m(nullptr),
active_m(0),
parent(parent_m),
active(active_m)
{
pressedL = pressedR = hovered = captureAllKeys = strongInterest = false;
toNextTick = active = used = 0;
toNextTick = timerDelay = 0;
used = used_;
recActions = defActions = GH.defActionsDef;
pos.x = 0;
pos.y = 0;
pos.x = pos_.x;
pos.y = pos_.y;
pos.w = 0;
pos.h = 0;
if(GH.captureChildren)
{
assert(GH.createdObj.size());
parent = GH.createdObj.front();
parent->children.push_back(this);
GH.createdObj.front()->addChild(this, true);
}
if(parent->defActions & SHARE_POS)
{
pos.x = parent->pos.x;
pos.y = parent->pos.y;
}
void CIntObject::setTimer(int msToTrigger)
{
if (!active & TIME )
activateTimer();
toNextTick = timerDelay = msToTrigger;
used |= TIME;
}
void CIntObject::onTimer(int timePassed)
{
toNextTick -= timePassed;
if (toNextTick < 0)
{
toNextTick += timerDelay;
tick();
}
else
parent = NULL;
}
void CIntObject::show(SDL_Surface * to)
@ -155,15 +170,23 @@ void CIntObject::showAll(SDL_Surface * to)
if(children[i]->recActions & SHOWALL)
children[i]->showAll(to);
}
}//FIXME: needed?
/*
else
show(to);
show(to);*/
}
void CIntObject::activate()
{
assert(!active);
active |= GENERAL;
if (active_m)
{
if ((used | GENERAL) == active_m)
return;
else
deactivate(); //FIXME: better to avoid such possibility at all
}
active_m |= GENERAL;
activate(used);
if(defActions & ACTIVATE)
@ -194,11 +217,13 @@ void CIntObject::activate(ui16 what)
void CIntObject::deactivate()
{
assert(active);
active &= ~ GENERAL;
deactivate(used);
if (!active_m)
return;
assert(!active);
active_m &= ~ GENERAL;
deactivate(active_m);
assert(!active_m);
if(defActions & DEACTIVATE)
for(size_t i = 0; i < children.size(); i++)
@ -218,7 +243,7 @@ void CIntObject::deactivate(ui16 what)
deactivateMouseMove();
if(what & KEYBOARD)
deactivateKeys();
if(what & TIME) // TIME is special
if(what & TIME)
deactivateTimer();
if(what & WHEEL)
deactivateWheel();
@ -228,15 +253,20 @@ void CIntObject::deactivate(ui16 what)
CIntObject::~CIntObject()
{
assert(!active); //do not delete active obj
if (active_m)
deactivate();
if(defActions & DISPOSE)
for(size_t i = 0; i < children.size(); i++)
if(children[i]->recActions & DISPOSE)
delete children[i];
{
while (!children.empty())
if(children.front()->recActions & DISPOSE)
delete children.front();
else
removeChild(children.front());
}
if(parent && GH.createdObj.size()) //temporary object destroyed
parent->children -= this;
if(parent_m)
parent_m->removeChild(this);
}
void CIntObject::printAtLoc( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::Cornsilk*/, SDL_Surface * dst/*=screen*/ )
@ -274,6 +304,20 @@ void CIntObject::printToLoc( const std::string & text, int x, int y, EFonts font
CSDL_Ext::printTo(text, pos.x + x, pos.y + y, font, kolor, dst);
}
void CIntObject::addUsedEvents(ui16 newActions)
{
if (active_m)
activate(newActions & ~used);
used |= newActions;
}
void CIntObject::removeUsedEvents(ui16 newActions)
{
if (active_m)
deactivate(newActions & used);
used &= ~newActions;
}
void CIntObject::disable()
{
if(active)
@ -282,9 +326,9 @@ void CIntObject::disable()
recActions = DISPOSE;
}
void CIntObject::enable(bool activation)
void CIntObject::enable()
{
if(!active && activation)
if(!active_m && parent_m->active)
activate();
recActions = 255;
@ -300,42 +344,6 @@ bool CIntObject::isItInLoc( const SDL_Rect &rect, const Point &p )
return isItIn(&rect, p.x - pos.x, p.y - pos.y);
}
void CIntObject::activateWheel()
{
GH.wheelInterested.push_front(this);
active |= 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 &= ~WHEEL;
}
void CIntObject::wheelScrolled(bool down, bool in)
{
}
void CIntObject::activateDClick()
{
GH.doubleClickInterested.push_front(this);
active |= 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 &= ~DOUBLECLICK;
}
void CIntObject::onDoubleClick()
{
}
void CIntObject::fitToScreen(int borderWidth, bool propagate)
{
Point newPos = pos.topLeft();
@ -361,48 +369,42 @@ void CIntObject::moveTo( const Point &p, bool propagate /*= true*/ )
moveBy(Point(p.x - pos.x, p.y - pos.y), propagate);
}
void CIntObject::delChild(CIntObject *child)
{
children -= child;
delete child;
}
void CIntObject::addChild(CIntObject *child, bool adjustPosition /*= false*/)
{
assert(!vstd::contains(children, child));
assert(child->parent == NULL);
if (vstd::contains(children, child))
{
tlog4<< "Warning: object already assigned to this parent!\n";
return;
}
if (child->parent_m)
{
tlog4<< "Warning: object already has parent!\n";
child->parent_m->removeChild(child, adjustPosition);
}
children.push_back(child);
child->parent = this;
child->parent_m = this;
if(adjustPosition)
child->pos += pos;
if (!active && child->active)
child->deactivate();
if (active && !child->active)
child->activate();
}
void CIntObject::removeChild(CIntObject *child, bool adjustPosition /*= false*/)
{
if (!child)
return;
assert(vstd::contains(children, child));
assert(child->parent == this);
assert(child->parent_m == this);
children -= child;
child->parent = NULL;
child->parent_m = NULL;
if(adjustPosition)
child->pos -= pos;
}
void CIntObject::changeUsedEvents(ui16 what, bool enable, bool adjust /*= true*/)
{
if(enable)
{
used |= what;
if(adjust && active)
activate(what);
}
else
{
used &= ~what;
if(adjust && active)
deactivate(what);
}
}
void CIntObject::drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &color)
{
CSDL_Ext::drawBorder(sur, r + pos, color);
@ -410,9 +412,9 @@ void CIntObject::drawBorderLoc(SDL_Surface * sur, const Rect &r, const int3 &col
void CIntObject::redraw()
{
if (parent && (type & REDRAW_PARENT))
if (parent_m && (type & REDRAW_PARENT))
{
parent->redraw();
parent_m->redraw();
}
else
{
@ -463,4 +465,4 @@ void CKeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
clickLeft(false, prev);
}
}
}
}