Game Development Reference
some people can type very fast, any artificial delay in the text boxes input is going to
severely hinder the user's experience and will give the impression that the software is run-
ning slowly; it must not, however, be so fast that holding a key for a fraction of a second
would display the character multiple times. This means we need to detect a long key press
andinthiscase, provideasmall delay andthenaccelerate therate ofcharacter entry,thisis
especially true for backspace, delete and the space bar.
Another thing that is expected during text entry is a caret, typically the caret is a thin pipe
that blinks to represent the location at which the next character will be displayed, in some
cases it may be a small square or some other shape that may depend on the art direction.
The caret as we discussed is a thin pipe that blinks to represents the position at which char-
acters will be displayed anytime we do any kind of text entry, as such it benefits of being
implemented as a reusable object.
Fortunately, it takes very little code to implement a nice looking caret that gently fades
rather than an abrupt blink in/out.
The main part of the caret's behavior takes place in the Update function, every frame we
decrease m_blinkTimer by the frame's deltaTime , when it reaches zero, we flip the toggle
and reset the timer. The alpha component of the caret's color is determined as the ratio of
how much time has elapsed and a target duration.
void Update(float deltaTime)
m_blinkTimer -= deltaTime;
if ( m_blinkTimer <= 0.f )
m_blinkTimer = m_blinkRate;
m_visible = !m_visible;
m_color.A() = m_toggle ? 1.f - (m_blinkTimer / m_blinkDuration) : (m_blinkTimer / m_blinkDuration);
Rendering the caret is a straightforward quad render that uses a texture or solid color with
the position and size defined in m_rectangle .
void Draw(std::shared_ptr<render::texture> texture, std::shared_ptr<DirectX::SpriteBatch> spriteBatch)