23#include "../ktech.hpp"
52 std::function<
void()> OnInsert,
55 const std::string& defaultNum =
"0",
57 const std::string& text =
"Value = ",
58 bool withFrame =
false,
61 :
Widget(
engine, ui, position),
m_OnInsert(std::move(OnInsert)), m_min(min), m_max(max), m_unselectedRGBA(unselected), m_selectedRGBA(selected)
64 for (
size_t i = 1; max / i > 0; i *= 10)
69 for (
size_t i = 1; min / i > 0; i *= 10)
89 void SetText(
const std::string& text,
bool withFrame)
122 for (
size_t x = 0; x < m_maxDigits && x < number.length(); x++)
133 enum TextureIndex :
size_t
137 TEXTURES_SIZE_FRAMELESS,
138 ti_topLeftCorner = TEXTURES_SIZE_FRAMELESS,
141 ti_bottomRightCorner,
150 size_t m_visibleNumber = 0;
151 uint32_t m_min, m_max;
152 uint8_t m_maxDigits = 0, m_minDigits = 0;
153 uint8_t m_currentDigit = 0;
155 void OnSelect()
override
160 void OnDeselect()
override
165 auto Insert() ->
bool
169 if (m_currentDigit == 0)
174 m_visibleNumber /= 10;
177 m_textures[ti_input](m_currentDigit, 0).c =
' ';
181 if (m_currentDigit == m_maxDigits)
192 if (m_visibleNumber < m_min)
196 else if (m_visibleNumber > m_max)
212 void RenderSelected()
216 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_selectedRGBA; });
220 void RenderUnselected()
223 if (m_visibleNumber < m_min)
225 m_visibleNumber = m_min;
227 m_currentDigit = m_minDigits;
229 std::string newTexture = std::to_string(m_min);
230 newTexture.resize(m_maxDigits,
' ');
233 else if (m_visibleNumber > m_max)
235 m_visibleNumber = m_max;
237 m_currentDigit = m_maxDigits;
239 std::string newTexture = std::to_string(m_max);
240 newTexture.resize(m_maxDigits,
' ');
243 else if (m_visibleNumber >= m_min && m_visibleNumber <= m_max)
251 texture.Transform([&](
KTech::CellA& cell){ cell.
f = m_unselectedRGBA; });
Widget for entering a number.
Definition intfield.hpp:29
void SetValue(const std::string &number)
Change the entered number.
Definition intfield.hpp:116
std::function< void()> m_OnInsert
Function to call when the user inserts or removes a digit.
Definition intfield.hpp:33
size_t m_number
The entered number adjusted to given maximum and minimum limits.
Definition intfield.hpp:31
IntField(KTech::Engine &engine, KTech::ID< KTech::UI > ui, std::function< void()> OnInsert, size_t min, size_t max, const std::string &defaultNum="0", KTech::Point position={0, 0}, const std::string &text="Value = ", bool withFrame=false, KTech::RGBA unselected=KTech::RGBAColors::gray, KTech::RGBA selected=KTech::RGBAColors::white)
Construct an IntField.
Definition intfield.hpp:50
void SetText(const std::string &text, bool withFrame)
Change the displayed text.
Definition intfield.hpp:89
Complete engine containing all engine components.
Definition engine.hpp:41
Input input
Input engine component.
Definition engine.hpp:47
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
Unsigned 2D vector, mostly used to store sizes and 2D indexes.
Definition upoint.hpp:29