summaryrefslogtreecommitdiff
path: root/src/gui/textfield.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/textfield.cpp')
-rw-r--r--src/gui/textfield.cpp70
1 files changed, 69 insertions, 1 deletions
diff --git a/src/gui/textfield.cpp b/src/gui/textfield.cpp
index ea82ba77..bd016a8d 100644
--- a/src/gui/textfield.cpp
+++ b/src/gui/textfield.cpp
@@ -23,6 +23,8 @@
#include <guichan/font.hpp>
+#include <guichan/sdl/sdlinput.hpp>
+
#include "textfield.h"
#include "../graphics.h"
@@ -36,7 +38,9 @@ int TextField::instances = 0;
ImageRect TextField::skin;
TextField::TextField(const std::string& text):
- gcn::TextField(text)
+ gcn::TextField(text),
+ mNumeric(false),
+ mListener(0)
{
setFrameSize(2);
@@ -100,3 +104,67 @@ void TextField::drawFrame(gcn::Graphics *graphics)
static_cast<Graphics*>(graphics)->drawImageRect(0, 0, w, h, skin);
}
+
+void TextField::setNumeric(bool numeric)
+{
+ mNumeric = numeric;
+ if (!numeric)
+ {
+ return;
+ }
+ const char *text = mText.c_str();
+ for (const char *textPtr = text; *textPtr; ++textPtr)
+ {
+ if (*textPtr < '0' || *textPtr > '9')
+ {
+ setText(mText.substr(0, textPtr - text));
+ return;
+ }
+ }
+}
+
+void TextField::keyPressed(gcn::KeyEvent &keyEvent)
+{
+ if (mNumeric)
+ {
+ while (true)
+ {
+ const gcn::Key &key = keyEvent.getKey();
+ if (key.isNumber())
+ {
+ break;
+ }
+ int value = key.getValue();
+ if (value == SDLK_LEFT || value == SDLK_RIGHT ||
+ value == SDLK_HOME || value == SDLK_END ||
+ value == SDLK_BACKSPACE || value == SDLK_DELETE)
+ {
+ break;
+ }
+ return;
+ }
+ }
+ gcn::TextField::keyPressed(keyEvent);
+ if (mListener)
+ {
+ mListener->listen(this);
+ }
+}
+
+int TextField::getValue() const
+{
+ if (!mNumeric)
+ {
+ return 0;
+ }
+ int value = atoi(mText.c_str());
+ if (value < mMinimum)
+ {
+ return mMinimum;
+ }
+ if (value > mMaximum)
+ {
+ return mMaximum;
+ }
+ return value;
+}