Rose
TextField.h
1 
8 #pragma once
9 
10 #include <6>
11 #include <utility>
12 #include "Constants.h"
13 #include "Frame.h"
14 #include "Texture.h"
15 
16 namespace rose {
17 
23  class TextField : public Frame {
24  public:
25  struct FieldSettings {
26  DataType dataType;
27  std::string_view id, prefix, suffix;
28  int maxLength;
29  char eM;
30  };
31 
32  protected:
33  static void enterSettings(std::unique_ptr<rose::Settings> &db, const FieldSettings &setting);
34 
35  void saveValue();
36 
38  char mEm{'M'};
39  bool mModified{};
40  bool mValidEntry{};
41  bool mHasFocus{};
42  bool mToUpper{};
43  int mMaxLength{};
45  std::string mFontName{};
51  std::string mPrefix{};
52  std::string mSuffix{};
53  std::string mText{};
54  std::string::iterator mCaretLoc;
58  int mTextMaxGlyph{};
59  int mGlyphAdvance{};
60 
64 
65  std::shared_ptr<std::regex> mValidationPattern{};
66  std::shared_ptr<TextField> mPair{};
67  Id mPairId{};
68  size_t mPairIdx{};
69 
70  public:
71  TextField() = delete;
72  ~TextField() override = default;
73  TextField(TextField &&) = delete;
74  TextField(const TextField &) = delete;
75  TextField& operator=(TextField &&) = delete;
76  TextField& operator=(const TextField &) = delete;
77 
85  explicit TextField(int maxLength, int padding = 0, FontSize fontSize = 0, const string &fontName = "");
86 
96  TextField(int maxLenght, const std::string &text, const std::string &suffix = "", const std::string &prefix = "",
97  int padding = 0, FontSize fontSize = 0, const string &fontName = "");
98 
106  explicit TextField(Id id, int padding = 0, FontSize fontSize = 0, const string &fontName = "");
107 
108  template<size_t N>
109  static void Settings(std::unique_ptr<rose::Settings> &db, const std::array<FieldSettings,N> &settings) {
110  for (const auto& setting : settings) {
111  enterSettings(db, setting);
112  }
113  }
114 
121  void setPair(const Id &pairId, std::shared_ptr<TextField> &pair);
122 
124  Rectangle widgetLayout(sdl::Renderer &renderer, Rectangle available, uint layoutStage) override;
125 
127  void draw(sdl::Renderer &renderer, Rectangle parentRect) override;
128 
130  void initializeComposite() override;
131 
133  void setFontSize(FontSize fontSize) override;
134 
136  void setFontName(const std::string &fontName);
137 
139  bool textInputEvent(const std::string &text) override;
140 
142  bool keyboardFocusEvent(bool focus) override;
143 
145  bool keyboardEvent(uint state, uint repeat, SDL_Keysym keysym) override;
146 
151  void setRegex(std::shared_ptr<std::regex> regex) {
152  mValidationPattern = std::move(regex);
153  }
154 
159  void setToUpper(bool toUpper) {
160  mToUpper = toUpper;
161  }
162 
163  bool isModified() const { return mModified; }
164  };
165 
166  struct ToUpperCase {
167  bool mToUpperCase{true};
168  };
169 }
170 
178 template <class WidgetClass>
179 inline std::shared_ptr<WidgetClass> operator<<(std::shared_ptr<WidgetClass> widget, const std::shared_ptr<std::regex>& regex) {
180  static_assert(std::is_base_of_v<rose::TextField,WidgetClass>, "Regex can only be set on TextField Widgets." );
181  widget->setRegex(regex);
182  return widget;
183 }
184 
192 template <class WidgetClass>
193 inline std::shared_ptr<WidgetClass> operator<<(std::shared_ptr<WidgetClass> widget, const rose::ToUpperCase toUpperCase) {
194  static_assert(std::is_base_of_v<rose::TextField,WidgetClass>, "ToUpperCase can only be set on TextField Widgets." );
195  widget->setToUpper(toUpperCase.mToUpperCase);
196  return widget;
197 }
Size mTextSize
The size of the rendered Text.
Definition: TextField.h:57
bool mModified
True when the value has been modified.
Definition: TextField.h:39
sdl::Texture mTextTexture
Texture to render the Text;.
Definition: TextField.h:63
std::shared_ptr< TextField > mPair
The companion in a TextField pair.
Definition: TextField.h:66
color::RGBA mErrorColor
The color used when validation fails.
Definition: TextField.h:49
An Widget to edit a small amount of text.
Definition: TextField.h:28
void setPair(const Id &pairId, std::shared_ptr< TextField > &pair)
Set up a pair of TextField widgets.
Definition: TextField.cpp:152
DataType
Encode expected data types.
Definition: Constants.h:45
sdl::Texture mSuffixTexture
Texture to render the Suffix;.
Definition: TextField.h:62
Rectangle widgetLayout(sdl::Renderer &renderer, Rectangle available, uint layoutStage) override
See Frame::widgetLayout()
Definition: TextField.cpp:38
Size mSuffixSize
The size of the rendered Suffix.
Definition: TextField.h:56
std::string mFontName
The font name.
Definition: TextField.h:45
std::string mText
The current text value.
Definition: TextField.h:53
Red Green Blue Alpha representation of a color.
Definition: Color.h:64
Not set to a valid value.
void draw(gm::Context &context, const Position< int > &containerPosition) override
Draw the visual.
Definition: TextField.h:77
FontSize mFontSize
The font size in pixels.
Definition: TextField.h:44
bool keyboardEvent(uint state, uint repeat, SDL_Keysym keysym) override
Handle KeyboardEvent.
Definition: TextField.cpp:338
sdl::Texture mPrefixTexture
Texture to render the Prefix;.
Definition: TextField.h:61
std::shared_ptr< TTF_Font > FontPointer
Type for TTF smart pointer.
Definition: Font.h:54
bool mHasFocus
True if the TextField has keyboard focus.
Definition: TextField.h:41
int mGlyphAdvance
The advance of mEm.
Definition: TextField.h:59
An encapsulation of the SDL_Texture structure.
Definition: Texture.h:40
void initializeComposite() override
See Widget::initializeComposite();.
Definition: TextField.cpp:181
void setFontName(const std::string &fontName)
Set the font name.
Definition: TextField.cpp:236
std::string::iterator mCaretLoc
The caret location in mText (one past).
Definition: TextField.h:54
size_t mPairIdx
The index of this object in the pair (0 or 1).
Definition: TextField.h:68
int FontSize
Type specifier to pass a Font size to a Widget through a manipulator.
Definition: Types.h:43
std::string mPrefix
The prefix to the text.
Definition: TextField.h:51
color::RGBA mTextColor
Text foreground color.
Definition: TextField.h:48
int mMaxLength
The maximum length of the text.
Definition: TextField.h:43
Constants and Enumerations.
FontPointer mFont
FontPointer to font in use.
Definition: TextField.h:46
Id mPairId
The Id of the pair.
Definition: TextField.h:67
Definition: TextField.h:25
char mEm
The largest Glyph that will be in the text.
Definition: TextField.h:38
int mTextMaxGlyph
The width of mEm.
Definition: TextField.h:58
bool keyboardFocusEvent(bool focus) override
Handle keyboard focus events.
Definition: TextField.cpp:368
FontMetrics mFontMetrics
The metrics of the current font.
Definition: TextField.h:47
A settings database.
Definition: Settings.h:27
A composite of a Position and a Size.
Definition: Types.h:307
A type to specify an Id value.
Definition: StructuredTypes.h:148
bool toUpperCase
If true input is forced to upper case.
Definition: TextField.h:91
bool mValidEntry
True if the current value passes validation.
Definition: TextField.h:40
Written as a workaround for an issue in the SDL2 Library.
Definition: Renderer.h:64
Size mPrefixSize
The size of the rendered Prefix.
Definition: TextField.h:55
A size in integer dimensions.
Definition: Types.h:230
Definition: TextField.h:98
The size metrics that pertain to a particular font.
Definition: Font.h:173
bool mToUpper
Convert chars to upper case.
Definition: TextField.h:42
void setRegex(std::shared_ptr< std::regex > regex)
Set the validation regex.
Definition: TextField.h:151
DataType mDataType
The type of data expected by the TextField.
Definition: TextField.h:37
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
color::RGBA mUnmodifiedColor
The color used when data is unmodified.
Definition: TextField.h:50
void setFontSize(FontSize fontSize) override
Set the font size in pixels.
Definition: TextField.cpp:230
std::shared_ptr< std::regex > mValidationPattern
Regular expression to validate content.
Definition: TextField.h:65
void setToUpper(bool toUpper)
Set the ToUpper flag.
Definition: TextField.h:159
std::string mSuffix
The suffix to the text.
Definition: TextField.h:52