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