Limiting numbers in editbox window

Forum for general chit-chat or off-topic discussion.

Moderators: CEGUI MVP, CEGUI Team

Jmiller
Just popping in
Just popping in
Posts: 3
Joined: Wed Apr 16, 2008 23:11

Limiting numbers in editbox window

Postby Jmiller » Wed Jun 04, 2008 18:29

I had been searching the forums for this topic and saw that some people had similar questions, but I didn't find a whole lot about how to implement it.

My problem was when creating an editbox window, I wanted to limit the characters being entered and specifically I wanted only numeric values in the range of 0-359. Here's how I was able to do it after reading up on regular expression syntax.

First it has been explained that you can limit what kind of input is acceptable in the editbox via the setValidationString function. The string this takes in is based on regular expression or regex. You can do a google search on regex and see all the nifty things that can be done with it.

Now here is how I solved my specific problem, only numbers 0 - 359.

"(3[0-5][0-9]|[0-2]?[0-9]?[0-9]?)"

Pass that into setValidationString() and you'll get the proper results.

I hope this helps someone save some time. I'm sure that you can do the research on your own and figure it out, but that's what I've come up with. Now all I need to know is why the number pad doesn't seem to inject input. Anyone know a solution for that?

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Postby CrazyEddie » Thu Jun 05, 2008 08:53

Cool. Thanks for sharing your solution :)

CE.

jaguillard
Just popping in
Just popping in
Posts: 12
Joined: Wed Jun 04, 2008 21:24

Postby jaguillard » Thu Jun 05, 2008 14:16

For negative numbers, here's what I pieced together. If the range is the same on both sides, eg -128 to 128, then you just need an optional - sign (marked by the ? postfix). In this situation, you are telling Regex that the "-" is allowed, but not required.

Code: Select all

-?1[0-2][0-8]|-?[0-9]?[0-9]?


If the range is not even, like -30 to 40, you'll have to make conditional statements for both sides of zero. The negative range needs to come first in the statement, and the negative sign non-optional. In this situation, you are telling Regex that for statements starting with "-", this is the ranges allowed.

Code: Select all

-[3][0]|-[0-2]?[0-9]?|[4][0]|[0-3]?[0-9]?


If you don't put the negative statements first, you won't be able to add a negative sign into the editbox. The Regex checks from left to right, and it would fail you on the [0-2]?[0-9]?, since the two ? marks both character slots as optional, but neither accepts a "-". It won't check any further.

To clarify what JMiller wrote (we worked together on figuring this out), there are some necessary parts you need to add to your statements. He described a range of 0-359. Note that the " | " is an OR operator, like in C++.

Code: Select all

3[0-5][0-9]|[0-2]?[0-9]?[0-9]?

The trick to this is creating the multiple ranges that will make up the whole range. The first statement allows the range between 300-359. This needs to come first in the case "3" is the first character, the Regex will match the following characters with it first. It will block any input that would create a value over 359, eg 371, the "1" won't get inputted.

The next statements allows for 0-299, filling out the range. However, each character is postfix with the ?. This makes both character slot optional. This is necessary because now it allows for a blank editbox, too. Otherwise, you'd have to highlight your text to change it.

Hope this helps people out with making some number-only editboxes.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Limiting numbers in editbox window

Postby CrazyEddie » Thu Jun 05, 2008 18:14

Jmiller wrote:Now all I need to know is why the number pad doesn't seem to inject input. Anyone know a solution for that?

Not sure about this, are you sure your input system is generating character inputs in response to those key presses?

jaguillard
Just popping in
Just popping in
Posts: 12
Joined: Wed Jun 04, 2008 21:24

Re: Limiting numbers in editbox window

Postby jaguillard » Thu Jun 05, 2008 18:29

CrazyEddie wrote:
Jmiller wrote:Now all I need to know is why the number pad doesn't seem to inject input. Anyone know a solution for that?

Not sure about this, are you sure your input system is generating character inputs in response to those key presses?


We looked into this and it's just a matter of how OIS deals with the keypad numbers it seems. None of the numbers were mapped to the font map (value of 0, so undefined basically). He got around it by manually mapping them in code. It had nothing to do with CEGUI.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Postby CrazyEddie » Thu Jun 05, 2008 18:43

Thanks for the update :) It's useful in case someone else has the issue.

CE.

westpointer
Just popping in
Just popping in
Posts: 16
Joined: Sat Nov 15, 2008 13:12

Re: Limiting numbers in editbox window

Postby westpointer » Sun Mar 29, 2009 13:50

jaguillard wrote:
CrazyEddie wrote:
Jmiller wrote:Now all I need to know is why the number pad doesn't seem to inject input. Anyone know a solution for that?

Not sure about this, are you sure your input system is generating character inputs in response to those key presses?


We looked into this and it's just a matter of how OIS deals with the keypad numbers it seems. None of the numbers were mapped to the font map (value of 0, so undefined basically). He got around it by manually mapping them in code. It had nothing to do with CEGUI.


I also want OIS keypad work for CEGUI input, but I still don't know how.
Anyone shed light on how to do this?

lonestarr
Just popping in
Just popping in
Posts: 1
Joined: Tue Oct 19, 2010 17:04

Re: Limiting numbers in editbox window

Postby lonestarr » Tue Oct 19, 2010 17:06

in case someone still cares about that, here is the code:

Code: Select all

static CEGUI::uint _considerNumpadKey(OIS::KeyCode argkey,unsigned int* text=NULL)
{
    switch (argkey)
    {
    case OIS::KC_NUMPAD0: if (text) *text=48; return 1;
    case OIS::KC_NUMPAD1: if (text) *text=49; return 2;
    case OIS::KC_NUMPAD2: if (text) *text=50; return 3;
    case OIS::KC_NUMPAD3: if (text) *text=51; return 4;
    case OIS::KC_NUMPAD4: if (text) *text=52; return 5;
    case OIS::KC_NUMPAD5: if (text) *text=53; return 6;
    case OIS::KC_NUMPAD6: if (text) *text=54; return 7;
    case OIS::KC_NUMPAD7: if (text) *text=55; return 8;
    case OIS::KC_NUMPAD8: if (text) *text=56; return 9;
    case OIS::KC_NUMPAD9: if (text) *text=57; return 10;
    }

    return argkey;
}

// KeyListener
bool MyListener::keyPressed(const OIS::KeyEvent &arg)
{
    CEGUI::System &sys = CEGUI::System::getSingleton();
    unsigned int text = arg.text;
    sys.injectKeyDown(_considerNumpadKey(arg.key,&text));
    sys.injectChar(text);
    return true;
}

bool MyListener::keyReleased(const OIS::KeyEvent &arg)
{
    CEGUI::System::getSingleton().injectKeyUp(_considerNumpadKey(arg.key));
    return true;
}



Return to “Offtopic Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests