summaryrefslogtreecommitdiff
path: root/Editor/GUI/ContainerWindow.cpp
blob: 7a71d14ff9c35cb469a05a1b32e0bc103767662f (plain)
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��������ڵIJ˵���ɾ��
				::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)
{

}