Skip to content

Commit

Permalink
gui: don't mix up display value with actual value
Browse files Browse the repository at this point in the history
In the case of password entry, the two differ. Password entry was broken
by the earlier commit entitled "Improve input box text handling" because
it started using the displayValue as the actual value, even in cases where
they differ because of a mask value.

Change-Id: Iaf5a67e1d928f34595962a1f1b80eebb64e8b493
  • Loading branch information
sultanqasim authored and mdmower committed Apr 4, 2016
1 parent 654617d commit 15bcf13
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
58 changes: 29 additions & 29 deletions gui/input.cpp
Expand Up @@ -57,6 +57,7 @@ GUIInput::GUIInput(xml_node<>* node)
CursorWidth = 3;
ConvertStrToColor("black", &mBackgroundColor);
ConvertStrToColor("white", &mCursorColor);
mValue = "";
displayValue = "";

if (!node)
Expand Down Expand Up @@ -110,10 +111,6 @@ GUIInput::GUIInput(xml_node<>* node)
mFontHeight = mFont->GetHeight();
}

child = FindNode(node, "text");
if (child) mText = child->value();
mLastValue = gui_parse_text(mText);

child = FindNode(node, "data");
if (child)
{
Expand Down Expand Up @@ -182,7 +179,7 @@ void GUIInput::HandleTextLocation(int x) {
lastX = x;
}

void GUIInput::UpdateTextWidth() {
void GUIInput::UpdateDisplayText() {
void* fontResource = NULL;

if (mFont) {
Expand All @@ -192,14 +189,17 @@ void GUIInput::UpdateTextWidth() {
return;
}

DataManager::GetValue(mVariable, displayValue);
DataManager::GetValue(mVariable, mValue);
if (HasMask) {
int index, string_size = displayValue.size();
int index, string_size = mValue.size();
string maskedValue;
for (index=0; index<string_size; index++)
maskedValue += mMask;
displayValue = maskedValue;
} else {
displayValue = mValue;
}

textWidth = gr_ttf_measureEx(displayValue.c_str(), fontResource);
}

Expand Down Expand Up @@ -260,19 +260,19 @@ void GUIInput::HandleCursorByText() {
return;
}

int cursorWidth = textWidth;
int cursorTextWidth = textWidth; // width of text to the left of the cursor

if (mCursorLocation != -1) {
string cursorDisplay = displayValue;
cursorDisplay.resize(mCursorLocation);
cursorWidth = gr_ttf_measureEx(cursorDisplay.c_str(), fontResource);
cursorTextWidth = gr_ttf_measureEx(cursorDisplay.c_str(), fontResource);
}
cursorX = mRenderX + cursorWidth + scrollingX;
cursorX = mRenderX + cursorTextWidth + scrollingX;
if (cursorX >= mRenderX + mRenderW) {
scrollingX = mRenderW - cursorWidth;
scrollingX = mRenderW - cursorTextWidth;
cursorX = mRenderX + mRenderW - CursorWidth;
} else if (cursorX < mRenderX) {
scrollingX = cursorWidth * -1;
scrollingX = cursorTextWidth * -1;
cursorX = mRenderX;
}
}
Expand Down Expand Up @@ -411,14 +411,14 @@ int GUIInput::NotifyVarChange(const std::string& varName, const std::string& val

if (varName == mVariable) {
if (!isLocalChange) {
UpdateTextWidth();
UpdateDisplayText();
HandleTextLocation(TW_INPUT_NO_UPDATE);
} else
isLocalChange = false;
return 0;
}
if (varName.empty()) {
UpdateTextWidth();
UpdateDisplayText();
HandleTextLocation(TW_INPUT_NO_UPDATE);
HandleCursorByText();
}
Expand Down Expand Up @@ -483,16 +483,16 @@ int GUIInput::NotifyCharInput(int key)
if (HasInputFocus) {
if (key == KEYBOARD_BACKSPACE) {
//Backspace
if (displayValue.size() > 0 && mCursorLocation != 0) {
if (mValue.size() > 0 && mCursorLocation != 0) {
if (mCursorLocation == -1) {
displayValue.resize(displayValue.size() - 1);
mValue.resize(mValue.size() - 1);
} else {
displayValue.erase(mCursorLocation - 1, 1);
mValue.erase(mCursorLocation - 1, 1);
mCursorLocation--;
}
isLocalChange = true;
DataManager::SetValue(mVariable, displayValue);
UpdateTextWidth();
DataManager::SetValue(mVariable, mValue);
UpdateDisplayText();
HandleTextLocation(TW_INPUT_NO_UPDATE);
HandleCursorByText();
}
Expand All @@ -501,15 +501,15 @@ int GUIInput::NotifyCharInput(int key)
isLocalChange = true;
if (mCursorLocation == -1) {
DataManager::SetValue (mVariable, "");
displayValue = "";
mValue = "";
textWidth = 0;
mCursorLocation = -1;
} else {
displayValue.erase(0, mCursorLocation);
DataManager::SetValue(mVariable, displayValue);
UpdateTextWidth();
mValue.erase(0, mCursorLocation);
DataManager::SetValue(mVariable, mValue);
mCursorLocation = 0;
}
UpdateDisplayText();
cursorX = mRenderX;
scrollingX = 0;
mRendered = false;
Expand All @@ -522,24 +522,24 @@ int GUIInput::NotifyCharInput(int key)
if (HasDisabled && DisabledList.find((char)key) != string::npos) {
return 0;
}
if (MaxLen != 0 && displayValue.size() >= MaxLen) {
if (MaxLen != 0 && mValue.size() >= MaxLen) {
return 0;
}
if (mCursorLocation == -1) {
displayValue += key;
mValue += key;
} else {
displayValue.insert(mCursorLocation, 1, key);
mValue.insert(mCursorLocation, 1, key);
mCursorLocation++;
}
isLocalChange = true;
DataManager::SetValue(mVariable, displayValue);
UpdateTextWidth();
DataManager::SetValue(mVariable, mValue);
UpdateDisplayText();
HandleTextLocation(TW_INPUT_NO_UPDATE);
HandleCursorByText();
} else if (key == KEYBOARD_ACTION) {
// Action
if (mAction) {
unsigned inputLen = displayValue.length();
unsigned inputLen = mValue.length();
if (inputLen < MinLen)
return 0;
else if (MaxLen != 0 && inputLen > MaxLen)
Expand Down
7 changes: 3 additions & 4 deletions gui/objects.hpp
Expand Up @@ -1002,7 +1002,7 @@ class GUIInput : public GUIObject, public RenderObject, public ActionObject, pub

// Handles displaying the text properly when chars are added, deleted, or for scrolling
void HandleTextLocation(int x);
void UpdateTextWidth();
void UpdateDisplayText();
void HandleCursorByTouch(int x);
void HandleCursorByText();

Expand All @@ -1012,15 +1012,14 @@ class GUIInput : public GUIObject, public RenderObject, public ActionObject, pub
ImageResource* mBackground;
ImageResource* mCursor;
FontResource* mFont;
std::string mText;
std::string mLastValue;
std::string mVariable;
std::string mMask;
std::string mValue;
std::string displayValue;
COLOR mBackgroundColor;
COLOR mCursorColor;
int scrollingX;
int cursorX;
int cursorX; // actual x axis location of the cursor
int lastX;
int mCursorLocation;
int mBackgroundX, mBackgroundY, mBackgroundW, mBackgroundH;
Expand Down

0 comments on commit 15bcf13

Please sign in to comment.