button interface-api separation

This commit is contained in:
Andrea Blankenstijn 2021-08-17 11:40:03 +02:00
parent 0eb300ceeb
commit fbaabd5aa7
8 changed files with 71 additions and 62 deletions

View File

@ -1,14 +1,14 @@
#include <iostream>
#include <basic_widgets/w/button.hpp>
#include <basic_widgets/w/button_impl.hpp>
#include "run.hpp"
using bwidgets::Button;
using bwidgets::ButtonImpl;
auto main() -> int
{
run_example<Button>([](auto w, auto f, auto x, auto y) {
run_example<ButtonImpl>([](auto w, auto f, auto x, auto y) {
w->click_handler([x, y](const SDL_MouseButtonEvent&) {
std::cout << "button(" << x << ',' << y << "):click!" << std::endl;
});

View File

@ -5,53 +5,30 @@
#include <string>
#include <basic_widgets/core/type/color.hpp>
#include <basic_widgets/w/caption_impl.hpp>
#include <basic_widgets/w/feat/mouse_handler_impl.hpp>
#include <basic_widgets/w/caption.hpp>
#include <basic_widgets/w/feat/mouse_handler.hpp>
namespace bwidgets
{
class Button : public virtual WidgetImpl,
class Button : public virtual Widget,
public virtual FontHandler,
public virtual MouseHandlerImpl
public virtual MouseHandler
{
protected:
CaptionImpl _caption;
SDL_Rect _caption_area {};
Color _color_foreground = default_color_fg;
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 _on_push(bool) override;
public:
static const Color default_color_bg;
static const Color default_color_bg_hover;
static const Color default_color_fg;
static inline const Color default_color_bg {150, 150, 150, SDL_ALPHA_OPAQUE};
static inline const Color default_color_bg_hover {175, 175, 175,
SDL_ALPHA_OPAQUE};
static inline const Color default_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
std::function<Color(int, int, float)> border_gradient;
Size border_size {3, 3};
Color color_bg = default_color_bg;
Color color_bg_hover = default_color_bg_hover;
using EventHandlerImpl::handle_event;
using FocusHandlerImpl::focus;
using FocusHandlerImpl::focus_handler;
using Widget::Widget;
Button(WidgetImpl* parent = nullptr) noexcept;
Button(const Button&) = delete;
Button(Button&&) = delete;
~Button() override = default;
auto operator=(const Button&) = delete;
auto operator=(Button&&) = delete;
[[nodiscard]] auto size() const noexcept -> Size override;
[[nodiscard]] virtual auto text() const noexcept -> const std::string&;
virtual auto text(const std::string&) -> Button*;
[[nodiscard]] virtual auto text() const noexcept -> const std::string& = 0;
virtual void text(const std::string&) = 0;
};
}

View File

@ -0,0 +1,37 @@
#ifndef BWIDGETS_BUTTON_IMPL_HPP
#define BWIDGETS_BUTTON_IMPL_HPP
#include <basic_widgets/w/base/widget_impl.hpp>
#include <basic_widgets/w/button.hpp>
#include <basic_widgets/w/caption_impl.hpp>
#include <basic_widgets/w/feat/mouse_handler_impl.hpp>
namespace bwidgets
{
class ButtonImpl : public virtual Button,
public virtual WidgetImpl,
public virtual MouseHandlerImpl
{
public:
ButtonImpl(Widget* parent = nullptr) noexcept;
[[nodiscard]] auto size() const noexcept -> Size override;
[[nodiscard]] auto text() const noexcept -> const std::string& override;
void text(const std::string&) override;
protected:
CaptionImpl _caption;
SDL_Rect _caption_area {};
Color _color_foreground = default_color_fg;
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 _on_push(bool) override;
};
}
#endif

View File

@ -7,7 +7,7 @@
namespace bwidgets
{
class MouseHandlerImpl : public MouseHandler,
class MouseHandlerImpl : public virtual MouseHandler,
virtual public EventHandlerImpl,
virtual public FocusHandlerImpl
{

View File

@ -7,7 +7,7 @@
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/core/type/concepts.hpp>
#include <basic_widgets/w/base/input.hpp>
#include <basic_widgets/w/button.hpp>
#include <basic_widgets/w/button_impl.hpp>
namespace bwidgets
{
@ -15,9 +15,9 @@ namespace bwidgets
class NumericInput : public Input<T>
{
protected:
Button _increment_button;
ButtonImpl _increment_button;
SDL_Rect _increment_button_area {};
Button _decrement_button;
ButtonImpl _decrement_button;
SDL_Rect _decrement_button_area {};
std::pair<T, T> _value_range {std::numeric_limits<T>::lowest(),
std::numeric_limits<T>::max()};

View File

@ -30,7 +30,7 @@ libbasic_widgets = static_library('basic_widgets',
'src/core/texture.cpp',
'src/w/base/layout.cpp',
'src/w/base/widget.cpp',
'src/w/button.cpp',
'src/w/button_impl.cpp',
'src/w/caption_impl.cpp',
'src/w/feat/event_handler_impl.cpp',
'src/w/feat/keyboard_handler_impl.cpp',

View File

@ -3,58 +3,53 @@
#include <SDL2/SDL_render.h>
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/w/button.hpp>
#include <basic_widgets/w/button_impl.hpp>
using namespace bwidgets;
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(WidgetImpl* parent) noexcept : WidgetImpl {parent}, _caption {this}
ButtonImpl::ButtonImpl(Widget* parent) noexcept : WidgetImpl {parent}, _caption {this}
{
enable_mouse_handler(&_widget_area, &_viewport);
MouseHandlerImpl::enable_mouse_handler(&_widget_area, &_viewport);
_caption.alignment = Caption::Alignment::CENTER;
_caption.render_mode(Font::RenderMode::BLENDED);
border_gradient = [this](int len, int pos, float divider) -> Color {
const auto& end_color = hovered() ? color_bg_hover : color_bg;
const auto& end_color = MouseHandlerImpl::hovered() ? color_bg_hover : color_bg;
const auto start_color = end_color / divider;
const auto factor = linear(pos, 0, len);
return lerp(start_color, end_color, factor);
};
}
auto Button::size() const noexcept -> Size
auto ButtonImpl::size() const noexcept -> Size
{
return _caption.size() + border_size * 2;
}
auto Button::text() const noexcept -> const std::string&
auto ButtonImpl::text() const noexcept -> const std::string&
{
return _caption.text();
}
auto Button::text(const std::string& txt) -> Button*
void ButtonImpl::text(const std::string& txt)
{
_caption.text(txt);
_handle_geometry_change(_viewport);
return this;
}
void Button::_handle_font_change(const std::shared_ptr<Font>& f)
void ButtonImpl::_handle_font_change(const std::shared_ptr<Font>& f)
{
_caption.font(f);
_handle_geometry_change(_viewport);
}
void Button::_handle_font_color_change(Color fg, Color)
void ButtonImpl::_handle_font_color_change(Color fg, Color)
{
_caption.font_color_fg(fg);
}
void Button::_handle_geometry_change(const SDL_Rect& vp)
void ButtonImpl::_handle_geometry_change(const SDL_Rect& vp)
{
const auto h = _caption.size().h + 2 * border_size.h;
@ -67,15 +62,15 @@ void Button::_handle_geometry_change(const SDL_Rect& vp)
_caption.viewport(rect_offset(_caption_area, vp));
}
void Button::_handle_renderer_change(const std::shared_ptr<Renderer>& r)
void ButtonImpl::_handle_renderer_change(const std::shared_ptr<Renderer>& r)
{
_caption.renderer(r);
}
void Button::_handle_rendering()
void ButtonImpl::_handle_rendering()
{
Color c = hovered() ? color_bg_hover : color_bg;
const auto divider = pushed() ? 1.5 : 2;
Color c = MouseHandlerImpl::hovered() ? color_bg_hover : color_bg;
const auto divider = MouseHandlerImpl::pushed() ? 1.5 : 2;
auto x = 0;
auto y = 0;
const auto biggest = border_size.w > border_size.h ? border_size.w : border_size.h;
@ -93,7 +88,7 @@ void Button::_handle_rendering()
_caption.render();
}
void Button::_on_push(bool state)
void ButtonImpl::_on_push(bool state)
{
SDL_Point offset {_viewport.x, _viewport.y};
if (state) {