Permalink
Browse files

Fix Unicode text input

  • Loading branch information...
SupSuper committed Nov 5, 2018
1 parent 3045a6a commit f4ac110a3814c01a4166e5a409c45fc46116e396
Showing with 33 additions and 33 deletions.
  1. +28 −29 src/Interface/TextEdit.cpp
  2. +5 −4 src/Interface/TextEdit.h
@@ -34,7 +34,7 @@ namespace OpenXcom
* @param x X position in pixels.
* @param y Y position in pixels.
*/
TextEdit::TextEdit(State *state, int width, int height, int x, int y) : InteractiveSurface(width, height, x, y), _blink(true), _modal(true), _ascii('A'), _caretPos(0), _textEditConstraint(TEC_NONE), _change(0), _state(state)
TextEdit::TextEdit(State *state, int width, int height, int x, int y) : InteractiveSurface(width, height, x, y), _blink(true), _modal(true), _char('A'), _caretPos(0), _textEditConstraint(TEC_NONE), _change(0), _state(state)
{
_isFocused = false;
_text = new Text(width, height, 0, 0);
@@ -144,7 +144,7 @@ void TextEdit::initText(Font *big, Font *small, Language *lang)
*/
void TextEdit::setText(const std::string &text)
{
_value = text;
_value = Unicode::convUtf8ToUtf32(text);
_caretPos = _value.length();
_redraw = true;
}
@@ -155,7 +155,7 @@ void TextEdit::setText(const std::string &text)
*/
std::string TextEdit::getText() const
{
return _value;
return Unicode::convUtf32ToUtf8(_value);
}
/**
@@ -299,16 +299,15 @@ void TextEdit::blink()
void TextEdit::draw()
{
Surface::draw();
_text->setText(_value);
UString newValue = _value;
if (Options::keyboardMode == KEYBOARD_OFF)
{
std::string newValue = _value;
if (_isFocused && _blink)
{
newValue += _ascii;
_text->setText(newValue);
newValue += _char;
}
}
_text->setText(Unicode::convUtf32ToUtf8(_value));
clear();
_text->blit(this);
if (Options::keyboardMode == KEYBOARD_ON)
@@ -359,13 +358,13 @@ void TextEdit::draw()
* @param c Character to add.
* @return True if it exceeds, False if it doesn't.
*/
bool TextEdit::exceedsMaxWidth(char c)
bool TextEdit::exceedsMaxWidth(UCode c) const
{
int w = 0;
std::string s = _value;
UString s = _value;
s += c;
for (std::string::iterator i = s.begin(); i < s.end(); ++i)
for (UString::const_iterator i = s.begin(); i < s.end(); ++i)
{
w += _text->getFont()->getCharSize(*i).w;
}
@@ -377,32 +376,32 @@ bool TextEdit::exceedsMaxWidth(char c)
* Checks if input key character is valid to
* be inserted at caret position in the text edit
* without breaking the text edit constraint.
* @param key Key code.
* @param c Character to validate.
* @return True if character can be inserted, False if it cannot.
*/
bool TextEdit::isValidChar(Uint16 key)
bool TextEdit::isValidChar(UCode c) const
{
switch (_textEditConstraint)
{
case TEC_NUMERIC_POSITIVE:
return key >= '0' && key <= '9';
return c >= '0' && c <= '9';
// If constraint is "(signed) numeric", need to check:
// - user does not input a character before '-' or '+'
// - user enter either figure anywhere, or a sign at first position
case TEC_NUMERIC:
if (_caretPos > 0)
{
return key >= '0' && key <= '9';
return c >= '0' && c <= '9';
}
else
{
return ((key >= '0' && key <= '9') || key == '+' || key == '-') &&
(_value.size() == 0 || (_value[0] != '+' && _value[0] != '-'));
return ((c >= '0' && c <= '9') || c == '+' || c == '-') &&
(_value.size() == 0 || (_value[0] != '+' && _value[0] != '-'));
}
case TEC_NONE:
return (key >= ' ' && key <= '~') || key >= 160;
return (c >= ' ' && c <= '~') || c >= 160;
default:
return false;
@@ -428,7 +427,7 @@ void TextEdit::mousePress(Action *action, State *state)
double scaleX = action->getXScale();
double w = 0;
int c = 0;
for (std::string::iterator i = _value.begin(); i < _value.end(); ++i)
for (UString::iterator i = _value.begin(); i < _value.end(); ++i)
{
if (mouseX <= w)
{
@@ -461,17 +460,17 @@ void TextEdit::keyboardPress(Action *action, State *state)
switch (action->getDetails()->key.keysym.sym)
{
case SDLK_UP:
_ascii++;
if (_ascii > '~')
_char++;
if (_char > '~')
{
_ascii = ' ';
_char = ' ';
}
break;
case SDLK_DOWN:
_ascii--;
if (_ascii < ' ')
_char--;
if (_char < ' ')
{
_ascii = '~';
_char = '~';
}
break;
case SDLK_LEFT:
@@ -481,9 +480,9 @@ void TextEdit::keyboardPress(Action *action, State *state)
}
break;
case SDLK_RIGHT:
if (!exceedsMaxWidth(_ascii))
if (!exceedsMaxWidth(_char))
{
_value += _ascii;
_value += _char;
}
break;
default: break;
@@ -532,10 +531,10 @@ void TextEdit::keyboardPress(Action *action, State *state)
}
break;
default:
Uint16 key = action->getDetails()->key.keysym.unicode;
if (isValidChar(key) && !exceedsMaxWidth((char)key))
UCode c = action->getDetails()->key.keysym.unicode;
if (isValidChar(c) && !exceedsMaxWidth(c))
{
_value.insert(_caretPos, 1, (char)action->getDetails()->key.keysym.unicode);
_value.insert(_caretPos, 1, c);
_caretPos++;
}
}
@@ -19,6 +19,7 @@
*/
#include "../Engine/InteractiveSurface.h"
#include "Text.h"
#include "../Engine/Unicode.h"
namespace OpenXcom
{
@@ -35,18 +36,18 @@ class TextEdit : public InteractiveSurface
{
private:
Text *_text, *_caret;
std::string _value;
UString _value;
bool _blink, _modal;
Timer *_timer;
char _ascii;
UCode _char;
size_t _caretPos;
TextEditConstraint _textEditConstraint;
ActionHandler _change;
State *_state;
/// Checks if a character will exceed the maximum width.
bool exceedsMaxWidth(char c);
bool exceedsMaxWidth(UCode c) const;
/// Checks if character is valid to be inserted at caret position.
bool isValidChar(Uint16 key);
bool isValidChar(UCode c) const;
public:
/// Creates a new text edit with the specified size and position.
TextEdit(State *state, int width, int height, int x = 0, int y = 0);

0 comments on commit f4ac110

Please sign in to comment.