<commit_before>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#include <windowsx.h>
#include "WindowResourceWin.hpp"
#include "core/Engine.hpp"
#include "core/Window.hpp"
#include "input/windows/InputWin.hpp"
#include "graphics/Renderer.hpp"
#include "graphics/RenderDevice.hpp"
#include "graphics/direct3d11/RenderDeviceD3D11.hpp"
#include "utils/Log.hpp"

static void handleKeyEvent(UINT msg, WPARAM wParam, LPARAM lParam)
{
    UINT key = static_cast<UINT>(wParam);
    switch (key)
    {
        case VK_MENU:
            if ((lParam & 0x1000000) == 0)
                key = VK_LMENU;
            else
                key = VK_RMENU;
            break;
        case VK_CONTROL:
            if ((lParam & 0x1000000) == 0)
                key = VK_LCONTROL;
            else
                key = VK_RCONTROL;
            break;
        case VK_SHIFT:
            key = MapVirtualKey((lParam & 0x00ff0000) >> 16, MAPVK_VSC_TO_VK_EX);
            break;
    }

    if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN)
    {
        ouzel::engine->getInput()->keyPress(ouzel::input::InputWin::convertKeyCode(key), 0);
    }
    else if (msg == WM_KEYUP || msg == WM_SYSKEYUP)
    {
        ouzel::engine->getInput()->keyRelease(ouzel::input::InputWin::convertKeyCode(key), 0);
    }
}

static void handleMouseMoveEvent(UINT, WPARAM wParam, LPARAM lParam)
{
    ouzel::Vector2 position(static_cast<float>(GET_X_LPARAM(lParam)),
                            static_cast<float>(GET_Y_LPARAM(lParam)));

    ouzel::engine->getInput()->mouseMove(ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                               ouzel::input::InputWin::getModifiers(wParam));
}

static void handleMouseButtonEvent(UINT msg, WPARAM wParam, LPARAM lParam)
{
    ouzel::Vector2 position(static_cast<float>(GET_X_LPARAM(lParam)),
                            static_cast<float>(GET_Y_LPARAM(lParam)));

    ouzel::input::MouseButton button;

    if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP)
    {
        button = ouzel::input::MouseButton::LEFT;
    }
    else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP)
    {
        button = ouzel::input::MouseButton::RIGHT;
    }
    else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP)
    {
        button = ouzel::input::MouseButton::MIDDLE;
    }
    else if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP)
    {
        if (GET_XBUTTON_WPARAM(wParam) == XBUTTON1)
            button = ouzel::input::MouseButton::X1;
        else if (GET_XBUTTON_WPARAM(wParam) == XBUTTON2)
            button = ouzel::input::MouseButton::X2;
        else
            return;
    }
    else
    {
        return;
    }

    if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_XBUTTONDOWN)
    {
        ouzel::engine->getInput()->mouseButtonPress(button,
                                                          ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                          ouzel::input::InputWin::getModifiers(wParam));
    }
    else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP || msg == WM_XBUTTONUP)
    {
        ouzel::engine->getInput()->mouseButtonRelease(button,
                                                            ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                            ouzel::input::InputWin::getModifiers(wParam));
    }
}

static void handleMouseWheelEvent(UINT msg, WPARAM wParam, LPARAM lParam)
{
    ouzel::Vector2 position(static_cast<float>(GET_X_LPARAM(lParam)),
                            static_cast<float>(GET_Y_LPARAM(lParam)));

    if (msg == WM_MOUSEWHEEL)
    {
        short param = static_cast<short>(HIWORD(wParam));
        ouzel::engine->getInput()->mouseScroll(ouzel::Vector2(0.0f, -static_cast<float>(param) / static_cast<float>(WHEEL_DELTA)),
                                                     ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                     ouzel::input::InputWin::getModifiers(wParam));
    }
    else if (msg == WM_MOUSEHWHEEL)
    {
        short param = static_cast<short>(HIWORD(wParam));
        ouzel::engine->getInput()->mouseScroll(ouzel::Vector2(static_cast<float>(param) / static_cast<float>(WHEEL_DELTA), 0.0f),
                                                     ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                     ouzel::input::InputWin::getModifiers(wParam));
    }
}

