diff --git a/2dgk_7/2dgk_7/2dgk_7.vcxproj b/2dgk_7/2dgk_7/2dgk_7.vcxproj
index 065bd5f..9c54956 100644
--- a/2dgk_7/2dgk_7/2dgk_7.vcxproj
+++ b/2dgk_7/2dgk_7/2dgk_7.vcxproj
@@ -169,7 +169,9 @@
+
+
diff --git a/2dgk_7/2dgk_7/2dgk_7.vcxproj.filters b/2dgk_7/2dgk_7/2dgk_7.vcxproj.filters
index 637c52f..f8aacd2 100644
--- a/2dgk_7/2dgk_7/2dgk_7.vcxproj.filters
+++ b/2dgk_7/2dgk_7/2dgk_7.vcxproj.filters
@@ -80,5 +80,11 @@
Header Files
+
+ Header Files
+
+
+ Header Files
+
\ No newline at end of file
diff --git a/2dgk_7/2dgk_7/Controllers.h b/2dgk_7/2dgk_7/Controllers.h
new file mode 100644
index 0000000..6df735f
--- /dev/null
+++ b/2dgk_7/2dgk_7/Controllers.h
@@ -0,0 +1,9 @@
+#pragma once
+namespace KapitanGame {
+ enum class Controllers : int {
+ Controller1,
+ Controller2,
+ Controller3,
+ Controller4
+ };
+}
diff --git a/2dgk_7/2dgk_7/KActionBind.h b/2dgk_7/2dgk_7/KActionBind.h
new file mode 100644
index 0000000..0cf1f35
--- /dev/null
+++ b/2dgk_7/2dgk_7/KActionBind.h
@@ -0,0 +1,19 @@
+#pragma once
+#include "Controllers.h"
+#include "KPlayerController.h"
+
+namespace KapitanGame {;
+ enum class InputState : int {
+ Pressed,
+ Released,
+ Hold
+ };
+ struct KActionBind {
+ std::string Name;
+ InputState ExpectedInputState;
+ KPlayerController* ControllerObject;
+ KPlayerCommand Command;
+ Controllers Controller;
+ };
+}
+
diff --git a/2dgk_7/2dgk_7/KInput.cpp b/2dgk_7/2dgk_7/KInput.cpp
index c071cfe..5c1bd78 100644
--- a/2dgk_7/2dgk_7/KInput.cpp
+++ b/2dgk_7/2dgk_7/KInput.cpp
@@ -117,11 +117,54 @@ namespace KapitanGame {
break;
}
}
+
+ void KInput::BindAction(std::string name, InputState expectedInputState, KPlayerController* controller,
+ KPlayerCommand command) {
+ Actions.emplace_back(name, expectedInputState, controller, command);
+ }
+
+ bool KInput::CheckAction(const std::string& name, const InputState expectedInputState, Controllers controller) {
+ switch(expectedInputState) {
+
+ case InputState::Pressed:
+ const auto keyBind = KeyBinds.find(name);
+ if(keyBind!=KeyBinds.end()) {
+ if(IsKeyboardButtonPressed(keyBind->second)) {
+ return true;
+ }
+ }
+ const auto buttonBind = ButtonBinds[controller].find(name);
+ if(buttonBind!=ButtonBinds[controller].end()) {
+ if(IsControllerButtonPressed(controller, buttonBind)) {
+ return true;
+ }
+ }
+ return false;
+ case InputState::Released: break;
+ case InputState::Hold: break;
+ default: return false;
+ }
+ }
+
+ void KInput::ProcessActions() {
+ for(auto action : Actions) {
+ if(CheckAction(action.Name, action.ExpectedInputState, action.Controller)) {
+ //(action.ControllerObject->*action.Command)();
+ std::invoke(action.Command, action.ControllerObject);
+ }
+ }
+ }
+
bool KInput::IsControllerButtonPressed(const Controllers controllerId, const SDL_GameControllerButton button) {
if (controllerId < Controllers::Controller1 || controllerId > static_cast(GamepadsCount - 1)) return false;
return ControllerInputs[static_cast(controllerId)].Buttons[button] && !LastControllerInputs[static_cast(controllerId)].Buttons[button];
}
+ bool KInput::IsControllerButtonReleased(const Controllers controllerId, const SDL_GameControllerButton button) {
+ if (controllerId < Controllers::Controller1 || controllerId > static_cast(GamepadsCount - 1)) return false;
+
+ return !ControllerInputs[static_cast(controllerId)].Buttons[button] && LastControllerInputs[static_cast(controllerId)].Buttons[button];
+ }
bool KInput::IsControllerButtonHeld(const Controllers controllerId, const SDL_GameControllerButton button) {
if (controllerId < Controllers::Controller1 || controllerId > static_cast(GamepadsCount - 1)) return false;
@@ -144,6 +187,12 @@ namespace KapitanGame {
return KeyboardInputs[scanCode] && KeyboardInputs[scanCode] != LastKeyboardInputs[scanCode];
}
+ bool KInput::IsKeyboardButtonReleased(const Uint8 scanCode) {
+ if (scanCode > KeyboardInputs.size() || scanCode > LastKeyboardInputs.size())
+ return false;
+ return !KeyboardInputs[scanCode] && KeyboardInputs[scanCode] != LastKeyboardInputs[scanCode];
+ }
+
bool KInput::IsKeyboardButtonHeld(const Uint8 scanCode) {
if (scanCode > KeyboardInputs.size())
return false;
diff --git a/2dgk_7/2dgk_7/KInput.h b/2dgk_7/2dgk_7/KInput.h
index 0542139..f580969 100644
--- a/2dgk_7/2dgk_7/KInput.h
+++ b/2dgk_7/2dgk_7/KInput.h
@@ -1,23 +1,20 @@
#pragma once
#include
+#include
#include
#include "GamePad.h"
+#include "KActionBind.h"
namespace KapitanGame {
- enum class Controllers : int {
- Controller1,
- Controller2,
- Controller3,
- Controller4
- };
-
class KInput {
std::vector ConnectedControllers;
std::vector ControllerInputs;
std::vector LastControllerInputs;
std::vector KeyboardInputs;
std::vector LastKeyboardInputs;
+ std::vector Actions;
+ std::unordered_map KeyBinds;
int GamepadsCount;
bool Initialized;
public:
@@ -27,11 +24,16 @@ namespace KapitanGame {
void HandleInputPreEvents();
void HandleInputPostEvents();
void HandleEvent(const SDL_Event& event);
+ void BindAction(std::string name, InputState expectedInputState, KPlayerController* controller, KPlayerCommand command);
+ bool CheckAction(const std::string& name, InputState expectedInputState, Controllers controller);
+ void ProcessActions();
bool IsControllerButtonPressed(Controllers controllerId, SDL_GameControllerButton button);
+ bool IsControllerButtonReleased(Controllers controllerId, SDL_GameControllerButton button);
bool IsControllerButtonHeld(Controllers controllerId, SDL_GameControllerButton button);
float GetControllerAxis(Controllers controllerId, SDL_GameControllerAxis axis);
bool IsKeyboardButtonHeld(Uint8 scanCode);
bool IsKeyboardButtonPressed(Uint8 scanCode);
+ bool IsKeyboardButtonReleased(Uint8 scanCode);
};
}
diff --git a/2dgk_7/2dgk_7/KPlayerController.h b/2dgk_7/2dgk_7/KPlayerController.h
index 1f29a80..aa66873 100644
--- a/2dgk_7/2dgk_7/KPlayerController.h
+++ b/2dgk_7/2dgk_7/KPlayerController.h
@@ -9,6 +9,7 @@ namespace KapitanGame {
KVector2D Input;
KShape Shape;
};
+ typedef void (KPlayerController::* KPlayerCommand)();
}