[text] button

Viewer

  1. #ifndef BUTTON_H
  2. #define BUTTON_H
  3.  
  4. #include <GL/freeglut.h>
  5. #include <GL/gl.h>
  6. #include <string>
  7.  
  8. struct Button {
  9.     std::string text;
  10.     float x;
  11.     float y;
  12.     float w;
  13.     float h;
  14.     bool pressed;
  15.  
  16.     Button() {
  17.         x = 0.0f;
  18.         y = 0.0f;
  19.         w = 0.4f;
  20.         h = 0.2f;
  21.         text = "Button";
  22.         pressed = false;
  23.         float textWidth = 0.0f;
  24.         for (int i = 0; i < text.length(); i++) {
  25.             textWidth += glutBitmapWidth(GLUT_BITMAP_9_BY_15, text[i]);
  26.         }
  27.         float padding = 0.06;
  28.         textWidth = 2.0f * (textWidth / 400.0);
  29.         w = textWidth + padding;
  30.     }
  31.  
  32.     Button(std::string text, float x, float y) {
  33.         this->text = text;
  34.         this->x = x;
  35.         this->y = y;
  36.         w = 0.4f;
  37.         h = 0.2f;
  38.         pressed = false;
  39.         float textWidth = 0.0f;
  40.         for (int i = 0; i < text.length(); i++) {
  41.             textWidth += glutBitmapWidth(GLUT_BITMAP_9_BY_15, text[i]);
  42.         }
  43.         float padding = 0.06;
  44.         textWidth = 2.0f * (textWidth / 400.0);
  45.         w = textWidth + padding;
  46.     }
  47.  
  48.     void draw() {
  49.  
  50.         float textWidth = 0.0f;
  51.         for (int i = 0; i < text.length(); i++) {
  52.             textWidth += glutBitmapWidth(GLUT_BITMAP_9_BY_15, text[i]);
  53.         }
  54.         float padding = 0.06;
  55.         textWidth = 2.0f * (textWidth / 400.0);
  56.         w = textWidth + padding;
  57.         
  58.         glColor3f(1.0f, 1.0f, 1.0f);
  59.         if (pressed) glColor3f(0.8, 0.8, 0.8);
  60.         glBegin(GL_POLYGON);
  61.             glVertex2f(x, y);
  62.             glVertex2f(x + w, y);
  63.             glVertex2f(x + w, y - h);
  64.             glVertex2f(x, y - h);
  65.         glEnd();
  66.  
  67.         glColor3f(0.0f, 0.0f, 0.0f);
  68.         glBegin(GL_LINES);
  69.             glVertex2f(x, y);
  70.             glVertex2f(x + w, y);
  71.  
  72.             glVertex2f(x + w, y);
  73.             glVertex2f(x + w, y - h);
  74.  
  75.             glVertex2f(x + w, y - h);
  76.             glVertex2f(x, y - h);
  77.  
  78.             glVertex2f(x, y - h);
  79.             glVertex2f(x, y);
  80.         glEnd();
  81.  
  82.      
  83.         glRasterPos2f(x + (padding / 2), y - (h / 2) - 0.025);
  84.         for (int i = 0; i < text.length(); i++) {
  85.             glutBitmapCharacter(GLUT_BITMAP_9_BY_15, text[i]);
  86.         }
  87.     }
  88.  
  89.     bool contains(float mx, float my) {
  90.         if (mx >= x && mx <= x + w && my <= y && my >= y - h) {
  91.             return true;
  92.         } else {
  93.             return false;
  94.         }
  95.     }
  96. };
  97.  
  98. #endif

Editor

You can edit this paste and save as new:


File Description
  • button
  • Paste Code
  • 20 Apr-2024
  • 2.45 Kb
You can Share it: