Sign in to follow this  
Codorke

OpenGL Mouse Clickable Buttons

Recommended Posts

I'm looking for a nice tutorial for mouse clickable buttons in OpenGL. So i can make my menu's interactive with the mouse. Does someone know some tutorials about this subj ? Any help would be appreciated. Thanks in advance

Share this post


Link to post
Share on other sites
Do you REALLY need a tutorial for this?
Just detect when the mouse button is pressed/released and update the button state as required (once you've made sure the mouse is over the button ofcourse)

Share this post


Link to post
Share on other sites
So if i store my coord of all my buttons i can detect or the mouse is clicked on wich coord. Then i've to lookup wich button was hit.

Something like this ?

Share this post


Link to post
Share on other sites
Include the following "button.h" in ur header and create objects. This code has more than the stuff u want,


button button1(startx, starty); //give the co-ordinates


//and in the mousing handling function

void mouseevent(GLint button,GLint action,GLint xM,GLint yM)

{
int x, y;
x=xM;
y=mwh-yM;
if(button==GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
if(button1.IsButtonClicked(x,y) == 1)
{
button1.ButtonPressed();

}
}
}
[/code]


[code]

// ******************** button.h ******************** //

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>

class button {

private:
GLint xb,yb;
public:
button(GLint x,GLint y) {
xb = x;
yb = y;
}

~button() {
}

public:
void ButtonDraw(char *button_text1);
void ButtonPressed();
int IsButtonClicked(GLint x, GLint y);


};




void button::ButtonDraw(char *button_text1 ) {

GLint bx = xb;
GLint by = yb;
GLint bx1 = xb + 80;
GLint by1 = yb + 20;
GLint xpos, xposinc;
char ch;

glDrawBuffer(GL_BACK);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

//black
glColor3f(0.0f,0.0f,0.0f);
glBegin(GL_QUADS);
glVertex2f(bx-1,by-2);
glVertex2f(bx1+2,by-2);
glVertex2f(bx1+2,by1+1);
glVertex2f(bx-1,by1+1);
glEnd();

//gray inner quad
glColor3f(0.3f,0.3f,0.3f);
glBegin(GL_QUADS);
glVertex2f(bx,by);
glVertex2f(bx1,by);
glVertex2f(bx1,by1);
glVertex2f(bx,by1);
glEnd();

glColor3f(0.5f,0.5f,0.5f);

glBegin(GL_QUADS);
glVertex2f(bx+2,by+2);
glVertex2f(bx1-2,by+2);
glVertex2f(bx1-2,by1-2);
glVertex2f(bx+2,by1-2);
glEnd();


//right
glColor3f(0.3f,0.3f,0.3f);
glBegin(GL_QUADS);
glVertex2f(bx1-2,by+2);
glVertex2f(bx1,by);
glVertex2f(bx1,by1);
glVertex2f(bx1-2,by1-2);
glEnd();

//down quad
glBegin(GL_QUADS);
glVertex2f(bx,by);
glVertex2f(bx+2,by+2);
glVertex2f(bx1-2,by+2);
glVertex2f(bx1,by);
glEnd();

//left
glColor3f(0.8f,0.8f,0.8f);
glBegin(GL_QUADS);
glVertex2f(bx,by);
glVertex2f(bx+2,by+2);
glVertex2f(bx+2,by1-2);
glVertex2f(bx,by1);
glEnd();

//upperquad
glBegin(GL_QUADS);
glVertex2f(bx,by1);
glVertex2f(bx1,by1);
glVertex2f(bx1-2,by1-2);
glVertex2f(bx+2,by1-2);

glColor3f(0.0f,0.0f,0.0f);
glEnd();


for(xpos=0;button_text1[xpos]!='\0';xpos++);
xposinc = xpos;
xpos = (100-16) - (xposinc*8);
xpos /= 2;
glRasterPos2f(xb+xpos,yb+5);
for(int i=0;i<xposinc;i++)
glutBitmapCharacter(GLUT_BITMAP_8_BY_13,button_text1[i]);

glutSwapBuffers();
}



void button::ButtonPressed() {
char arr[900000];
glDrawBuffer(GL_FRONT);
glPixelTransferi(GL_MAP_COLOR, false);
glReadPixels(xb-1, yb-1, 82, 22, GL_RGB, GL_UNSIGNED_BYTE, arr);
glRasterPos2i(xb, yb-2);
glDrawPixels(82, 22, GL_RGB, GL_UNSIGNED_BYTE,arr);
usleep(50000);
glRasterPos2i(xb-1, yb-1);
glDrawPixels(82, 22, GL_RGB, GL_UNSIGNED_BYTE, arr);
glDrawBuffer(GL_BACK);
glutSwapBuffers();
}

int button::IsButtonClicked(GLint x, GLint y) {
if((x >= xb && x <= (xb+80)) && (y >= yb && y <= (yb+20)))
return 1;
else
return 0;

}




Changed tags to source tags, please use these when you have more than a couple of lines of code to page

[Edited by - kmyadam on August 5, 2005 3:44:37 PM]

Share this post


Link to post
Share on other sites
another mode is(maybe you want to add); if you only check mouse coordinate when LMB is down the action will be start immediatly. this is not good for some systems(e.g, you press the "X" button of window, but you can still give up to close it before realesing the mouse button).
you should check mouse again when it is realesed and control the button again. if still mouse over there, then the action should start.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this