proj home

Files   Classes   Functions   Hierarchy  

menusystem.cpp

Go to the documentation of this file.
00001 #include <cassert>
00002 #include <string>
00003 
00004 #include <menusystem.h>
00005 #include <tokenizer.h>
00006 
00007 
00008 namefont::namefont
00009 (
00010   point2<GLint> const & X_,
00011   stringc & name_, 
00012   void* font_
00013 )
00014   : X(X_), name(name_), font(font_) 
00015 {
00016 }
00017 
00018 
00019 void namefont::draw()
00020 {
00021   GOBJDEBUGCODE
00022   assert(font!=0);
00023 
00024   glRasterPos2i(X.x,X.y);
00025   uintc kmax = name.size();
00026   for (uint k=0; k<kmax; ++k)
00027     glutBitmapCharacter(font, name[k]);
00028 }
00029 
00030 
00031 
00032 void menusystem::addsubmenu(menusystem* submenu)
00033 {
00034   submenues.push_back(submenu);
00035   submenu->parent = this;
00036 }
00037 
00038 menusystem::~menusystem()
00039 {
00040   uint imax = submenues.size();
00041   for (uint i=0; i<imax; ++i)
00042   {
00043     delete submenues[i];
00044     submenues[i]=0;
00045   }
00046 }
00047 
00048 void menusystem::drawparent()
00049 {
00050   assert(parent!=0);
00051 
00052   if (parent!=0)
00053     draw(parent);
00054 }
00055 
00056 void menusystem::draw(menusystem *x)
00057 {
00058   assert(x!=0);
00059 
00060   // Save previous state.
00061   menusystem * t2 = x->current;
00062 
00063   // draw
00064   x->current = x;
00065   x->draw();
00066   x->current = t2;
00067 }
00068 
00069 void menusystem::drawparents()
00070 {
00071   deque<menusystem*> parents;
00072   menusystem * t2 = this->parent;
00073   for ( ; t2 != 0; t2 = t2->parent )
00074     parents.push_front(t2);
00075 
00076   uint sz=parents.size();
00077   for (uint i=0; i<sz; ++i)
00078     draw(parents[i]);
00079 }
00080 
00081 
00082 void menusystem::drawparentsnested()
00083 {
00084   deque<menusystem*> parents;
00085   menusystem * t2 = this->parent;
00086   for ( ; t2 != 0; t2 = t2->parent )
00087     parents.push_front(t2);
00088 
00089   uint sz=parents.size();
00090 
00091   if (sz==0)
00092   {
00093     menusystem::draw();
00094     return;
00095   }
00096 
00097   for (uint i=0; i<sz; ++i)
00098   {
00099     assertreturn(parents[i]);
00100 
00101     parents[i]->drawpre.draw();
00102     parents[i]->visibleGraphics.draw();
00103   }
00104 
00105   menusystem::draw();
00106 
00107   for (uint i=0; i<sz; ++i)
00108     parents[sz-1-i]->drawpost.draw();
00109 }
00110 
00111 void menusystem::transfercontroltoroot()
00112 {
00113   if (parent==0)
00114     return;
00115 
00116   current = parent;
00117   for ( ; current->parent != 0; current = current->parent );
00118   current->current = current;
00119 }
00120 
00121 void menusystem::transfercontroltoparent()
00122 {
00123   assert(parent!=0);
00124   current = parent;
00125   parent->current = parent;
00126 }
00127 
00128 
00129 void menusystem::transfercontroltosubmenu(uint k)
00130 {
00131   assert( k<submenues.size() );
00132 
00133   assert(submenues[k]!=0);
00134 
00135   submenues[k]->current = submenues[k];
00136   current = submenues[k];
00137 }
00138 
00139 /*
00140 Let the client do this.
00141 void menusystem::nodedelete()
00142 {
00143   uintc sz = node.size(); 
00144   for (uint i=0; i<sz; ++i) 
00145   {
00146     if (node[i]==0)
00147       continue;
00148 
00149     node[i]->nodedelete();
00150     delete node[i];
00151   }
00152 
00153   node.clear();
00154 }
00155 */
00156 
00157 void menusystem::readBufferedString(uintc index)
00158 {
00159   readBufferedIndex = index;
00160   readBufferedStringInit = "";
00161   vItems[index]->name="";
00162   //glutPostRedisplay();
00163   readBufferedResult = "";
00164 
00165   readmodeimmediate=false;
00166 }
00167 
00168 void menusystem::readBufferedSet(uintc index)
00169 {
00170   readBufferedIndex = index;
00171   readBufferedStringInit = vItems[readBufferedIndex]->name;
00172   readBufferedResult = "";
00173 
00174   readmodeimmediate=false;
00175 }
00176 
00177 void menusystem::readBuffered(charc ch)
00178 {
00179 //uint k = ch;
00180 //cout << "*" << k << "*" << endl; 
00181 
00182   // Return key pressed.
00183   if (ch==13)
00184   {
00185     readmodeimmediate=true;
00186     vItems[readBufferedIndex]->name = readBufferedStringInit;
00187     readBufferedTerminationAction();
00188     return;
00189   }
00190 
00191   // Backspace key pressed
00192   if (ch==8)
00193   {
00194     uint sz = readBufferedResult.size();
00195 
00196     if (sz==0)
00197       return;
00198 
00199     // sz>0
00200     readBufferedResult.erase(sz-1,1);
00201     string & s(vItems[readBufferedIndex]->name);
00202     s.erase(s.size()-1,1);
00203 
00204     return;
00205   }
00206 
00207   readBufferedResult += ch;
00208   vItems[readBufferedIndex]->name += ch;
00209 }
00210 
00211 
00212 void menusystem::read(charc ch)
00213 {
00214   assertreturn(current!=0);
00215 
00216   for ( ; current->current!=current; )
00217   {
00218     current = current->current;
00219     assert(current!=0);
00220   }
00221 
00222   if (current->readmodeimmediate)
00223     current->readImmediate(ch);
00224   else
00225     current->readBuffered(ch);
00226 
00227   glutPostRedisplay();
00228 }
00229 
00230 void menusystem::draw()
00231 {
00232   GOBJDEBUGCODE
00233   if (this!=current)
00234   {
00235     if (current!=0)
00236     {
00237       current->draw();
00238       return;
00239     }
00240   }
00241 
00242   drawpre.draw();
00243 
00244   visibleGraphics.draw();
00245 
00246   drawpost.draw();
00247 }
00248 
00249 void menusystem::fontcolorenable()
00250 {
00251 // glDisable(GL_DEPTH_TEST);
00252 // Test for depth test state!
00253   drawpre.push_back(new gobjglDisable(GL_DEPTH_TEST));
00254   drawpre.push_back(new gobjglEnable(GL_BLEND));
00255   drawpre.push_back(new gobjglBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA));
00256 
00257   drawpre.push_back(new gobjglColor4f(fontcolor.x,fontcolor.y,fontcolor.z,fontcolor.w));
00258 
00259   drawpost.push_front(new gobjglEnable(GL_DEPTH_TEST));
00260   drawpost.push_front(new gobjglDisable(GL_BLEND));
00261 }
00262 
00263 void menusystem::addfont10paragraphstart
00264 (
00265   stringc & s, 
00266   uintc len,
00267   uintc newlines
00268 )
00269 {
00270   for (uint i=0; i<newlines; ++i)
00271     scrolldown();
00272 
00273   uint slen = s.size();
00274   uint k=0;
00275   size_t k2;
00276   uint k3;
00277   uint k4;
00278 
00279   vector<string> vi;
00280 
00281   for ( ; k<slen; )
00282   {
00283     k4 = k + len;
00284     // Test if at the end of the iteration.
00285     //if (slen-k <= len )
00286     if (k4 >= slen)
00287     {
00288       vi.push_back(s.substr(k));
00289       break;
00290     }
00291 
00292     k2 = s.find(" ",k4);
00293     if (k2==string::npos)
00294     {
00295       vi.push_back(s.substr(k));
00296       break; 
00297     }
00298 
00299     vi.push_back(s.substr(k,k2-k));
00300 
00301     k=k2;
00302 
00303     k3 = s.find_first_not_of(" ",k);
00304     if (k3==string::npos)
00305       break;
00306 
00307     k = k3;
00308   }
00309 
00310   uintc sz=vi.size();
00311   for ( uint i=0; i<sz; ++i)
00312   {
00313     addfont10start(vi[sz-i-1],1); 
00314   }
00315 }
00316 
00317 
00318 void menusystem::addfont10paragraph
00319 (
00320   stringc & s, 
00321   uintc len,
00322   uintc newlines
00323 )
00324 {
00325   uint slen = s.size();
00326   uint k=0;
00327   uint k2;
00328   uint k3;
00329   uint k4;
00330 
00331   for ( ; k<slen; )
00332   {
00333     k4 = k + len;
00334     // Test if at the end of the iteration.
00335     //if (slen-k <= len )
00336     if (k4 >= slen)
00337     {
00338       addfont10(s.substr(k),1); 
00339       //addnewline();
00340 
00341       //return;
00342       break;
00343     }
00344 
00345     k2 = s.find(" ",k4);
00346     if (k2==string::npos)
00347     {
00348       addfont10(s.substr(k),1); 
00349       //addnewline();
00350       //return;
00351       break; 
00352     }
00353 
00354     addfont10(s.substr(k,k2-k),1);
00355     //addnewline();
00356 
00357     k=k2;
00358 
00359     k3 = s.find_first_not_of(" ",k);
00360     if (k3==string::npos)
00361       //return;
00362       break;
00363 
00364     k = k3;
00365   }
00366 
00367   addnewline(newlines);
00368 }
00369 
00370 
00371 void menusystem::addfont10(uint & index, stringc & s, uintc newlines)
00372 {
00373   vItems.push_back( new namefont(X,s,GLUT_BITMAP_HELVETICA_10) );
00374   index = vItems.size()-1;
00375   visibleGraphics.push( vItems[index] );
00376 
00377   addnewline(newlines);
00378 }
00379 
00380 void menusystem::addfont10(stringc & s, point2<GLint> const & X2 )
00381 {
00382   vItems.push_back( new namefont(X2,s,GLUT_BITMAP_HELVETICA_10) );
00383   visibleGraphics.push( vItems[vItems.size()-1] );
00384 }
00385 
00386 
00387 void menusystem::addfont10(stringc & s, uintc newlines)
00388 {
00389   vItems.push_back( new namefont(X,s,GLUT_BITMAP_HELVETICA_10) );
00390   visibleGraphics.push( vItems[vItems.size()-1] );
00391 
00392   addnewline(newlines);
00393 }
00394 
00395 void menusystem::addfont10start(stringc & s, uintc newlines)
00396 {
00397   for (uint i=0; i<newlines; ++i)
00398     scrolldown();
00399 
00400   vItems.push_back( new namefont(X0,s,GLUT_BITMAP_HELVETICA_10) );
00401   visibleGraphics.push( vItems[vItems.size()-1] );
00402 }
00403 
00404 void menusystem::addfont12(uint & index, stringc & s, uintc newlines)
00405 {
00406 //cout << SHOW(X) << " " << s << endl;
00407   vItems.push_back( new namefont(X,s,GLUT_BITMAP_HELVETICA_12) );
00408   index = vItems.size()-1;
00409   visibleGraphics.push( vItems[index] );
00410 
00411   addnewline(newlines);
00412 }
00413 
00414 void menusystem::addfont12start(stringc & s, uintc newlines)
00415 {
00416   for (uint i=0; i<newlines; ++i)
00417     scrolldown();
00418 
00419   vItems.push_back( new namefont(X0,s,GLUT_BITMAP_HELVETICA_12) );
00420   visibleGraphics.push( vItems[vItems.size()-1] );
00421 }
00422 
00423 void menusystem::addfont12(stringc & s, uintc newlines)
00424 {
00425 //cout << "@" << SHOW(X) << " " << s << endl;
00426   vItems.push_back( new namefont(X,s,GLUT_BITMAP_HELVETICA_12) );
00427   visibleGraphics.push( vItems[vItems.size()-1] );
00428 
00429   addnewline(newlines);
00430 }
00431 
00432 void menusystem::addfont12(stringc & s, point2<GLint> const & X2 )
00433 {
00434   vItems.push_back( new namefont(X2,s,GLUT_BITMAP_HELVETICA_12) );
00435   visibleGraphics.push( vItems[vItems.size()-1] );
00436 }
00437 
00438 
00439 void menusystem::addfont10blockstart
00440 (
00441   stringc & s, 
00442   uintc newlines
00443 )
00444 {
00445   scroll(newlines);
00446 
00447   tokenizer tz(s);
00448 
00449   tz.subtract("\n");
00450   vector<string> vi;
00451   for ( list<string>::reverse_iterator i = tz.seq.rbegin(); i!=tz.seq.rend(); ++i )
00452     vi.push_back(*i);
00453 
00454   for (uint i=0; i<vi.size(); ++i)
00455     addfont10start(vi[i],1);
00456 }
00457 
00458 menusystem::menusystem
00459 (
00460   menusystem * current_,
00461   menusystem * parent_,
00462   bool readmodeimmediate_,
00463   point2<GLint> const & X0_,
00464   GLint const columnchange_
00465 ) : 
00466   current(current_), 
00467   parent(parent_), 
00468   readmodeimmediate(readmodeimmediate_),
00469   visibleGraphics(true),
00470   X0(X0_), 
00471   columnchange(columnchange_),
00472   fontcolor(1.0,1.0,1.0,0.8) 
00473 {
00474   reset();
00475 }
00476 
00477 menusystem::menusystem
00478 (
00479   bool readmodeimmediate_,
00480   point2<GLint> const & X0_,
00481   GLint const columnchange_
00482 ) : 
00483   current(0), 
00484   parent(0), 
00485   readmodeimmediate(readmodeimmediate_),
00486   visibleGraphics(true),
00487   X0(X0_), 
00488   columnchange(columnchange_), 
00489   fontcolor(1.0,1.0,1.0,0.8) 
00490 {
00491   reset();
00492 }
00493 
00494 menusystem::menusystem
00495 (
00496   point2<GLint> const & X0_,
00497   GLint const columnchange_
00498 ) : 
00499   current(0), 
00500   parent(0), 
00501   readmodeimmediate(true),
00502   visibleGraphics(true),
00503   X0(X0_), 
00504   columnchange(columnchange_), 
00505   fontcolor(1.0,1.0,1.0,0.8) 
00506 {
00507   reset();
00508 }
00509 
00510 void menusystem::scroll(GLint const k)
00511 {
00512   uintc imax = vItems.size();
00513   for (uint i=0; i<imax; ++i)
00514   {
00515     if (vItems[i]==0)
00516       continue;
00517 
00518     vItems[i]->X.y += k;
00519   }
00520 }
00521 
00522 void menusystem::scrolldown()
00523 {
00524   scroll(columnchange);
00525 }
00526 
00527 void menusystem::scrollup()
00528 {
00529   scroll(-columnchange);
00530 }
00531 
00532 void menusystemOneShot::draw()
00533 {
00534   GOBJDEBUGCODE
00535   switch (drawchoice)
00536   {
00537     case drawmenuthis:
00538       break;
00539     case drawmenuparent:
00540       drawparent();
00541       break;
00542     case drawmenuparents:
00543       drawparents();
00544       break;
00545   }
00546   menusystem::draw();
00547 }
00548 
00549 void menusystem::Xpush()
00550 {
00551   Xstack.push_back(X);
00552 }
00553 
00554 void menusystem::Xpop()
00555 {
00556   uint sz = Xstack.size();
00557   if (sz==0)
00558     return;
00559   X = Xstack[sz-1];
00560 
00561   Xstack.pop_back();
00562 }
00563 
00564 void menusystem::addfont10paragraphs
00565 (
00566   stringc & s, 
00567   uintc len, 
00568   stringc & token
00569 )
00570 {
00571   tokenizer tk(s);
00572   tk.subtract(token);
00573 
00574   for ( tk.reset(); !tk; ++tk)
00575     addfont10paragraph(tk(),len,0);
00576 }
00577 
00578 
00579 
00580 

Generated on Fri Mar 4 00:49:27 2011 for Chelton Evans Source by  doxygen 1.5.8