2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// SwitchComponent.cpp
|
2020-06-06 14:48:05 +00:00
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// Basic switch used in the 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
|
|
|
|
2020-06-06 14:48:05 +00:00
|
|
|
SwitchComponent::SwitchComponent(
|
2020-06-21 12:25:28 +00:00
|
|
|
Window* window,
|
|
|
|
bool state)
|
|
|
|
: GuiComponent(window),
|
|
|
|
mImage(window),
|
2020-07-15 15:44:27 +00:00
|
|
|
mState(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
|
|
|
}
|
|
|
|
|
2014-03-22 21:55:18 +00:00
|
|
|
void SwitchComponent::onSizeChanged()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
mImage.setSize(mSize);
|
2014-03-22 21:55:18 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void SwitchComponent::render(const Transform4x4f& parentTrans)
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
Transform4x4f trans = parentTrans * getTransform();
|
|
|
|
mImage.render(trans);
|
|
|
|
renderChildren(trans);
|
2013-06-19 01:12:30 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 01:35:16 +00:00
|
|
|
bool SwitchComponent::getState() const
|
2013-06-19 01:12:30 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
return mState;
|
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
|
|
|
}
|
|
|
|
|
2017-06-12 16:38:59 +00:00
|
|
|
std::string SwitchComponent::getValue() const
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-05 17:31:59 +00:00
|
|
|
void SwitchComponent::setOpacity(unsigned char opacity)
|
|
|
|
{
|
|
|
|
mImage.setOpacity(opacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchComponent::setColorShift(unsigned int color)
|
|
|
|
{
|
|
|
|
mImage.setColorShift(color);
|
|
|
|
}
|
|
|
|
|
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");
|
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
|
|
|
}
|