FreeRDP
Loading...
Searching...
No Matches
SdlWidget Class Reference
Inheritance diagram for SdlWidget:

Public Member Functions

 SdlWidget (SDL_Renderer *renderer, SDL_Rect rect, bool input)
 
 SdlWidget (SdlWidget &&other) noexcept
 
bool fill (SDL_Renderer *renderer, SDL_Color color)
 
bool fill (SDL_Renderer *renderer, const std::vector< SDL_Color > &colors)
 
bool update_text (SDL_Renderer *renderer, const std::string &text, SDL_Color fgcolor)
 
bool update_text (SDL_Renderer *renderer, const std::string &text, SDL_Color fgcolor, SDL_Color bgcolor)
 
bool wrap () const
 
bool set_wrap (bool wrap=true, size_t width=0)
 
const SDL_Rect & rect () const
 
 SdlWidget (const SdlWidget &other)=delete
 
SdlWidgetoperator= (const SdlWidget &other)=delete
 
SdlWidgetoperator= (SdlWidget &&other)=delete
 
 SdlWidget (std::shared_ptr< SDL_Renderer > &renderer, const SDL_FRect &rect)
 
 SdlWidget (const SdlWidget &other)=delete
 
 SdlWidget (SdlWidget &&other) noexcept
 
SdlWidgetoperator= (const SdlWidget &other)=delete
 
SdlWidgetoperator= (SdlWidget &&other)=delete
 
bool fill (SDL_Color color) const
 
bool fill (const std::vector< SDL_Color > &colors) const
 
bool update_text (const std::string &text)
 
bool wrap () const
 
bool set_wrap (bool wrap=true, size_t width=0)
 
const SDL_FRect & rect () const
 
bool update ()
 

Static Public Member Functions

static bool error_ex (Sint32 res, const char *what, const char *file, size_t line, const char *fkt)
 
static bool error_ex (bool success, const char *what, const char *file, size_t line, const char *fkt)
 

Protected Member Functions

virtual bool clear () const
 
virtual bool updateInternal ()
 

Protected Attributes

std::shared_ptr< SDL_Renderer > _renderer {}
 
SDL_Color _backgroundcolor = { 0x56, 0x56, 0x56, 0xff }
 
SDL_Color _fontcolor = { 0xd1, 0xcf, 0xcd, 0xff }
 
std::string _text
 

Detailed Description

Definition at line 48 of file SDL2/dialogs/sdl_widget.hpp.

Constructor & Destructor Documentation

◆ SdlWidget() [1/3]

SdlWidget::SdlWidget ( SDL_Renderer *  renderer,
SDL_Rect  rect,
bool  input 
)

Definition at line 45 of file SDL2/dialogs/sdl_widget.cpp.

46 : _rect(rect), _input(input)
47{
48 assert(renderer);
49
51 "OpenSans-VariableFont_wdth,wght.ttf");
52 if (!ops)
53 widget_log_error(-1, "SDLResourceManager::get");
54 else
55 {
56 _font = TTF_OpenFontRW(ops, 1, 64);
57 if (!_font)
58 widget_log_error(-1, "TTF_OpenFontRW");
59 }
60}
static SDL_RWops * get(const std::string &type, const std::string &id)
static std::string typeFonts()

◆ SdlWidget() [2/3]

SdlWidget::SdlWidget ( SdlWidget &&  other)
noexcept

Definition at line 74 of file SDL2/dialogs/sdl_widget.cpp.

75 : _font(other._font), _image(other._image), _rect(other._rect), _input(other._input),
76 _wrap(other._wrap), _text_width(other._text_width)
77{
78 other._font = nullptr;
79 other._image = nullptr;
80}

◆ ~SdlWidget()

SdlWidget::~SdlWidget ( )
virtualdefault

Definition at line 173 of file SDL2/dialogs/sdl_widget.cpp.

174{
175 TTF_CloseFont(_font);
176 if (_image)
177 SDL_DestroyTexture(_image);
178}

◆ SdlWidget() [3/3]

SdlWidget::SdlWidget ( std::shared_ptr< SDL_Renderer > &  renderer,
const SDL_FRect &  rect 
)

Definition at line 44 of file SDL3/dialogs/sdl_widget.cpp.

45 : _renderer(renderer),
46 _engine(TTF_CreateRendererTextEngine(renderer.get()), TTF_DestroyRendererTextEngine),
47 _rect(rect)
48{
49 assert(renderer);
50
52 "OpenSans-VariableFont_wdth,wght.ttf");
53 if (!ops)
54 widget_log_error(false, "SDLResourceManager::get");
55 else
56 {
57 _font = std::shared_ptr<TTF_Font>(TTF_OpenFontIO(ops, true, 64), TTF_CloseFont);
58 if (!_font)
59 widget_log_error(false, "TTF_OpenFontRW");
60 }
61}
static SDL_IOStream * get(const std::string &type, const std::string &id)

Member Function Documentation

◆ clear()

bool SdlWidget::clear ( ) const
protectedvirtual

Definition at line 274 of file SDL3/dialogs/sdl_widget.cpp.

275{
276 if (!_renderer)
277 return false;
278
279 SdlBlendModeGuard guard(_renderer, SDL_BLENDMODE_NONE);
280
281 const auto drc = SDL_SetRenderDrawColor(_renderer.get(), _backgroundcolor.r, _backgroundcolor.g,
282 _backgroundcolor.b, _backgroundcolor.a);
283 if (widget_log_error(drc, "SDL_SetRenderDrawColor"))
284 return false;
285
286 const auto rcls = SDL_RenderRect(_renderer.get(), &_rect);
287 return !widget_log_error(rcls, "SDL_RenderRect");
288}

◆ error_ex() [1/2]

bool SdlWidget::error_ex ( bool  success,
const char *  what,
const char *  file,
size_t  line,
const char *  fkt 
)
static

Definition at line 204 of file SDL3/dialogs/sdl_widget.cpp.

206{
207 if (success)
208 {
209 // Flip SDL3 convention to existing code convention to minimize code changes
210 return false;
211 }
212 static wLog* log = nullptr;
213 if (!log)
214 log = WLog_Get(TAG);
215 // Use -1 as it indicates error similar to SDL2 conventions
216 // sdl_log_error_ex treats any value other than 0 as SDL error
217 return sdl_log_error_ex(-1, log, what, file, line, fkt);
218}

◆ error_ex() [2/2]

bool SdlWidget::error_ex ( Sint32  res,
const char *  what,
const char *  file,
size_t  line,
const char *  fkt 
)
static

Definition at line 180 of file SDL2/dialogs/sdl_widget.cpp.

182{
183 static wLog* log = nullptr;
184 if (!log)
185 log = WLog_Get(TAG);
186 return sdl_log_error_ex(res, log, what, file, line, fkt);
187}

◆ fill() [1/4]

bool SdlWidget::fill ( const std::vector< SDL_Color > &  colors) const

Definition at line 241 of file SDL3/dialogs/sdl_widget.cpp.

242{
243 SdlBlendModeGuard guard(_renderer, SDL_BLENDMODE_NONE);
244
245 for (auto color : colors)
246 {
247 if (!draw_rect(_rect, color))
248 return false;
249
250 if (!guard.update(SDL_BLENDMODE_ADD))
251 return false;
252 }
253
254 return true;
255}

◆ fill() [2/4]

bool SdlWidget::fill ( SDL_Color  color) const

Definition at line 235 of file SDL3/dialogs/sdl_widget.cpp.

236{
237 std::vector<SDL_Color> colors = { color };
238 return fill(colors);
239}

◆ fill() [3/4]

bool SdlWidget::fill ( SDL_Renderer *  renderer,
const std::vector< SDL_Color > &  colors 
)

Definition at line 205 of file SDL2/dialogs/sdl_widget.cpp.

206{
207 assert(renderer);
208 SDL_BlendMode mode = SDL_BLENDMODE_INVALID;
209 SDL_GetRenderDrawBlendMode(renderer, &mode);
210 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
211 for (auto color : colors)
212 {
213 draw_rect(renderer, &_rect, color);
214 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
215 }
216 SDL_SetRenderDrawBlendMode(renderer, mode);
217 return true;
218}

◆ fill() [4/4]

bool SdlWidget::fill ( SDL_Renderer *  renderer,
SDL_Color  color 
)

Definition at line 199 of file SDL2/dialogs/sdl_widget.cpp.

200{
201 std::vector<SDL_Color> colors = { color };
202 return fill(renderer, colors);
203}

◆ rect()

const SDL_FRect & SdlWidget::rect ( ) const

Definition at line 242 of file SDL2/dialogs/sdl_widget.cpp.

243{
244 return _rect;
245}

◆ set_wrap()

bool SdlWidget::set_wrap ( bool  wrap = true,
size_t  width = 0 
)

Definition at line 235 of file SDL2/dialogs/sdl_widget.cpp.

236{
237 _wrap = wrap;
238 _text_width = width;
239 return _wrap;
240}

◆ update()

bool SdlWidget::update ( )

Definition at line 290 of file SDL3/dialogs/sdl_widget.cpp.

291{
292 if (!clear())
293 return false;
294 // TODO: Draw widget specifics
295 return updateInternal();
296}

◆ update_text() [1/3]

bool SdlWidget::update_text ( const std::string &  text)

Definition at line 298 of file SDL3/dialogs/sdl_widget.cpp.

299{
300 _text = text;
301 if (_text.empty())
302 return true;
303
304 SDL_FRect src{};
305 SDL_FRect dst{};
306
307 std::shared_ptr<SDL_Texture> texture;
308 if (_image)
309 {
310 texture = _image;
311 dst = _rect;
312 auto propId = SDL_GetTextureProperties(_image.get());
313 auto w = SDL_GetNumberProperty(propId, SDL_PROP_TEXTURE_WIDTH_NUMBER, -1);
314 auto h = SDL_GetNumberProperty(propId, SDL_PROP_TEXTURE_HEIGHT_NUMBER, -1);
315 if (w < 0 || h < 0)
316 widget_log_error(false, "SDL_GetTextureProperties");
317 src.w = static_cast<float>(w);
318 src.h = static_cast<float>(h);
319 }
320 else if (_wrap)
321 texture = render_text_wrapped(_text, _fontcolor, src, dst);
322 else
323 texture = render_text(_text, _fontcolor, src, dst);
324 if (!texture)
325 return false;
326
327 const auto rc = SDL_RenderTexture(_renderer.get(), texture.get(), &src, &dst);
328 return !widget_log_error(rc, "SDL_RenderCopy");
329}

◆ update_text() [2/3]

bool SdlWidget::update_text ( SDL_Renderer *  renderer,
const std::string &  text,
SDL_Color  fgcolor 
)

Definition at line 247 of file SDL2/dialogs/sdl_widget.cpp.

248{
249
250 if (text.empty())
251 return true;
252
253 SDL_Rect src{};
254 SDL_Rect dst{};
255
256 SDL_Texture* texture = nullptr;
257 if (_image)
258 {
259 texture = _image;
260 dst = _rect;
261 auto rc = SDL_QueryTexture(_image, nullptr, nullptr, &src.w, &src.h);
262 if (rc < 0)
263 widget_log_error(rc, "SDL_QueryTexture");
264 }
265 else if (_wrap)
266 texture = render_text_wrapped(renderer, text, fgcolor, src, dst);
267 else
268 texture = render_text(renderer, text, fgcolor, src, dst);
269 if (!texture)
270 return false;
271
272 const int rc = SDL_RenderCopy(renderer, texture, &src, &dst);
273 if (!_image)
274 SDL_DestroyTexture(texture);
275 if (rc < 0)
276 return !widget_log_error(rc, "SDL_RenderCopy");
277 return true;
278}

◆ update_text() [3/3]

bool SdlWidget::update_text ( SDL_Renderer *  renderer,
const std::string &  text,
SDL_Color  fgcolor,
SDL_Color  bgcolor 
)

Definition at line 220 of file SDL2/dialogs/sdl_widget.cpp.

222{
223 assert(renderer);
224
225 if (!fill(renderer, bgcolor))
226 return false;
227 return update_text(renderer, text, fgcolor);
228}

◆ updateInternal()

bool SdlWidget::updateInternal ( )
protectedvirtual

Definition at line 220 of file SDL3/dialogs/sdl_widget.cpp.

221{
222 return update_text(_text);
223}

◆ wrap()

bool SdlWidget::wrap ( ) const

Definition at line 230 of file SDL2/dialogs/sdl_widget.cpp.

231{
232 return _wrap;
233}

Field Documentation

◆ _backgroundcolor

SDL_Color SdlWidget::_backgroundcolor = { 0x56, 0x56, 0x56, 0xff }
protected

Definition at line 78 of file SDL3/dialogs/sdl_widget.hpp.

78{ 0x56, 0x56, 0x56, 0xff };

◆ _fontcolor

SDL_Color SdlWidget::_fontcolor = { 0xd1, 0xcf, 0xcd, 0xff }
protected

Definition at line 79 of file SDL3/dialogs/sdl_widget.hpp.

79{ 0xd1, 0xcf, 0xcd, 0xff };

◆ _renderer

std::shared_ptr<SDL_Renderer> SdlWidget::_renderer {}
protected

Definition at line 77 of file SDL3/dialogs/sdl_widget.hpp.

77{};

◆ _text

std::string SdlWidget::_text
mutableprotected

Definition at line 80 of file SDL3/dialogs/sdl_widget.hpp.


The documentation for this class was generated from the following files: