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 }
What exactly is the problem?
That I have no clue how to make the function.. Suppose I could've mentioned that.
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
| 1 | iter = vecWidgets.begin(); |
| 2 | // loop through all the widgets |
| 3 | while(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 
[Edit]
Sorry about the crappy intendation, wrote that in notepad
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:
| 1 | vector<radiobutton> radios; |
| 2 | |
| 3 | void 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.
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++; }
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?
Not really sure what you mean, but here goes
| 1 | vector<radiobutton> radios; |
| 2 | |
| 3 | bool 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 | } |
That looks promising! I still don't know what getGroup() was suppose to be, though.
I'm guessing it is a "getter function", which basically just return an int that identifies that group (GroupID)
Hmm okay then. Thank you again!
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.
A GUI doesn't work well as a single thread.
Funny, I would have said the opposite...:-X
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.