Button.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /*if (action != nullptr)
  22. delete action;*/
  23. }
  24. void Button::draw(void)
  25. {
  26. glColor4f(background.x/255.0f, background.y / 255.0f, background.z / 255.0f, alfa);
  27. glBegin(GL_QUADS);
  28. glVertex2f(planePosition.x, planePosition.y);
  29. glVertex2f(planePosition.x, planePosition.y + height);
  30. glVertex2f(planePosition.x + width, planePosition.y + height);
  31. glVertex2f(planePosition.x + width, planePosition.y);
  32. glEnd();
  33. /*glColor4f(foreground.x / 255.0f, foreground.y / 255.0f, foreground.z / 255.0f, 1.0f);
  34. Util::glutBitmapString(text, position.x, position.y+height/2+14/2);*/
  35. Text::draw();
  36. }
  37. void Button::update(int x, int y)
  38. {
  39. cursorOnButton =
  40. (x > planePosition.x && x < planePosition.x + width) &&
  41. y > planePosition.y && y < planePosition.y + height;
  42. if (cursorOnButton)
  43. alfa = 1.0f;
  44. else
  45. alfa = 0.5f;
  46. if (cursorOnButton && Cursor::getInstance()->clicked)
  47. if(action != nullptr)
  48. action(this);
  49. }
  50. void Button::addAction(action_function action)
  51. {
  52. this->action = action;
  53. }
  54. void Button::setForeground(Vec3f color)
  55. {
  56. this->setColor(color);
  57. }
  58. void Button::setBackground(Vec3f color)
  59. {
  60. background = color;
  61. }