1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include <cmath>
using namespace std;
#define WIDTH 700
#define HEIGHT 600
#define MENUWIDTH 100
#define BOXHEIGHT (HEIGHT/NCOLORS)
#define DSLEFT MENUWIDTH
#define DSRIGHT WIDTH
#define DSBOTTOM 0
#define DSTOP HEIGHT
#define NCOLORS 6
#define RGBRED 1, 0, 0
#define RGBGREEN 0, 1, 0
#define RGBBLUE 0, 0, 1
#define RGBYELLOW 1, 1, 0
#define RGBMAGENTA 1, 0, 1
#define RGBCYAN 0, 1, 1
#define RGBWHITE 1, 1, 1
#define R 0
#define G 1
#define B 2
static float colormenu[6][3] = {{RGBRED}, {RGBGREEN}, {RGBBLUE}, {RGBYELLOW}, {RGBMAGENTA}, {RGBCYAN}};
static float selectedcolor[3];
static int _x;
static int _y;
static bool tracking=false;
int incolormenu(int x, int y)
{
return (x >= 0 && x <= MENUWIDTH && y >= 0 && y <= HEIGHT);
}
int canvas(int x, int y)
{
return (x>DSLEFT && x<DSRIGHT && y>DSBOTTOM && y<DSTOP);
}
int colormenuindex(int x, int y)
{
if(!incolormenu(x, y))
return -1;
else
return(y / BOXHEIGHT);
}
void handleButton(int button, int state, int x, int y)
{
static int menuindex = -1;
y = HEIGHT - y;
if(button != GLUT_LEFT_BUTTON)
return;
if(state == GLUT_DOWN)
{
if(incolormenu(x, y))
{
menuindex = colormenuindex(x, y);
selectedcolor[0]=(colormenu[menuindex][R]);
selectedcolor[1]=(colormenu[menuindex][G]);
selectedcolor[2]=(colormenu[menuindex][B]);
}
if(canvas(x,y))
{
tracking=true;
_x=x;
_y=y;
}
}
else
{
tracking=false;
glFlush();
}
}
void m_motion(int x, int y)
{
y=DSTOP-y;
if (tracking=true && canvas(x,y))
{
glColor3f(selectedcolor[0],selectedcolor[1],selectedcolor[2]);
glBegin(GL_LINES);
glVertex2i(_x,_y);
glVertex2i(x,y);
glEnd();
glFlush();
_x=x;
_y=y;
}
}
void drawMenu()
{
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < NCOLORS; i++)
{
glColor3f(colormenu[i][R], colormenu[i][G], colormenu[i][B]);
glRecti(1, BOXHEIGHT * i + 1, MENUWIDTH - 1, BOXHEIGHT * (i + 1) - 1);
}
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Color Menu");
gluOrtho2D(0, WIDTH, 0, HEIGHT);
glutDisplayFunc(drawMenu);
glutMouseFunc(handleButton);
glutMotionFunc(m_motion);
glClearColor(RGBWHITE, 1);
glutMainLoop();
return 0;
}
|