First I will present some code, then make a description of a problem:
class CGUIObject
{
protected:
int m_id;
bool m_bVisible;
// Other non-relevant fields and methods specific for gui object...
};
class CClickable
{
private:
bool m_bClicked;
public:
bool isClicked();
void setClicked(bool bClicked);
virtual bool wasClicked(const TPoint& clickPos) = 0;
// Other non-relevant fields and methods specific for clickable object...
};
class CComponent : public CGUIObject
{
// The only important part of this class is that it derives from CGUIObject
};
class CButton : public CComponent, CClickable
{
// The only important part of this class is that it derives from CComponent and CClickable
};
// Now there is a method in my EventManager which informs all clickables about left mouse click event
void CEventManager::lMouseButtonClickEvent(const CPoint& clickPos)
{
// Go through all clickables
for(unsigned int i = 0; i < m_clickableObjectsList.size(); i++)
{
TClickable* obj = m_clickableObjectsList[i];
// Here I would like to also check if obj is visible
if(obj->wasClicked(clickPos))
{
obj->setClicked(true);
if(obj->m_pOnClickListener != nullptr)
obj->m_pOnClickListener->onClick();
return; // Only one object can be clicked at once
}
}
}
Ok, so as you can see:
- CButton derives both from CComponent and CClickable
- CComponent derives from CGUIObject
- CGUIObject has
m_bVisiblefield which is important for me - In EventManager I have created a list of CClickable* objects
Now I'd like to inform specific CClickable object which was clicked but ONLY if it's visible. I know that all clickables also derive from CGUIObject (e.g. CButton), but it's a list of CClickable* so it's understandable I can't get access to m_bVisible field. I know it simply shows I've made a mistake in designing, but is there way to resolve this problem in some elegant and easy way?
Clickableobjects one at a time to check whether they are visible (and the event should be dispatched to them) or they are not (and nothing has to be done). Therefore, this will take a time at least proportional to the number ofClickableobjects, even when only a few are actually of concern.