aboutsummaryrefslogtreecommitdiff
path: root/include/mglpp/window/Window.hpp
blob: b4975b47d0ec2dc79714f31c5526f7be4c9cc394 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef MGLPP_WINDOW_HPP
#define MGLPP_WINDOW_HPP

#include "../graphics/PrimitiveType.hpp"
#include "../graphics/Color.hpp"
#include "../system/vec.hpp"
#include "Keyboard.hpp"
#include "Mouse.hpp"
#include <stddef.h>
#include <string>
#include <functional>

extern "C" {
#include <mgl/window/window.h>
}

namespace mgl {
    typedef mgl_window_handle WindowHandle;

    class Event;
    class Drawable;
    class Shader;
    struct Vertex;

    struct View {
        vec2i position;
        vec2i size;
    };

    /*
        Return true to continue. |get_clipboard| returns false if this returns false.
        Note: |size| is the size of the current data, not the total data (if the callback only contains a part of the data).
    */
    using ClipboardCallback = std::function<bool(const unsigned char *data, size_t size, mgl_clipboard_type clipboard_type)>;

    class Window {
    public:
        /* Has to match mgl_window_create_params */
        struct CreateParams {
            mgl::vec2i position;
            mgl::vec2i size;
            mgl::vec2i min_size; /* (0, 0) = no limit */
            mgl::vec2i max_size; /* (0, 0) = no limit */
            WindowHandle parent_window = 0; /* 0 = root window */
            bool hidden = false;
            bool override_redirect = false;
            bool support_alpha = false; /* support alpha for the window */
        };

        Window();
        Window(const Window&) = delete;
        Window& operator=(const Window&) = delete;
        ~Window();

        bool create(const char *title, CreateParams create_params);
        // Initialize this window from an existing window
        bool create(WindowHandle existing_window);

        bool poll_event(Event &event);

        void clear(Color color = Color(0, 0, 0, 255));
        void draw(Drawable &drawable, Shader *shader = nullptr);
        void draw(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Shader *shader = nullptr);
        void display();

        void set_visible(bool visible);
        bool is_open() const;
        bool has_focus() const;
        void close();
        void set_title(const char *title);
        // 0 = no fps limit, or limit fps to vsync if vsync is enabled
        void set_framerate_limit(unsigned int fps);
        void set_key_repeat_enabled(bool enabled);
        void set_cursor_visible(bool visible);
        vec2i get_size() const;
        void set_size(mgl::vec2i size);
        void set_position(mgl::vec2i position);
        /* if |minimum| is (0, 0) then there is no minimum limit, if |maximum| is (0, 0) then there is no maximum limit */
        void set_size_limits(mgl::vec2i minimum, mgl::vec2i maximum);
        vec2i get_mouse_position() const;
        
        /*
            This should be called every frame to retain the view.
            Make sure to set the view back to the previous view after rendering items
            by saving the previous view with |get_view| and then call
            |set_view| with that saved view.
        */
        void set_view(const View &new_view);
        View get_view();

        bool is_key_pressed(Keyboard::Key key) const;
        bool is_mouse_button_pressed(Mouse::Button button) const;

        void set_clipboard(const std::string &str);
        bool get_clipboard(ClipboardCallback callback);
        std::string get_clipboard_string();

        WindowHandle get_system_handle() const;
    private:
        mgl_window window;
    };
}

#endif /* MGLPP_WINDOW_HPP */