2014-07-15 10:14:49 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Buttons.h"
|
|
|
|
|
|
|
|
#include "Images.h"
|
|
|
|
#include "TextControls.h"
|
|
|
|
|
|
|
|
#include "../CMusicHandler.h"
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../battle/CBattleInterface.h"
|
|
|
|
#include "../battle/CBattleInterfaceClasses.h"
|
|
|
|
#include "../gui/CAnimation.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Buttons.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
ClickableArea::ClickableArea(CIntObject * object, CFunctionList<void()> callback):
|
|
|
|
callback(callback),
|
|
|
|
area(nullptr)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
if (object)
|
|
|
|
pos = object->pos;
|
|
|
|
setArea(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClickableArea::addCallback(std::function<void()> callback)
|
|
|
|
{
|
|
|
|
this->callback += callback;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void ClickableArea::setArea(CIntObject * object)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
delete area;
|
|
|
|
addChild(area);
|
|
|
|
pos.w = object->pos.w;
|
|
|
|
pos.h = object->pos.h;
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void ClickableArea::onClick()
|
|
|
|
{
|
|
|
|
callback();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void ClickableArea::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if (down)
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::update()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if (overlay)
|
|
|
|
{
|
|
|
|
if (state == PRESSED)
|
|
|
|
overlay->moveTo(overlay->pos.centerIn(pos).topLeft() + Point(1,1));
|
|
|
|
else
|
|
|
|
overlay->moveTo(overlay->pos.centerIn(pos).topLeft());
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
int newPos = stateToIndex[int(state)];
|
2014-07-15 10:14:49 +03:00
|
|
|
if (newPos < 0)
|
|
|
|
newPos = 0;
|
|
|
|
|
|
|
|
if (state == HIGHLIGHTED && image->size() < 4)
|
|
|
|
newPos = image->size()-1;
|
2014-08-03 14:16:19 +03:00
|
|
|
image->setFrame(newPos);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
if (active)
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::addCallback(std::function<void()> callback)
|
|
|
|
{
|
|
|
|
this->callback += callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::addTextOverlay( const std::string &Text, EFonts font, SDL_Color color)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
|
|
|
addOverlay(new CLabel(pos.w/2, pos.h/2, font, CENTER, color, Text));
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::addOverlay(CIntObject *newOverlay)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
delete overlay;
|
|
|
|
overlay = newOverlay;
|
|
|
|
addChild(newOverlay);
|
|
|
|
overlay->moveTo(overlay->pos.centerIn(pos).topLeft());
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::addImage(std::string filename)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
imageNames.push_back(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::addHoverText(ButtonState state, std::string text)
|
|
|
|
{
|
|
|
|
hoverTexts[state] = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::setImageOrder(int state1, int state2, int state3, int state4)
|
|
|
|
{
|
|
|
|
stateToIndex[0] = state1;
|
|
|
|
stateToIndex[1] = state2;
|
|
|
|
stateToIndex[2] = state3;
|
|
|
|
stateToIndex[3] = state4;
|
2014-07-15 10:14:49 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::setState(ButtonState newState)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if (state == newState)
|
|
|
|
return;
|
|
|
|
state = newState;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
CButton::ButtonState CButton::getState()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
bool CButton::isBlocked()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
return state == BLOCKED;
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
bool CButton::isHighlighted()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
return state == HIGHLIGHTED;
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::block(bool on)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2015-03-16 14:55:36 +02:00
|
|
|
if(on || state == BLOCKED) //dont change button state if unblock requested, but it's not blocked
|
|
|
|
setState(on ? BLOCKED : NORMAL);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::onButtonClicked()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
// debug logging to figure out pressed button (and as result - player actions) in case of crash
|
2014-11-12 22:45:48 +02:00
|
|
|
logAnim->traceStream() << "Button clicked at " << pos.x << "x" << pos.y;
|
2014-07-15 10:14:49 +03:00
|
|
|
CIntObject * parent = this->parent;
|
|
|
|
std::string prefix = "Parent is";
|
|
|
|
while (parent)
|
|
|
|
{
|
|
|
|
logAnim->traceStream() << prefix << typeid(*parent).name() << " at " << parent->pos.x << "x" << parent->pos.y;
|
|
|
|
parent = parent->parent;
|
|
|
|
prefix = '\t' + prefix;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::clickLeft(tribool down, bool previousState)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if(isBlocked())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (down)
|
|
|
|
{
|
|
|
|
if (!soundDisabled)
|
|
|
|
CCS->soundh->playSound(soundBase::button);
|
|
|
|
setState(PRESSED);
|
|
|
|
}
|
|
|
|
else if(hoverable && hovered)
|
|
|
|
setState(HIGHLIGHTED);
|
|
|
|
else
|
|
|
|
setState(NORMAL);
|
|
|
|
|
|
|
|
if (actOnDown && down)
|
|
|
|
{
|
|
|
|
onButtonClicked();
|
|
|
|
}
|
|
|
|
else if (!actOnDown && previousState && (down==false))
|
|
|
|
{
|
|
|
|
onButtonClicked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::clickRight(tribool down, bool previousState)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if(down && helpBox.size()) //there is no point to show window with nothing inside...
|
|
|
|
CRClickPopup::createAndPush(helpBox);
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::hover (bool on)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 18:50:59 +03:00
|
|
|
if(hoverable && !isBlocked())
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if(on)
|
|
|
|
setState(HIGHLIGHTED);
|
|
|
|
else
|
|
|
|
setState(NORMAL);
|
|
|
|
}
|
|
|
|
|
2014-08-03 18:50:59 +03:00
|
|
|
/*if(pressedL && on) // WTF is this? When this is used?
|
|
|
|
setState(PRESSED);*/
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
std::string name = hoverTexts[getState()].empty()
|
2015-08-22 04:32:24 +02:00
|
|
|
? hoverTexts[0]
|
|
|
|
: hoverTexts[getState()];
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2015-08-22 04:39:56 +02:00
|
|
|
if(!name.empty() && !isBlocked()) //if there is no name, there is nothing to display also
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
|
|
|
|
{
|
|
|
|
if(on && LOCPLINT->battleInt->console->alterTxt == "")
|
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
LOCPLINT->battleInt->console->alterTxt = name;
|
2014-07-15 10:14:49 +03:00
|
|
|
LOCPLINT->battleInt->console->whoSetAlter = 1;
|
|
|
|
}
|
2014-08-03 14:16:19 +03:00
|
|
|
else if (LOCPLINT->battleInt->console->alterTxt == name)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
LOCPLINT->battleInt->console->alterTxt = "";
|
|
|
|
LOCPLINT->battleInt->console->whoSetAlter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(GH.statusbar) //for other buttons
|
|
|
|
{
|
|
|
|
if (on)
|
2014-08-03 14:16:19 +03:00
|
|
|
GH.statusbar->setText(name);
|
|
|
|
else if ( GH.statusbar->getText()==(name) )
|
2014-07-15 10:14:49 +03:00
|
|
|
GH.statusbar->clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
CButton::CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, int key, bool playerColoredButton):
|
|
|
|
CKeyShortcut(key),
|
|
|
|
callback(Callback)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
stateToIndex[0] = 0;
|
|
|
|
stateToIndex[1] = 1;
|
|
|
|
stateToIndex[2] = 2;
|
|
|
|
stateToIndex[3] = 3;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
state=NORMAL;
|
|
|
|
image = nullptr;
|
|
|
|
overlay = nullptr;
|
|
|
|
|
|
|
|
currentImage = -1;
|
|
|
|
hoverable = actOnDown = soundDisabled = false;
|
|
|
|
hoverTexts[0] = help.first;
|
|
|
|
helpBox=help.second;
|
|
|
|
|
|
|
|
pos.x += position.x;
|
|
|
|
pos.y += position.y;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
if (!defName.empty())
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
2014-07-15 10:14:49 +03:00
|
|
|
imageNames.push_back(defName);
|
2014-08-03 14:16:19 +03:00
|
|
|
setIndex(0, playerColoredButton);
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::setIndex(size_t index, bool playerColoredButton)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if (index == currentImage || index>=imageNames.size())
|
|
|
|
return;
|
|
|
|
currentImage = index;
|
|
|
|
setImage(new CAnimation(imageNames[index]), playerColoredButton);
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::setImage(CAnimation* anim, bool playerColoredButton, int animFlags)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
|
|
|
|
|
|
|
image = new CAnimImage(anim, getState(), 0, 0, 0, animFlags);
|
|
|
|
if (playerColoredButton)
|
|
|
|
image->playerColored(LOCPLINT->playerID);
|
2014-08-03 14:16:19 +03:00
|
|
|
pos = image->pos;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::setPlayerColor(PlayerColor player)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if (image)
|
|
|
|
image->playerColored(player);
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::showAll(SDL_Surface * to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
CIntObject::showAll(to);
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
if (borderColor && borderColor->a == 0)
|
|
|
|
CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, int3(borderColor->r, borderColor->g, borderColor->b));
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
std::pair<std::string, std::string> CButton::tooltip()
|
|
|
|
{
|
|
|
|
return std::pair<std::string, std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, std::string> CButton::tooltip(const JsonNode & localizedTexts)
|
|
|
|
{
|
|
|
|
return std::make_pair(localizedTexts["label"].String(), localizedTexts["help"].String());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, std::string> CButton::tooltip(const std::string & hover, const std::string & help)
|
|
|
|
{
|
|
|
|
return std::make_pair(hover, help);
|
|
|
|
}
|
|
|
|
|
|
|
|
CToggleBase::CToggleBase(CFunctionList<void (bool)> callback):
|
2014-08-03 16:19:16 +03:00
|
|
|
callback(callback),
|
|
|
|
selected(false),
|
|
|
|
allowDeselection(true)
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CToggleBase::~CToggleBase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CToggleBase::doSelect(bool on)
|
|
|
|
{
|
|
|
|
// for overrides
|
|
|
|
}
|
|
|
|
|
|
|
|
void CToggleBase::setSelected(bool on)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 16:19:16 +03:00
|
|
|
bool changed = (on != selected);
|
2014-07-15 10:14:49 +03:00
|
|
|
selected = on;
|
2014-08-03 14:16:19 +03:00
|
|
|
doSelect(on);
|
2014-08-03 16:19:16 +03:00
|
|
|
if (changed)
|
|
|
|
callback(on);
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CToggleBase::canActivate()
|
|
|
|
{
|
|
|
|
if (selected && !allowDeselection)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CToggleBase::addCallback(std::function<void(bool)> function)
|
|
|
|
{
|
2014-11-12 22:45:48 +02:00
|
|
|
callback += function;
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CToggleButton::CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
|
|
|
|
CFunctionList<void(bool)> callback, int key, bool playerColoredButton):
|
|
|
|
CButton(position, defName, help, 0, key, playerColoredButton),
|
|
|
|
CToggleBase(callback)
|
|
|
|
{
|
2014-08-03 16:19:16 +03:00
|
|
|
allowDeselection = true;
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CToggleButton::doSelect(bool on)
|
|
|
|
{
|
2014-07-15 10:14:49 +03:00
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
setState(HIGHLIGHTED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setState(NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CToggleButton::clickLeft(tribool down, bool previousState)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
// force refresh
|
|
|
|
hover(false);
|
|
|
|
hover(true);
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
if(isBlocked())
|
|
|
|
return;
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
if (down && canActivate())
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
CCS->soundh->playSound(soundBase::button);
|
|
|
|
setState(PRESSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(previousState)//mouse up
|
|
|
|
{
|
2014-08-03 16:19:16 +03:00
|
|
|
if(down == false && getState() == PRESSED && canActivate())
|
2014-08-03 17:31:56 +03:00
|
|
|
{
|
|
|
|
onButtonClicked();
|
2014-08-03 14:16:19 +03:00
|
|
|
setSelected(!selected);
|
2014-08-03 17:31:56 +03:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
else
|
2014-08-03 16:19:16 +03:00
|
|
|
doSelect(selected); // restore
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CToggleGroup::addCallback(std::function<void(int)> callback)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
onChange += callback;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CToggleGroup::addToggle(int identifier, CToggleBase* bt)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
if (auto intObj = dynamic_cast<CIntObject*>(bt)) // hack-ish workagound to avoid diamond problem with inheritance
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
if (intObj->parent)
|
|
|
|
intObj->parent->removeChild(intObj);
|
|
|
|
addChild(intObj);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2014-08-03 16:19:16 +03:00
|
|
|
bt->addCallback([=] (bool on) { if (on) selectionChanged(identifier);});
|
2014-08-03 14:16:19 +03:00
|
|
|
bt->allowDeselection = false;
|
|
|
|
|
2014-08-03 16:19:16 +03:00
|
|
|
assert(buttons[identifier] == nullptr);
|
2014-08-03 14:16:19 +03:00
|
|
|
buttons[identifier] = bt;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2015-08-22 05:07:51 +02:00
|
|
|
CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange)
|
|
|
|
: onChange(OnChange), selectedID(-2)
|
2014-07-15 10:14:49 +03:00
|
|
|
{}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CToggleGroup::setSelected(int id)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 16:19:16 +03:00
|
|
|
selectionChanged(id);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CToggleGroup::selectionChanged(int to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 16:19:16 +03:00
|
|
|
if (to == selectedID)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int oldSelection = selectedID;
|
|
|
|
selectedID = to;
|
|
|
|
|
|
|
|
if (buttons.count(oldSelection))
|
|
|
|
buttons[oldSelection]->setSelected(false);
|
2014-08-03 14:16:19 +03:00
|
|
|
|
|
|
|
if (buttons.count(to))
|
|
|
|
buttons[to]->setSelected(true);
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
onChange(to);
|
|
|
|
if (parent)
|
|
|
|
parent->redraw();
|
|
|
|
}
|
|
|
|
|
2015-08-22 04:45:31 +02:00
|
|
|
CVolumeSlider::CVolumeSlider(const Point &position, const std::string &defName, const int value,
|
|
|
|
const std::pair<std::string, std::string> * const help) :
|
|
|
|
value(value),
|
|
|
|
helpHandlers(help)
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
|
|
|
animImage = new CAnimImage(new CAnimation(defName), 0, 0, position.x, position.y),
|
|
|
|
assert(!defName.empty());
|
|
|
|
addUsedEvents(LCLICK | RCLICK | WHEEL);
|
|
|
|
pos.x += position.x;
|
|
|
|
pos.y += position.y;
|
|
|
|
pos.w = (animImage->pos.w + 1) * animImage->size();
|
|
|
|
pos.h = animImage->pos.h;
|
|
|
|
type |= REDRAW_PARENT;
|
|
|
|
setVolume(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeSlider::setVolume(int value_)
|
|
|
|
{
|
|
|
|
value = value_;
|
|
|
|
moveTo(value * static_cast<double>(animImage->size()) / 100.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeSlider::moveTo(int id)
|
|
|
|
{
|
|
|
|
vstd::abetween(id, 0, animImage->size() - 1);
|
|
|
|
animImage->setFrame(id);
|
|
|
|
animImage->moveTo(Point(pos.x + (animImage->pos.w + 1) * id, pos.y));
|
|
|
|
if (active)
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeSlider::addCallback(std::function<void(int)> callback)
|
|
|
|
{
|
|
|
|
onChange += callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeSlider::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if (down)
|
|
|
|
{
|
|
|
|
double px = GH.current->motion.x - pos.x;
|
|
|
|
double rx = px / static_cast<double>(pos.w);
|
|
|
|
// setVolume is out of 100
|
|
|
|
setVolume(rx * 100);
|
|
|
|
// Volume config is out of 100, set to increments of 5ish roughly based on the half point of the indicator
|
|
|
|
// 0.0 -> 0, 0.05 -> 5, 0.09 -> 5,...,
|
|
|
|
// 0.1 -> 10, ..., 0.19 -> 15, 0.2 -> 20, ...,
|
|
|
|
// 0.28 -> 25, 0.29 -> 30, 0.3 -> 30, ...,
|
|
|
|
// 0.85 -> 85, 0.86 -> 90, ..., 0.87 -> 90,...,
|
|
|
|
// 0.95 -> 95, 0.96 -> 100, 0.99 -> 100
|
|
|
|
int volume = 5 * int(rx * (2 * animImage->size() + 1));
|
|
|
|
onChange(volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeSlider::clickRight(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if (down)
|
|
|
|
{
|
|
|
|
double px = GH.current->motion.x - pos.x;
|
|
|
|
int index = px / static_cast<double>(pos.w) * animImage->size();
|
|
|
|
std::string hoverText = helpHandlers[index].first;
|
|
|
|
std::string helpBox = helpHandlers[index].second;
|
|
|
|
if(!helpBox.empty())
|
|
|
|
CRClickPopup::createAndPush(helpBox);
|
|
|
|
if(GH.statusbar)
|
|
|
|
GH.statusbar->setText(helpBox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVolumeSlider::wheelScrolled(bool down, bool in)
|
|
|
|
{
|
|
|
|
if (in)
|
|
|
|
{
|
|
|
|
int volume = value + 3 * (down ? 1 : -1);
|
|
|
|
vstd::abetween(volume, 0, 100);
|
|
|
|
setVolume(volume);
|
|
|
|
onChange(volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
void CSlider::sliderClicked()
|
|
|
|
{
|
|
|
|
if(!(active & MOVE))
|
|
|
|
addUsedEvents(MOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
|
|
|
|
{
|
|
|
|
double 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.5;
|
|
|
|
if(v!=value)
|
|
|
|
{
|
|
|
|
moveTo(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
void CSlider::setScrollStep(int to)
|
|
|
|
{
|
|
|
|
scrollStep = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSlider::getAmount()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 17:31:56 +03:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSlider::getValue()
|
|
|
|
{
|
|
|
|
return value;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::moveLeft()
|
|
|
|
{
|
|
|
|
moveTo(value-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::moveRight()
|
|
|
|
{
|
|
|
|
moveTo(value+1);
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
void CSlider::moveBy(int amount)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 17:31:56 +03:00
|
|
|
moveTo(value + amount);
|
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
void CSlider::updateSliderPos()
|
|
|
|
{
|
2014-07-15 10:14:49 +03:00
|
|
|
if(horizontal)
|
|
|
|
{
|
|
|
|
if(positions)
|
|
|
|
{
|
2014-08-03 17:31:56 +03:00
|
|
|
double part = static_cast<double>(value) / positions;
|
2014-07-15 10:14:49 +03:00
|
|
|
part*=(pos.w-48);
|
|
|
|
int newPos = part + pos.x + 16 - slider->pos.x;
|
|
|
|
slider->moveBy(Point(newPos, 0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
slider->moveTo(Point(pos.x+16, pos.y));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(positions)
|
|
|
|
{
|
2014-08-03 17:31:56 +03:00
|
|
|
double part = static_cast<double>(value) / positions;
|
2014-07-15 10:14:49 +03:00
|
|
|
part*=(pos.h-48);
|
|
|
|
int newPos = part + pos.y + 16 - slider->pos.y;
|
|
|
|
slider->moveBy(Point(0, newPos));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
slider->moveTo(Point(pos.x, pos.y+16));
|
|
|
|
}
|
2014-08-03 17:31:56 +03:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
void CSlider::moveTo(int to)
|
|
|
|
{
|
|
|
|
vstd::amax(to, 0);
|
|
|
|
vstd::amin(to, positions);
|
|
|
|
|
|
|
|
//same, old position?
|
|
|
|
if(value == to)
|
|
|
|
return;
|
|
|
|
value = to;
|
|
|
|
|
|
|
|
updateSliderPos();
|
|
|
|
|
|
|
|
moved(to);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::clickLeft(tribool down, bool previousState)
|
|
|
|
{
|
|
|
|
if(down && !slider->isBlocked())
|
|
|
|
{
|
|
|
|
double pw = 0;
|
|
|
|
double rw = 0;
|
|
|
|
if(horizontal)
|
|
|
|
{
|
|
|
|
pw = GH.current->motion.x-pos.x-25;
|
|
|
|
rw = pw / static_cast<double>(pos.w - 48);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pw = GH.current->motion.y-pos.y-24;
|
|
|
|
rw = pw / (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.5);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(active & MOVE)
|
|
|
|
removeUsedEvents(MOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSlider::~CSlider()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
CSlider::CSlider(Point position, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, CSlider::EStyle style):
|
2014-07-15 10:14:49 +03:00
|
|
|
capacity(Capacity),
|
2014-08-03 17:31:56 +03:00
|
|
|
horizontal(Horizontal),
|
2014-07-15 10:14:49 +03:00
|
|
|
amount(Amount),
|
2014-08-03 17:31:56 +03:00
|
|
|
value(Value),
|
2014-07-15 10:14:49 +03:00
|
|
|
scrollStep(1),
|
|
|
|
moved(Moved)
|
|
|
|
{
|
|
|
|
OBJ_CONSTRUCTION_CAPTURING_ALL;
|
|
|
|
setAmount(amount);
|
2014-08-11 19:16:39 +03:00
|
|
|
vstd::amax(value, 0);
|
|
|
|
vstd::amin(value, positions);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
addUsedEvents(LCLICK | KEYBOARD | WHEEL);
|
|
|
|
strongInterest = true;
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
pos.x += position.x;
|
|
|
|
pos.y += position.y;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
if(style == BROWN)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
|
|
|
|
//NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
left = new CButton(Point(), name, CButton::tooltip());
|
|
|
|
right = new CButton(Point(), name, CButton::tooltip());
|
|
|
|
slider = new CButton(Point(), name, CButton::tooltip());
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
left->setImageOrder(0, 1, 1, 1);
|
|
|
|
right->setImageOrder(2, 3, 3, 3);
|
|
|
|
slider->setImageOrder(4, 4, 4, 4);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
left = new CButton(Point(), horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", CButton::tooltip());
|
|
|
|
right = new CButton(Point(), horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", CButton::tooltip());
|
|
|
|
slider = new CButton(Point(), "SCNRBSL.DEF", CButton::tooltip());
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
slider->actOnDown = true;
|
|
|
|
slider->soundDisabled = true;
|
|
|
|
left->soundDisabled = true;
|
|
|
|
right->soundDisabled = true;
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
if (horizontal)
|
|
|
|
right->moveBy(Point(totalw - right->pos.w, 0));
|
|
|
|
else
|
|
|
|
right->moveBy(Point(0, totalw - right->pos.h));
|
|
|
|
|
2014-08-09 15:14:31 +03:00
|
|
|
left->addCallback(std::bind(&CSlider::moveLeft,this));
|
|
|
|
right->addCallback(std::bind(&CSlider::moveRight,this));
|
|
|
|
slider->addCallback(std::bind(&CSlider::sliderClicked,this));
|
2014-08-03 14:16:19 +03:00
|
|
|
|
|
|
|
if(horizontal)
|
|
|
|
{
|
|
|
|
pos.h = slider->pos.h;
|
|
|
|
pos.w = totalw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos.w = slider->pos.w;
|
|
|
|
pos.h = totalw;
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
updateSliderPos();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::block( bool on )
|
|
|
|
{
|
|
|
|
left->block(on);
|
|
|
|
right->block(on);
|
|
|
|
slider->block(on);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::setAmount( int to )
|
|
|
|
{
|
|
|
|
amount = to;
|
|
|
|
positions = to - capacity;
|
|
|
|
vstd::amax(positions, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::showAll(SDL_Surface * to)
|
|
|
|
{
|
|
|
|
CSDL_Ext::fillRectBlack(to, &pos);
|
|
|
|
CIntObject::showAll(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::wheelScrolled(bool down, bool in)
|
|
|
|
{
|
|
|
|
moveTo(value + 3 * (down ? +scrollStep : -scrollStep));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSlider::keyPressed(const SDL_KeyboardEvent & key)
|
|
|
|
{
|
|
|
|
if(key.state != SDL_PRESSED) return;
|
|
|
|
|
2014-09-18 17:08:21 +03:00
|
|
|
int moveDest = value;
|
2014-07-15 10:14:49 +03:00
|
|
|
switch(key.keysym.sym)
|
|
|
|
{
|
|
|
|
case SDLK_UP:
|
2014-09-18 17:08:21 +03:00
|
|
|
if (!horizontal)
|
|
|
|
moveDest = value - scrollStep;
|
|
|
|
break;
|
2014-07-15 10:14:49 +03:00
|
|
|
case SDLK_LEFT:
|
2014-09-18 17:08:21 +03:00
|
|
|
if (horizontal)
|
|
|
|
moveDest = value - scrollStep;
|
2014-07-15 10:14:49 +03:00
|
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
2014-09-18 17:08:21 +03:00
|
|
|
if (!horizontal)
|
|
|
|
moveDest = value + scrollStep;
|
|
|
|
break;
|
2014-07-15 10:14:49 +03:00
|
|
|
case SDLK_RIGHT:
|
2014-09-18 17:08:21 +03:00
|
|
|
if (horizontal)
|
|
|
|
moveDest = value + scrollStep;
|
2014-07-15 10:14:49 +03:00
|
|
|
break;
|
|
|
|
case SDLK_PAGEUP:
|
|
|
|
moveDest = value - capacity + scrollStep;
|
|
|
|
break;
|
|
|
|
case SDLK_PAGEDOWN:
|
|
|
|
moveDest = value + capacity - scrollStep;
|
|
|
|
break;
|
|
|
|
case SDLK_HOME:
|
|
|
|
moveDest = 0;
|
|
|
|
break;
|
|
|
|
case SDLK_END:
|
|
|
|
moveDest = amount - capacity;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
moveTo(moveDest);
|
|
|
|
}
|
|
|
|
|
2014-11-24 14:24:03 +02:00
|
|
|
void CSlider::moveToMin()
|
|
|
|
{
|
|
|
|
moveTo(0);
|
|
|
|
}
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
void CSlider::moveToMax()
|
|
|
|
{
|
|
|
|
moveTo(amount);
|
|
|
|
}
|