Button.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "Button.h"
  2. #include "Painter.h"
  3. Button::Button(Widget* parent)
  4. : Widget(parent)
  5. {
  6. }
  7. Button::~Button()
  8. {
  9. }
  10. void Button::setCaption(String&& caption)
  11. {
  12. if (caption == m_caption)
  13. return;
  14. m_caption = move(caption);
  15. update();
  16. }
  17. void Button::paintEvent(PaintEvent&)
  18. {
  19. Color buttonColor = Color::LightGray;
  20. Color highlightColor = Color::White;
  21. Color shadowColor = Color(96, 96, 96);
  22. Painter painter(*this);
  23. painter.drawPixel({ 0, 0 }, backgroundColor());
  24. painter.drawPixel({ width() - 1, 0 }, backgroundColor());
  25. painter.drawPixel({ 0, height() - 1 }, backgroundColor());
  26. painter.drawPixel({ width() - 1, height() - 1 }, backgroundColor());
  27. painter.drawLine({ 1, 0 }, { width() - 2, 0 }, Color::Black);
  28. painter.drawLine({ 1, height() - 1 }, { width() - 2, height() - 1}, Color::Black);
  29. painter.drawLine({ 0, 1 }, { 0, height() - 2 }, Color::Black);
  30. painter.drawLine({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black);
  31. if (m_beingPressed) {
  32. // Base
  33. painter.fillRect({ 1, 1, width() - 2, height() - 2 }, buttonColor);
  34. // Sunken shadow
  35. painter.drawLine({ 1, 1 }, { width() - 2, 1 }, shadowColor);
  36. painter.drawLine({ 1, 2 }, {1, height() - 2 }, shadowColor);
  37. } else {
  38. // Base
  39. painter.fillRect({ 3, 3, width() - 6, height() - 6 }, buttonColor);
  40. // White highlight
  41. painter.drawLine({ 1, 1 }, { width() - 2, 1 }, highlightColor);
  42. painter.drawLine({ 1, 2 }, { width() - 3, 2 }, highlightColor);
  43. painter.drawLine({ 1, 3 }, { 1, height() - 2 }, highlightColor);
  44. painter.drawLine({ 2, 3 }, { 2, height() - 3 }, highlightColor);
  45. // Gray shadow
  46. painter.drawLine({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadowColor);
  47. painter.drawLine({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadowColor);
  48. painter.drawLine({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadowColor);
  49. painter.drawLine({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadowColor);
  50. }
  51. if (!caption().is_empty()) {
  52. auto textRect = rect();
  53. if (m_beingPressed)
  54. textRect.moveBy(1, 1);
  55. painter.drawText(textRect, caption(), Painter::TextAlignment::Center, Color::Black);
  56. }
  57. }
  58. void Button::mouseDownEvent(MouseEvent& event)
  59. {
  60. printf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  61. m_beingPressed = true;
  62. update();
  63. Widget::mouseDownEvent(event);
  64. }
  65. void Button::mouseUpEvent(MouseEvent& event)
  66. {
  67. printf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
  68. m_beingPressed = false;
  69. update();
  70. Widget::mouseUpEvent(event);
  71. if (onClick)
  72. onClick(*this);
  73. }