Game Development Reference
In-Depth Information
is trivial, but it illustrates how easily we can add behaviors that respond to user input,
sounds can be played when the mouse cursor enters or leaves a control, animations could
be triggered on the control itself, even gameplay systems could listen for this events and
cause elements in the game world to react to the player's input.
4.3.2 Interface
Somecontrolsmayrequirecalculationstobeperformedeachframe,wecanimplementthis
in the Update function and it can be different for each deriving control. One common be-
havior in the base Update function is to set the alpha of both foreground and background
colors to that of the m_alpha property to make sure the control always has the same trans-
parency.
void control::Update(float deltaTime)
{
m_backgroundColor.A() = m_foregroundColor.A() = m_alpha;
}
Each control will need to be able to draw itself, for this we can implement a virtual draw
function. As it happens often in games, in some situations users may want to alter the way
an instance of a control looks, without changing all the existing controls or using a custom
object for one place. We can provide a way for the drawing of a control to be provided by
an external source by adding a custom draw function.
void control::Draw()
{
if ( m_customDrawFunction != nullptr )
{
m_customDrawFunction(*this);
}
else
{
InternalDraw();
}
}
Drawing a control then means, checking if a custom draw function is available, if it is, we
will use it and we will pass the control itself as reference to this function. Otherwise we
will call the interface's virtual InternalDraw function, which is where the default appear-
ance of a control will be drawn.
For example, we could provide a custom draw function using a lambda that overrides the
background color of a progress bar and pads its size a little bit.
Search WWH ::




Custom Search