Repository navigation
Expand file tree
/
Copy pathVinaApp.cpp
More file actions
425 lines (332 loc) · 14.1 KB
/
Copy pathVinaApp.cpp
File metadata and controls
425 lines (332 loc) · 14.1 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "VinaApp.h"
struct WaterfallCard {
float y;
std::wstring text;
unsigned long color;
float animY = 20.0f;
float alpha = 0.0f;
bool animated = false;
};
class MainAppContext {
public:
int currentTabIndex = 0;
float scrollVal=0;
std::vector<std::shared_ptr<VinaSideTab>> tabs;
std::shared_ptr<VinaButton> btn = std::make_shared<VinaButton>();
std::shared_ptr<VinaSlider> sli = std::make_shared<VinaSlider>();
std::shared_ptr<VinaSwitch> sw = std::make_shared<VinaSwitch>();
std::shared_ptr<VinaFileSelector> fi = std::make_shared<VinaFileSelector>();
std::shared_ptr<VinaMultiTextBox> mt = std::make_shared<VinaMultiTextBox>();
std::shared_ptr<VinaEdit> edt = std::make_shared<VinaEdit>();
std::shared_ptr<VinaCaptionBar> capt = std::make_shared<VinaCaptionBar>();
std::wstring lastText;
std::list<CharAnim> animatingChars;
ID2D1Bitmap* pAppLogo = nullptr;
std::vector<WaterfallCard> waterfallData;
float maxScrollLimit = 0.0f;
bool isGenerating = false;
int activeAnimationCount = 0;
void InitTabs() {
if (!tabs.empty()) return;
auto tab1 = std::make_shared<VinaSideTab>();
tab1->Set(0, 55, 120, L"DemoUI", L"test-home", 20, VuiFadeColor(VERTEXUICOLOR_WHITE,40));
tab1->Activate(true);
auto tab2 = std::make_shared<VinaSideTab>();
tab2->Set(0, 55, 120, L"Waterfall", L"test-test1", 20, VuiFadeColor(VERTEXUICOLOR_WHITE, 20));
auto tab3 = std::make_shared<VinaSideTab>();
tab3->Set(0, 55, 120, L"About", L"test-info", 20, VuiFadeColor(VERTEXUICOLOR_WHITE, 20));
tabs.push_back(tab1);
tabs.push_back(tab2);
tabs.push_back(tab3);
}
void InitWaterfall() {
if (!waterfallData.empty()) return;
GenerateCards(6);
}
void GenerateCards(int count) {
unsigned long macaronColors[] = { VERTEXUICOLOR_SEA, VERTEXUICOLOR_BLOOMLAVENDER, VERTEXUICOLOR_DAWN, VERTEXUICOLOR_FOREST, VERTEXUICOLOR_SEA };
for (int i = 0; i < count; i++) {
WaterfallCard card;
card.text = L"VinaUI Card " + std::to_wstring(waterfallData.size());
card.color = macaronColors[rand() % 5];
waterfallData.push_back(card);
}
}
void ResetWaterfallAnimationState() {
activeAnimationCount = 0;
for (auto& card : waterfallData) {
if (card.animated && (card.alpha < 1.0f || card.animY > 0.0f)) {
card.alpha = 1.0f;
card.animY = 0.0f;
}
}
}
};
VinaWindow* MainWindow = new VinaWindow;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
InitGlobalD2D();
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_VINAAPP));
VuiColorSystemInit();
gScale = GetScreenScale();
//LoadVinaCom();
auto fontData = AppResourceManager::LoadRawResource(hInstance, IDF_FONTAWESOME, L"BINARY");
if (!fontData.empty()) {
DWORD nFonts = 0;
static UniqueFontHandle myFontHandle{
AddFontMemResourceEx((void*)fontData.data(), (DWORD)fontData.size(), nullptr, &nFonts)
};
}
namespace fs = std::filesystem;
fs::path fontPath = L"Font-AwesomeFree.ttf";
FreeAnyResource(IDF_FONTAWESOME, L"BINARY", fontPath.c_str());
AddFontResource(fontPath.c_str());
auto ctx = std::make_shared<MainAppContext>();
MainWindow->Set(100, 100, 720 * gScale, 480 * gScale, L"Vina.Class.App.Main.Test", L"Vilinko VinaUI");
MainWindow->CreatePanel([ctx](HWND hWnd, ID2D1HwndRenderTarget* hrt)->void {
RECT rc;
GetClientRect(hWnd, &rc);
D2DDrawSolidRect(hrt, 0, 0, rc.right, rc.bottom, VERTEXUICOLOR_DARKNIGHT);
MainWindow->GetPanel()->Set(hWnd, hrt);
/*
CreatePanel 会在窗口刷新时被调用。
控件需要使用静态标识,或者在 CreatePanel 外部初始化。
使用 std::vector 等容器储存控件也是较好的方法。
*/
if (ctx->currentTabIndex == 0) {
ctx->btn->Set(40, 120, 140, 60, L"Button", [] {return 0; });
MainWindow->GetPanel()->Add(ctx->btn);
ctx->sli->Set(40, 200, 140, 30, -1, VERTEXUICOLOR_DARKEN, L"Slider", [] {});
MainWindow->GetPanel()->Add(ctx->sli);
ctx->sw->Set(40, 255, 60, 30, { VERTEXUICOLOR_DARKEN }, [] {});
MainWindow->GetPanel()->Add(ctx->sw);
if (ctx->sw->GetValue())
{
ctx->fi->Set(40, 300, 260, 160);
ctx->fi->SetFileOpenCallback([](std::wstring path)->void {
MessageBox(0, path.c_str(), L"Current", 0);
});
ctx->fi->SetParent(MainWindow->GetPanel());
MainWindow->GetPanel()->Add(ctx->fi);
}
ctx->mt->Set(260, 120, 300, 100, L"This is a test string.\nAnd this is a multi line text area.\ne.g:\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
ctx->mt->SetParent(MainWindow->GetPanel());
MainWindow->GetPanel()->Add(ctx->mt);
if (ctx->edt->cx == 0)/*这里为了演示方便,做简单的初始化。实际使用建议将Set移动到外层。*/
{
ctx->edt->Set(260, 240, 300, 30, L"This is a Edit...");
}
ctx->edt->Set2(260, 240, 300, 35, VERTEXUICOLOR_MIDNIGHT, VERTEXUICOLOR_WHITE);
MainWindow->GetPanel()->Add(ctx->edt);
/*
此处演示动画效果,以及动画播放时的内存管理。
动画效果可以直接在 CreatePanel(也就是 OnPaint )时操作,也可以在控件内部操作。
此处为了演示方便,直接在 CreatePanel 内部操作。
*/
std::wstring currentText = ctx->edt->GetText();
if (currentText != ctx->lastText) {
if (currentText.length() > ctx->lastText.length()) {
for (size_t i = ctx->lastText.length(); i < currentText.length(); ++i) {
float lockedX = VuiMeasureStringWidth(currentText.substr(0, i), 18);
ctx->animatingChars.push_back({ currentText[i], 0.0f, lockedX, 18.0f, false });
CharAnim& ref = ctx->animatingChars.back();
ref.alphaAnimId = MainWindow->AnimateVariableWithBezier(hWnd, ref.alpha, 0.0f, 1.0f, 0.4);
ref.yOffsetAnimId = MainWindow->AnimateVariableWithBezier(hWnd, ref.yOffset, 18.0f, 0.0f, 0.4);
}
}
else if (currentText.length() < ctx->lastText.length()) {
size_t diff = ctx->lastText.length() - currentText.length();
for (size_t i = 0; i < diff; ++i) {
auto it = std::find_if(ctx->animatingChars.rbegin(), ctx->animatingChars.rend(),
[](const CharAnim& c) { return !c.isRemoving; });
if (it != ctx->animatingChars.rend()) {
it->isRemoving = true;
CharAnim& ref = *it;
ref.alphaAnimId = MainWindow->AnimateVariableWithBezier(hWnd, ref.alpha, ref.alpha, 0.0f, 0.3);
ref.yOffsetAnimId = MainWindow->AnimateVariableWithBezier(hWnd, ref.yOffset, ref.yOffset, 10.0f, 0.3);
}
}
}
ctx->lastText = currentText;
}
//清理
ctx->animatingChars.remove_if([&](const CharAnim& c) {
if (c.isRemoving && c.alpha <= 0.01f) {
if (c.alphaAnimId != -1) {
MainWindow->StopAnimation(c.alphaAnimId);
}
if (c.yOffsetAnimId != -1) {
MainWindow->StopAnimation(c.yOffsetAnimId);
}
return true;
}
return false;
});
float startX_txt = 260.0f;
float startY_txt = 290.0f;
for (auto& item : ctx->animatingChars) {
D2DDrawText2(hrt, std::wstring(1, item.ch).c_str(),
startX_txt + item.xOffset, startY_txt + item.yOffset,
20, 20, 18,
VERTEXUICOLOR_WHITE, L"Segoe UI", item.alpha);
}
}
if (ctx->currentTabIndex == 1)
{
float delta = MainWindow->GetPanel()->GetInstantScrollDepth();
ctx->scrollVal -= delta;
if (ctx->scrollVal > 0.0f) ctx->scrollVal = 0.0f;
float viewWidth = rc.right / gScale;
float viewHeight = rc.bottom / gScale;
float cardW = (viewWidth - 60.0f) / 2.0f;
float cardH = 150.0f;
float spacing = 20.0f;
float startY = 120.0f;
int rowCount = (int)ceil(ctx->waterfallData.size() / 2.0f);
float totalContentHeight = startY + rowCount * (cardH + spacing);
float minScroll = viewHeight - totalContentHeight - 50.0f;
if (minScroll > 0) minScroll = 0;
if (ctx->scrollVal < minScroll) ctx->scrollVal = minScroll;
// 触发加载
if (totalContentHeight + ctx->scrollVal < viewHeight + 100.0f && ctx->waterfallData.size() < 200) {
ctx->GenerateCards(6);
}
for (size_t i = 0; i < ctx->waterfallData.size(); i++) {
auto& card = ctx->waterfallData[i];
int row = (int)(i / 2);
int col = (int)(i % 2);
float drawX = 20.0f + col * (cardW + spacing);
float drawY = startY + row * (cardH + spacing) + ctx->scrollVal;
// 剪裁
if (drawY + cardH < 0) continue;
if (drawY > viewHeight) break;
if (!card.animated) {
// 渲染排队
if (ctx->activeAnimationCount < 2) {
card.animated = true;
ctx->activeAnimationCount++;
MainWindow->AnimateVariableWithBezier(hWnd, card.alpha, 0.0f, 1.0f, 0.5);
MainWindow->AnimateVariableWithBezier(hWnd, card.animY, 30.0f, 0.0f, 0.6,
0.25, 0.1, 0.25, 1.0,
[ctx, &card] {
card.animY = 0.0f;
card.alpha = 1.0f;
if (ctx->activeAnimationCount > 0) ctx->activeAnimationCount--;
}
);
}
}
else {
// 同步动画位置
if (card.alpha > 0.0f && card.alpha < 1.0f) {
card.alpha += 0.08f;
card.animY -= 2.4f;
if (card.alpha >= 1.0f) {
card.alpha = 1.0f;
card.animY = 0.0f;
if (ctx->activeAnimationCount > 0) ctx->activeAnimationCount--;
}
}
}
if (card.animated) {
float finalY = drawY + (card.animY < 0 ? 0 : card.animY);
D2DDrawQuickShadow(hrt, drawX, finalY, cardW, cardH, 10, 0, 2, 10, 5, 0, 0.05f * card.alpha, VERTEXUICOLOR_WHITE);
D2DDrawRoundRect(hrt, drawX, finalY, cardW, cardH, card.color, 12, card.alpha);
if(card.text.c_str())D2DDrawText3(hrt, card.text.c_str(), drawX + 15, finalY+cardH - 45, cardW - 30, cardH - 30, 20,
VERTEXUICOLOR_WHITEDRAW, L"Segoe UI", card.alpha * 0.9f);
}
}
}
if (ctx->currentTabIndex == 2)
{
float pageX = 60.0f;
float pageY = 120.0f;
float cardW = rc.right / gScale - 120.0f;
float cardH = 300.0f;
// 静态缓存位图,提升性能
if (!ctx->pAppLogo) {
ctx->pAppLogo = LoadIconToD2DBitmap(hrt, IDI_SMALL);
}
D2DDrawQuickShadow(hrt, pageX, pageY, rc.right / gScale - 120, 300, 15, 0, 2, 16, 8, 0, 0.02f, VERTEXUICOLOR_WHITE);
D2DDrawRoundRect(hrt, pageX, pageY, rc.right / gScale - 120, 300, VERTEXUICOLOR_MIDNIGHT, 15, 0.5f, 1, 0, 0.1f);
if (ctx->pAppLogo) {
D2D1_RECT_F iconRect = D2D1::RectF(pageX + 40, pageY + 40, pageX + 104, pageY + 104);
hrt->DrawBitmap(ctx->pAppLogo, iconRect);
}
D2DDrawText3(hrt, L"Vilinko VinaUI", pageX + 130, pageY + 45, 300, 40, 28,
VERTEXUICOLOR_WHITE);
D2DDrawText(hrt, L"Developer & Designer : CimiMoly", pageX + 130, pageY + 85, 300, 30, 14,
VuiDarkenColor(VERTEXUICOLOR_WHITE, 100), L"Segoe UI", 0.8f);
D2DDrawSolidRect(hrt, pageX + 40, pageY + 130, cardW - 80, 1,
VuiDarkenColor(VERTEXUICOLOR_DARKNIGHT, 20), 0.5f);
std::wstring description = L"Nice to meet U(●'◡'●)! VinaUI is here! Special thanks to @Haceau-Zoac for vina parser~\n";
D2DDrawText(hrt, description.c_str(), pageX + 40, pageY + 150, cardW - 80, 100, 16,
VERTEXUICOLOR_WHITE, L"Segoe UI", 0.9f);
std::wstring copyright = L"Copyright © 2026 Vilinko. All rights reserved.";
float cpWidth = VuiMeasureStringWidth(copyright, 14);
D2DDrawText(hrt, copyright.c_str(), (rc.right / gScale - cpWidth) / 2.0f, pageY + cardH - 40,
cpWidth + 20, 30, 14, VuiDarkenColor(VERTEXUICOLOR_WHITE, 150), L"Segoe UI", 0.7f);
static std::shared_ptr<VinaButton> webBtn = std::make_shared<VinaButton>();
webBtn->Set(pageX + 40, pageY + cardH - 90, 120, 35, L"Github", [] {
ShellExecute(0, L"open", L"https://github.com/EnderMo/VinaUI", 0, 0, SW_SHOWNORMAL);
});
MainWindow->GetPanel()->Add(webBtn);
static std::shared_ptr<VinaButton> spBtn = std::make_shared<VinaButton>();
spBtn->Set(pageX + 180, pageY + cardH - 90, 120, 35, L"Sponsor", [] {
ShellExecute(0, L"open", L"https://afdian.com/a/EnderMo", 0, 0, SW_SHOWNORMAL);
});
MainWindow->GetPanel()->Add(spBtn);
}
for (float i = 2; i < 90; i++)
{
D2DDrawSolidRect(hrt, 0, 1 * (i - 2), rc.right / gScale, 1.5F, VERTEXUICOLOR_DARKNIGHT, 1.0f - (i - 1.0f) / 80.0f);
}
D2DCreateQuickHeavyBlur(hrt, 0, 0, rc.right, 90 * gScale, 12);
ctx->capt->Set(0, 0, rc.right / gScale - 160, 40, L"VinaUI App", VERTEXUICOLOR_DARKNIGHT, 18);
MainWindow->GetPanel()->Add(ctx->capt);
static std::shared_ptr<VinaFAIcon>close = std::make_shared<VinaFAIcon>();
close->Set(rc.right / gScale - 32, 20, L"win-close", 15, VERTEXUICOLOR_WHITE, [] {DestroyWindow(MainWindow->GetHandle()); PostQuitMessage(0); });
MainWindow->GetPanel()->Add(close);
bool IsMaximized = IsZoomed(hWnd) != 0;
if (!IsMaximized)
{
static std::shared_ptr<VinaFAIcon>max = std::make_shared<VinaFAIcon>();
max->Set(rc.right / gScale - 32 - 32, 20, L"win-max", 15, VERTEXUICOLOR_WHITE, [hWnd] {
SendMessage(hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0); });
MainWindow->GetPanel()->Add(max);
}
else {
static std::shared_ptr<VinaFAIcon>max = std::make_shared<VinaFAIcon>();
max->Set(rc.right / gScale - 32 - 32, 20, L"win-restore", 15, VERTEXUICOLOR_WHITE, [hWnd] {
SendMessage(hWnd, WM_SYSCOMMAND, SC_RESTORE, 0); });
MainWindow->GetPanel()->Add(max);
}
static std::shared_ptr<VinaFAIcon>min = std::make_shared<VinaFAIcon>();
min->Set(rc.right / gScale - 32 - 32 - 32, 20, L"win-min", 15, VERTEXUICOLOR_WHITE, [hWnd] {MainWindow->KillAnimation(); SendMessage(hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0); });
MainWindow->GetPanel()->Add(min);
ctx->InitTabs();
float totalWidth = ctx->tabs.size() * 130.0f;
float startX = (rc.right / gScale - totalWidth) / 2.0f;
for (size_t i = 0; i < ctx->tabs.size(); i++) {
ctx->tabs[i]->Set(startX + i * 130, 55, 120, ctx->tabs[i]->GetText().c_str(), ctx->tabs[i]->txt.c_str(), 20, VuiFadeColor(VERTEXUICOLOR_WHITE, 20), [ctx, i] {
ctx->currentTabIndex = (int)i;
ctx->ResetWaterfallAnimationState();
});
ctx->tabs[i]->Activate(ctx->currentTabIndex == i);
MainWindow->GetPanel()->Add(ctx->tabs[i]);
}
//LoadBitmapFromFile(hrt, m_ImageFactory, L"D:\\FLUENTEMOJI\\four_leaf_clover_3d.png", 20, 20, 100, 100);
});
MainWindow->SetOutFrame(VinaWindow::Client);
MainWindow->OnCreateCmd = [] {
CenterWindow(MainWindow->GetHandle());
MainWindow->InitAnimation();
MainWindow->StartAnimationSystem();
};
MainWindow->RunFull();
return 0;
}