Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Radio.. Yes same ol' problem.

This thread is locked; no one can reply to it. rss feed Print
Radio.. Yes same ol' problem.
julian_boolean
Member #8,201
January 2007

I'm STILL trying to finish this one function for a radio button class I've been working on for the past 4 months called is_selected() but since I'm an idiot, it's slightly difficult for me.

Basically I want the function to do this:

1) Check to see if the object that's stored in the vector is in fact of the radio button class by checking it's type. (type = radio) All my GUI objects are stored in the same vector.

2) Check to see what the group ID is.

3) Check to see if your mouse is hovered over the object. The function for this is already done (is_hovered())

4) Check to see if you clicked on the object. If so, that object will become selected and all other objects within that group will become unselected. This will also return true. If nothing is clicked, then return false.

So outside the class it would be used something like this:

if(myRadioButtonInstance->is_selected())
{
  // do some stuff
}

ixilom
Member #7,167
April 2006
avatar

What exactly is the problem?

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

julian_boolean
Member #8,201
January 2007

That I have no clue how to make the function.. Suppose I could've mentioned that.

ixilom
Member #7,167
April 2006
avatar

Not really sure if I follow, but I'll try to write some sort of simplified code of what I think you're asking for

1iter = vecWidgets.begin();
2// loop through all the widgets
3while(iter!=vecWidgets.end())
4{
5 // is it a radiobutton?
6 if(iter->type==wtRadio) {
7 // get a real pointer to the RadioButton class
8 CRadioButton *rb = (CRadioButton*)iter;
9 // is this one currently being clicked on?
10 if(rb->is_hovered() && rb->is_clicked()) {
11 // we need to loop through the widgets again to find the other RadioButtons in the same GroupID
12 iter2 = vecWidgets.begin();
13 while(iter2!=vecWidgets.end()) {
14 // is this a radiobutton?
15 if(iter2->type==wtRadio) {
16 // get real pointer to RadioButton class
17 CRadioButton *rb2 = (CRadioButton*)iter2;
18 // is the radiobutton in the same group as the other (from outter while-loop)
19 if(rb2->GroupID==rb->GroupID) {
20 // change it's Selected property to true/false depending on whetter its the clicked one or not
21 rb2->Selected = (rb2==rb);
22 }
23 }
24 ++iter2;
25 
26 }
27 }
28 }
29 
30 ++iter;
31}

I'm sure that could be optimized in like 10 ways, but I'm too lazy 8-)

[Edit]
Sorry about the crappy intendation, wrote that in notepad :P

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

julian_boolean
Member #8,201
January 2007

Thanks :) going to take a closer look at that.

I made another post about this before, well similar to it and this was one of the examples given:

1vector<radiobutton> radios;
2 
3void changeSelection(int selection)
4{
5 vector<radiobutton>::iterator selectedRadio = radios.at(selection);
6 int theGroup = selectedRadio->getGroup();
7 for(vector<radiobutton>::iterator curRadio = radios.begin(); curRadio != radios.end(); curRadio++)
8 {
9 if(curRadio == selectedRadio)
10 {
11 curRadio->selected = true;
12 }
13 else
14 {
15 if(curRadio->getGroup() == theGroup)
16 {
17 curRadio->selected = false;
18 }
19 }
20 }
21}

The problem was I couldn't seem to alter it the way I wanted.. Like where would I stick in that is_hovered function, or to check to see if the object was clicked on? The whole int selection part sorta confused me too.

ixilom
Member #7,167
April 2006
avatar

As I see it, that function needs to be called when you have found the RadioButton that has been clicked (and hovering above it).
The parameter is the index in the vector of radiobuttons. Observe, its a separate vector for radiobuttons only, not all the widgets.

so, somewhere in your logic you would do something like

unsigned int RadioIndex = 0;
while(RadioIndex < radios.size()) {
  if(radios.at(RadioIndex)->is_hovered() && bMouse & 1) {
    changeSelection(RadioIndex);
    break;
  }
  RadioIndex++;
}

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

julian_boolean
Member #8,201
January 2007

There's no way to combine all that together in one function that will return a true once an object within the group is selected/clicked?

ixilom
Member #7,167
April 2006
avatar

Not really sure what you mean, but here goes :P

1vector<radiobutton> radios;
2 
3bool CheckRadioButtons(void)
4{
5 bool bRadioWasSelected = false;
6 int RadioIndex = 0;
7 while(RadioIndex < radios.size()) {
8 if(radios.at(RadioIndex)->is_hovered() && bMouse & 1) {
9 vector<radiobutton>::iterator selectedRadio = radios.at(selection);
10 int theGroup = selectedRadio->getGroup();
11 for(vector<radiobutton>::iterator curRadio = radios.begin(); curRadio != radios.end(); curRadio++) {
12 if(curRadio == selectedRadio) {
13 bRadioWasSelected = true;
14 curRadio->selected = true;
15 }
16 else if(curRadio->getGroup() == theGroup) {
17 curRadio->selected = false;
18 }
19 }
20 break;
21 }
22 RadioIndex++;
23 }
24 return bRadioWasSelected;
25}

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

julian_boolean
Member #8,201
January 2007

That looks promising! I still don't know what getGroup() was suppose to be, though.

ixilom
Member #7,167
April 2006
avatar

I'm guessing it is a "getter function", which basically just return an int that identifies that group (GroupID)

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

julian_boolean
Member #8,201
January 2007

Hmm okay then. Thank you again! :)

bamccaig
Member #7,536
July 2006
avatar

I haven't done any GUI programming yet so I'll just spit my opinion, whatever it's worth. :)

It sounds like the is_selected() method is supposed to do too much work. There should be events for mouse clicks or activation/deactivation, and there should be numerous methods for many of the things you want done with this one function. :-/

A GUI doesn't work well as a single thread.

ImLeftFooted
Member #3,935
October 2003
avatar

Quote:

A GUI doesn't work well as a single thread.

Funny, I would have said the opposite...:-X

julian_boolean
Member #8,201
January 2007

Quote:

It sounds like the is_selected() method is supposed to do too much work.

I've been looking this over again and I think your probably right. I might just put the for statement in a seperate function called updater() or something, which should look something like this:

void radioButton::updater()
{
  for (curRadio = radios.begin(); curRadio != radios.end(); curRadio++)
  {
    if(this->is_hovered && mouse_b &1)
    {
      this->selected = true;
      group->selected = false; // everything that has the same group_id is made false
    }
  }
}

I just don't understand the whole, tracking-the-group-and-switching-its-bool-to-false, part.

Go to: