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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
#include <string>
#include "EditorWindows.h"
#include "WinUtils.h"
#include "Editor/Utils/HelperFuncs.h"
#include "MenuManager.h"
#include "Runtime/Utilities/Assert.h"
#include "Editor/Graphics/Graphics.h"
#include "Runtime/Math/Math.h"
using namespace std;
using namespace LuaBind;
static bool s_IsMainWindowMaximized;
extern bool ProcessMainWindowMessages(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT& result);
LRESULT CALLBACK ContainerWindow::ContainerWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//#if GAMELAB_DEBUG
// static int _event_count = 0;
// log_info_tag("WndProc", "ContrainerWindow Event %d", ++_event_count);
//#endif
ContainerWindow *self = (ContainerWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (!self)
{
log_warning("Nullptr ContainerWindow");
return DefWindowProcW(hWnd, message, wParam, lParam);
}
switch (message)
{
case WM_MENUSELECT:
{
if (self->m_ShowMode != ShowMode::kShowMainWindow)
return 0;
uint menuId = LOWORD(wParam);
uint flags = HIWORD(wParam);
HMENU menu = (HMENU)lParam;
MenuManager::Instance()->HandleMenuItemEvent(menu, menuId, flags);
return 0;
}
case WM_SETICON:
return WM_SETICON;
//case WM_PAINT:
//{
// static PAINTSTRUCT ps;
// self->SetAsRenderContext();
// glEnable(GL_BLEND);
// float c = 26 / 255.f;
// glClearColor(c, c, c, 1);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glFlush();
// BeginPaint(self->m_Window, &ps);
// EndPaint(self->m_Window, &ps);
// UpdateWindow(self->m_Window);
// return 0;
//}
case WM_ENTERMENULOOP: // 点击菜单
return 0;
case WM_EXITMENULOOP: // 松开菜单
return 0;
case WM_KEYDOWN: // key down
case WM_KEYUP: // key up
case WM_SYSKEYDOWN: // 和 WM_KEYDOWN 的区别是可以捕获快捷键或系统命令按键,比如alt键, Ctrl和shift不属于WM_SYSKEYDOWN
case WM_SYSKEYUP: // 和 WM_KEYUP 的区别是可以捕获快捷键或系统命令按键,比如alt键
if (message == WM_KEYDOWN && wParam == VK_ESCAPE) {
// Escape should close utility container windows
// 如果本窗口是 utility 窗口,关闭它
if (self->m_ShowMode == ShowMode::kShowUtility) {
self->Close();
return 0;
}
}
return 0;
case WM_ERASEBKGND:// WM_ERASEBKGND是在当窗口*背景*必须被擦除时 (例如,窗口的移动,窗口的大小的改变)发送。
{
// unity的窗口背景色要么是灰色(免费版) ,要么是深黑色(专业版)
// 对于透明窗口
if (GetWindowLong(hWnd, GWL_EXSTYLE) & WS_EX_LAYERED) { // WS_EX_LAYERED是extendedStyle透明窗口
// 填充用户区
RECT rc;
GetClientRect(hWnd, &rc);
FillRect((HDC)wParam, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
}
// do not erase background
// 如果不是透明窗口不要清除背景,跳过DefWindowProc函数
return 1;
}
case WM_WINDOWPOSCHANGED: // 窗口大小、位置、z层级改变,即窗口区域改变
{
//log_info_tag("WndProc", "WM_WINDOWPOSCHANGED" );
if (!self->m_IsClosing && !IsIconic(hWnd)) //IsIconic 窗口是否是最小化(图标化)
{
self->OnSizeChanged();
}
break;
}
case WM_SIZE: // 窗口大小改变
if (self->m_ShowMode == ShowMode::kShowMainWindow)
{
if (IsWindowVisible(hWnd))
{
bool nowMaximized = wParam == SIZE_MAXIMIZED;
if (s_IsMainWindowMaximized != nowMaximized)//更新s_IsMainWindowMaximized变量
s_IsMainWindowMaximized = nowMaximized;
}
}
break;
case WM_SETFOCUS: // 窗口获得焦点
return 0;
case WM_ACTIVATEAPP: // 编辑器的激活状态改变,所有的窗口都会获得这个消息,和WM_ACTIVATE不同,WM_ACTIVATE只是单个窗口被激活
// 发送一个 OnActivateApplication 消息
self->OnActivateApplication(wParam != FALSE);
break; // fall through to main window processing
case WM_NCACTIVATE: // non-client active 即非用户区被激活,用这个消息来刷新编辑器
break;
case WM_ACTIVATE: // 单个窗口的激活状态改变
return 0;
case WM_CLOSE: // 窗口关闭消息
{
bool isMainAndShouldExit = (self->m_ShowMode == kShowMainWindow && !self->m_CloseFromScriptDontShutdown);
if (isMainAndShouldExit)
{
//退出编辑器
}
else
{
// 关闭此窗口
::SetMenu(hWnd, NULL); // 给窗口分配一个菜单,如果是NULL,这个窗口的菜单被删除
::DestroyWindow(hWnd);
}
return 0;
}
case WM_DESTROY: // 窗口被销毁
return 0;
case WM_INITMENUPOPUP: //下拉菜单或子菜单将要被激活的时候
return 0;
case WM_ENTERSIZEMOVE: // 在大小、位置改变前,即当用户点击标题栏、边框
return 0;
}
if (self->m_ShowMode == ShowMode::kShowMainWindow)
{
LRESULT result;
if (ProcessMainWindowMessages(hWnd, message, wParam, lParam, result)) {
return result;
}
}
long flag = DefWindowProcW(hWnd, message, wParam, lParam);
return flag;
}
ContainerWindow::ContainerWindow(LuaBind::VM* vm)
: LuaBind::NativeClass<ContainerWindow>(vm)
{
}
ContainerWindow::~ContainerWindow()
{
}
void ContainerWindow::DoPaint()
{
SetWindowLongPtr(m_Window, GWLP_USERDATA, (LONG_PTR)this);
SendMessage(m_Window, WM_PAINT, 0, 0);
}
void ContainerWindow::SetAsRenderContext()
{
Assert(m_DC != NULL);
Assert(g_GLRC != NULL);
Assert(wglMakeCurrent(m_DC, g_GLRC));
RECT rect;
GetWindowRect(m_Window, &rect);
glViewport(0, 0, rect.right - rect.left, rect.bottom - rect.top);
}
bool ContainerWindow::SetRenderContext()
{
m_DC = ::GetDC(m_Window);
if (!m_DC)
log_error("Can't create DC");
int bits = 32;
//static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
//{
// sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
// 1, // Version Number
// PFD_DRAW_TO_WINDOW | // Format Must Support Window
// PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
// PFD_DOUBLEBUFFER, // Must Support Double Buffering
// PFD_TYPE_RGBA, // Request An RGBA Format
// bits, // Select Our Color Depth
// 0, 0, 0, 0, 0, 0, // Color Bits Ignored
// 0, // No Alpha Buffer
// 0, // Shift Bit Ignored
// 0, // No Accumulation Buffer
// 0, 0, 0, 0, // Accumulation Bits Ignored
// 16, // 16Bit Z-Buffer (Depth Buffer)
// 0, // No Stencil Buffer
// 0, // No Auxiliary Buffer
// PFD_MAIN_PLANE, // Main Drawing Layer
// 0, // Reserved
// 0, 0, 0 // Layer Masks Ignored
//};
static PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
GLuint PixelFormat;
if (!(PixelFormat = ChoosePixelFormat(m_DC, &pfd))) // Did Windows Find A Matching Pixel Format?
{
MessageBox(NULL, "Can't Find A Suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!SetPixelFormat(m_DC, PixelFormat, &pfd)) // Are We Able To Set The Pixel Format?
{
MessageBox(NULL, "Can't Set The PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
int pf = 0;
DescribePixelFormat(m_DC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
if (g_GLRC == NULL || !g_IsGLInitialized)
{
if (!(g_GLRC = wglCreateContext(m_DC))) // Are We Able To Get A Rendering Context?
{
MessageBox(NULL, "Can't Create A GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
log_info("Initialize OpenGL");
wglMakeCurrent(m_DC, g_GLRC);
if (!gladLoadGL()) {
log_error("初始化GL错误");
}
g_IsGLInitialized = true;
}
}
// 初始化,创建窗口
void ContainerWindow::Init(Rect pixelRect, int showMode, const Vector2& minSize, const Vector2& maxSize, std::string name)
{
// Aux windows are mac only. on windows they look just like normal utility windows.
if (showMode == kShowAuxWindow)
showMode = kShowUtility;
m_Name = name;
m_ShowMode = static_cast<ShowMode>(showMode);
m_IsClosing = false;
m_InMenuLoop = false;
m_CloseFromScriptDontShutdown = false;
bool shouldMaximize = false;
if (showMode == kShowMainWindow)
{
//shouldMaximize = s_IsMainWindowMaximized = EditorPrefs::GetBool(kIsMainWindowMaximized, false);
//GetRestoredMainWindowDimensions();
}
RECT rect;
rect.left = pixelRect.x;
rect.right = pixelRect.x + pixelRect.width;
rect.top = pixelRect.y;
rect.bottom = pixelRect.y + pixelRect.height;
m_InternalRect.x = m_InternalRect.y = m_InternalRect.width = m_InternalRect.height = 0.0f;
DWORD windowStyle = 0;
DWORD extendedStyle = 0;
LPCWSTR windowClass = WindowUtil::kContainerWindowClassName;
switch (showMode) {
// 容纳GUIPanel的非主窗口
case kShowNormalWindow:
windowStyle = WS_POPUP | WS_CLIPCHILDREN | WS_THICKFRAME;
extendedStyle = WS_EX_TOOLWINDOW;
break;
// 主窗口,含菜单
case kShowMainWindow:
windowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
extendedStyle = 0;
break;
case kShowPopupMenu:
windowStyle = WS_POPUP | WS_CLIPCHILDREN;
extendedStyle = WS_EX_TOOLWINDOW;
windowClass = WindowUtil::kPopupWindowClassName;
break;
case kShowNoShadow:
windowStyle = WS_POPUP | WS_CLIPCHILDREN;
extendedStyle = WS_EX_TOOLWINDOW;
break;
case kShowUtility:
windowStyle = WS_POPUP | WS_CAPTION | WS_THICKFRAME | WS_SYSMENU;
extendedStyle = WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW;
break;
default:
// ErrorString("Unknown container show mode");
break;
}
HWND parentWindow = NULL;
if (showMode == kShowMainWindow)
{
parentWindow = NULL;
}
else
{
// parentWindow = GetMainEditorWindow();
}
//bool notSizeable = (minSize == maxSize) && (minSize != Vector2::zero);
//if (notSizeable)
// windowStyle &= ~(WS_THICKFRAME);
AdjustWindowRectEx(&rect, windowStyle, showMode == kShowMainWindow, extendedStyle);
int extraX = rect.right - rect.left - 200;
int extraY = rect.bottom - rect.top - 200;
m_MinSize.x = minSize.x + extraX;
m_MinSize.y = minSize.y + extraY;
m_MaxSize.x = maxSize.x + extraX;
m_MaxSize.y = maxSize.y + extraY;
//if (showMode == kShowUtility)
// s_ZUtilityWindows.push_back(m_ContainerListNode);
// Create window
m_Window = CreateWindowExW(
extendedStyle,
windowClass,
L"",
windowStyle,
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
parentWindow,
NULL,
winutils::GetInstanceHandle(),
NULL
);
SetWindowLongPtr(m_Window, GWLP_USERDATA, (LONG_PTR)this);
//UpdateMaxMaximizedRect();
if (pixelRect.width > 10 || pixelRect.height > 10)
{
//SetRect(pixelRect);
//if (shouldMaximize)
// SetWindowLong(m_Window, GWL_STYLE, windowStyle | WS_MAXIMIZE);
}
if (showMode == kShowMainWindow)
{
//SetMainEditorWindow(m_Window);
//HMENU menu = GetMainMenuHandle();
//if (menu)
// SetMenu(m_Window, menu);
//GetApplication().UpdateMainWindowTitle();
//GetApplication().UpdateDocumentEdited();
}
ShowWindow(m_Window, SW_SHOW);
//s_ContainerWindows.insert(this);
//ShowInTaskbarIfNoMainWindow(m_Window);
SetRenderContext();
}
void ContainerWindow::SetTitle(const char* title)
{
SetWindowTextA(m_Window, title);
}
void ContainerWindow::SetIcon(LPCSTR iconName)
{
Assert(m_ShowMode == ShowMode::kShowMainWindow);
HICON hWindowIcon = (HICON )LoadImage(winutils::GetInstanceHandle(), iconName, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (hWindowIcon == NULL)
log_error("icon error: %lu", GetLastError());
if (SendMessage(m_Window, WM_SETICON, ICON_BIG, (LPARAM)hWindowIcon) != WM_SETICON)
log_error("icon error: %lu", GetLastError());
if (SendMessage(m_Window, WM_SETICON, ICON_SMALL, (LPARAM)hWindowIcon) != WM_SETICON)
log_error("icon error: %lu", GetLastError());
}
void ContainerWindow::Close()
{
log_info("Close window %s", m_Name);
SendMessage(m_Window, WM_CLOSE, 0, 0);
}
Vector2f ContainerWindow::GetSize()
{
RECT rect;
GetClientRect(m_Window, &rect);
Vector2f size;
size.x = rect.right - rect.left;
size.y = rect.bottom - rect.top;
return size;
}
void ContainerWindow::OnSizeChanged()
{
LuaBind::State state = GetVM()->GetCurThread();
LuaBind::MemberInvoker invoker = MemberInvoker(state, this);
invoker.member = m_Script;
invoker.method = "OnSizeChanged";
invoker.AddMember(m_Script);
invoker.Invoke(0);
}
void ContainerWindow::OnActivateApplication(bool active)
{
}
|