Browse Source

Formatted input, added tap repeat code from 7drl2018

master
Macoy Madson 1 year ago
parent
commit
c7b35643be
2 changed files with 580 additions and 408 deletions
  1. +398
    -251
      input/input.cpp
  2. +182
    -157
      input/input.hpp

+ 398
- 251
input/input.cpp View File

@ -1,428 +1,575 @@
#ifndef INPUT_CPP
#define INPUT_CPP
#include "input.hpp"
#include <iostream>
inputManager::inputManager(sf::RenderWindow *newWin)
void inputManager::initializeKeyRepeat()
{
sfWin=newWin;
repeatTimer.start();
KeyRepeatPauseTime = .500f;
KeyRepeatTimePerRepeat = .1f;
}
inputManager::inputManager(window *newWin)
inputManager::inputManager(sf::RenderWindow* newWin)
{
sfWin=newWin->getBase();
sfWin = newWin;
initializeKeyRepeat();
}
inputManager::inputManager(window* newWin)
{
sfWin = newWin->getBase();
win = newWin;
initializeKeyRepeat();
}
inputManager::inputManager()
{
sfWin=NULL;
sfWin = NULL;
initializeKeyRepeat();
}
void inputManager::setWindow(sf::RenderWindow *newWin)
void inputManager::setWindow(sf::RenderWindow* newWin)
{
sfWin=newWin;
sfWin = newWin;
}
void inputManager::setWindow(window *newWin)
void inputManager::setWindow(window* newWin)
{
sfWin=newWin->getBase();
sfWin = newWin->getBase();
win = newWin;
}
int inputManager::getMouseX()
{
return sf::Mouse::getPosition(*sfWin).x;
}
int inputManager::getMouseY()
{
return sf::Mouse::getPosition(*sfWin).y;
}
//Universal button function. Call if you want to use both mouse
//and keyboard together (more abstract)
// Universal button function. Call if you want to use both mouse
// and keyboard together (more abstract)
bool inputManager::isPressed(int code)
{
if (win && !win->isFocused())
return false;
//Mouse code (they start at 1000)
if (code >= 1000)
{
inputCode::mouseCode mouseButton = static_cast<inputCode::mouseCode>(code);
return isMousePressed(mouseButton);
}
//Keyboard code (they start at 0)
inputCode::keyCode keyButton = static_cast<inputCode::keyCode>(code);
return isPressed(keyButton);
}
// Mouse code (they start at 1000)
if (code >= 1000)
{
inputCode::mouseCode mouseButton = static_cast<inputCode::mouseCode>(code);
return isMousePressed(mouseButton);
}
// Keyboard code (they start at 0)
inputCode::keyCode keyButton = static_cast<inputCode::keyCode>(code);
return isPressed(keyButton);
}
bool inputManager::isPressed(sf::Keyboard::Key key)
{
return sf::Keyboard::isKeyPressed(key);
}
bool inputManager::isPressed(inputCode::keyCode key)
{
if (win && !win->isFocused())
return false;
return sf::Keyboard::isKeyPressed(convertKeyCode(key));
}
bool inputManager::isMousePressed(sf::Mouse::Button button)
{
return sf::Mouse::isButtonPressed(button);
}
bool inputManager::isMousePressed(inputCode::mouseCode button)
{
if (win && !win->isFocused())
return false;
return sf::Mouse::isButtonPressed(convertMouseCode(button));
}
sf::Keyboard::Key inputManager::convertKeyCode(inputCode::keyCode key)
{
sf::Keyboard::Key newKey=sf::Keyboard::Unknown;
sf::Keyboard::Key newKey = sf::Keyboard::Unknown;
//:(
switch(key)
switch (key)
{
case inputCode::A:
newKey=sf::Keyboard::A;
newKey = sf::Keyboard::A;
break;
case inputCode::B:
newKey=sf::Keyboard::B;
break;
newKey = sf::Keyboard::B;
break;
case inputCode::C:
newKey=sf::Keyboard::C;
break;
newKey = sf::Keyboard::C;
break;
case inputCode::D:
newKey=sf::Keyboard::D;
break;
newKey = sf::Keyboard::D;
break;
case inputCode::E:
newKey=sf::Keyboard::E;
break;
newKey = sf::Keyboard::E;
break;
case inputCode::F:
newKey=sf::Keyboard::F;
break;
newKey = sf::Keyboard::F;
break;
case inputCode::G:
newKey=sf::Keyboard::G;
break;
newKey = sf::Keyboard::G;
break;
case inputCode::H:
newKey=sf::Keyboard::H;
newKey = sf::Keyboard::H;
break;
case inputCode::I:
newKey=sf::Keyboard::I;
break; ///< The I key
newKey = sf::Keyboard::I;
break; ///< The I key
case inputCode::J:
newKey=sf::Keyboard::J;
break; ///< The J key
newKey = sf::Keyboard::J;
break; ///< The J key
case inputCode::K:
newKey=sf::Keyboard::K;
break; ///< The K key
newKey = sf::Keyboard::K;
break; ///< The K key
case inputCode::L:
newKey=sf::Keyboard::L;
break; ///< The L key
newKey = sf::Keyboard::L;
break; ///< The L key
case inputCode::M:
newKey=sf::Keyboard::M;
break; ///< The M key
newKey = sf::Keyboard::M;
break; ///< The M key
case inputCode::N:
newKey=sf::Keyboard::N;
break; ///< The N key
newKey = sf::Keyboard::N;
break; ///< The N key
case inputCode::O:
newKey=sf::Keyboard::O;
break; ///< The O key
newKey = sf::Keyboard::O;
break; ///< The O key
case inputCode::P:
newKey=sf::Keyboard::P;
break; ///< The P key
newKey = sf::Keyboard::P;
break; ///< The P key
case inputCode::Q:
newKey=sf::Keyboard::Q;
break; ///< The Q key
newKey = sf::Keyboard::Q;
break; ///< The Q key
case inputCode::R:
newKey=sf::Keyboard::R;
break; ///< The R key
newKey = sf::Keyboard::R;
break; ///< The R key
case inputCode::S:
newKey=sf::Keyboard::S;
break; ///< The S key
newKey = sf::Keyboard::S;
break; ///< The S key
case inputCode::T:
newKey=sf::Keyboard::T;
break; ///< The T key
newKey = sf::Keyboard::T;
break; ///< The T key
case inputCode::U:
newKey=sf::Keyboard::U;
break; ///< The U key
newKey = sf::Keyboard::U;
break; ///< The U key
case inputCode::V:
newKey=sf::Keyboard::V;
break;///< The V key
newKey = sf::Keyboard::V;
break; ///< The V key
case inputCode::W:
newKey=sf::Keyboard::W;
break; ///< The W key
newKey = sf::Keyboard::W;
break; ///< The W key
case inputCode::X:
newKey=sf::Keyboard::X;
break; ///< The X key
newKey = sf::Keyboard::X;
break; ///< The X key
case inputCode::Y:
newKey=sf::Keyboard::Y;
break; ///< The Y key
newKey = sf::Keyboard::Y;
break; ///< The Y key
case inputCode::Z:
newKey=sf::Keyboard::Z;
break; ///< The Z key
newKey = sf::Keyboard::Z;
break; ///< The Z key
case inputCode::Num0:
newKey=sf::Keyboard::Num0;
break; ///< The 0 key
newKey = sf::Keyboard::Num0;
break; ///< The 0 key
case inputCode::Num1:
newKey=sf::Keyboard::Num1;
break; ///< The 1 key
newKey = sf::Keyboard::Num1;
break; ///< The 1 key
case inputCode::Num2:
newKey=sf::Keyboard::Num2;
break; ///< The 2 key
newKey = sf::Keyboard::Num2;
break; ///< The 2 key
case inputCode::Num3:
newKey=sf::Keyboard::Num3;
break; ///< The 3 key
newKey = sf::Keyboard::Num3;
break; ///< The 3 key
case inputCode::Num4:
newKey=sf::Keyboard::Num4;
break; ///< The 4 key
newKey = sf::Keyboard::Num4;
break; ///< The 4 key
case inputCode::Num5:
newKey=sf::Keyboard::Num5;
break; ///< The 5 key
newKey = sf::Keyboard::Num5;
break; ///< The 5 key
case inputCode::Num6:
newKey=sf::Keyboard::Num6;
break; ///< The 6 key
newKey = sf::Keyboard::Num6;
break; ///< The 6 key
case inputCode::Num7:
newKey=sf::Keyboard::Num7;
break; ///< The 7 key
newKey = sf::Keyboard::Num7;
break; ///< The 7 key
case inputCode::Num8:
newKey=sf::Keyboard::Num8;
break; ///< The 8 key
newKey = sf::Keyboard::Num8;
break; ///< The 8 key
case inputCode::Num9:
newKey=sf::Keyboard::Num9;
break; ///< The 9 key
newKey = sf::Keyboard::Num9;
break; ///< The 9 key
case inputCode::Escape:
newKey=sf::Keyboard::Escape;
break; ///< The Escape key
newKey = sf::Keyboard::Escape;
break; ///< The Escape key
case inputCode::LControl:
newKey=sf::Keyboard::LControl;
break; ///< The left Control key
newKey = sf::Keyboard::LControl;
break; ///< The left Control key
case inputCode::LShift:
newKey=sf::Keyboard::LShift;
break; ///< The left Shift key
newKey = sf::Keyboard::LShift;
break; ///< The left Shift key
case inputCode::LAlt:
newKey=sf::Keyboard::LAlt;
break; ///< The left Alt key
newKey = sf::Keyboard::LAlt;
break; ///< The left Alt key
case inputCode::LSystem:
newKey=sf::Keyboard::LSystem;
break; ///< The left OS specific key: window (Windows and Linux): apple (MacOS X): ...
newKey = sf::Keyboard::LSystem;
break; ///< The left OS specific key: window (Windows and Linux): apple (MacOS X): ...
case inputCode::RControl:
newKey=sf::Keyboard::RControl;
break; ///< The right Control key
newKey = sf::Keyboard::RControl;
break; ///< The right Control key
case inputCode::RShift:
newKey=sf::Keyboard::RShift;
break; ///< The right Shift key
newKey = sf::Keyboard::RShift;
break; ///< The right Shift key
case inputCode::RAlt:
newKey=sf::Keyboard::RAlt;
break; ///< The right Alt key
newKey = sf::Keyboard::RAlt;
break; ///< The right Alt key
case inputCode::RSystem:
newKey=sf::Keyboard::RSystem;
break; ///< The right OS specific key: window (Windows and Linux): apple (MacOS X): ...
newKey = sf::Keyboard::RSystem;
break; ///< The right OS specific key: window (Windows and Linux): apple (MacOS X): ...
case inputCode::Menu:
newKey=sf::Keyboard::Menu;
break; ///< The Menu key
newKey = sf::Keyboard::Menu;
break; ///< The Menu key
case inputCode::LBracket:
newKey=sf::Keyboard::LBracket;
break; ///< The [ key
newKey = sf::Keyboard::LBracket;
break; ///< The [ key
case inputCode::RBracket:
newKey=sf::Keyboard::RBracket;
break; ///< The ] key
newKey = sf::Keyboard::RBracket;
break; ///< The ] key
case inputCode::SemiColon:
newKey=sf::Keyboard::SemiColon;
break; ///< The ; key
newKey = sf::Keyboard::SemiColon;
break; ///< The ; key
case inputCode::Comma:
newKey=sf::Keyboard::Comma;
break; ///< The : key
newKey = sf::Keyboard::Comma;
break; ///< The : key
case inputCode::Period:
newKey=sf::Keyboard::Period;
break; ///< The . key
newKey = sf::Keyboard::Period;
break; ///< The . key
case inputCode::Quote:
newKey=sf::Keyboard::Quote;
break; ///< The ' key
newKey = sf::Keyboard::Quote;
break; ///< The ' key
case inputCode::Slash:
newKey=sf::Keyboard::Slash;
break; ///< The / key
newKey = sf::Keyboard::Slash;
break; ///< The / key
case inputCode::BackSlash:
newKey=sf::Keyboard::BackSlash;
break; ///< The \ key
newKey = sf::Keyboard::BackSlash;
break; ///< The \ key
case inputCode::Tilde:
newKey=sf::Keyboard::Tilde;
break; ///< The ~ key
newKey = sf::Keyboard::Tilde;
break; ///< The ~ key
case inputCode::Equal:
newKey=sf::Keyboard::Equal;
break; ///< The = key
newKey = sf::Keyboard::Equal;
break; ///< The = key
case inputCode::Dash:
newKey=sf::Keyboard::Dash;
break; ///< The - key
newKey = sf::Keyboard::Dash;
break; ///< The - key
case inputCode::Space:
newKey=sf::Keyboard::Space;
break; ///< The Space key
newKey = sf::Keyboard::Space;
break; ///< The Space key
case inputCode::Return:
newKey=sf::Keyboard::Return;
break; ///< The Return key
newKey = sf::Keyboard::Return;
break; ///< The Return key
case inputCode::BackSpace:
newKey=sf::Keyboard::BackSpace;
break; ///< The Backspace key
newKey = sf::Keyboard::BackSpace;
break; ///< The Backspace key
case inputCode::Tab:
newKey=sf::Keyboard::Tab;
break; ///< The Tabulation key
newKey = sf::Keyboard::Tab;
break; ///< The Tabulation key
case inputCode::PageUp:
newKey=sf::Keyboard::PageUp;
break; ///< The Page up key
newKey = sf::Keyboard::PageUp;
break; ///< The Page up key
case inputCode::PageDown:
newKey=sf::Keyboard::PageDown;
break; ///< The Page down key
newKey = sf::Keyboard::PageDown;
break; ///< The Page down key
case inputCode::End:
newKey=sf::Keyboard::End;
break; ///< The End key
newKey = sf::Keyboard::End;
break; ///< The End key
case inputCode::Home:
newKey=sf::Keyboard::Home;
newKey = sf::Keyboard::Home;
break;
case inputCode::Insert:
newKey=sf::Keyboard::Insert;
break; ///< The Insert key
newKey = sf::Keyboard::Insert;
break; ///< The Insert key
case inputCode::Delete:
newKey=sf::Keyboard::Delete;
break; ///< The Delete key
newKey = sf::Keyboard::Delete;
break; ///< The Delete key
case inputCode::Add:
newKey=sf::Keyboard::Add;
break; ///< The + key
newKey = sf::Keyboard::Add;
break; ///< The + key
case inputCode::Subtract:
newKey=sf::Keyboard::Subtract;
break; ///< The - key
newKey = sf::Keyboard::Subtract;
break; ///< The - key
case inputCode::Multiply:
newKey=sf::Keyboard::Multiply;
break; ///< The * key
newKey = sf::Keyboard::Multiply;
break; ///< The * key
case inputCode::Divide:
newKey=sf::Keyboard::Divide;
break; ///< The / key
newKey = sf::Keyboard::Divide;
break; ///< The / key
case inputCode::Left:
newKey=sf::Keyboard::Left;
break; ///< Left arrow
newKey = sf::Keyboard::Left;
break; ///< Left arrow
case inputCode::Right:
newKey=sf::Keyboard::Right;
break; ///< Right arrow
newKey = sf::Keyboard::Right;
break; ///< Right arrow
case inputCode::Up:
newKey=sf::Keyboard::Up;
break; ///< Up arrow
newKey = sf::Keyboard::Up;
break; ///< Up arrow
case inputCode::Down:
newKey=sf::Keyboard::Down;
break; ///< Down arrow
newKey = sf::Keyboard::Down;
break; ///< Down arrow
case inputCode::Numpad0:
newKey=sf::Keyboard::Numpad0;
break; ///< The numpad 0 key
newKey = sf::Keyboard::Numpad0;
break; ///< The numpad 0 key
case inputCode::Numpad1:
newKey=sf::Keyboard::Numpad1;
break; ///< The numpad 1 key
newKey = sf::Keyboard::Numpad1;
break; ///< The numpad 1 key
case inputCode::Numpad2:
newKey=sf::Keyboard::Numpad2;
break; ///< The numpad 2 key
newKey = sf::Keyboard::Numpad2;
break; ///< The numpad 2 key
case inputCode::Numpad3:
newKey=sf::Keyboard::Numpad3;
break; ///< The numpad 3 key
newKey = sf::Keyboard::Numpad3;
break; ///< The numpad 3 key
case inputCode::Numpad4:
newKey=sf::Keyboard::Numpad4;
break; ///< The numpad 4 key
newKey = sf::Keyboard::Numpad4;
break; ///< The numpad 4 key
case inputCode::Numpad5:
newKey=sf::Keyboard::Numpad5;
break; ///< The numpad 5 key
newKey = sf::Keyboard::Numpad5;
break; ///< The numpad 5 key
case inputCode::Numpad6:
newKey=sf::Keyboard::Numpad6;
break; ///< The numpad 6 key
newKey = sf::Keyboard::Numpad6;
break; ///< The numpad 6 key
case inputCode::Numpad7:
newKey=sf::Keyboard::Numpad7;
break; ///< The numpad 7 key
newKey = sf::Keyboard::Numpad7;
break; ///< The numpad 7 key
case inputCode::Numpad8:
newKey=sf::Keyboard::Numpad8;
break; ///< The numpad 8 key
newKey = sf::Keyboard::Numpad8;
break; ///< The numpad 8 key
case inputCode::Numpad9:
newKey=sf::Keyboard::Numpad9;
break; ///< The numpad 9 key
newKey = sf::Keyboard::Numpad9;
break; ///< The numpad 9 key
case inputCode::F1:
newKey=sf::Keyboard::F1;
break; ///< The F1 key
newKey = sf::Keyboard::F1;
break; ///< The F1 key
case inputCode::F2:
newKey=sf::Keyboard::F2;
break; ///< The F2 key
newKey = sf::Keyboard::F2;
break; ///< The F2 key
case inputCode::F3:
newKey=sf::Keyboard::F3;
break; ///< The F3 key
newKey = sf::Keyboard::F3;
break; ///< The F3 key
case inputCode::F4:
newKey=sf::Keyboard::F4;
break; ///< The F4 key
newKey = sf::Keyboard::F4;
break; ///< The F4 key
case inputCode::F5:
newKey=sf::Keyboard::F5;
break; ///< The F5 key
newKey = sf::Keyboard::F5;
break; ///< The F5 key
case inputCode::F6:
newKey=sf::Keyboard::F6;
break; ///< The F6 key
newKey = sf::Keyboard::F6;
break; ///< The F6 key
case inputCode::F7:
newKey=sf::Keyboard::F7;
break; ///< The F7 key
newKey = sf::Keyboard::F7;
break; ///< The F7 key
case inputCode::F8:
newKey=sf::Keyboard::F8;
break; ///< The F8 key
newKey = sf::Keyboard::F8;
break; ///< The F8 key
case inputCode::F9:
newKey=sf::Keyboard::F9;
break; ///< The F9 key
newKey = sf::Keyboard::F9;
break; ///< The F9 key
case inputCode::F10:
newKey=sf::Keyboard::F10;
break; ///< The F10 key
newKey = sf::Keyboard::F10;
break; ///< The F10 key
case inputCode::F11:
newKey=sf::Keyboard::F11;
break; ///< The F11 key
newKey = sf::Keyboard::F11;
break; ///< The F11 key
case inputCode::F12:
newKey=sf::Keyboard::F12;
break; ///< The F12 key
newKey = sf::Keyboard::F12;
break; ///< The F12 key
case inputCode::F13:
newKey=sf::Keyboard::F13;
break; ///< The F13 key
newKey = sf::Keyboard::F13;
break; ///< The F13 key
case inputCode::F14:
newKey=sf::Keyboard::F14;
break; ///< The F14 key
newKey = sf::Keyboard::F14;
break; ///< The F14 key
case inputCode::F15:
newKey=sf::Keyboard::F15;
newKey = sf::Keyboard::F15;
break;
default:
break;
};
return newKey;
};
sf::Mouse::Button inputManager::convertMouseCode(inputCode::mouseCode button)
{
//Default left click
sf::Mouse::Button newButton=sf::Mouse::Left;
switch(button)
// Default left click
sf::Mouse::Button newButton = sf::Mouse::Left;
switch (button)
{
case inputCode::LeftMouse:
newButton=sf::Mouse::Left;
newButton = sf::Mouse::Left;
break;
case inputCode::RightMouse:
newButton=sf::Mouse::Right;
newButton = sf::Mouse::Right;
break;
case inputCode::MiddleMouse:
newButton=sf::Mouse::Middle;
newButton = sf::Mouse::Middle;
break;
default:
break;
}
return newButton;
}
//Universal input function. Call if you want to use mouse and keyboard
//buttons or even return mouse position
// Universal input function. Call if you want to use mouse and keyboard
// buttons or even return mouse position
int inputManager::getState(int code)
{
if (code >= 2000) //Mouse position code
{
switch(code)
{
case inputCode::mousePosition::MouseX:
return getMouseX();
break;
case inputCode::mousePosition::MouseY:
return getMouseY();
break;
default:
break;
}
}
else //Button code
{
return isPressed(code);
}
//Input code not recognized
std::cout << "WARNING: inputManager.getState(): Input code " << code << " not recognized! Returning 0\n";
return 0;
if (code >= 2000) // Mouse position code
{
switch (code)
{
case inputCode::mousePosition::MouseX:
return getMouseX();
break;
case inputCode::mousePosition::MouseY:
return getMouseY();
break;
default:
break;
}
}
else // Button code
{
return isPressed(code);
}
// Input code not recognized
std::cout << "WARNING: inputManager.getState(): Input code " << code
<< " not recognized! Returning 0\n";
return 0;
}
bool inputManager::UpdateRepeat(InputState& state, bool currentlyPressed)
{
float currentTime = repeatTimer.getTime();
if (currentlyPressed)
{
if (state.KeyRepeatTime)
{
if (state.KeyRepeatTime <= currentTime)
{
state.KeyRepeatTime = currentTime + KeyRepeatTimePerRepeat;
return true;
}
else
return false;
}
else
{
state.KeyRepeatTime = currentTime + KeyRepeatPauseTime;
return false;
}
}
state.KeyRepeatTime = 0.f;
return false;
}
bool inputManager::WasTapped(inputCode::keyCode key, bool enableKeyRepeat)
{
bool currentlyPressed = isPressed(key);
std::map<inputCode::keyCode, InputState>::iterator findIt = keyTapStates.find(key);
if (findIt != keyTapStates.end())
{
bool shouldRepeat = UpdateRepeat(findIt->second, currentlyPressed);
if (currentlyPressed)
{
if (findIt->second.ProcessedTapped)
{
if (shouldRepeat && enableKeyRepeat)
return true;
// Wasn't tapped this frame
return false;
}
else
{
findIt->second.ProcessedTapped = true;
return true;
}
}
else
{
findIt->second.ProcessedTapped = false;
findIt->second.KeyRepeatTime = 0.f;
}
}
else
{
keyTapStates[key] = {true, 0.f};
return currentlyPressed;
}
return false;
}
void inputManager::GetInpDirectionalNavigation(int& deltaX, int& deltaY)
{
if (WasTapped(inputCode::Up, true) || WasTapped(inputCode::Numpad8, true))
deltaY = -1;
if (WasTapped(inputCode::Down, true) || WasTapped(inputCode::Numpad2, true))
deltaY = 1;
if (WasTapped(inputCode::Left, true) || WasTapped(inputCode::Numpad4, true))
deltaX = -1;
if (WasTapped(inputCode::Right, true) || WasTapped(inputCode::Numpad6, true))
deltaX = 1;
if (WasTapped(inputCode::Numpad9, true))
{
deltaX = 1;
deltaY = -1;
}
if (WasTapped(inputCode::Numpad3, true))
{
deltaX = 1;
deltaY = 1;
}
if (WasTapped(inputCode::Numpad1, true))
{
deltaX = -1;
deltaY = 1;
}
if (WasTapped(inputCode::Numpad7, true))
{
deltaX = -1;
deltaY = -1;
}
}
std::vector<int> inputManager::GetInpNumbersTapped()
{
std::vector<int> numbersTapped;
if (WasTapped(inputCode::Num0, false))
numbersTapped.push_back(0);
if (WasTapped(inputCode::Num1, false))
numbersTapped.push_back(1);
if (WasTapped(inputCode::Num2, false))
numbersTapped.push_back(2);
if (WasTapped(inputCode::Num3, false))
numbersTapped.push_back(3);
if (WasTapped(inputCode::Num4, false))
numbersTapped.push_back(4);
if (WasTapped(inputCode::Num5, false))
numbersTapped.push_back(5);
if (WasTapped(inputCode::Num6, false))
numbersTapped.push_back(6);
if (WasTapped(inputCode::Num7, false))
numbersTapped.push_back(7);
if (WasTapped(inputCode::Num8, false))
numbersTapped.push_back(8);
if (WasTapped(inputCode::Num9, false))
numbersTapped.push_back(9);
return numbersTapped;
}
#endif

