Game Development Reference
In-Depth Information
static std::shared_ptr<TextBlock>
Create(const wchar_t *font, const wchar_t
*text, float x, float y, Placement placement);
};
Here you can see that we have implemented the pure virtual methods in
Con-
trolBase
, as well as some get/set methods for the text and color variables. Another
method of note here is the
MeasureString()
method, which has the same imple-
mentation that we had earlier. We also have a helper that sets common parameters,
such as the position and placement, to make our creation code cleaner.
Finally we used the
std::function
class to allow for a function to be stored so
that it can be called when the control is clicked on. With this we can use lambdas or
normal functions to implement code that will run when the control is clicked on, and
the
std::function
(inside
<functional>
) makes adding this in easy.
void TextBlock::HandleClick()
{
if (_clickHandler != nullptr)
_clickHandler(this);
}
We allow for external code to run if the
TextBlock
is clicked on, so we need a nice
way to trigger this code. For this we just ensure that an external method has actually
been associated with
_clickHandler
, and then if one has been associated we can
call that method.
void
TextBlock::Draw(std::shared_ptr<SpriteBatch>
sb, bool focused)
{
if (_text.empty()) return;
// Calculate position
DirectX::XMFLOAT2 origin =
DirectX::XMFLOAT2(0, 0);