123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689 |
- #include "AdventureMapButton.h"
- #include "CAdvmapInterface.h"
- #include "SDL_Extensions.h"
- #include "../hch/CDefHandler.h"
- #include "CGameInfo.h"
- #include "../hch/CLodHandler.h"
- #include "../hch/CGeneralTextHandler.h"
- #include "../hch/CTownHandler.h"
- #include "../CCallback.h"
- #include "CConfigHandler.h"
- #include "Graphics.h"
- #include "CBattleInterface.h"
- #include "CPlayerInterface.h"
- #include "CMessage.h"
- /*
- * AdevntureMapButton.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- CButtonBase::CButtonBase()
- {
- bitmapOffset = 0;
- curimg=0;
- type=-1;
- abs=false;
- //active=false;
- notFreeButton = false;
- ourObj=NULL;
- state=0;
- text = NULL;
- }
- CButtonBase::~CButtonBase()
- {
- delete text;
- if(notFreeButton)
- return;
- for(int i =0; i<imgs.size();i++)
- for(int j=0;j<imgs[i].size();j++)
- SDL_FreeSurface(imgs[i][j]);
- }
- void CButtonBase::show(SDL_Surface * to)
- {
- int img = std::min(state+bitmapOffset,int(imgs[curimg].size()-1));
- img = std::max(0, img);
- if (abs)
- {
- blitAt(imgs[curimg][img],pos.x,pos.y,to);
- if(text)
- {//using "state" instead of "state == 1" screwed up hoverable buttons with text
- CSDL_Ext::printAt(text->text, text->x + pos.x + (state == 1), text->y + pos.y + (state == 1), text->font, text->color, to);
- }
- }
- else
- {
- blitAt(imgs[curimg][img],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y,to);
- }
- }
- void CButtonBase::showAll( SDL_Surface * to )
- {
- show(to);
- }
- void CButtonBase::addTextOverlay( const std::string Text, EFonts font, SDL_Color color)
- {
- text = new TextOverlay;
- text->text = Text;
- const Font *f = graphics->fonts[font];
- text->x = pos.w/2 - f->getWidth(Text.c_str())/2;
- text->y = pos.h/2 - f->height/2;
- text->font = font;
- text->color = color;
- }
- AdventureMapButton::AdventureMapButton ()
- {
- type=2;
- abs=true;
- hoverable = false;
- //active=false;
- ourObj=NULL;
- state=0;
- blocked = actOnDown = false;
- used = LCLICK | RCLICK | HOVER | KEYBOARD;
- }
- //AdventureMapButton::AdventureMapButton( std::string Name, std::string HelpBox, boost::function<void()> Callback, int x, int y, std::string defName, bool activ, std::vector<std::string> * add, bool playerColoredButton)
- //{
- // init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, activ);
- //}
- AdventureMapButton::AdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName,int key, std::vector<std::string> * add, bool playerColoredButton )
- {
- std::map<int,std::string> pom;
- pom[0] = Name;
- init(Callback, pom, HelpBox, playerColoredButton, defName, add, x, y, key);
- }
- AdventureMapButton::AdventureMapButton( const std::map<int,std::string> &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName, int key, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
- {
- init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, key);
- }
- AdventureMapButton::AdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key/*=0*/ )
- {
- std::map<int,std::string> pom;
- pom[0] = Name;
- init(Callback, pom, HelpBox, info->playerColoured, info->defName, &info->additionalDefs, info->x, info->y, key);
- }
- AdventureMapButton::AdventureMapButton( const std::pair<std::string, std::string> help, const CFunctionList<void()> &Callback, int x, int y, const std::string &defName, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
- {
- std::map<int,std::string> pom;
- pom[0] = help.first;
- init(Callback, pom, help.second, playerColoredButton, defName, add, x, y, key);
- }
- void AdventureMapButton::clickLeft(tribool down, bool previousState)
- {
- if(blocked)
- return;
- if (down)
- {
- CGI->soundh->playSound(soundBase::button);
- state = 1;
- }
- else if(hoverable && hovered)
- state = 3;
- else
- state = 0;
- show(screenBuf);
- if (actOnDown && down)
- {
- callback();
- }
- else if (!actOnDown && previousState && (down==false))
- {
- callback();
- }
- }
- void AdventureMapButton::clickRight(tribool down, bool previousState)
- {
- if(down && helpBox.size()) //there is no point to show window with nothing inside...
- CRClickPopup::createAndPush(helpBox);
- }
- void AdventureMapButton::hover (bool on)
- {
- if(hoverable)
- {
- if(on)
- state = 3;
- else
- state = 0;
- }
- if(pressedL)
- {
- if(on)
- state = 1;
- else
- state = hoverable ? 3 : 0;
- }
- ////Hoverable::hover(on);
- std::string *name = (vstd::contains(hoverTexts,state))
- ? (&hoverTexts[state])
- : (vstd::contains(hoverTexts,0) ? (&hoverTexts[0]) : NULL);
- if(name && name->size() && blocked!=1) //if there is no name, there is nohing to display also
- {
- if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
- {
- if(on && LOCPLINT->battleInt->console->alterTxt == "")
- {
- LOCPLINT->battleInt->console->alterTxt = *name;
- LOCPLINT->battleInt->console->whoSetAlter = 1;
- }
- else if (LOCPLINT->battleInt->console->alterTxt == *name)
- {
- LOCPLINT->battleInt->console->alterTxt = "";
- LOCPLINT->battleInt->console->whoSetAlter = 0;
- }
- }
- else if(GH.statusbar) //for other buttons
- {
- if (on)
- GH.statusbar->print(*name);
- else if ( GH.statusbar->getCurrent()==(*name) )
- GH.statusbar->clear();
- }
- }
- }
- void AdventureMapButton::init(const CFunctionList<void()> &Callback, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key)
- {
- used = LCLICK | RCLICK | HOVER | KEYBOARD;
- callback = Callback;
- blocked = actOnDown = false;
- type=2;
- abs=true;
- hoverable = false;
- //active=false;
- ourObj=NULL;
- assignedKeys.insert(key);
- state=0;
- hoverTexts = Name;
- helpBox=HelpBox;
- setDef(defName, playerColoredButton);
- if (add && add->size())
- {
- imgs.resize(imgs.size()+add->size());
- for (size_t i=0; i<add->size();i++)
- {
- CDefHandler *temp = CDefHandler::giveDef((*add)[i]);
- temp->notFreeImgs = true;
- for (size_t j=0;j<temp->ourImages.size();j++)
- {
- imgs[i+1].push_back(temp->ourImages[j].bitmap);
- if(playerColoredButton)
- {
- graphics->blueToPlayersAdv(imgs[1+i][j],LOCPLINT->playerID);
- }
- }
- delete temp;
- }
- //delete add;
- }
- pos.x += x;
- pos.y += y;
- pos.w = imgs[curimg][0]->w;
- pos.h = imgs[curimg][0]->h -1;
- }
- void AdventureMapButton::block( ui8 on )
- {
- blocked = on;
- state = 0;
- bitmapOffset = on ? 2 : 0;
- show(screenBuf);
- }
- void AdventureMapButton::setDef(const std::string & defName, bool playerColoredButton, bool reset/*=false*/)
- {
- if (reset)
- {
- for (size_t i=0;i<imgs[0].size();i++)
- SDL_FreeSurface(imgs[0][i]);
- imgs[0].clear();
- }
-
- CDefHandler * temp = CDefHandler::giveDef(defName);
- temp->notFreeImgs = true;
- for (size_t i=0;i<temp->ourImages.size();i++)
- {
- imgs.resize(1);
- imgs[0].push_back(temp->ourImages[i].bitmap);
- if(playerColoredButton)
- {
- graphics->blueToPlayersAdv(imgs[curimg][i],LOCPLINT->playerID);
- }
- }
- delete temp;
- }
- void AdventureMapButton::setPlayerColor(int player)
- {
- for(int i =0; i<imgs.size();i++)
- for(int j=0;j<imgs[i].size();j++)
- graphics->blueToPlayersAdv(imgs[i][j],player);
- }
- void CHighlightableButton::select(bool on)
- {
- selected = on;
- state = selected ? 3 : 0;
- if(selected)
- callback();
- else
- callback2();
- if(hoverTexts.size()>1)
- {
- hover(false);
- hover(true);
- }
- }
- void CHighlightableButton::clickLeft(tribool down, bool previousState)
- {
- if(blocked)
- return;
- if (down)
- {
- CGI->soundh->playSound(soundBase::button);
- state = 1;
- }
- else
- state = selected ? 3 : 0;
- show(screenBuf);
- if(previousState && down == false)
- {
- if(!onlyOn || !selected)
- select(!selected);
- }
- }
- CHighlightableButton::CHighlightableButton( const CFunctionList<void()> &onSelect, const CFunctionList<void()> &onDeselect, const std::map<int,std::string> &Name, const std::string &HelpBox, bool playerColoredButton, const std::string &defName, std::vector<std::string> * add, int x, int y, int key)
- : onlyOn(false), selected(false), callback2(onDeselect)
- {
- init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
- }
- CHighlightableButton::CHighlightableButton( const std::pair<std::string, std::string> help, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
- : onlyOn(false), selected(false) // TODO: callback2(???)
- {
- ID = myid;
- std::map<int,std::string> pom;
- pom[0] = help.first;
- init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
- }
- CHighlightableButton::CHighlightableButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
- : onlyOn(false), selected(false) // TODO: callback2(???)
- {
- ID = myid;
- std::map<int,std::string> pom;
- pom[0] = Name;
- init(onSelect, pom,HelpBox, playerColoredButton, defName, add, x, y, key);
- }
- void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
- {
- bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
- bt->onlyOn = true;
- 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, int key)
- {
- CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
- if(musicLike)
- {
- bt->bitmapOffset = buttons.size() - 3;
- }
- 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, bool musicLikeButtons)
- : musicLike(musicLikeButtons), onChange(OnChange)
- {}
- CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
- {
- for(size_t i=0;i<buttons.size();i++)
- {
- delete buttons[i];
- }
- }
- void CHighlightableButtonsGroup::activate()
- {
- for(size_t i=0;i<buttons.size();i++)
- {
- buttons[i]->activate();
- }
- }
- void CHighlightableButtonsGroup::deactivate()
- {
- for(size_t i=0;i<buttons.size();i++)
- {
- buttons[i]->deactivate();
- }
- }
- void CHighlightableButtonsGroup::select(int id, bool mode)
- {
- CHighlightableButton *bt = NULL;
- if(mode)
- {
- for(size_t 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(size_t 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(size_t i=0;i<buttons.size(); ++i)
- {
- if(!musicLike || (musicLike && buttons[i]->selected)) //if musicLike, print only selected button
- buttons[i]->show(to);
- }
- }
- void CHighlightableButtonsGroup::showAll( SDL_Surface * to )
- {
- show(to);
- }
- void CHighlightableButtonsGroup::block( ui8 on )
- {
- for(size_t i=0;i<buttons.size(); ++i)
- {
- buttons[i]->block(on);
- }
- }
- void CSlider::sliderClicked()
- {
- if(!(active & MOVE))
- {
- activateMouseMove();
- used |= MOVE;
- }
- }
- void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
- {
- float v = 0;
- if(horizontal)
- {
- if( std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2+40 || std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2 )
- return;
- v = sEvent.x - pos.x - 24;
- v *= positions;
- v /= (pos.w - 48);
- }
- else
- {
- if(std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2+40 || std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2 )
- return;
- v = sEvent.y - pos.y - 24;
- v *= positions;
- v /= (pos.h - 48);
- }
- v += 0.5f;
- if(v!=value)
- {
- moveTo(v);
- redrawSlider();
- }
- }
- void CSlider::redrawSlider()
- {
- //slider->show(screenBuf);
- }
- void CSlider::moveLeft()
- {
- moveTo(value-1);
- }
- void CSlider::moveRight()
- {
- moveTo(value+1);
- }
- void CSlider::moveTo(int to)
- {
- amax(to, 0);
- amin(to, positions);
- //same, old position?
- if(value == to)
- return;
- value = to;
- if(horizontal)
- {
- if(positions)
- {
- float part = (float)to/positions;
- part*=(pos.w-48);
- slider->pos.x = part + pos.x + 16;
- }
- else
- slider->pos.x = pos.x+16;
- }
- else
- {
- if(positions)
- {
- float part = (float)to/positions;
- part*=(pos.h-48);
- slider->pos.y = part + pos.y + 16;
- }
- else
- slider->pos.y = pos.y+16;
- }
- if(moved)
- moved(to);
- }
- void CSlider::clickLeft(tribool down, bool previousState)
- {
- if(down && !slider->blocked)
- {
- float pw = 0;
- float rw = 0;
- if(horizontal)
- {
- pw = GH.current->motion.x-pos.x-25;
- rw = pw / ((float)(pos.w-48));
- }
- else
- {
- pw = GH.current->motion.y-pos.y-24;
- rw = pw / ((float)(pos.h-48));
- }
- if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
- return;
- // if (rw>1) return;
- // if (rw<0) return;
- slider->clickLeft(true, slider->pressedL);
- moveTo(rw * positions + 0.5f);
- return;
- }
- if(active & MOVE)
- {
- deactivateMouseMove();
- used &= ~MOVE;
- }
- }
- CSlider::~CSlider()
- {
- delete imgs;
- }
- CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
- :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
- {
- OBJ_CONSTRUCTION_CAPTURING_ALL;
- setAmount(amount);
- used = LCLICK;
- strongInterest = true;
- left = new AdventureMapButton;
- right = new AdventureMapButton;
- slider = new AdventureMapButton;
- pos.x += x;
- pos.y += y;
- if(horizontal)
- {
- left->pos.y = slider->pos.y = right->pos.y = pos.y;
- left->pos.x = pos.x;
- right->pos.x = pos.x + totalw - 16;
- }
- else
- {
- left->pos.x = slider->pos.x = right->pos.x = pos.x;
- left->pos.y = pos.y;
- right->pos.y = pos.y + totalw - 16;
- }
- left->callback = boost::bind(&CSlider::moveLeft,this);
- right->callback = boost::bind(&CSlider::moveRight,this);
- slider->callback = boost::bind(&CSlider::sliderClicked,this);
- left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
- if(horizontal)
- {
- pos.h = 16;
- pos.w = totalw;
- }
- else
- {
- pos.w = 16;
- pos.h = totalw;
- }
- if(style == 0)
- {
- if (horizontal)
- imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
- else
- imgs = CDefHandler::giveDefEss("OVBUTN2.DEF");
- left->imgs.resize(1); right->imgs.resize(1); slider->imgs.resize(1);
- left->imgs[0].push_back(imgs->ourImages[0].bitmap); left->imgs[0].push_back(imgs->ourImages[1].bitmap);
- right->imgs[0].push_back(imgs->ourImages[2].bitmap); right->imgs[0].push_back(imgs->ourImages[3].bitmap);
- slider->imgs[0].push_back(imgs->ourImages[4].bitmap);
- left->notFreeButton = right->notFreeButton = slider->notFreeButton = true;
- }
- else
- {
- imgs = NULL;
- left->setDef(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", false);
- right->setDef(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", false);
- slider->setDef("SCNRBSL.DEF", false);
- }
- slider->actOnDown = true;
- value = -1;
- moveTo(Value);
- }
- void CSlider::block( bool on )
- {
- left->block(on);
- right->block(on);
- slider->block(on);
- }
- void CSlider::setAmount( int to )
- {
- amount = to;
- positions = to - capacity;
- amax(positions, 0);
- }
- void CSlider::showAll(SDL_Surface * to)
- {
- CSDL_Ext::fillRect(to, &pos, 0);
- CIntObject::showAll(to);
- }
- void CSlider::wheelScrolled(bool down, bool in)
- {
- moveTo(value + 3 * (down ? +1 : -1));
- }
- void CSlider::keyPressed(const SDL_KeyboardEvent & key)
- {
- if(key.state != SDL_PRESSED) return;
- int moveDest = 0;
- switch(key.keysym.sym)
- {
- case SDLK_UP:
- moveDest = value - 1;
- break;
- case SDLK_DOWN:
- moveDest = value + 1;
- break;
- case SDLK_PAGEUP:
- moveDest = value - capacity + 1;
- break;
- case SDLK_PAGEDOWN:
- moveDest = value + capacity - 1;
- break;
- case SDLK_HOME:
- moveDest = 0;
- break;
- case SDLK_END:
- moveDest = amount - capacity;
- break;
- default:
- return;
- }
- moveTo(moveDest);
- }
|