Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
16 KB
Referenced Files
None
Subscribers
None
diff --git a/util/input/input-manager.cpp b/util/input/input-manager.cpp
index 08246d46..b7f686d3 100644
--- a/util/input/input-manager.cpp
+++ b/util/input/input-manager.cpp
@@ -1,238 +1,242 @@
#include "input-manager.h"
#include "configuration.h"
/* FIXME: break this dependancy. this is used for PaintownInput */
#include "paintown-engine/object/object.h"
#include "joystick.h"
#include "util/events.h"
#include "globals.h"
#include "util/debug.h"
#include <stdlib.h>
#include <vector>
using namespace std;
InputManager * InputManager::manager = 0;
InputManager::InputManager():
capture(0),
joystick(NULL){
manager = this;
if (Configuration::isJoystickEnabled()){
joystick = Joystick::create();
}
}
InputManager::~InputManager(){
delete joystick;
}
bool InputManager::anyInput(){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_anyInput();
}
bool InputManager::_anyInput(){
if (keyboard.keypressed()){
return true;
}
if (joystick){
return joystick->pressed();
/*
JoystickInput all_joystick = joystick->readAll();
if (all_joystick.pressed()){
return true;
}
*/
}
return false;
}
+/*
vector<Input::PaintownInput> InputManager::getInput(const Configuration & configuration, const int facing){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_getInput(configuration, facing);
}
+*/
/*
void InputManager::enableBufferInput(){
manager->bufferKeys = true;
}
void InputManager::disableBufferInput(){
manager->bufferKeys = false;
}
*/
void InputManager::waitForClear(){
manager->keyboard.clear();
while (anyInput()){
poll();
Util::rest(1);
}
}
void InputManager::waitForKeys(int key1, int key2){
InputMap<int> wait;
wait.set(key1, 0, false, 1);
wait.set(key2, 0, false, 1);
InputManager::waitForRelease(wait, 1);
InputManager::waitForPress(wait, 1);
InputManager::waitForRelease(wait, 1);
}
void InputManager::waitForKeys(int key){
InputMap<int> wait;
wait.set(key, 0, false, 1);
InputManager::waitForRelease(wait, 1);
InputManager::waitForPress(wait, 1);
InputManager::waitForRelease(wait, 1);
}
/* these mappings should agree with configuration.cpp:defaultJoystick1Keys,
* but otherwise they are completely arbitrary
*/
static Input::PaintownInput convertJoystickKey(const Joystick::Key key, const int facing){
switch (key){
case Joystick::Up : return Input::Up;
case Joystick::Down : return Input::Down;
case Joystick::Left : {
if (facing == Paintown::Object::FACING_RIGHT){
return Input::Back;
} else {
return Input::Forward;
}
}
case Joystick::Right : {
if (facing == Paintown::Object::FACING_RIGHT){
return Input::Forward;
} else {
return Input::Back;
}
}
case Joystick::Button1 : return Input::Attack1;
case Joystick::Button2 : return Input::Attack2;
case Joystick::Button3 : return Input::Attack3;
case Joystick::Button4 : return Input::Jump;
default : return Input::Unknown;
}
}
static vector<Input::PaintownInput> convertJoystick(const Configuration & configuration, JoystickInput input, const int facing){
vector<Input::PaintownInput> all;
if (input.up){
all.push_back(convertJoystickKey(configuration.getJoystickUp(), facing));
}
if (input.right){
all.push_back(convertJoystickKey(configuration.getJoystickRight(), facing));
}
if (input.left){
all.push_back(convertJoystickKey(configuration.getJoystickLeft(), facing));
}
if (input.down){
all.push_back(convertJoystickKey(configuration.getJoystickDown(), facing));
}
if (input.button1){
all.push_back(convertJoystickKey(configuration.getJoystickAttack1(), facing));
}
if (input.button2){
all.push_back(convertJoystickKey(configuration.getJoystickAttack2(), facing));
}
if (input.button3){
all.push_back(convertJoystickKey(configuration.getJoystickAttack3(), facing));
}
if (input.button4){
all.push_back(convertJoystickKey(configuration.getJoystickJump(), facing));
}
return all;
}
void InputManager::poll(){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_poll();
}
int InputManager::readKey(){
return manager->_readKey();
}
int InputManager::_readKey(){
std::vector<int> keys;
do{
keyboard.readKeys(keys);
if (keys.size() == 0){
Util::rest(1);
poll();
}
} while (keys.size() == 0);
return keys.front();
}
void InputManager::_poll(){
/* FIXME: not sure if its a good idea to put the event manager here */
/*
if (bufferKeys){
eventManager.enableKeyBuffer();
}
*/
eventManager.run(keyboard, joystick);
// keyboard.poll();
/*
if (joystick != NULL){
joystick->poll();
}
*/
// const vector<Util::EventManager::KeyType> & keys = eventManager.getBufferedKeys();
// bufferedKeys.insert(bufferedKeys.end(), keys.begin(), keys.end());
}
+#if 0
vector<Input::PaintownInput> InputManager::_getInput(const Configuration & configuration, const int facing){
InputMap<Input::PaintownInput> input;
enum Input::PaintownInput all[] = {Input::Forward, Input::Back, Input::Up, Input::Down, Input::Attack1, Input::Attack2, Input::Attack3, Input::Attack4, Input::Attack5, Input::Attack6, Input::Jump, Input::Grab};
for (unsigned int i = 0; i < sizeof(all) / sizeof(Input::PaintownInput); i++){
input.set(configuration.getKey(all[i], facing), 0, false, all[i]);
input.set(configuration.getJoystickKey(all[i], facing), 0, false, all[i]);
}
InputMap<Input::PaintownInput>::Output state = InputManager::getMap(input);
return InputMap<Input::PaintownInput>::getAllPressed(state);
/*
vector<int> all_keys;
keyboard.readKeys( all_keys );
vector<Input::PaintownInput> real_input = Input::convertKeyboard(configuration, facing, all_keys);
if (joystick != NULL){
vector<Input::PaintownInput> joystick_keys = convertJoystick(configuration, joystick->readAll(), facing);
real_input.insert(real_input.begin(), joystick_keys.begin(), joystick_keys.end());
}
return real_input;
*/
}
+#endif
diff --git a/util/input/input-manager.h b/util/input/input-manager.h
index 11d0802d..fd9de0cf 100644
--- a/util/input/input-manager.h
+++ b/util/input/input-manager.h
@@ -1,291 +1,291 @@
#ifndef _paintown_input_manager
#define _paintown_input_manager
#include <vector>
#include <algorithm>
#include "input.h"
#include "input-map.h"
#include "util/funcs.h"
#include "util/events.h"
#include "keyboard.h"
#include "exceptions/exception.h"
class Configuration;
class Joystick;
template <class Output>
class InputHandler{
public:
InputHandler(){
}
virtual ~InputHandler(){
}
virtual void press(const Output & out, Keyboard::unicode_t unicode) = 0;
virtual void release(const Output & out, Keyboard::unicode_t unicode) = 0;
};
/* handles keyboard/joystick/whatever input during the game */
class InputManager{
public:
/* main has one instance of this and thats it.
* should the janitor have the reference instead?
*/
friend int paintown_main(int, char**);
friend int main(int, char **);
/* returns true if any input device is activated (keys pressed, joystick button */
static bool anyInput();
- static std::vector<Input::PaintownInput> getInput(const Configuration & configuration, const int facing);
+ // static std::vector<Input::PaintownInput> getInput(const Configuration & configuration, const int facing);
static void poll();
/*
static void enableBufferInput();
static void disableBufferInput();
*/
static void waitForKeys(int key1, int key2);
static void waitForKeys(int key1);
static int readKey();
static void waitForClear();
/*
template <class X>
static void observeKeyboard(InputMap<X> & input){
manager->keyboard.addObserver(InputMap<X>::observeKey, &input);
}
template <class X>
static void unObserveKeyboard(InputMap<X> & input){
manager->keyboard.removeObserver(InputMap<X>::observeKey, &input);
}
*/
static std::vector<Keyboard::unicode_t> readText(){
return manager->keyboard.readText();
}
template <typename X>
static std::vector<Keyboard::unicode_t> readText(InputMap<X> & input, typename InputMap<X>::Output & output){
return manager->_readText(input, output);
}
template <typename X>
static typename InputMap<X>::Output getMap(InputMap<X> & input){
if (manager){
return manager->_getMap(input);
}
/* just crash hard.. who cares */
throw Exception::Base(__FILE__, __LINE__);
/* make the compiler happy about returning something */
return *(typename InputMap<X>::Output*)1;
}
template <typename X>
static typename std::vector<typename InputMap<X>::InputEvent> getEvents(InputMap<X> & input){
return manager->_getEvents(input);
}
template <typename X>
static void handleEvents(InputMap<X> & input, InputHandler<X> & handler){
typename std::vector<typename InputMap<X>::InputEvent> events = getEvents(input);
for (typename std::vector<typename InputMap<X>::InputEvent>::iterator it = events.begin(); it != events.end(); it++){
const typename InputMap<X>::InputEvent & event = *it;
if (event.enabled){
handler.press(event.out, event.unicode);
} else {
handler.release(event.out, event.unicode);
}
}
}
template <typename X>
static bool pressed(InputMap<X> & input, X out){
if (manager){
return manager->_pressed(input, out);
}
return false;
}
/* wait for a key to be released
* really this waits for all inputs that would result in `out'
* being generated to stop.
*/
template <typename X>
static void waitForRelease(InputMap<X> & input, X out){
while (InputManager::pressed(input, out)){
Util::rest(1);
InputManager::poll();
}
}
template <typename X>
static void waitForPress(InputMap<X> & input, X out){
while (!InputManager::pressed(input, out)){
Util::rest(1);
InputManager::poll();
}
}
template <typename X>
static void captureInput(InputMap<X> & input){
manager->_captureInput(input);
}
template <typename X>
static void releaseInput(InputMap<X> & input){
manager->_releaseInput(input);
}
protected:
InputManager();
virtual ~InputManager();
virtual bool _anyInput();
virtual int _readKey();
- virtual std::vector<Input::PaintownInput> _getInput(const Configuration & configuration, const int facing);
+ // virtual std::vector<Input::PaintownInput> _getInput(const Configuration & configuration, const int facing);
template <typename X>
void _captureInput(InputMap<X> & input){
capture = (void*) &input;
}
template <typename X>
void _releaseInput(InputMap<X> & input){
if (capture == (void*) &input){
capture = 0;
}
}
template <typename X>
std::vector<Keyboard::unicode_t> _readText(InputMap<X> & input, typename InputMap<X>::Output & output){
std::vector<Keyboard::unicode_t> text;
std::vector<Keyboard::KeyData> all = keyboard.readData();
for (std::vector<Keyboard::KeyData>::iterator it = all.begin(); it != all.end(); it++){
const Keyboard::KeyData & data = *it;
KeyState<X> * state = input.getState(data.key);
if (state != NULL && output[state->out]){
text.push_back(data.unicode);
}
}
return text;
}
void removeDuplicates(std::vector<int> & storage){
std::vector<int> output;
int last = -1;
for (std::vector<int>::iterator it = storage.begin(); it != storage.end(); it++){
if (*it != last){
output.push_back(*it);
last = *it;
}
}
storage = output;
}
template <typename X>
typename std::vector<typename InputMap<X>::InputEvent> _getEvents(InputMap<X> & input){
std::vector<typename InputMap<X>::InputEvent> events;
if (capture != NULL && capture != &input){
return events;
}
const std::vector<typename Keyboard::KeyData> & buffer = keyboard.getBufferedKeys();
for (std::vector<Keyboard::KeyData>::const_iterator it = buffer.begin(); it != buffer.end(); it++){
const Keyboard::KeyData & data = *it;
KeyState<X> * state = input.getState(data.key);
if (state != NULL){
events.push_back(typename InputMap<X>::InputEvent(state->out, data.unicode, data.enabled));
}
}
if (joystick){
const std::vector<typename Joystick::Event> & joystickEvents = joystick->getEvents();
for (std::vector<Joystick::Event>::const_iterator it = joystickEvents.begin(); it != joystickEvents.end(); it++){
Joystick::Event event = *it;
JoystickState<X> * state = input.getJoystickState(event.key);
if (state != NULL){
events.push_back(typename InputMap<X>::InputEvent(state->out, -1, event.enabled));
}
}
}
return events;
}
template <typename X>
typename InputMap<X>::Output _getMap(InputMap<X> & input){
typename InputMap<X>::Output output;
if (capture != 0 && capture != &input){
return output;
}
std::vector<int> all_keys;
keyboard.readKeys(all_keys);
keyboard.readBufferedKeys(all_keys);
// all_keys.insert(all_keys.end(), bufferedKeys.begin(), bufferedKeys.end());
// all_keys.insert(all_keys.end(), keyboard.currentKeys().begin(), keyboard.currentKeys.end());
std::sort(all_keys.begin(), all_keys.end());
removeDuplicates(all_keys);
// std::unique(all_keys.begin(), all_keys.end());
// bufferedKeys.clear();
input.read(all_keys, &output);
/*
if (joystick != NULL){
JoystickInput all_joystick = joystick->readAll();
input.read(all_joystick, &output);
}
*/
/* just bumps an internal counter */
input.update();
return output;
}
template <typename X>
bool _pressed(InputMap<X> & input, X result){
if (capture != 0 && capture != &input){
return false;
}
std::vector<int> all_keys;
keyboard.readKeys(all_keys);
// all_keys.insert(all_keys.end(), bufferedKeys.begin(), bufferedKeys.end());
std::sort(all_keys.begin(), all_keys.end());
removeDuplicates(all_keys);
// bufferedKeys.clear();
bool out = false;
out = input.pressed(all_keys, result);
/*
if (joystick != NULL){
JoystickInput all_joystick = joystick->readAll();
out |= input.pressed(all_joystick, result);
}
*/
return out;
}
virtual void _poll();
private:
static InputManager * manager;
void * capture;
Joystick * joystick;
Keyboard keyboard;
Util::EventManager eventManager;
// std::vector<int> bufferedKeys;
// bool bufferKeys;
};
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 10:03 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68264
Default Alt Text
(16 KB)

Event Timeline