aboutsummaryrefslogtreecommitdiff
path: root/include/gui/ScrollablePage.hpp
blob: 452d0e9763c81948747d87e24dd0b81df9458e62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once

#include "Widget.hpp"
#include "../SafeVector.hpp"
#include <memory>

#include <mglpp/system/FloatRect.hpp>

namespace gsr {
    class ScrollablePage : public Widget {
    public:
        ScrollablePage(mgl::vec2f size);
        ScrollablePage(const ScrollablePage&) = delete;
        ScrollablePage& operator=(const ScrollablePage&) = delete;

        bool on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) override;
        void draw(mgl::Window &window, mgl::vec2f offset) override;

        mgl::vec2f get_size() override;
        mgl::vec2f get_inner_size() override;
        void set_size(mgl::vec2f size);

        void add_widget(std::unique_ptr<Widget> widget);

        void reset_scroll();
    private:
        void apply_animation();
        void limit_scroll(double child_height);
        void limit_scroll_cursor(mgl::Window &window, double child_height, double scrollbar_empty_space);
        void draw_scrollbar();
        float get_scrollbar_width() const;
    private:
        mgl::vec2f size;
        SafeVector<std::unique_ptr<Widget>> widgets;
        int scroll_target_y = 0;
        double scroll_y = 0.0;
        mgl::FloatRect scrollbar_rect;
        bool moving_scrollbar_with_cursor = false;
        mgl::vec2f scrollbar_move_cursor_start_pos;
        double scrollbar_move_cursor_scroll_y_start = 0.0;
    };
}