-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerManager.cpp
More file actions
404 lines (358 loc) · 14.6 KB
/
Copy pathLayerManager.cpp
File metadata and controls
404 lines (358 loc) · 14.6 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
// Copyright 2026 Focus <scutjiaodian@outlook.com>
//
// 左侧图层面板实现,自绘图层列表项及交互逻辑,支持选中图层、
// 切换可见性和锁定状态、调整图层顺序、增删图层。
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <windowsx.h>
#include <cstdio>
#include <string>
#include "LayerManager.h"
#include "Canvas.h"
#include "CurveData.h"
static const COLORREF COL_BG = RGB(240, 240, 240);
static const COLORREF COL_PANEL = RGB(245, 245, 245);
static const COLORREF COL_BORDER = RGB(200, 200, 200);
static const COLORREF COL_TEXT = RGB( 50, 50, 50);
static const COLORREF COL_TEXT_L = RGB(150, 150, 150);
static const COLORREF COL_SEL = RGB(210, 230, 250);
static const COLORREF COL_ACCENT = RGB( 86, 156, 214);
static const COLORREF COL_HIDDEN = RGB(220, 220, 220);
static const wchar_t* PANEL_CLASS = L"CurveLayerPanel";
static bool s_panelClassReg = false;
static int SC(int v) { return (int)(v * g_dpiScale + 0.5); }
static const int ID_ADD_BTN = 3001;
static const int ID_DEL_BTN = 3002;
static const int ID_UP_BTN = 3003;
static const int ID_DOWN_BTN = 3004;
LayerManager::LayerManager(HWND parent, Canvas* canvas)
: m_parent(parent), m_canvas(canvas)
{
m_bgBrush = CreateSolidBrush(COL_PANEL);
m_selBrush = CreateSolidBrush(COL_SEL);
m_itemBgBrush = CreateSolidBrush(COL_BG);
m_font = CreateFontW(-(int)(13 * g_dpiScale + 0.5), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
m_smallFont = CreateFontW(-(int)(11 * g_dpiScale + 0.5), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Segoe UI");
if (!s_panelClassReg) {
WNDCLASSEXW wc{};
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = PanelProc;
wc.hInstance = GetModuleHandleW(nullptr);
wc.hbrBackground = nullptr;
wc.lpszClassName = PANEL_CLASS;
wc.hCursor = LoadCursorW(nullptr, MAKEINTRESOURCEW(IDC_ARROW));
RegisterClassExW(&wc);
s_panelClassReg = true;
}
m_hwnd = CreateWindowExW(
0, PANEL_CLASS, nullptr,
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
0, 0, SC(180), SC(600),
parent, nullptr, GetModuleHandleW(nullptr), this);
CreateControls();
}
LayerManager::~LayerManager()
{
if (m_bgBrush) DeleteObject(m_bgBrush);
if (m_selBrush) DeleteObject(m_selBrush);
if (m_itemBgBrush) DeleteObject(m_itemBgBrush);
if (m_font) DeleteObject(m_font);
if (m_smallFont) DeleteObject(m_smallFont);
}
// 图层面板的窗口过程。WM_NCCREATE 时将 LayerManager 实例指针存入窗口附加数据。
LRESULT CALLBACK LayerManager::PanelProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
LayerManager* self = nullptr;
if (msg == WM_NCCREATE) {
auto* cs = reinterpret_cast<CREATESTRUCTW*>(lp);
self = reinterpret_cast<LayerManager*>(cs->lpCreateParams);
SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)self);
self->m_hwnd = hWnd;
} else {
self = reinterpret_cast<LayerManager*>(GetWindowLongPtrW(hWnd, GWLP_USERDATA));
}
if (self) return self->HandleMsg(msg, wp, lp);
return DefWindowProcW(hWnd, msg, wp, lp);
}
// 面板消息处理。WM_PAINT 自绘图层列表(颜色方块、名称、可见性/锁定按钮),
// WM_LBUTTONDOWN 切换可见性/锁定或选中图层,WM_COMMAND 处理底部操作按钮。
LRESULT LayerManager::HandleMsg(UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_ERASEBKGND: {
HDC hdc = (HDC)wp;
RECT rc; GetClientRect(m_hwnd, &rc);
FillRect(hdc, &rc, m_bgBrush);
return 1;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(m_hwnd, &ps);
RECT rc; GetClientRect(m_hwnd, &rc);
FillRect(hdc, &rc, m_bgBrush);
// 面板右侧边框分隔线
HPEN pen = CreatePen(PS_SOLID, 1, COL_BORDER);
HPEN oldPen = (HPEN)SelectObject(hdc, pen);
MoveToEx(hdc, rc.right - 1, 0, nullptr);
LineTo(hdc, rc.right - 1, rc.bottom);
SelectObject(hdc, oldPen);
DeleteObject(pen);
HFONT oldFont = (HFONT)SelectObject(hdc, m_font);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, COL_TEXT);
RECT titleRc = { SC(10), SC(8), rc.right - SC(10), SC(30) };
DrawTextW(hdc, L"图层管理", -1, &titleRc,
DT_VCENTER | DT_SINGLELINE | DT_LEFT);
MoveToEx(hdc, SC(6), SC(32), nullptr);
LineTo(hdc, rc.right - SC(6), SC(32));
int count = m_canvas ? m_canvas->GetTotalLayerCount() : 0;
if (count == 0) {
SelectObject(hdc, m_smallFont);
SetTextColor(hdc, COL_TEXT_L);
RECT emptyRc = { SC(10), SC(50), rc.right - SC(10), SC(80) };
DrawTextW(hdc, L"暂无图层", -1, &emptyRc,
DT_VCENTER | DT_SINGLELINE | DT_CENTER);
SelectObject(hdc, oldFont);
EndPaint(m_hwnd, &ps);
return 0;
}
int active = m_canvas->GetCombinedActiveLayer();
int itemH = SC(32), colorSq = SC(16);
for (int i = 0; i < count; ++i) {
std::wstring name = m_canvas->GetLayerName(i);
COLORREF color = m_canvas->GetLayerColor(i);
bool visible = m_canvas->GetLayerVisibility(i);
bool locked = m_canvas->GetLayerLocked(i);
int y = GetItemY(i);
RECT itemRc = { SC(3), y, rc.right - SC(3), y + itemH };
bool isActive = (i == active);
FillRect(hdc, &itemRc, isActive ? m_selBrush : m_bgBrush);
// 颜色方块:隐藏时显示灰色
COLORREF sqColor = visible ? color : COL_HIDDEN;
int sqX = SC(10), sqY = y + (itemH - colorSq) / 2;
HBRUSH colorBrush = CreateSolidBrush(sqColor);
RECT colorRc = { sqX, sqY, sqX + colorSq, sqY + colorSq };
FillRect(hdc, &colorRc, colorBrush);
DeleteObject(colorBrush);
HPEN colorPen = CreatePen(PS_SOLID, 1, COL_BORDER);
HPEN oldP2 = (HPEN)SelectObject(hdc, colorPen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
Rectangle(hdc, colorRc.left, colorRc.top,
colorRc.right, colorRc.bottom);
SelectObject(hdc, oldP2);
DeleteObject(colorPen);
SelectObject(hdc, m_font);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, visible ? COL_TEXT : COL_TEXT_L);
wchar_t nameBuf[48];
wcsncpy_s(nameBuf, name.c_str(), _TRUNCATE);
if (nameBuf[0] == 0) {
// 无名图层自动编号:N 前缀为 NURBS,B 前缀为贝塞尔
if (m_canvas->IsNURBSLayer(i))
swprintf_s(nameBuf, L"N%d", i - m_canvas->GetStrokeCount() + 1);
else
swprintf_s(nameBuf, L"B%d", i + 1);
}
RECT textRc = { sqX + colorSq + SC(6), y, rc.right - SC(78), y + itemH };
DrawTextW(hdc, nameBuf, -1, &textRc,
DT_VCENTER | DT_SINGLELINE | DT_LEFT | DT_END_ELLIPSIS);
// 可见性/锁定文字按钮
int iconSize = SC(16);
int btnW = SC(26);
RECT visRc = { rc.right - SC(68), y + (itemH - iconSize) / 2,
rc.right - SC(68) + btnW, y + (itemH + iconSize) / 2 };
SelectObject(hdc, m_smallFont);
SetTextColor(hdc, visible ? COL_TEXT_L : COL_ACCENT);
DrawTextW(hdc, L"隐藏", -1, &visRc,
DT_VCENTER | DT_SINGLELINE | DT_CENTER);
RECT lockRc = { rc.right - SC(36), y + (itemH - iconSize) / 2,
rc.right - SC(36) + btnW, y + (itemH + iconSize) / 2 };
SetTextColor(hdc, locked ? COL_ACCENT : COL_TEXT_L);
DrawTextW(hdc, L"锁定", -1, &lockRc,
DT_VCENTER | DT_SINGLELINE | DT_CENTER);
SelectObject(hdc, oldFont);
MoveToEx(hdc, SC(6), y + itemH - 1, nullptr);
LineTo(hdc, rc.right - SC(6), y + itemH - 1);
}
EndPaint(m_hwnd, &ps);
return 0;
}
case WM_CTLCOLORSTATIC: {
HDC hdcCtrl = (HDC)wp;
SetTextColor(hdcCtrl, COL_TEXT);
SetBkColor(hdcCtrl, COL_PANEL);
return (LRESULT)m_bgBrush;
}
case WM_CTLCOLORBTN:
return (LRESULT)m_bgBrush;
case WM_SIZE: {
int w = LOWORD(lp), h = HIWORD(lp);
LayoutControls(w, h);
InvalidateRect(m_hwnd, nullptr, FALSE);
return 0;
}
case WM_LBUTTONDOWN: {
int mx = GET_X_LPARAM(lp), my = GET_Y_LPARAM(lp);
if (!m_canvas) break;
int count = m_canvas->GetTotalLayerCount();
int itemIdx = HitTestItem(my);
if (itemIdx >= 0 && itemIdx < count) {
int icon = HitTestIcon(mx, my, itemIdx);
if (icon == 0) {
bool cur = m_canvas->GetLayerVisibility(itemIdx);
m_canvas->SetLayerVisibility(itemIdx, !cur);
} else if (icon == 1) {
bool cur = m_canvas->GetLayerLocked(itemIdx);
m_canvas->SetLayerLocked(itemIdx, !cur);
} else {
m_canvas->SelectLayerByIndex(itemIdx);
}
}
return 0;
}
case WM_COMMAND: {
int id = LOWORD(wp);
if (!m_canvas) break;
if (id == ID_ADD_BTN) {
CurveStroke newStroke;
int layerNum = m_canvas->GetStrokeCount() + 1;
wchar_t nameBuf[32];
swprintf_s(nameBuf, L"B%d", layerNum);
newStroke.name = nameBuf;
m_canvas->AddEmptyStroke(newStroke);
break;
}
if (id == ID_DEL_BTN) {
int active = m_canvas->GetCombinedActiveLayer();
if (active >= 0) {
m_canvas->DeleteLayerByIndex(active);
}
break;
}
if (id == ID_UP_BTN) {
int active = m_canvas->GetCombinedActiveLayer();
if (active > 0) {
m_canvas->MoveLayerByIndex(active, active - 1);
}
break;
}
if (id == ID_DOWN_BTN) {
int active = m_canvas->GetCombinedActiveLayer();
int count = m_canvas->GetTotalLayerCount();
if (active >= 0 && active < count - 1) {
m_canvas->MoveLayerByIndex(active, active + 1);
}
break;
}
break;
}
}
return DefWindowProcW(m_hwnd, msg, wp, lp);
}
// 创建底部操作按钮:添加、删除、上移、下移。
void LayerManager::CreateControls()
{
int btnH = SC(26);
m_addBtn = CreateWindowExW(0, L"BUTTON", L"添加",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, SC(80), btnH,
m_hwnd, (HMENU)(UINT_PTR)ID_ADD_BTN,
GetModuleHandleW(nullptr), nullptr);
SendMessage(m_addBtn, WM_SETFONT, (WPARAM)m_font, TRUE);
m_delBtn = CreateWindowExW(0, L"BUTTON", L"删除",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, SC(80), btnH,
m_hwnd, (HMENU)(UINT_PTR)ID_DEL_BTN,
GetModuleHandleW(nullptr), nullptr);
SendMessage(m_delBtn, WM_SETFONT, (WPARAM)m_font, TRUE);
m_upBtn = CreateWindowExW(0, L"BUTTON", L"上移",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, SC(80), btnH,
m_hwnd, (HMENU)(UINT_PTR)ID_UP_BTN,
GetModuleHandleW(nullptr), nullptr);
SendMessage(m_upBtn, WM_SETFONT, (WPARAM)m_font, TRUE);
m_downBtn = CreateWindowExW(0, L"BUTTON", L"下移",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, SC(80), btnH,
m_hwnd, (HMENU)(UINT_PTR)ID_DOWN_BTN,
GetModuleHandleW(nullptr), nullptr);
SendMessage(m_downBtn, WM_SETFONT, (WPARAM)m_font, TRUE);
}
// 布局底部操作按钮的位置,两行两列排列。
void LayerManager::LayoutControls(int w, int h)
{
int btnH = SC(26);
int btnW = (w - SC(18)) / 2;
if (btnW < SC(50)) btnW = SC(50);
int bottomY = h - SC(8) - btnH;
if (m_addBtn) {
SetWindowPos(m_addBtn, nullptr, SC(6), bottomY - SC(66), btnW, btnH,
SWP_NOZORDER | SWP_NOACTIVATE);
}
if (m_delBtn) {
SetWindowPos(m_delBtn, nullptr, SC(6) + btnW + SC(6), bottomY - SC(66), btnW, btnH,
SWP_NOZORDER | SWP_NOACTIVATE);
}
if (m_upBtn) {
SetWindowPos(m_upBtn, nullptr, SC(6), bottomY - SC(34), btnW, btnH,
SWP_NOZORDER | SWP_NOACTIVATE);
}
if (m_downBtn) {
SetWindowPos(m_downBtn, nullptr, SC(6) + btnW + SC(6), bottomY - SC(34), btnW, btnH,
SWP_NOZORDER | SWP_NOACTIVATE);
}
}
// 计算第 index 个图层的 Y 坐标。
int LayerManager::GetItemY(int index) const
{
return SC(36) + index * (SC(32) + SC(2));
}
// 测试鼠标 Y 坐标是否落在某个图层的点击区域,返回图层索引或 -1。
int LayerManager::HitTestItem(int mouseY) const
{
int count = m_canvas ? m_canvas->GetTotalLayerCount() : 0;
for (int i = 0; i < count; ++i) {
int y = GetItemY(i);
if (mouseY >= y && mouseY < y + SC(32))
return i;
}
return -1;
}
// 测试鼠标点击落在图层的哪个区域:0=可见性按钮,1=锁定按钮,-1=选中图层。
int LayerManager::HitTestIcon(int mouseX, int mouseY, int itemIdx) const
{
RECT rc;
GetClientRect(m_hwnd, &rc);
int y = GetItemY(itemIdx);
int itemH = SC(32);
int iconYOff = (itemH - SC(16)) / 2;
RECT visRc = { rc.right - SC(68), y + iconYOff,
rc.right - SC(68) + SC(26), y + iconYOff + SC(16) };
if (mouseX >= visRc.left && mouseX <= visRc.right &&
mouseY >= visRc.top && mouseY <= visRc.bottom)
return 0;
RECT lockRc = { rc.right - SC(36), y + iconYOff,
rc.right - SC(36) + SC(26), y + iconYOff + SC(16) };
if (mouseX >= lockRc.left && mouseX <= lockRc.right &&
mouseY >= lockRc.top && mouseY <= lockRc.bottom)
return 1;
return -1;
}
// 从 Canvas 同步图层变更,刷新面板显示。
void LayerManager::UpdateFromCanvas()
{
InvalidateRect(m_hwnd, nullptr, FALSE);
}
// 移动并调整图层面板窗口的大小和位置。
void LayerManager::Resize(int x, int y, int w, int h)
{
SetWindowPos(m_hwnd, nullptr, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
}