Files Classes Functions Hierarchy
00001 #include <buttonpanel02.h> 00002 00003 00004 buttonpanel02::buttonpanel02(zprmouse* zm_) 00005 : zm(zm_) 00006 { 00007 ratio_center = point2<double>(0.9,0.8); 00008 ratio_centerdelta = 0.012; 00009 ratio_radius = 0.008; 00010 00011 /* 00012 color[0] = point3<uint>(239,151,255); 00013 color[1] = point3<uint>(127,255,0); 00014 color[2] = point3<uint>(240,192,10); 00015 color[3] = point3<uint>(239,254,240); 00016 */ 00017 color[0] = point3<uint>(255,0,0); 00018 color[1] = point3<uint>(0,255,0); 00019 color[2] = point3<uint>(0,0,255); 00020 color[3] = point3<uint>(127,127,127); 00021 00022 update(); 00023 } 00024 00025 void buttonpanel02::update() 00026 { 00027 assert(zm); 00028 if (zm==0) 00029 return; 00030 00031 center.x = zm->zz.width * ratio_center.x; 00032 center.y = zm->zz.height * ratio_center.y; 00033 00034 centerdelta = zm->zz.width * ratio_centerdelta; 00035 00036 radius = zm->zz.width * ratio_radius; 00037 00038 } 00039 00040 00041 void buttonpanel02::process 00042 ( 00043 bool & isclicked, 00044 uint& index, 00045 point2<int> const & p 00046 ) 00047 { 00048 assert(centerdelta>0); 00049 assert(radius>0); 00050 00051 isclicked=false; 00052 00053 assert(zm); 00054 // Pushing the button triggers two calls, one 00055 // down and the other up. This rejects up. 00056 if (zm->zz.mouseLeft==false) 00057 return; 00058 00059 00060 // Bounding box rejects most cases. 00061 if (p.x < (center.x - centerdelta - radius)) 00062 return; 00063 if (p.x > (center.x + centerdelta + radius)) 00064 return; 00065 if (p.y < (center.y - centerdelta - radius)) 00066 return; 00067 if (p.y > (center.y + centerdelta + radius)) 00068 return; 00069 00070 point2<double> current(p.x,p.y); 00071 double radius2 = radius*radius; 00072 00073 for (uint i=0; i<4; ++i) 00074 { 00075 point2<double> b0(centerpos(i).x,centerpos(i).y); 00076 if ((b0-current).dot() < radius2) 00077 { 00078 isclicked=true; 00079 index=i; 00080 return; 00081 } 00082 } 00083 00084 } 00085 00086 void buttonpanel02::drawcircle 00087 ( 00088 point2<int> const & c, 00089 point3<uint> const & color 00090 ) const 00091 { 00092 int x1=c.x; 00093 int y1=c.y; 00094 00095 glPushAttrib(GL_CURRENT_BIT); 00096 glPushAttrib(GL_LIGHTING_BIT); 00097 glDisable(GL_LIGHTING); 00098 glColor3ub(color.x,color.y,color.z); 00099 00100 doublec rad=3.141592*2.0/360.0; 00101 00102 glBegin(GL_TRIANGLE_FAN); 00103 glVertex2f(x1,y1); 00104 for (double angle=0; angle<=360; angle+=2) 00105 { 00106 point2<double> x2(x1+ sin(rad*angle)*radius, y1 + cos(rad*angle)*radius); 00107 // cout << SHOW(x2) << " "; 00108 glVertex2d(x2.x,x2.y); 00109 } 00110 glEnd(); 00111 00112 //cout << endl; 00113 00114 glPopAttrib(); 00115 glPopAttrib(); 00116 } 00117 00118 void buttonpanel02::draw() 00119 { 00120 assert(zm); 00121 00122 // Code taken from tutorial 00123 // http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL 00124 00125 glMatrixMode(GL_PROJECTION); 00126 glPushMatrix(); 00127 glLoadIdentity(); 00128 glOrtho(0,zm->zz.width,zm->zz.height,0,0,1); 00129 glDisable(GL_DEPTH_TEST); 00130 glMatrixMode(GL_MODELVIEW); 00131 glPushMatrix(); 00132 glLoadIdentity(); 00133 glTranslatef(0.375,0.375,0); 00134 00135 /* 00136 radius=5; 00137 int x1=320; 00138 int y1=240; 00139 00140 drawcircle(x1,y1,point3<uint>(255,0,0)); 00141 */ 00142 00143 drawcircle(centerpos(0),color[0]); 00144 drawcircle(centerpos(1),color[1]); 00145 drawcircle(centerpos(2),color[2]); 00146 drawcircle(centerpos(3),color[3]); 00147 00148 // Returning to previous state. 00149 glPopMatrix(); 00150 glMatrixMode(GL_PROJECTION); 00151 glPopMatrix(); 00152 glMatrixMode(GL_MODELVIEW); 00153 00154 glEnable(GL_DEPTH_TEST); 00155 } 00156 00157 00158 point2<int> buttonpanel02::centerpos(uintc i) const 00159 { 00160 assert(i<4); 00161 00162 if (i==0) 00163 return point2<int>(center.x,center.y-centerdelta); 00164 if (i==1) 00165 return point2<int>(center.x+centerdelta,center.y); 00166 if (i==2) 00167 return point2<int>(center.x,center.y+centerdelta); 00168 00169 return point2<int>(center.x-centerdelta,center.y); 00170 } 00171 00172 00173 00174 00175
1.5.8