+ 182
- 157
input/input.hpp View File

@ -1,168 +1,193 @@
#ifndef INPUT_HPP
#define INPUT_HPP
//This is an evil dependency. Should probably get rid of it some time
//No, it's fine
#pragma once
// This is an evil dependency. Should probably get rid of it some time
// No, it's fine
#include "../graphics/graphics.hpp"
#include "../timer/timer.hpp"
#include <map>
#include <vector>
//Using a namespace because scoping isn't standard in GCC (or C++)
//See http://stackoverflow.com/questions/5188554/my-enum-is-not-a-class-or-namespace
// Using a namespace because scoping isn't standard in GCC (or C++)
// See http://stackoverflow.com/questions/5188554/my-enum-is-not-a-class-or-namespace
namespace inputCode
{
//Enums for key codes
//Keyboard codes (for input independence)
//An *almost* direct copy of SFML 2.0 sf::Key (see /usr/local/include/SFML/Window/Keyboard.hpp)
enum keyCode:int
{
A = 0, ///< The A key
B, ///< The B key
C, ///< The C key
D, ///< The D key
E, ///< The E key
F, ///< The F key
G, ///< The G key
H, ///< The H key
I, ///< The I key
J, ///< The J key
K, ///< The K key
L, ///< The L key
M, ///< The M key
N, ///< The N key
O, ///< The O key
P, ///< The P key
Q, ///< The Q key
R, ///< The R key
S, ///< The S key
T, ///< The T key
U, ///< The U key
V, ///< The V key
W, ///< The W key
X, ///< The X key
Y, ///< The Y key
Z, ///< The Z key
Num0, ///< The 0 key
Num1, ///< The 1 key
Num2, ///< The 2 key
Num3, ///< The 3 key
Num4, ///< The 4 key
Num5, ///< The 5 key
Num6, ///< The 6 key
Num7, ///< The 7 key
Num8, ///< The 8 key
Num9, ///< The 9 key
Escape, ///< The Escape key
LControl, ///< The left Control key
LShift, ///< The left Shift key
LAlt, ///< The left Alt key
LSystem, ///< The left OS specific key: window (Windows and Linux), apple (MacOS X), ...
RControl, ///< The right Control key
RShift, ///< The right Shift key
RAlt, ///< The right Alt key
RSystem, ///< The right OS specific key: window (Windows and Linux), apple (MacOS X), ...
Menu, ///< The Menu key
LBracket, ///< The [ key
RBracket, ///< The ] key
SemiColon, ///< The ; key
Comma, ///< The , key
Period, ///< The . key
Quote, ///< The ' key
Slash, ///< The / key
BackSlash, ///< The \ key
Tilde, ///< The ~ key
Equal, ///< The = key
Dash, ///< The - key
Space, ///< The Space key
Return, ///< The Return key
BackSpace, ///< The Backspace key
Tab, ///< The Tabulation key
PageUp, ///< The Page up key
PageDown, ///< The Page down key
End, ///< The End key
Home, ///< The Home key
Insert, ///< The Insert key
Delete, ///< The Delete key
Add, ///< The + key
Subtract, ///< The - key
Multiply, ///< The * key
Divide, ///< The / key
Left, ///< Left arrow
Right, ///< Right arrow
Up, ///< Up arrow
Down, ///< Down arrow
Numpad0, ///< The numpad 0 key
Numpad1, ///< The numpad 1 key
Numpad2, ///< The numpad 2 key
Numpad3, ///< The numpad 3 key
Numpad4, ///< The numpad 4 key
Numpad5, ///< The numpad 5 key
Numpad6, ///< The numpad 6 key
Numpad7, ///< The numpad 7 key
Numpad8, ///< The numpad 8 key
Numpad9, ///< The numpad 9 key
F1, ///< The F1 key
F2, ///< The F2 key
F3, ///< The F3 key
F4, ///< The F4 key
F5, ///< The F5 key
F6, ///< The F6 key
F7, ///< The F7 key
F8, ///< The F8 key
F9, ///< The F9 key
F10, ///< The F10 key
F11, ///< The F11 key
F12, ///< The F12 key
F13, ///< The F13 key
F14, ///< The F14 key
F15 ///< The F15 key
};
//Mouse codes
enum mouseCode:int
{
LeftMouse=1000,
RightMouse,
MiddleMouse
};
//Mouse position codes
enum mousePosition:int
{
MouseX=2000,
MouseY
};
}
// Enums for key codes
// Keyboard codes (for input independence)
// An *almost* direct copy of SFML 2.0 sf::Key (see /usr/local/include/SFML/Window/Keyboard.hpp)
enum keyCode : int
{
A = 0, ///< The A key
B, ///< The B key
C, ///< The C key
D, ///< The D key
E, ///< The E key
F, ///< The F key
G, ///< The G key
H, ///< The H key
I, ///< The I key
J, ///< The J key
K, ///< The K key
L, ///< The L key
M, ///< The M key
N, ///< The N key
O, ///< The O key
P, ///< The P key
Q, ///< The Q key
R, ///< The R key
S, ///< The S key
T, ///< The T key
U, ///< The U key
V, ///< The V key
W, ///< The W key
X, ///< The X key
Y, ///< The Y key
Z, ///< The Z key
Num0, ///< The 0 key
Num1, ///< The 1 key
Num2, ///< The 2 key
Num3, ///< The 3 key
Num4, ///< The 4 key
Num5, ///< The 5 key
Num6, ///< The 6 key
Num7, ///< The 7 key
Num8, ///< The 8 key
Num9, ///< The 9 key
Escape, ///< The Escape key
LControl, ///< The left Control key
LShift, ///< The left Shift key
LAlt, ///< The left Alt key
LSystem, ///< The left OS specific key: window (Windows and Linux), apple (MacOS X), ...
RControl, ///< The right Control key
RShift, ///< The right Shift key
RAlt, ///< The right Alt key
RSystem, ///< The right OS specific key: window (Windows and Linux), apple (MacOS X), ...
Menu, ///< The Menu key
LBracket, ///< The [ key
RBracket, ///< The ] key
SemiColon, ///< The ; key
Comma, ///< The , key
Period, ///< The . key
Quote, ///< The ' key
Slash, ///< The / key
BackSlash, ///< The \ key
Tilde, ///< The ~ key
Equal, ///< The = key
Dash, ///< The - key
Space, ///< The Space key
Return, ///< The Return key
BackSpace, ///< The Backspace key
Tab, ///< The Tabulation key
PageUp, ///< The Page up key
PageDown, ///< The Page down key
End, ///< The End key
Home, ///< The Home key
Insert, ///< The Insert key
Delete, ///< The Delete key
Add, ///< The + key
Subtract, ///< The - key
Multiply, ///< The * key
Divide, ///< The / key
Left, ///< Left arrow
Right, ///< Right arrow
Up, ///< Up arrow
Down, ///< Down arrow
Numpad0, ///< The numpad 0 key
Numpad1, ///< The numpad 1 key
Numpad2, ///< The numpad 2 key
Numpad3, ///< The numpad 3 key
Numpad4, ///< The numpad 4 key
Numpad5, ///< The numpad 5 key
Numpad6, ///< The numpad 6 key
Numpad7, ///< The numpad 7 key
Numpad8, ///< The numpad 8 key
Numpad9, ///< The numpad 9 key
F1, ///< The F1 key
F2, ///< The F2 key
F3, ///< The F3 key
F4, ///< The F4 key
F5, ///< The F5 key
F6, ///< The F6 key
F7, ///< The F7 key
F8, ///< The F8 key
F9, ///< The F9 key
F10, ///< The F10 key
F11, ///< The F11 key
F12, ///< The F12 key
F13, ///< The F13 key
F14, ///< The F14 key
F15 ///< The F15 key
};
// Mouse codes
enum mouseCode : int
{
LeftMouse = 1000,
RightMouse,
MiddleMouse
};
// Mouse position codes
enum mousePosition : int
{
MouseX = 2000,
MouseY
};
} // namespace inputCode
class inputManager
{
private:
sf::RenderWindow *sfWin;
window* win;
//sf::Keyboard convertKeyCode(keyCode);
public:
//Use inputManager(window*) over lib-specific functions
inputManager(sf::RenderWindow *);
inputManager(window *);
inputManager();
void setWindow(sf::RenderWindow *);
void setWindow(window *);
int getMouseX();
int getMouseY();
//Universal button function. Call if you want to use both mouse
//and keyboard together (more abstract)
bool isPressed(int code);
//Sadly, library dependant. Returns whether or not a key is pressed
bool isPressed(inputCode::keyCode);
//Note that this function is now deprecated; use isPressed(inputCode::keyCode) instead
bool isPressed(sf::Keyboard::Key);
private:
sf::RenderWindow* sfWin;
window* win;
struct InputState
{
bool ProcessedTapped;
float KeyRepeatTime;
};
std::map<inputCode::keyCode, InputState> keyTapStates;
timer repeatTimer;
float KeyRepeatPauseTime;
float KeyRepeatTimePerRepeat;
void initializeKeyRepeat();
bool UpdateRepeat(InputState& state, bool isPressed);
public:
// Use inputManager(window*) over lib-specific functions
inputManager(sf::RenderWindow*);
inputManager(window*);
inputManager();
void setWindow(sf::RenderWindow*);
void setWindow(window*);
int getMouseX();
int getMouseY();
// Universal button function. Call if you want to use both mouse
// and keyboard together (more abstract)
bool isPressed(int code);
// Sadly, library dependant. Returns whether or not a key is pressed
bool isPressed(inputCode::keyCode);
// Note that this function is now deprecated; use isPressed(inputCode::keyCode) instead
bool isPressed(sf::Keyboard::Key);
// Use these functions if you need lib-specific codes (and these are used internally)
sf::Keyboard::Key convertKeyCode(inputCode::keyCode);
sf::Mouse::Button convertMouseCode(inputCode::mouseCode);
// Returns whether or not a mouse key is pressed
bool isMousePressed(inputCode::mouseCode);
// Note that this function is now deprecated; use isMousePressed(inputCode::keyCode) instead
bool isMousePressed(sf::Mouse::Button);
//Use these functions if you need lib-specific codes (and these are used internally)
sf::Keyboard::Key convertKeyCode(inputCode::keyCode);
sf::Mouse::Button convertMouseCode(inputCode::mouseCode);
//Returns whether or not a mouse key is pressed
bool isMousePressed(inputCode::mouseCode);
//Note that this function is now deprecated; use isMousePressed(inputCode::keyCode) instead
bool isMousePressed(sf::Mouse::Button);
// Universal input function. Call if you want to use mouse and keyboard
// buttons or even return mouse position
int getState(int code);
//Universal input function. Call if you want to use mouse and keyboard
//buttons or even return mouse position
int getState(int code);
// Tapped with key repeat
bool WasTapped(inputCode::keyCode key, bool enableKeyRepeat);
// Arrow keys and numpad directional navigation
void GetInpDirectionalNavigation(int& deltaX, int& deltaY);
// Top row numbers only
std::vector<int> GetInpNumbersTapped();
};
#endif

Loading…
Cancel
Save