CUGL 2.0
Cornell University Game Library
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::scene2::TextField Class Reference

#include <CUTextField.h>

Inheritance diagram for cugl::scene2::TextField:
cugl::scene2::Label cugl::scene2::SceneNode

Public Types

typedef std::function< void(const std::string &name, const std::string &current)> Listener
 
- Public Types inherited from cugl::scene2::Label
enum  HAlign : int {
  HAlign::LEFT = 0, HAlign::CENTER = 1, HAlign::RIGHT = 2, HAlign::HARDLEFT = 3,
  HAlign::TRUECENTER = 4, HAlign::HARDRIGHT = 5
}
 
enum  VAlign {
  VAlign::BOTTOM = 0, VAlign::MIDDLE = 1, VAlign::TOP = 2, VAlign::HARDBOTTOM = 3,
  VAlign::TRUEMIDDLE = 4, VAlign::HARDTOP = 5
}
 

Public Member Functions

 TextField ()
 
 ~TextField ()
 
virtual void dispose () override
 
bool hasTypeListener () const
 
const Listener getTypeListener (Uint32 key) const
 
const std::vector< ListenergetTypeListeners () const
 
Uint32 addTypeListener (Listener listener)
 
bool removeTypeListener (Uint32 key)
 
void clearTypeListeners ()
 
bool hasExitListener () const
 
const Listener getExitListener (Uint32 key) const
 
const std::vector< ListenergetExitListeners () const
 
Uint32 addExitListener (Listener listener)
 
bool removeExitListener (Uint32 key)
 
void clearExitListeners ()
 
virtual void setText (const std::string &text, bool resize=false) override
 
bool activate ()
 
bool deactivate (bool dispose=false)
 
bool isActive () const
 
bool requestFocus ()
 
bool releaseFocus ()
 
bool hasFocus () const
 
- Public Member Functions inherited from cugl::scene2::Label
 Label ()
 
 ~Label ()
 
virtual bool init () override
 
bool init (const Size size, const std::shared_ptr< Font > &font)
 
bool initWithText (const char *text, const std::shared_ptr< Font > &font)
 
bool initWithText (const std::string &text, const std::shared_ptr< Font > &font)
 
virtual bool initWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data) override
 
const std::string & getText () const
 
const Vec2 getPadding () const
 
float getPaddingX () const
 
float getPaddingY () const
 
void setPadding (const Vec2 padding)
 
void setPadding (float padx, float pady)
 
void setPaddingX (float padx)
 
void setPaddingY (float pady)
 
HAlign getHorizontalAlignment () const
 
void setHorizontalAlignment (HAlign halign)
 
VAlign getVerticalAlignment () const
 
void setVerticalAlignment (VAlign valign)
 
const Rect getTextBounds () const
 
const Rect getTrueBounds () const
 
float getBaseLine () const
 
virtual void setContentSize (const Size size) override
 
virtual void setContentSize (float width, float height) override
 
Color4 getForeground () const
 
void setForeground (Color4 color)
 
Color4 getBackground () const
 
void setBackground (Color4 color)
 
std::shared_ptr< FontgetFont () const
 
void setFont (const std::shared_ptr< Font > &font, bool resize=false)
 
void setBlendFunc (GLenum srcFactor, GLenum dstFactor)
 
GLenum getSourceBlendFactor () const
 
GLenum getDestinationBlendFactor () const
 
void setBlendEquation (GLenum equation)
 
GLenum getBlendEquation () const
 
- Public Member Functions inherited from cugl::scene2::SceneNode
 SceneNode ()
 
 ~SceneNode ()
 
virtual bool initWithPosition (const Vec2 pos)
 
bool initWithPosition (float x, float y)
 
virtual bool initWithBounds (const Size size)
 
bool initWithBounds (float width, float height)
 
virtual bool initWithBounds (const Rect rect)
 
bool initWithBounds (float x, float y, float width, float height)
 
virtual SceneNodecopy (SceneNode *dst)
 
unsigned int getTag () const
 
void setTag (unsigned int tag)
 
const std::string & getName () const
 
void setName (const std::string &name)
 
virtual std::string toString (bool verbose=false) const
 
 operator std::string () const
 
const Vec2 getPosition () const
 
void setPosition (const Vec2 &position)
 
void setPosition (float x, float y)
 
float getPositionX (void) const
 
void setPositionX (float x)
 
float getPositionY (void) const
 
void setPositionY (float y)
 
Vec2 getWorldPosition () const
 
const Size getContentSize () const
 
float getContentWidth () const
 
void setContentWidth (float width)
 
float getContentHeight () const
 
void setContentHeight (float height)
 
Size getSize () const
 
float getWidth () const
 
float getHeight () const
 
Rect getBoundingBox () const
 
virtual void setAnchor (const Vec2 anchor)
 
void setAnchor (float x, float y)
 
const Vec2 getAnchor () const
 
Vec2 getAnchorInPixels ()
 
Color4 getColor () const
 
virtual void setColor (Color4 color)
 
Color4 getAbsoluteColor ()
 
bool isVisible () const
 
void setVisible (bool visible)
 
bool hasRelativeColor ()
 
void setRelativeColor (bool flag)
 
std::shared_ptr< ScissorgetScissor () const
 
void setScissor (const std::shared_ptr< Scissor > &scissor)
 
void setScissor ()
 
const Vec2 getScale () const
 
float getScaleX () const
 
float getScaleY () const
 
void setScale (float scale)
 
void setScale (const Vec2 vec)
 
void setScale (float sx, float sy)
 
float getAngle ()
 
void setAngle (float angle)
 
const Mat4getAlternateTransform ()
 
void setAlternateTransform (const Mat4 &transform)
 
bool withAlternateTransform ()
 
void chooseAlternateTransform (bool active)
 
const Mat4getNodeToParentTransform () const
 
Mat4 getParentToNodeTransform () const
 
Mat4 getNodeToWorldTransform () const
 
Mat4 getWorldToNodeTransform () const
 
Vec2 screenToNodeCoords (const Vec2 screenPoint) const
 
Vec2 worldToNodeCoords (const Vec2 worldPoint) const
 
Vec2 nodeToScreenCoords (const Vec2 nodePoint) const
 
Vec2 nodeToWorldCoords (const Vec2 nodePoint) const
 
Vec2 parentToNodeCoords (const Vec2 parentPoint) const
 
Vec2 nodeToParentCoords (const Vec2 nodePoint) const
 
void setZOrder (int z)
 
int getZOrder () const
 
bool isZDirty () const
 
void sortZOrder ()
 
size_t getChildCount () const
 
std::shared_ptr< SceneNodegetChild (unsigned int pos)
 
const std::shared_ptr< SceneNode > & getChild (unsigned int pos) const
 
template<typename T >
std::shared_ptr< T > getChild (unsigned int pos) const
 
std::shared_ptr< SceneNodegetChildByTag (unsigned int tag) const
 
template<typename T >
std::shared_ptr< T > getChildByTag (unsigned int tag) const
 
std::shared_ptr< SceneNodegetChildByName (const std::string name) const
 
template<typename T >
std::shared_ptr< T > getChildByName (const std::string name) const
 
std::vector< std::shared_ptr< SceneNode > > getChildren ()
 
const std::vector< std::shared_ptr< SceneNode > > & getChildren () const
 
void addChild (std::shared_ptr< SceneNode > child)
 
virtual void addChild (const std::shared_ptr< SceneNode > &child, int zval)
 
void addChildWithTag (const std::shared_ptr< SceneNode > &child, unsigned int tag)
 
void addChildWithTag (const std::shared_ptr< SceneNode > &child, unsigned int tag, int zval)
 
void addChildWithName (const std::shared_ptr< SceneNode > &child, const std::string &name)
 
void addChildWithName (const std::shared_ptr< SceneNode > &child, const std::string &name, int zval)
 
void swapChild (const std::shared_ptr< SceneNode > &child1, const std::shared_ptr< SceneNode > &child2, bool inherit=false)
 
SceneNodegetParent ()
 
const SceneNodegetParent () const
 
Scene2getScene ()
 
const Scene2getScene () const
 
void removeFromParent ()
 
virtual void removeChild (unsigned int pos)
 
void removeChild (const std::shared_ptr< SceneNode > &child)
 
void removeChildByTag (unsigned int tag)
 
void removeChildByName (const std::string name)
 
