Rose
Text.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include "Color.h"
11 #include "Font.h"
12 #include "GraphicsModel.h"
13 #include <string>
14 #include <utility>
15 #include <regex>
16 
17 namespace rose {
18 
19  static constexpr std::string_view CallPattern = "[A-Z]+[0-9][A-Z]+";
20  static constexpr std::string_view FloatPattern = "([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+))?";
21 
22  struct PointSize {
23  int pointSize{};
24  PointSize() : pointSize(0) {}
25  explicit PointSize(int size) : pointSize(size) {}
26  };
27 
28  struct FontName {
29  std::string fontName;
30  FontName() : fontName() {}
31  explicit FontName(std::string name) : fontName(std::move(name)) {}
32  FontName& operator=(const FontName& name) {
33  fontName = name.fontName;
34  return *this;
35  }
36  };
37 
42  class Text {
43  public:
47  Solid
48  };
49 
50  enum Status {
51  OK,
56  };
57 
58  protected:
59  std::string mText{};
60  std::string mSuffix{};
63  color::RGBA mCaretColor{};
64  color::RGBA mRegexFail{};
65  color::RGBA mDataSaved{};
66  float mCaretAlpha{0.f};
67  RenderStyle mRenderStyle{Blended};
68  std::string mFontName;
69  std::shared_ptr<_TTF_Font> mFont{};
70  int mPointSize;
71  gm::Texture mTexture{};
72  Size mTextSize{};
73  Status mStatus{OK};
74  int mCaretLocation{0};
75  bool mEditingActive{false};
76 
77  bool mEditable{false};
78  bool mTextValidated{true};
79  bool mSaveToSettings{false};
80  int mMaxSize{0};
81  char eM{'N'};
82  std::unique_ptr<std::regex> mValidationPattern{};
83 
85  void setEditingMode(bool editing, int carret);
86 
87  public:
88  Text();
89  virtual ~Text() = default;
90 
91  Text(const Text &) = delete;
92  Text(Text &&) = delete;
93  Text& operator=(const Text &) = delete;
94  Text& operator=(Text &&) = delete;
95 
105  Status createTextureBlended(gm::Context &context);
106 
108  void setPointSize(int pointSize) {
109  mPointSize = pointSize;
110  }
111 
113  void setFontName(const std::string &fontName) {
114  mFontName = fontName;
115  }
116 
122  bool setText(const std::string &text) {
123  if (mText != text) {
124  mText = text;
125  textUpdated();
126  return true;
127  }
128  return false;
129  }
130 
131  bool textUpdated();
132 
133  void setSuffix(const std::string &suffix) {
134  mSuffix = suffix;
135  }
136 
137  void setTextMaxSize(int maxSize, char em = '\0') {
138  mMaxSize = maxSize;
139  if (em)
140  eM = em;
141  }
142 
143  void setTextValidationPattern(const std::string& regex) {
144  mValidationPattern = std::make_unique<std::regex>(regex);
145  }
146  };
147 }
148 
156 template<class WidgetClass>
157 inline std::shared_ptr<WidgetClass> operator<<(std::shared_ptr<WidgetClass> widget, rose::PointSize pointSize) {
158  static_assert(std::is_base_of_v<rose::Text,WidgetClass>, "WidgetClass must be derived from rose::Text." );
159  widget->setPointSize(pointSize.pointSize);
160  return widget;
161 }
162 
170 template<class WidgetClass>
171 inline std::shared_ptr<WidgetClass> operator<<(std::shared_ptr<WidgetClass> widget, rose::FontName fontName) {
172  static_assert(std::is_base_of_v<rose::Text,WidgetClass>, "WidgetClass must be derived from rose::Text." );
173  widget->setFontName(fontName.fontName);
174  return widget;
175 }
color::RGBA mTextBgColor
The background color to use if rendering Shaded.
Definition: Text.h:62
True Type Fonts and supporting types and functions.
Render text shaded on a solid background.
Definition: Text.h:46
Render text blended on a transparent background.
Definition: Text.h:45
Red Green Blue Alpha representation of a color.
Definition: Color.h:64
The Font was not found.
Definition: Text.h:53
A structure to pass a Font name to a Widget through a manipulator.
Definition: Text.h:28
void setPointSize(int pointSize)
Set the font point size.
Definition: Text.h:108
Status
Definition: Text.h:50
Definition: Text.h:22
bool setText(const std::string &text)
Set the text displayed.
Definition: Text.h:122
Abstraction of SDL_Texture.
Definition: Texture.h:46
Context
Definition: GraphicsModel.h:76
Encapsulation of code for rendering Text.
Definition: Text.h:42
void setFontName(const std::string &fontName)
Set the font name.
Definition: Text.h:113
Abstraction of the graphics model.
std::string mFontName
The name of the True Type Font to use.
Definition: Text.h:68
int mPointSize
The point (pixel) size of the font.
Definition: Text.h:70
The text to be rendered was empty.
Definition: Text.h:52
color::RGBA mTextFgColor
The foreground color to use.
Definition: Text.h:61
RenderStyle
Definition: Text.h:44
The API returned a nullptr Surface.
Definition: Text.h:54
A size in integer dimensions.
Definition: Types.h:230
The API returned a nullptr Texture.
Definition: Text.h:55
ToDo: There is an issue that the initial scroll interaction is lost if the click/press lands on a Wid...
Definition: CelestialOverlay.cpp:13
Operation succeeded.
Definition: Text.h:51