CUGL 2.0
Cornell University Game Library
|
#include <CUTextField.h>
Public Types | |
typedef std::function< void(const std::string &name, const std::string ¤t)> | 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< Listener > | getTypeListeners () const |
Uint32 | addTypeListener (Listener listener) |
bool | removeTypeListener (Uint32 key) |
void | clearTypeListeners () |
bool | hasExitListener () const |
const Listener | getExitListener (Uint32 key) const |
const std::vector< Listener > | getExitListeners () 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< Font > | getFont () 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 SceneNode * | copy (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< Scissor > | getScissor () 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 Mat4 & | getAlternateTransform () |
void | setAlternateTransform (const Mat4 &transform) |
bool | withAlternateTransform () |
void | chooseAlternateTransform (bool active) |
const Mat4 & | getNodeToParentTransform () 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< SceneNode > | getChild (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< SceneNode > | getChildByTag (unsigned int tag) const |
template<typename T > | |
std::shared_ptr< T > | getChildByTag (unsigned int tag) const |
std::shared_ptr< SceneNode > | getChildByName (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) |
SceneNode * | getParent () |
const SceneNode * | getParent () const |
Scene2 * | getScene () |
const Scene2 * | getScene () 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< TextField > | alloc (const Size size, const std::shared_ptr< Font > &font) |
static std::shared_ptr< TextField > | alloc (const std::string &text, const std::shared_ptr< Font > &font) |
static std::shared_ptr< TextField > | alloc (const char *text, const std::shared_ptr< Font > &font) |
static std::shared_ptr< SceneNode > | allocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > data) |
Static Public Member Functions inherited from cugl::scene2::Label | |
static std::shared_ptr< Label > | alloc (const Size size, const std::shared_ptr< Font > &font) |
static std::shared_ptr< Label > | alloc (const std::string &text, const std::shared_ptr< Font > &font) |
static std::shared_ptr< Label > | alloc (const char *text, const std::shared_ptr< Font > &font) |
static std::shared_ptr< SceneNode > | allocWithData (const Scene2Loader *loader, const std::shared_ptr< JsonValue > &data) |
Static Public Member Functions inherited from cugl::scene2::SceneNode | |
static std::shared_ptr< SceneNode > | alloc () |
static std::shared_ptr< SceneNode > | allocWithPosition (const Vec2 pos) |
static std::shared_ptr< SceneNode > | allocWithPosition (float x, float y) |
static std::shared_ptr< SceneNode > | allocWithBounds (const Size size) |
static std::shared_ptr< SceneNode > | allocWithBounds (float width, float height) |
static std::shared_ptr< SceneNode > | allocWithBounds (const Rect rect) |
static std::shared_ptr< SceneNode > | allocWithBounds (float x, float y, float width, float height) |
static std::shared_ptr< SceneNode > | allocWithData (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) |
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.
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)>
name | The text field name |
current | Text after editing |
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.
|
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.
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.
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.
listener | The listener to add |
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.
listener | The listener to add |
|
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.
text | The initial text of this text field, in C string format. |
font | The font of this text field. |
|
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.
size | The size of the text field. |
font | The font of this text field. |
|
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.
text | The initial text of this text field. |
font | The font of this text field. |
|
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.
loader | The scene loader passing this JSON file |
data | The JSON object specifying the node |
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.
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.
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.
dispose | Whether this request is the result of a dispose action |
|
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.
counter | The timer to validate deletion |
|
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.
|
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.
|
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.
batch | The SpriteBatch to draw with. |
transform | The global transformation matrix. |
tint | The tint to blend with the Node color. |
Reimplemented from cugl::scene2::Label.
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.
key | The identifier for the listener |
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.
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.
key | The identifier for the listener |
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.
|
inline |
Returns true if this text field has an exit listener
This listener is invoked when the field loses focus.
|
inline |
Returns true if this text field has focus.
|
inline |
Returns true if this text field has a type listener
This listener is invoked when the text changes.
|
inline |
Returns true if this text field has been activated.
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.
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.
key | The key of the listener to remove |
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.
key | The key of the listener to remove |
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.
|
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.
text | The text for this label. |
resize | Whether to resize the label to fit the new text. |
Reimplemented from cugl::scene2::Label.
|
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.
forward | Whether to move the cursor forward. |
|
protected |
Updates the cursor position.
This method is called whenever either the text changes or the cursor moves.
|
protected |
Updates the text with the given input data.
This method is a callback for the TextInput device.
event | The text input event to be handled. |
focus | Whether the text field currently has text input focus. |
|
protected |
Updates the state of any special keys.
This method is a callback for the Keyboard device.
event | The key event to be handled. |
focus | Whether the text field currently has keyboard focus |
down | Whether the key is pressed down (as opposed to released) |
|
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.
pos | The screen coordinate where the event happened. |
focus | Whether the text field currently has keyboard focus |
|
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.
value | The (UTF8) character to validate |
|
protected |
Whether the field is actively checking for state changes
|
protected |
Whether the Alt key is down (used for word level editing)
|
protected |
A timer to safely implement continuous deletion
|
protected |
Whether the backspace key is held down (used for continuous deleting)
|
protected |
The current cursor rectangle,
|
protected |
Timer for blinking the cursor.
|
protected |
Cursor position indexed from the end of text. 0 means the end.
|
protected |
The listener callbacks for loss of focus
|
protected |
The focus key when the text field is checking for events
|
protected |
Whether the field is actively receiving keyboad events
|
protected |
The keyboard key when the text field is checking for events
|
protected |
Whether the Meta key is down (used for line level editing)
|
protected |
Whether we are using the mouse (as opposed to the touch screen)
|
protected |
The next available key for a listener
|
protected |
Actual length of text, used to accelerate cursor placement.
|
protected |
The text input key when the text field is checking for events
|
protected |
The listener callbacks for text changes