mirror of
https://git.shylie.info/shylie/glerminal.git
synced 2025-10-01 04:30:06 +00:00
Add mouse callbacks
This commit is contained in:
@@ -139,7 +139,7 @@ namespace
|
||||
lua_remove(L, handler);
|
||||
}
|
||||
|
||||
void mainloop(float dt)
|
||||
void mainloop(double dt)
|
||||
{
|
||||
lua_pushcfunction(L, message_handler);
|
||||
const int handler = lua_gettop(L);
|
||||
@@ -155,7 +155,7 @@ namespace
|
||||
lua_remove(L, handler);
|
||||
}
|
||||
|
||||
void pressed(int key)
|
||||
void keypressed(int key)
|
||||
{
|
||||
const char* const name = glfwGetKeyName(key, 0);
|
||||
if (name)
|
||||
@@ -163,7 +163,7 @@ namespace
|
||||
lua_pushcfunction(L, message_handler);
|
||||
const int handler = lua_gettop(L);
|
||||
lua_getglobal(L, "glerminal");
|
||||
lua_getfield(L, -1, "pressed");
|
||||
lua_getfield(L, -1, "keypress");
|
||||
lua_remove(L, -2);
|
||||
lua_pushstring(L, name);
|
||||
if (lua_pcall(L, 1, 0, handler) != LUA_OK)
|
||||
@@ -175,7 +175,7 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
void released(int key)
|
||||
void keyrelease(int key)
|
||||
{
|
||||
const char* const name = glfwGetKeyName(key, 0);
|
||||
if (name)
|
||||
@@ -183,7 +183,7 @@ namespace
|
||||
lua_pushcfunction(L, message_handler);
|
||||
const int handler = lua_gettop(L);
|
||||
lua_getglobal(L, "glerminal");
|
||||
lua_getfield(L, -1, "released");
|
||||
lua_getfield(L, -1, "keyrelease");
|
||||
lua_remove(L, -2);
|
||||
lua_pushstring(L, name);
|
||||
if (lua_pcall(L, 1, 0, handler) != LUA_OK)
|
||||
@@ -194,6 +194,97 @@ namespace
|
||||
lua_remove(L, handler);
|
||||
}
|
||||
}
|
||||
|
||||
void mousemove(double x, double y)
|
||||
{
|
||||
lua_pushcfunction(L, message_handler);
|
||||
const int handler = lua_gettop(L);
|
||||
lua_getglobal(L, "glerminal");
|
||||
lua_getfield(L, -1, "mousemove");
|
||||
lua_remove(L, -2);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
if (lua_pcall(L, 2, 0, handler) != LUA_OK)
|
||||
{
|
||||
std::cout << lua_tostring(L, -1) << std::endl;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_remove(L, handler);
|
||||
}
|
||||
|
||||
void mousepress(int button, double x, double y)
|
||||
{
|
||||
const char* button_name = nullptr;
|
||||
switch (button)
|
||||
{
|
||||
case GLFW_MOUSE_BUTTON_LEFT:
|
||||
button_name = "left";
|
||||
break;
|
||||
|
||||
case GLFW_MOUSE_BUTTON_RIGHT:
|
||||
button_name = "right";
|
||||
break;
|
||||
|
||||
case GLFW_MOUSE_BUTTON_MIDDLE:
|
||||
button_name = "middle";
|
||||
break;
|
||||
}
|
||||
|
||||
if (button_name)
|
||||
{
|
||||
lua_pushcfunction(L, message_handler);
|
||||
const int handler = lua_gettop(L);
|
||||
lua_getglobal(L, "glerminal");
|
||||
lua_getfield(L, -1, "mousepress");
|
||||
lua_remove(L, -2);
|
||||
lua_pushstring(L, button_name);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
if (lua_pcall(L, 3, 0, handler) != LUA_OK)
|
||||
{
|
||||
std::cout << lua_tostring(L, -1) << std::endl;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_remove(L, handler);
|
||||
}
|
||||
}
|
||||
|
||||
void mouserelease(int button, double x, double y)
|
||||
{
|
||||
const char* button_name = nullptr;
|
||||
switch (button)
|
||||
{
|
||||
case GLFW_MOUSE_BUTTON_LEFT:
|
||||
button_name = "left";
|
||||
break;
|
||||
|
||||
case GLFW_MOUSE_BUTTON_RIGHT:
|
||||
button_name = "right";
|
||||
break;
|
||||
|
||||
case GLFW_MOUSE_BUTTON_MIDDLE:
|
||||
button_name = "middle";
|
||||
break;
|
||||
}
|
||||
|
||||
if (button_name)
|
||||
{
|
||||
lua_pushcfunction(L, message_handler);
|
||||
const int handler = lua_gettop(L);
|
||||
lua_getglobal(L, "glerminal");
|
||||
lua_getfield(L, -1, "mouserelease");
|
||||
lua_remove(L, -2);
|
||||
lua_pushstring(L, button_name);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
if (lua_pcall(L, 3, 0, handler) != LUA_OK)
|
||||
{
|
||||
std::cout << lua_tostring(L, -1) << std::endl;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_remove(L, handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
@@ -232,7 +323,15 @@ int main(int argc, char** argv)
|
||||
}
|
||||
else
|
||||
{
|
||||
glerminal_run(init, mainloop, pressed, released);
|
||||
glerminal_init_params params;
|
||||
params.init = init;
|
||||
params.main = mainloop;
|
||||
params.keypress = keypressed;
|
||||
params.keyrelease = keyrelease;
|
||||
params.moved = mousemove;
|
||||
params.mousepress = mousepress;
|
||||
params.mouserelease = mouserelease;
|
||||
glerminal_run(params);
|
||||
}
|
||||
|
||||
lua_close(L);
|
||||
|
@@ -27,7 +27,7 @@ namespace glerminal
|
||||
class glerminal
|
||||
{
|
||||
public:
|
||||
glerminal(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released);
|
||||
explicit glerminal(glerminal_init_params params);
|
||||
~glerminal();
|
||||
|
||||
glerminal(const glerminal&) = delete;
|
||||
@@ -83,7 +83,9 @@ namespace glerminal
|
||||
|
||||
unsigned int m_sprites[(CELL_SIZE + 2) * (CELL_SIZE + 2) * MAX_SPRITES];
|
||||
glerminal_main_cb m_main;
|
||||
glerminal_keys_cb m_pressed, m_released;
|
||||
glerminal_keys_cb m_keypressed, m_keyreleased;
|
||||
glerminal_mousemoved_cb m_mousemoved;
|
||||
glerminal_mousepress_cb m_mousepressed, m_mousereleased;
|
||||
|
||||
#ifdef GLERMINAL_OPENGL_DEBUG_CONTEXT
|
||||
mutable std::ofstream m_log;
|
||||
@@ -101,6 +103,8 @@ namespace glerminal
|
||||
void update_scales();
|
||||
|
||||
static void glfw_key_handler(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
static void glfw_mousemoved_handler(GLFWwindow* window, double x, double y);
|
||||
static void glfw_mousepress_handler(GLFWwindow* window, int button, int action, int mods);
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -189,10 +189,13 @@ namespace
|
||||
|
||||
namespace glerminal
|
||||
{
|
||||
glerminal::glerminal(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released) :
|
||||
m_main(main),
|
||||
m_pressed(pressed),
|
||||
m_released(released),
|
||||
glerminal::glerminal(glerminal_init_params params) :
|
||||
m_main(params.main),
|
||||
m_keypressed(params.keypress),
|
||||
m_keyreleased(params.keyrelease),
|
||||
m_mousemoved(params.moved),
|
||||
m_mousepressed(params.mousepress),
|
||||
m_mousereleased(params.mouserelease),
|
||||
m_cells{ },
|
||||
m_offsets{ },
|
||||
m_sprites{ },
|
||||
@@ -208,7 +211,7 @@ namespace glerminal
|
||||
}
|
||||
|
||||
// unsure if this should be an error
|
||||
if (!init)
|
||||
if (!params.init)
|
||||
{
|
||||
throw std::runtime_error("No init callback provided.");
|
||||
}
|
||||
@@ -229,7 +232,7 @@ namespace glerminal
|
||||
|
||||
GLERMINAL_G = this;
|
||||
|
||||
init();
|
||||
params.init();
|
||||
}
|
||||
|
||||
glerminal::~glerminal()
|
||||
@@ -242,12 +245,12 @@ namespace glerminal
|
||||
|
||||
void glerminal::run()
|
||||
{
|
||||
float last = glfwGetTime();
|
||||
double last = glfwGetTime();
|
||||
while (!glfwWindowShouldClose(m_window))
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
const float current = glfwGetTime();
|
||||
const double current = glfwGetTime();
|
||||
|
||||
m_main(current - last);
|
||||
|
||||
@@ -382,7 +385,9 @@ namespace glerminal
|
||||
}
|
||||
|
||||
glfwSetWindowUserPointer(m_window, this);
|
||||
glfwSetKeyCallback(m_window, glerminal::glfw_key_handler);
|
||||
glfwSetKeyCallback(m_window, glfw_key_handler);
|
||||
glfwSetCursorPosCallback(m_window, glfw_mousemoved_handler);
|
||||
glfwSetMouseButtonCallback(m_window, glfw_mousepress_handler);
|
||||
}
|
||||
|
||||
void glerminal::log(GLenum type, GLuint id, GLenum severity, const char* message) const
|
||||
@@ -780,23 +785,49 @@ namespace glerminal
|
||||
{
|
||||
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
|
||||
|
||||
if (self->m_pressed && action == GLFW_PRESS)
|
||||
if (self->m_keypressed && action == GLFW_PRESS)
|
||||
{
|
||||
self->m_pressed(key);
|
||||
self->m_keypressed(key);
|
||||
}
|
||||
|
||||
if (self->m_released && action == GLFW_RELEASE)
|
||||
if (self->m_keyreleased && action == GLFW_RELEASE)
|
||||
{
|
||||
self->m_released(key);
|
||||
self->m_keyreleased(key);
|
||||
}
|
||||
}
|
||||
|
||||
void glerminal::glfw_mousemoved_handler(GLFWwindow *window, double x, double y)
|
||||
{
|
||||
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
|
||||
|
||||
if (self->m_mousemoved) { self->m_mousemoved(x / CELL_SIZE, y / CELL_SIZE); }
|
||||
}
|
||||
|
||||
void glerminal::glfw_mousepress_handler(GLFWwindow *window, int button, int action, int mods)
|
||||
{
|
||||
glerminal* const self = static_cast<glerminal*>(glfwGetWindowUserPointer(window));
|
||||
|
||||
double x, y;
|
||||
glfwGetCursorPos(window, &x, &y);
|
||||
|
||||
if (self->m_mousepressed && action == GLFW_PRESS)
|
||||
{
|
||||
self->m_mousepressed(button, x / CELL_SIZE, y / CELL_SIZE);
|
||||
}
|
||||
|
||||
if (self->m_mousereleased && action == GLFW_RELEASE)
|
||||
{
|
||||
self->m_mousereleased(button, x / CELL_SIZE, y / CELL_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void glerminal_run(glerminal_init_cb init, glerminal_main_cb main, glerminal_keys_cb pressed, glerminal_keys_cb released)
|
||||
void glerminal_run(glerminal_init_params params)
|
||||
{
|
||||
try
|
||||
{
|
||||
glerminal::glerminal* g = new glerminal::glerminal(init, main, pressed, released);
|
||||
glerminal::glerminal* g = new glerminal::glerminal(params);
|
||||
g->run();
|
||||
delete g;
|
||||
}
|
||||
|
Reference in New Issue
Block a user