Ejemplo n.º 1
0
internal LRESULT CALLBACK Win32MainWindowCallback(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam)
{
    if (EasyTab_HandleEvent(Window, Message, LParam, WParam) == EASYTAB_OK)
    {
        return true;  // Tablet input
    }

    LRESULT Result = 0;
    ImGuiIO& io = ImGui::GetIO();

    switch (Message)
    {
        // Mouse

        case WM_LBUTTONDOWN:
        {
            io.MouseDown[0] = true;
            return true;
        } break;

        case WM_LBUTTONUP:
        {
            io.MouseDown[0] = false;
            return true;
        } break;

        case WM_RBUTTONDOWN:
        {
            io.MouseDown[1] = true;
            return true;
        } break;
        case WM_RBUTTONUP:
        {
            io.MouseDown[1] = false;
            return true;
        } break;

        case WM_MBUTTONDOWN:
        {
            io.MouseDown[2] = true;
            return true;
        } break;

        case WM_MBUTTONUP:
        {
            io.MouseDown[2] = false;
            return true;
        } break;

        case WM_MOUSEWHEEL:
        {
            io.MouseWheel += GET_WHEEL_DELTA_WPARAM(WParam) > 0 ? +1.0f : -1.0f;
            return true;
        } break;

        case WM_MOUSEMOVE:
        {
            TRACKMOUSEEVENT TrackParam = {};
            TrackParam.dwFlags |= TME_LEAVE;
            TrackParam.hwndTrack = Window;
            TrackParam.cbSize = sizeof(TrackParam);
            TrackMouseEvent(&TrackParam);
            io.MousePos.x = (signed short)(LParam);
            io.MousePos.y = (signed short)(LParam >> 16);
            return true;
        } break;

        case WM_MOUSELEAVE:
        {
            ImGui::GetIO().MouseDown[0] = false;
            ImGui::GetIO().MouseDown[1] = false;
            ImGui::GetIO().MouseDown[2] = false;
            return true;
        } break;


        // Keyboard

        case WM_KEYDOWN:
        {
            if (WParam < 256)
                io.KeysDown[WParam] = 1;
            return true;
        } break;

        case WM_KEYUP:
        {
            if (WParam < 256)
                io.KeysDown[WParam] = 0;
            return true;
        } break;

        case WM_CHAR:
        {
            // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
            if (WParam > 0 && WParam < 0x10000)
                io.AddInputCharacter((unsigned short)WParam);
            return true;
        } break;


        // Window handling

        case WM_DESTROY:
        {
            Mem.IsRunning = false;
            if (RenderingContext)
            {
                wglMakeCurrent(NULL, NULL);
                wglDeleteContext(RenderingContext);
            }
            ReleaseDC(Window, DeviceContext);
            PostQuitMessage(0);
        } break;

        case WM_PAINT:
        {
            PAINTSTRUCT Paint;
            BeginPaint(Window, &Paint);
            // TODO: Redraw here
            EndPaint(Window, &Paint);
        } break;

        case WM_CLOSE:
        {
            // TODO: Handle this with a message to the user?
            Mem.IsRunning = false;
        } break;

        case WM_SIZE:
        {
            int32 Width, Height;

            if (WParam == SIZE_MAXIMIZED)
            {
                int32 WorkAreaWidth = WindowsWorkArea.right - WindowsWorkArea.left;
                int32 WorkAreaHeight = WindowsWorkArea.bottom - WindowsWorkArea.top;
                SetWindowPos(Window, HWND_TOP, WindowsWorkArea.left, WindowsWorkArea.top, WorkAreaWidth, WorkAreaHeight, NULL);
                Width = WorkAreaWidth;
                Height = WorkAreaHeight;
            }
            else
            {
                Width = (int32) LOWORD(LParam);
                Height = (int32) HIWORD(LParam);
            }

            Core::OnWindowResize(&Mem, Width, Height);

            // Clear and swap buffers
            {
                if (Mem.Colors[PapayaCol_Clear])
                {
                    glClearBufferfv(GL_COLOR, 0, (GLfloat*)&Mem.Colors[PapayaCol_Clear]);
                }
                SwapBuffers(DeviceContext);
            }
        } break;

        // WM_NCHITTEST

        case WM_NCHITTEST:
        {
            const LONG BorderWidth = 8; //in pixels
            RECT WindowRect;
            GetWindowRect(Window, &WindowRect);
            long X = GET_X_LPARAM(LParam);
            long Y = GET_Y_LPARAM(LParam);

            if (!IsMaximized(Window))
            {
                //bottom left corner
                if (X >= WindowRect.left && X < WindowRect.left + BorderWidth &&
                    Y < WindowRect.bottom && Y >= WindowRect.bottom - BorderWidth)
                {
                    return HTBOTTOMLEFT;
                }
                //bottom right corner
                if (X < WindowRect.right && X >= WindowRect.right - BorderWidth &&
                    Y < WindowRect.bottom && Y >= WindowRect.bottom - BorderWidth)
                {
                    return HTBOTTOMRIGHT;
                }
                //top left corner
                if (X >= WindowRect.left && X < WindowRect.left + BorderWidth &&
                    Y >= WindowRect.top && Y < WindowRect.top + BorderWidth)
                {
                    return HTTOPLEFT;
                }
                //top right corner
                if (X < WindowRect.right && X >= WindowRect.right - BorderWidth &&
                    Y >= WindowRect.top && Y < WindowRect.top + BorderWidth)
                {
                    return HTTOPRIGHT;
                }
                //left border
                if (X >= WindowRect.left && X < WindowRect.left + BorderWidth)
                {
                    return HTLEFT;
                }
                //right border
                if (X < WindowRect.right && X >= WindowRect.right - BorderWidth)
                {
                    return HTRIGHT;
                }
                //bottom border
                if (Y < WindowRect.bottom && Y >= WindowRect.bottom - BorderWidth)
                {
                    return HTBOTTOM;
                }
                //top border
                if (Y >= WindowRect.top && Y < WindowRect.top + BorderWidth)
                {
                    return HTTOP;
                }
            }

            if (Y - WindowRect.top <= (float)Mem.Window.TitleBarHeight &&
                X > WindowRect.left + 200.0f &&
                X < WindowRect.right - (float)(Mem.Window.TitleBarButtonsWidth + 10))
            {
                return HTCAPTION;
            }

            SetCursor(LoadCursor(NULL, IDC_ARROW));
            return HTCLIENT;
        } break;

        default:
        {
            Result = DefWindowProcA(Window, Message, WParam, LParam);
        } break;
    }

    return(Result);
}
internal LRESULT CALLBACK Win32MainWindowCallback(HWND window, UINT msg, WPARAM w_param, LPARAM l_param)
{
    if (EasyTab_HandleEvent(window, msg, l_param, w_param) == EASYTAB_OK) {
        return true;  // Tablet input
    }

    LRESULT result = 0;
    ImGuiIO& io = ImGui::GetIO();

    switch (msg) {
        // Mouse
        case WM_LBUTTONDOWN: {
            io.MouseDown[0] = true;
            return true;
        } break;

        case WM_LBUTTONUP: {
            io.MouseDown[0] = false;
            return true;
        } break;

        case WM_RBUTTONDOWN: {
            io.MouseDown[1] = true;
            return true;
        } break;

        case WM_RBUTTONUP: {
            io.MouseDown[1] = false;
            return true;
        } break;

        case WM_MBUTTONDOWN: {
            io.MouseDown[2] = true;
            return true;
        } break;

        case WM_MBUTTONUP: {
            io.MouseDown[2] = false;
            return true;
        } break;

        case WM_MOUSEWHEEL: {
            io.MouseWheel += GET_WHEEL_DELTA_WPARAM(w_param) > 0 ? +1.0f : -1.0f;
            return true;
        } break;

        case WM_MOUSEMOVE: {
            TRACKMOUSEEVENT track_param = {};
            track_param.dwFlags |= TME_LEAVE;
            track_param.hwndTrack = window;
            track_param.cbSize = sizeof(track_param);
            TrackMouseEvent(&track_param);
            io.MousePos.x = (signed short)(l_param);
            io.MousePos.y = (signed short)(l_param >> 16);
            return true;
        } break;

        case WM_MOUSELEAVE: {
            ImGui::GetIO().MouseDown[0] = false;
            ImGui::GetIO().MouseDown[1] = false;
            ImGui::GetIO().MouseDown[2] = false;
            return true;
        } break;

        // Keyboard
        case WM_KEYDOWN: {
            if (w_param < 256)
                io.KeysDown[w_param] = 1;
            return true;
        } break;

        case WM_KEYUP: {
            if (w_param < 256)
                io.KeysDown[w_param] = 0;
            return true;
        } break;

        case WM_CHAR: {
            // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
            if (w_param > 0 && w_param < 0x10000)
                io.AddInputCharacter((unsigned short)w_param);
            return true;
        } break;


        // window handling
        case WM_DESTROY: {
            mem.is_running = false;
            if (rendering_context)
            {
                wglMakeCurrent(NULL, NULL);
                wglDeleteContext(rendering_context);
            }
            ReleaseDC(window, device_context);
            PostQuitMessage(0);
        } break;

        case WM_PAINT: {
            PAINTSTRUCT paint;
            BeginPaint(window, &paint);
            // TODO: Redraw here
            EndPaint(window, &paint);
        } break;

        case WM_CLOSE: {
            // TODO: Handle this with a message to the user?
            mem.is_running = false;
        } break;

        case WM_SIZE: {
            int32 width, height;

            if (w_param == SIZE_MAXIMIZED) {
                int32 work_area_width = windows_work_area.right - windows_work_area.left;
                int32 work_area_height = windows_work_area.bottom - windows_work_area.top;
                SetWindowPos(window, HWND_TOP, windows_work_area.left, windows_work_area.top, work_area_width, work_area_height, NULL);
                width = work_area_width;
                height = work_area_height;
            }
            else
            {
                width = (int32) LOWORD(l_param);
                height = (int32) HIWORD(l_param);
            }

            core::resize(&mem, width, height);

            // Clear and swap buffers
            {
                if (mem.colors[PapayaCol_Clear]) {
                    glClearBufferfv(GL_COLOR, 0, (GLfloat*)&mem.colors[PapayaCol_Clear]);
                }
                SwapBuffers(device_context);
            }
        } break;

        // WM_NCHITTEST

        case WM_NCHITTEST: {
            const LONG border_width = 8; //in pixels
            RECT window_rect;
            GetWindowRect(window, &window_rect);
            long X = GET_X_LPARAM(l_param);
            long Y = GET_Y_LPARAM(l_param);

            if (!IsMaximized(window))
            {
                //bottom left corner
                if (X >= window_rect.left && X < window_rect.left + border_width &&
                    Y < window_rect.bottom && Y >= window_rect.bottom - border_width) {
                    return HTBOTTOMLEFT;
                }
                //bottom right corner
                if (X < window_rect.right && X >= window_rect.right - border_width &&
                    Y < window_rect.bottom && Y >= window_rect.bottom - border_width) {
                    return HTBOTTOMRIGHT;
                }
                //top left corner
                if (X >= window_rect.left && X < window_rect.left + border_width &&
                    Y >= window_rect.top && Y < window_rect.top + border_width) {
                    return HTTOPLEFT;
                }
                //top right corner
                if (X < window_rect.right && X >= window_rect.right - border_width &&
                    Y >= window_rect.top && Y < window_rect.top + border_width) {
                    return HTTOPRIGHT;
                }
                //left border
                if (X >= window_rect.left && X < window_rect.left + border_width) {
                    return HTLEFT;
                }
                //right border
                if (X < window_rect.right && X >= window_rect.right - border_width) {
                    return HTRIGHT;
                }
                //bottom border
                if (Y < window_rect.bottom && Y >= window_rect.bottom - border_width) {
                    return HTBOTTOM;
                }
                //top border
                if (Y >= window_rect.top && Y < window_rect.top + border_width) {
                    return HTTOP;
                }
            }

            if (Y - window_rect.top <= (float)mem.window.title_bar_height &&
                X > window_rect.left + 200.0f &&
                X < window_rect.right - (float)(mem.window.title_bar_buttons_width + 10)) {
                return HTCAPTION;
            }

            SetCursor(LoadCursor(NULL, IDC_ARROW));
            return HTCLIENT;
        } break;

        default: {
            result = DefWindowProcA(window, msg, w_param, l_param);
        } break;
    }

    return(result);
}