Button.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "Button.h"
  2. #include <string>
  3. #include "Util.h"
  4. #include "Vector.h"
  5. Button::Button(const std::string & text, Vec2f position, float width, float height) : Text(text,position)
  6. {
  7. this->width = width;
  8. this->height = height;
  9. this->planePosition = position;
  10. cursorOnButton = false;
  11. alfa = 0.5f;
  12. background = Vec3f(10, 10, 10);
  13. //foreground = Vec3f(255, 255, 255);
  14. this->setColor(Vec3f(255, 255, 255));
  15. this->position.x += width / 2 - textWidth / 2;
  16. this->position.y += height / 2 - textHeight / 2;
  17. }
  18. Button::~Button()
  19. {
  20. }
  21. void Button::draw(void)
  22. {
  23. glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, alfa);
  24. glBegin(GL_QUADS);
  25. glVertex2f(planePosition.x, planePosition.y);
  26. glVertex2f(planePosition.x, planePosition.y + height);
  27. glVertex2f(planePosition.x + width, planePosition.y + height);
  28. glVertex2f(planePosition.x + width, planePosition.y);
  29. glEnd();
  30. /*glColor4f(foreground.x / 255.0f, foreground.y / 255.0f, foreground.z / 255.0f, 1.0f);
  31. Util::glutBitmapString(text, position.x, position.y+height/2+14/2);*/
  32. Text::draw();
  33. }
  34. void Button::update(int x, int y)
  35. {
  36. cursorOnButton =
  37. (x > planePosition.x && x < planePosition.x + width) &&
  38. y > planePosition.y && y < planePosition.y + height;
  39. if (cursorOnButton)
  40. alfa = 1.0f;
  41. else
  42. alfa = 0.5f;
  43. }
  44. void Button::setForeground(Vec3f color)
  45. {
  46. this->setColor(color);
  47. }
  48. void Button::setBackground(Vec3f color)
  49. {
  50. background = color;
  51. }