Number type for text fields, addresses some of issue #39
This commit is contained in:
parent
418373a3cd
commit
d138b2de54
@ -253,21 +253,29 @@ GameView::GameView():
|
|||||||
colourRSlider->SetActionCallback(new ColourChange(this));
|
colourRSlider->SetActionCallback(new ColourChange(this));
|
||||||
colourRValue = new ui::Textbox(ui::Point(60, Size.Y-41), ui::Point(25, 17), "255");
|
colourRValue = new ui::Textbox(ui::Point(60, Size.Y-41), ui::Point(25, 17), "255");
|
||||||
colourRValue->SetActionCallback(new ColourChange(this));
|
colourRValue->SetActionCallback(new ColourChange(this));
|
||||||
|
colourRValue->SetLimit(3);
|
||||||
|
colourRValue->SetInputType(ui::Textbox::Number);
|
||||||
|
|
||||||
colourGSlider = new ui::Slider(ui::Point(95, Size.Y-39), ui::Point(50, 14), 255);
|
colourGSlider = new ui::Slider(ui::Point(95, Size.Y-39), ui::Point(50, 14), 255);
|
||||||
colourGSlider->SetActionCallback(new ColourChange(this));
|
colourGSlider->SetActionCallback(new ColourChange(this));
|
||||||
colourGValue = new ui::Textbox(ui::Point(150, Size.Y-41), ui::Point(25, 17), "255");
|
colourGValue = new ui::Textbox(ui::Point(150, Size.Y-41), ui::Point(25, 17), "255");
|
||||||
colourGValue->SetActionCallback(new ColourChange(this));
|
colourGValue->SetActionCallback(new ColourChange(this));
|
||||||
|
colourGValue->SetLimit(3);
|
||||||
|
colourGValue->SetInputType(ui::Textbox::Number);
|
||||||
|
|
||||||
colourBSlider = new ui::Slider(ui::Point(185, Size.Y-39), ui::Point(50, 14), 255);
|
colourBSlider = new ui::Slider(ui::Point(185, Size.Y-39), ui::Point(50, 14), 255);
|
||||||
colourBSlider->SetActionCallback(new ColourChange(this));
|
colourBSlider->SetActionCallback(new ColourChange(this));
|
||||||
colourBValue = new ui::Textbox(ui::Point(240, Size.Y-41), ui::Point(25, 17), "255");
|
colourBValue = new ui::Textbox(ui::Point(240, Size.Y-41), ui::Point(25, 17), "255");
|
||||||
colourBValue->SetActionCallback(new ColourChange(this));
|
colourBValue->SetActionCallback(new ColourChange(this));
|
||||||
|
colourBValue->SetLimit(3);
|
||||||
|
colourBValue->SetInputType(ui::Textbox::Number);
|
||||||
|
|
||||||
colourASlider = new ui::Slider(ui::Point(275, Size.Y-39), ui::Point(50, 14), 255);
|
colourASlider = new ui::Slider(ui::Point(275, Size.Y-39), ui::Point(50, 14), 255);
|
||||||
colourASlider->SetActionCallback(new ColourChange(this));
|
colourASlider->SetActionCallback(new ColourChange(this));
|
||||||
colourAValue = new ui::Textbox(ui::Point(330, Size.Y-41), ui::Point(25, 17), "255");
|
colourAValue = new ui::Textbox(ui::Point(330, Size.Y-41), ui::Point(25, 17), "255");
|
||||||
colourAValue->SetActionCallback(new ColourChange(this));
|
colourAValue->SetActionCallback(new ColourChange(this));
|
||||||
|
colourAValue->SetLimit(3);
|
||||||
|
colourAValue->SetInputType(ui::Textbox::Number);
|
||||||
|
|
||||||
class ElementSearchAction : public ui::ButtonAction
|
class ElementSearchAction : public ui::ButtonAction
|
||||||
{
|
{
|
||||||
|
@ -15,7 +15,8 @@ Textbox::Textbox(Point position, Point size, std::string textboxText, std::strin
|
|||||||
masked(false),
|
masked(false),
|
||||||
border(true),
|
border(true),
|
||||||
mouseDown(false),
|
mouseDown(false),
|
||||||
limit(0)
|
limit(std::string::npos),
|
||||||
|
inputType(All)
|
||||||
{
|
{
|
||||||
placeHolder = textboxPlaceholder;
|
placeHolder = textboxPlaceholder;
|
||||||
|
|
||||||
@ -74,6 +75,26 @@ void Textbox::SetText(std::string newText)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Textbox::ValidInput Textbox::GetInputType()
|
||||||
|
{
|
||||||
|
return inputType;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Textbox::SetInputType(ValidInput input)
|
||||||
|
{
|
||||||
|
inputType = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Textbox::SetLimit(size_t limit)
|
||||||
|
{
|
||||||
|
this->limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Textbox::GetLimit()
|
||||||
|
{
|
||||||
|
return limit;
|
||||||
|
}
|
||||||
|
|
||||||
void Textbox::SetDisplayText(std::string newText)
|
void Textbox::SetDisplayText(std::string newText)
|
||||||
{
|
{
|
||||||
Label::SetText(text);
|
Label::SetText(text);
|
||||||
@ -194,6 +215,20 @@ void Textbox::pasteIntoSelection()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Textbox::CharacterValid(Uint16 character)
|
||||||
|
{
|
||||||
|
switch(inputType)
|
||||||
|
{
|
||||||
|
case Number:
|
||||||
|
case Numeric:
|
||||||
|
return (character >= '0' && character <= '9');
|
||||||
|
case All:
|
||||||
|
default:
|
||||||
|
return (character >= ' ' && character < 127);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
|
void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt)
|
||||||
{
|
{
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
@ -274,7 +309,7 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(character >= ' ' && character < 127)
|
if(CharacterValid(character))
|
||||||
{
|
{
|
||||||
if(HasSelection())
|
if(HasSelection())
|
||||||
{
|
{
|
||||||
@ -284,6 +319,8 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
|
|||||||
cursor = getLowerSelectionBound();
|
cursor = getLowerSelectionBound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(limit==std::string::npos || backingText.length() < limit)
|
||||||
|
{
|
||||||
if(cursor == backingText.length())
|
if(cursor == backingText.length())
|
||||||
{
|
{
|
||||||
backingText += character;
|
backingText += character;
|
||||||
@ -292,6 +329,7 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
|
|||||||
{
|
{
|
||||||
backingText.insert(cursor, 1, (char)character);
|
backingText.insert(cursor, 1, (char)character);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
cursor++;
|
cursor++;
|
||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
@ -302,6 +340,18 @@ void Textbox::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
|
|||||||
cursor = 0;
|
cursor = 0;
|
||||||
backingText = "";
|
backingText = "";
|
||||||
}
|
}
|
||||||
|
if(inputType == Number)
|
||||||
|
{
|
||||||
|
if(backingText.length()>1)
|
||||||
|
{
|
||||||
|
while(backingText[0] == '0')
|
||||||
|
backingText.erase(backingText.begin());
|
||||||
|
}
|
||||||
|
if(!backingText.length())
|
||||||
|
backingText = "0";
|
||||||
|
}
|
||||||
|
if(cursor >= backingText.length())
|
||||||
|
cursor = backingText.length()-1;
|
||||||
if(changed)
|
if(changed)
|
||||||
{
|
{
|
||||||
if(masked)
|
if(masked)
|
||||||
|
@ -19,18 +19,8 @@ public:
|
|||||||
class Textbox : public Label
|
class Textbox : public Label
|
||||||
{
|
{
|
||||||
friend class TextboxAction;
|
friend class TextboxAction;
|
||||||
protected:
|
|
||||||
size_t limit;
|
|
||||||
bool mouseDown;
|
|
||||||
bool masked, border;
|
|
||||||
int cursor, cursorPositionX, cursorPositionY;
|
|
||||||
TextboxAction *actionCallback;
|
|
||||||
std::string backingText;
|
|
||||||
std::string placeHolder;
|
|
||||||
|
|
||||||
virtual void cutSelection();
|
|
||||||
virtual void pasteIntoSelection();
|
|
||||||
public:
|
public:
|
||||||
|
enum ValidInput { All, Numeric, Number };
|
||||||
Textbox(Point position, Point size, std::string textboxText = "", std::string textboxPlaceholder = "");
|
Textbox(Point position, Point size, std::string textboxText = "", std::string textboxPlaceholder = "");
|
||||||
virtual ~Textbox();
|
virtual ~Textbox();
|
||||||
|
|
||||||
@ -45,12 +35,34 @@ public:
|
|||||||
bool GetHidden() { return masked; }
|
bool GetHidden() { return masked; }
|
||||||
void SetActionCallback(TextboxAction * action) { actionCallback = action; }
|
void SetActionCallback(TextboxAction * action) { actionCallback = action; }
|
||||||
|
|
||||||
|
void SetLimit(size_t limit);
|
||||||
|
size_t GetLimit();
|
||||||
|
|
||||||
|
ValidInput GetInputType();
|
||||||
|
void SetInputType(ValidInput input);
|
||||||
|
|
||||||
|
//Determines if the given character is valid given the input type
|
||||||
|
bool CharacterValid(Uint16 character);
|
||||||
|
|
||||||
virtual void OnContextMenuAction(int item);
|
virtual void OnContextMenuAction(int item);
|
||||||
virtual void OnMouseClick(int x, int y, unsigned button);
|
virtual void OnMouseClick(int x, int y, unsigned button);
|
||||||
virtual void OnMouseUp(int x, int y, unsigned button);
|
virtual void OnMouseUp(int x, int y, unsigned button);
|
||||||
virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
|
virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
|
||||||
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
|
virtual void OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool alt);
|
||||||
virtual void Draw(const Point& screenPos);
|
virtual void Draw(const Point& screenPos);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ValidInput inputType;
|
||||||
|
size_t limit;
|
||||||
|
bool mouseDown;
|
||||||
|
bool masked, border;
|
||||||
|
int cursor, cursorPositionX, cursorPositionY;
|
||||||
|
TextboxAction *actionCallback;
|
||||||
|
std::string backingText;
|
||||||
|
std::string placeHolder;
|
||||||
|
|
||||||
|
virtual void cutSelection();
|
||||||
|
virtual void pasteIntoSelection();
|
||||||
};
|
};
|
||||||
|
|
||||||
/*class Textbox : public Component
|
/*class Textbox : public Component
|
||||||
|
Reference in New Issue
Block a user