2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// SwitchComponent.cpp
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-12-15 17:49:43 +00:00
|
|
|
// Basic on/off switch used in menus.
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
|
|
|
|
2013-06-19 01:12:30 +00:00
|
|
|
#include "SwitchComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "resources/Font.h"
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
SwitchComponent::SwitchComponent(Window* window, bool state)
|
|
|
|
: GuiComponent(window)
|
|
|
|
, mImage(window)
|
|
|
|
, mState(state)
|
|
|
|
, mOriginalValue(state)
|
|
|
|
, mColorOriginalValue(DEFAULT_COLORSHIFT)
|
|
|
|
, mColorChangedValue(DEFAULT_COLORSHIFT)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-21 17:35:43 +00:00
|
|
|
mImage.setImage(":/graphics/off.svg");
|
2020-06-21 12:25:28 +00:00
|
|
|
mImage.setResize(0, Font::get(FONT_SIZE_MEDIUM)->getLetterHeight());
|
|
|
|
mSize = mImage.getSize();
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SwitchComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
2020-07-13 18:58:25 +00:00
|
|
|
if (config->isMappedTo("a", input) && input.value) {
|
2020-08-05 17:31:59 +00:00
|
|
|
// Ignore input if the component has been disabled.
|
|
|
|
if (!mEnabled)
|
|
|
|
return true;
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
mState = !mState;
|
|
|
|
onStateChanged();
|
2020-12-15 17:49:43 +00:00
|
|
|
|
|
|
|
if (mToggleCallback)
|
|
|
|
mToggleCallback();
|
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-06-19 01:12:30 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
return false;
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void SwitchComponent::render(const glm::mat4& parentTrans)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2022-01-16 11:09:55 +00:00
|
|
|
glm::mat4 trans {parentTrans * getTransform()};
|
2020-06-21 12:25:28 +00:00
|
|
|
mImage.render(trans);
|
|
|
|
renderChildren(trans);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::setState(bool state)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mState = state;
|
|
|
|
onStateChanged();
|
2014-03-08 01:35:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
std::string SwitchComponent::getValue() const { return mState ? "true" : "false"; }
|
2017-06-12 16:38:59 +00:00
|
|
|
|
|
|
|
void SwitchComponent::setValue(const std::string& statestring)
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
if (statestring == "true")
|
|
|
|
mState = true;
|
|
|
|
else
|
|
|
|
mState = false;
|
2020-07-15 15:44:27 +00:00
|
|
|
|
|
|
|
mOriginalValue = mState;
|
2020-06-21 12:25:28 +00:00
|
|
|
onStateChanged();
|
2017-06-12 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 01:35:16 +00:00
|
|
|
void SwitchComponent::onStateChanged()
|
|
|
|
{
|
2020-06-21 17:35:43 +00:00
|
|
|
mImage.setImage(mState ? ":/graphics/on.svg" : ":/graphics/off.svg");
|
2021-10-25 16:39:58 +00:00
|
|
|
mImage.setResize(mSize);
|
2020-07-15 15:44:27 +00:00
|
|
|
|
|
|
|
// Change the color of the switch to reflect the changes.
|
|
|
|
if (mState == mOriginalValue)
|
|
|
|
mImage.setColorShift(mColorOriginalValue);
|
|
|
|
else
|
|
|
|
mImage.setColorShift(mColorChangedValue);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
std::vector<HelpPrompt> SwitchComponent::getHelpPrompts()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
std::vector<HelpPrompt> prompts;
|
|
|
|
prompts.push_back(HelpPrompt("a", "toggle"));
|
|
|
|
return prompts;
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|