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
|
#include "EditorWindows.h"
#include "WinUtils.h"
#include "Runtime/Graphics/OpenGL.h"
#include "Editor/Graphics/Graphics.h"
static bool RedirectMouseWheel(HWND window, WPARAM wParam, LPARAM lParam)
{
/// prevent reentrancy
//static bool s_ReentrancyCheck = false;
//if (s_ReentrancyCheck)
// return false;
/// 找到对应的子窗口
//GUIWindow* view = GetMouseOverWindow();
//if (!view)
// return false;
/// 将鼠标滚轮事件派发到子窗口上去
/// send mouse wheel to view under mouse
//s_ReentrancyCheck = true;
//::SendMessage(view->GetWindowHandle(), WM_MOUSEWHEEL, wParam, lParam);
//s_ReentrancyCheck = false;
return true;
}
static PAINTSTRUCT ps;
LRESULT CALLBACK GUIWindow::GUIViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
GUIWindow* self = (GUIWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (!self)
{
log_warning("GUIWindow::GUIViewWndProc(), lParam为空");
return DefWindowProcW(hWnd, message, wParam, lParam);
}
//log_info("WndProc", "GUIWindow::GUIViewWndProc(), GUIWindow name is " + wnd->GetName());
switch (message)
{
case WM_ERASEBKGND:
return 1;
case WM_PAINT:
{
log_info("WndProc", "WM_PAINT");
self->SetAsRenderContext();
self->OnPaint();
BeginPaint(self->m_Handle, &ps);
EndPaint(self->m_Handle, &ps);
UpdateWindow(self->m_Handle);
return 0;
}
case WM_MOUSEWHEEL: // 在这个子窗口滚动
{
log_info("WndProc","WM_MOUSEWHEEL");
// quick check if mouse is in our window
RECT rc;
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
::GetWindowRect(hWnd, &rc);
if (!::PtInRect(&rc, pt)) //如果不在这个子窗口范围内
{
// 重新派发到鼠标所在的那个窗口上去,这里其实就是之前很多windows用户反映的,明明是在某个窗口上滚动,但由于这个窗口没
// 被选中,导致滚动事件被触发在选中的那个窗口上
if (RedirectMouseWheel(hWnd, wParam, lParam))
return 0;
}
//Assert(self->m_Handle != hWnd);
//self->ProcessEventMessages(message, wParam, lParam);
return 0;
}
case WM_CAPTURECHANGED:
return 0;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_XBUTTONDBLCLK:
case WM_KEYUP:
case WM_KEYDOWN:
case WM_SYSKEYUP:
case WM_SYSKEYDOWN:
case WM_CHAR:
case WM_MOUSEMOVE:
switch (message)
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
{
SetFocus(hWnd);
self->DoPaint();
if (/*processInput && !focused*/ true)
{
//focused = (GetFocus() == hWnd);
//if (focused)
//{
// BOOL handled = FALSE;
// ProcessInputMessage(hWnd, message, wParam, lParam, handled);
//}
}
}
break;
}
// 处理键盘输入,比如输入内容
self->ProcessEventMessages(message, wParam, lParam);
case WM_SETCURSOR:
// We're setting the cursor on every WM_MOUSEMOVE.
//We eat the WM_SETCURSOR event here so the OS doesn't conflict with us causing cursor garbage flicker.
return 1;
case WM_WINDOWPOSCHANGED:
{
// 切换opengl渲染目标,重绘用户区
//if (self->m_GfxWindow)
//{
// RECT rect;
// GetClientRect(hWnd, &rect);
// // 大小变动了
// if (self->m_GfxWindow->GetWidth() != rect.right || self->m_GfxWindow->GetHeight() != rect.bottom)
// {
// // 调用wglMakeCurrent()绑定用户区
// // GLWindow.cpp > Reshape
// self->m_GfxWindow->Reshape(rect.right, rect.bottom, self->m_DepthFormat, self->m_AntiAlias);
// // 请求重绘
// self->RequestRepaint();
// }
// // Update the scene so that scripts marked with [ExecuteInEditMode] are able to react to screen size changes
// GetApplication().SetSceneRepaintDirty();
//}
return 0;
}
case WM_SETFOCUS: //获得焦点
self->OnFocus();
return 0;
case WM_KILLFOCUS: //失去焦点
return 0;
case WM_ACTIVATE:
break;
case WM_CLOSE:
delete self;
return 0;
case WM_INITMENUPOPUP: //下拉菜单或子菜单将要被激活的时候
return 0;
}
return DefWindowProcW(hWnd, message, wParam, lParam);
}
void GUIWindow::RepaintAll()
{
}
////////////////////////////////////////////////////////////
GUIWindow::GUIWindow(LuaBind::VM* vm)
: LuaBind::NativeClass<GUIWindow>(vm)
, m_Script()
{
}
void GUIWindow::ProcessEventMessages(UINT message, WPARAM wParam, LPARAM lParam)
{
}
void GUIWindow::Init(std::string name)
{
log_info("Init GUIWindow " /*+ (long)this*/);
m_Name = name;
DWORD windowStyle = WS_CHILD;
//DWORD windowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
DWORD extendedStyle = WS_EX_TOOLWINDOW;
Internal::Rect pixelRect;
pixelRect.x = 0;
pixelRect.y = 0;
pixelRect.width = 32;
pixelRect.height = 32;
RECT rc;
rc.left = pixelRect.x;
rc.top = pixelRect.y;
rc.right = pixelRect.x + pixelRect.width;
rc.bottom = pixelRect.y + pixelRect.height;
AdjustWindowRectEx(&rc, windowStyle, FALSE, extendedStyle);
m_Handle = CreateWindowExW(
extendedStyle,
WindowUtil::kGUIWindowClassName,
L"",
windowStyle,
rc.left,
rc.top,
rc.right - rc.left,
rc.bottom - rc.top,
HWND_MESSAGE, // message only
NULL,
winutils::GetInstanceHandle(),
NULL
);
SetWindowLongPtr(m_Handle, GWLP_USERDATA, (LONG_PTR)this);
ShowWindow(m_Handle, SW_SHOW);
bool bRC = SetRenderContext();
if (!bRC)
log_error("Failed to setup rendering context");
log_info("Created GUIWindow " /*+ (long)this*/);
}
bool GUIWindow::SetRenderContext()
{
m_DC = ::GetDC(m_Handle);
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 (!(m_RC = 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
}
if (m_RC && !g_IsGLInitialized)
{
log_info("Initialize OpenGL");
wglMakeCurrent(m_DC, m_RC);
if (!gladLoadGL()) {
log_error("初始化GL错误");
}
g_IsGLInitialized = true;
}
return true;
}
void GUIWindow::DoPaint()
{
SetWindowLongPtr(m_Handle, GWLP_USERDATA,(LONG_PTR ) this);
SendMessage(m_Handle, WM_PAINT, 0, 0);
}
void GUIWindow::OnFocus()
{
InvokeLuaCallback(m_Script, "OnFocus");
}
void GUIWindow::OnLostFocus()
{
}
void GUIWindow::OnPaint()
{
InvokeLuaCallback(m_Script, "OnGUI");
}
void GUIWindow::SetPosition(Internal::Rect position)
{
log_info("GUIWindow::SetPosition()");
RECT rc;
rc.left = position.x;
rc.top = position.y;
rc.right = position.x + position.width;
rc.bottom = position.y + position.height;
::SetWindowPos(m_Handle, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_FRAMECHANGED | SWP_NOOWNERZORDER | SWP_NOACTIVATE | SWP_NOZORDER);
}
void GUIWindow::SetAsRenderContext()
{
Assert(m_DC != NULL);
Assert(m_RC != NULL);
Assert(wglMakeCurrent(m_DC, m_RC));
RECT rect;
GetWindowRect(m_Handle, &rect);
glViewport(0, 0, rect.right - rect.left, rect.bottom - rect.top);
}
void GUIWindow::Focus()
{
log_info("Focus GUIWindow " + (long)this);
if (m_Handle)
{
if (::GetForegroundWindow() != m_ContainerWindow->GetWindowHandle())
::SetForegroundWindow(m_ContainerWindow->GetWindowHandle());
SetFocus(m_Handle);
}
}
void GUIWindow::SetContainerWindow(ContainerWindow* wnd)
{
Assert(wnd != NULL);
m_ContainerWindow = wnd;
if (wnd->GetWindowHandle() != m_Handle)
{
HWND parentWindowHandle = wnd->GetWindowHandle();
if (::GetParent(m_Handle) != parentWindowHandle)
::SetParent(m_Handle, parentWindowHandle);
::ShowWindow(m_Handle, SW_SHOWNORMAL);
}
}
|