summaryrefslogtreecommitdiff
path: root/src/gui/window.hpp
blob: eb6216e8c6bebfef7cb23a1e4ee21d41131ddb77 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 Leo Tenenbaum
// This file is part of GraphColoring.
//
// GraphColoring is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GraphColoring is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GraphColoring.  If not, see <https://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////////////

#ifndef GRAPHCOLORING_GUI_WINDOW_H_
#define GRAPHCOLORING_GUI_WINDOW_H_

#include <cstdint>
#include <functional>
#include <vector>
#include <map>

#include <gtk/gtk.h>

#include "colors.hpp"
#include "position.hpp"

namespace gui {


class Window
{
public:
	typedef std::function<void(Window*)> callback_t; // General 0-parameter callbacks.
	typedef std::map<guint, std::vector<callback_t>> callback_map_t;
	typedef std::function<void(Window*,int,int)> mouse_callback_t; // Mouse callbacks take x and y coordinates.
	typedef std::map<guint, std::vector<mouse_callback_t>> mouse_callback_map_t;
	typedef std::function<void(Window*,GdkScrollDirection)> scroll_callback_t;

    Window(const char* title, int width, int height, int fps = 30);
    virtual ~Window();
    // window_main.cpp methods
    int GetWidth() const;
    int GetHeight() const;
    Position GetPosition(int x = 0,int y = 0, double xrel = 0, double yrel = 0);
    const Size* GetSizePtr() const;
    void RemoveAllCallbacks();
    void OnActivate(std::function<void()> callback);
    void Quit();
    void Mainloop(); // Start application.
    // window_rendering.cpp methods
    int SetRenderCallback(callback_t callback);
    void RemoveRenderCallback(int id);
    void SetDrawColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
    void SetDrawColor(Color color); // Sets color to value. Format: 0xRRGGBBAA.
    void SetLineWidth(int line_width);
    void Clear();
    void DrawRectangle(int x, int y, int w, int h, bool filled = true);
    void DrawPoint(int x, int y);
    void DrawLine(int x1, int y1, int x2, int y2);
    void DrawArc(int x, int y, int r, double startAngle, double endAngle,
        bool filled = true);
    void DrawCircle(int x, int y, int r, bool filled = true);
    void DrawPolygon(std::vector<std::pair<int,int>> points, bool filled=true);
    void SetTextSize(int size);
    void DrawText(const std::string& text, int x, int y);
    void DrawText(const std::string& text, Position pos,
    	Alignment horizontal_align = Alignment::LEFT,
		Alignment vertical_align = Alignment::TOP);
    void DrawImage(const std::string& filename, int x, int y);
    void DrawImage(const std::string& filename, Position pos,
    	Alignment horizontal_align = Alignment::LEFT,
		Alignment vertical_align = Alignment::TOP);
    void GetImageSize(const std::string& filename, int* w, int* h);
    void GetTextSize(const std::string& text, int* w, int* h);

    // window_events_keyboard.cpp methods
    int SetKeydownCallback(callback_t callback, guint key);
    void RemoveKeydownCallback(int id, guint key);
    int SetKeyupCallback(callback_t callback, guint key);
    void RemoveKeyupCallback(int id, guint key);
    bool IsKeyDown(guint key) const;
    bool IsControlDown() const;

    // window_events_mouse.cpp
    int SetMousedownCallback(mouse_callback_t callback,
    	guint button = GDK_BUTTON_PRIMARY);
    void RemoveMousedownCallback(int id, guint button = GDK_BUTTON_PRIMARY);
    int SetMouseupCallback(mouse_callback_t callback,
    	guint button = GDK_BUTTON_PRIMARY);
    void RemoveMouseupCallback(int id, guint button = GDK_BUTTON_PRIMARY);
    int SetMousemotionCallback(mouse_callback_t callback);
    void RemoveMousemotionCallback(int id);
    int SetScrollCallback(scroll_callback_t callback);
    void RemoveScrollCallback(int id);
    bool IsMouseDown(guint button = GDK_BUTTON_PRIMARY) const;
    int GetMouseX() const;
    int GetMouseY() const;

private:
    // window_main.cpp methods
    void InitializeWindow();
    void InitializeApplication(const char* title);
    friend void Activate(GtkApplication*, gpointer);

    // window_events.cpp methods
    void InitializeEvents();

    template<typename F>
    static int AddToCallbackMap(std::map<guint,std::vector<F>>& callback_map,
		guint id, F callback);
    template<typename F>
    static std::vector<F> CheckCallbackMap(
    	std::map<guint,std::vector<F>>& callback_map, guint id);
    template<typename F>
    static void RemoveFromCallbackList(
    	std::vector<F>& callback_list, int id);

    friend void GtkKeydownCallback(GtkWidget*, GdkEventKey*, gpointer);
	void ProcessKeydown(GdkEventKey* event);
    friend void GtkKeyupCallback(GtkWidget*, GdkEventKey*, gpointer);
	void ProcessKeyup(GdkEventKey* event);

	friend void GtkMousedownCallback(GtkWidget*, GdkEventButton*, gpointer);
	void ProcessMousedown(GdkEventButton* event);

	friend void GtkMouseupCallback(GtkWidget*, GdkEventButton*, gpointer);
	void ProcessMouseup(GdkEventButton* event);

	friend void GtkMousemotionCallback(GtkWidget*, GdkEventMotion*, gpointer);
	void ProcessMousemotion(GdkEventMotion* event);

	friend void GtkScrollCallback(GtkWidget*, GdkEventScroll*, gpointer);
	void ProcessScroll(GdkEventScroll* event);

    // window_rendering.cpp methods
    friend void GtkDrawCallback(GtkWidget*, cairo_t*, gpointer);
    void InitializeDrawingArea();
    void Render();
    cairo_surface_t* GetSurface(const std::string& filename);

    // window_main.cpp members
    Size size;
    const char* title;
    const int FPS;
    GtkApplication* application;
    GtkWidget* window;
    std::function<void()> on_activate;

    // window_rendering.cpp members
    cairo_t* cr;
    cairo_font_face_t* font;
    std::map<std::string, cairo_surface_t*> images; // Only load each image once.
    std::vector<callback_t> render_callbacks;
    bool is_render_callbacks_modified;

    // window_events.cpp members
    mouse_callback_map_t mousedown_callbacks;
    bool is_mousedown_callbacks_modified;
    mouse_callback_map_t mouseup_callbacks;
    bool is_mouseup_callbacks_modified;
    std::vector<mouse_callback_t> mousemotion_callbacks;
    bool is_mousemotion_callbacks_modified;
    std::vector<scroll_callback_t> scroll_callbacks;
    bool is_scroll_callbacks_modified;
    callback_map_t keyup_callbacks;
    bool is_keyup_callbacks_modified;
    callback_map_t keydown_callbacks;
    bool is_keydown_callbacks_modified;
    std::map<guint, bool> keymap; // Which keys are down?
    std::map<guint, bool> mousemap; // Which mouse buttons are down?
    int mouse_x, mouse_y;
};

} // namespace gui

#endif // GRAPHCOLORING_GUI_WINDOW_H_