aboutsummaryrefslogtreecommitdiffstats
path: root/src/window/EventsManager.cpp
blob: ddf290140da0fecf45b48eca85b42e354b60cf15 (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
#include <stdio.h>
#include <GLFW/glfw3.h>

#include "Window.hpp"
#include "EventsManager.hpp"
#include "Events.hpp"

struct lStorage EventsManager::listeners(2);

void EventsManager::PollEvents()
{
	int max = listeners.max_n, i;
	for(i = 0; i < max; i++) {
		if(!listeners.list[i])
			continue;

		listeners.list[i]->NextFrame();
	}
	glfwPollEvents();
}

void EventsManager::AddListener(Events *e, int listen)
{
	if(listen <= 0)
		return;

	GLFWwindow *win = e->GetWindow()->GetWin();
	if(listen & EM_LISTEN_KEY)
		glfwSetKeyCallback(win, key_callback);
	if(listen & EM_LISTEN_CURSOR)
		glfwSetCursorPosCallback(win, cursor_pos_callback);
	if(listen & EM_LISTEN_MBUTTON)
		glfwSetMouseButtonCallback(win, mouse_button_callback);
	if(listen & EM_LISTEN_WINDOWSIZE)
		glfwSetWindowSizeCallback(win, window_size_callback);
	if(listen & EM_LISTEN_MSCROLL)
		glfwSetScrollCallback(win, scroll_callback);

	listeners.Add(e, win, listen);
}

//////////////////////////////////////////////////////////////////////////////

void EventsManager::key_callback(GLFWwindow *window, int key, int scancode,
	int action, int mode)
{
	int max = listeners.max_n, i;
	for(i = 0; i < max; i++) {
		if(!listeners.list[i])
			continue;
		if(!(listeners.listen_events[i] & EM_LISTEN_KEY))
			continue;
		if(listeners.win[i] != window)
			continue;

		//fprintf(stderr, "key_callback\n");
		listeners.list[i]->KeyHandle(key, scancode, action, mode);
	}
}

void EventsManager::cursor_pos_callback(GLFWwindow *window, double xpos,
	double ypos)
{
	int max = listeners.max_n, i;
	for(i = 0; i < max; i++) {
		if(!listeners.list[i])
			continue;
		if(!(listeners.listen_events[i] & EM_LISTEN_CURSOR))
			continue;
		if(listeners.win[i] != window)
			continue;

		//fprintf(stderr, "cursor_pos_callback\n");
		listeners.list[i]->CursorPosHandle(xpos, ypos);
	}
}

void EventsManager::mouse_button_callback(GLFWwindow *window, int button,
	int action, int mode)
{
	int max = listeners.max_n, i;
	for(i = 0; i < max; i++) {
		if(!listeners.list[i])
			continue;
		if(!(listeners.listen_events[i] & EM_LISTEN_MBUTTON))
			continue;
		if(listeners.win[i] != window)
			continue;

		//fprintf(stderr, "mouse_button_callback\n");
		listeners.list[i]->MouseButtonHandle(button, action, mode);
	}
}

void EventsManager::window_size_callback(GLFWwindow* window,
	int width, int height)
{
	int max = listeners.max_n, i;
	for(i = 0; i < max; i++) {
		if(!listeners.list[i])
			continue;
		if(!(listeners.listen_events[i] & EM_LISTEN_WINDOWSIZE))
			continue;
		if(listeners.win[i] != window)
			continue;

		//fprintf(stderr, "window_size_callback\n");
		listeners.list[i]->WindowResizeHandle(width, height);
	}
}

void EventsManager::scroll_callback(GLFWwindow* window,
	double xoffset, double yoffset)
{
	int max = listeners.max_n, i;
	for(i = 0; i < max; i++) {
		if(!listeners.list[i])
			continue;
		if(!(listeners.listen_events[i] & EM_LISTEN_MSCROLL))
			continue;
		if(listeners.win[i] != window)
			continue;

		//fprintf(stderr, "scroll_callback\n");
		listeners.list[i]->MouseScrollHandle(xoffset, yoffset);
	}
}