Button.cpp 1.5 KB

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