diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 2cfbafbc5..6d9a3e1df 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -752,7 +752,7 @@ void EditorComponent::Update(float dt) selectionOutlineTimer += dt; // Exit cinema mode: - if (wiInput::down(wiInput::KEYBOARD_BUTTON_ESCAPE)) + if (wiInput::Down(wiInput::KEYBOARD_BUTTON_ESCAPE)) { if (renderPath != nullptr) { @@ -773,49 +773,49 @@ void EditorComponent::Update(float dt) static bool camControlStart = true; if (camControlStart) { - originalMouse = wiInput::getpointer(); + originalMouse = wiInput::GetPointer(); } - XMFLOAT4 currentMouse = wiInput::getpointer(); + XMFLOAT4 currentMouse = wiInput::GetPointer(); float xDif = 0, yDif = 0; - if (wiInput::down(wiInput::MOUSE_BUTTON_MIDDLE)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_MIDDLE)) { camControlStart = false; xDif = currentMouse.x - originalMouse.x; yDif = currentMouse.y - originalMouse.y; xDif = 0.1f*xDif*(1.0f / 60.0f); yDif = 0.1f*yDif*(1.0f / 60.0f); - wiInput::setpointer(originalMouse); - wiInput::hidepointer(true); + wiInput::SetPointer(originalMouse); + wiInput::HidePointer(true); } else { camControlStart = true; - wiInput::hidepointer(false); + wiInput::HidePointer(false); } const float buttonrotSpeed = 2.0f / 60.0f; - if (wiInput::down(wiInput::KEYBOARD_BUTTON_LEFT)) + if (wiInput::Down(wiInput::KEYBOARD_BUTTON_LEFT)) { xDif -= buttonrotSpeed; } - if (wiInput::down(wiInput::KEYBOARD_BUTTON_RIGHT)) + if (wiInput::Down(wiInput::KEYBOARD_BUTTON_RIGHT)) { xDif += buttonrotSpeed; } - if (wiInput::down(wiInput::KEYBOARD_BUTTON_UP)) + if (wiInput::Down(wiInput::KEYBOARD_BUTTON_UP)) { yDif -= buttonrotSpeed; } - if (wiInput::down(wiInput::KEYBOARD_BUTTON_DOWN)) + if (wiInput::Down(wiInput::KEYBOARD_BUTTON_DOWN)) { yDif += buttonrotSpeed; } - const XMFLOAT4 leftStick = wiInput::getanalog(wiInput::GAMEPAD_ANALOG_THUMBSTICK_L, 0); - const XMFLOAT4 rightStick = wiInput::getanalog(wiInput::GAMEPAD_ANALOG_THUMBSTICK_R, 0); - const XMFLOAT4 rightTrigger = wiInput::getanalog(wiInput::GAMEPAD_ANALOG_TRIGGER_R, 0); + const XMFLOAT4 leftStick = wiInput::GetAnalog(wiInput::GAMEPAD_ANALOG_THUMBSTICK_L, 0); + const XMFLOAT4 rightStick = wiInput::GetAnalog(wiInput::GAMEPAD_ANALOG_THUMBSTICK_R, 0); + const XMFLOAT4 rightTrigger = wiInput::GetAnalog(wiInput::GAMEPAD_ANALOG_TRIGGER_R, 0); const float jostickrotspeed = 0.05f; xDif += rightStick.x * jostickrotspeed; @@ -830,19 +830,19 @@ void EditorComponent::Update(float dt) // FPS Camera const float clampedDT = min(dt, 0.1f); // if dt > 100 millisec, don't allow the camera to jump too far... - const float speed = ((wiInput::down(wiInput::KEYBOARD_BUTTON_LSHIFT) ? 10.0f : 1.0f) + rightTrigger.x * 10.0f) * cameraWnd->movespeedSlider->GetValue() * clampedDT; + const float speed = ((wiInput::Down(wiInput::KEYBOARD_BUTTON_LSHIFT) ? 10.0f : 1.0f) + rightTrigger.x * 10.0f) * cameraWnd->movespeedSlider->GetValue() * clampedDT; static XMVECTOR move = XMVectorSet(0, 0, 0, 0); XMVECTOR moveNew = XMVectorSet(leftStick.x, 0, leftStick.y, 0); - if (!wiInput::down(wiInput::KEYBOARD_BUTTON_LCONTROL)) + if (!wiInput::Down(wiInput::KEYBOARD_BUTTON_LCONTROL)) { // Only move camera if control not pressed - if (wiInput::down((wiInput::BUTTON)'A') || wiInput::down(wiInput::GAMEPAD_BUTTON_LEFT)) { moveNew += XMVectorSet(-1, 0, 0, 0); } - if (wiInput::down((wiInput::BUTTON)'D') || wiInput::down(wiInput::GAMEPAD_BUTTON_RIGHT)) { moveNew += XMVectorSet(1, 0, 0, 0); } - if (wiInput::down((wiInput::BUTTON)'W') || wiInput::down(wiInput::GAMEPAD_BUTTON_UP)) { moveNew += XMVectorSet(0, 0, 1, 0); } - if (wiInput::down((wiInput::BUTTON)'S') || wiInput::down(wiInput::GAMEPAD_BUTTON_DOWN)) { moveNew += XMVectorSet(0, 0, -1, 0); } - if (wiInput::down((wiInput::BUTTON)'E') || wiInput::down(wiInput::GAMEPAD_BUTTON_2)) { moveNew += XMVectorSet(0, 1, 0, 0); } - if (wiInput::down((wiInput::BUTTON)'Q') || wiInput::down(wiInput::GAMEPAD_BUTTON_1)) { moveNew += XMVectorSet(0, -1, 0, 0); } + if (wiInput::Down((wiInput::BUTTON)'A') || wiInput::Down(wiInput::GAMEPAD_BUTTON_LEFT)) { moveNew += XMVectorSet(-1, 0, 0, 0); } + if (wiInput::Down((wiInput::BUTTON)'D') || wiInput::Down(wiInput::GAMEPAD_BUTTON_RIGHT)) { moveNew += XMVectorSet(1, 0, 0, 0); } + if (wiInput::Down((wiInput::BUTTON)'W') || wiInput::Down(wiInput::GAMEPAD_BUTTON_UP)) { moveNew += XMVectorSet(0, 0, 1, 0); } + if (wiInput::Down((wiInput::BUTTON)'S') || wiInput::Down(wiInput::GAMEPAD_BUTTON_DOWN)) { moveNew += XMVectorSet(0, 0, -1, 0); } + if (wiInput::Down((wiInput::BUTTON)'E') || wiInput::Down(wiInput::GAMEPAD_BUTTON_2)) { moveNew += XMVectorSet(0, 1, 0, 0); } + if (wiInput::Down((wiInput::BUTTON)'Q') || wiInput::Down(wiInput::GAMEPAD_BUTTON_1)) { moveNew += XMVectorSet(0, -1, 0, 0); } moveNew += XMVector3Normalize(moveNew); } moveNew *= speed; @@ -872,14 +872,14 @@ void EditorComponent::Update(float dt) { // Orbital Camera - if (wiInput::down(wiInput::KEYBOARD_BUTTON_LSHIFT)) + if (wiInput::Down(wiInput::KEYBOARD_BUTTON_LSHIFT)) { XMVECTOR V = XMVectorAdd(camera.GetRight() * xDif, camera.GetUp() * yDif) * 10; XMFLOAT3 vec; XMStoreFloat3(&vec, V); cameraWnd->camera_target.Translate(vec); } - else if (wiInput::down(wiInput::KEYBOARD_BUTTON_LCONTROL) || currentMouse.z != 0.0f) + else if (wiInput::Down(wiInput::KEYBOARD_BUTTON_LCONTROL) || currentMouse.z != 0.0f) { cameraWnd->camera_transform.Translate(XMFLOAT3(0, 0, yDif * 4 + currentMouse.z)); cameraWnd->camera_transform.translation_local.z = std::min(0.0f, cameraWnd->camera_transform.translation_local.z); @@ -1076,7 +1076,7 @@ void EditorComponent::Update(float dt) { if (object->GetRenderTypes() & RENDERTYPE_WATER) { - if (wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { // if water, then put a water ripple onto it: wiRenderer::PutWaterRipple(wiHelper::GetOriginalWorkingDirectory() + "images/ripple.png", hovered.position); @@ -1084,10 +1084,10 @@ void EditorComponent::Update(float dt) } else { - if (wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { SoftBodyPhysicsComponent* softBody = scene.softbodies.GetComponent(object->meshID); - if (softBody != nullptr && wiInput::down((wiInput::BUTTON)'P')) + if (softBody != nullptr && wiInput::Down((wiInput::BUTTON)'P')) { MeshComponent* mesh = scene.meshes.GetComponent(object->meshID); @@ -1122,7 +1122,7 @@ void EditorComponent::Update(float dt) } // Visualize soft body pinning: - if (wiInput::down((wiInput::BUTTON)'P')) + if (wiInput::Down((wiInput::BUTTON)'P')) { for (size_t i = 0; i < scene.softbodies.GetCount(); ++i) { @@ -1155,7 +1155,7 @@ void EditorComponent::Update(float dt) // Select... static bool selectAll = false; - if (wiInput::press(wiInput::MOUSE_BUTTON_RIGHT) || selectAll) + if (wiInput::Press(wiInput::MOUSE_BUTTON_RIGHT) || selectAll) { wiArchive* archive = AdvanceHistory(); @@ -1193,7 +1193,7 @@ void EditorComponent::Update(float dt) { // Add the hovered item to the selection: - if (!selected.empty() && wiInput::down(wiInput::KEYBOARD_BUTTON_LSHIFT)) + if (!selected.empty() && wiInput::Down(wiInput::KEYBOARD_BUTTON_LSHIFT)) { // Union selection: list saved = selected; @@ -1327,7 +1327,7 @@ void EditorComponent::Update(float dt) } // Delete - if (wiInput::press(wiInput::KEYBOARD_BUTTON_DELETE)) + if (wiInput::Press(wiInput::KEYBOARD_BUTTON_DELETE)) { wiArchive* archive = AdvanceHistory(); @@ -1352,15 +1352,15 @@ void EditorComponent::Update(float dt) } // Control operations... - if (wiInput::down(wiInput::KEYBOARD_BUTTON_LCONTROL)) + if (wiInput::Down(wiInput::KEYBOARD_BUTTON_LCONTROL)) { // Select All - if (wiInput::press((wiInput::BUTTON)'A')) + if (wiInput::Press((wiInput::BUTTON)'A')) { selectAll = true; } // Copy - if (wiInput::press((wiInput::BUTTON)'C')) + if (wiInput::Press((wiInput::BUTTON)'C')) { auto prevSel = selected; EndTranslate(); @@ -1377,7 +1377,7 @@ void EditorComponent::Update(float dt) BeginTranslate(); } // Paste - if (wiInput::press((wiInput::BUTTON)'V')) + if (wiInput::Press((wiInput::BUTTON)'V')) { auto prevSel = selected; EndTranslate(); @@ -1395,7 +1395,7 @@ void EditorComponent::Update(float dt) BeginTranslate(); } // Duplicate Instances - if (wiInput::press((wiInput::BUTTON)'D')) + if (wiInput::Press((wiInput::BUTTON)'D')) { auto prevSel = selected; EndTranslate(); @@ -1408,7 +1408,7 @@ void EditorComponent::Update(float dt) BeginTranslate(); } // Put Instances - if (clipboard != nullptr && hovered.subsetIndex >= 0 && wiInput::down(wiInput::KEYBOARD_BUTTON_LSHIFT) && wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (clipboard != nullptr && hovered.subsetIndex >= 0 && wiInput::Down(wiInput::KEYBOARD_BUTTON_LSHIFT) && wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { XMMATRIX M = XMLoadFloat4x4(&hovered.orientation); @@ -1427,12 +1427,12 @@ void EditorComponent::Update(float dt) } } // Undo - if (wiInput::press((wiInput::BUTTON)'Z')) + if (wiInput::Press((wiInput::BUTTON)'Z')) { ConsumeHistoryOperation(true); } // Redo - if (wiInput::press((wiInput::BUTTON)'Y')) + if (wiInput::Press((wiInput::BUTTON)'Y')) { ConsumeHistoryOperation(false); } diff --git a/Editor/Translator.cpp b/Editor/Translator.cpp index 2d8b77472..1df655ddb 100644 --- a/Editor/Translator.cpp +++ b/Editor/Translator.cpp @@ -203,7 +203,7 @@ void Translator::Update() dragStarted = false; dragEnded = false; - XMFLOAT4 pointer = wiInput::getpointer(); + XMFLOAT4 pointer = wiInput::GetPointer(); const CameraComponent& cam = wiRenderer::GetCamera(); XMVECTOR pos = transform.GetPositionV(); @@ -285,7 +285,7 @@ void Translator::Update() } } - if (dragging || (state != TRANSLATOR_IDLE && wiInput::down(wiInput::MOUSE_BUTTON_LEFT))) + if (dragging || (state != TRANSLATOR_IDLE && wiInput::Down(wiInput::MOUSE_BUTTON_LEFT))) { XMVECTOR plane, planeNormal; if (state == TRANSLATOR_X) @@ -403,7 +403,7 @@ void Translator::Update() dragging = true; } - if (!wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (!wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { if (dragging) { diff --git a/Editor/main.cpp b/Editor/main.cpp index cb84a7a95..bf64c6c67 100644 --- a/Editor/main.cpp +++ b/Editor/main.cpp @@ -214,9 +214,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) break; case WM_MOUSEWHEEL: { - XMFLOAT4 pointer = wiInput::getpointer(); + XMFLOAT4 pointer = wiInput::GetPointer(); float delta = GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA; - wiInput::setpointer(XMFLOAT4(pointer.x, pointer.y, delta, 0)); + wiInput::SetPointer(XMFLOAT4(pointer.x, pointer.y, delta, 0)); } break; case WM_KEYDOWN: diff --git a/README.md b/README.md index 272c2303f..4e2098605 100644 --- a/README.md +++ b/README.md @@ -124,8 +124,8 @@ wiAudio::Play(&mySoundInstance); // Play the sound instance wiAudio::SetVolume(0.6, &mySoundInstance); // Set the volume of this soundinstance wiAudio::SetVolume(0.2); // Set the master volume -if (wiInput::press(wiInput::KEYBOARD_BUTTON_SPACE)) { wiAudio::Stop(&mySoundInstance); } // You can check if a button is pressed or not (this only triggers once) -if (wiInput::down(wiInput::KEYBOARD_BUTTON_SPACE)) { wiAudio::Play(&mySoundInstance); } // You can check if a button is pushed down or not (this triggers repeatedly) +if (wiInput::Press(wiInput::KEYBOARD_BUTTON_SPACE)) { wiAudio::Stop(&mySoundInstance); } // You can check if a button is pressed or not (this only triggers once) +if (wiInput::Down(wiInput::KEYBOARD_BUTTON_SPACE)) { wiAudio::Play(&mySoundInstance); } // You can check if a button is pushed down or not (this triggers repeatedly) ``` Some scripting examples (LUA): @@ -169,7 +169,7 @@ audio.SetVolume(0.6, soundinstance) -- sets the volume of this soundinstance audio.SetVolume(0.2) -- sets the master volume -- Check for input: -if(input.press(KEYBOARD_BUTTON_LEFT)) then +if(input.Press(KEYBOARD_BUTTON_LEFT)) then audio.Play(soundinstance); -- this will play the sound if you press the left arrow on the keyboard end ``` diff --git a/WickedEngine/wiDirectInput.cpp b/WickedEngine/wiDirectInput.cpp index 86c47f3b9..816d8ab04 100644 --- a/WickedEngine/wiDirectInput.cpp +++ b/WickedEngine/wiDirectInput.cpp @@ -6,8 +6,7 @@ #include "wiWindowRegistration.h" -#include - +#include // cstdio doesn't work here!! #include #include @@ -15,7 +14,9 @@ namespace wiDirectInput { IDirectInput8* directInput = nullptr; IDirectInputDevice8* joysticks[4] = {}; - DIJOYSTATE2 joystick_states[4] = {}; + GUID joystick_guids[arraysize(joysticks)] = {}; + bool joystick_initialized[arraysize(joysticks)] = {}; + DIJOYSTATE2 joystick_states[arraysize(joysticks)] = {}; short connectedJoys = 0; //----------------------------------------------------------------------------- @@ -25,6 +26,17 @@ namespace wiDirectInput //----------------------------------------------------------------------------- BOOL IsXInputDevice(const GUID* pGuidProductFromDirectInput) { + // Cache the GUIDs that were recognized as xinput, because this function is slow and it can be called frequently: + static GUID xinput_guids[16] = {}; + static short xinput_count = 0; + for (short i = 0; i < xinput_count; ++i) + { + if (*pGuidProductFromDirectInput == xinput_guids[i]) + { + return TRUE; + } + } + IWbemLocator* pIWbemLocator = NULL; IEnumWbemClassObject* pEnumDevices = NULL; IWbemClassObject* pDevices[20] = { 0 }; @@ -127,30 +139,54 @@ namespace wiDirectInput if (bCleanupCOM) CoUninitialize(); + if (bIsXinputDevice) + { + xinput_guids[xinput_count++] = *pGuidProductFromDirectInput; + } + return bIsXinputDevice; } BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context) { - HRESULT hr; - - //SLOW!!! - if (IsXInputDevice(&instance->guidProduct)) - return DIENUM_CONTINUE; - - // Obtain an interface to the enumerated joystick. - hr = directInput->CreateDevice(instance->guidInstance, &joysticks[connectedJoys], NULL); - - // If it failed, then we can't use this joystick. (Maybe the user unplugged - // it while we were in the middle of enumerating it.) - if (FAILED(hr)) { + if (connectedJoys >= arraysize(joysticks)) + { + return DIENUM_STOP; + } + if (joystick_guids[connectedJoys] == instance->guidInstance) + { + // If joystick guid in this slot hasn't changed, we don't create it again: + connectedJoys++; return DIENUM_CONTINUE; } - connectedJoys++; - if (connectedJoys < arraysize(joysticks)) + if (IsXInputDevice(&instance->guidProduct)) // <- Watch out, this is very slow!!! + { return DIENUM_CONTINUE; - else - return DIENUM_STOP; + } + + if (joysticks[connectedJoys] != nullptr) + { + // This slot GUID is changed, that means this is an other device now, so delete it: + joysticks[connectedJoys]->Unacquire(); + SAFE_RELEASE(joysticks[connectedJoys]); + } + + // Obtain an interface to the enumerated joystick. + HRESULT hr = directInput->CreateDevice(instance->guidInstance, &joysticks[connectedJoys], NULL); + + // If it failed, then we can't use this joystick. (Maybe the user unplugged + // it while we were in the middle of enumerating it.) + if (FAILED(hr)) + { + return DIENUM_CONTINUE; + } + + // Save the joystick guid, so it will not be readded if hadn't changed: + joystick_guids[connectedJoys] = instance->guidInstance; + joystick_initialized[connectedJoys] = false; + + connectedJoys++; + return DIENUM_CONTINUE; } BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context) { @@ -167,7 +203,7 @@ namespace wiDirectInput // Set the range for the axis for (short i = 0; i < connectedJoys; i++) { - if (FAILED(joysticks[i]->SetProperty(DIPROP_RANGE, &propRange.diph))) + if (FAILED(joysticks[i]->SetProperty(DIPROP_RANGE, &propRange.diph))) { return DIENUM_STOP; } @@ -182,39 +218,6 @@ namespace wiDirectInput hr = DirectInput8Create(wiWindowRegistration::GetRegisteredInstance(), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInput, NULL); assert(SUCCEEDED(hr)); - - hr = directInput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, NULL, DIEDFL_ATTACHEDONLY); - assert(SUCCEEDED(hr)); - - for (short i = 0; i < connectedJoys; i++) - { - DIDEVCAPS capabilities; - - // Set the data format to "simple joystick" - a predefined data format - // - // A data format specifies which controls on a device we are interested in, - // and how they should be reported. This tells DInput that we will be - // passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState(). - hr = joysticks[i]->SetDataFormat(&c_dfDIJoystick2); - assert(SUCCEEDED(hr)); - - // Set the cooperative level to let DInput know how this device should - // interact with the system and with other DInput applications. - hr = joysticks[i]->SetCooperativeLevel(wiWindowRegistration::GetRegisteredWindow(), DISCL_EXCLUSIVE | DISCL_FOREGROUND); - assert(SUCCEEDED(hr)); - - // Determine how many axis the joystick has (so we don't error out setting - // properties for unavailable axis) - capabilities.dwSize = sizeof(DIDEVCAPS); - hr = joysticks[i]->GetCapabilities(&capabilities); - assert(SUCCEEDED(hr)); - - // Enumerate the axes of the joyctick and set the range of each axis. Note: - // we could just use the defaults, but we're just trying to show an example - // of enumerating device objects (axes, buttons, etc.). - hr = joysticks[i]->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS); - assert(SUCCEEDED(hr)); - } } void CleanUp() { @@ -232,27 +235,59 @@ namespace wiDirectInput SAFE_RELEASE(directInput); } - void Update() { + connectedJoys = 0; + HRESULT hr; + + hr = directInput->EnumDevices(DI8DEVCLASS_GAMECTRL, enumCallback, NULL, DIEDFL_ATTACHEDONLY); + assert(SUCCEEDED(hr)); + for (short i = 0; i < connectedJoys; i++) { - if (joysticks[i] == nullptr) + if (!joystick_initialized[i]) { - continue; + joystick_initialized[i] = true; + + DIDEVCAPS capabilities; + + // Set the data format to "simple joystick" - a predefined data format + // + // A data format specifies which controls on a device we are interested in, + // and how they should be reported. This tells DInput that we will be + // passing a DIJOYSTATE2 structure to IDirectInputDevice::GetDeviceState(). + hr = joysticks[i]->SetDataFormat(&c_dfDIJoystick2); + assert(SUCCEEDED(hr)); + + // Set the cooperative level to let DInput know how this device should + // interact with the system and with other DInput applications. + hr = joysticks[i]->SetCooperativeLevel(wiWindowRegistration::GetRegisteredWindow(), DISCL_EXCLUSIVE | DISCL_FOREGROUND); + assert(SUCCEEDED(hr)); + + // Determine how many axis the joystick has (so we don't error out setting + // properties for unavailable axis) + capabilities.dwSize = sizeof(DIDEVCAPS); + hr = joysticks[i]->GetCapabilities(&capabilities); + assert(SUCCEEDED(hr)); + + // Enumerate the axes of the joyctick and set the range of each axis. Note: + // we could just use the defaults, but we're just trying to show an example + // of enumerating device objects (axes, buttons, etc.). + hr = joysticks[i]->EnumObjects(enumAxesCallback, NULL, DIDFT_AXIS); + assert(SUCCEEDED(hr)); } // Poll the device to read the current state hr = joysticks[i]->Poll(); - if (FAILED(hr)) + if (FAILED(hr)) { // DInput is telling us that the input stream has been // interrupted. We aren't tracking any state between polls, so // we don't have any special reset that needs to be done. We // just re-acquire and try again. hr = joysticks[i]->Acquire(); - while (hr == DIERR_INPUTLOST) + while (hr == DIERR_INPUTLOST) { hr = joysticks[i]->Acquire(); } @@ -274,7 +309,10 @@ namespace wiDirectInput // Get the input's device state hr = joysticks[i]->GetDeviceState(sizeof(DIJOYSTATE2), &joystick_states[i]); - assert(SUCCEEDED(hr)); + if (FAILED(hr)) + { + joystick_guids[i] = {}; + } } } @@ -282,14 +320,13 @@ namespace wiDirectInput { return connectedJoys; } - bool GetJoystickData(DIJOYSTATE2* result, short index) + DIJOYSTATE2 GetJoystickData(short index) { if (index < connectedJoys) { - *result = joystick_states[index]; - return true; + return joystick_states[index]; } - return false; + return {}; } } diff --git a/WickedEngine/wiDirectInput.h b/WickedEngine/wiDirectInput.h index e2dc46f0b..4bf50e0fc 100644 --- a/WickedEngine/wiDirectInput.h +++ b/WickedEngine/wiDirectInput.h @@ -2,8 +2,12 @@ #include "CommonInclude.h" #include "Platform.h" -#ifndef WINSTORE_SUPPORT +#if __has_include("dinput.h") && !defined(WINSTORE_SUPPORT) #define WICKEDENGINE_BUILD_DIRECTINPUT +#endif + + +#ifdef WICKEDENGINE_BUILD_DIRECTINPUT #define DIRECTINPUT_VERSION 0x0800 #include @@ -24,11 +28,19 @@ namespace wiDirectInput POV_IDLE = 0xFFFFFFFF }; + // Create the DirectInput device, call this before calling any other functions here void Initialize(); + + // Destroy the DirectInput device void CleanUp(); + + // Update the connected devices void Update(); + // Returns how many joystick devices are connected short GetConnectedJoystickCount(); - bool GetJoystickData(DIJOYSTATE2* result, short index); + + // Returns the state of the specified joystick + DIJOYSTATE2 GetJoystickData(short index); } -#endif // WINSTORE_SUPPORT +#endif // WICKEDENGINE_BUILD_DIRECTINPUT diff --git a/WickedEngine/wiGUI.cpp b/WickedEngine/wiGUI.cpp index 7070af730..709277411 100644 --- a/WickedEngine/wiGUI.cpp +++ b/WickedEngine/wiGUI.cpp @@ -36,7 +36,7 @@ void wiGUI::Update(float dt) UpdateTransform(); } - XMFLOAT4 _p = wiInput::getpointer(); + XMFLOAT4 _p = wiInput::GetPointer(); pointerpos.x = _p.x; pointerpos.y = _p.y; diff --git a/WickedEngine/wiInitializer.cpp b/WickedEngine/wiInitializer.cpp index 583b20385..9e1e6d3a0 100644 --- a/WickedEngine/wiInitializer.cpp +++ b/WickedEngine/wiInitializer.cpp @@ -52,5 +52,6 @@ namespace wiInitializer wiAudio::CleanUp(); wiNetwork::CleanUp(); wiPhysicsEngine::CleanUp(); + wiInput::CleanUp(); } } \ No newline at end of file diff --git a/WickedEngine/wiInput.cpp b/WickedEngine/wiInput.cpp index 4e1561dc8..cac58f195 100644 --- a/WickedEngine/wiInput.cpp +++ b/WickedEngine/wiInput.cpp @@ -45,8 +45,6 @@ namespace wiInput std::map inputs; std::vector touches; - wiXInput* xinput = nullptr; - struct Controller { enum DeviceType @@ -64,22 +62,8 @@ namespace wiInput { #ifdef WICKEDENGINE_BUILD_DIRECTINPUT wiDirectInput::Initialize(); - for (int i = 0; i < wiDirectInput::GetConnectedJoystickCount(); ++i) - { - controllers.push_back({ Controller::DIRECTINPUT, i }); - } #endif // WICKEDENGINE_BUILD_DIRECTINPUT - xinput = new wiXInput; - - for (int i = 0; i < MAX_CONTROLLERS; ++i) - { - if (xinput->controllers[i].bConnected) - { - controllers.push_back({ Controller::XINPUT, i }); - } - } - wiBackLog::post("wiInput Initialized"); initialized.store(true); } @@ -98,13 +82,27 @@ namespace wiInput return; } + controllers.clear(); + +#ifdef WICKEDENGINE_BUILD_XINPUT + wiXInput::Update(); + for (short i = 0; i < wiXInput::GetMaxGamepadCount(); ++i) + { + if (wiXInput::IsGamepadConnected(i)) + { + controllers.push_back({ Controller::XINPUT, i }); + } + } +#endif // WICKEDENGINE_BUILD_XINPUT + #ifdef WICKEDENGINE_BUILD_DIRECTINPUT wiDirectInput::Update(); + for (int i = 0; i < wiDirectInput::GetConnectedJoystickCount(); ++i) + { + controllers.push_back({ Controller::DIRECTINPUT, i }); + } #endif // WICKEDENGINE_BUILD_DIRECTINPUT - if(xinput != nullptr) xinput->UpdateControllerState(); - //if(rawinput != nullptr) rawinput->RetrieveBufferedData(); - for (auto iter = inputs.begin(); iter != inputs.end();) { BUTTON button = iter->first.button; @@ -112,7 +110,7 @@ namespace wiInput bool todelete = false; - if (down(button, playerIndex)) + if (Down(button, playerIndex)) { iter->second++; } @@ -137,7 +135,7 @@ namespace wiInput } - bool down(BUTTON button, short playerindex) + bool Down(BUTTON button, short playerindex) { if (!initialized.load()) { @@ -154,35 +152,38 @@ namespace wiInput { const Controller& controller = controllers[playerindex]; - if (xinput != nullptr && controller.deviceType == Controller::XINPUT) +#ifdef WICKEDENGINE_BUILD_XINPUT + if (controller.deviceType == Controller::XINPUT) { + XINPUT_STATE state = wiXInput::GetGamepadData(controller.deviceIndex); + switch (button) { - case GAMEPAD_BUTTON_UP: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_DPAD_UP); - case GAMEPAD_BUTTON_LEFT: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_DPAD_LEFT); - case GAMEPAD_BUTTON_DOWN: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_DPAD_DOWN); - case GAMEPAD_BUTTON_RIGHT: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_DPAD_RIGHT); - case GAMEPAD_BUTTON_1: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_X); - case GAMEPAD_BUTTON_2: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_A); - case GAMEPAD_BUTTON_3: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_B); - case GAMEPAD_BUTTON_4: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_Y); - case GAMEPAD_BUTTON_5: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_LEFT_SHOULDER); - case GAMEPAD_BUTTON_6: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_RIGHT_SHOULDER); - case GAMEPAD_BUTTON_7: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_LEFT_THUMB); - case GAMEPAD_BUTTON_8: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_RIGHT_THUMB); - case GAMEPAD_BUTTON_9: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_BACK); - case GAMEPAD_BUTTON_10: return xinput->isButtonDown(controller.deviceIndex, XINPUT_GAMEPAD_START); + case GAMEPAD_BUTTON_UP: return state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP; + case GAMEPAD_BUTTON_LEFT: return state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT; + case GAMEPAD_BUTTON_DOWN: return state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN; + case GAMEPAD_BUTTON_RIGHT: return state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT; + case GAMEPAD_BUTTON_1: return state.Gamepad.wButtons & XINPUT_GAMEPAD_X; + case GAMEPAD_BUTTON_2: return state.Gamepad.wButtons & XINPUT_GAMEPAD_A; + case GAMEPAD_BUTTON_3: return state.Gamepad.wButtons & XINPUT_GAMEPAD_B; + case GAMEPAD_BUTTON_4: return state.Gamepad.wButtons & XINPUT_GAMEPAD_Y; + case GAMEPAD_BUTTON_5: return state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER; + case GAMEPAD_BUTTON_6: return state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER; + case GAMEPAD_BUTTON_7: return state.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB; + case GAMEPAD_BUTTON_8: return state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB; + case GAMEPAD_BUTTON_9: return state.Gamepad.wButtons & XINPUT_GAMEPAD_BACK; + case GAMEPAD_BUTTON_10: return state.Gamepad.wButtons & XINPUT_GAMEPAD_START; default: break; } } +#endif // WICKEDENGINE_BUILD_XINPUT #ifdef WICKEDENGINE_BUILD_DIRECTINPUT if (controller.deviceType == Controller::DIRECTINPUT) { - DIJOYSTATE2 dinput_joystate; - wiDirectInput::GetJoystickData(&dinput_joystate, controller.deviceIndex); - DWORD dinput_directions = dinput_joystate.rgdwPOV[0]; + DIJOYSTATE2 state = wiDirectInput::GetJoystickData(controller.deviceIndex); + DWORD dinput_directions = state.rgdwPOV[0]; switch (button) { @@ -190,20 +191,20 @@ namespace wiInput case GAMEPAD_BUTTON_LEFT: return dinput_directions == wiDirectInput::POV_LEFT || dinput_directions == wiDirectInput::POV_LEFTUP || dinput_directions == wiDirectInput::POV_DOWNLEFT; case GAMEPAD_BUTTON_DOWN: return dinput_directions == wiDirectInput::POV_DOWN || dinput_directions == wiDirectInput::POV_DOWNLEFT || dinput_directions == wiDirectInput::POV_RIGHTDOWN; case GAMEPAD_BUTTON_RIGHT: return dinput_directions == wiDirectInput::POV_RIGHT || dinput_directions == wiDirectInput::POV_RIGHTDOWN || dinput_directions == wiDirectInput::POV_UPRIGHT; - case GAMEPAD_BUTTON_1: return dinput_joystate.rgbButtons[0] != 0; - case GAMEPAD_BUTTON_2: return dinput_joystate.rgbButtons[1] != 0; - case GAMEPAD_BUTTON_3: return dinput_joystate.rgbButtons[2] != 0; - case GAMEPAD_BUTTON_4: return dinput_joystate.rgbButtons[3] != 0; - case GAMEPAD_BUTTON_5: return dinput_joystate.rgbButtons[4] != 0; - case GAMEPAD_BUTTON_6: return dinput_joystate.rgbButtons[5] != 0; - case GAMEPAD_BUTTON_7: return dinput_joystate.rgbButtons[6] != 0; - case GAMEPAD_BUTTON_8: return dinput_joystate.rgbButtons[7] != 0; - case GAMEPAD_BUTTON_9: return dinput_joystate.rgbButtons[8] != 0; - case GAMEPAD_BUTTON_10: return dinput_joystate.rgbButtons[9] != 0; - case GAMEPAD_BUTTON_11: return dinput_joystate.rgbButtons[10] != 0; - case GAMEPAD_BUTTON_12: return dinput_joystate.rgbButtons[11] != 0; - case GAMEPAD_BUTTON_13: return dinput_joystate.rgbButtons[12] != 0; - case GAMEPAD_BUTTON_14: return dinput_joystate.rgbButtons[13] != 0; + case GAMEPAD_BUTTON_1: return state.rgbButtons[0] != 0; + case GAMEPAD_BUTTON_2: return state.rgbButtons[1] != 0; + case GAMEPAD_BUTTON_3: return state.rgbButtons[2] != 0; + case GAMEPAD_BUTTON_4: return state.rgbButtons[3] != 0; + case GAMEPAD_BUTTON_5: return state.rgbButtons[4] != 0; + case GAMEPAD_BUTTON_6: return state.rgbButtons[5] != 0; + case GAMEPAD_BUTTON_7: return state.rgbButtons[6] != 0; + case GAMEPAD_BUTTON_8: return state.rgbButtons[7] != 0; + case GAMEPAD_BUTTON_9: return state.rgbButtons[8] != 0; + case GAMEPAD_BUTTON_10: return state.rgbButtons[9] != 0; + case GAMEPAD_BUTTON_11: return state.rgbButtons[10] != 0; + case GAMEPAD_BUTTON_12: return state.rgbButtons[11] != 0; + case GAMEPAD_BUTTON_13: return state.rgbButtons[12] != 0; + case GAMEPAD_BUTTON_14: return state.rgbButtons[13] != 0; default: break; } @@ -309,9 +310,9 @@ namespace wiInput return false; } - bool press(BUTTON button, short playerindex) + bool Press(BUTTON button, short playerindex) { - if (!down(button, playerindex)) + if (!Down(button, playerindex)) return false; Input input; @@ -329,9 +330,9 @@ namespace wiInput } return false; } - bool hold(BUTTON button, uint32_t frames, bool continuous, short playerIndex) + bool Hold(BUTTON button, uint32_t frames, bool continuous, short playerIndex) { - if (!down(button, playerIndex)) + if (!Down(button, playerIndex)) return false; Input input; @@ -349,7 +350,7 @@ namespace wiInput } return false; } - XMFLOAT4 getpointer() + XMFLOAT4 GetPointer() { #ifndef WINSTORE_SUPPORT POINT p; @@ -361,7 +362,7 @@ namespace wiInput return XMFLOAT4(p.X, p.Y, mousewheel_scrolled, 0); #endif } - void setpointer(const XMFLOAT4& props) + void SetPointer(const XMFLOAT4& props) { #ifndef WINSTORE_SUPPORT POINT p; @@ -373,7 +374,7 @@ namespace wiInput mousewheel_scrolled = props.z; } - void hidepointer(bool value) + void HidePointer(bool value) { #ifndef WINSTORE_SUPPORT if (value) @@ -397,39 +398,40 @@ namespace wiInput x = 1.0f; return x; } - XMFLOAT4 getanalog(GAMEPAD_ANALOG analog, short playerindex) + XMFLOAT4 GetAnalog(GAMEPAD_ANALOG analog, short playerindex) { if (playerindex < (int)controllers.size()) { const Controller& controller = controllers[playerindex]; - if (xinput != nullptr && controller.deviceType == Controller::XINPUT) +#ifdef WICKEDENGINE_BUILD_XINPUT + if (controller.deviceType == Controller::XINPUT) { - const auto& state = xinput->controllers[controller.deviceIndex].state.Gamepad; + XINPUT_STATE state = wiXInput::GetGamepadData(controller.deviceIndex); switch (analog) { - case GAMEPAD_ANALOG_THUMBSTICK_L: return XMFLOAT4(deadzone((float)state.sThumbLX / 32767.0f), deadzone((float)state.sThumbLY / 32767.0f), 0, 0); - case GAMEPAD_ANALOG_THUMBSTICK_R: return XMFLOAT4(deadzone((float)state.sThumbRX / 32767.0f), deadzone((float)state.sThumbRY / 32767.0f), 0, 0); - case GAMEPAD_ANALOG_TRIGGER_L: return XMFLOAT4((float)state.bLeftTrigger / 255.0f, 0, 0, 0); - case GAMEPAD_ANALOG_TRIGGER_R: return XMFLOAT4((float)state.bRightTrigger / 255.0f, 0, 0, 0); + case GAMEPAD_ANALOG_THUMBSTICK_L: return XMFLOAT4(deadzone((float)state.Gamepad.sThumbLX / 32767.0f), deadzone((float)state.Gamepad.sThumbLY / 32767.0f), 0, 0); + case GAMEPAD_ANALOG_THUMBSTICK_R: return XMFLOAT4(deadzone((float)state.Gamepad.sThumbRX / 32767.0f), -deadzone((float)state.Gamepad.sThumbRY / 32767.0f), 0, 0); + case GAMEPAD_ANALOG_TRIGGER_L: return XMFLOAT4((float)state.Gamepad.bLeftTrigger / 255.0f, 0, 0, 0); + case GAMEPAD_ANALOG_TRIGGER_R: return XMFLOAT4((float)state.Gamepad.bRightTrigger / 255.0f, 0, 0, 0); default: break; } } +#endif // WICKEDENGINE_BUILD_XINPUT #ifdef WICKEDENGINE_BUILD_DIRECTINPUT if (controller.deviceType == Controller::DIRECTINPUT) { - DIJOYSTATE2 dinput_joystate; - wiDirectInput::GetJoystickData(&dinput_joystate, controller.deviceIndex); + DIJOYSTATE2 state = wiDirectInput::GetJoystickData(controller.deviceIndex); switch (analog) { - case GAMEPAD_ANALOG_THUMBSTICK_L: return XMFLOAT4(deadzone((float)dinput_joystate.lX / 1000.0f), deadzone(-(float)dinput_joystate.lY / 1000.0f), 0, 0); - case GAMEPAD_ANALOG_THUMBSTICK_R: return XMFLOAT4(deadzone((float)dinput_joystate.lZ / 1000.0f), deadzone((float)dinput_joystate.lRz / 1000.0f), 0, 0); - case GAMEPAD_ANALOG_TRIGGER_L: return XMFLOAT4((float)dinput_joystate.lRx / 1000.0f * 0.5f + 0.5f, 0, 0, 0); - case GAMEPAD_ANALOG_TRIGGER_R: return XMFLOAT4((float)dinput_joystate.lRy / 1000.0f * 0.5f + 0.5f, 0, 0, 0); + case GAMEPAD_ANALOG_THUMBSTICK_L: return XMFLOAT4(deadzone((float)state.lX / 1000.0f), deadzone(-(float)state.lY / 1000.0f), 0, 0); + case GAMEPAD_ANALOG_THUMBSTICK_R: return XMFLOAT4(deadzone((float)state.lZ / 1000.0f), deadzone((float)state.lRz / 1000.0f), 0, 0); + case GAMEPAD_ANALOG_TRIGGER_L: return XMFLOAT4((float)state.lRx / 1000.0f * 0.5f + 0.5f, 0, 0, 0); + case GAMEPAD_ANALOG_TRIGGER_R: return XMFLOAT4((float)state.lRy / 1000.0f * 0.5f + 0.5f, 0, 0, 0); default: break; } @@ -485,7 +487,7 @@ namespace wiInput } #endif // WINSTORE_SUPPORT - const std::vector& getTouches() + const std::vector& GetTouches() { static bool isRegisteredTouch = false; if (!isRegisteredTouch) diff --git a/WickedEngine/wiInput.h b/WickedEngine/wiInput.h index 570afd694..12439f67c 100644 --- a/WickedEngine/wiInput.h +++ b/WickedEngine/wiInput.h @@ -81,19 +81,19 @@ namespace wiInput void Update(); // check if a button is down - bool down(BUTTON button, short playerindex = 0); + bool Down(BUTTON button, short playerindex = 0); // check if a button is pressed once - bool press(BUTTON button, short playerindex = 0); + bool Press(BUTTON button, short playerindex = 0); // check if a button is held down - bool hold(BUTTON button, uint32_t frames = 30, bool continuous = false, short playerIndex = 0); + bool Hold(BUTTON button, uint32_t frames = 30, bool continuous = false, short playerIndex = 0); // get pointer position (eg. mouse pointer) (.xy) + scroll delta (.z) + 1 unused (.w) - XMFLOAT4 getpointer(); + XMFLOAT4 GetPointer(); // set pointer position (eg. mouse pointer) + scroll delta (.z) - void setpointer(const XMFLOAT4& props); + void SetPointer(const XMFLOAT4& props); // hide pointer - void hidepointer(bool value); + void HidePointer(bool value); // read analog input from controllers, like thumbsticks or triggers - XMFLOAT4 getanalog(GAMEPAD_ANALOG analog, short playerIndex = 0); + XMFLOAT4 GetAnalog(GAMEPAD_ANALOG analog, short playerIndex = 0); struct Touch { @@ -107,7 +107,7 @@ namespace wiInput // current position of touch XMFLOAT2 pos; }; - const std::vector& getTouches(); + const std::vector& GetTouches(); }; diff --git a/WickedEngine/wiInput_BindLua.cpp b/WickedEngine/wiInput_BindLua.cpp index 5a60105ef..8d01a1040 100644 --- a/WickedEngine/wiInput_BindLua.cpp +++ b/WickedEngine/wiInput_BindLua.cpp @@ -29,7 +29,7 @@ int wiInput_BindLua::Down(lua_State* L) { playerindex = (short)wiLua::SGetInt(L, 2); } - wiLua::SSetBool(L, wiInput::down(button, playerindex)); + wiLua::SSetBool(L, wiInput::Down(button, playerindex)); return 1; } else @@ -47,7 +47,7 @@ int wiInput_BindLua::Press(lua_State* L) { playerindex = (short)wiLua::SGetInt(L, 2); } - wiLua::SSetBool(L, wiInput::press(code, playerindex)); + wiLua::SSetBool(L, wiInput::Press(code, playerindex)); return 1; } else @@ -75,7 +75,7 @@ int wiInput_BindLua::Hold(lua_State* L) { playerindex = (short)wiLua::SGetInt(L, 4); } - wiLua::SSetBool(L, wiInput::hold(button, duration, continuous, playerindex)); + wiLua::SSetBool(L, wiInput::Hold(button, duration, continuous, playerindex)); return 1; } else @@ -84,7 +84,7 @@ int wiInput_BindLua::Hold(lua_State* L) } int wiInput_BindLua::GetPointer(lua_State* L) { - Luna::push(L, new Vector_BindLua(XMLoadFloat4(&wiInput::getpointer()))); + Luna::push(L, new Vector_BindLua(XMLoadFloat4(&wiInput::GetPointer()))); return 1; } int wiInput_BindLua::SetPointer(lua_State* L) @@ -97,7 +97,7 @@ int wiInput_BindLua::SetPointer(lua_State* L) { XMFLOAT4 props; XMStoreFloat4(&props, vec->vector); - wiInput::setpointer(props); + wiInput::SetPointer(props); } else wiLua::SError(L, "SetPointer(Vector props) argument is not a Vector!"); @@ -111,7 +111,7 @@ int wiInput_BindLua::HidePointer(lua_State* L) int argc = wiLua::SGetArgCount(L); if (argc > 0) { - wiInput::hidepointer(wiLua::SGetBool(L, 1)); + wiInput::HidePointer(wiLua::SGetBool(L, 1)); } else wiLua::SError(L, "HidePointer(bool value) not enough arguments!"); @@ -130,7 +130,7 @@ int wiInput_BindLua::GetAnalog(lua_State* L) { playerindex = (short)wiLua::SGetInt(L, 2); } - result = wiInput::getanalog(type, playerindex); + result = wiInput::GetAnalog(type, playerindex); } else wiLua::SError(L, "GetAnalog(int type, opt int playerindex = 0) not enough arguments!"); @@ -140,7 +140,7 @@ int wiInput_BindLua::GetAnalog(lua_State* L) } int wiInput_BindLua::GetTouches(lua_State* L) { - auto& touches = wiInput::getTouches(); + auto& touches = wiInput::GetTouches(); for (auto& touch : touches) { Luna::push(L, new Touch_BindLua(touch)); diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 227b6c302..96a7a7877 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 35; // minor bug fixes, alterations, refactors, updates - const int revision = 3; + const int revision = 4; long GetVersion() diff --git a/WickedEngine/wiWidget.cpp b/WickedEngine/wiWidget.cpp index 7f5909f59..c5867fbc2 100644 --- a/WickedEngine/wiWidget.cpp +++ b/WickedEngine/wiWidget.cpp @@ -314,7 +314,7 @@ void wiButton::Update(wiGUI* gui, float dt) } } - if (wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { if (state == FOCUS) { @@ -323,7 +323,7 @@ void wiButton::Update(wiGUI* gui, float dt) } } - if (wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { if (state == DEACTIVATING) { @@ -519,7 +519,7 @@ void wiTextInputField::Update(wiGUI* gui, float dt) } } - if (wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { if (state == FOCUS) { @@ -528,7 +528,7 @@ void wiTextInputField::Update(wiGUI* gui, float dt) } } - if (wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { if (state == DEACTIVATING) { @@ -546,7 +546,7 @@ void wiTextInputField::Update(wiGUI* gui, float dt) if (state == ACTIVE) { - if (wiInput::press(wiInput::KEYBOARD_BUTTON_ENTER)) + if (wiInput::Press(wiInput::KEYBOARD_BUTTON_ENTER)) { // accept input... @@ -561,8 +561,8 @@ void wiTextInputField::Update(wiGUI* gui, float dt) gui->DeactivateWidget(this); } - else if ((wiInput::press(wiInput::MOUSE_BUTTON_LEFT) && !intersectsPointer) || - wiInput::press(wiInput::KEYBOARD_BUTTON_ESCAPE)) + else if ((wiInput::Press(wiInput::MOUSE_BUTTON_LEFT) && !intersectsPointer) || + wiInput::Press(wiInput::KEYBOARD_BUTTON_ESCAPE)) { // cancel input value_new.clear(); @@ -697,7 +697,7 @@ void wiSlider::Update(wiGUI* gui, float dt) } if (state == ACTIVE) { - if (wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { if (state == ACTIVE) { @@ -730,7 +730,7 @@ void wiSlider::Update(wiGUI* gui, float dt) } } - if (wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { if (state == FOCUS) { @@ -863,7 +863,7 @@ void wiCheckBox::Update(wiGUI* gui, float dt) } } - if (wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { if (state == FOCUS) { @@ -872,7 +872,7 @@ void wiCheckBox::Update(wiGUI* gui, float dt) } } - if (wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { if (state == DEACTIVATING) { @@ -1011,14 +1011,14 @@ void wiComboBox::Update(wiGUI* gui, float dt) } } - if (wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { // activate clicked = true; } bool click_down = false; - if (wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { click_down = true; if (state == DEACTIVATING) @@ -1076,7 +1076,7 @@ void wiComboBox::Update(wiGUI* gui, float dt) } else if (combostate == COMBOSTATE_HOVER) { - int scroll = (int)wiInput::getpointer().z; + int scroll = (int)wiInput::GetPointer().z; firstItemVisible -= scroll; firstItemVisible = std::max(0, std::min((int)items.size() - maxVisibleItemCount, firstItemVisible)); if (scroll) @@ -1723,7 +1723,7 @@ void wiColorPicker::Update(wiGUI* gui, float dt) bool dragged = false; - if (wiInput::press(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { if (state == FOCUS) { @@ -1732,7 +1732,7 @@ void wiColorPicker::Update(wiGUI* gui, float dt) } } - if (wiInput::down(wiInput::MOUSE_BUTTON_LEFT)) + if (wiInput::Down(wiInput::MOUSE_BUTTON_LEFT)) { if (state == ACTIVE) { diff --git a/WickedEngine/wiXInput.cpp b/WickedEngine/wiXInput.cpp index f0d4e8b76..a1c062a0a 100644 --- a/WickedEngine/wiXInput.cpp +++ b/WickedEngine/wiXInput.cpp @@ -1,62 +1,52 @@ -#include #include "wiXInput.h" +#ifdef WICKEDENGINE_BUILD_XINPUT #pragma comment(lib,"xinput.lib") -namespace wiInput +namespace wiXInput { + XINPUT_STATE controllers[4] = {}; + bool connected[arraysize(controllers)] = {}; - wiXInput::wiXInput() + void Update() { - g_bDeadZoneOn = true; - for (int i = 0; i < MAX_CONTROLLERS; ++i) { - controllers[i] = CONTROLLER_STATE(); - controllers[i].bConnected = false; - } - } - wiXInput::~wiXInput() - { - CleanUp(); - } - - HRESULT wiXInput::UpdateControllerState() - { - DWORD dwResult = 0; - for (DWORD i = 0; i < MAX_CONTROLLERS; i++) + for (DWORD i = 0; i < arraysize(controllers); i++) { - // Simply get the state of the controller from XInput. - dwResult = XInputGetState(i, &controllers[i].state); + controllers[i] = {}; + DWORD dwResult = XInputGetState(i, &controllers[i]); if (dwResult == ERROR_SUCCESS) - controllers[i].bConnected = true; + { + connected[i] = true; + } else - controllers[i].bConnected = false; + { + connected[i] = false; + } } - - - return S_OK; } - DWORD wiXInput::GetButtons(SHORT newPlayerIndex) + short GetMaxGamepadCount() { - if (controllers[newPlayerIndex].bConnected) - return controllers[newPlayerIndex].state.Gamepad.wButtons; - return 0; + return arraysize(controllers); } - DWORD wiXInput::GetDirections(short newPlayerIndex) { - if (controllers[newPlayerIndex].bConnected) - return controllers[newPlayerIndex].state.Gamepad.wButtons; - return 0; - } - bool wiXInput::isButtonDown(short newPlayerIndex, DWORD checkforButton) { - if (controllers[newPlayerIndex].bConnected) - return (controllers[newPlayerIndex].state.Gamepad.wButtons & checkforButton) != 0; + bool IsGamepadConnected(short index) + { + if (index < arraysize(connected)) + { + return connected[index]; + } return false; } - - void wiXInput::CleanUp() + XINPUT_STATE GetGamepadData(short index) { + if (index < arraysize(controllers)) + { + return controllers[index]; + } + return {}; } - } + +#endif // WICKEDENGINE_BUILD_XINPUT diff --git a/WickedEngine/wiXInput.h b/WickedEngine/wiXInput.h index ae16d9138..a7f5e345d 100644 --- a/WickedEngine/wiXInput.h +++ b/WickedEngine/wiXInput.h @@ -1,37 +1,28 @@ #pragma once #include "CommonInclude.h" -#include -// TODO REFACTOR +#if __has_include("xinput.h") +#define WICKEDENGINE_BUILD_XINPUT +#endif -namespace wiInput +#ifdef WICKEDENGINE_BUILD_XINPUT + +#include +#include + +namespace wiXInput { + // Call once per frame to read and update controller states + void Update(); -#define MAX_CONTROLLERS 4 // XInput handles up to 4 controllers -#define INPUT_DEADZONE ( 0.24f * FLOAT(0x7FFF) ) // Default to 24% of the +/- 32767 range. This is a reasonable default value but can be altered if needed. + // Returns how many gamepads can Xinput handle + short GetMaxGamepadCount(); - class wiXInput - { - private: - - struct CONTROLLER_STATE - { - XINPUT_STATE state; - bool bConnected; - }; - - public: - wiXInput(); - ~wiXInput(); - HRESULT UpdateControllerState(); - DWORD GetButtons(SHORT); - DWORD GetDirections(short); - bool isButtonDown(short, DWORD); - void CleanUp(); - - CONTROLLER_STATE controllers[MAX_CONTROLLERS]; - WCHAR g_szMessage[4][1024]; - bool g_bDeadZoneOn; - }; + // Returns if the specified gamepad is connected or not + bool IsGamepadConnected(short index); + // Returns the specified gamepad's state + XINPUT_STATE GetGamepadData(short index); } + +#endif // WICKEDENGINE_BUILD_XINPUT