2009-05-19 21:23:04 +03:00
|
|
|
#include "AdventureMapButton.h"
|
2010-11-15 17:15:00 +02:00
|
|
|
#include "CAnimation.h"
|
2008-05-18 20:33:39 +03:00
|
|
|
#include "CAdvmapInterface.h"
|
|
|
|
#include "SDL_Extensions.h"
|
|
|
|
#include "CGameInfo.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "../lib/CLodHandler.h"
|
|
|
|
#include "../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../lib/CTownHandler.h"
|
2009-05-20 13:08:56 +03:00
|
|
|
#include "../CCallback.h"
|
|
|
|
#include "CConfigHandler.h"
|
|
|
|
#include "Graphics.h"
|
2009-01-29 20:07:31 +02:00
|
|
|
#include "CBattleInterface.h"
|
2009-05-20 12:02:50 +03:00
|
|
|
#include "CPlayerInterface.h"
|
2009-08-17 11:50:31 +03:00
|
|
|
#include "CMessage.h"
|
2010-12-20 23:22:53 +02:00
|
|
|
#include "CMusicHandler.h"
|
2009-04-15 17:03:31 +03:00
|
|
|
|
|
|
|
/*
|
2010-10-18 18:08:59 +03:00
|
|
|
* AdventureMapButton.cpp, part of VCMI engine
|
2009-04-15 17:03:31 +03:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-08-17 11:50:31 +03:00
|
|
|
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;
|
2010-10-18 18:08:59 +03:00
|
|
|
for (size_t i = 0; i<imgs.size(); i++)
|
|
|
|
delete imgs[i];
|
|
|
|
imgs.clear();
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CButtonBase::show(SDL_Surface * to)
|
|
|
|
{
|
2010-10-18 18:08:59 +03:00
|
|
|
int img = std::min(state+bitmapOffset,int(imgs[curimg]->size()-1));
|
2009-08-17 11:50:31 +03:00
|
|
|
img = std::max(0, img);
|
|
|
|
|
2010-10-25 00:29:18 +03:00
|
|
|
SDL_Surface *toBlit = imgs[curimg]->image(img);
|
|
|
|
|
2009-08-17 11:50:31 +03:00
|
|
|
if (abs)
|
|
|
|
{
|
2010-10-25 00:29:18 +03:00
|
|
|
if(toBlit)
|
|
|
|
{
|
|
|
|
blitAt(toBlit,pos.x,pos.y,to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SDL_Rect r = pos;
|
|
|
|
SDL_FillRect(to, &r, 0x505000);
|
|
|
|
}
|
2009-08-17 11:50:31 +03:00
|
|
|
if(text)
|
2010-01-30 21:08:26 +02:00
|
|
|
{//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);
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-25 00:29:18 +03:00
|
|
|
blitAt(toBlit,pos.x+ourObj->pos.x,pos.y+ourObj->pos.y,to);
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButtonBase::showAll( SDL_Surface * to )
|
|
|
|
{
|
|
|
|
show(to);
|
|
|
|
}
|
|
|
|
|
2010-01-30 21:08:26 +02:00
|
|
|
void CButtonBase::addTextOverlay( const std::string Text, EFonts font, SDL_Color color)
|
2009-08-17 11:50:31 +03:00
|
|
|
{
|
2010-09-03 21:42:54 +03:00
|
|
|
delete text;
|
2009-08-17 11:50:31 +03:00
|
|
|
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;
|
2010-02-01 19:07:46 +02:00
|
|
|
text->font = font;
|
2010-01-30 21:08:26 +02:00
|
|
|
text->color = color;
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-18 20:33:39 +03:00
|
|
|
AdventureMapButton::AdventureMapButton ()
|
|
|
|
{
|
|
|
|
type=2;
|
|
|
|
abs=true;
|
2009-08-17 11:50:31 +03:00
|
|
|
hoverable = false;
|
|
|
|
//active=false;
|
2008-05-18 20:33:39 +03:00
|
|
|
ourObj=NULL;
|
|
|
|
state=0;
|
2008-08-27 13:19:18 +03:00
|
|
|
blocked = actOnDown = false;
|
2009-09-13 01:17:23 +03:00
|
|
|
used = LCLICK | RCLICK | HOVER | KEYBOARD;
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
2008-08-25 13:25:16 +03:00
|
|
|
//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);
|
|
|
|
//}
|
2008-10-19 16:17:32 +03:00
|
|
|
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 )
|
2008-08-15 15:11:42 +03:00
|
|
|
{
|
2008-08-28 20:36:34 +03:00
|
|
|
std::map<int,std::string> pom;
|
|
|
|
pom[0] = Name;
|
2008-10-19 16:17:32 +03:00
|
|
|
init(Callback, pom, HelpBox, playerColoredButton, defName, add, x, y, key);
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
|
2008-10-19 16:17:32 +03:00
|
|
|
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 */ )
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
2008-10-19 16:17:32 +03:00
|
|
|
init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, key);
|
2008-08-28 20:36:34 +03:00
|
|
|
}
|
2008-05-18 20:33:39 +03:00
|
|
|
|
2008-11-12 20:26:23 +02:00
|
|
|
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);
|
|
|
|
}
|
2009-02-06 13:15:39 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2009-08-11 19:05:33 +03:00
|
|
|
void AdventureMapButton::clickLeft(tribool down, bool previousState)
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2008-08-27 13:19:18 +03:00
|
|
|
if(blocked)
|
|
|
|
return;
|
2009-11-21 00:35:18 +02:00
|
|
|
|
|
|
|
if (down)
|
|
|
|
{
|
2010-12-19 16:39:56 +02:00
|
|
|
CCS->soundh->playSound(soundBase::button);
|
2010-03-08 00:56:51 +02:00
|
|
|
state = 1;
|
2009-11-21 00:35:18 +02:00
|
|
|
}
|
2010-03-08 00:56:51 +02:00
|
|
|
else if(hoverable && hovered)
|
|
|
|
state = 3;
|
2009-11-21 00:35:18 +02:00
|
|
|
else
|
2010-03-08 00:56:51 +02:00
|
|
|
state = 0;
|
2009-11-21 00:35:18 +02:00
|
|
|
|
2009-05-25 02:21:55 +03:00
|
|
|
show(screenBuf);
|
2008-05-18 20:33:39 +03:00
|
|
|
if (actOnDown && down)
|
|
|
|
{
|
2009-08-11 19:05:33 +03:00
|
|
|
callback();
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
2009-08-11 19:05:33 +03:00
|
|
|
else if (!actOnDown && previousState && (down==false))
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2009-08-11 19:05:33 +03:00
|
|
|
callback();
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-11 19:05:33 +03:00
|
|
|
void AdventureMapButton::clickRight(tribool down, bool previousState)
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2009-08-17 11:50:31 +03:00
|
|
|
if(down && helpBox.size()) //there is no point to show window with nothing inside...
|
2010-03-21 00:17:19 +02:00
|
|
|
CRClickPopup::createAndPush(helpBox);
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AdventureMapButton::hover (bool on)
|
|
|
|
{
|
2009-08-17 11:50:31 +03:00
|
|
|
if(hoverable)
|
|
|
|
{
|
|
|
|
if(on)
|
2009-08-27 11:04:32 +03:00
|
|
|
state = 3;
|
2009-08-17 11:50:31 +03:00
|
|
|
else
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pressedL)
|
|
|
|
{
|
|
|
|
if(on)
|
|
|
|
state = 1;
|
|
|
|
else
|
2009-08-27 11:04:32 +03:00
|
|
|
state = hoverable ? 3 : 0;
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
|
2009-08-11 19:05:33 +03:00
|
|
|
////Hoverable::hover(on);
|
2008-08-28 20:36:34 +03:00
|
|
|
std::string *name = (vstd::contains(hoverTexts,state))
|
|
|
|
? (&hoverTexts[state])
|
|
|
|
: (vstd::contains(hoverTexts,0) ? (&hoverTexts[0]) : NULL);
|
2010-02-15 08:54:57 +02:00
|
|
|
if(name && name->size() && blocked!=1) //if there is no name, there is nohing to display also
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2010-02-15 08:54:57 +02:00
|
|
|
if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
|
2009-01-29 20:07:31 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2010-02-15 08:54:57 +02:00
|
|
|
else if(GH.statusbar) //for other buttons
|
2009-01-29 20:07:31 +02:00
|
|
|
{
|
|
|
|
if (on)
|
2010-02-15 08:54:57 +02:00
|
|
|
GH.statusbar->print(*name);
|
|
|
|
else if ( GH.statusbar->getCurrent()==(*name) )
|
|
|
|
GH.statusbar->clear();
|
2009-01-29 20:07:31 +02:00
|
|
|
}
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-19 16:17:32 +03:00
|
|
|
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)
|
2008-08-15 15:11:42 +03:00
|
|
|
{
|
2009-09-11 08:45:40 +03:00
|
|
|
used = LCLICK | RCLICK | HOVER | KEYBOARD;
|
2008-08-15 15:11:42 +03:00
|
|
|
callback = Callback;
|
2008-08-27 13:19:18 +03:00
|
|
|
blocked = actOnDown = false;
|
2008-08-15 15:11:42 +03:00
|
|
|
type=2;
|
|
|
|
abs=true;
|
2009-08-17 11:50:31 +03:00
|
|
|
hoverable = false;
|
|
|
|
//active=false;
|
2008-08-15 15:11:42 +03:00
|
|
|
ourObj=NULL;
|
2008-11-02 00:32:56 +02:00
|
|
|
assignedKeys.insert(key);
|
2008-08-15 15:11:42 +03:00
|
|
|
state=0;
|
2008-08-28 20:36:34 +03:00
|
|
|
hoverTexts = Name;
|
2008-08-15 15:11:42 +03:00
|
|
|
helpBox=HelpBox;
|
2009-08-17 11:50:31 +03:00
|
|
|
|
|
|
|
setDef(defName, playerColoredButton);
|
|
|
|
|
2008-11-12 20:26:23 +02:00
|
|
|
if (add && add->size())
|
2008-12-21 21:17:35 +02:00
|
|
|
for (size_t i=0; i<add->size();i++)
|
2010-10-18 18:08:59 +03:00
|
|
|
setDef((*add)[i], playerColoredButton);
|
|
|
|
if (playerColoredButton)
|
|
|
|
setPlayerColor(LOCPLINT->playerID);
|
|
|
|
|
2009-08-17 11:50:31 +03:00
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
2010-10-18 18:08:59 +03:00
|
|
|
pos.w = imgs[curimg]->image(0)->w;
|
|
|
|
pos.h = imgs[curimg]->image(0)->h -1;
|
2008-08-15 15:11:42 +03:00
|
|
|
}
|
2008-05-18 20:33:39 +03:00
|
|
|
|
2009-02-09 18:18:48 +02:00
|
|
|
void AdventureMapButton::block( ui8 on )
|
2008-08-27 13:19:18 +03:00
|
|
|
{
|
|
|
|
blocked = on;
|
|
|
|
state = 0;
|
|
|
|
bitmapOffset = on ? 2 : 0;
|
2009-05-25 02:21:55 +03:00
|
|
|
show(screenBuf);
|
2008-08-27 13:19:18 +03:00
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
2010-07-22 03:32:45 +03:00
|
|
|
void AdventureMapButton::setDef(const std::string & defName, bool playerColoredButton, bool reset/*=false*/)
|
2009-08-17 11:50:31 +03:00
|
|
|
{
|
2010-07-22 03:32:45 +03:00
|
|
|
if (reset)
|
|
|
|
{
|
2010-10-18 18:08:59 +03:00
|
|
|
for (size_t i=0; i<imgs.size(); i++)
|
|
|
|
delete imgs[i];
|
|
|
|
imgs.clear();
|
2010-07-22 03:32:45 +03:00
|
|
|
}
|
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
imgs.push_back(new CAnimation(defName));
|
|
|
|
imgs.back()->load();
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
2010-02-20 15:24:38 +02:00
|
|
|
|
|
|
|
void AdventureMapButton::setPlayerColor(int player)
|
|
|
|
{
|
2010-10-18 18:08:59 +03:00
|
|
|
for(size_t i =0; i<imgs.size();i++)
|
|
|
|
for(size_t j=0;j<imgs[i]->size();j++)
|
|
|
|
{
|
|
|
|
graphics->blueToPlayersAdv(imgs[i]->image(j),player);
|
|
|
|
}
|
2010-02-20 15:24:38 +02:00
|
|
|
}
|
|
|
|
|
2008-09-18 16:54:54 +03:00
|
|
|
void CHighlightableButton::select(bool on)
|
|
|
|
{
|
|
|
|
selected = on;
|
|
|
|
state = selected ? 3 : 0;
|
2009-08-27 11:04:32 +03:00
|
|
|
|
2008-09-18 16:54:54 +03:00
|
|
|
if(selected)
|
|
|
|
callback();
|
|
|
|
else
|
|
|
|
callback2();
|
|
|
|
if(hoverTexts.size()>1)
|
|
|
|
{
|
|
|
|
hover(false);
|
|
|
|
hover(true);
|
|
|
|
}
|
|
|
|
}
|
2008-08-27 13:19:18 +03:00
|
|
|
|
2009-08-11 19:05:33 +03:00
|
|
|
void CHighlightableButton::clickLeft(tribool down, bool previousState)
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
|
|
|
if(blocked)
|
|
|
|
return;
|
2010-03-06 00:51:09 +02:00
|
|
|
if (down)
|
|
|
|
{
|
2010-12-19 16:39:56 +02:00
|
|
|
CCS->soundh->playSound(soundBase::button);
|
2009-08-27 11:04:32 +03:00
|
|
|
state = 1;
|
2010-03-06 00:51:09 +02:00
|
|
|
}
|
|
|
|
else
|
2008-08-28 20:36:34 +03:00
|
|
|
state = selected ? 3 : 0;
|
2010-03-06 00:51:09 +02:00
|
|
|
|
2009-05-25 02:21:55 +03:00
|
|
|
show(screenBuf);
|
2009-08-11 19:05:33 +03:00
|
|
|
if(previousState && down == false)
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
2008-09-19 11:16:19 +03:00
|
|
|
if(!onlyOn || !selected)
|
|
|
|
select(!selected);
|
2008-08-28 20:36:34 +03:00
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
}
|
2008-08-28 20:36:34 +03:00
|
|
|
|
2009-05-06 13:14:48 +03:00
|
|
|
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)
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
2008-10-19 16:17:32 +03:00
|
|
|
init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
|
2008-08-28 20:36:34 +03:00
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
2009-08-17 11:50:31 +03:00
|
|
|
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 */ )
|
2009-08-21 03:59:12 +03:00
|
|
|
: onlyOn(false), selected(false) // TODO: callback2(???)
|
2009-08-17 11:50:31 +03:00
|
|
|
{
|
|
|
|
ID = myid;
|
|
|
|
std::map<int,std::string> pom;
|
|
|
|
pom[0] = help.first;
|
|
|
|
init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
|
|
|
|
}
|
|
|
|
|
2009-08-27 11:04:32 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-09-24 11:15:49 +03:00
|
|
|
void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
|
|
|
|
{
|
|
|
|
bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
|
2010-03-06 00:51:09 +02:00
|
|
|
bt->onlyOn = true;
|
2008-09-24 11:15:49 +03:00
|
|
|
buttons.push_back(bt);
|
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
2008-10-19 16:17:32 +03:00
|
|
|
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)
|
2008-09-24 11:15:49 +03:00
|
|
|
{
|
2009-05-06 13:14:48 +03:00
|
|
|
CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
|
|
|
|
if(musicLike)
|
|
|
|
{
|
|
|
|
bt->bitmapOffset = buttons.size() - 3;
|
|
|
|
}
|
2008-09-24 11:15:49 +03:00
|
|
|
bt->ID = uid;
|
|
|
|
bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
|
|
|
|
bt->onlyOn = true;
|
|
|
|
buttons.push_back(bt);
|
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
|
|
|
CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
|
2010-10-18 18:08:59 +03:00
|
|
|
: onChange(OnChange), musicLike(musicLikeButtons)
|
2009-05-06 13:14:48 +03:00
|
|
|
{}
|
|
|
|
|
2008-09-24 11:15:49 +03:00
|
|
|
CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
|
|
|
|
{
|
2009-05-06 13:14:48 +03:00
|
|
|
for(size_t i=0;i<buttons.size();i++)
|
|
|
|
{
|
|
|
|
delete buttons[i];
|
|
|
|
}
|
2008-09-24 11:15:49 +03:00
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
2008-09-24 11:15:49 +03:00
|
|
|
void CHighlightableButtonsGroup::activate()
|
|
|
|
{
|
2009-05-06 13:14:48 +03:00
|
|
|
for(size_t i=0;i<buttons.size();i++)
|
|
|
|
{
|
2008-09-24 11:15:49 +03:00
|
|
|
buttons[i]->activate();
|
2009-05-06 13:14:48 +03:00
|
|
|
}
|
2008-09-24 11:15:49 +03:00
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
2008-09-24 11:15:49 +03:00
|
|
|
void CHighlightableButtonsGroup::deactivate()
|
|
|
|
{
|
2009-05-06 13:14:48 +03:00
|
|
|
for(size_t i=0;i<buttons.size();i++)
|
|
|
|
{
|
2008-09-24 11:15:49 +03:00
|
|
|
buttons[i]->deactivate();
|
2009-05-06 13:14:48 +03:00
|
|
|
}
|
2008-09-24 11:15:49 +03:00
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
2008-09-24 11:15:49 +03:00
|
|
|
void CHighlightableButtonsGroup::select(int id, bool mode)
|
|
|
|
{
|
|
|
|
CHighlightableButton *bt = NULL;
|
|
|
|
if(mode)
|
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t i=0;i<buttons.size() && !bt; ++i)
|
2008-09-24 11:15:49 +03:00
|
|
|
if (buttons[i]->ID == id)
|
|
|
|
bt = buttons[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bt = buttons[id];
|
|
|
|
}
|
|
|
|
bt->select(true);
|
|
|
|
selectionChanged(bt->ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHighlightableButtonsGroup::selectionChanged(int to)
|
|
|
|
{
|
2008-12-21 21:17:35 +02:00
|
|
|
for(size_t i=0;i<buttons.size(); ++i)
|
2008-09-24 11:15:49 +03:00
|
|
|
if(buttons[i]->ID!=to && buttons[i]->selected)
|
|
|
|
buttons[i]->select(false);
|
|
|
|
onChange(to);
|
|
|
|
}
|
2009-05-06 13:14:48 +03:00
|
|
|
|
2008-09-24 11:15:49 +03:00
|
|
|
void CHighlightableButtonsGroup::show(SDL_Surface * to )
|
|
|
|
{
|
2009-04-14 15:47:09 +03:00
|
|
|
for(size_t i=0;i<buttons.size(); ++i)
|
|
|
|
{
|
2009-05-06 13:21:05 +03:00
|
|
|
if(!musicLike || (musicLike && buttons[i]->selected)) //if musicLike, print only selected button
|
2009-05-06 13:14:48 +03:00
|
|
|
buttons[i]->show(to);
|
2009-04-14 15:47:09 +03:00
|
|
|
}
|
2008-09-24 11:15:49 +03:00
|
|
|
}
|
|
|
|
|
2009-08-17 11:50:31 +03:00
|
|
|
void CHighlightableButtonsGroup::showAll( SDL_Surface * to )
|
|
|
|
{
|
|
|
|
show(to);
|
|
|
|
}
|
|
|
|
|
2009-08-27 11:04:32 +03:00
|
|
|
void CHighlightableButtonsGroup::block( ui8 on )
|
|
|
|
{
|
|
|
|
for(size_t i=0;i<buttons.size(); ++i)
|
|
|
|
{
|
|
|
|
buttons[i]->block(on);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 20:33:39 +03:00
|
|
|
void CSlider::sliderClicked()
|
|
|
|
{
|
2009-09-13 01:17:23 +03:00
|
|
|
if(!(active & MOVE))
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2009-08-11 19:05:33 +03:00
|
|
|
activateMouseMove();
|
2010-02-10 05:21:23 +02:00
|
|
|
used |= MOVE;
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-25 17:09:31 +03:00
|
|
|
void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2009-08-17 11:50:31 +03:00
|
|
|
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);
|
|
|
|
}
|
2009-03-27 01:05:40 +02:00
|
|
|
v += 0.5f;
|
2008-05-18 20:33:39 +03:00
|
|
|
if(v!=value)
|
|
|
|
{
|
|
|
|
moveTo(v);
|
|
|
|
redrawSlider();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::redrawSlider()
|
|
|
|
{
|
2010-05-08 01:10:32 +03:00
|
|
|
//slider->show(screenBuf);
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::moveLeft()
|
|
|
|
{
|
|
|
|
moveTo(value-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::moveRight()
|
|
|
|
{
|
|
|
|
moveTo(value+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::moveTo(int to)
|
|
|
|
{
|
2009-08-17 11:50:31 +03:00
|
|
|
amax(to, 0);
|
|
|
|
amin(to, positions);
|
|
|
|
|
|
|
|
//same, old position?
|
|
|
|
if(value == to)
|
|
|
|
return;
|
|
|
|
|
2008-05-18 20:33:39 +03:00
|
|
|
value = to;
|
2009-08-17 11:50:31 +03:00
|
|
|
if(horizontal)
|
2008-05-31 23:37:54 +03:00
|
|
|
{
|
2009-08-17 11:50:31 +03:00
|
|
|
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;
|
2008-05-31 23:37:54 +03:00
|
|
|
}
|
|
|
|
else
|
2009-08-17 11:50:31 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2010-06-27 19:03:01 +03:00
|
|
|
|
|
|
|
if(moved)
|
|
|
|
moved(to);
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
|
2009-08-11 19:05:33 +03:00
|
|
|
void CSlider::clickLeft(tribool down, bool previousState)
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2010-07-20 09:05:45 +03:00
|
|
|
if(down && !slider->blocked)
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2009-08-17 11:50:31 +03:00
|
|
|
float pw = 0;
|
|
|
|
float rw = 0;
|
|
|
|
if(horizontal)
|
|
|
|
{
|
2009-09-13 01:17:23 +03:00
|
|
|
pw = GH.current->motion.x-pos.x-25;
|
2009-08-17 11:50:31 +03:00
|
|
|
rw = pw / ((float)(pos.w-48));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-13 01:17:23 +03:00
|
|
|
pw = GH.current->motion.y-pos.y-24;
|
2009-08-17 11:50:31 +03:00
|
|
|
rw = pw / ((float)(pos.h-48));
|
|
|
|
}
|
2009-09-13 01:17:23 +03:00
|
|
|
if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
|
2009-08-17 11:50:31 +03:00
|
|
|
return;
|
|
|
|
// if (rw>1) return;
|
|
|
|
// if (rw<0) return;
|
2009-09-13 01:17:23 +03:00
|
|
|
slider->clickLeft(true, slider->pressedL);
|
2009-08-17 11:50:31 +03:00
|
|
|
moveTo(rw * positions + 0.5f);
|
2008-05-18 20:33:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2009-09-13 01:17:23 +03:00
|
|
|
if(active & MOVE)
|
2010-02-10 05:21:23 +02:00
|
|
|
{
|
2009-08-11 19:05:33 +03:00
|
|
|
deactivateMouseMove();
|
2010-02-10 05:21:23 +02:00
|
|
|
used &= ~MOVE;
|
|
|
|
}
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CSlider::~CSlider()
|
|
|
|
{
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2008-05-18 20:33:39 +03:00
|
|
|
}
|
|
|
|
|
2009-08-17 11:50:31 +03:00
|
|
|
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)
|
2008-05-18 20:33:39 +03:00
|
|
|
{
|
2009-09-09 09:04:42 +03:00
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
2009-08-17 11:50:31 +03:00
|
|
|
setAmount(amount);
|
|
|
|
|
|
|
|
used = LCLICK;
|
2008-05-18 20:33:39 +03:00
|
|
|
strongInterest = true;
|
2009-08-17 11:50:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
left = new AdventureMapButton;
|
|
|
|
right = new AdventureMapButton;
|
|
|
|
slider = new AdventureMapButton;
|
|
|
|
|
2009-08-27 11:04:32 +03:00
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
|
2009-08-17 11:50:31 +03:00
|
|
|
if(horizontal)
|
|
|
|
{
|
2009-08-27 11:04:32 +03:00
|
|
|
left->pos.y = slider->pos.y = right->pos.y = pos.y;
|
|
|
|
left->pos.x = pos.x;
|
|
|
|
right->pos.x = pos.x + totalw - 16;
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-27 11:04:32 +03:00
|
|
|
left->pos.x = slider->pos.x = right->pos.x = pos.x;
|
|
|
|
left->pos.y = pos.y;
|
|
|
|
right->pos.y = pos.y + totalw - 16;
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2009-12-29 19:13:40 +02:00
|
|
|
{
|
2010-10-18 18:08:59 +03:00
|
|
|
CAnimation * pics = new CAnimation(horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF");
|
|
|
|
pics->load();
|
|
|
|
|
|
|
|
left->imgs.push_back(new CAnimation());
|
|
|
|
right->imgs.push_back(new CAnimation());
|
|
|
|
slider->imgs.push_back(new CAnimation());
|
|
|
|
|
|
|
|
left->imgs.back()->add(pics->image(0), true);
|
|
|
|
left->imgs.back()->add(pics->image(1), true);
|
|
|
|
right->imgs.back()->add(pics->image(2), true);
|
|
|
|
right->imgs.back()->add(pics->image(3), true);
|
|
|
|
slider->imgs.back()->add(pics->image(4), true);
|
|
|
|
|
|
|
|
delete pics;
|
2009-08-17 11:50:31 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
2008-08-02 18:08:03 +03:00
|
|
|
}
|
2008-09-07 06:38:37 +03:00
|
|
|
|
|
|
|
void CSlider::block( bool on )
|
|
|
|
{
|
2009-08-17 11:50:31 +03:00
|
|
|
left->block(on);
|
|
|
|
right->block(on);
|
|
|
|
slider->block(on);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::setAmount( int to )
|
|
|
|
{
|
2009-08-27 11:04:32 +03:00
|
|
|
amount = to;
|
2009-08-17 11:50:31 +03:00
|
|
|
positions = to - capacity;
|
2009-09-07 05:29:44 +03:00
|
|
|
amax(positions, 0);
|
2009-08-21 03:59:12 +03:00
|
|
|
}
|
2010-07-06 05:10:26 +03:00
|
|
|
|
|
|
|
void CSlider::showAll(SDL_Surface * to)
|
|
|
|
{
|
2010-08-23 19:13:30 +03:00
|
|
|
CSDL_Ext::fillRect(to, &pos, 0);
|
2010-07-06 05:10:26 +03:00
|
|
|
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);
|
2010-10-24 14:35:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::moveToMax()
|
|
|
|
{
|
|
|
|
moveTo(amount);
|
2010-07-06 05:10:26 +03:00
|
|
|
}
|