virtual void removeAllChildren ()
 
virtual void render (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint)
 
virtual void render (const std::shared_ptr< SpriteBatch > &batch)
 
const std::shared_ptr< Layout > & getLayout () const
 
void setLayout (const std::shared_ptr< Layout > &layout)
 
virtual void doLayout ()
 

Static Public Member Functions

static std::shared_ptr< TextFieldalloc (const Size size, const std::shared_ptr< Font > &font)
 
static std::shared_ptr< TextFieldalloc (const std::string &text, const std::shared_ptr< Font > &font)
 
static std::shared_ptr< TextFieldalloc (const char *text, const std::shared_ptr< Font > &font)
 
static std::shared_ptr< SceneNodeallocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > data)
 
- Static Public Member Functions inherited from cugl::scene2::Label
static std::shared_ptr< Labelalloc (const Size size, const std::shared_ptr< Font > &font)
 
static std::shared_ptr< Labelalloc (const std::string &text, const std::shared_ptr< Font > &font)
 
static std::shared_ptr< Labelalloc (const char *text, const std::shared_ptr< Font > &font)
 
static std::shared_ptr< SceneNodeallocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data)
 
- Static Public Member Functions inherited from cugl::scene2::SceneNode
static std::shared_ptr< SceneNodealloc ()
 
static std::shared_ptr< SceneNodeallocWithPosition (const Vec2 pos)
 
static std::shared_ptr< SceneNodeallocWithPosition (float x, float y)
 
static std::shared_ptr< SceneNodeallocWithBounds (const Size size)
 
static std::shared_ptr< SceneNodeallocWithBounds (float width, float height)
 
static std::shared_ptr< SceneNodeallocWithBounds (const Rect rect)
 
static std::shared_ptr< SceneNodeallocWithBounds (float x, float y, float width, float height)
 
static std::shared_ptr< SceneNodeallocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data)
 

Protected Member Functions

virtual void draw (const std::shared_ptr< SpriteBatch > &batch, const Mat4 &transform, Color4 tint) override
 
bool validate (const std::string &value)
 
void updateInput (const TextInputEvent &event, bool focus)
 
void updateKey (const KeyEvent &event, bool focus, bool down)
 
void updatePress (Vec2 pos, bool focus)
 
void updateCursor ()
 
int skipWord (bool forward)
 
void deleteOne ()
 
bool deleteMany (Uint32 counter)
 

Protected Attributes

Rect _cursor
 
int _cursorBlink
 
int _cursorIndex
 
float _textLength
 
bool _active
 
bool _focused
 
bool _mouse
 
Uint32 _tkey
 
Uint32 _kkey
 
Uint32 _fkey
 
Uint32 _nextKey
 
std::unordered_map< Uint32, Listener_typeListeners
 
std::unordered_map< Uint32, Listener_exitListeners
 
bool _altDown
 
bool _metaDown
 
bool _backDown
 
Uint32 _backCount
 
- Protected Attributes inherited from cugl::scene2::Label
std::shared_ptr< Font_font
 
std::string _text
 
Rect _textbounds
 
Rect _truebounds
 
Vec2 _padding
 
HAlign _halign
 
VAlign _valign
 
Color4 _foreground
 
Color4 _background
 
GLenum _blendEquation
 
GLenum _srcFactor
 
GLenum _dstFactor
 
bool _rendered
 
Mesh< SpriteVertex2_mesh
 
Rect _bounds
 
std::shared_ptr< Texture_texture
 
- Protected Attributes inherited from cugl::scene2::SceneNode
Vec2 _position
 
Vec2 _anchor
 
Size _contentSize
 
Color4 _tintColor
 
bool _hasParentColor
 
bool _isVisible
 
std::shared_ptr< Scissor_scissor
 
Vec2 _scale
 
float _angle
 
Mat4 _transform
 
bool _useTransform
 
Mat4 _combined
 
std::vector< std::shared_ptr< SceneNode > > _children
 
SceneNode_parent
 
Scene2_graph
 
std::shared_ptr< Layout_layout
 
int _childOffset
 
unsigned int _tag
 
std::string _name
 
size_t _hashOfName
 
int _zOrder
 
bool _zDirty
 
std::shared_ptr< JsonValue_json
 

Detailed Description

This class represents a single line text field.

A text field is a subclass of {link Label} that permits the user to edit it when focused (e.g. when it is selected to receive keyboard events). A focused field displays a blinking cursor with the current edit position. There is no cursor displayed when the field does not have focus.

The textfield can track its own state, via the activate() method, relieving you of having to manually the keyboard. In addition, it also reponds to mouse/touch input, allowing you to reposition the cursor and either focus or unfocus the text field. However, the appropriate input devices must be active before you can activate the text fields, as it needs to attach internal listeners. These devices include TextInput, Keyboard, and either Mouse or Touchscreen.

The text field supports two category of listeners. The first tracks any changes to the text. The second only updates when the field loses focus, such as when the user presses return.

Member Typedef Documentation

◆ Listener

This type represents a listener for text change in the TextField class.

In CUGL, listeners are implemented as a set of callback functions, not as objects. This allows each listener to implement as much or as little functionality as it wants. For simplicity, TextField nodes only support a single listener. If you wish for more than one listener, then your listener should handle its own dispatch.

The function type is equivalent to

 std::function<void (const std::string& name, const std::string& current)>
Parameters
nameThe text field name
currentText after editing

Constructor & Destructor Documentation

◆ TextField()

cugl::scene2::TextField::TextField ( )

Creates an uninitialized text field with no size or font.

You must initialize this field before use.

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a Node on the heap, use one of the static constructors instead.

◆ ~TextField()

cugl::scene2::TextField::~TextField ( )
inline

Deletes this text field, disposing all resources

It is unsafe to call this on a text field that is still currently inside of a scene graph.

Member Function Documentation

◆ activate()

bool cugl::scene2::TextField::activate ( )

Activates this text field to enable editing.

This method attaches a listener to either the Mouse or Touchscreen inputs to monitor when the text field is pressed and/or released. The text field will favor the mouse, but will use the touch screen if no mouse input is active. If neither input is active, this method will fail.

It will also attach a listener to TextInput to provide access to a (possibly virtual) keyboard and collect user typing. Finally, it attaches a liatener to Keyboard to monitor special keys such as Alt, Meta, and the arrow keys.

Notice that activating a text field and requesting focus is separate. The field will request focus if selected by a touch or press, but it cannot be editted until it has focus.

Returns
true if the text field was successfully activated

◆ addExitListener()

Uint32 cugl::scene2::TextField::addExitListener ( Listener  listener)

Returns a key for a exit listener after adding it to this field.

This listener is invoked when the field loses focus.

C++ cannot hash functions types. Therefore, the listener will be identified by a unique key, returned by this function. You should remember this key to remove the listener if necessary.

Parameters
listenerThe listener to add
Returns
the key for the listener

◆ addTypeListener()

Uint32 cugl::scene2::TextField::addTypeListener ( Listener  listener)

Returns a key for a type listener after adding it to this text field.

This listener is invoked when the text changes.

C++ cannot hash functions types. Therefore, the listener will be identified by a unique key, returned by this function. You should remember this key to remove the listener if necessary.

Parameters
listenerThe listener to add
Returns
the key for the listener

◆ alloc() [1/3]

static std::shared_ptr<TextField> cugl::scene2::TextField::alloc ( const char *  text,
const std::shared_ptr< Font > &  font 
)
inlinestatic

Returns a newly allocated text field with the given text and font.

The label will be sized to fit the rendered text exactly. That is, the height will be the maximum height of the font, and the width will be the sum of the advance of the rendered characters. That means that there may be some natural spacing around the characters.

All unprintable characters will be removed from the string. This includes tabs and newlines. They will be replaced by spaces. If any glyphs are missing from the font atlas, they will not be rendered.

Parameters
textThe initial text of this text field, in C string format.
fontThe font of this text field.
Returns
a newly allocated text field with the given text and font.

◆ alloc() [2/3]

static std::shared_ptr<TextField> cugl::scene2::TextField::alloc ( const Size  size,
const std::shared_ptr< Font > &  font 
)
inlinestatic

Returns a newly allocated text field with the given size and font.

The text is empty and may be set later with setText() or by the user.

Parameters
sizeThe size of the text field.
fontThe font of this text field.
Returns
a newly allocated text field with the given size and font.

