Skip to content

Instantly share code, notes, and snippets.

@CraftedPvP
Created September 14, 2019 11:29
Show Gist options
  • Save CraftedPvP/e1433f1b4a4620ca90cce2c9b2be65fe to your computer and use it in GitHub Desktop.
Save CraftedPvP/e1433f1b4a4620ca90cce2c9b2be65fe to your computer and use it in GitHub Desktop.
A C++ template for Glut Mouse button events
int lastMouseBtnStateLeft = GLUT_UP; // last left mouse buttons state
int lastMouseBtnStateRight = GLUT_UP; // last right mouse buttons state
void processMouse(int button, int state1, int x, int y){
if (button == GLUT_LEFT_BUTTON) {
// decode and handle the mouse events by type
if ((lastMouseBtnStateLeft == GLUT_UP )&&(state1 == GLUT_DOWN)) // mouse down (click)
{
// here do your stuff
}
if ((lastMouseBtnStateLeft == GLUT_DOWN)&&(state1 == GLUT_DOWN)) // mouse move while clicked
{
// here do your stuff
}
if ((lastMouseBtnStateLeft == GLUT_DOWN)&&(state1 == GLUT_UP )) // mouse up (release)
{
// here do your stuff
}
if ((lastMouseBtnStateLeft == GLUT_UP )&&(state1 == GLUT_UP )) // mouse move without buttons
{
// here do your stuff
}
// store actual buttons state for next time
lastMouseBtnStateLeft = state1;
}
if (button == GLUT_RIGHT_BUTTON){
// decode and handle the mouse events by type
if ((lastMouseBtnStateRight == GLUT_UP )&&(state1 == GLUT_DOWN)) // mouse down (click)
{
// here do your stuff
}
if ((lastMouseBtnStateRight == GLUT_DOWN)&&(state1 == GLUT_DOWN)) // mouse move while clicked
{
// here do your stuff
}
if ((lastMouseBtnStateRight == GLUT_DOWN)&&(state1 == GLUT_UP )) // mouse up (release)
{
// here do your stuff
}
if ((lastMouseBtnStateRight == GLUT_UP )&&(state1 == GLUT_UP )) // mouse move without buttons
{
// here do your stuff
}
// store actual buttons state for next time
lastMouseBtnStateRight = state1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment