caption interface-api separation

This commit is contained in:
Andrea Blankenstijn 2021-08-17 11:00:18 +02:00
parent bb934d4f86
commit 0eb300ceeb
6 changed files with 29 additions and 52 deletions

View file

@ -1,14 +1,14 @@
#include <basic_widgets/w/caption.hpp> #include <basic_widgets/w/caption_impl.hpp>
#include "run.hpp" #include "run.hpp"
using bwidgets::Caption; using bwidgets::CaptionImpl;
auto main() -> int auto main() -> int
{ {
run_example<Caption>( run_example<CaptionImpl>(
[](auto w, auto f, auto, auto) { [](auto w, auto f, auto, auto) {
w->alignment = Caption::Alignment::CENTER; w->alignment = CaptionImpl::Alignment::CENTER;
w->text("¡jello!"); w->text("¡jello!");
w->font(f); w->font(f);
}, },

View file

@ -12,7 +12,7 @@
#include <basic_widgets/core/math.hpp> #include <basic_widgets/core/math.hpp>
#include <basic_widgets/core/type/concepts.hpp> #include <basic_widgets/core/type/concepts.hpp>
#include <basic_widgets/w/caption.hpp> #include <basic_widgets/w/caption_impl.hpp>
#include <basic_widgets/w/feat/keyboard_handler_impl.hpp> #include <basic_widgets/w/feat/keyboard_handler_impl.hpp>
#include <basic_widgets/w/feat/mouse_handler_impl.hpp> #include <basic_widgets/w/feat/mouse_handler_impl.hpp>
@ -20,13 +20,13 @@ namespace bwidgets
{ {
template<typename T> template<typename T>
class Input : public WidgetImpl, class Input : public virtual WidgetImpl,
public FontHandler, public FontHandler,
public KeyboardHandlerImpl, public KeyboardHandlerImpl,
public MouseHandlerImpl public MouseHandlerImpl
{ {
protected: protected:
Caption _input_caption; CaptionImpl _input_caption;
Input(Widget* parent = nullptr) : WidgetImpl {parent}, _input_caption {this} Input(Widget* parent = nullptr) : WidgetImpl {parent}, _input_caption {this}
{ {

View file

@ -5,7 +5,7 @@
#include <string> #include <string>
#include <basic_widgets/core/type/color.hpp> #include <basic_widgets/core/type/color.hpp>
#include <basic_widgets/w/caption.hpp> #include <basic_widgets/w/caption_impl.hpp>
#include <basic_widgets/w/feat/mouse_handler_impl.hpp> #include <basic_widgets/w/feat/mouse_handler_impl.hpp>
namespace bwidgets namespace bwidgets
@ -15,7 +15,7 @@ namespace bwidgets
public virtual MouseHandlerImpl public virtual MouseHandlerImpl
{ {
protected: protected:
Caption _caption; CaptionImpl _caption;
SDL_Rect _caption_area {}; SDL_Rect _caption_area {};
Color _color_foreground = default_color_fg; Color _color_foreground = default_color_fg;

View file

@ -3,7 +3,7 @@
#include <basic_widgets/core/texture.hpp> #include <basic_widgets/core/texture.hpp>
#include <basic_widgets/core/type/size.hpp> #include <basic_widgets/core/type/size.hpp>
#include <basic_widgets/w/base/widget_impl.hpp> #include <basic_widgets/w/base/widget.hpp>
#include <basic_widgets/w/feat/font_handler.hpp> #include <basic_widgets/w/feat/font_handler.hpp>
#include <basic_widgets/w/feat/texture_handler.hpp> #include <basic_widgets/w/feat/texture_handler.hpp>
@ -11,23 +11,10 @@ struct SDL_Surface;
namespace bwidgets namespace bwidgets
{ {
class Caption : public WidgetImpl, class Caption : public virtual Widget,
public FontHandler, public FontHandler,
public TextureHandler public TextureHandler
{ {
protected:
Font::RenderMode _render_mode {Font::RenderMode::SHADED};
std::string _text;
std::shared_ptr<Texture> _text_texture {nullptr};
void _handle_font_change(const std::shared_ptr<Font>&) override;
void _handle_font_color_change(Color, Color)
override;
void _handle_geometry_change(const SDL_Rect&) override;
void _handle_renderer_change(const std::shared_ptr<Renderer>&) override;
void _handle_rendering() override;
void _handle_texture_update() override;
public: public:
enum struct Alignment enum struct Alignment
{ {
@ -35,26 +22,18 @@ namespace bwidgets
LEFT, LEFT,
RIGHT RIGHT
}; };
inline static const Color default_color_bg = {255, 255, 255, SDL_ALPHA_OPAQUE}; inline static const Color default_color_bg = {255, 255, 255, SDL_ALPHA_OPAQUE};
inline static const Color default_color_fg = {0, 0, 0, SDL_ALPHA_OPAQUE}; inline static const Color default_color_fg = {0, 0, 0, SDL_ALPHA_OPAQUE};
inline static const Size default_margins = {3, 3}; inline static const Size default_margins = {3, 3};
Alignment alignment {Alignment::LEFT}; Alignment alignment {Alignment::LEFT};
Size margins {3, 3}; Size margins {default_margins};
Caption(WidgetImpl* parent = nullptr) noexcept; using Widget::Widget;
Caption(const Caption&) = delete;
Caption(Caption&&) = delete;
~Caption() override = default;
auto operator=(const Caption&) = delete;
auto operator=(Caption&&) = delete;
virtual auto render_mode(Font::RenderMode) -> Caption*; virtual void render_mode(Font::RenderMode) = 0;
[[nodiscard]] virtual auto text() const noexcept -> const std::string&; [[nodiscard]] virtual auto text() const noexcept -> const std::string& = 0;
virtual auto text(const std::string&) -> Caption*; virtual void text(const std::string&) = 0;
[[nodiscard]] auto size() const noexcept -> Size override;
}; };
} }

View file

@ -31,7 +31,7 @@ libbasic_widgets = static_library('basic_widgets',
'src/w/base/layout.cpp', 'src/w/base/layout.cpp',
'src/w/base/widget.cpp', 'src/w/base/widget.cpp',
'src/w/button.cpp', 'src/w/button.cpp',
'src/w/caption.cpp', 'src/w/caption_impl.cpp',
'src/w/feat/event_handler_impl.cpp', 'src/w/feat/event_handler_impl.cpp',
'src/w/feat/keyboard_handler_impl.cpp', 'src/w/feat/keyboard_handler_impl.cpp',
'src/w/feat/mouse_handler_impl.cpp', 'src/w/feat/mouse_handler_impl.cpp',

View file

@ -2,27 +2,26 @@
#include <basic_widgets/core/math.hpp> #include <basic_widgets/core/math.hpp>
#include <basic_widgets/core/texture.hpp> #include <basic_widgets/core/texture.hpp>
#include <basic_widgets/w/caption.hpp> #include <basic_widgets/w/caption_impl.hpp>
#include <basic_widgets/w/numeric_input.hpp> #include <basic_widgets/w/numeric_input.hpp>
using namespace bwidgets; using namespace bwidgets;
Caption::Caption(WidgetImpl* parent) noexcept : WidgetImpl {parent} CaptionImpl::CaptionImpl(Widget* parent) noexcept : WidgetImpl {parent}
{ {
_font_color_bg = default_font_color_bg; _font_color_bg = default_font_color_bg;
_font_color_fg = default_font_color_fg; _font_color_fg = default_font_color_fg;
} }
auto Caption::render_mode(Font::RenderMode m) -> Caption* void CaptionImpl::render_mode(Font::RenderMode m)
{ {
if (m != _render_mode) { if (m != _render_mode) {
_text_texture.reset(); _text_texture.reset();
_render_mode = m; _render_mode = m;
} }
return this;
} }
auto Caption::size() const noexcept -> Size auto CaptionImpl::size() const noexcept -> Size
{ {
if (_font == nullptr) return {0, 0}; if (_font == nullptr) return {0, 0};
@ -31,26 +30,25 @@ auto Caption::size() const noexcept -> Size
return {size.w + 2 * margins.w, size.h + 2 * margins.h}; return {size.w + 2 * margins.w, size.h + 2 * margins.h};
} }
auto Caption::text() const noexcept -> const std::string& auto CaptionImpl::text() const noexcept -> const std::string&
{ {
return _text; return _text;
} }
auto Caption::text(const std::string& t) -> Caption* void CaptionImpl::text(const std::string& t)
{ {
if (t != _text) { if (t != _text) {
_text_texture.reset(); _text_texture.reset();
_text = t; _text = t;
} }
return this;
} }
void Caption::_handle_font_change(const std::shared_ptr<Font>&) void CaptionImpl::_handle_font_change(const std::shared_ptr<Font>&)
{ {
_text_texture.reset(); _text_texture.reset();
} }
void Caption::_handle_font_color_change(Color fg, Color bg) void CaptionImpl::_handle_font_color_change(Color fg, Color bg)
{ {
if (fg != _font_color_fg if (fg != _font_color_fg
|| (bg != _font_color_bg && _font_render_mode == Font::RenderMode::SHADED)) || (bg != _font_color_bg && _font_render_mode == Font::RenderMode::SHADED))
@ -60,19 +58,19 @@ void Caption::_handle_font_color_change(Color fg, Color bg)
} }
} }
void Caption::_handle_geometry_change(const SDL_Rect& vp) void CaptionImpl::_handle_geometry_change(const SDL_Rect& vp)
{ {
if (vp.w != _viewport.w || vp.h != _viewport.h) { if (vp.w != _viewport.w || vp.h != _viewport.h) {
_widget_area = rect_margin({0, 0, vp.w, vp.h}, margins); _widget_area = rect_margin({0, 0, vp.w, vp.h}, margins);
} }
} }
void Caption::_handle_renderer_change(const std::shared_ptr<Renderer>&) void CaptionImpl::_handle_renderer_change(const std::shared_ptr<Renderer>&)
{ {
_text_texture.reset(); _text_texture.reset();
} }
void Caption::_handle_rendering() void CaptionImpl::_handle_rendering()
{ {
if (_text_texture == nullptr) _handle_texture_update(); if (_text_texture == nullptr) _handle_texture_update();
@ -99,7 +97,7 @@ void Caption::_handle_rendering()
_renderer->copy(_text_texture.get(), nullptr, texture_dst); _renderer->copy(_text_texture.get(), nullptr, texture_dst);
} }
void Caption::_handle_texture_update() void CaptionImpl::_handle_texture_update()
{ {
SDL_assert_release(_font != nullptr && _text_texture == nullptr); // NOLINT SDL_assert_release(_font != nullptr && _text_texture == nullptr); // NOLINT
SDL_Surface* s {nullptr}; SDL_Surface* s {nullptr};