1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

classes for buttons group and functionlist with arg

This commit is contained in:
Michał W. Urbańczyk 2008-09-24 08:15:49 +00:00
parent c00d0e4aca
commit 16303ab1df
5 changed files with 139 additions and 45 deletions

View File

@ -201,6 +201,68 @@ CHighlightableButton::CHighlightableButton( const CFunctionList<void()> &onSelec
init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,activ);
callback2 = onDeselect;
}
void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
{
bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
buttons.push_back(bt);
}
void CHighlightableButtonsGroup::addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList<void()> &OnSelect)
{
CHighlightableButton *bt = new CHighlightableButton(OnSelect,0,tooltip,HelpBox,false,defName,0,x,y,0);
bt->ID = uid;
bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
bt->onlyOn = true;
buttons.push_back(bt);
}
CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange)
{
onChange = OnChange;
}
CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
{
for(int i=0;i<buttons.size();i++)
delete buttons[i];
}
void CHighlightableButtonsGroup::activate()
{
for(int i=0;i<buttons.size();i++)
buttons[i]->activate();
}
void CHighlightableButtonsGroup::deactivate()
{
for(int i=0;i<buttons.size();i++)
buttons[i]->deactivate();
}
void CHighlightableButtonsGroup::select(int id, bool mode)
{
CHighlightableButton *bt = NULL;
if(mode)
{
for(int i=0;i<buttons.size() && !bt;i++)
if (buttons[i]->ID == id)
bt = buttons[i];
}
else
{
bt = buttons[id];
}
bt->select(true);
selectionChanged(bt->ID);
}
void CHighlightableButtonsGroup::selectionChanged(int to)
{
for(int i=0;i<buttons.size();i++)
if(buttons[i]->ID!=to && buttons[i]->selected)
buttons[i]->select(false);
onChange(to);
}
void CHighlightableButtonsGroup::show(SDL_Surface * to )
{
for(int i=0;i<buttons.size();i++)
buttons[i]->show();
}
void CSlider::sliderClicked()
{
if(!moving)

View File

@ -40,8 +40,25 @@ public:
void clickLeft (tribool down);
};
class CHighlightableButtonsGroup
{
public:
CFunctionList2<void(int)> onChange; //called when changing selected button with new button's id
std::vector<CHighlightableButton*> buttons;
//void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid);
void addButton(CHighlightableButton* bt);//add existing button, it'll be deleted by CHighlightableButtonsGroup destructor
void addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList<void()> &OnSelect=0); //creates new button
CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange);
~CHighlightableButtonsGroup();
void activate();
void deactivate();
void select(int id, bool mode); //mode==0: id is serial; mode==1: id is unique button id
void selectionChanged(int to);
void show(SDL_Surface * to = NULL);
};
//template<typename T>
class CSlider : public IShowable, public MotionInterested, public ClickableL
{
public:

View File

@ -48,9 +48,12 @@ CHeroWindow::CHeroWindow(int playerColor):
dismissButton = new AdventureMapButton(std::string(), CGI->generaltexth->heroscrn[28], boost::bind(&CHeroWindow::dismissCurrent,this), 519, 437, "hsbtns2.def", false, NULL, false);
questlogButton = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CHeroWindow::questlog,this), 379, 437, "hsbtns4.def", false, NULL, false);
gar1button = new CHighlightableButton(0,0,map_list_of(0,CGI->generaltexth->heroscrn[23]), CGI->generaltexth->heroscrn[29], false, "hsbtns6.def" , NULL,546, 491, false);
gar3button = new CHighlightableButton(0,0,map_list_of(0,CGI->generaltexth->heroscrn[24]), CGI->generaltexth->heroscrn[30], false, "hsbtns7.def", NULL, 546, 527, false);
gar1button->onlyOn = gar3button->onlyOn = true;
formations = new CHighlightableButtonsGroup(0);
formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[23]),CGI->generaltexth->heroscrn[29], "hsbtns6.def",546, 491, 0);
formations->addButton(map_list_of(0,CGI->generaltexth->heroscrn[24]),CGI->generaltexth->heroscrn[30], "hsbtns7.def",546, 527, 1);
//gar1button = new CHighlightableButton(0,0,map_list_of(0,CGI->generaltexth->heroscrn[23]), CGI->generaltexth->heroscrn[29], false, "hsbtns6.def" , NULL,546, 491, false);
//gar3button = new CHighlightableButton(0,0,map_list_of(0,CGI->generaltexth->heroscrn[24]), CGI->generaltexth->heroscrn[30], false, "hsbtns7.def", NULL, 546, 527, false);
gar2button = new CHighlightableButton(0, 0, map_list_of(0,CGI->generaltexth->heroscrn[26])(3,CGI->generaltexth->heroscrn[25]), CGI->generaltexth->heroscrn[31], false, "hsbtns8.def", NULL, 604, 491, false);
gar4button = new AdventureMapButton(CGI->generaltexth->allTexts[256], CGI->generaltexth->heroscrn[32], boost::function<void()>(), 604, 527, "hsbtns9.def", false, NULL, false);
@ -120,9 +123,8 @@ CHeroWindow::~CHeroWindow()
delete quitButton;
delete dismissButton;
delete questlogButton;
delete gar1button;
delete formations;
delete gar2button;
delete gar3button;
delete gar4button;
delete leftArtRoll;
delete rightArtRoll;
@ -171,9 +173,8 @@ void CHeroWindow::show(SDL_Surface *to)
quitButton->show();
dismissButton->show();
questlogButton->show();
gar1button->show();
formations->show();
gar2button->show();
gar3button->show();
gar4button->show();
leftArtRoll->show();
rightArtRoll->show();
@ -312,16 +313,10 @@ void CHeroWindow::setHero(const CGHeroInstance *Hero)
gar2button->callback = vstd::assigno(hero->tacticFormationEnabled,true);
gar2button->callback2 = vstd::assigno(hero->tacticFormationEnabled,false);
}
gar1button->callback.clear();
gar1button->callback += boost::bind(&CHighlightableButton::select, gar3button, false);
gar1button->callback += boost::bind(&CCallback::setFormation, LOCPLINT->cb, Hero, false);
gar3button->callback.clear();
gar3button->callback += boost::bind(&CHighlightableButton::select, gar1button, false);
gar3button->callback += boost::bind(&CCallback::setFormation, LOCPLINT->cb, Hero, true);
if(hero->army.formation)
gar3button->select(true);
else
gar1button->select(true);
formations->onChange = boost::bind(&CCallback::setFormation, LOCPLINT->cb, Hero, _1);
formations->select(hero->army.formation,true);
redrawCurBack();
}
@ -357,9 +352,8 @@ void CHeroWindow::activate()
quitButton->activate();
dismissButton->activate();
questlogButton->activate();
gar1button->activate();
gar2button->activate();
gar3button->activate();
formations->activate();
gar4button->activate();
leftArtRoll->activate();
rightArtRoll->activate();
@ -403,9 +397,8 @@ void CHeroWindow::deactivate()
quitButton->deactivate();
dismissButton->deactivate();
questlogButton->deactivate();
gar1button->deactivate();
gar2button->deactivate();
gar3button->deactivate();
formations->deactivate();
gar4button->deactivate();
leftArtRoll->deactivate();
rightArtRoll->deactivate();
@ -450,19 +443,6 @@ void CHeroWindow::dismissCurrent()
void CHeroWindow::questlog()
{
}
void CHeroWindow::gar1()
{
}
void CHeroWindow::gar3()
{
}
void CHeroWindow::gar4()
{
}
void CHeroWindow::leftArtRoller()
{
if(curHero->artifacts.size()>5) //if it is <=5, we have nothing to scroll

View File

@ -106,7 +106,8 @@ class CHeroWindow: public IShowActivable, public virtual CIntObject
public:
AdventureMapButton * quitButton, * dismissButton, * questlogButton, //general
* leftArtRoll, * rightArtRoll;
CHighlightableButton *gar2button, *gar1button, *gar3button; //garrison / formation handling;
CHighlightableButton *gar2button; //garrison / formation handling;
CHighlightableButtonsGroup *formations;
int player;
CHeroWindow(int playerColor); //c-tor
~CHeroWindow(); //d-tor
@ -118,10 +119,6 @@ public:
void quit(); //stops displaying hero window
void dismissCurrent(); //dissmissed currently displayed hero (curHero)
void questlog(); //show quest log in hero window
void gar1(); //garrison / formation handling
void gar2(); //garrison / formation handling
void gar3(); //garrison / formation handling
void gar4(); //garrison / formation handling
void leftArtRoller(); //scrolls artifacts in bag left
void rightArtRoller(); //scrolls artifacts in bag right
void switchHero(); //changes displayed hero

View File

@ -27,11 +27,6 @@ public:
funcs.push_back(first);
return *this;
}
//CFunctionList<Signature> & operator=(const boost::function<Signature> &first)
//{
// funcs.push_back(first);
// return first;
//}
void clear()
{
funcs.clear();
@ -53,4 +48,47 @@ public:
for(int i=0;i<funcs2.size(); i++)
funcs2[i](a);
}
};
template<typename Signature>
class CFunctionList2
{
public:
std::vector<boost::function<Signature> > funcs;
CFunctionList2(int){};
CFunctionList2(){};
template <typename Functor>
CFunctionList2(const Functor &f)
{
funcs.push_back(boost::function<Signature>(f));
}
CFunctionList2(const boost::function<Signature> &first)
{
funcs.push_back(first);
}
CFunctionList2(boost::function<Signature> &first)
{
funcs.push_back(first);
}
CFunctionList2 & operator+=(const boost::function<Signature> &first)
{
funcs.push_back(first);
return *this;
}
void clear()
{
funcs.clear();
}
operator bool() const
{
return funcs.size();
}
template <typename Arg>
void operator()(const Arg & a) const
{
std::vector<boost::function<Signature> > funcs2 = funcs; //backup
for(int i=0;i<funcs2.size(); i++)
funcs2[i](a);
}
};