KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
switch.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#include "../ktech.hpp"
24
28class Switch : public KTech::Widget
29{
30public:
31 bool m_on = false;
32
33 std::function<void()> m_OnPress;
34
54 std::function<void()> OnPress,
55 const std::string& key = KTech::Keys::return_,
56 KTech::Point position = { 0, 0 },
57 const std::string& text = "Switch",
58 bool on = false,
59 bool withFrame = false,
60 KTech::RGBA unselectedOff = KTech::RGBAColors::gray,
61 KTech::RGBA selectedOff = KTech::RGBAColors::white,
62 KTech::RGBA unselectedOn = KTech::RGBAColors::Widgets::switchUnselectedOnGreen,
63 KTech::RGBA selectedOn = KTech::RGBAColors::Widgets::switchSelectedOnGreen,
64 KTech::RGBA down = KTech::RGBAColors::Widgets::buttonDownBlue)
65 : Widget(engine, ui, position),
66 m_OnPress(std::move(OnPress)),
67 m_on(on),
68 m_unselectedOffRGBA(unselectedOff),
69 m_selectedOffRGBA(selectedOff),
70 m_unselectedOnRGBA(unselectedOn),
71 m_selectedOnRGBA(selectedOn),
72 m_downRGBA(down),
73 m_downInvocation(engine, [this]() -> bool { return RemovePressColor(); })
74 {
75 // Textures
76 SetText(text, withFrame);
77 // Input handlers
78 m_callbackGroup.RegisterCallback(key, [this]() -> bool { return this->OnPress(); });
79 }
80
87 void SetText(const std::string& text, bool withFrame)
88 {
89 KTech::RGBA tempRGBA;
90 if (m_on)
91 {
92 if (m_selected)
93 {
94 tempRGBA = m_selectedOnRGBA;
95 }
96 else
97 {
98 tempRGBA = m_unselectedOnRGBA;
99 }
100 }
101 else
102 {
103 if (m_selected)
104 {
105 tempRGBA = m_selectedOffRGBA;
106 }
107 else
108 {
109 tempRGBA = m_unselectedOffRGBA;
110 }
111 }
112 // Texture
113 if (withFrame)
114 {
115 m_textures.resize(TEXTURES_SIZE_FRAMED);
116 m_textures[ti_topLeftCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(0, 0));
117 m_textures[ti_topRightCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(1 + text.length(), 0));
118 m_textures[ti_bottomLeftCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(0, 2));
119 m_textures[ti_bottomRightCorner].Simple(KTech::UPoint(1, 1), KTech::CellA('#', tempRGBA), KTech::Point(1 + text.length(), 2));
120 m_textures[ti_topFrame].Simple(KTech::UPoint(text.length(), 1), KTech::CellA('-', tempRGBA), KTech::Point(1, 0));
121 m_textures[ti_leftFrame].Simple(KTech::UPoint(1, 1), KTech::CellA('|', tempRGBA), KTech::Point(0, 1));
122 m_textures[ti_bottomFrame].Simple(KTech::UPoint(text.length(), 1), KTech::CellA('-', tempRGBA), KTech::Point(1, 2));
123 m_textures[ti_rightFrame].Simple(KTech::UPoint(1, 1), KTech::CellA('|', tempRGBA), KTech::Point(1 + text.length(), 1));
124 }
125 else
126 {
127 m_textures.resize(TEXTURES_SIZE_FRAMELESS);
128 }
129 m_textures[ti_text].Write({text}, tempRGBA, KTech::RGBA(0, 0, 0, 0), KTech::Point(1, 1));
130 }
131
136 void SetValue(bool value)
137 {
138 m_on = value;
139 RemovePressColor();
140 }
141
142private:
143 enum TextureIndex : size_t
144 {
145 ti_text,
146 TEXTURES_SIZE_FRAMELESS,
147 ti_topLeftCorner = TEXTURES_SIZE_FRAMELESS,
148 ti_topRightCorner,
149 ti_bottomLeftCorner,
150 ti_bottomRightCorner,
151 ti_topFrame,
152 ti_leftFrame,
153 ti_bottomFrame,
154 ti_rightFrame,
155 TEXTURES_SIZE_FRAMED
156 };
157
158 KTech::RGBA m_unselectedOffRGBA, m_selectedOffRGBA, m_unselectedOnRGBA, m_selectedOnRGBA, m_downRGBA;
159 static constexpr size_t pressLength = 100;
160 KTech::Time::Invocation m_downInvocation;
161
162 void OnSelect() override
163 {
164 RenderSelected();
165 }
166
167 void OnDeselect() override
168 {
169 RenderUnselected();
170 }
171
172 auto OnPress() -> bool
173 {
174 m_on = !m_on;
175
176 for (KTech::Texture& texture : m_textures)
177 {
178 texture.Transform([&](KTech::CellA& cell){ cell.f = m_downRGBA; });
179 }
180
181 m_downInvocation.Invoke(pressLength, KTech::Time::Measurement::milliseconds);
182
183 if (m_OnPress)
184 {
185 m_OnPress();
186 }
187
188 return true;
189 }
190
191 void RenderSelected()
192 {
193 if (m_on)
194 {
195 for (KTech::Texture& texture : m_textures)
196 {
197 texture.Transform([&](KTech::CellA& cell){ cell.f = m_selectedOnRGBA; });
198 }
199 }
200 else
201 {
202 for (KTech::Texture& texture : m_textures)
203 {
204 texture.Transform([&](KTech::CellA& cell){ cell.f = m_selectedOffRGBA; });
205 }
206 }
207 }
208
209 void RenderUnselected()
210 {
211 if (m_on)
212 {
213 for (KTech::Texture& texture : m_textures)
214 {
215 texture.Transform([&](KTech::CellA& cell){ cell.f = m_unselectedOnRGBA; });
216 }
217 }
218 else
219 {
220 for (KTech::Texture& texture : m_textures)
221 {
222 texture.Transform([&](KTech::CellA& cell){ cell.f = m_unselectedOffRGBA; });
223 }
224 }
225 }
226
227 auto RemovePressColor() -> bool
228 {
229 if (m_selected)
230 {
231 if (m_on)
232 {
233 for (KTech::Texture& texture : m_textures)
234 {
235 texture.Transform([&](KTech::CellA& cell){ cell.f = m_selectedOnRGBA; });
236 }
237 }
238 else
239 {
240 for (KTech::Texture& texture : m_textures)
241 {
242 texture.Transform([&](KTech::CellA& cell){ cell.f = m_selectedOffRGBA; });
243 }
244 }
245 }
246 else
247 {
248 if (m_on)
249 {
250 for (KTech::Texture& texture : m_textures)
251 {
252 texture.Transform([&](KTech::CellA& cell){ cell.f = m_unselectedOnRGBA; });
253 }
254 }
255 else
256 {
257 for (KTech::Texture& texture : m_textures)
258 {
259 texture.Transform([&](KTech::CellA& cell){ cell.f = m_unselectedOffRGBA; });
260 }
261 }
262 }
263 return true;
264 }
265};
Complete engine containing all engine components.
Definition engine.hpp:41
World structure that comprises Textures, behaves as a user interface element, and exists within UI.
Definition widget.hpp:45
Engine & engine
Parent Engine.
Definition widget.hpp:57
Widget(Engine &engine, Point position=Point(0, 0), std::string name="")
Construct a Widget.
Definition widget.cpp:34
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