-
Notifications
You must be signed in to change notification settings - Fork 45
/
mouse.c
145 lines (122 loc) · 2.87 KB
/
mouse.c
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
#include <stdlib.h>
#include <string.h>
#include <libretro.h>
#include "mouse.h"
#include "lutro.h"
static int16_t mouse_cache[8];
int lutro_mouse_preload(lua_State *L)
{
static luaL_Reg mouse_funcs[] = {
{ "isDown", mouse_isDown },
{ "getX", mouse_getX },
{ "getY", mouse_getY },
{ "getPosition", mouse_getPosition },
{NULL, NULL}
};
lutro_ensure_global_table(L, "lutro");
luaL_newlib(L, mouse_funcs);
lua_setfield(L, -2, "mouse");
return 1;
}
void lutro_mouse_init()
{
}
void lutro_mouseevent(lua_State* L)
{
unsigned i;
for (i = 0; i < 8; i++)
{
int16_t value = settings.input_cb(0, RETRO_DEVICE_MOUSE, 0, i);
if (i == RETRO_DEVICE_ID_MOUSE_X || i == RETRO_DEVICE_ID_MOUSE_Y) {
mouse_cache[i] += value;
}
else {
mouse_cache[i] = value;
}
}
}
/**
* lutro.mouse.isDown()
*
* https://love2d.org/wiki/love.mouse.isDown
*/
int mouse_isDown(lua_State *L)
{
int n = lua_gettop(L);
if (n < 1) {
return luaL_error(L, "lutro.mouse.isDown requires 1 or more arguments, %d given.", n);
}
int buttonToCheck = 0;
bool output = false;
unsigned i;
for (i = 0; i < n; i++) {
buttonToCheck = (int) luaL_checknumber(L, i + 1);
if (buttonToCheck == 1) {
buttonToCheck = RETRO_DEVICE_ID_MOUSE_LEFT;
}
else if (buttonToCheck == 2) {
buttonToCheck = RETRO_DEVICE_ID_MOUSE_RIGHT;
}
else if (buttonToCheck == 3) {
buttonToCheck = RETRO_DEVICE_ID_MOUSE_MIDDLE;
}
else {
buttonToCheck = 0;
}
if (buttonToCheck > 0) {
if (mouse_cache[buttonToCheck]) {
output = true;
break;
}
}
}
lua_pushboolean(L, output);
return 1;
}
/**
* lutro.mouse.getX()
*
* https://love2d.org/wiki/love.mouse.getX
*/
int mouse_getX(lua_State *L)
{
int n = lua_gettop(L);
if (n > 0) {
return luaL_error(L, "lutro.mouse.getX takes no arguments, %d given.", n);
}
unsigned x = (unsigned) mouse_cache[RETRO_DEVICE_ID_MOUSE_X];
lua_pushnumber(L, x);
return 1;
}
/**
* lutro.mouse.getY()
*
* https://love2d.org/wiki/love.mouse.getY
*/
int mouse_getY(lua_State *L)
{
int n = lua_gettop(L);
if (n > 0) {
return luaL_error(L, "lutro.mouse.getX takes no arguments, %d given.", n);
}
unsigned y = (unsigned) mouse_cache[RETRO_DEVICE_ID_MOUSE_Y];
lua_pushnumber(L, y);
return 1;
}
/**
* lutro.mouse.getPosition()
*
* https://love2d.org/wiki/love.mouse.getPosition
*/
int mouse_getPosition(lua_State *L)
{
int n = lua_gettop(L);
if (n > 0) {
return luaL_error(L, "lutro.mouse.getX takes no arguments, %d given.", n);
}
unsigned x = (unsigned) mouse_cache[RETRO_DEVICE_ID_MOUSE_X];
unsigned y = (unsigned) mouse_cache[RETRO_DEVICE_ID_MOUSE_Y];
lua_pushnumber(L, x);
lua_pushnumber(L, y);
return 2;
}