static void handleTouchEvent(WPARAM wParam, LPARAM lParam)
{
    UINT inputCount = LOWORD(wParam);
    std::vector<TOUCHINPUT> touches(inputCount);

    if (GetTouchInputInfo(reinterpret_cast<HTOUCHINPUT>(lParam), inputCount, touches.data(), sizeof(TOUCHINPUT)))
    {
        ouzel::Vector2 position;

        for (const TOUCHINPUT& touch : touches)
        {
            position.x = static_cast<float>(touch.x / 100);
            position.y = static_cast<float>(touch.y / 100);

            if (touch.dwFlags & TOUCHEVENTF_DOWN)
            {
                ouzel::engine->getInput()->touchBegin(touch.dwID,
                                                            ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position));
            }

            if (touch.dwFlags & TOUCHEVENTF_UP)
            {
                ouzel::engine->getInput()->touchEnd(touch.dwID,
                                                          ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position));
            }

            if (touch.dwFlags & TOUCHEVENTF_MOVE)
            {
                ouzel::engine->getInput()->touchMove(touch.dwID,
                                                           ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position));
            }
        }

        if (!CloseTouchInputHandle(reinterpret_cast<HTOUCHINPUT>(lParam)))
        {
            ouzel::Log(ouzel::Log::Level::ERR) << "Failed to close touch input handle";
        }
    }
    else
    {
        ouzel::Log(ouzel::Log::Level::ERR) << "Failed to get touch info";
    }
}

static const LONG_PTR SIGNATURE_MASK = 0x0FFFFFF00;
static const LONG_PTR MOUSEEVENTF_FROMTOUCH = 0x0FF515700;

static LRESULT CALLBACK windowProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam)
{
    ouzel::WindowResourceWin* windowWin = reinterpret_cast<ouzel::WindowResourceWin*>(GetWindowLongPtr(window, GWLP_USERDATA));
    if (!windowWin) return DefWindowProcW(window, msg, wParam, lParam);

    switch (msg)
    {
        case WM_ACTIVATEAPP:
        {
            if (wParam)
            {
                if (ouzel::engine) ouzel::engine->resume();
            }
            else
            {
                if (ouzel::engine) ouzel::engine->pause();
            }
            break;
        }
        case WM_KEYUP:
        case WM_KEYDOWN:
        case WM_SYSKEYDOWN:<commit_msg>Fix the order of the case statements<commit_after>// Copyright (C) 2017 <NAME>
// This file is part of the Ouzel engine.

#include <windowsx.h>
#include "WindowResourceWin.hpp"
#include "core/Engine.hpp"
#include "core/Window.hpp"
#include "input/windows/InputWin.hpp"
#include "graphics/Renderer.hpp"
#include "graphics/RenderDevice.hpp"
#include "graphics/direct3d11/RenderDeviceD3D11.hpp"
#include "utils/Log.hpp"

static void handleKeyEvent(UINT msg, WPARAM wParam, LPARAM lParam)
{
    UINT key = static_cast<UINT>(wParam);
    switch (key)
    {
        case VK_MENU:
            if ((lParam & 0x1000000) == 0)
                key = VK_LMENU;
            else
                key = VK_RMENU;
            break;
        case VK_CONTROL:
            if ((lParam & 0x1000000) == 0)
                key = VK_LCONTROL;
            else
                key = VK_RCONTROL;
            break;
        case VK_SHIFT:
            key = MapVirtualKey((lParam & 0x00ff0000) >> 16, MAPVK_VSC_TO_VK_EX);
            break;
    }

    if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN)
    {
        ouzel::engine->getInput()->keyPress(ouzel::input::InputWin::convertKeyCode(key), 0);
    }
    else if (msg == WM_KEYUP || msg == WM_SYSKEYUP)
    {
        ouzel::engine->getInput()->keyRelease(ouzel::input::InputWin::convertKeyCode(key), 0);
    }
}

static void handleMouseMoveEvent(UINT, WPARAM wParam, LPARAM lParam)
{
    ouzel::Vector2 position(static_cast<float>(GET_X_LPARAM(lParam)),
                            static_cast<float>(GET_Y_LPARAM(lParam)));

    ouzel::engine->getInput()->mouseMove(ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                               ouzel::input::InputWin::getModifiers(wParam));
}

static void handleMouseButtonEvent(UINT msg, WPARAM wParam, LPARAM lParam)
{
    ouzel::Vector2 position(static_cast<float>(GET_X_LPARAM(lParam)),
                            static_cast<float>(GET_Y_LPARAM(lParam)));

    ouzel::input::MouseButton button;

    if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP)
    {
        button = ouzel::input::MouseButton::LEFT;
    }
    else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP)
    {
        button = ouzel::input::MouseButton::RIGHT;
    }
    else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP)
    {
        button = ouzel::input::MouseButton::MIDDLE;
    }
    else if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONUP)
    {
        if (GET_XBUTTON_WPARAM(wParam) == XBUTTON1)
            button = ouzel::input::MouseButton::X1;
        else if (GET_XBUTTON_WPARAM(wParam) == XBUTTON2)
            button = ouzel::input::MouseButton::X2;
        else
            return;
    }
    else
    {
        return;
    }

    if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_XBUTTONDOWN)
    {
        ouzel::engine->getInput()->mouseButtonPress(button,
                                                          ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                          ouzel::input::InputWin::getModifiers(wParam));
    }
    else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP || msg == WM_XBUTTONUP)
    {
        ouzel::engine->getInput()->mouseButtonRelease(button,
                                                            ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                            ouzel::input::InputWin::getModifiers(wParam));
    }
}

