aboutsummaryrefslogtreecommitdiff
path: root/include/gui/FileChooser.hpp
blob: 36dac2d423ca1dd8465cb5b1e8af1a46ce3a945e (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once

#include "Widget.hpp"
#include "ScrollablePage.hpp"
#include <functional>
#include <vector>

#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Sprite.hpp>
#include <mglpp/system/Clock.hpp>

// This currently only supports displaying folders
// TODO: Support files as well

namespace gsr {
    class FileChooser;

    class FileChooserBody : public Widget {
        friend class FileChooser;
    public:
        FileChooserBody(FileChooser *file_chooser, mgl::vec2f size);
        FileChooserBody(const FileChooserBody&) = delete;
        FileChooserBody& operator=(const FileChooserBody&) = 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);
    private:
        void set_current_directory(const char *directory);
    private:
        struct Folder {
            mgl::Text text;
            time_t last_modified_seconds = 0;
        };

        FileChooser *file_chooser = nullptr;
        mgl::vec2f size;
        mgl::vec2f inner_size;
        std::vector<Folder> folders;
        int mouse_over_item = -1;
        int selected_item = -1;
        mgl::Clock double_click_timer;
        int times_clicked_within_timer = 0;
    };

    class FileChooser : public Widget {
    public:
        FileChooser(const char *start_directory, mgl::vec2f size);
        FileChooser(const FileChooser&) = delete;
        FileChooser& operator=(const FileChooser&) = 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;

        void set_current_directory(const char *directory);
        void open_subdirectory(const char *name);
        void open_parent_directory();
        const std::string& get_current_directory() const;
    private:
        struct Folder {
            mgl::Text text;
            time_t last_modified_seconds = 0;
        };

        mgl::vec2f size;
        mgl::Text current_directory_text;
        mgl::Sprite up_arrow_sprite;
        ScrollablePage scrollable_page;
        FileChooserBody *file_chooser_body_ptr = nullptr;
    };
}