Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

menumenu.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "menumenu.h"
  2. namespace segl {
  3. MenuMenu::MenuMenu() {
  4. itempos = 0;
  5. centermenu = false;
  6. centerScreenX = false;
  7. dohighlight = true;
  8. paintbackground = true;
  9. basefontsize = 20;
  10. maxwidth = 0;
  11. valuewidth = 100;
  12. offset = 20;
  13. }
  14. void MenuMenu::setPos(const Punkt2D &p) {
  15. menupos = p;
  16. }
  17. void MenuMenu::setCenter(bool _c) {
  18. centermenu = _c;
  19. }
  20. void MenuMenu::setCenterScreenX(bool _c) {
  21. centerScreenX = _c;
  22. }
  23. void MenuMenu::setBaseFontSize(int _bfs) {
  24. if(_bfs>0)
  25. basefontsize = _bfs;
  26. }
  27. void MenuMenu::setMaxWidth(int _mw) {
  28. if(_mw>0)
  29. maxwidth = _mw;
  30. }
  31. void MenuMenu::setPaintBackground(bool _pbg) {
  32. paintbackground = _pbg;
  33. }
  34. void MenuMenu::addMenuItem(MenuItem* mi) {
  35. menuitems.push_back(mi);
  36. }
  37. void MenuMenu::up() {
  38. do {
  39. if(itempos==0)
  40. itempos = menuitems.size()-1;
  41. else
  42. itempos--;
  43. } while(!menuitems[itempos]->isSelectable());
  44. }
  45. void MenuMenu::down() {
  46. do {
  47. if((unsigned int)itempos==menuitems.size()-1)
  48. itempos = 0;
  49. else
  50. itempos++;
  51. } while(!menuitems[itempos]->isSelectable());
  52. }
  53. void MenuMenu::left() {
  54. menuitems[itempos]->left();
  55. }
  56. void MenuMenu::right() {
  57. menuitems[itempos]->right();
  58. }
  59. void MenuMenu::select() {
  60. menuitems[itempos]->select();
  61. }
  62. void MenuMenu::charInput(char c) {
  63. menuitems[itempos]->charInput(c);
  64. }
  65. void MenuMenu::resetItemPos() {
  66. dohighlight = true;
  67. for(itempos = 0; (unsigned int)itempos<menuitems.size()&&!menuitems[itempos]->isSelectable(); itempos++);
  68. if((unsigned int)itempos==menuitems.size()) {
  69. itempos = 0;
  70. dohighlight = false;
  71. }
  72. }
  73. void MenuMenu::render() {
  74. Punkt2D pos = menupos;
  75. pos.y += offset;
  76. if(centerScreenX) {
  77. SDL_Surface *screen = SDL_GetVideoSurface();
  78. pos.x = screen->w/2;
  79. }
  80. int clen=0, vlen=0, cvlen=0, height=0, fullvalwidth=0, maxlen=0, extravalwidth=0;
  81. for(unsigned int i=0; i<menuitems.size(); i++) {
  82. fontengine.setSize(basefontsize+menuitems[i]->getFontSizeAdd());
  83. if(menuitems[i]->hasValue()) {
  84. // cvlen = std::max(cvlen, fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()));
  85. cvlen = std::max(cvlen, fontengine.getTextWidth(menuitems[i]->getCaption()));
  86. vlen = std::max(vlen, fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()) + fontengine.getTextWidth(menuitems[i]->getCaption()) + valuewidth);
  87. extravalwidth = std::max(extravalwidth, fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()));
  88. // if(menuitems[i]->getMaxValueLen()==9)
  89. // std::cout << "Moep: " << menuitems[i]->getMaxValueLen() << " * " << fontengine.getTextWidthbyInt(1) << " + " << fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()) << " + " << valuewidth << " und das erg: " << fontengine.getTextWidthbyInt(menuitems[i]->getMaxValueLen()) + fontengine.getTextWidth(menuitems[i]->getCaption()) + valuewidth<< std::endl;
  90. } else {
  91. clen = std::max(clen, fontengine.getTextWidth(menuitems[i]->getCaption()));
  92. }
  93. // std::cout << "moep" << extravalwidth << std::endl;
  94. height += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
  95. }
  96. // fullvalwidth = clen + vlen + valuewidth;
  97. maxlen = std::max(clen, cvlen+extravalwidth+(cvlen>0?valuewidth:0));
  98. glDisable(GL_DEPTH_TEST);
  99. if(paintbackground&&true) {
  100. // geht noch nicht ganz
  101. glDisable(GL_TEXTURE_2D);
  102. // glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
  103. glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
  104. glEnable(GL_BLEND);
  105. SDL_Rect bgarea = { 0, (int)pos.y, 0, height };
  106. if(vlen>0&&fullvalwidth>=clen) {
  107. bgarea.x = (int)pos.x-fullvalwidth/2;
  108. bgarea.w = fullvalwidth;
  109. } else {
  110. bgarea.x = (int)pos.x-clen/2;
  111. bgarea.w = clen;
  112. }
  113. bgarea.x = (Sint16)pos.x-maxlen/2;
  114. bgarea.w = maxlen;
  115. //offset
  116. // bgarea.x += offset;
  117. // bgarea.y += offset;
  118. bgarea.w += offset*2;
  119. bgarea.h += offset;
  120. //Wenn schatten dann hier...
  121. // bgarea.x += offset;
  122. // bgarea.y += offset;
  123. bgarea.x -= offset/2;
  124. bgarea.y -= offset/2;
  125. glColor4f(0.1f, 0.1f, 0.1f, 0.2f);
  126. GLFontEngine::paintSDLRect(bgarea);
  127. bgarea.x -= offset/2;
  128. bgarea.y -= offset/2;
  129. glColor4f(0.2f, 0.2f, 0.2f, 0.3f);
  130. GLFontEngine::paintSDLRect(bgarea);
  131. glDisable(GL_BLEND);
  132. }
  133. for(unsigned int i=0; i<menuitems.size(); i++) {
  134. menuitems[i]->render(pos, centermenu, basefontsize, maxwidth, valuewidth, ((unsigned int)itempos==i)&&dohighlight, clen, cvlen, cvlen+extravalwidth+valuewidth);
  135. pos.y += basefontsize + menuitems[i]->getFontSizeAdd() + offset;
  136. }
  137. glEnable(GL_DEPTH_TEST);
  138. }
  139. } // namespace segl