Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » weird text input problem

This thread is locked; no one can reply to it. rss feed Print
weird text input problem
julian_boolean
Member #8,201
January 2007

What happens is when I type without actually clicking on the text field, then click on it, the text will appear even though I said for it to only poll the text code IF the flag is set to W_SELECTED. What can I do to fix this?

1void widget::input(void)
2{
3 if(flags == W_SELECTED)
4 {
5 while(keypressed())
6 {
7 int newkey = readkey();
8 char ASCII = newkey & 0xff;
9 char scancode = newkey >> 8;
10 
11 if(ASCII >= 32 && ASCII <= 126 && text.length() < length)
12 {
13 if(insert || iter == text.end())
14 iter = text.insert(iter, ASCII);
15
16 else
17 text.replace(caret, 1, 1, ASCII);
18
19 caret++;
20 iter++;
21 }
22 
23 else
24 switch(scancode)
25 {
26 case KEY_DEL:
27 if(iter != text.end()) iter = text.erase(iter);
28 break;
29 
30 case KEY_BACKSPACE:
31 if(iter != text.begin())
32 {
33 caret--;
34 iter--;
35 iter = text.erase(iter);
36 }break;
37
38 case KEY_RIGHT:
39 if(iter != text.end()) caret++, iter++;
40 break;
41
42 case KEY_LEFT:
43 if(iter != text.begin()) caret--, iter--;
44 break;
45
46 case KEY_INSERT:
47 insert = !insert;
48 break;
49 
50 default:
51 break;
52 }
53 }
54 }
55}
56 
57// elsewhere in a switch.. what the hell? isnt this black and white?
58 
59 case W_SELECTED:
60 {
61 text_input();
62 return true;
63 }break;

Matthew Dalrymple
Member #7,922
October 2006
avatar

clear_keybuf();

I'm pretty sure you'll need that if no input widgets are selected.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

I've already tried that. :-/ I put it inside the case AND I put an "else { clear_keybuf(); }" after the if statement in the input function and it doesn't do anything. ???

Edit:

Oh wait you mean inside every case that's NOT W_SELECTED. Isn't that kinda redundant though? Why is it even doing this to begin with, even after I specifically said for it NOT to poll if it's not selected.

Steve Terry
Member #1,989
March 2002
avatar

This shouldn't be part of the widget but rather the input manager or widget manager. That is to say you have a vector of input objects and none are selected then you can clear the keybuf there.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

julian_boolean
Member #8,201
January 2007

Widget is my base class for other classes such as Button or Textfield, and also holds various functions like input() or play_sound(). The only thing inside classes derived from Widget is a constructor and a function called is_clicked, which contains a switch (each case in the switch being a different state of the object, based on the class of course.)

Edit:

Might want to mention that I'm currently not using vectors for anything right now.

Steve Terry
Member #1,989
March 2002
avatar

Widgets should go in a widget manager class of some sort. Widgets themselves shouldn't have to know about any other widget to function properly.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

julian_boolean
Member #8,201
January 2007

The widget class does handle everything, by setting global variables based on what you do (if you hover over the object, if you click it, etc.) I just wanted to keep all the functions inside that one class so I wouldn't have to repeat them.

Then somewhere in the widget class I've got some kinda master draw function that also happened to be a switch, each case being a different frame and also checks the object's ID to draw text for buttons or the input text and a caret for textfields.

Matthew Dalrymple
Member #7,922
October 2006
avatar

Did you get things to work?

As Steve Terry was trying to make clear more then me is something like this:

vector<widget> widgets; // holds all widgets currently in use
bool noneSelected = true;
for(vector<widget>::iterator curWidget = widgets.begin(); curWidget != widgets.end(); curWidget++)
{
  if(curWidget->getType == TEXT_FIELD && curWidget->isSelected)
  {
    noneSelected = false;
  }
}
if(noneSelected)
{
  clear_keybuf();
}

At least I hope that helps/fixes things.:P

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

Trying to figure out where the hell to put this for statement. Putting it around the code in my handler function was definitely a bad idea.

1 enum { W_HIDDEN, W_DISABLED, W_LOSTMOUSE, W_GOTMOUSE, W_UNSELECTED, W_SELECTED, W_CLICKED, W_RELEASED };
2 
3// elsewhere
4 
5 void widget::handler(void)
6 {
7// for(current = widgets.begin(); current != widgets.end(); current++)
8// {
9 if(mouse_x > x_pos && mouse_x < w_pos + x_pos
10 && mouse_y > y_pos && mouse_y < h_pos + y_pos)
11 {
12 if(mouse_b &1)
13 flags = W_CLICKED;
14
15 else if(!(mouse_b &1) && flags == W_CLICKED)
16 flags = W_RELEASED;
17 
18 else
19 flags = W_GOTMOUSE;
20 }
21 
22 else
23 flags = W_LOSTMOUSE;
24// }
25 }

Also, I'm guessing that "getType == TEXT_FIELD" would be the same thing as my "ID == 0" (0 meaning it's a text field.)

Steve Terry
Member #1,989
March 2002
avatar

Quote:

The widget class does handle everything, by setting global variables based on what you do

:o:o:o:o

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

julian_boolean
Member #8,201
January 2007

What? Is something on my face?

Jonatan Hedborg
Member #4,886
July 2004
avatar

Yes... Globals. All over. No, you missed it. Yeah, there.... No, still there.
Bah, never mind - it's hardly noticeable...

julian_boolean
Member #8,201
January 2007

Lawl.

Is there something actually wrong with this?

Rampage
Member #3,035
December 2002
avatar

Quote:

Is there something actually wrong with this?

Yes: the globals! You should store all your state variables in a struct which you pass around to all the methods that need it.

-R

julian_boolean
Member #8,201
January 2007

How might I do that?

Actually, how is it even better? Wouldn't it be basically doing the same thing.. And why are the globals not good for this?

Steve Terry
Member #1,989
March 2002
avatar

Also based on your widget::handler method think about what would happen if you set your flags to disabled then moused over the widget. That code would set the flags to W_GOTMOUSE essentially removing the disabled state. What you need desperately is another class as so:

1class widget_manager
2{
3 private:
4 std::vector<widget> WidgetList;
5 // Your global variables
6 public:
7 int add_widget(widget w);
8 int remove_widget(widget w);
9 void update();
10};
11 
12int widget_manager::add_widget(widget w)
13{
14 return WidgetList.push_back(w);
15}
16 
17void widget_manager::update()
18{
19 for(vector<widget>::iterator iter = WidgetList.begin(); iter != WidgetList.end(); iter++)
20 {
21 // handle widgets
22 }
23}

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Rampage
Member #3,035
December 2002
avatar

Why you should never use globals.

Remove the globals for your code to be easier to maintain. If you want to add new functionality to your code three months from now, it'll be easier to understand and modify.

-R

julian_boolean
Member #8,201
January 2007

Fair enough, that looks good.. But I'm not sure how I'm suppose to "handle" the different states of my objects without setting any globals.

Steve Terry
Member #1,989
March 2002
avatar

Humor us and show the global variables you are using and we can try and help you place them in a meaningful place.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

julian_boolean
Member #8,201
January 2007

Scroll up about 10 posts or so, it's up there. And I see what you meant by the flag resetting itself as soon as you scroll your mouse over the object. I mean, when it's just dealing with normal buttons the code works wonders, but after I started to get into dealing with anything radial it began screwing me up royally.

Go to: