-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
170 lines (151 loc) · 4.24 KB
/
Copy pathButton.cpp
File metadata and controls
170 lines (151 loc) · 4.24 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "Button.h"
namespace FD2D
{
Button::Button()
: Wnd()
{
SetContentMargin(10.0f, 5.0f);
SetContentAlign(AlignH::Center, AlignV::Center);
}
Button::Button(const std::wstring& name)
: Wnd(name)
{
SetContentMargin(10.0f, 5.0f);
SetContentAlign(AlignH::Center, AlignV::Center);
}
Size Button::Measure(Size available)
{
Size labelSize = m_label.Measure(available);
m_desired = {
labelSize.w + m_contentMargin.Horizontal() + 2.0f * m_margin,
labelSize.h + m_contentMargin.Vertical() + 2.0f * m_margin
};
return m_desired;
}
void Button::SetRect(const D2D1_RECT_F& rect)
{
SetLayoutRect(rect);
}
void Button::Arrange(Rect finalRect)
{
Wnd::Arrange(finalRect);
Size labelSize = m_label.Measure({ 0.0f, 0.0f });
m_label.SetRect(ToD2D(ContentRectFor(labelSize)));
}
void Button::SetLabel(const std::wstring& text)
{
m_label.SetText(text);
NotifyContentLayoutChanged();
}
void Button::SetColors(const D2D1_COLOR_F& normal, const D2D1_COLOR_F& hot, const D2D1_COLOR_F& pressed)
{
m_colorNormal = normal;
m_colorHot = hot;
m_colorPressed = pressed;
}
void Button::OnClick(ClickHandler handler)
{
m_click = std::move(handler);
}
bool Button::OnInputEvent(const InputEvent& event)
{
switch (event.type)
{
case InputEventType::MouseMove:
{
if (!event.hasPoint)
{
return false;
}
POINT pt = event.point;
bool prevHover = m_hovered;
m_hovered = HitTest(pt);
if (m_hovered != prevHover)
{
Invalidate();
}
return m_hovered;
}
case InputEventType::MouseDown:
{
if (event.button != MouseButton::Left || !event.hasPoint)
{
break;
}
POINT pt = event.point;
if (HitTest(pt))
{
m_pressed = true;
Invalidate();
return true;
}
break;
}
case InputEventType::MouseUp:
{
if (event.button != MouseButton::Left || !event.hasPoint)
{
break;
}
bool wasPressed = m_pressed;
m_pressed = false;
POINT pt = event.point;
if (wasPressed && HitTest(pt))
{
if (m_click)
{
m_click();
}
Invalidate();
return true;
}
if (wasPressed)
{
Invalidate();
}
break;
}
default:
break;
}
return Wnd::OnInputEvent(event);
}
void Button::OnRender(ID2D1RenderTarget* target)
{
if (target == nullptr)
{
return;
}
if (!m_brush)
{
target->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &m_brush);
}
D2D1_COLOR_F fillColor = m_colorNormal;
if (m_pressed)
{
fillColor = m_colorPressed;
}
else if (m_hovered)
{
fillColor = m_colorHot;
}
// Flat, rounded surface with a subtle hairline border that lifts to the
// accent blue on hover (replaces the old hard white 1.5px outline).
D2D1_ROUNDED_RECT rr = D2D1::RoundedRect(LayoutRect(), 4.0f, 4.0f);
m_brush->SetColor(fillColor);
target->FillRoundedRectangle(rr, m_brush.Get());
m_brush->SetColor(m_hovered ? D2D1::ColorF(0.26f, 0.55f, 0.96f, 0.90f)
: D2D1::ColorF(1.0f, 1.0f, 1.0f, 0.16f));
target->DrawRoundedRectangle(rr, m_brush.Get(), 1.0f);
m_label.OnRender(target);
Wnd::OnRender(target);
}
bool Button::HitTest(const POINT& pt) const
{
const auto& rect = LayoutRect();
return pt.x >= rect.left &&
pt.x <= rect.right &&
pt.y >= rect.top &&
pt.y <= rect.bottom;
}
}