23#include "../ktech.hpp"
54 std::function<
void()> OnPress,
55 const std::string& key = KTech::Keys::return_,
57 const std::string& text =
"Switch",
59 bool withFrame =
false,
60 KTech::RGBA unselectedOff = KTech::RGBAColors::gray,
62 KTech::RGBA unselectedOn = KTech::RGBAColors::Widgets::switchUnselectedOnGreen,
63 KTech::RGBA selectedOn = KTech::RGBAColors::Widgets::switchSelectedOnGreen,
64 KTech::RGBA down = KTech::RGBAColors::Widgets::buttonDownBlue)
68 m_unselectedOffRGBA(unselectedOff),
69 m_selectedOffRGBA(selectedOff),
70 m_unselectedOnRGBA(unselectedOn),
71 m_selectedOnRGBA(selectedOn),
73 m_downInvocation(
engine, [this]() -> bool { return RemovePressColor(); })
76 SetText(text, withFrame);
78 m_callbackGroup.RegisterCallback(key, [
this]() ->
bool {
return this->OnPress(); });
87 void SetText(
const std::string& text,
bool withFrame)
94 tempRGBA = m_selectedOnRGBA;
98 tempRGBA = m_unselectedOnRGBA;
105 tempRGBA = m_selectedOffRGBA;
109 tempRGBA = m_unselectedOffRGBA;
115 m_textures.resize(TEXTURES_SIZE_FRAMED);
127 m_textures.resize(TEXTURES_SIZE_FRAMELESS);
143 enum TextureIndex :
size_t
146 TEXTURES_SIZE_FRAMELESS,
147 ti_topLeftCorner = TEXTURES_SIZE_FRAMELESS,
150 ti_bottomRightCorner,
158 KTech::RGBA m_unselectedOffRGBA, m_selectedOffRGBA, m_unselectedOnRGBA, m_selectedOnRGBA, m_downRGBA;
159 static constexpr size_t pressLength = 100;
162 void OnSelect()
override
167 void OnDeselect()
override
172 auto OnPress() ->
bool
178 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_downRGBA; });
181 m_downInvocation.
Invoke(pressLength, KTech::Time::Measurement::milliseconds);
191 void RenderSelected()
197 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_selectedOnRGBA; });
204 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_selectedOffRGBA; });
209 void RenderUnselected()
215 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_unselectedOnRGBA; });
222 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_unselectedOffRGBA; });
227 auto RemovePressColor() ->
bool
235 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_selectedOnRGBA; });
242 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_selectedOffRGBA; });
252 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_unselectedOnRGBA; });
259 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_unselectedOffRGBA; });
Complete engine containing all engine components.
Definition engine.hpp:41
Widget that toggles a value and calls a callback function when pressed.
Definition switch.hpp:29
void SetText(const std::string &text, bool withFrame)
Change the displayed text.
Definition switch.hpp:87
std::function< void()> m_OnPress
Function to call when pressed.
Definition switch.hpp:33
bool m_on
Toggled value.
Definition switch.hpp:31
void SetValue(bool value)
Set the toggled value.
Definition switch.hpp:136
Switch(KTech::Engine &engine, KTech::ID< KTech::UI > ui, std::function< void()> OnPress, const std::string &key=KTech::Keys::return_, KTech::Point position={ 0, 0 }, const std::string &text="Switch", bool on=false, bool withFrame=false, KTech::RGBA unselectedOff=KTech::RGBAColors::gray, KTech::RGBA selectedOff=KTech::RGBAColors::white, KTech::RGBA unselectedOn=KTech::RGBAColors::Widgets::switchUnselectedOnGreen, KTech::RGBA selectedOn=KTech::RGBAColors::Widgets::switchSelectedOnGreen, KTech::RGBA down=KTech::RGBAColors::Widgets::buttonDownBlue)
Construct a Switch.
Definition switch.hpp:52
Like Cell, but with RGBA foreground and background colors, instead of RGB.
Definition cella.hpp:32
RGBA f
Foreground (character) color.
Definition cella.hpp:33
Serializable world structure identifier.
Definition id.hpp:38
2D vector, mostly used to store positions and directions.
Definition point.hpp:30
Like RGB, but also has an alpha channel representing transparency.
Definition rgba.hpp:30
A CellA-based sprite.
Definition texture.hpp:48
Calls a function after a given time.
Definition invocation.hpp:68
void Invoke(long time, Measurement measurement)
Invoke your callback function.
Definition invocation.cpp:82
Unsigned 2D vector, mostly used to store sizes and 2D indexes.
Definition upoint.hpp:29