aboutsummaryrefslogtreecommitdiffstats
path: root/src/window/Events.cpp
blob: 09f6acde80217c05c8fc4396d0fd5cc8457db03e (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
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "Events.hpp"

Events::Events() : cur_frame(0), delta_x(0.0), delta_y(0.0),
	x(0.0), y(0.0), cursor_locked(false)
{
	keys = new bool[MAX_KEYS];
	memset(keys, false, MAX_KEYS * sizeof(bool));
	frames = new unsigned int[MAX_KEYS];
	memset(frames, 0, MAX_KEYS * sizeof(unsigned int));
}

Events::~Events()
{
	delete[] keys;
	delete[] frames;
}

bool Events::Pressed(int keycode)
{
	if(keycode < 0 || keycode >= MOUSE_BUTTONS)
		return false;
	return keys[keycode];
}

bool Events::Jpressed(int keycode)
{
	if(keycode < 0 || keycode >= MOUSE_BUTTONS)
		return false;
	return keys[keycode] && (frames[keycode] == cur_frame);
}

bool Events::Clicked(int button)
{
	return keys[MOUSE_BUTTONS + button];
}

bool Events::Jclicked(int button)
{
	int idx = MOUSE_BUTTONS + button;
	return keys[idx] && (frames[idx] == cur_frame);
}

void Events::NextFrame()
{
	cur_frame++;
}

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

void Events::KeyHandle(int key, int scancode, int action, int mode)
{
	if(action == GLFW_PRESS) {
		keys[key] = true;
		frames[key] = cur_frame;
	}
	else if(action == GLFW_RELEASE) {
		keys[key] = false;
		frames[key] = cur_frame;
	}
}

void Events::CursorPosHandle(double xpos, double ypos)
{
	delta_x = xpos - x;
	delta_y = ypos - y;

	x = xpos;
	y = ypos;
}

void Events::MouseButtonHandle(int button, int action, int mode)
{
	if(action == GLFW_PRESS) {
		keys[MOUSE_BUTTONS + button] = true;
		frames[MOUSE_BUTTONS + button] = cur_frame;
	}
	else if (action == GLFW_RELEASE){
		keys[MOUSE_BUTTONS + button] = false;
		frames[MOUSE_BUTTONS + button] = cur_frame;
	}
}