diff --git a/examples/example_widget.hpp b/examples/example_widget.hpp index eaaa2ad..5d677eb 100644 --- a/examples/example_widget.hpp +++ b/examples/example_widget.hpp @@ -5,14 +5,14 @@ #include #include -#include +#include using bwidgets::Color; using bwidgets::rect_margin; using bwidgets::Size; -using bwidgets::Widget; +using bwidgets::WidgetImpl; -class Example final : public Widget +class Example final : public WidgetImpl { void _handle_geometry_change(const SDL_Rect& vp) noexcept override { @@ -50,7 +50,7 @@ public: unsigned int cycle_g {3500}; // NOLINT(readability-magic-numbers) unsigned int cycle_b {3500}; // NOLINT(readability-magic-numbers) - using Widget::Widget; + using WidgetImpl::WidgetImpl; Example(Example&&) = delete; Example(const Example&) = delete; auto operator=(Example&&) = delete; diff --git a/inc/basic_widgets/w/base/input.hpp b/inc/basic_widgets/w/base/input.hpp index 1aa8622..0a59d28 100644 --- a/inc/basic_widgets/w/base/input.hpp +++ b/inc/basic_widgets/w/base/input.hpp @@ -20,7 +20,7 @@ namespace bwidgets { template - class Input : public Widget, + class Input : public WidgetImpl, public FontHandler, public KeyboardHandlerImpl, public MouseHandlerImpl @@ -28,7 +28,7 @@ namespace bwidgets protected: Caption _input_caption; - Input(Widget* parent = nullptr) : Widget {parent}, _input_caption {this} + Input(Widget* parent = nullptr) : WidgetImpl {parent}, _input_caption {this} { FocusHandlerImpl::focus_handler([this](bool focus) { if (focus) { diff --git a/inc/basic_widgets/w/base/layout.hpp b/inc/basic_widgets/w/base/layout.hpp index bd5859a..6395704 100644 --- a/inc/basic_widgets/w/base/layout.hpp +++ b/inc/basic_widgets/w/base/layout.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include namespace bwidgets { @@ -14,7 +14,7 @@ namespace bwidgets namespace bwidgets { - class Layout : public Widget + class Layout : public WidgetImpl { protected: std::vector> _widgets; @@ -30,7 +30,7 @@ namespace bwidgets Size margins = default_margins; - using Widget::Widget; + using WidgetImpl::WidgetImpl; Layout(const Layout&) = delete; Layout(Layout&&) = delete; diff --git a/inc/basic_widgets/w/base/widget.hpp b/inc/basic_widgets/w/base/widget.hpp index 2d5c926..f539bba 100644 --- a/inc/basic_widgets/w/base/widget.hpp +++ b/inc/basic_widgets/w/base/widget.hpp @@ -15,15 +15,6 @@ namespace bwidgets { class Widget : public virtual EventHandlerImpl { - protected: - std::shared_ptr _renderer; - SDL_Rect _viewport {0, 0, 0, 0}; - SDL_Rect _widget_area {0, 0, 0, 0}; - - virtual void _handle_geometry_change(const SDL_Rect&) = 0; - virtual void _handle_renderer_change(const std::shared_ptr&) {} - virtual void _handle_rendering() = 0; - public: Widget* parent; @@ -31,10 +22,10 @@ namespace bwidgets [[nodiscard]] virtual auto size() const noexcept -> Size = 0; - virtual auto render() -> Widget* final; - virtual auto renderer(std::shared_ptr) -> Widget* final; - virtual auto viewport(const SDL_Rect&) -> Widget* final; - [[nodiscard]] virtual auto viewport() const -> const SDL_Rect& final; + virtual void render() = 0; + virtual void renderer(std::shared_ptr) = 0; + virtual void viewport(const SDL_Rect&) = 0; + [[nodiscard]] virtual auto viewport() const -> const SDL_Rect& = 0; }; } diff --git a/inc/basic_widgets/w/base/widget_impl.hpp b/inc/basic_widgets/w/base/widget_impl.hpp new file mode 100644 index 0000000..43e2313 --- /dev/null +++ b/inc/basic_widgets/w/base/widget_impl.hpp @@ -0,0 +1,29 @@ +#ifndef BWIDGETS_WIDGET_IMPL_HPP +#define BWIDGETS_WIDGET_IMPL_HPP + +#include + +namespace bwidgets +{ + class WidgetImpl : public virtual Widget + { + public: + void render() override; + void renderer(std::shared_ptr) override; + void viewport(const SDL_Rect&) override; + [[nodiscard]] auto viewport() const -> const SDL_Rect& override; + + protected: + std::shared_ptr _renderer; + SDL_Rect _viewport {0, 0, 0, 0}; + SDL_Rect _widget_area {0, 0, 0, 0}; + + using Widget::Widget; + + virtual void _handle_geometry_change(const SDL_Rect&) = 0; + virtual void _handle_renderer_change(const std::shared_ptr&) {} + virtual void _handle_rendering() = 0; + }; +} + +#endif diff --git a/inc/basic_widgets/w/button.hpp b/inc/basic_widgets/w/button.hpp index 135568a..a79fea0 100644 --- a/inc/basic_widgets/w/button.hpp +++ b/inc/basic_widgets/w/button.hpp @@ -10,7 +10,7 @@ namespace bwidgets { - class Button : public virtual Widget, + class Button : public virtual WidgetImpl, public virtual FontHandler, public virtual MouseHandlerImpl { @@ -41,7 +41,7 @@ namespace bwidgets using FocusHandlerImpl::focus; using FocusHandlerImpl::focus_handler; - Button(Widget* parent = nullptr) noexcept; + Button(WidgetImpl* parent = nullptr) noexcept; Button(const Button&) = delete; Button(Button&&) = delete; ~Button() override = default; diff --git a/inc/basic_widgets/w/caption.hpp b/inc/basic_widgets/w/caption.hpp index 13f6c0b..47b8cfa 100644 --- a/inc/basic_widgets/w/caption.hpp +++ b/inc/basic_widgets/w/caption.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include @@ -11,7 +11,7 @@ struct SDL_Surface; namespace bwidgets { - class Caption : public Widget, + class Caption : public WidgetImpl, public FontHandler, public TextureHandler { @@ -43,7 +43,7 @@ namespace bwidgets Alignment alignment {Alignment::LEFT}; Size margins {3, 3}; - Caption(Widget* parent = nullptr) noexcept; + Caption(WidgetImpl* parent = nullptr) noexcept; Caption(const Caption&) = delete; Caption(Caption&&) = delete; ~Caption() override = default; diff --git a/inc/basic_widgets/w/numeric_input.hpp b/inc/basic_widgets/w/numeric_input.hpp index 3fa8a74..b9acf8c 100644 --- a/inc/basic_widgets/w/numeric_input.hpp +++ b/inc/basic_widgets/w/numeric_input.hpp @@ -47,19 +47,19 @@ namespace bwidgets const int spacing = center_line(button_area_width, widest_button); const int field_width = vp.w - 2 * button_area_width; - Widget::_widget_area.x = Widget::_widget_area.x + button_area_width; - Widget::_widget_area.w = field_width; + WidgetImpl::_widget_area.x = WidgetImpl::_widget_area.x + button_area_width; + WidgetImpl::_widget_area.w = field_width; Input::_input_caption.viewport( - rect_offset(rect_margin(Widget::_widget_area, + rect_offset(rect_margin(WidgetImpl::_widget_area, {Input::border_width, Input::border_width}), vp)); - _decrement_button_area = {spacing, Widget::_widget_area.y, + _decrement_button_area = {spacing, WidgetImpl::_widget_area.y, _decrement_button.size().w, _decrement_button.size().h}; _increment_button_area = {vp.w - spacing - _increment_button.size().w, - Widget::_widget_area.y, _increment_button.size().w, + WidgetImpl::_widget_area.y, _increment_button.size().w, _increment_button.size().h}; _increment_button.viewport(rect_offset(_increment_button_area, vp)); @@ -83,7 +83,7 @@ namespace bwidgets public: T button_step = 1; - NumericInput(Widget* parent = nullptr) + NumericInput(WidgetImpl* parent = nullptr) : Input(parent), _increment_button(this), _decrement_button(this) { Input::_input_caption.alignment = Caption::Alignment::RIGHT; diff --git a/src/w/base/widget.cpp b/src/w/base/widget.cpp index c96be7c..11a9d65 100644 --- a/src/w/base/widget.cpp +++ b/src/w/base/widget.cpp @@ -1,10 +1,10 @@ -#include +#include using namespace bwidgets; -auto Widget::render() -> Widget* +void WidgetImpl::render() { - if (_renderer == nullptr) return this; + if (_renderer == nullptr) return; #ifdef BWIDGETS_DEBUG _renderer @@ -17,27 +17,23 @@ auto Widget::render() -> Widget* _renderer->viewport(_viewport); _handle_rendering(); - - return this; } -auto Widget::renderer(std::shared_ptr r) -> Widget* +void WidgetImpl::renderer(std::shared_ptr r) { if (r != _renderer) { _handle_renderer_change(r); _renderer = r; } - return this; } -auto Widget::viewport(const SDL_Rect& vp) -> Widget* +void WidgetImpl::viewport(const SDL_Rect& vp) { _handle_geometry_change(vp); _viewport = vp; - return this; } -auto Widget::viewport() const -> const SDL_Rect& +auto WidgetImpl::viewport() const -> const SDL_Rect& { return _viewport; } diff --git a/src/w/button.cpp b/src/w/button.cpp index 1d6bc43..ab14076 100644 --- a/src/w/button.cpp +++ b/src/w/button.cpp @@ -11,7 +11,7 @@ const Color Button::default_color_bg {150, 150, 150, SDL_ALPHA_OPAQUE}; const Color Button::default_color_bg_hover {175, 175, 175, SDL_ALPHA_OPAQUE}; const Color Button::default_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE}; -Button::Button(Widget* parent) noexcept : Widget {parent}, _caption {this} +Button::Button(WidgetImpl* parent) noexcept : WidgetImpl {parent}, _caption {this} { enable_mouse_handler(&_widget_area, &_viewport); _caption.alignment = Caption::Alignment::CENTER; diff --git a/src/w/caption.cpp b/src/w/caption.cpp index 8019a8c..a9b02ca 100644 --- a/src/w/caption.cpp +++ b/src/w/caption.cpp @@ -7,7 +7,7 @@ using namespace bwidgets; -Caption::Caption(Widget* parent) noexcept : Widget {parent} +Caption::Caption(WidgetImpl* parent) noexcept : WidgetImpl {parent} { _font_color_bg = default_font_color_bg; _font_color_fg = default_font_color_fg;