|
|
@@ -16,6 +16,18 @@ TextBox::~TextBox()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+TextBox* TextBox::create(const char* id, Theme::Style* style)
|
|
|
+{
|
|
|
+ GP_ASSERT(style);
|
|
|
+
|
|
|
+ TextBox* textBox = new TextBox();
|
|
|
+ if (id)
|
|
|
+ textBox->_id = id;
|
|
|
+ textBox->setStyle(style);
|
|
|
+
|
|
|
+ return textBox;
|
|
|
+}
|
|
|
+
|
|
|
TextBox* TextBox::create(Theme::Style* style, Properties* properties)
|
|
|
{
|
|
|
TextBox* textBox = new TextBox();
|
|
|
@@ -49,36 +61,35 @@ bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
|
|
|
switch (evt)
|
|
|
{
|
|
|
case Touch::TOUCH_PRESS:
|
|
|
- if (_state == NORMAL)
|
|
|
- {
|
|
|
- _state = ACTIVE;
|
|
|
- Game::getInstance()->displayKeyboard(true);
|
|
|
- _dirty = true;
|
|
|
- return _consumeInputEvents;
|
|
|
- }
|
|
|
- else if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
|
|
|
+ if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
|
|
|
y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
|
|
|
{
|
|
|
- setCaretLocation(x, y);
|
|
|
+ _contactIndex = (int) contactIndex;
|
|
|
+
|
|
|
+ if (_state == NORMAL)
|
|
|
+ Game::getInstance()->displayKeyboard(true);
|
|
|
+ else
|
|
|
+ setCaretLocation(x, y);
|
|
|
+
|
|
|
+ _state = ACTIVE;
|
|
|
_dirty = true;
|
|
|
- return _consumeInputEvents;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ _contactIndex = INVALID_CONTACT_INDEX;
|
|
|
_state = NORMAL;
|
|
|
Game::getInstance()->displayKeyboard(false);
|
|
|
_dirty = true;
|
|
|
- return _consumeInputEvents;
|
|
|
+ return false;
|
|
|
}
|
|
|
break;
|
|
|
case Touch::TOUCH_MOVE:
|
|
|
- if (_state == FOCUS &&
|
|
|
+ if (_state == ACTIVE &&
|
|
|
x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
|
|
|
y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
|
|
|
{
|
|
|
setCaretLocation(x, y);
|
|
|
_dirty = true;
|
|
|
- return _consumeInputEvents;
|
|
|
}
|
|
|
break;
|
|
|
case Touch::TOUCH_RELEASE:
|
|
|
@@ -87,16 +98,14 @@ bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int conta
|
|
|
{
|
|
|
setCaretLocation(x, y);
|
|
|
_state = FOCUS;
|
|
|
- _dirty = true;
|
|
|
- return _consumeInputEvents;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_state = NORMAL;
|
|
|
Game::getInstance()->displayKeyboard(false);
|
|
|
- _dirty = true;
|
|
|
- return _consumeInputEvents;
|
|
|
}
|
|
|
+ _contactIndex = INVALID_CONTACT_INDEX;
|
|
|
+ _dirty = true;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -156,7 +165,7 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
|
|
|
Font::Justify textAlignment = getTextAlignment(_state);
|
|
|
bool rightToLeft = getTextRightToLeft(_state);
|
|
|
|
|
|
- unsigned int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
|
|
|
+ int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
|
|
|
textAlignment, true, rightToLeft);
|
|
|
font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex - 1,
|
|
|
textAlignment, true, rightToLeft);
|
|
|
@@ -187,10 +196,15 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
|
|
|
unsigned int fontSize = getFontSize(_state);
|
|
|
Font::Justify textAlignment = getTextAlignment(_state);
|
|
|
bool rightToLeft = getTextRightToLeft(_state);
|
|
|
-
|
|
|
+ _prevCaretLocation.set(_caretLocation);
|
|
|
_caretLocation.y -= fontSize;
|
|
|
- font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
|
|
|
+ int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
|
|
|
textAlignment, true, rightToLeft);
|
|
|
+ if (textIndex == -1)
|
|
|
+ {
|
|
|
+ _caretLocation.set(_prevCaretLocation);
|
|
|
+ }
|
|
|
+
|
|
|
_dirty = true;
|
|
|
consume = true;
|
|
|
break;
|
|
|
@@ -202,10 +216,15 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
|
|
|
unsigned int fontSize = getFontSize(_state);
|
|
|
Font::Justify textAlignment = getTextAlignment(_state);
|
|
|
bool rightToLeft = getTextRightToLeft(_state);
|
|
|
-
|
|
|
+ _prevCaretLocation.set(_caretLocation);
|
|
|
_caretLocation.y += fontSize;
|
|
|
- font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
|
|
|
+ int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
|
|
|
textAlignment, true, rightToLeft);
|
|
|
+ if (textIndex == -1)
|
|
|
+ {
|
|
|
+ _caretLocation.set(_prevCaretLocation);
|
|
|
+ }
|
|
|
+
|
|
|
_dirty = true;
|
|
|
consume = true;
|
|
|
break;
|
|
|
@@ -224,6 +243,12 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
|
|
|
|
|
|
int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
|
|
|
textAlignment, true, rightToLeft);
|
|
|
+ if (textIndex == -1)
|
|
|
+ {
|
|
|
+ textIndex = 0;
|
|
|
+ font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, 0,
|
|
|
+ textAlignment, true, rightToLeft);
|
|
|
+ }
|
|
|
|
|
|
switch (key)
|
|
|
{
|
|
|
@@ -237,13 +262,17 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
|
|
|
textAlignment, true, rightToLeft);
|
|
|
|
|
|
_dirty = true;
|
|
|
- consume = true;
|
|
|
}
|
|
|
+ consume = true;
|
|
|
break;
|
|
|
}
|
|
|
case Keyboard::KEY_RETURN:
|
|
|
// TODO: Handle line-break insertion correctly.
|
|
|
break;
|
|
|
+ case Keyboard::KEY_ESCAPE:
|
|
|
+ break;
|
|
|
+ case Keyboard::KEY_TAB:
|
|
|
+ break;
|
|
|
default:
|
|
|
{
|
|
|
// Insert character into string.
|
|
|
@@ -293,6 +322,7 @@ bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
|
|
|
}
|
|
|
|
|
|
notifyListeners(Listener::TEXT_CHANGED);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -312,7 +342,7 @@ void TextBox::update(const Control* container, const Vector2& offset)
|
|
|
|
|
|
void TextBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
|
|
|
{
|
|
|
- if (_state == FOCUS)
|
|
|
+ if (_state == ACTIVE || _state == FOCUS)
|
|
|
{
|
|
|
// Draw the cursor at its current location.
|
|
|
GP_ASSERT(_caretImage);
|
|
|
@@ -352,4 +382,9 @@ void TextBox::setCaretLocation(int x, int y)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-}
|
|
|
+const char* TextBox::getType() const
|
|
|
+{
|
|
|
+ return "textBox";
|
|
|
+}
|
|
|
+
|
|
|
+}
|