diff --git a/Documentation/ScriptingAPI-Documentation.md b/Documentation/ScriptingAPI-Documentation.md index 55f72c75e..cb9c7d58c 100644 --- a/Documentation/ScriptingAPI-Documentation.md +++ b/Documentation/ScriptingAPI-Documentation.md @@ -623,9 +623,9 @@ These provide functions to check the state of the input devices. Query input devices - [outer]input : InputManager - [void-constructor]InputManager() -- Down(int code, opt int type = KEYBOARD) : bool result -- Check whether a button is currently being held down -- Press(int code, opt int type = KEYBOARD) : bool result -- Check whether a button has just been pushed that wasn't before -- Hold(int code, opt int duration = 30, opt boolean continuous = false, opt int type = KEYBOARD) : bool result -- Check whether a button was being held down for a specific duration (nunmber of frames). If continuous == true, than it will also return true after the duration was reached +- Down(int code, opt int type = INPUT_TYPE_KEYBOARD, opt int playerindex = 0) : bool result -- Check whether a button is currently being held down +- Press(int code, opt int type = INPUT_TYPE_KEYBOARD, opt int playerindex = 0) : bool result -- Check whether a button has just been pushed that wasn't before +- Hold(int code, opt int duration = 30, opt boolean continuous = false, opt int type = INPUT_TYPE_KEYBOARD, opt int playerindex = 0) : bool result -- Check whether a button was being held down for a specific duration (nunmber of frames). If continuous == true, than it will also return true after the duration was reached - GetPointer() : Vector result - SetPointer(Vector pos) - HidePointer(bool visible) @@ -643,9 +643,8 @@ Describes a touch contact point - [outer]TOUCHSTATE_MOVED : int #### Input types -- [outer]DIRECTINPUT_JOYPAD : int -- [outer]XINPUT_JOYPAD : int -- [outer]KEYBOARD : int +- [outer]INPUT_TYPE_KEYBOARD : int -- keyboard or mouse +- [outer]INPUT_TYPE_GAMEPAD : int -- xinput or directinput gamepad #### Keyboard Key codes - [outer]VK_UP : int @@ -676,6 +675,18 @@ Describes a touch contact point - [outer]VK_MBUTTON : int - [outer]VK_RBUTTON : int +#### Gamepad Key Codes +- [outer]GAMEPAD_BUTTON_UP : int +- [outer]GAMEPAD_BUTTON_LEFT : int +- [outer]GAMEPAD_BUTTON_DOWN : int +- [outer]GAMEPAD_BUTTON_RIGHT : int +- [outer]GAMEPAD_BUTTON_1 : int +- [outer]GAMEPAD_BUTTON_2 : int +- [outer]GAMEPAD_BUTTON_3 : int +- [outer]GAMEPAD_BUTTON_4 : int +... +- [outer]GAMEPAD_14 : int + ### ResourceManager Stores and manages resources such as textures, sounds and shaders. - [outer]globalResources : Resource diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index eff75c5fd..c1200dff0 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -48,8 +48,6 @@ void Editor::Initialize() wiRenderer::GetDevice()->SetVSyncEnabled(true); wiRenderer::SetOcclusionCullingEnabled(true); - wiInputManager::addXInput(new wiXInput()); - renderComponent = new EditorComponent; renderComponent->Initialize(); loader = new EditorLoadingScreen; @@ -793,12 +791,12 @@ void EditorComponent::Update(float dt) if (!wiInputManager::down(VK_CONTROL)) { // Only move camera if control not pressed - if (wiInputManager::down('A')) { moveNew += XMVectorSet(-1, 0, 0, 0); } - if (wiInputManager::down('D')) { moveNew += XMVectorSet(1, 0, 0, 0); } - if (wiInputManager::down('W')) { moveNew += XMVectorSet(0, 0, 1, 0); } - if (wiInputManager::down('S')) { moveNew += XMVectorSet(0, 0, -1, 0); } - if (wiInputManager::down('E')) { moveNew += XMVectorSet(0, 1, 0, 0); } - if (wiInputManager::down('Q')) { moveNew += XMVectorSet(0, -1, 0, 0); } + if (wiInputManager::down('A') || wiInputManager::down(GAMEPAD_BUTTON_LEFT, INPUT_TYPE_GAMEPAD)) { moveNew += XMVectorSet(-1, 0, 0, 0); } + if (wiInputManager::down('D') || wiInputManager::down(GAMEPAD_BUTTON_RIGHT, INPUT_TYPE_GAMEPAD)) { moveNew += XMVectorSet(1, 0, 0, 0); } + if (wiInputManager::down('W') || wiInputManager::down(GAMEPAD_BUTTON_UP, INPUT_TYPE_GAMEPAD)) { moveNew += XMVectorSet(0, 0, 1, 0); } + if (wiInputManager::down('S') || wiInputManager::down(GAMEPAD_BUTTON_DOWN, INPUT_TYPE_GAMEPAD)) { moveNew += XMVectorSet(0, 0, -1, 0); } + if (wiInputManager::down('E') || wiInputManager::down(GAMEPAD_BUTTON_2, INPUT_TYPE_GAMEPAD)) { moveNew += XMVectorSet(0, 1, 0, 0); } + if (wiInputManager::down('Q') || wiInputManager::down(GAMEPAD_BUTTON_1, INPUT_TYPE_GAMEPAD)) { moveNew += XMVectorSet(0, -1, 0, 0); } moveNew = XMVector3Normalize(moveNew) * speed; } diff --git a/Editor/main.cpp b/Editor/main.cpp index 6fd47efa7..e6756db58 100644 --- a/Editor/main.cpp +++ b/Editor/main.cpp @@ -159,9 +159,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) return FALSE; } - if (!editor.SetWindow(hWnd, hInst)) - return false; - + editor.SetWindow(hWnd, hInst); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); diff --git a/Tests/main.cpp b/Tests/main.cpp index 4436a0825..75ebf3805 100644 --- a/Tests/main.cpp +++ b/Tests/main.cpp @@ -115,10 +115,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) return FALSE; } - if (!tests.SetWindow(hWnd, hInst)) - { - return FALSE; - } + tests.SetWindow(hWnd, hInst); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); diff --git a/WickedEngine/MainComponent.cpp b/WickedEngine/MainComponent.cpp index d1f09766b..e409d1697 100644 --- a/WickedEngine/MainComponent.cpp +++ b/WickedEngine/MainComponent.cpp @@ -291,20 +291,15 @@ void MainComponent::Compose() } #ifndef WINSTORE_SUPPORT -bool MainComponent::SetWindow(wiWindowRegistration::window_type window, HINSTANCE hInst) +void MainComponent::SetWindow(wiWindowRegistration::window_type window, HINSTANCE hInst) { - this->hInst = hInst; - + wiWindowRegistration::RegisterInstance(hInst); wiWindowRegistration::RegisterWindow(window); - - return true; } #else -bool MainComponent::SetWindow(wiWindowRegistration::window_type window) +void MainComponent::SetWindow(wiWindowRegistration::window_type window) { wiWindowRegistration::RegisterWindow(window); - - return true; } #endif diff --git a/WickedEngine/MainComponent.h b/WickedEngine/MainComponent.h index d1aae436e..3e18db229 100644 --- a/WickedEngine/MainComponent.h +++ b/WickedEngine/MainComponent.h @@ -62,12 +62,11 @@ public: virtual void Compose(); #ifndef WINSTORE_SUPPORT - HINSTANCE hInst = NULL; // You need to call this before calling Run() or Initialize() if you want to render to a Win32 window handle - bool SetWindow(wiWindowRegistration::window_type window, HINSTANCE hInst = NULL); + void SetWindow(wiWindowRegistration::window_type window, HINSTANCE hInst = NULL); #else // You need to call this before calling Run() or Initialize() if you want to render to a UWP window - bool SetWindow(wiWindowRegistration::window_type window); + void SetWindow(wiWindowRegistration::window_type window); #endif diff --git a/WickedEngine/wiDirectInput.cpp b/WickedEngine/wiDirectInput.cpp index ab09ce66b..f2c0d46d2 100644 --- a/WickedEngine/wiDirectInput.cpp +++ b/WickedEngine/wiDirectInput.cpp @@ -1,6 +1,8 @@ #include "wiDirectInput.h" #include "CommonInclude.h" +short wiDirectInput::connectedJoys = 0; + #ifndef WINSTORE_SUPPORT #include @@ -136,7 +138,6 @@ wiDirectInput::~wiDirectInput() IDirectInputDevice8* m_keyboard = 0; IDirectInputDevice8* joystick[2] = {0,0}; IDirectInput8* m_directInput = 0; -short wiDirectInput::connectedJoys = 0; BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context) { @@ -404,7 +405,7 @@ DWORD wiDirectInput::getDirections(short pIndex) if(connectedJoys) for(short i=0;i<4;i++){ //if((LOWORD(joyState[pIndex].rgdwPOV[i]) != 0xFFFF)) - if(buttons+=joyState[pIndex].rgdwPOV[i]!=POV_IDLE) + if(buttons+=joyState[pIndex].rgdwPOV[i] != DIRECTINPUT_POV_IDLE) buttons+=joyState[pIndex].rgdwPOV[i]; } return buttons; diff --git a/WickedEngine/wiDirectInput.h b/WickedEngine/wiDirectInput.h index 2903fb769..493b94753 100644 --- a/WickedEngine/wiDirectInput.h +++ b/WickedEngine/wiDirectInput.h @@ -1,14 +1,16 @@ #pragma once -#define POV_IDLE 4294967292 -#define POV_UP 1 -#define POV_UPRIGHT 4501 -#define POV_RIGHT 9001 -#define POV_RIGHTDOWN 13501 -#define POV_DOWN 18001 -#define POV_DOWNLEFT 22501 -#define POV_LEFT 27001 -#define POV_LEFTUP 31501 +// TODO REFACTOR + +#define DIRECTINPUT_POV_IDLE 4294967292 +#define DIRECTINPUT_POV_UP 1 +#define DIRECTINPUT_POV_UPRIGHT 4501 +#define DIRECTINPUT_POV_RIGHT 9001 +#define DIRECTINPUT_POV_RIGHTDOWN 13501 +#define DIRECTINPUT_POV_DOWN 18001 +#define DIRECTINPUT_POV_DOWNLEFT 22501 +#define DIRECTINPUT_POV_LEFT 27001 +#define DIRECTINPUT_POV_LEFTUP 31501 #ifndef WINSTORE_SUPPORT #define DIRECTINPUT_VERSION 0x0800 @@ -21,6 +23,9 @@ class wiDirectInput { +public: + static short connectedJoys; + #ifndef WINSTORE_SUPPORT friend BOOL CALLBACK enumCallback(const DIDEVICEINSTANCE* instance, VOID* context); friend BOOL CALLBACK enumAxesCallback(const DIDEVICEOBJECTINSTANCE* instance, VOID* context); @@ -40,7 +45,6 @@ public: DWORD getDirections(short pIndex); DIJOYSTATE2 joyState[2]; - static short connectedJoys; static HRESULT InitJoy(HWND hwnd); @@ -53,7 +57,7 @@ private: #else public: - wiDirectInput(HINSTANCE hinst, HWND hwnd){} + wiDirectInput(...){} void Shutdown(){} bool Frame(){ return false; } bool IsKeyDown(INT){ return false; } @@ -61,6 +65,7 @@ public: bool isButtonDown(short pIndex, unsigned int buttoncode){ return false; } DWORD getDirections(short pIndex){ return 0; } static HRESULT InitJoy(HWND hwnd){ return E_FAIL; } + int GetNumControllers() { return 0; } #endif }; diff --git a/WickedEngine/wiInitializer.cpp b/WickedEngine/wiInitializer.cpp index 1efe11fa1..14ac99783 100644 --- a/WickedEngine/wiInitializer.cpp +++ b/WickedEngine/wiInitializer.cpp @@ -25,6 +25,7 @@ namespace wiInitializer wiJobSystem::Execute([] { wiFont::Initialize(); }); wiJobSystem::Execute([] { wiImage::Initialize(); }); + wiJobSystem::Execute([] { wiInputManager::Initialize(); }); wiJobSystem::Execute([] { wiRenderer::Initialize(); wiWidget::LoadShaders(); }); wiJobSystem::Execute([] { wiSoundEffect::Initialize(); wiMusic::Initialize(); }); wiJobSystem::Execute([] { wiTextureHelper::Initialize(); }); diff --git a/WickedEngine/wiInputManager.cpp b/WickedEngine/wiInputManager.cpp index aecb04052..c8e88be42 100644 --- a/WickedEngine/wiInputManager.cpp +++ b/WickedEngine/wiInputManager.cpp @@ -4,8 +4,11 @@ #include "wiRawInput.h" #include "wiWindowRegistration.h" #include "wiHelper.h" +#include "wiBackLog.h" #include +#include +#include using namespace std; @@ -22,34 +25,83 @@ namespace wiInputManager #define KEY_UP(vk_code) (!KEY_DOWN(vk_code)) static float mousewheel_scrolled = 0.0f; - std::map inputs; + + struct Input + { + INPUT_TYPE type; + uint32_t button; + short playerIndex; + + Input() { + type = INPUT_TYPE_KEYBOARD; + button = 0; + playerIndex = 0; + } + bool operator<(const Input other) { + return (button != other.button || type != other.type || playerIndex != other.playerIndex); + } + struct LessComparer { + bool operator()(Input const& a, Input const& b) const { + return (a.button < b.button || a.type < b.type || a.playerIndex < b.playerIndex); + } + }; + }; + std::map inputs; std::vector touches; wiXInput* xinput = nullptr; wiDirectInput* dinput = nullptr; wiRawInput* rawinput = nullptr; + struct Controller + { + enum DeviceType + { + XINPUT, + DIRECTINPUT, + }; + DeviceType deviceType; + short deviceIndex; + }; + std::vector controllers; + std::atomic_bool initialized = false; - void addXInput(wiXInput* input) { xinput = input; } - void addDirectInput(wiDirectInput* input) { dinput = input; } - void addRawInput(wiRawInput* input) { rawinput = input; } + void Initialize() + { + xinput = new wiXInput; + dinput = new wiDirectInput(wiWindowRegistration::GetRegisteredInstance(), wiWindowRegistration::GetRegisteredWindow()); + //rawinput = new wiRawInput; + + for (short i = 0; i < MAX_CONTROLLERS; ++i) + { + if (xinput->controllers[i].bConnected) + { + controllers.push_back({ Controller::XINPUT, i }); + } + } + for (short i = 0; i < wiDirectInput::connectedJoys; ++i) + { + controllers.push_back({ Controller::DIRECTINPUT, i }); + } + + wiBackLog::post("wiInputManager Initialized"); + initialized.store(true); + } void Update() { + if (!initialized.load()) + { + return; + } - if (dinput) { - dinput->Frame(); - } - if (xinput) { - xinput->UpdateControllerState(); - } - if (rawinput) { - //rawinput->RetrieveBufferedData(); - } + if(dinput != nullptr) dinput->Frame(); + if(xinput != nullptr) xinput->UpdateControllerState(); + if(rawinput != nullptr) rawinput->RetrieveBufferedData(); for (auto iter = inputs.begin(); iter != inputs.end();) { - InputType type = iter->first.type; - DWORD button = iter->first.button; + INPUT_TYPE type = iter->first.type; + uint32_t button = iter->first.button; short playerIndex = iter->first.playerIndex; bool todelete = false; @@ -79,33 +131,89 @@ namespace wiInputManager } - bool down(DWORD button, InputType inputType, short playerindex) + bool down(uint32_t button, INPUT_TYPE inputType, short playerindex) { + if (!initialized.load()) + { + return false; + } if (!wiWindowRegistration::IsWindowActive()) { return false; } - switch (inputType) { - case KEYBOARD: - return KEY_DOWN(static_cast(button)) | KEY_TOGGLE(static_cast(button)); + case INPUT_TYPE_KEYBOARD: + if (playerindex == 0) // can't differentiate between keyboards now.. + { + return KEY_DOWN(static_cast(button)) | KEY_TOGGLE(static_cast(button)); + } break; - case XINPUT_JOYPAD: - if (xinput != nullptr && xinput->isButtonDown(playerindex, button)) - return true; - break; - case DIRECTINPUT_JOYPAD: - if (dinput != nullptr && (dinput->isButtonDown(playerindex, button) || dinput->getDirections(playerindex) == button)) { - return true; + case INPUT_TYPE_GAMEPAD: + if (playerindex < controllers.size()) + { + + const Controller& controller = controllers[playerindex]; + + if (xinput != nullptr && controller.deviceType == Controller::XINPUT) + { + 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); + default: + break; + } + } + + if (dinput != nullptr && controller.deviceType == Controller::DIRECTINPUT) + { + DWORD dinput_directions = dinput->getDirections(controller.deviceIndex); + switch (button) + { + case GAMEPAD_BUTTON_UP: return dinput_directions == DIRECTINPUT_POV_UP || dinput_directions == DIRECTINPUT_POV_LEFTUP || dinput_directions == DIRECTINPUT_POV_UPRIGHT; + case GAMEPAD_BUTTON_LEFT: return dinput_directions == DIRECTINPUT_POV_LEFT || dinput_directions == DIRECTINPUT_POV_LEFTUP || dinput_directions == DIRECTINPUT_POV_DOWNLEFT; + case GAMEPAD_BUTTON_DOWN: return dinput_directions == DIRECTINPUT_POV_DOWN || dinput_directions == DIRECTINPUT_POV_DOWNLEFT || dinput_directions == DIRECTINPUT_POV_RIGHTDOWN; + case GAMEPAD_BUTTON_RIGHT: return dinput_directions == DIRECTINPUT_POV_RIGHT || dinput_directions == DIRECTINPUT_POV_RIGHTDOWN || dinput_directions == DIRECTINPUT_POV_UPRIGHT; + case GAMEPAD_BUTTON_1: return dinput->isButtonDown(controller.deviceIndex, 1); + case GAMEPAD_BUTTON_2: return dinput->isButtonDown(controller.deviceIndex, 2); + case GAMEPAD_BUTTON_3: return dinput->isButtonDown(controller.deviceIndex, 3); + case GAMEPAD_BUTTON_4: return dinput->isButtonDown(controller.deviceIndex, 4); + case GAMEPAD_BUTTON_5: return dinput->isButtonDown(controller.deviceIndex, 5); + case GAMEPAD_BUTTON_6: return dinput->isButtonDown(controller.deviceIndex, 6); + case GAMEPAD_BUTTON_7: return dinput->isButtonDown(controller.deviceIndex, 7); + case GAMEPAD_BUTTON_8: return dinput->isButtonDown(controller.deviceIndex, 8); + case GAMEPAD_BUTTON_9: return dinput->isButtonDown(controller.deviceIndex, 9); + case GAMEPAD_BUTTON_10: return dinput->isButtonDown(controller.deviceIndex, 10); + case GAMEPAD_BUTTON_11: return dinput->isButtonDown(controller.deviceIndex, 11); + case GAMEPAD_BUTTON_12: return dinput->isButtonDown(controller.deviceIndex, 12); + case GAMEPAD_BUTTON_13: return dinput->isButtonDown(controller.deviceIndex, 13); + case GAMEPAD_BUTTON_14: return dinput->isButtonDown(controller.deviceIndex, 14); + default: + break; + } + } + } break; default:break; } return false; } - bool press(DWORD button, InputType inputType, short playerindex) + bool press(uint32_t button, INPUT_TYPE inputType, short playerindex) { if (!down(button, inputType, playerindex)) return false; @@ -126,9 +234,8 @@ namespace wiInputManager } return false; } - bool hold(DWORD button, DWORD frames, bool continuous, InputType inputType, short playerIndex) + bool hold(uint32_t button, uint32_t frames, bool continuous, INPUT_TYPE inputType, short playerIndex) { - if (!down(button, inputType, playerIndex)) return false; diff --git a/WickedEngine/wiInputManager.h b/WickedEngine/wiInputManager.h index 635ef49b8..177aaa075 100644 --- a/WickedEngine/wiInputManager.h +++ b/WickedEngine/wiInputManager.h @@ -3,56 +3,52 @@ #include -class wiXInput; -class wiDirectInput; -class wiRawInput; +enum INPUT_TYPE +{ + INPUT_TYPE_KEYBOARD, + INPUT_TYPE_GAMEPAD, +}; +enum GAMEPAD_BUTTON +{ + GAMEPAD_BUTTON_UP, + GAMEPAD_BUTTON_LEFT, + GAMEPAD_BUTTON_DOWN, + GAMEPAD_BUTTON_RIGHT, + GAMEPAD_BUTTON_1, + GAMEPAD_BUTTON_2, + GAMEPAD_BUTTON_3, + GAMEPAD_BUTTON_4, + GAMEPAD_BUTTON_5, + GAMEPAD_BUTTON_6, + GAMEPAD_BUTTON_7, + GAMEPAD_BUTTON_8, + GAMEPAD_BUTTON_9, + GAMEPAD_BUTTON_10, + GAMEPAD_BUTTON_11, + GAMEPAD_BUTTON_12, + GAMEPAD_BUTTON_13, + GAMEPAD_BUTTON_14, +}; namespace wiInputManager { - void addXInput(wiXInput* input); - void addDirectInput(wiDirectInput* input); - void addRawInput(wiRawInput* input); + // call once at app start + void Initialize(); + // call once per frame void Update(); - - enum InputType{ - DIRECTINPUT_JOYPAD, - XINPUT_JOYPAD, - KEYBOARD, - RAWINPUT_JOYPAD, - INPUTTYPE_COUNT - }; - struct Input{ - InputType type; - DWORD button; - short playerIndex; - - Input(){ - type=InputType::DIRECTINPUT_JOYPAD; - button=(DWORD)0; - playerIndex=0; - } - bool operator<(const Input other){ - return (button!=other.button || type!=other.type || playerIndex!=other.playerIndex); - } - struct LessComparer{ - bool operator()(Input const& a,Input const& b) const{ - return (a.button 0) { - int code = wiLua::SGetInt(L, 1); - wiInputManager::InputType type = wiInputManager::KEYBOARD; + uint32_t code = (uint32_t)wiLua::SGetInt(L, 1); + INPUT_TYPE type = INPUT_TYPE_KEYBOARD; if (argc > 1) { - type = (wiInputManager::InputType)wiLua::SGetInt(L, 2); + type = (INPUT_TYPE)wiLua::SGetInt(L, 2); } - wiLua::SSetBool(L, wiInputManager::down((DWORD)code, type)); + short playerindex = 0; + if (argc > 2) + { + playerindex = (short)wiLua::SGetInt(L, 3); + } + wiLua::SSetBool(L, wiInputManager::down(code, type, playerindex)); return 1; } else - wiLua::SError(L, "Down(int code, opt int type = KEYBOARD) not enough arguments!"); + wiLua::SError(L, "Down(int code, opt int type = INPUT_TYPE_KEYBOARD) not enough arguments!"); return 0; } int wiInputManager_BindLua::Press(lua_State* L) @@ -40,17 +45,22 @@ int wiInputManager_BindLua::Press(lua_State* L) int argc = wiLua::SGetArgCount(L); if (argc > 0) { - int code = wiLua::SGetInt(L, 1); - wiInputManager::InputType type = wiInputManager::KEYBOARD; + uint32_t code = (uint32_t)wiLua::SGetInt(L, 1); + INPUT_TYPE type = INPUT_TYPE_KEYBOARD; if (argc > 1) { - type = (wiInputManager::InputType)wiLua::SGetInt(L, 2); + type = (INPUT_TYPE)wiLua::SGetInt(L, 2); } - wiLua::SSetBool(L, wiInputManager::press((DWORD)code, type)); + short playerindex = 0; + if (argc > 2) + { + playerindex = (short)wiLua::SGetInt(L, 3); + } + wiLua::SSetBool(L, wiInputManager::press(code, type, playerindex)); return 1; } else - wiLua::SError(L, "Press(int code, opt int type = KEYBOARD) not enough arguments!"); + wiLua::SError(L, "Press(int code, opt int type = INPUT_TYPE_KEYBOARD) not enough arguments!"); return 0; } int wiInputManager_BindLua::Hold(lua_State* L) @@ -58,8 +68,8 @@ int wiInputManager_BindLua::Hold(lua_State* L) int argc = wiLua::SGetArgCount(L); if (argc > 0) { - int code = wiLua::SGetInt(L, 1); - int duration = 30; + uint32_t code = (uint32_t)wiLua::SGetInt(L, 1); + uint32_t duration = 30; if (argc > 1) { duration = wiLua::SGetInt(L, 2); @@ -69,12 +79,17 @@ int wiInputManager_BindLua::Hold(lua_State* L) { continuous = wiLua::SGetBool(L, 3); } - wiInputManager::InputType type = wiInputManager::KEYBOARD; + INPUT_TYPE type = INPUT_TYPE_KEYBOARD; if (argc > 3) { - type = (wiInputManager::InputType)wiLua::SGetInt(L, 4); + type = (INPUT_TYPE)wiLua::SGetInt(L, 4); } - wiLua::SSetBool(L, wiInputManager::hold((DWORD)code, (DWORD)duration, continuous, type)); + short playerindex = 0; + if (argc > 4) + { + playerindex = (short)wiLua::SGetInt(L, 5); + } + wiLua::SSetBool(L, wiInputManager::hold(code, duration, continuous, type, playerindex)); return 1; } else @@ -136,10 +151,8 @@ void wiInputManager_BindLua::Bind() wiLua::GetGlobal()->RunText("input = InputManager()"); //Input types - wiLua::GetGlobal()->RunText("DIRECTINPUT_JOYPAD = 0"); - wiLua::GetGlobal()->RunText("XINPUT_JOYPAD = 1"); - wiLua::GetGlobal()->RunText("KEYBOARD = 2"); - wiLua::GetGlobal()->RunText("RAWINPUT_JOYPAD = 3"); + wiLua::GetGlobal()->RunText("INPUT_TYPE_KEYBOARD = 0"); + wiLua::GetGlobal()->RunText("INPUT_TYPE_GAMEPAD = 1"); //Keyboard wiLua::GetGlobal()->RunText("VK_UP = 0x26"); @@ -169,6 +182,26 @@ void wiInputManager_BindLua::Bind() wiLua::GetGlobal()->RunText("VK_MBUTTON = 0x04"); wiLua::GetGlobal()->RunText("VK_RBUTTON = 0x02"); + //Gamepad + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_UP = 0"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_LEFT = 1"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_DOWN = 2"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_RIGHT = 3"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_1 = 4"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_2 = 5"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_3 = 6"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_4 = 7"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_5 = 8"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_6 = 9"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_7 = 10"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_8 = 11"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_9 = 12"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_10 = 13"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_11 = 14"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_12 = 15"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_13 = 16"); + wiLua::GetGlobal()->RunText("GAMEPAD_BUTTON_14 = 17"); + //Touch wiLua::GetGlobal()->RunText("TOUCHSTATE_PRESSED = 0"); wiLua::GetGlobal()->RunText("TOUCHSTATE_RELEASED = 1"); diff --git a/WickedEngine/wiSceneSystem_BindLua.cpp b/WickedEngine/wiSceneSystem_BindLua.cpp index 41521fdba..bc951e4a5 100644 --- a/WickedEngine/wiSceneSystem_BindLua.cpp +++ b/WickedEngine/wiSceneSystem_BindLua.cpp @@ -419,6 +419,11 @@ int Scene_BindLua::Component_GetName(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); NameComponent* component = scene->names.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new NameComponent_BindLua(component)); return 1; } @@ -436,6 +441,11 @@ int Scene_BindLua::Component_GetLayer(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); LayerComponent* component = scene->layers.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new LayerComponent_BindLua(component)); return 1; } @@ -453,6 +463,11 @@ int Scene_BindLua::Component_GetTransform(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); TransformComponent* component = scene->transforms.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new TransformComponent_BindLua(component)); return 1; } @@ -470,6 +485,11 @@ int Scene_BindLua::Component_GetCamera(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); CameraComponent* component = scene->cameras.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new CameraComponent_BindLua(component)); return 1; } @@ -487,6 +507,11 @@ int Scene_BindLua::Component_GetAnimation(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); AnimationComponent* component = scene->animations.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new AnimationComponent_BindLua(component)); return 1; } @@ -504,6 +529,11 @@ int Scene_BindLua::Component_GetMaterial(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); MaterialComponent* component = scene->materials.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new MaterialComponent_BindLua(component)); return 1; } @@ -521,6 +551,11 @@ int Scene_BindLua::Component_GetEmitter(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); wiEmittedParticle* component = scene->emitters.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new EmitterComponent_BindLua(component)); return 1; } @@ -538,6 +573,11 @@ int Scene_BindLua::Component_GetLight(lua_State* L) Entity entity = (Entity)wiLua::SGetInt(L, 1); LightComponent* component = scene->lights.GetComponent(entity); + if (component == nullptr) + { + return 0; + } + Luna::push(L, new LightComponent_BindLua(component)); return 1; } diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index a2e01765e..730a6a0ee 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 26; // minor bug fixes, alterations, refactors, updates - const int revision = 20; + const int revision = 21; long GetVersion() diff --git a/WickedEngine/wiWidget.cpp b/WickedEngine/wiWidget.cpp index ec98c2144..e37e80bd5 100644 --- a/WickedEngine/wiWidget.cpp +++ b/WickedEngine/wiWidget.cpp @@ -338,7 +338,7 @@ void wiButton::Update(wiGUI* gui, float dt) } } - if (wiInputManager::press(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::press(VK_LBUTTON)) { if (state == FOCUS) { @@ -347,7 +347,7 @@ void wiButton::Update(wiGUI* gui, float dt) } } - if (wiInputManager::down(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::down(VK_LBUTTON)) { if (state == DEACTIVATING) { @@ -543,7 +543,7 @@ void wiTextInputField::Update(wiGUI* gui, float dt) } } - if (wiInputManager::press(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::press(VK_LBUTTON)) { if (state == FOCUS) { @@ -552,7 +552,7 @@ void wiTextInputField::Update(wiGUI* gui, float dt) } } - if (wiInputManager::down(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::down(VK_LBUTTON)) { if (state == DEACTIVATING) { @@ -570,7 +570,7 @@ void wiTextInputField::Update(wiGUI* gui, float dt) if (state == ACTIVE) { - if (wiInputManager::press(VK_RETURN, wiInputManager::KEYBOARD)) + if (wiInputManager::press(VK_RETURN)) { // accept input... @@ -585,8 +585,8 @@ void wiTextInputField::Update(wiGUI* gui, float dt) gui->DeactivateWidget(this); } - else if ((wiInputManager::press(VK_LBUTTON, wiInputManager::KEYBOARD) && !intersectsPointer) || - wiInputManager::press(VK_ESCAPE, wiInputManager::KEYBOARD)) + else if ((wiInputManager::press(VK_LBUTTON) && !intersectsPointer) || + wiInputManager::press(VK_ESCAPE)) { // cancel input value_new.clear(); @@ -722,7 +722,7 @@ void wiSlider::Update(wiGUI* gui, float dt) } if (state == ACTIVE) { - if (wiInputManager::down(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::down(VK_LBUTTON)) { if (state == ACTIVE) { @@ -755,7 +755,7 @@ void wiSlider::Update(wiGUI* gui, float dt) } } - if (wiInputManager::press(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::press(VK_LBUTTON)) { if (state == FOCUS) { @@ -889,7 +889,7 @@ void wiCheckBox::Update(wiGUI* gui, float dt) } } - if (wiInputManager::press(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::press(VK_LBUTTON)) { if (state == FOCUS) { @@ -898,7 +898,7 @@ void wiCheckBox::Update(wiGUI* gui, float dt) } } - if (wiInputManager::down(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::down(VK_LBUTTON)) { if (state == DEACTIVATING) { @@ -1036,13 +1036,13 @@ void wiComboBox::Update(wiGUI* gui, float dt) } } - if (wiInputManager::press(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::press(VK_LBUTTON)) { // activate clicked = true; } - if (wiInputManager::down(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::down(VK_LBUTTON)) { if (state == DEACTIVATING) { @@ -1597,7 +1597,7 @@ void wiColorPicker::Update(wiGUI* gui, float dt) bool dragged = false; - if (wiInputManager::press(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::press(VK_LBUTTON)) { if (state == FOCUS) { @@ -1606,7 +1606,7 @@ void wiColorPicker::Update(wiGUI* gui, float dt) } } - if (wiInputManager::down(VK_LBUTTON, wiInputManager::KEYBOARD)) + if (wiInputManager::down(VK_LBUTTON)) { if (state == ACTIVE) { diff --git a/WickedEngine/wiWindowRegistration.cpp b/WickedEngine/wiWindowRegistration.cpp index 6304a2697..e8b0a0356 100644 --- a/WickedEngine/wiWindowRegistration.cpp +++ b/WickedEngine/wiWindowRegistration.cpp @@ -3,13 +3,20 @@ namespace wiWindowRegistration { window_type window = nullptr; + instance_type instance = nullptr; window_type GetRegisteredWindow() { return window; } + instance_type GetRegisteredInstance() { + return instance; + } void RegisterWindow(window_type wnd) { window = wnd; } + void RegisterInstance(instance_type inst) { + instance = inst; + } bool IsWindowActive() { #ifndef WINSTORE_SUPPORT HWND fgw = GetForegroundWindow(); diff --git a/WickedEngine/wiWindowRegistration.h b/WickedEngine/wiWindowRegistration.h index f3ee75e96..8114bdacd 100644 --- a/WickedEngine/wiWindowRegistration.h +++ b/WickedEngine/wiWindowRegistration.h @@ -5,11 +5,15 @@ namespace wiWindowRegistration { #ifndef WINSTORE_SUPPORT typedef HWND window_type; + typedef HINSTANCE instance_type; #else typedef Windows::UI::Core::CoreWindow^ window_type; + typedef void* instance_type; #endif + instance_type GetRegisteredInstance(); window_type GetRegisteredWindow(); + void RegisterInstance(instance_type wnd); void RegisterWindow(window_type wnd); bool IsWindowActive(); }; diff --git a/WickedEngine/wiXInput.h b/WickedEngine/wiXInput.h index 753fee917..b9c56f503 100644 --- a/WickedEngine/wiXInput.h +++ b/WickedEngine/wiXInput.h @@ -2,6 +2,8 @@ #include "CommonInclude.h" #include +// TODO REFACTOR + #pragma comment(lib,"xinput.lib")