2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* 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-07-15 10:14:49 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Buttons.h"
|
|
|
|
|
|
|
|
#include "Images.h"
|
|
|
|
#include "TextControls.h"
|
|
|
|
|
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
2022-12-09 13:38:46 +02:00
|
|
|
#include "../battle/BattleInterface.h"
|
|
|
|
#include "../battle/BattleInterfaceClasses.h"
|
2023-07-29 00:11:16 +02:00
|
|
|
#include "../eventsSDL/InputHandler.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "../gui/CGuiHandler.h"
|
2023-05-18 22:31:05 +02:00
|
|
|
#include "../gui/MouseButton.h"
|
2023-04-27 19:21:06 +02:00
|
|
|
#include "../gui/Shortcut.h"
|
2024-02-28 22:24:19 +02:00
|
|
|
#include "../gui/InterfaceObjectConfigurable.h"
|
2024-05-02 14:55:20 +02:00
|
|
|
#include "../media/ISoundPlayer.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "../windows/InfoWindows.h"
|
2023-06-02 15:42:18 +02:00
|
|
|
#include "../render/Canvas.h"
|
2023-09-04 17:01:44 +02:00
|
|
|
#include "../render/IRenderHandler.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/texts/CGeneralTextHandler.h"
|
2024-02-28 22:24:19 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
void ButtonBase::update()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if (overlay)
|
|
|
|
{
|
2023-01-17 22:01:35 +02:00
|
|
|
Point targetPos = Rect::createCentered( pos, overlay->pos.dimensions()).topLeft();
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
if (state == EButtonState::PRESSED)
|
2023-01-17 22:01:35 +02:00
|
|
|
overlay->moveTo(targetPos + Point(1,1));
|
2014-07-15 10:14:49 +03:00
|
|
|
else
|
2023-01-17 22:01:35 +02:00
|
|
|
overlay->moveTo(targetPos);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 22:24:19 +02:00
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
// checkbox - has only have two frames: normal and pressed/highlighted
|
|
|
|
// hero movement speed buttons: only three frames: normal, pressed and blocked/highlighted
|
|
|
|
if (state == EButtonState::HIGHLIGHTED && image->size() < 4)
|
|
|
|
image->setFrame(image->size()-1);
|
2024-11-30 18:20:39 +02:00
|
|
|
else
|
|
|
|
image->setFrame(stateToIndex[vstd::to_underlying(state)]);
|
2024-02-28 22:24:19 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-17 22:22:45 +02:00
|
|
|
if (isActive())
|
2014-07-15 10:14:49 +03:00
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
2023-07-31 18:50:55 +02:00
|
|
|
void CButton::setBorderColor(std::optional<ColorRGBA> newBorderColor)
|
2018-03-17 06:23:22 +02:00
|
|
|
{
|
2023-07-31 18:50:55 +02:00
|
|
|
borderColor = newBorderColor;
|
2018-03-17 06:23:22 +02:00
|
|
|
}
|
|
|
|
|
2023-10-29 00:55:25 +02:00
|
|
|
void CButton::addCallback(const std::function<void()> & callback)
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
|
|
|
this->callback += callback;
|
|
|
|
}
|
|
|
|
|
2024-05-19 20:45:55 +02:00
|
|
|
void CButton::addPopupCallback(const std::function<void()> & callback)
|
|
|
|
{
|
|
|
|
this->callbackPopup += callback;
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
void ButtonBase::setTextOverlay(const std::string & Text, EFonts font, ColorRGBA color)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2024-02-27 22:19:09 +02:00
|
|
|
setOverlay(std::make_shared<CLabel>(pos.w/2, pos.h/2, font, ETextAlignment::CENTER, color, Text));
|
2014-07-15 10:14:49 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:39:13 +02:00
|
|
|
void ButtonBase::setOverlay(const std::shared_ptr<CIntObject>& newOverlay)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
overlay = newOverlay;
|
2022-12-17 18:50:33 +02:00
|
|
|
if(overlay)
|
|
|
|
{
|
|
|
|
addChild(newOverlay.get());
|
2023-01-17 22:01:35 +02:00
|
|
|
Point targetPos = Rect::createCentered( pos, overlay->pos.dimensions()).topLeft();
|
|
|
|
overlay->moveTo(targetPos);
|
2022-12-17 18:50:33 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
void ButtonBase::setImage(const AnimationPath & defName, bool playerColoredButton)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2024-02-27 22:19:09 +02:00
|
|
|
|
2024-02-28 22:24:19 +02:00
|
|
|
configurable.reset();
|
2024-02-27 22:19:09 +02:00
|
|
|
image = std::make_shared<CAnimImage>(defName, vstd::to_underlying(getState()));
|
|
|
|
pos = image->pos;
|
|
|
|
|
|
|
|
if (playerColoredButton)
|
2024-06-05 17:50:50 +02:00
|
|
|
image->setPlayerColor(LOCPLINT->playerID);
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 22:24:19 +02:00
|
|
|
const JsonNode & ButtonBase::getCurrentConfig() const
|
|
|
|
{
|
|
|
|
if (!config)
|
|
|
|
throw std::runtime_error("No config found in button!");
|
|
|
|
|
|
|
|
static constexpr std::array stateToConfig = {
|
|
|
|
"normal",
|
|
|
|
"pressed",
|
|
|
|
"blocked",
|
|
|
|
"highlighted"
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string key = stateToConfig[vstd::to_underlying(getState())];
|
|
|
|
const JsonNode & value = (*config)[key];
|
|
|
|
|
|
|
|
if (value.isNull())
|
|
|
|
throw std::runtime_error("No config found in button for state " + key + "!");
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ButtonBase::setConfigurable(const JsonPath & jsonName, bool playerColoredButton)
|
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2024-02-28 22:24:19 +02:00
|
|
|
|
|
|
|
config = std::make_unique<JsonNode>(jsonName);
|
|
|
|
|
|
|
|
image.reset();
|
|
|
|
configurable = std::make_shared<InterfaceObjectConfigurable>(getCurrentConfig());
|
|
|
|
pos = configurable->pos;
|
|
|
|
|
|
|
|
if (playerColoredButton)
|
2024-06-05 17:50:50 +02:00
|
|
|
image->setPlayerColor(LOCPLINT->playerID);
|
2024-02-28 22:24:19 +02:00
|
|
|
}
|
|
|
|
|
2024-02-29 15:39:13 +02:00
|
|
|
void CButton::addHoverText(EButtonState state, const std::string & text)
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
hoverTexts[vstd::to_underlying(state)] = text;
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
void ButtonBase::setImageOrder(int state1, int state2, int state3, int state4)
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
|
|
|
stateToIndex[0] = state1;
|
|
|
|
stateToIndex[1] = state2;
|
|
|
|
stateToIndex[2] = state3;
|
|
|
|
stateToIndex[3] = state4;
|
2014-07-15 10:14:49 +03:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:52:11 +02:00
|
|
|
std::shared_ptr<CIntObject> ButtonBase::getOverlay()
|
|
|
|
{
|
|
|
|
return overlay;
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
void ButtonBase::setStateImpl(EButtonState newState)
|
2017-12-26 12:03:19 +02:00
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
state = newState;
|
2024-02-28 22:24:19 +02:00
|
|
|
|
|
|
|
if (configurable)
|
|
|
|
{
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2024-02-28 22:24:19 +02:00
|
|
|
configurable = std::make_shared<InterfaceObjectConfigurable>(getCurrentConfig());
|
|
|
|
pos = configurable->pos;
|
|
|
|
|
|
|
|
if (overlay)
|
|
|
|
{
|
|
|
|
// Force overlay on top
|
|
|
|
removeChild(overlay.get());
|
|
|
|
addChild(overlay.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
update();
|
2017-12-26 12:03:19 +02:00
|
|
|
}
|
2024-02-27 22:19:09 +02:00
|
|
|
|
|
|
|
void CButton::setState(EButtonState newState)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
if (getState() == newState)
|
2014-07-15 10:14:49 +03:00
|
|
|
return;
|
2023-09-06 00:06:01 +02:00
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
if (newState == EButtonState::BLOCKED)
|
2023-09-06 00:06:01 +02:00
|
|
|
removeUsedEvents(LCLICK | SHOW_POPUP | HOVER | KEYBOARD);
|
|
|
|
else
|
|
|
|
addUsedEvents(LCLICK | SHOW_POPUP | HOVER | KEYBOARD);
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
setStateImpl(newState);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 22:24:19 +02:00
|
|
|
EButtonState ButtonBase::getState() const
|
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
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
return getState() == EButtonState::BLOCKED;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
bool CButton::isHighlighted()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
return getState() == EButtonState::HIGHLIGHTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::setHoverable(bool on)
|
|
|
|
{
|
|
|
|
hoverable = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::setSoundDisabled(bool on)
|
|
|
|
{
|
|
|
|
soundDisabled = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::setActOnDown(bool on)
|
|
|
|
{
|
|
|
|
actOnDown = on;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-03-02 20:30:29 +02:00
|
|
|
void CButton::setHelp(const std::pair<std::string, std::string> & help)
|
|
|
|
{
|
|
|
|
hoverTexts[0] = help.first;
|
|
|
|
helpBox = help.second;
|
|
|
|
}
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void CButton::block(bool on)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
if(on || getState() == EButtonState::BLOCKED) //dont change button state if unblock requested, but it's not blocked
|
|
|
|
setState(on ? EButtonState::BLOCKED : EButtonState::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
|
2017-08-11 13:38:10 +02:00
|
|
|
logAnim->trace("Button clicked at %dx%d", pos.x, pos.y);
|
2014-07-15 10:14:49 +03:00
|
|
|
CIntObject * parent = this->parent;
|
|
|
|
std::string prefix = "Parent is";
|
|
|
|
while (parent)
|
|
|
|
{
|
2017-08-11 13:38:10 +02:00
|
|
|
logAnim->trace("%s%s at %dx%d", prefix, typeid(*parent).name(), parent->pos.x, parent->pos.y);
|
2014-07-15 10:14:49 +03:00
|
|
|
parent = parent->parent;
|
|
|
|
prefix = '\t' + prefix;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CButton::clickPressed(const Point & cursorPosition)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
if(isBlocked())
|
|
|
|
return;
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
if (getState() != EButtonState::PRESSED)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
if (!soundDisabled)
|
2023-07-29 00:11:16 +02:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
CCS->soundh->playSound(soundBase::button);
|
2023-07-29 00:11:16 +02:00
|
|
|
GH.input().hapticFeedback();
|
|
|
|
}
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::PRESSED);
|
2023-07-08 13:33:04 +02:00
|
|
|
|
|
|
|
if (actOnDown)
|
|
|
|
onButtonClicked();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2023-07-08 13:33:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::clickReleased(const Point & cursorPosition)
|
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
if (getState() == EButtonState::PRESSED)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
if(hoverable && isHovered())
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::HIGHLIGHTED);
|
2023-07-08 13:33:04 +02:00
|
|
|
else
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::NORMAL);
|
2023-07-08 13:33:04 +02:00
|
|
|
|
|
|
|
if (!actOnDown)
|
|
|
|
onButtonClicked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CButton::clickCancel(const Point & cursorPosition)
|
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
if (getState() == EButtonState::PRESSED)
|
2023-07-08 13:33:04 +02:00
|
|
|
{
|
|
|
|
if(hoverable && isHovered())
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::HIGHLIGHTED);
|
2023-07-08 13:33:04 +02:00
|
|
|
else
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::NORMAL);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CButton::showPopupWindow(const Point & cursorPosition)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-05-19 20:45:55 +02:00
|
|
|
callbackPopup();
|
|
|
|
|
2024-02-29 15:39:13 +02:00
|
|
|
if(!helpBox.empty()) //there is no point to show window with nothing inside...
|
2014-07-15 10:14:49 +03:00
|
|
|
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)
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::HIGHLIGHTED);
|
2014-07-15 10:14:49 +03:00
|
|
|
else
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::NORMAL);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
std::string name = hoverTexts[vstd::to_underlying(getState())].empty()
|
2015-08-22 04:32:24 +02:00
|
|
|
? hoverTexts[0]
|
2024-02-27 22:19:09 +02:00
|
|
|
: hoverTexts[vstd::to_underlying(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
|
|
|
{
|
2022-11-18 17:54:10 +02:00
|
|
|
if (on)
|
2023-05-16 17:34:23 +02:00
|
|
|
GH.statusbar()->write(name);
|
2022-11-18 17:54:10 +02:00
|
|
|
else
|
2023-05-16 17:34:23 +02:00
|
|
|
GH.statusbar()->clearIfMatching(name);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
ButtonBase::ButtonBase(Point position, const AnimationPath & defName, EShortcut key, bool playerColoredButton)
|
|
|
|
: CKeyShortcut(key)
|
|
|
|
, stateToIndex({0, 1, 2, 3})
|
|
|
|
, state(EButtonState::NORMAL)
|
|
|
|
{
|
|
|
|
pos.x += position.x;
|
|
|
|
pos.y += position.y;
|
|
|
|
|
2024-02-28 22:24:19 +02:00
|
|
|
JsonPath jsonConfig = defName.toType<EResType::JSON>().addPrefix("CONFIG/WIDGETS/BUTTONS/");
|
|
|
|
if (CResourceHandler::get()->existsResource(jsonConfig))
|
|
|
|
{
|
|
|
|
setConfigurable(jsonConfig, playerColoredButton);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setImage(defName, playerColoredButton);
|
|
|
|
return;
|
|
|
|
}
|
2024-02-27 22:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-02-28 22:24:19 +02:00
|
|
|
ButtonBase::~ButtonBase() = default;
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CButton::CButton(Point position, const AnimationPath &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, EShortcut key, bool playerColoredButton):
|
2024-02-27 22:19:09 +02:00
|
|
|
ButtonBase(position, defName, key, playerColoredButton),
|
2024-02-29 15:39:13 +02:00
|
|
|
callback(Callback),
|
|
|
|
helpBox(help.second),
|
|
|
|
actOnDown(false),
|
2024-02-29 15:52:11 +02:00
|
|
|
hoverable(false),
|
2024-02-29 15:39:13 +02:00
|
|
|
soundDisabled(false)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2024-07-17 01:02:11 +02:00
|
|
|
addUsedEvents(LCLICK | SHOW_POPUP | HOVER | KEYBOARD);
|
2014-08-03 14:16:19 +03:00
|
|
|
hoverTexts[0] = help.first;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
void ButtonBase::setPlayerColor(PlayerColor player)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-05-03 18:05:07 +02:00
|
|
|
if (image && image->isPlayerColored())
|
2024-06-05 17:50:50 +02:00
|
|
|
image->setPlayerColor(player);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void CButton::showAll(Canvas & to)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
CIntObject::showAll(to);
|
2016-10-16 11:58:18 +02:00
|
|
|
|
2023-01-17 22:01:35 +02:00
|
|
|
if (borderColor)
|
2023-06-02 15:42:18 +02:00
|
|
|
to.drawBorder(Rect::createAround(pos, 1), *borderColor);
|
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>();
|
|
|
|
}
|
|
|
|
|
2022-12-27 22:19:05 +02:00
|
|
|
std::pair<std::string, std::string> CButton::tooltipLocalized(const std::string & key)
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
2022-12-27 22:19:05 +02:00
|
|
|
return std::make_pair(
|
2022-12-30 19:09:19 +02:00
|
|
|
CGI->generaltexth->translate(key, "hover"),
|
|
|
|
CGI->generaltexth->translate(key, "help")
|
2022-12-27 22:19:05 +02:00
|
|
|
);
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
CToggleBase::~CToggleBase() = default;
|
2014-08-03 14:16:19 +03:00
|
|
|
|
|
|
|
void CToggleBase::doSelect(bool on)
|
|
|
|
{
|
|
|
|
// for overrides
|
|
|
|
}
|
|
|
|
|
2020-01-21 11:09:08 +02:00
|
|
|
void CToggleBase::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
// for overrides
|
|
|
|
}
|
|
|
|
|
2023-07-05 17:14:37 +02:00
|
|
|
void CToggleBase::setSelectedSilent(bool on)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
selected = on;
|
2014-08-03 14:16:19 +03:00
|
|
|
doSelect(on);
|
2023-07-05 17:14:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CToggleBase::setSelected(bool on)
|
|
|
|
{
|
|
|
|
bool changed = (on != selected);
|
|
|
|
setSelectedSilent(on);
|
2014-08-03 16:19:16 +03:00
|
|
|
if (changed)
|
|
|
|
callback(on);
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
bool CToggleBase::isSelected() const
|
|
|
|
{
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:39:13 +02:00
|
|
|
bool CToggleBase::canActivate() const
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
2024-02-29 15:39:13 +02:00
|
|
|
return !selected || allowDeselection;
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
2024-02-29 15:39:13 +02:00
|
|
|
void CToggleBase::addCallback(const std::function<void(bool)> & function)
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
2014-11-12 22:45:48 +02:00
|
|
|
callback += function;
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
void CToggleBase::setAllowDeselection(bool on)
|
|
|
|
{
|
|
|
|
allowDeselection = on;
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CToggleButton::CToggleButton(Point position, const AnimationPath &defName, const std::pair<std::string, std::string> &help,
|
2024-09-06 00:08:18 +02:00
|
|
|
CFunctionList<void(bool)> callback, EShortcut key, bool playerColoredButton,
|
|
|
|
CFunctionList<void()> callbackSelected):
|
2014-08-03 14:16:19 +03:00
|
|
|
CButton(position, defName, help, 0, key, playerColoredButton),
|
2024-09-06 00:08:18 +02:00
|
|
|
CToggleBase(callback),
|
|
|
|
callbackSelected(callbackSelected)
|
2014-08-03 14:16:19 +03:00
|
|
|
{
|
2024-09-06 00:08:18 +02:00
|
|
|
addUsedEvents(DOUBLECLICK);
|
2014-08-03 14:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CToggleButton::doSelect(bool on)
|
|
|
|
{
|
2014-07-15 10:14:49 +03:00
|
|
|
if (on)
|
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::HIGHLIGHTED);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::NORMAL);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 11:09:08 +02:00
|
|
|
void CToggleButton::setEnabled(bool enabled)
|
|
|
|
{
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(enabled ? EButtonState::NORMAL : EButtonState::BLOCKED);
|
2020-01-21 11:09:08 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CToggleButton::clickPressed(const Point & cursorPosition)
|
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;
|
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
if (canActivate())
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
CCS->soundh->playSound(soundBase::button);
|
2023-07-29 00:11:16 +02:00
|
|
|
GH.input().hapticFeedback();
|
2024-02-27 22:19:09 +02:00
|
|
|
setState(EButtonState::PRESSED);
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2023-07-08 13:33:04 +02:00
|
|
|
}
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-07-08 13:33:04 +02:00
|
|
|
void CToggleButton::clickReleased(const Point & cursorPosition)
|
|
|
|
{
|
|
|
|
// force refresh
|
|
|
|
hover(false);
|
|
|
|
hover(true);
|
|
|
|
|
|
|
|
if(isBlocked())
|
|
|
|
return;
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
if (getState() == EButtonState::PRESSED && canActivate())
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-07-08 13:33:04 +02:00
|
|
|
onButtonClicked();
|
2024-02-27 22:19:09 +02:00
|
|
|
setSelected(!isSelected());
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2023-07-08 13:33:04 +02:00
|
|
|
else
|
2024-02-27 22:19:09 +02:00
|
|
|
doSelect(isSelected()); // restore
|
2023-07-08 13:33:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CToggleButton::clickCancel(const Point & cursorPosition)
|
|
|
|
{
|
|
|
|
// force refresh
|
|
|
|
hover(false);
|
|
|
|
hover(true);
|
|
|
|
|
|
|
|
if(isBlocked())
|
|
|
|
return;
|
|
|
|
|
2024-02-27 22:19:09 +02:00
|
|
|
doSelect(isSelected());
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2024-09-06 00:08:18 +02:00
|
|
|
void CToggleButton::clickDouble(const Point & cursorPosition)
|
|
|
|
{
|
|
|
|
if(callbackSelected)
|
|
|
|
callbackSelected();
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:39:13 +02:00
|
|
|
void CToggleGroup::addCallback(const 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
|
|
|
}
|
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
void CToggleGroup::resetCallback()
|
|
|
|
{
|
|
|
|
onChange.clear();
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:39:13 +02:00
|
|
|
void CToggleGroup::addToggle(int identifier, const std::shared_ptr<CToggleBase> & button)
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
if(auto intObj = std::dynamic_pointer_cast<CIntObject>(button)) // hack-ish workagound to avoid diamond problem with inheritance
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2018-04-07 13:34:11 +02:00
|
|
|
addChild(intObj.get());
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
button->addCallback([=] (bool on) { if (on) selectionChanged(identifier);});
|
2024-02-27 22:19:09 +02:00
|
|
|
button->setAllowDeselection(false);
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
if(buttons.count(identifier)>0)
|
|
|
|
logAnim->error("Duplicated toggle button id %d", identifier);
|
|
|
|
buttons[identifier] = button;
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2015-08-22 05:07:51 +02:00
|
|
|
CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange)
|
2018-04-07 13:34:11 +02:00
|
|
|
: 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
|
|
|
}
|
|
|
|
|
2020-01-21 11:09:08 +02:00
|
|
|
void CToggleGroup::setSelectedOnly(int id)
|
|
|
|
{
|
2024-02-29 15:39:13 +02:00
|
|
|
for(const auto & button : buttons)
|
|
|
|
button.second->setEnabled(button.first == id);
|
2020-01-21 11:09:08 +02:00
|
|
|
|
|
|
|
selectionChanged(id);
|
|
|
|
}
|
|
|
|
|
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);
|
2023-03-03 01:21:11 +02:00
|
|
|
redraw();
|
2014-07-15 10:14:49 +03:00
|
|
|
}
|
|
|
|
|
2022-12-17 06:19:16 +02:00
|
|
|
int CToggleGroup::getSelected() const
|
|
|
|
{
|
|
|
|
return selectedID;
|
|
|
|
}
|