diff --git a/AdventureMapButton.cpp b/AdventureMapButton.cpp index f2c712822..4e781e155 100644 --- a/AdventureMapButton.cpp +++ b/AdventureMapButton.cpp @@ -201,6 +201,68 @@ CHighlightableButton::CHighlightableButton( const CFunctionList &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 &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList &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 &OnChange) +{ + onChange = OnChange; +} +CHighlightableButtonsGroup::~CHighlightableButtonsGroup() +{ + for(int i=0;iactivate(); +} +void CHighlightableButtonsGroup::deactivate() +{ + for(int i=0;ideactivate(); +} +void CHighlightableButtonsGroup::select(int id, bool mode) +{ + CHighlightableButton *bt = NULL; + if(mode) + { + for(int i=0;iID == id) + bt = buttons[i]; + } + else + { + bt = buttons[id]; + } + bt->select(true); + selectionChanged(bt->ID); +} + +void CHighlightableButtonsGroup::selectionChanged(int to) +{ + for(int i=0;iID!=to && buttons[i]->selected) + buttons[i]->select(false); + onChange(to); +} +void CHighlightableButtonsGroup::show(SDL_Surface * to ) +{ + for(int i=0;ishow(); +} + void CSlider::sliderClicked() { if(!moving) diff --git a/AdventureMapButton.h b/AdventureMapButton.h index cc1686769..07e923e93 100644 --- a/AdventureMapButton.h +++ b/AdventureMapButton.h @@ -40,8 +40,25 @@ public: void clickLeft (tribool down); }; +class CHighlightableButtonsGroup +{ +public: + CFunctionList2 onChange; //called when changing selected button with new button's id + std::vector buttons; + + //void addButton(const std::map &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 &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList &OnSelect=0); //creates new button + CHighlightableButtonsGroup(const CFunctionList2 &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 class CSlider : public IShowable, public MotionInterested, public ClickableL { public: diff --git a/CHeroWindow.cpp b/CHeroWindow.cpp index 5d984b219..cf1d70ce7 100644 --- a/CHeroWindow.cpp +++ b/CHeroWindow.cpp @@ -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(), 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 diff --git a/CHeroWindow.h b/CHeroWindow.h index 57c3ce68f..8d3933119 100644 --- a/CHeroWindow.h +++ b/CHeroWindow.h @@ -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 diff --git a/client/FunctionList.h b/client/FunctionList.h index 133e02e39..9a86ea897 100644 --- a/client/FunctionList.h +++ b/client/FunctionList.h @@ -27,11 +27,6 @@ public: funcs.push_back(first); return *this; } - //CFunctionList & operator=(const boost::function &first) - //{ - // funcs.push_back(first); - // return first; - //} void clear() { funcs.clear(); @@ -53,4 +48,47 @@ public: for(int i=0;i +class CFunctionList2 +{ +public: + std::vector > funcs; + + CFunctionList2(int){}; + CFunctionList2(){}; + template + CFunctionList2(const Functor &f) + { + funcs.push_back(boost::function(f)); + } + CFunctionList2(const boost::function &first) + { + funcs.push_back(first); + } + CFunctionList2(boost::function &first) + { + funcs.push_back(first); + } + CFunctionList2 & operator+=(const boost::function &first) + { + funcs.push_back(first); + return *this; + } + void clear() + { + funcs.clear(); + } + operator bool() const + { + return funcs.size(); + } + template + void operator()(const Arg & a) const + { + std::vector > funcs2 = funcs; //backup + for(int i=0;i