◆ alloc() [3/3]

static std::shared_ptr<TextField> cugl::scene2::TextField::alloc ( const std::string &  text,
const std::shared_ptr< Font > &  font 
)
inlinestatic

Returns a newly allocated text field with the given text and font.

The label will be sized to fit the rendered text exactly. That is, the height will be the maximum height of the font, and the width will be the sum of the advance of the rendered characters. That means that there may be some natural spacing around the characters.

All unprintable characters will be removed from the string. This includes tabs and newlines. They will be replaced by spaces. If any glyphs are missing from the font atlas, they will not be rendered.

Parameters
textThe initial text of this text field.
fontThe font of this text field.
Returns
a newly allocated text field with the given text and font.

◆ allocWithData()

static std::shared_ptr<SceneNode> cugl::scene2::TextField::allocWithData ( const Scene2Loader loader,
const std::shared_ptr< JsonValue data 
)
inlinestatic

Returns a newly allocated node with the given JSON specificaton.

This initializer is designed to receive the "data" object from the JSON passed to Scene2Loader. This JSON format supports all of the attribute values of its parent class. In addition, it supports the following additional attributes:

 "font":         The name of a previously loaded font asset
 "text":         The initial label text
 "foreground":   A four-element integer array. Values should be 0..255
 "background":   A four-element integer array. Values should be 0..255
 "padding":      A two-element float array.
 "halign":       One of 'left', 'center', 'right', 'hard left',
                 'true center' and 'hard right'.
 "valign":       One of 'top', 'middle', 'bottom', 'hard top',
                 'true middle' and 'hard bottom'.

The attribute 'font' is REQUIRED. All other attributes are optional.

Parameters
loaderThe scene loader passing this JSON file
dataThe JSON object specifying the node
Returns
a newly allocated node with the given JSON specificaton.

◆ clearExitListeners()

void cugl::scene2::TextField::clearExitListeners ( )

Clears all listeners for this text field.

These listener are invoked when the field loses focus. This method does not require you to remember the keys assigned to the individual listeners.

Returns
true if the listener was succesfully removed

◆ clearTypeListeners()

void cugl::scene2::TextField::clearTypeListeners ( )

Clears all type listeners for this text field.

These listeners are invoked when the text changes. This method does not require you to remember the keys assigned to the individual listeners.

Returns
true if the listener was succesfully removed

◆ deactivate()

bool cugl::scene2::TextField::deactivate ( bool  dispose = false)

Deactivates this text field, ignoring any future input.

This method removes its internal listener from either the Mouse or Touchscreen, and from Keyboard and TextInput.

When deactivated, the text field will no longer change its text on its own. However, the user can still change manually with the setText() method. Futhermore, the appropriate type listener will be called when the text changes. However, any attempts to manually acquire focus will fail.

Parameters
disposeWhether this request is the result of a dispose action
Returns
true if the text field was successfully deactivated

◆ deleteMany()

bool cugl::scene2::TextField::deleteMany ( Uint32  counter)
protected

Deletes one character if counter is the current back counter.

This method is used to implement continous deletion. When the backspace key is held for a long time, the field schedules this function as a callback with the application. When in invoked, it will delete a single letter each frame. It will execute until the back counter changes, which happens when the key is released.

Parameters
counterThe timer to validate deletion
Returns
true if deletion should continue

◆ deleteOne()

void cugl::scene2::TextField::deleteOne ( )
protected

Deletes one character before the current cursor.

If alt is pressed, it will delete an entire word. If meta is pressed, it will delete the entire field.

◆ dispose()

virtual void cugl::scene2::TextField::dispose ( )
overridevirtual

Disposes all of the resources used by this text field.

A disposed text field can be safely reinitialized. Any child will be released. They will be deleted if no other object owns them.

It is unsafe to call this on a text field that is still currently inside of a scene graph.

Reimplemented from cugl::scene2::Label.

◆ draw()

virtual void cugl::scene2::TextField::draw ( const std::shared_ptr< SpriteBatch > &  batch,
const Mat4 transform,
Color4  tint 
)
overrideprotectedvirtual

Draws this text field via the given SpriteBatch.

This method only worries about drawing the current text field. It does not attempt to render the children.

This method provides the correct transformation matrix and tint color. You do not need to worry about whether the node uses relative color. This method is called by render() and these values are guaranteed to be correct. In addition, this method does not need to check for visibility, as it is guaranteed to only be called when the node is visible.

This method overrides the one from Label. It adds the drawing of a blinking cursor that indicates the edit position.

Parameters
batchThe SpriteBatch to draw with.
transformThe global transformation matrix.
tintThe tint to blend with the Node color.

Reimplemented from cugl::scene2::Label.

◆ getExitListener()

const Listener cugl::scene2::TextField::getExitListener ( Uint32  key) const

Returns the exit listener for the given key

This listener is invoked when the field loses focus.

If there is no listener for the given key, it returns nullptr.

Parameters
keyThe identifier for the listener
Returns
the listener for the given key

◆ getExitListeners()

const std::vector<Listener> cugl::scene2::TextField::getExitListeners ( ) const

Returns all exit listeners for this text field

These listeners are invoked when the field loses focus.

Returns
the exits listeners for this text field

◆ getTypeListener()

const Listener cugl::scene2::TextField::getTypeListener ( Uint32  key) const

Returns the type listener for the given key

This listener is invoked when the text changes.

If there is no listener for the given key, it returns nullptr.

Parameters
keyThe identifier for the listener
Returns
the type listener for the given key

◆ getTypeListeners()

const std::vector<Listener> cugl::scene2::TextField::getTypeListeners ( ) const

Returns all type listeners for this text field

This listener is invoked when the text changes.

Returns
the type listeners for this text field

◆ hasExitListener()

bool cugl::scene2::TextField::hasExitListener ( ) const
inline

Returns true if this text field has an exit listener

This listener is invoked when the field loses focus.

Returns
true if this text field has an exit listener

◆ hasFocus()

bool cugl::scene2::TextField::hasFocus ( ) const
inline

Returns true if this text field has focus.

Returns
true if this text field has focus.

◆ hasTypeListener()

bool cugl::scene2::TextField::hasTypeListener ( ) const
inline

Returns true if this text field has a type listener

This listener is invoked when the text changes.

Returns
true if this text field has a type listener

◆ isActive()

bool cugl::scene2::TextField::isActive ( ) const
inline

Returns true if this text field has been activated.

Returns
true if this text field has been activated.

◆ releaseFocus()

bool cugl::scene2::TextField::releaseFocus ( )

Releases text input focus for this text field.

When the focus is released, the label can no longer be editting. Typically this means that the user has input the final value, which is why the exit listener (if any exists) is called.

In addition to calling this method manually, a user can release focus either by pressing RETURN or clicking somewhere outside of the field.

Returns
true if successfully released focus.

◆ removeExitListener()

bool cugl::scene2::TextField::removeExitListener ( Uint32  key)

Removes a listener from this text fieldexi.

This listener is invoked when the field loses focus.

Listeners must be identified by the key returned by the addExitListener method. If this text field does not have a listener for the given key, this method will fail.

Parameters
keyThe key of the listener to remove
Returns
true if the listener was succesfully removed

◆ removeTypeListener()

bool cugl::scene2::TextField::removeTypeListener ( Uint32  key)

Removes a type listener from this text field.

This listener is invoked when the text changes.

Listeners must be identified by the key returned by the addTypeListener method. If this text field does not have a listener for the given key, this method will fail.

Parameters
keyThe key of the listener to remove
Returns
true if the listener was succesfully removed

◆ requestFocus()

bool cugl::scene2::TextField::requestFocus ( )

Requests text input focus for this text field.

When a text field is activated, it does not immediately have focus. A text field without focus cannot be editted. By either clicking on the field or calling thus function, you can acquire focus and edit the field.

This method will fail if the text field is not active.

Returns
true if successfully requested focus.

◆ setText()

virtual void cugl::scene2::TextField::setText ( const std::string &  text,
bool  resize = false 
)
overridevirtual

Sets the text for this label.

All unprintable characters will be removed from the string. This includes tabs and newlines. They will be replaced by spaces.

The string must be in either ASCII or UTF8 format. No other string encodings are supported. As all ASCII strings are also UTF8, you can this effectively means that the text must be UTF8.

If the font is missing glyphs in this string, the characters in the text may be different than those displayed.

If this text field has a type listener, it will be invoked when this method is called.

Parameters
textThe text for this label.
resizeWhether to resize the label to fit the new text.

Reimplemented from cugl::scene2::Label.

◆ skipWord()

int cugl::scene2::TextField::skipWord ( bool  forward)
protected

Moves the cursor one word forward or backward.

If there is any space between the cursor and the word in the correct direction, it will move adjacent to the word. Otherwise, it will skip over the word.

Parameters
forwardWhether to move the cursor forward.
Returns
the index of the new cursor position

◆ updateCursor()

void cugl::scene2::TextField::updateCursor ( )
protected

Updates the cursor position.

This method is called whenever either the text changes or the cursor moves.

◆ updateInput()

void cugl::scene2::TextField::updateInput ( const TextInputEvent event,
bool  focus 
)
protected

Updates the text with the given input data.

This method is a callback for the TextInput device.

Parameters
eventThe text input event to be handled.
focusWhether the text field currently has text input focus.

◆ updateKey()

void cugl::scene2::TextField::updateKey ( const KeyEvent event,
bool  focus,
bool  down 
)
protected

Updates the state of any special keys.

This method is a callback for the Keyboard device.

Parameters
eventThe key event to be handled.
focusWhether the text field currently has keyboard focus
downWhether the key is pressed down (as opposed to released)

◆ updatePress()

void cugl::scene2::TextField::updatePress ( Vec2  pos,
bool  focus 
)
protected

Responds to a touch or press, changing the field focus.

If the press is outside text field, focus will be released. If the press is within the bounds, it will request focus (if not currently in focus) and move the cursor to the position pressed.

Parameters
posThe screen coordinate where the event happened.
focusWhether the text field currently has keyboard focus

◆ validate()

bool cugl::scene2::TextField::validate ( const std::string &  value)
protected

Returns true if value is a valid input character.

This method is used by TextInput to validate input. If a character is not valid, it is dropped.

Parameters
valueThe (UTF8) character to validate
Returns
true if value is a valid input character.

Member Data Documentation

◆ _active

bool cugl::scene2::TextField::_active
protected

Whether the field is actively checking for state changes

◆ _altDown

bool cugl::scene2::TextField::_altDown
protected

Whether the Alt key is down (used for word level editing)

◆ _backCount

Uint32 cugl::scene2::TextField::_backCount
protected

A timer to safely implement continuous deletion

◆ _backDown

bool cugl::scene2::TextField::_backDown
protected

Whether the backspace key is held down (used for continuous deleting)

◆ _cursor

Rect cugl::scene2::TextField::_cursor
protected

The current cursor rectangle,

◆ _cursorBlink

int cugl::scene2::TextField::_cursorBlink
protected

Timer for blinking the cursor.

◆ _cursorIndex

int cugl::scene2::TextField::_cursorIndex
protected

Cursor position indexed from the end of text. 0 means the end.

◆ _exitListeners

std::unordered_map<Uint32,Listener> cugl::scene2::TextField::_exitListeners
protected

The listener callbacks for loss of focus

◆ _fkey

Uint32 cugl::scene2::TextField::_fkey
protected

The focus key when the text field is checking for events

◆ _focused

bool cugl::scene2::TextField::_focused
protected

Whether the field is actively receiving keyboad events

◆ _kkey

Uint32 cugl::scene2::TextField::_kkey
protected

The keyboard key when the text field is checking for events

◆ _metaDown

bool cugl::scene2::TextField::_metaDown
protected

Whether the Meta key is down (used for line level editing)

◆ _mouse

bool cugl::scene2::TextField::_mouse
protected

Whether we are using the mouse (as opposed to the touch screen)

◆ _nextKey

Uint32 cugl::scene2::TextField::_nextKey
protected

The next available key for a listener

◆ _textLength

float cugl::scene2::TextField::_textLength
protected

Actual length of text, used to accelerate cursor placement.

◆ _tkey

Uint32 cugl::scene2::TextField::_tkey
protected

The text input key when the text field is checking for events

◆ _typeListeners

std::unordered_map<Uint32,Listener> cugl::scene2::TextField::_typeListeners
protected

The listener callbacks for text changes


The documentation for this class was generated from the following file: