Add penPressure callback support

This commit is contained in:
Raja 2017-11-05 21:29:48 -05:00
parent bdf67d938d
commit 07053602a5
6 changed files with 303 additions and 3 deletions

1
.gitignore vendored
View File

@ -82,3 +82,4 @@ tests/title
tests/vulkan tests/vulkan
tests/windows tests/windows
/build

171
examples/pen.c Normal file
View File

@ -0,0 +1,171 @@
//========================================================================
// Simple GLFW example
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
//! [code]
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "linmath.h"
#include <stdlib.h>
#include <stdio.h>
static const struct
{
float x, y;
float r, g, b;
} vertices[3] =
{
{ -0.6f, -0.4f, 1.f, 0.f, 0.f },
{ 0.6f, -0.4f, 0.f, 1.f, 0.f },
{ 0.f, 0.6f, 0.f, 0.f, 1.f }
};
static const char* vertex_shader_text =
"#version 110\n"
"uniform mat4 MVP;\n"
"attribute vec3 vCol;\n"
"attribute vec2 vPos;\n"
"varying vec3 color;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
" color = vCol;\n"
"}\n";
static const char* fragment_shader_text =
"#version 110\n"
"varying vec3 color;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(color, 1.0);\n"
"}\n";
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
static void pen_callback(GLFWwindow* window, int pressure)
{
fprintf(stderr, "pressure -- %d --\n", pressure);
}
int main(void)
{
GLFWwindow* window;
GLuint vertex_buffer, vertex_shader, fragment_shader, program;
GLint mvp_location, vpos_location, vcol_location;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwSetPenPressureCallback(window, pen_callback);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1);
// NOTE: OpenGL error checks have been omitted for brevity
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
mvp_location = glGetUniformLocation(program, "MVP");
vpos_location = glGetAttribLocation(program, "vPos");
vcol_location = glGetAttribLocation(program, "vCol");
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) 0);
glEnableVertexAttribArray(vcol_location);
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) (sizeof(float) * 2));
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
mat4x4 m, p, mvp;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
mat4x4_identity(m);
mat4x4_rotate_Z(m, m, (float) glfwGetTime());
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
mat4x4_mul(mvp, p, m);
glUseProgram(program);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
//! [code]

View File

