From 3f15d1d857c28531309fe214cd5c2ee1e46d6761 Mon Sep 17 00:00:00 2001 From: Andrea Blankenstijn Date: Wed, 26 Jan 2022 22:49:41 +0100 Subject: [PATCH] scrollable (texture) area works --- examples/button_example.cpp | 2 +- examples/caption_example.cpp | 2 +- examples/example_example.cpp | 2 +- examples/input_example.cpp | 2 +- examples/run.hpp | 11 ++++--- examples/scrollable_area_example.cpp | 6 +++- examples/scrollbar_example.cpp | 14 ++++++-- inc/basic_widgets/w/scrollbar.hpp | 12 ++++--- inc/basic_widgets/w/scrollbar_impl.hpp | 23 +++++++------ src/w/scrollable_area_impl.cpp | 25 ++++++++++++++ src/w/scrollbar_impl.cpp | 45 ++++++++++++++++---------- 11 files changed, 99 insertions(+), 45 deletions(-) diff --git a/examples/button_example.cpp b/examples/button_example.cpp index 56f70e9..0a23c86 100644 --- a/examples/button_example.cpp +++ b/examples/button_example.cpp @@ -8,7 +8,7 @@ auto main() -> int { run_example( []() { return bwidgets::create_button(); }, - [](auto w, auto f, auto x, auto y) { + [](auto w, auto f, auto x, auto y, auto) { w->click_handler([x, y](const SDL_MouseButtonEvent&) { std::cout << "button(" << x << ',' << y << "):click!" << std::endl; }); diff --git a/examples/caption_example.cpp b/examples/caption_example.cpp index 1842afb..7033f39 100644 --- a/examples/caption_example.cpp +++ b/examples/caption_example.cpp @@ -5,7 +5,7 @@ auto main() -> int { run_example([]() { return bwidgets::create_caption(); }, - [](auto w, auto f, auto, auto) { + [](auto w, auto f, auto, auto, auto) { w->alignment( bwidgets::Caption::Alignment::CENTER); w->text("¡jello!"); diff --git a/examples/example_example.cpp b/examples/example_example.cpp index 28f54e0..e82bbcb 100644 --- a/examples/example_example.cpp +++ b/examples/example_example.cpp @@ -5,7 +5,7 @@ auto main() -> int { run_example( []() { return std::make_unique(); }, - [](auto w, auto, auto x, auto y) { + [](auto w, auto, auto x, auto y, auto) { w->cycle_r = (x + 1) * 3000; // NOLINT(readability-magic-numbers) w->cycle_b = (y + 1) * 3000; // NOLINT(readability-magic-numbers) w->cycle_b = (1 + x + y) * (y + 1) * 400; // NOLINT(readability-magic-numbers) diff --git a/examples/input_example.cpp b/examples/input_example.cpp index 594bd54..07f0a3b 100644 --- a/examples/input_example.cpp +++ b/examples/input_example.cpp @@ -6,7 +6,7 @@ auto main() -> int { run_example>( []() { return bwidgets::create_input_float(); }, - [](auto w, auto f, auto, auto) { + [](auto w, auto f, auto, auto, auto) { w->font(f); w->button_step = 0.5; // NOLINT(readability-magic-numbers) w->value_range(-3.14, 8.5); // NOLINT(readability-magic-numbers) diff --git a/examples/run.hpp b/examples/run.hpp index 810401f..f89d32a 100644 --- a/examples/run.hpp +++ b/examples/run.hpp @@ -11,10 +11,11 @@ template concept WidgetType = std::derived_from; template -void run_example( - std::function()> factory, - std::function, int, int)> setup, - int w = 3, int h = 3) +void run_example(std::function()> factory, + std::function&, int, int, + const std::shared_ptr)> + setup, + int w = 3, int h = 3) { std::atexit([]() { TTF_Quit(); @@ -43,7 +44,7 @@ void run_example( auto col = bwidgets::create_vertical_layout(); for (auto y = 0; y < h; y++) { auto widget = factory(); - setup(widget.get(), font, x, y); + setup(widget.get(), font, x, y, renderer); col->add_widget(std::move(widget)); } layout->add_widget(std::move(col)); diff --git a/examples/scrollable_area_example.cpp b/examples/scrollable_area_example.cpp index b58bf6d..19bda1d 100644 --- a/examples/scrollable_area_example.cpp +++ b/examples/scrollable_area_example.cpp @@ -1,3 +1,4 @@ +#include #include #include "run.hpp" @@ -6,6 +7,9 @@ auto main() -> int { run_example( []() { return bwidgets::create_scrollable_area(); }, - [](auto w, auto f, auto x, auto y) { w->font(f); }); + [](auto w, auto f, auto x, auto y, auto r) { + w->font(f); + w->texture(bwidgets::filled_circle({255, 0, 0, 255}, 300, *r)); + }); return 0; } diff --git a/examples/scrollbar_example.cpp b/examples/scrollbar_example.cpp index 0adb1e4..eacce47 100644 --- a/examples/scrollbar_example.cpp +++ b/examples/scrollbar_example.cpp @@ -1,11 +1,19 @@ +#include + #include #include "run.hpp" auto main() -> int { - run_example( - []() { return bwidgets::create_scrollable_area(); }, - [](auto w, auto f, auto x, auto y) {}); + run_example([]() { return bwidgets::create_scrollbar(); }, + [](auto w, auto f, auto x, auto y, auto) { + w->font(f); + w->on_scroll([x, y](const float v) { + std::cout << "Scroll(" << x << "," << y + << "): " << v * 100 << "%" + << std::endl; + }); + }); return 0; } diff --git a/inc/basic_widgets/w/scrollbar.hpp b/inc/basic_widgets/w/scrollbar.hpp index ff5b7eb..fb09c72 100644 --- a/inc/basic_widgets/w/scrollbar.hpp +++ b/inc/basic_widgets/w/scrollbar.hpp @@ -20,11 +20,13 @@ namespace bwidgets VERTICAL }; - virtual void on_scroll(std::function) = 0; - [[nodiscard]] virtual auto orientation() const -> Orientation = 0; - virtual void orientation(Orientation) = 0; - [[nodiscard]] virtual auto value() const -> int = 0; - virtual void value(int) = 0; + virtual void on_scroll(std::function) = 0; + [[nodiscard]] virtual auto orientation() const -> Orientation = 0; + virtual void orientation(Orientation) = 0; + [[nodiscard]] virtual auto step() const -> float = 0; + virtual void step(float) = 0; + [[nodiscard]] virtual auto value() const -> float = 0; + virtual void value(float) = 0; }; } diff --git a/inc/basic_widgets/w/scrollbar_impl.hpp b/inc/basic_widgets/w/scrollbar_impl.hpp index 9447d66..cf1d8c3 100644 --- a/inc/basic_widgets/w/scrollbar_impl.hpp +++ b/inc/basic_widgets/w/scrollbar_impl.hpp @@ -22,22 +22,25 @@ namespace bwidgets auto cursor_area() const -> SDL_Rect; void handle_event(const SDL_Event&) override; - void on_scroll(std::function) override; + void on_scroll(std::function) override; [[nodiscard]] auto orientation() const -> Orientation override; void orientation(Orientation) override; [[nodiscard]] auto size() const noexcept -> Size override; - [[nodiscard]] auto value() const -> int override; - void value(int) override; + [[nodiscard]] auto step() const -> float override; + void step(float) override; + [[nodiscard]] auto value() const -> float override; + void value(float) override; protected: - SDL_Rect _bar_area; - std::unique_ptr