-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpareja-mainwnd-xaml.hpp
329 lines (259 loc) · 9.69 KB
/
pareja-mainwnd-xaml.hpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#pragma once
#include "win32-xaml-island-base.hpp"
#include "pareja-common-xaml.hpp"
namespace Pareja
{
template <typename ClrDlgT> class MainWndXaml : public Win32XamlIsland
{
public:
MainWndXaml() = default;
void OnHostWndResize() const { AdjustWndSize(); } //
void EnableControls(bool) const;
RECT GetTargetRect() const;
auto& ClrDlg() { return clr_dlg; }
private:
void DoInitXamlContent() final;
UIElement DoTopXamlContainer() const final { return grid.as<UIElement>(); }
void AdjustWndSize() const;
Grid grid{};
TextBlock
tb_hdr_c0{}, tb_hdr_c2{}, tb_hdr_c4{},
tb_c0_r1{}, tb_c0_r2{}, tb_c0_r3{},
tb_c2_r1{}, tb_c2_r2{}, tb_c2_r3{},
tb_c4_r1{}, tb_c4_r2{};
Slider
slider_c1_r1{}, slider_c1_r2{},
slider_c3_r1{}, slider_c3_r2{},
slider_c5_r1{}, slider_c5_r2{};
ToggleSwitch ts1{}, ts2{};
StackPanel sp_buttons{};
Button bu_colors{}, bu_step{}, bu_run{};
ClrDlgT clr_dlg{ *this };
};
template<typename ClrDlgT>
void MainWndXaml<ClrDlgT>::DoInitXamlContent()
{
// ----------------- [ The Grid - top level container ] ----------------- //
//SolidColorBrush br;
//br.Color(Colors::LightGray());
//grid.Background(br);
//grid.RequestedTheme(ElementTheme::Dark);
constexpr auto
num_columns = 6, num_rows = 4;
constexpr auto
column_width_odd = 120.0, column_width_even = 150.0;
constexpr double
column_def[num_columns] = {
column_width_odd, column_width_even,
column_width_odd, column_width_even,
column_width_odd, column_width_even + 9.0 },// + 5.0},
row_def[num_rows] = { 30.0, 30.0, 30.0, 50.0 };
double grid_height = 0.0;
ColumnDefinition cd[num_columns]{};
RowDefinition rd[num_rows]{};
for (auto ind = 0; ind < num_columns; ++ind)
{
cd[ind].Width(GridLength{ column_def[ind], GridUnitType::Pixel });
grid.ColumnDefinitions().Append(cd[ind]);
}
for (auto ind = 0; ind < num_rows; ++ind)
{
rd[ind].Height(GridLength{ row_def[ind], GridUnitType::Pixel });
grid.RowDefinitions().Append(rd[ind]);
grid_height += row_def[ind];
}
grid.Height(grid_height);
grid.HorizontalAlignment(HorizontalAlignment::Left);
grid.VerticalAlignment(VerticalAlignment::Top);
// auto on_xaml_root_changed = [=](XamlRoot const& s, XamlRootChangedEventArgs const&)
// {
// AdjustWndSize();
// };
// assert(grid.XamlRoot());
// grid.XamlRoot().Changed(on_xaml_root_changed);
// -------------------- [ Text blocks - headers ] -------------------- //
auto init_header_tb = [=](TextBlock& tb, const wchar_t* t, int32_t col)
{
constexpr Thickness margin = { 0.0, 5.0, 0.0, 5.0 };
tb.Text(t);
tb.HorizontalAlignment(HorizontalAlignment::Right);
tb.VerticalAlignment(VerticalAlignment::Center);
tb.Margin(margin);
tb.FontWeight(FontWeights::SemiBold()); // BodyStrongTextBlockStyle
Grid::SetColumn(tb, col);
Grid::SetRow(tb, 0);
grid.Children().Append(tb);
};
init_header_tb(tb_hdr_c0, L"Velocity", 0);
init_header_tb(tb_hdr_c2, L"Size", 2);
init_header_tb(tb_hdr_c4, L"Controls", 4);
// ------------------------ [ Text blocks ] ------------------------ //
auto init_tb = [=](TextBlock& tb, const wchar_t* t, int32_t col, int32_t row, double bm = -5.0)
{
Thickness margin = { 0.0, 0.0, 0.0, bm };
tb.Text(t);
tb.HorizontalAlignment(HorizontalAlignment::Right);
tb.VerticalAlignment(VerticalAlignment::Center);
tb.Margin(margin);
Grid::SetColumn(tb, col);
Grid::SetRow(tb, row);
grid.Children().Append(tb);
};
init_tb(tb_c0_r1, L"Searcher One", 0, 1);
init_tb(tb_c0_r2, L"Searcher Two", 0, 2);
init_tb(tb_c0_r3, L"Equal velocities", 0, 3, 0.0);
init_tb(tb_c2_r1, L"Searcher One", 2, 1);
init_tb(tb_c2_r2, L"Searcher Two", 2, 2);
init_tb(tb_c2_r3, L"Equal sizes", 2, 3, 0.0);
init_tb(tb_c4_r1, L"Screen speed", 4, 1);
init_tb(tb_c4_r2, L"Step size", 4, 2);
// -------------------------- [ Sliders ] -------------------------- //
constexpr auto
v_min = 1.0, v_max = 25.0,
s_min = 1.0, s_max = 10.0,
scr_speed_min = 1.0, scr_speed_max = 10.0,
step_min = 1.0, step_max = 10.0;
auto init_sl = [=](Slider& sl, double rmin, double rmax, int32_t col, int32_t row)
{
constexpr double lm = 20.0;
constexpr Thickness margin = { lm, 0.0, 0.0, -5.0 };
sl.Width(column_width_even - lm);
sl.HorizontalAlignment(HorizontalAlignment::Left);
sl.VerticalAlignment(VerticalAlignment::Center);
sl.Margin(margin);
sl.Minimum(rmin);
sl.Maximum(rmax);
Grid::SetColumn(sl, col);
Grid::SetRow(sl, row);
grid.Children().Append(sl);
};
init_sl(slider_c1_r1, v_min, v_max, 1, 1);
init_sl(slider_c1_r2, v_min, v_max, 1, 2);
init_sl(slider_c3_r1, s_min, s_max, 3, 1);
init_sl(slider_c3_r2, s_min, s_max, 3, 2);
init_sl(slider_c5_r1, scr_speed_min, scr_speed_max, 5, 1);
//slider_c5_r1.StepFrequency(0.1);
//slider_c5_r1.SnapsTo(SliderSnapsTo::StepValues);
init_sl(slider_c5_r2, step_min, step_max, 5, 2);
//slider_c5_r2.StepFrequency(0.1);
//slider_c5_r2.SnapsTo(SliderSnapsTo::StepValues);
auto on_slider_c1_r1_changed = [=](IInspectable const&, RangeBaseValueChangedEventArgs const&)
{
if (ts1.IsOn()) slider_c1_r2.Value(slider_c1_r1.Value());
};
auto on_slider_c1_r2_changed = [=](IInspectable const&, RangeBaseValueChangedEventArgs const&)
{
if (ts1.IsOn()) slider_c1_r1.Value(slider_c1_r2.Value());
};
auto on_slider_c3_r1_changed = [=](IInspectable const&, RangeBaseValueChangedEventArgs const&)
{
if (ts2.IsOn()) slider_c3_r2.Value(slider_c3_r1.Value());
};
auto on_slider_c3_r2_changed = [=](IInspectable const&, RangeBaseValueChangedEventArgs const&)
{
if (ts2.IsOn()) slider_c3_r1.Value(slider_c3_r2.Value());
};
slider_c1_r1.ValueChanged(on_slider_c1_r1_changed);
slider_c1_r2.ValueChanged(on_slider_c1_r2_changed);
slider_c3_r1.ValueChanged(on_slider_c3_r1_changed);
slider_c3_r2.ValueChanged(on_slider_c3_r2_changed);
// ---------------------- [ Toggle switches ] ---------------------- //
auto init_ts = [=](ToggleSwitch& ts, int32_t col)
{
constexpr Thickness margin = { 20.0, 2.0, 0.0, 0.0 };
ts.HorizontalAlignment(HorizontalAlignment::Left);
ts.VerticalAlignment(VerticalAlignment::Center);
ts.Margin(margin);
ts.OffContent(box_value(L"Off"));
ts.OnContent(box_value(L"On"));
Grid::SetColumn(ts, col);
Grid::SetRow(ts, 3);
grid.Children().Append(ts);
};
init_ts(ts1, 1);
init_ts(ts2, 3);
auto on_ts1_toggled = [=](IInspectable const&, RoutedEventArgs const&)
{
if (ts1.IsOn()) slider_c1_r2.Value(slider_c1_r1.Value());
};
auto on_ts2_toggled = [=](IInspectable const&, RoutedEventArgs const&)
{
if (ts2.IsOn()) slider_c3_r2.Value(slider_c3_r1.Value());
};
ts1.Toggled(on_ts1_toggled);
ts2.Toggled(on_ts2_toggled);
// ------------------- [ Stack panel for buttons ] ------------------- //
sp_buttons.Orientation(Orientation::Horizontal);
sp_buttons.HorizontalAlignment(HorizontalAlignment::Left);
sp_buttons.VerticalAlignment(VerticalAlignment::Top);
Grid::SetColumn(sp_buttons, 4);
Grid::SetColumnSpan(sp_buttons, 2);
Grid::SetRow(sp_buttons, 3);
grid.Children().Append(sp_buttons);
// --------------------------- [ Buttons ] --------------------------- //
auto init_bu = [=](Button& bu, const wchar_t* c, double lm)
{
Thickness margin = { lm, 13.0, 0.0, 5.0 };
//bu.HorizontalAlignment(HorizontalAlignment::Left);
bu.VerticalAlignment(VerticalAlignment::Center);
bu.Margin(margin);
bu.Content(box_value(c));
bu.Width(77.0);
bu.CornerRadius(corner_radius);
//bu.Style(BuAccentedStyle());
sp_buttons.Children().Append(bu);
};
init_bu(bu_colors, L"Colors...", 30.0);
init_bu(bu_step, L"Step", 9.0);
init_bu(bu_run, L"Run", 9.0);
bu_step.Style(BuAccentedStyle());
bu_run.Style(BuAccentedStyle());
auto on_bu_colors_clicked = [=](IInspectable const&, RoutedEventArgs const&)
{
ClrDlg().ShowHost(true);
};
auto on_bu_step_clicked = [=](IInspectable const&, RoutedEventArgs const&)
{
MessageBeep(MB_ICONSTOP);
};
auto on_bu_run_clicked = [=](IInspectable const&, RoutedEventArgs const&)
{
static bool f{};
bu_run.Content(box_value((f = !f) ? L"Pause" : L"Run"));
};
bu_colors.Click(on_bu_colors_clicked);
bu_step.Click(on_bu_step_clicked);
bu_run.Click(on_bu_run_clicked);
}
template<typename ClrDlgT>
void MainWndXaml<ClrDlgT>::AdjustWndSize() const
{
RECT rct{ GetTargetRect() };
SetWindowPos(HwndXamlIsland(), 0, 0, 0, rct.right, rct.top, SWP_SHOWWINDOW);
}
template<typename ClrDlgT>
void MainWndXaml<ClrDlgT>::EnableControls(bool e) const
{
slider_c1_r1.IsEnabled(e);
slider_c1_r2.IsEnabled(e);
slider_c3_r1.IsEnabled(e);
slider_c3_r2.IsEnabled(e);
slider_c5_r1.IsEnabled(e);
slider_c5_r2.IsEnabled(e);
ts1.IsEnabled(e);
ts2.IsEnabled(e);
bu_colors.IsEnabled(e);
bu_step.IsEnabled(e);
bu_run.IsEnabled(e);
}
template<typename ClrDlgT>
RECT MainWndXaml<ClrDlgT>::GetTargetRect() const
{
const auto factor = RasterizationScale();
RECT rcc{};
GetClientRect(HwndHost(), &rcc);
auto height = grid.Height();
height = std::min(height * factor, double(rcc.bottom - rcc.top));
return { 0, LONG(height + 0.5), rcc.right, rcc.bottom };
}
}