@ -507,6 +507,8 @@ extern "C" {
/*! @} */ /*! @} */
/*! @} */
/*! @defgroup buttons Mouse buttons /*! @defgroup buttons Mouse buttons
* @brief Mouse button IDs. * @brief Mouse button IDs.
* *
@ -1277,6 +1279,28 @@ typedef void (* GLFWframebuffersizefun)(GLFWwindow*,int,int);
*/ */
typedef void (* GLFWmousebuttonfun)(GLFWwindow*,int,int,int); typedef void (* GLFWmousebuttonfun)(GLFWwindow*,int,int,int);
/*! @brief The function signature for pen --button-- callbacks.
*
* This is the function signature for pen --button-- callback functions.
*
* @param[in] window The window that received the event.
* @param[in] button The [pen button](@ref buttons) that was pressed or
* released.
* @param[in] action One of `GLFW_PRESS` or `GLFW_RELEASE`.
* @param[in] mods Bit field describing which [modifier keys](@ref mods) were
* held down.
*
* @sa @ref input_pen_button
* @sa @ref glfwSetPenButtonCallback
*
* ---- @since Added in version 1.0.
* @glfw3 Added window handle and modifier mask parameters.------
*
* @ingroup input
*/
typedef void(*GLFWpenpressurefun)(GLFWwindow*, int);
/*! @brief The function signature for cursor position callbacks. /*! @brief The function signature for cursor position callbacks.
* *
* This is the function signature for cursor position callback functions. * This is the function signature for cursor position callback functions.
@ -1349,6 +1373,7 @@ typedef void (* GLFWscrollfun)(GLFWwindow*,double,double);
* *
* @ingroup input * @ingroup input
*/ */
typedef void (* GLFWkeyfun)(GLFWwindow*,int,int,int,int); typedef void (* GLFWkeyfun)(GLFWwindow*,int,int,int,int);
/*! @brief The function signature for Unicode character callbacks. /*! @brief The function signature for Unicode character callbacks.
@ -3762,7 +3787,32 @@ GLFWAPI int glfwGetKey(GLFWwindow* window, int key);
* *
* @ingroup input * @ingroup input
*/ */
GLFWAPI int glfwGetMouseButton(GLFWwindow* window, int button); GLFWAPI int glfwGetMouseButton(GLFWwindow* handle, int button);
/*! @brief Returns the last reported state of a pen button for the specified
* window.
*
* This function returns the last state reported for the specified pen button
* to the specified window. The returned state is one of `GLFW_PRESS` or
* `GLFW_RELEASE`.
*
* @param[in] window The desired window.
* @param[in] button The desired [pen button](@ref pen buttons).
* @return One of `GLFW_PRESS` or `GLFW_RELEASE`.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref
* GLFW_INVALID_ENUM.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref input_pen_button
*
* @since Added in version 1.0.
* @glfw3 Added window handle parameter.
*
* @ingroup input
*/
GLFWAPI int glfwGetPenPressure(GLFWwindow* handle);
/*! @brief Retrieves the position of the cursor relative to the client area of /*! @brief Retrieves the position of the cursor relative to the client area of
* the window. * the window.
@ -4100,6 +4150,35 @@ GLFWAPI GLFWcharmodsfun glfwSetCharModsCallback(GLFWwindow* window, GLFWcharmods
* @ingroup input * @ingroup input
*/ */
GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun cbfun); GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* window, GLFWmousebuttonfun cbfun);
/*! @brief Sets the pen --button-- callback.
*
* This function sets the pen --button-- callback of the specified window, which
* is called when a pen --button-- is pressed or released.
*
* When a window loses input focus, it will generate synthetic pen --button--
* release events for all pressed pen buttons. You can tell these events
* from user-generated events by the fact that the synthetic ones are generated
* after the focus loss event has been processed, i.e. after the
* [window focus callback](@ref glfwSetWindowFocusCallback) has been called.
*
* @param[in] window The window whose callback to set.
* @param[in] cbfun The new callback, or `NULL` to remove the currently set
* callback.
* @return The previously set callback, or `NULL` if no callback was set or the
* library had not been [initialized](@ref intro_init).
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref input_pen_button
*
* @since Added in version 1.0.
* @glfw3 Added window handle parameter and return value.
*
* @ingroup input
*/
GLFWAPI GLFWpenpressurefun glfwSetPenPressureCallback(GLFWwindow* window, GLFWpenpressurefun cbfun);
/*! @brief Sets the cursor position callback. /*! @brief Sets the cursor position callback.
* *

View File

@ -232,6 +232,16 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods)
window->callbacks.mouseButton((GLFWwindow*) window, button, action, mods); window->callbacks.mouseButton((GLFWwindow*) window, button, action, mods);
} }
/*************************PEN**************************************************/
void _glfwInputPenPressure(_GLFWwindow* window, int pressure)
{
window->penPressure = pressure;
if (window->callbacks.penPressure)
window->callbacks.penPressure((GLFWwindow*)window, pressure);
}
/***********************************PEN*************************************/
void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos) void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos)
{ {
if (window->virtualCursorPosX == xpos && window->virtualCursorPosY == ypos) if (window->virtualCursorPosX == xpos && window->virtualCursorPosY == ypos)
@ -513,6 +523,19 @@ GLFWAPI int glfwGetMouseButton(GLFWwindow* handle, int button)
return (int) window->mouseButtons[button]; return (int) window->mouseButtons[button];
} }
/***************************PEN**************************************************/
GLFWAPI int glfwGetPenPressure(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*)handle;
assert(window != NULL);
_GLFW_REQUIRE_INIT_OR_RETURN(0);
return window->penPressure;
}
/**************************PEN***************************************************/
GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, double* xpos, double* ypos) GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, double* xpos, double* ypos)
{ {
_GLFWwindow* window = (_GLFWwindow*) handle; _GLFWwindow* window = (_GLFWwindow*) handle;
@ -708,6 +731,19 @@ GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* handle,
return cbfun; return cbfun;
} }
/*********PEN************************************/
GLFWAPI GLFWpenpressurefun glfwSetPenPressureCallback(GLFWwindow* handle,
GLFWpenpressurefun cbfun)
{
_GLFWwindow* window = (_GLFWwindow*)handle;
assert(window != NULL);
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP_POINTERS(window->callbacks.penPressure, cbfun);
return cbfun;
}
/************************************************/
GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle, GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle,
GLFWcursorposfun cbfun) GLFWcursorposfun cbfun)
{ {

View File

@ -417,6 +417,7 @@ struct _GLFWwindow
GLFWbool stickyKeys; GLFWbool stickyKeys;
GLFWbool stickyMouseButtons; GLFWbool stickyMouseButtons;
int penPressure;
int cursorMode; int cursorMode;
char mouseButtons[GLFW_MOUSE_BUTTON_LAST + 1]; char mouseButtons[GLFW_MOUSE_BUTTON_LAST + 1];
char keys[GLFW_KEY_LAST + 1]; char keys[GLFW_KEY_LAST + 1];
@ -434,7 +435,8 @@ struct _GLFWwindow
GLFWwindowiconifyfun iconify; GLFWwindowiconifyfun iconify;
GLFWwindowmaximizefun maximize; GLFWwindowmaximizefun maximize;
GLFWframebuffersizefun fbsize; GLFWframebuffersizefun fbsize;
GLFWmousebuttonfun mouseButton; GLFWmousebuttonfun mouseButton;
GLFWpenpressurefun penPressure;
GLFWcursorposfun cursorPos; GLFWcursorposfun cursorPos;
GLFWcursorenterfun cursorEnter; GLFWcursorenterfun cursorEnter;
GLFWscrollfun scroll; GLFWscrollfun scroll;
@ -816,6 +818,15 @@ void _glfwInputScroll(_GLFWwindow* window, double xoffset, double yoffset);
*/ */
void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods); void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods);
/*! @brief Notifies shared code of a pen button click event.
* @param[in] window The window that received the event.
* @param[in] button The button that was pressed or released.
* @param[in] action @ref GLFW_PRESS or @ref GLFW_RELEASE.
* @param[in] mods The modifiers pressed when the event was generated.
* @ingroup event
*/
void _glfwInputPenPressure(_GLFWwindow* window, int pressure);
/*! @brief Notifies shared code of a cursor motion event. /*! @brief Notifies shared code of a cursor motion event.
* @param[in] window The window that received the event. * @param[in] window The window that received the event.
* @param[in] xpos The new x-coordinate of the cursor, relative to the left * @param[in] xpos The new x-coordinate of the cursor, relative to the left

View File

@ -564,6 +564,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
} }
case WM_POINTERDOWN: case WM_POINTERDOWN:
case WM_POINTERUPDATE:
case WM_POINTERUP:
{ {
POINTER_TOUCH_INFO touchInfo; POINTER_TOUCH_INFO touchInfo;
POINTER_PEN_INFO penInfo; POINTER_PEN_INFO penInfo;
@ -599,7 +601,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
int yPos = GET_Y_LPARAM(lParam); int yPos = GET_Y_LPARAM(lParam);
if (IS_POINTER_INCONTACT_WPARAM(wParam)) if (IS_POINTER_INCONTACT_WPARAM(wParam))
{ {
printf("The pen pressure is = %d", penInfo.pressure); _glfwInputPenPressure(window, penInfo.pressure);
} }
// mark as handled to skip call to DefWindowProc // mark as handled to skip call to DefWindowProc
fHandled = TRUE; fHandled = TRUE;