From 59e6235113a4d933811aa2cf6fdc8282ce394b9d Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 10 Nov 2020 18:46:11 +0800 Subject: *window --- Editor/GUI/EditorWindows.cpp | 164 +++++++++++++++++++++++++++++++++++++++++++ Editor/GUI/EditorWindows.h | 28 +++++++- Editor/GUI/WinUtils.cpp | 43 ++++++++++++ Editor/GUI/WinUtils.h | 17 +++++ Editor/main.cpp | 76 +++++++++----------- Editor/wog.c | 58 +++++++-------- 6 files changed, 313 insertions(+), 73 deletions(-) create mode 100644 Editor/GUI/WinUtils.cpp create mode 100644 Editor/GUI/WinUtils.h (limited to 'Editor') diff --git a/Editor/GUI/EditorWindows.cpp b/Editor/GUI/EditorWindows.cpp index e69de29..18f2e3a 100644 --- a/Editor/GUI/EditorWindows.cpp +++ b/Editor/GUI/EditorWindows.cpp @@ -0,0 +1,164 @@ +#include "EditorWindows.h" +#include "WinUtils.h" + +static const wchar_t* kContainerWindowClassName = L"GameLabContainerWndClass"; +static const wchar_t* kPopupWindowClassName = L"GameLabPopupWndClass"; +static const wchar_t* kViewportClassName = L"GameLabViewportWndClass"; +static const char* kIsMainWindowMaximized = "IsMainWindowMaximized"; + +static ATOM s_ContainerWindowClassAtom; +static ATOM s_PopupWindowClassAtom; +static ATOM s_GUIViewClassAtom; + +void RegisterWindowClasses() +{ + s_ContainerWindowClassAtom = winutils::RegisterWindowClass(kContainerWindowClassName, ContainnerWindow::ContainerWndProc, CS_HREDRAW | CS_VREDRAW); + s_PopupWindowClassAtom = winutils::RegisterWindowClass(kPopupWindowClassName, ContainnerWindow::ContainerWndProc, CS_HREDRAW | CS_VREDRAW | CS_DROPSHADOW);//CS_HREDRAW宽度(水平)变化时重绘、CS_VREDRAW高度(垂直)变化时重绘 + s_GUIViewClassAtom = winutils::RegisterWindowClass(kViewportClassName, Viewport::ViewportWndProc, CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS); +} + +LRESULT CALLBACK ContainnerWindow::ContainerWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + long flag = DefWindowProcW(hWnd, message, wParam, lParam); + return flag; +} + +ContainnerWindow::ContainnerWindow() +{ +} + + +ContainnerWindow::~ContainnerWindow() +{ + +} + +void ContainnerWindow::Init(Rectf pixelRect, int showMode, const Vector2f& minSize, const Vector2f& maxSize) +{ + // Aux windows are mac only. on windows they look just like normal utility windows. + if (showMode == kShowAuxWindow) + showMode = kShowUtility; + + m_ShowMode = static_cast(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 = { 120, 120, 220, 220 }; + m_InternalRect.x = m_InternalRect.y = m_InternalRect.width = m_InternalRect.height = 0.0f; + + DWORD windowStyle = 0; + DWORD extendedStyle = 0; + LPCWSTR windowClass = 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 = 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 != Vector2f::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); + +} + +LRESULT CALLBACK Viewport::ViewportWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + long flag = DefWindowProcW(hWnd, message, wParam, lParam); + return flag; +} + diff --git a/Editor/GUI/EditorWindows.h b/Editor/GUI/EditorWindows.h index 90e71a4..ca1c059 100644 --- a/Editor/GUI/EditorWindows.h +++ b/Editor/GUI/EditorWindows.h @@ -2,23 +2,49 @@ #define EDITOR_WINDOW_H #include +#include "../../Runtime/Math/Rect.h" + +void RegisterWindowClasses(); // 一个containner window中有多个viewport class ContainnerWindow { public: + enum ShowMode { + kShowNormalWindow = 0, // normal window with max/min/close buttons + kShowPopupMenu = 1, // popup menu - no title bar + kShowUtility = 2, // tool window - floats in the app, and hides when the app deactivates + kShowNoShadow = 3, // no shadow/decorations + kShowMainWindow = 4, // main Unity window. On Mac does not have close button; on Windows has menu bar etc. + kShowAuxWindow = 5, // Popup windows like the color picker, gradient editor, etc. Drawn with black background on Mac + }; + + ContainnerWindow(); + ~ContainnerWindow(); + + static LRESULT CALLBACK ContainerWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); + + void Init(Rectf size, int showMode, const Vector2f& minSize, const Vector2f& maxSize); private: HWND m_Window; POINT m_Size; + ShowMode m_ShowMode; + bool m_IsClosing; + bool m_InMenuLoop; + bool m_CloseFromScriptDontShutdown; + Rectf m_InternalRect; + POINT m_MinSize; + POINT m_MaxSize; }; -// 窗口内的单个viewport +// 窗口内的单个子窗口,是事件相应、绘制、布局的单元 class Viewport { public: + static LRESULT CALLBACK ViewportWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); private: HWND m_View; diff --git a/Editor/GUI/WinUtils.cpp b/Editor/GUI/WinUtils.cpp new file mode 100644 index 0000000..e7b2d5b --- /dev/null +++ b/Editor/GUI/WinUtils.cpp @@ -0,0 +1,43 @@ +#include "winutils.h" + +namespace winutils +{ + + static HINSTANCE gInstanceHandle = NULL; + + HINSTANCE GetInstanceHandle() + { + return gInstanceHandle; + } + + ATOM RegisterWindowClass(const wchar_t* className, WNDPROC windowProc, unsigned int style) + { +#if DEBUG_WIN_UTILS + printf_console("Debug winutils: register class %s\n", className); +#endif + + WNDCLASSEXW wcex; + memset(&wcex, 0, sizeof(wcex)); + wcex.cbSize = sizeof(wcex); + wcex.style = style; + wcex.lpfnWndProc = windowProc; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = 0; + wcex.hInstance = winutils::GetInstanceHandle(); + //wcex.hIcon = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_APP_ICON); + wcex.hCursor = LoadCursor(NULL, IDC_ARROW); + wcex.hbrBackground = NULL; + wcex.lpszMenuName = NULL; + wcex.lpszClassName = className; + ATOM classAtom = RegisterClassExW(&wcex); + //if (!classAtom) + // printf("Failed to register window class %s: %s\n", className, WIN_LAST_ERROR_TEXT); + return classAtom; + } + + void UnregisterWindowClass(const wchar_t* className) + { + + } + +} diff --git a/Editor/GUI/WinUtils.h b/Editor/GUI/WinUtils.h new file mode 100644 index 0000000..9257d77 --- /dev/null +++ b/Editor/GUI/WinUtils.h @@ -0,0 +1,17 @@ +#ifndef WINUTILS_H +#define WINUTILS_H + +#include + +namespace winutils +{ + HINSTANCE GetInstanceHandle(); + + // 注册windows窗口类 + ATOM RegisterWindowClass(const wchar_t* className, WNDPROC windowProc, unsigned int style); + void UnregisterWindowClass(const wchar_t* className); + + +} + +#endif \ No newline at end of file diff --git a/Editor/main.cpp b/Editor/main.cpp index 58e5f04..193a52a 100644 --- a/Editor/main.cpp +++ b/Editor/main.cpp @@ -1,43 +1,33 @@ -extern "C" { -#include "wog.h" -} - -#include -#include - -wog_Window * wnd ; - -void* proc(const char *name) { - return wglGetProcAddress(name); -} - -int main(int argc, char* argv[]) { - wnd = wog_createWindow("GameLab" , 800, 600, 500, 500, 0); - wog_show(wnd); - - wog_GLContext* ctx = wog_createGLContext(wnd); - wog_makeCurrent(wnd, ctx); - - //gladLoadGLLoader(proc); - int load = gladLoadGL(); - -// wog_makeCurrent(wnd, ctx); - - while (true) { - wog_Event e; - while (wog_pollEvent(wnd, &e)) { - if (e.type == WOG_ECLOSE) - goto quit; - } - //glViewport(0, 0, 500, 500); - glClearColor(0.16, 0.16, 0.16, 1); - glClear(GL_COLOR_BUFFER_BIT); - glFlush(); - - wog_swapBuffers(wnd); - } - -quit: - - return 0; -} +#include +#include "GUI/EditorWindows.h" + +int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) +{ + RegisterWindowClasses(); + + ContainnerWindow* wnd = new ContainnerWindow(); + + Vector2f min = Vector2f(100, 100); + Vector2f max = Vector2f(700, 700); + wnd->Init(Rectf(400, 400, 500, 500), ContainnerWindow::ShowMode::kShowMainWindow, min, max); + + while (1) + { + MSG msg; + //if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + //{ + // if (msg.message == WM_QUIT) return 0; + // TranslateMessage(&msg); + //} + if (GetMessage(&msg, NULL, 0U, 0U) != 0) + { + //ResetGfxDeviceIfNeeded(); + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + } + + + return 0; +} diff --git a/Editor/wog.c b/Editor/wog.c index 6cfbf64..29f0810 100644 --- a/Editor/wog.c +++ b/Editor/wog.c @@ -247,7 +247,7 @@ static wog_Callback onQuit = 0; static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { // Get The Window Context - wog_Window* window = (wog_Window*)(GetWindowLong(hWnd, GWL_USERDATA)); + wog_Window* window = (wog_Window*)(GetWindowLong(hWnd, GWLP_WNDPROC)); // call callback functions #define call(callback)\ @@ -260,7 +260,7 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l { CREATESTRUCT* creation = (CREATESTRUCT*)(lParam); // Store Window Structure Pointer window = (wog_Window*)(creation->lpCreateParams); // Get wog_Window - SetWindowLong(hWnd, GWL_USERDATA, (LONG)(window)); // Save it + SetWindowLong(hWnd, GWLP_WNDPROC, (LONG)(window)); // Save it } return 0; @@ -631,42 +631,42 @@ static int ParseCommandLine(char *cmdline, char **argv) #define console_main main #endif -extern int wog_main(int argc, char* argv[]); +//extern int wog_main(int argc, char* argv[]); /** * Entry of console application. */ -int console_main(int argc, char* argv[]) -{ - int status = wog_main(argc, argv); - return status; -} +//int console_main(int argc, char* argv[]) +//{ +// int status = wog_main(argc, argv); +// return status; +//} /** * Entry of windows application. */ -int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) -{ - g_hinstance = hInst; - - char* temp = GetCommandLine(); - int len = strlen(temp) + 1; - char* cmd = stack_alloc(char, len); - strcpy(cmd, temp); - cmd[len - 1] = '\0'; - - int argc = 0; - char** argv = 0; - argc = ParseCommandLine(cmd, 0); - ParseCommandLine(cmd, 0); - argv = stack_alloc(char*, argc + 1); - ParseCommandLine(cmd, argv); - - int status = console_main(argc, argv); - - return status; -} +//int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) +//{ +// g_hinstance = hInst; +// +// char* temp = GetCommandLine(); +// int len = strlen(temp) + 1; +// char* cmd = stack_alloc(char, len); +// strcpy(cmd, temp); +// cmd[len - 1] = '\0'; +// +// int argc = 0; +// char** argv = 0; +// argc = ParseCommandLine(cmd, 0); +// ParseCommandLine(cmd, 0); +// argv = stack_alloc(char*, argc + 1); +// ParseCommandLine(cmd, argv); +// +// int status = console_main(argc, argv); +// +// return status; +//} void wog_swapBuffers(wog_Window* wnd) -- cgit v1.1-26-g67d0