changes… and forgotten file

This commit is contained in:
Andrea Blankenstijn 2022-02-01 20:48:54 +01:00
parent 6149c48664
commit 1294806696
13 changed files with 107 additions and 73 deletions

View file

@ -29,7 +29,12 @@ namespace bwidgets
_handle_geometry_change(viewport());
}
void for_widgets(const std::function<void(Widget*)>& f)
void for_widgets(const std::function<void(Widget*)>& f) override
{
for (const auto& w : _widgets) f(w.get());
}
void for_widgets(const std::function<void(const Widget*)>& f) const override
{
for (const auto& w : _widgets) f(w.get());
}
@ -40,10 +45,10 @@ namespace bwidgets
Size min_size {0, 0};
if constexpr (alignment == LayoutAlignment::HORIZONTAL) {
for (const auto& w : _widgets) {
for_widgets([&min_size](const Widget* w) {
if (w->size().w > min_size.w) min_size.w = w->size().w;
if (w->size().h > min_size.h) min_size.h = w->size().h;
}
});
return {(int)_widgets.size() * min_size.w
+ ((int)_widgets.size() + 1) * theme()->size_layout_margin().w,
@ -51,11 +56,11 @@ namespace bwidgets
}
// Vertical
for (const auto& w : _widgets) {
for_widgets([&min_size](const Widget* w) {
if (w->size().w > min_size.w) min_size.w = w->size().w;
min_size.h += w->size().h;
}
});
return {min_size.w + 2 * theme()->size_layout_margin().w,
min_size.h

View file

@ -8,14 +8,12 @@ namespace bwidgets
{
class Layout : public virtual Widget
{
protected:
using Widget::Widget;
public:
// Add widget to the layout
virtual void add_widget(std::unique_ptr<Widget>) = 0;
virtual void add_widget(std::unique_ptr<Widget>) = 0;
// Apply a function to every layout widget.
virtual void for_widgets(const std::function<void(Widget*)>&) = 0;
virtual void for_widgets(const std::function<void(Widget*)>&) = 0;
virtual void for_widgets(const std::function<void(const Widget*)>&) const = 0;
};
}

View file

@ -22,8 +22,6 @@ namespace bwidgets
void handle_event(const SDL_Event&) override;
protected:
std::vector<std::unique_ptr<Widget>> _widgets;
void _handle_renderer_change(const std::shared_ptr<Renderer>&) override;
void _handle_rendering() override;
void _handle_theme_change(const std::shared_ptr<Theme>&) override;

View file

@ -28,7 +28,7 @@ namespace bwidgets
SDL_Rect _mouse_area;
void _handle_font_change(const std::shared_ptr<Font>&) override;
void _handle_font_color_change(Color, Color) override;
void _handle_font_color_change(const Color&, const Color&) override;
void _handle_geometry_change(const SDL_Rect&) override;
void _handle_renderer_change(const std::shared_ptr<Renderer>&) override;
void _handle_rendering() override;

View file

@ -24,6 +24,7 @@ namespace bwidgets
// Text horizontal alignment on the caption viewport.
[[nodiscard]] virtual auto alignment() const -> Alignment = 0;
virtual void alignment(Alignment) = 0;
[[nodiscard]] virtual auto render_mode() const -> Font::RenderMode = 0;
// Set caption text rendering mode.
virtual void render_mode(Font::RenderMode) = 0;
// Get caption text.

View file

@ -7,21 +7,22 @@
namespace bwidgets
{
class CaptionImpl : public virtual Caption,
public virtual FontHandlerImpl,
public virtual WidgetImpl
class CaptionImpl final : public virtual Caption,
public virtual FontHandlerImpl,
public virtual WidgetImpl
{
public:
CaptionImpl(Widget* p = nullptr) noexcept;
using WidgetImpl::WidgetImpl;
[[nodiscard]] auto alignment() const -> Alignment override;
void alignment(Alignment) override;
[[nodiscard]] auto render_mode() const -> Font::RenderMode override;
void render_mode(Font::RenderMode) override;
[[nodiscard]] auto size() const noexcept -> Size override;
[[nodiscard]] auto text() const noexcept -> std::string_view override;
void text(std::string) override;
protected:
private:
Alignment _alignment;
Font::RenderMode _render_mode {Font::RenderMode::SHADED};
std::string _text;
@ -29,7 +30,7 @@ namespace bwidgets
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_font_color_change(const Color&, const Color&) override;
void _handle_geometry_change(const SDL_Rect&) override;
void _handle_renderer_change(const std::shared_ptr<Renderer>&) override;
void _handle_rendering() override;

View file

@ -18,13 +18,16 @@ namespace bwidgets
auto operator=(FontHandler&&) = delete;
virtual ~FontHandler() noexcept = default;
[[nodiscard]] virtual auto font() const -> const std::shared_ptr<Font>& = 0;
// Set the used font.
virtual void font(const std::shared_ptr<Font>& f) = 0;
virtual void font(std::shared_ptr<Font>) = 0;
[[nodiscard]] virtual auto font_color_bg() const -> const Color& = 0;
// Set background color used for shaded text rendering
// mode.
virtual void font_color_bg(Color c) = 0;
virtual void font_color_bg(const Color&) = 0;
[[nodiscard]] virtual auto font_color_fg() const -> const Color& = 0;
// Set foreground (glyphs) color.
virtual void font_color_fg(Color c) = 0;
virtual void font_color_fg(const Color&) = 0;
protected:
FontHandler() noexcept = default;

View file

@ -8,46 +8,23 @@ namespace bwidgets
class FontHandlerImpl : public virtual FontHandler
{
public:
void font(const std::shared_ptr<Font>& f) final
{
if (f
&& (f != _font || f->family_name != _font->family_name
|| f->height != _font->height || f->hinting() != _font->hinting()
|| f->kerning() != _font->kerning()
|| f->outline() != _font->outline()
|| f->style_name != _font->style_name))
{
_handle_font_change(f);
_font = f;
}
}
void font_color_bg(const Color c) final
{
if (c != _font_color_bg) {
_handle_font_color_change(_font_color_fg, c);
_font_color_bg = c;
}
}
void font_color_fg(const Color c) final
{
if (c != _font_color_fg) {
_handle_font_color_change(c, _font_color_fg);
_font_color_fg = c;
}
}
[[nodiscard]] auto font() const -> const std::shared_ptr<Font>& override;
void font(std::shared_ptr<Font>) override;
[[nodiscard]] auto font_color_bg() const -> const Color& override;
void font_color_bg(const Color&) override;
[[nodiscard]] auto font_color_fg() const -> const Color& override;
void font_color_fg(const Color&) override;
protected:
// Called on font changes.
virtual void _handle_font_change(const std::shared_ptr<Font>&) = 0;
// Called on font color changes.
virtual void _handle_font_color_change(const Color&, const Color&) = 0;
private:
std::shared_ptr<Font> _font;
Color _font_color_bg = default_font_color_bg;
Color _font_color_fg = default_font_color_fg;
Font::RenderMode _font_render_mode {Font::RenderMode::SHADED};
// Called on font changes.
virtual void _handle_font_change(const std::shared_ptr<Font>&) = 0;
// Called on font color changes. TODO: remove that, use theme
virtual void _handle_font_color_change(Color, Color) {}
};
}

View file

@ -34,6 +34,7 @@ libbasic_widgets = static_library('basic_widgets',
'src/w/caption_impl.cpp',
'src/w/default_theme.cpp',
'src/w/feat/event_handler_impl.cpp',
'src/w/feat/font_handler_impl.cpp',
'src/w/feat/keyboard_handler_impl.cpp',
'src/w/feat/mouse_handler_impl.cpp',
'src/w/scrollable_area_impl.cpp',

View file

@ -1,5 +1,4 @@
#include <basic_widgets/w/base/widget_impl.hpp>
#include <basic_widgets/w/default_theme.hpp>
using namespace bwidgets;

View file

@ -38,7 +38,7 @@ void ButtonImpl::_handle_font_change(const std::shared_ptr<Font>& f)
_handle_geometry_change(viewport());
}
void ButtonImpl::_handle_font_color_change(const Color fg, Color)
void ButtonImpl::_handle_font_color_change(const Color& fg, const Color&)
{
_caption->font_color_fg(fg);
}

View file

@ -5,18 +5,21 @@
using namespace bwidgets;
CaptionImpl::CaptionImpl(Widget* parent) noexcept : WidgetImpl {parent} {}
auto CaptionImpl::alignment() const -> Alignment
{
return _alignment;
}
void CaptionImpl::alignment(Alignment a)
void CaptionImpl::alignment(const Alignment a)
{
_alignment = a;
}
auto CaptionImpl::render_mode() const -> Font::RenderMode
{
return _render_mode;
}
void CaptionImpl::render_mode(const Font::RenderMode m)
{
if (m != _render_mode) {
@ -27,9 +30,9 @@ void CaptionImpl::render_mode(const Font::RenderMode m)
auto CaptionImpl::size() const noexcept -> Size
{
if (!_font) return {0, 0};
if (!font()) return {0, 0};
return _font->text_size(_text);
return font()->text_size(_text);
}
auto CaptionImpl::text() const noexcept -> std::string_view
@ -50,10 +53,10 @@ void CaptionImpl::_handle_font_change(const std::shared_ptr<Font>&)
_text_texture.reset();
}
void CaptionImpl::_handle_font_color_change(const Color fg, const Color bg)
void CaptionImpl::_handle_font_color_change(const Color& fg, const Color& bg)
{
if (fg != _font_color_fg
|| bg != _font_color_bg && _font_render_mode == Font::RenderMode::SHADED)
if (fg != font_color_fg()
|| (bg != font_color_bg() && render_mode() == Font::RenderMode::SHADED))
{
_text_texture.reset();
}
@ -84,7 +87,7 @@ void CaptionImpl::_handle_rendering()
// shaded text rendering mode.
if (_render_mode == Font::RenderMode::SHADED) {
renderer()
->draw_color(_font_color_bg)
->draw_color(font_color_bg())
->fill_rect({0, 0, viewport().w, viewport().h});
}
@ -112,16 +115,16 @@ void CaptionImpl::_handle_rendering()
void CaptionImpl::_handle_texture_update()
{
if (!_font) return;
if (!font()) return;
_text_texture.reset();
auto s = [this]() {
switch (_render_mode) {
switch (render_mode()) {
case Font::RenderMode::SHADED:
return _font->render(_render_mode, _text, _font_color_fg,
_font_color_bg);
return font()->render(_render_mode, _text, font_color_fg(),
font_color_bg());
default:
return _font->render(_render_mode, _text, _font_color_fg);
return font()->render(_render_mode, _text, font_color_fg());
}
}();
_text_texture = std::make_shared<Texture>(*renderer(), s.get());

View file

@ -0,0 +1,48 @@
#include <basic_widgets/w/feat/font_handler_impl.hpp>
using namespace bwidgets;
using namespace std;
auto FontHandlerImpl::font() const -> const shared_ptr<Font>&
{
return _font;
}
void FontHandlerImpl::font(shared_ptr<Font> f)
{
if (f
&& (f != _font || f->family_name != _font->family_name
|| f->height != _font->height || f->hinting() != _font->hinting()
|| f->kerning() != _font->kerning() || f->outline() != _font->outline()
|| f->style_name != _font->style_name))
{
_handle_font_change(f);
_font = std::move(f);
}
}
auto FontHandlerImpl::font_color_bg() const -> const Color&
{
return _font_color_bg;
}
void FontHandlerImpl::font_color_bg(const Color& c)
{
if (c != _font_color_bg) {
_handle_font_color_change(_font_color_fg, c);
_font_color_bg = c;
}
}
auto FontHandlerImpl::font_color_fg() const -> const Color&
{
return _font_color_fg;
}
void FontHandlerImpl::font_color_fg(const Color& c)
{
if (c != _font_color_fg) {
_handle_font_color_change(c, _font_color_fg);
_font_color_fg = c;
}
}