static void handleMouseWheelEvent(UINT msg, WPARAM wParam, LPARAM lParam)
{
    ouzel::Vector2 position(static_cast<float>(GET_X_LPARAM(lParam)),
                            static_cast<float>(GET_Y_LPARAM(lParam)));

    if (msg == WM_MOUSEWHEEL)
    {
        short param = static_cast<short>(HIWORD(wParam));
        ouzel::engine->getInput()->mouseScroll(ouzel::Vector2(0.0f, -static_cast<float>(param) / static_cast<float>(WHEEL_DELTA)),
                                                     ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                     ouzel::input::InputWin::getModifiers(wParam));
    }
    else if (msg == WM_MOUSEHWHEEL)
    {
        short param = static_cast<short>(HIWORD(wParam));
        ouzel::engine->getInput()->mouseScroll(ouzel::Vector2(static_cast<float>(param) / static_cast<float>(WHEEL_DELTA), 0.0f),
                                                     ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position),
                                                     ouzel::input::InputWin::getModifiers(wParam));
    }
}

static void handleTouchEvent(WPARAM wParam, LPARAM lParam)
{
    UINT inputCount = LOWORD(wParam);
    std::vector<TOUCHINPUT> touches(inputCount);

    if (GetTouchInputInfo(reinterpret_cast<HTOUCHINPUT>(lParam), inputCount, touches.data(), sizeof(TOUCHINPUT)))
    {
        ouzel::Vector2 position;

        for (const TOUCHINPUT& touch : touches)
        {
            position.x = static_cast<float>(touch.x / 100);
            position.y = static_cast<float>(touch.y / 100);

            if (touch.dwFlags & TOUCHEVENTF_DOWN)
            {
                ouzel::engine->getInput()->touchBegin(touch.dwID,
                                                            ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position));
            }

            if (touch.dwFlags & TOUCHEVENTF_UP)
            {
                ouzel::engine->getInput()->touchEnd(touch.dwID,
                                                          ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position));
            }

            if (touch.dwFlags & TOUCHEVENTF_MOVE)
            {
                ouzel::engine->getInput()->touchMove(touch.dwID,
                                                           ouzel::engine->getWindow()->convertWindowToNormalizedLocation(position));
            }
        }

        if (!CloseTouchInputHandle(reinterpret_cast<HTOUCHINPUT>(lParam)))
        {
            ouzel::Log(ouzel::Log::Level::ERR) << "Failed to close touch input handle";
        }
    }
    else
    {
        ouzel::Log(ouzel::Log::Level::ERR) << "Failed to get touch info";
    }
}

static const LONG_PTR SIGNATURE_MASK = 0x0FFFFFF00;
static const LONG_PTR MOUSEEVENTF_FROMTOUCH = 0x0FF515700;

static LRESULT CALLBACK windowProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam)
{
    ouzel::WindowResourceWin* windowWin = reinterpret_cast<ouzel::WindowResourceWin*>(GetWindowLongPtr(window, GWLP_USERDATA));
    if (!windowWin) return DefWindowProcW(window, msg, wParam, lParam);

    switch (msg)
    {
        case WM_ACTIVATEAPP:
        {
            if (wParam)
            {
                if (ouzel::engine) ouzel::engine->resume();
            }
            else
            {
                if (ouzel::engine) ouzel::engine->pause();
            }
            break;
        }
        case WM_KEYDOWN:
        case WM_KEYUP:
        case WM_SYSKEYDOWN:
{'fraction_non_alphanumeric': 0.12067385634736026, 'fraction_numerical': 0.01000916331853105, 'mean_word_length': 2.093089165031611, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3856695', 'n_tokens_mistral': 4239, 'n_tokens_neox': 4162, 'n_words': 667}