Skip to content

Instantly share code, notes, and snippets.

@joeld42
Created August 17, 2018 21:14
Show Gist options
  • Save joeld42/8397ab6cc9c0addde01daff987eb3c68 to your computer and use it in GitHub Desktop.
Save joeld42/8397ab6cc9c0addde01daff987eb3c68 to your computer and use it in GitHub Desktop.
RayGUI Mouse Events
bool g_isMouseDown;
float g_mouseButtonPrevPressTime;
float g_mouseButtonLastPressTime;
Vector2 g_mouseDownPos;
Vector2 g_mousePos;
bool g_isDragging = false;
// Frame mouse events
bool _isDragStarted = false;
bool _isDragEnded = false;
bool _isSingleClick = false;
bool _isDoubleClick = false;
void MouseButtonUpdate()
{
_isDragStarted = false;
_isDragEnded = false;
_isSingleClick = false;
_isDoubleClick = false;
static float timeVal = 0.0;
timeVal += FRAME_DT;
g_mousePos = GetMousePosition();
if (IsMouseButtonReleased( MOUSE_LEFT_BUTTON )) {
printf("release (%3.2f)...\n", timeVal);
g_isMouseDown = false;
if (!g_isDragging) {
if (timeVal - g_mouseButtonPrevPressTime < 0.4) {
printf("Double Clicked (%f)\n", timeVal - g_mouseButtonLastPressTime );
_isDoubleClick = true;
} else {
printf("Single Clicked\n");
_isSingleClick = true;
}
}
}
if (IsMouseButtonPressed( MOUSE_LEFT_BUTTON )) {
g_mouseDownPos = g_mousePos;
g_isMouseDown = true;
g_mouseButtonPrevPressTime = g_mouseButtonLastPressTime;
g_mouseButtonLastPressTime = timeVal;
//printf("press (%3.2f)...\n", timeVal);
}
float moveAmt = Vector2Distance(g_mousePos, g_mouseDownPos );
if ((g_isMouseDown) && (moveAmt > 0.1) && (!g_isDragging)) {
g_isDragging = true;
printf("Drag Started\n");
_isDragStarted = true;
} else if ((g_isDragging) && (!g_isMouseDown)) {
printf("Drag Ended\n");
g_isDragging = false;
_isDragEnded = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment