Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126226
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
36 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/events.cpp b/util/events.cpp
index 90eb5158..0954ef65 100644
--- a/util/events.cpp
+++ b/util/events.cpp
@@ -1,301 +1,406 @@
#ifdef USE_SDL
#include <SDL.h>
#endif
+#ifdef USE_ALLEGRO5
+#include <allegro5/allegro.h>
+#endif
#include <vector>
#include "bitmap.h"
#include "events.h"
#include "exceptions/shutdown_exception.h"
#include "configuration.h"
#include "debug.h"
#include "funcs.h"
#include "thread.h"
#include "init.h"
#include "parameter.h"
#include "input/keyboard.h"
#include "input/joystick.h"
#include "input/input-manager.h"
template <class Value> std::vector<Value> Util::Parameter<Value>::stack;
namespace Util{
EventManager::EventManager():
bufferKeys(false){
+#ifdef USE_ALLEGRO5
+ queue = al_create_event_queue();
+ al_register_event_source(queue, al_get_keyboard_event_source());
+#endif
}
#ifdef USE_SDL
static void handleKeyDown(Keyboard & keyboard, const SDL_Event & event){
keyboard.press(event.key.keysym.sym, event.key.keysym.unicode);
}
static void handleKeyUp(Keyboard & keyboard, const SDL_Event & event){
keyboard.release(event.key.keysym.sym);
}
static void handleJoystickButtonUp(Joystick * joystick, const SDL_Event & event){
int device = event.jbutton.which;
int button = event.jbutton.button;
if (device == joystick->getDeviceId()){
joystick->releaseButton(button);
}
}
static void handleJoystickHat(Joystick * joystick, const SDL_Event & event){
int device = event.jhat.which;
int motion = event.jhat.value;
/* should up/down control left/right -- flip these values? */
#if WII
const int axis_up_down = 0;
const int axis_left_right = 1;
const int up = 1;
const int down = -1;
const int left = -1;
const int right = 1;
#else
const int axis_up_down = 1;
const int axis_left_right = 0;
const int up = -1;
const int down = 1;
const int left = -1;
const int right = 1;
#endif
switch (motion){
case SDL_HAT_CENTERED: break;
case SDL_HAT_UP: joystick->axisMotion(axis_up_down, up); break;
case SDL_HAT_DOWN: joystick->axisMotion(axis_up_down, down); break;
case SDL_HAT_RIGHT: joystick->axisMotion(axis_left_right, right); break;
case SDL_HAT_LEFT: joystick->axisMotion(axis_left_right, left); break;
default: break;
}
}
static void handleJoystickButtonDown(Joystick * joystick, const SDL_Event & event){
int device = event.jbutton.which;
int button = event.jbutton.button;
if (device == joystick->getDeviceId()){
joystick->pressButton(button);
}
}
static void handleJoystickAxis(Joystick * joystick, const SDL_Event & event){
int device = event.jaxis.which;
int axis = event.jaxis.axis;
int value = event.jaxis.value;
if (device == joystick->getDeviceId()){
joystick->axisMotion(axis, value);
}
}
void EventManager::runSDL(Keyboard & keyboard, Joystick * joystick){
keyboard.poll();
if (joystick){
joystick->poll();
}
SDL_Event event;
while (SDL_PollEvent(&event) == 1){
switch (event.type){
case SDL_QUIT : {
dispatch(CloseWindow);
break;
}
case SDL_KEYDOWN : {
handleKeyDown(keyboard, event);
// dispatch(Key, event.key.keysym.sym);
break;
}
case SDL_KEYUP : {
handleKeyUp(keyboard, event);
break;
}
case SDL_JOYBUTTONDOWN: {
if (joystick != NULL){
handleJoystickButtonDown(joystick, event);
}
break;
}
case SDL_JOYHATMOTION : {
if (joystick != NULL){
handleJoystickHat(joystick, event);
}
break;
}
case SDL_JOYBUTTONUP: {
if (joystick != NULL){
handleJoystickButtonUp(joystick, event);
}
break;
}
case SDL_JOYAXISMOTION: {
if (joystick != NULL){
handleJoystickAxis(joystick, event);
}
break;
}
case SDL_VIDEORESIZE : {
int width = event.resize.w;
int height = event.resize.h;
/* to keep the perspective correct
* 640/480 = 1.33333
*/
if (width > height){
height = (int)((double) width / 1.3333333333);
} else {
width = (int)((double) height * 1.3333333333);
}
dispatch(ResizeScreen, width, height);
break;
}
default : {
break;
}
}
}
}
#endif
#ifdef USE_ALLEGRO
void EventManager::runAllegro(Keyboard & keyboard, Joystick * joystick){
keyboard.poll();
}
#endif
+#ifdef USE_ALLEGRO5
+static void handleKeyDown(Keyboard & keyboard, const ALLEGRO_EVENT & event){
+ keyboard.press(event.keyboard.keycode, 0);
+}
+
+static void handleKeyUp(Keyboard & keyboard, const ALLEGRO_EVENT & event){
+ keyboard.release(event.keyboard.keycode);
+}
+
+void EventManager::runAllegro5(Keyboard & keyboard, Joystick * joystick){
+ keyboard.poll();
+
+ ALLEGRO_EVENT event;
+ while (al_get_next_event(queue, &event)){
+ switch (event.type){
+ case ALLEGRO_EVENT_KEY_DOWN: {
+ handleKeyDown(keyboard, event);
+ break;
+ }
+ case ALLEGRO_EVENT_KEY_UP: {
+ handleKeyUp(keyboard, event);
+ break;
+ }
+ }
+ }
+
+ /*
+ if (joystick){
+ joystick->poll();
+ }
+ SDL_Event event;
+ while (SDL_PollEvent(&event) == 1){
+ switch (event.type){
+ case SDL_QUIT : {
+ dispatch(CloseWindow);
+ break;
+ }
+ case SDL_KEYDOWN : {
+ handleKeyDown(keyboard, event);
+ // dispatch(Key, event.key.keysym.sym);
+ break;
+ }
+ case SDL_KEYUP : {
+ handleKeyUp(keyboard, event);
+ break;
+ }
+ case SDL_JOYBUTTONDOWN: {
+ if (joystick != NULL){
+ handleJoystickButtonDown(joystick, event);
+ }
+ break;
+ }
+ case SDL_JOYHATMOTION : {
+ if (joystick != NULL){
+ handleJoystickHat(joystick, event);
+ }
+ break;
+ }
+ case SDL_JOYBUTTONUP: {
+ if (joystick != NULL){
+ handleJoystickButtonUp(joystick, event);
+ }
+ break;
+ }
+ case SDL_JOYAXISMOTION: {
+ if (joystick != NULL){
+ handleJoystickAxis(joystick, event);
+ }
+ break;
+ }
+ case SDL_VIDEORESIZE : {
+ int width = event.resize.w;
+ int height = event.resize.h;
+ / * to keep the perspective correct
+ * 640/480 = 1.33333
+ * /
+ if (width > height){
+ height = (int)((double) width / 1.3333333333);
+ } else {
+ width = (int)((double) height * 1.3333333333);
+ }
+ dispatch(ResizeScreen, width, height);
+ break;
+ }
+ default : {
+ break;
+ }
+ }
+ }
+ */
+}
+#endif
+
void EventManager::run(Keyboard & keyboard, Joystick * joystick){
#ifdef USE_SDL
runSDL(keyboard, joystick);
#elif USE_ALLEGRO
runAllegro(keyboard, joystick);
+#elif USE_ALLEGRO5
+ runAllegro5(keyboard, joystick);
#endif
}
/* kill the program if the user requests */
void EventManager::waitForThread(WaitThread & thread){
// Keyboard dummy;
while (!thread.isRunning()){
try{
/* input manager will run the event manager */
InputManager::poll();
// run(dummy);
} catch (const ShutdownException & death){
thread.kill();
throw death;
}
Util::rest(10);
}
}
EventManager::~EventManager(){
+#ifdef USE_ALLEGRO5
+ al_destroy_event_queue(queue);
+#endif
}
void EventManager::enableKeyBuffer(){
bufferKeys = true;
}
void EventManager::disableKeyBuffer(){
bufferKeys = false;
}
void EventManager::dispatch(Event type, int arg1){
switch (type){
case Key : {
if (bufferKeys){
keys.push_back(KeyType(arg1));
}
break;
}
default : {
break;
}
}
}
void EventManager::dispatch(Event type, int arg1, int arg2){
switch (type){
case ResizeScreen : {
Global::debug(1) << "Resizing screen to " << arg1 << ", " << arg2 << std::endl;
if (Graphics::setGraphicsMode(0, arg1, arg2) == 0){
Configuration::setScreenWidth(arg1);
Configuration::setScreenHeight(arg2);
}
break;
}
default : break;
}
}
void EventManager::dispatch(Event type){
switch (type){
case CloseWindow : {
throw ShutdownException();
}
default : break;
}
}
class LoopDone: public std::exception {
public:
LoopDone(){
}
~LoopDone() throw () {
}
};
Logic::~Logic(){
}
Draw::~Draw(){
}
static void doStandardLoop(Logic & logic, Draw & draw){
Global::speed_counter4 = 0;
double runCounter = 0;
try{
while (!logic.done()){
if (Global::speed_counter4 > 0){
runCounter += logic.ticks(Global::speed_counter4);
Global::speed_counter4 = 0;
bool need_draw = false;
while (runCounter >= 1.0){
need_draw = true;
InputManager::poll();
runCounter -= 1;
logic.run();
if (Global::shutdown()){
throw ShutdownException();
}
if (logic.done()){
/* quit the loop immediately */
throw LoopDone();
}
}
if (need_draw){
draw.draw();
}
}
while (Global::speed_counter4 == 0){
rest(1);
}
}
} catch (const LoopDone & done){
}
}
void standardLoop(Logic & logic, Draw & draw){
/* if a screen already exists (because we have nested standardLoops) then
* leave this parameter alone, otherwise set a new parameter.
*/
if (Parameter<Graphics::Bitmap*>::current() == NULL){
Graphics::Bitmap screen(Graphics::getScreenBuffer());
Parameter<Graphics::Bitmap*> use(&screen);
doStandardLoop(logic, draw);
} else {
doStandardLoop(logic, draw);
}
}
}
diff --git a/util/events.h b/util/events.h
index 6a70a3c1..b8128f2c 100644
--- a/util/events.h
+++ b/util/events.h
@@ -1,90 +1,99 @@
#ifndef _paintown_events_h
#define _paintown_events_h
/* handles global events from the system such as
* window manager events (press X button)
* keyboard/mouse/joystick input (for some backends like SDL)
*/
#include <vector>
+#ifdef USE_ALLEGRO5
+struct ALLEGRO_EVENT_SOURCE;
+struct ALLEGRO_EVENT_QUEUE;
+#endif
+
#ifdef USE_SDL
#include <SDL.h>
#endif
class Keyboard;
class Joystick;
namespace Util{
class WaitThread;
class EventManager{
public:
EventManager();
virtual void run(Keyboard & keyboard, Joystick * joystick);
virtual void waitForThread(WaitThread & thread);
virtual ~EventManager();
#ifdef USE_SDL
typedef SDLKey KeyType;
#else
typedef int KeyType;
#endif
inline const std::vector<KeyType> & getBufferedKeys() const {
return keys;
}
void enableKeyBuffer();
void disableKeyBuffer();
private:
enum Event{
CloseWindow,
ResizeScreen,
Key
};
virtual void dispatch(Event type, int arg1, int arg2);
virtual void dispatch(Event type, int arg1);
virtual void dispatch(Event type);
#ifdef USE_SDL
virtual void runSDL(Keyboard &, Joystick *);
#endif
#ifdef USE_ALLEGRO
virtual void runAllegro(Keyboard & keyboard, Joystick *);
#endif
+#ifdef USE_ALLEGRO5
+ virtual void runAllegro5(Keyboard & keyboard, Joystick *);
+ ALLEGRO_EVENT_QUEUE * queue;
+#endif
std::vector<KeyType> keys;
bool bufferKeys;
};
/* implement these classes to get the standard run loop */
class Logic{
public:
/* run a cycle of logic */
virtual void run() = 0;
/* the run loop should finish */
virtual bool done() = 0;
/* return a number of logic ticks for a given number of ticks on
* a real system.
*/
virtual double ticks(double systemTicks) = 0;
virtual ~Logic();
};
class Draw{
public:
virtual void draw() = 0;
virtual ~Draw();
};
void standardLoop(Logic & logic, Draw & draw);
}
#endif
diff --git a/util/input/allegro5/keyboard.cpp b/util/input/allegro5/keyboard.cpp
index 6a81982a..f5c9f0c7 100644
--- a/util/input/allegro5/keyboard.cpp
+++ b/util/input/allegro5/keyboard.cpp
@@ -1,159 +1,170 @@
#include "../keyboard.h"
#include <allegro5/keycodes.h>
const int Keyboard::Key_A = ALLEGRO_KEY_A;
const int Keyboard::Key_B = ALLEGRO_KEY_B;
const int Keyboard::Key_C = ALLEGRO_KEY_C;
const int Keyboard::Key_D = ALLEGRO_KEY_D;
const int Keyboard::Key_E = ALLEGRO_KEY_E;
const int Keyboard::Key_F = ALLEGRO_KEY_F;
const int Keyboard::Key_G = ALLEGRO_KEY_G;
const int Keyboard::Key_H = ALLEGRO_KEY_H;
const int Keyboard::Key_I = ALLEGRO_KEY_I;
const int Keyboard::Key_J = ALLEGRO_KEY_J;
const int Keyboard::Key_K = ALLEGRO_KEY_K;
const int Keyboard::Key_L = ALLEGRO_KEY_L;
const int Keyboard::Key_M = ALLEGRO_KEY_M;
const int Keyboard::Key_N = ALLEGRO_KEY_N;
const int Keyboard::Key_O = ALLEGRO_KEY_O;
const int Keyboard::Key_P = ALLEGRO_KEY_P;
const int Keyboard::Key_Q = ALLEGRO_KEY_Q;
const int Keyboard::Key_R = ALLEGRO_KEY_R;
const int Keyboard::Key_S = ALLEGRO_KEY_S;
const int Keyboard::Key_T = ALLEGRO_KEY_T;
const int Keyboard::Key_U = ALLEGRO_KEY_U;
const int Keyboard::Key_V = ALLEGRO_KEY_V;
const int Keyboard::Key_W = ALLEGRO_KEY_W;
const int Keyboard::Key_X = ALLEGRO_KEY_X;
const int Keyboard::Key_Y = ALLEGRO_KEY_Y;
const int Keyboard::Key_Z = ALLEGRO_KEY_Z;
const int Keyboard::Key_0 = ALLEGRO_KEY_0;
const int Keyboard::Key_1 = ALLEGRO_KEY_1;
const int Keyboard::Key_2 = ALLEGRO_KEY_2;
const int Keyboard::Key_3 = ALLEGRO_KEY_3;
const int Keyboard::Key_4 = ALLEGRO_KEY_4;
const int Keyboard::Key_5 = ALLEGRO_KEY_5;
const int Keyboard::Key_6 = ALLEGRO_KEY_6;
const int Keyboard::Key_7 = ALLEGRO_KEY_7;
const int Keyboard::Key_8 = ALLEGRO_KEY_8;
const int Keyboard::Key_9 = ALLEGRO_KEY_9;
const int Keyboard::Key_0_PAD = ALLEGRO_KEY_PAD_0;
const int Keyboard::Key_1_PAD = ALLEGRO_KEY_PAD_1;
const int Keyboard::Key_2_PAD = ALLEGRO_KEY_PAD_2;
const int Keyboard::Key_3_PAD = ALLEGRO_KEY_PAD_3;
const int Keyboard::Key_4_PAD = ALLEGRO_KEY_PAD_4;
const int Keyboard::Key_5_PAD = ALLEGRO_KEY_PAD_5;
const int Keyboard::Key_6_PAD = ALLEGRO_KEY_PAD_6;
const int Keyboard::Key_7_PAD = ALLEGRO_KEY_PAD_7;
const int Keyboard::Key_8_PAD = ALLEGRO_KEY_PAD_8;
const int Keyboard::Key_9_PAD = ALLEGRO_KEY_PAD_9;
const int Keyboard::Key_F1 = ALLEGRO_KEY_F1;
const int Keyboard::Key_F2 = ALLEGRO_KEY_F2;
const int Keyboard::Key_F3 = ALLEGRO_KEY_F3;
const int Keyboard::Key_F4 = ALLEGRO_KEY_F4;
const int Keyboard::Key_F5 = ALLEGRO_KEY_F5;
const int Keyboard::Key_F6 = ALLEGRO_KEY_F6;
const int Keyboard::Key_F7 = ALLEGRO_KEY_F7;
const int Keyboard::Key_F8 = ALLEGRO_KEY_F8;
const int Keyboard::Key_F9 = ALLEGRO_KEY_F9;
const int Keyboard::Key_F10 = ALLEGRO_KEY_F10;
const int Keyboard::Key_F11 = ALLEGRO_KEY_F11;
const int Keyboard::Key_F12 = ALLEGRO_KEY_F12;
const int Keyboard::Key_ESC = ALLEGRO_KEY_ESCAPE;
const int Keyboard::Key_TILDE = ALLEGRO_KEY_TILDE;
const int Keyboard::Key_MINUS = ALLEGRO_KEY_MINUS;
const int Keyboard::Key_EQUALS = ALLEGRO_KEY_EQUALS;
const int Keyboard::Key_BACKSPACE = ALLEGRO_KEY_BACKSPACE;
const int Keyboard::Key_TAB = ALLEGRO_KEY_TAB;
const int Keyboard::Key_OPENBRACE = ALLEGRO_KEY_OPENBRACE;
const int Keyboard::Key_CLOSEBRACE = ALLEGRO_KEY_CLOSEBRACE;
const int Keyboard::Key_ENTER = ALLEGRO_KEY_ENTER;
const int Keyboard::Key_COLON = ALLEGRO_KEY_SEMICOLON2;
const int Keyboard::Key_QUOTE = ALLEGRO_KEY_QUOTE;
const int Keyboard::Key_BACKSLASH = ALLEGRO_KEY_BACKSLASH;
const int Keyboard::Key_BACKSLASH2 = ALLEGRO_KEY_BACKSLASH2;
const int Keyboard::Key_COMMA = ALLEGRO_KEY_COMMA;
const int Keyboard::Key_STOP = ALLEGRO_KEY_FULLSTOP;
const int Keyboard::Key_SLASH = ALLEGRO_KEY_SLASH;
const int Keyboard::Key_SPACE = ALLEGRO_KEY_SPACE;
const int Keyboard::Key_INSERT = ALLEGRO_KEY_INSERT;
const int Keyboard::Key_DEL = ALLEGRO_KEY_DELETE;
const int Keyboard::Key_HOME = ALLEGRO_KEY_HOME;
const int Keyboard::Key_END = ALLEGRO_KEY_END;
const int Keyboard::Key_PGUP = ALLEGRO_KEY_PGUP;
const int Keyboard::Key_PGDN = ALLEGRO_KEY_PGDN;
const int Keyboard::Key_LEFT = ALLEGRO_KEY_LEFT;
const int Keyboard::Key_RIGHT = ALLEGRO_KEY_RIGHT;
const int Keyboard::Key_UP = ALLEGRO_KEY_UP;
const int Keyboard::Key_DOWN = ALLEGRO_KEY_DOWN;
const int Keyboard::Key_SLASH_PAD = ALLEGRO_KEY_PAD_SLASH;
const int Keyboard::Key_ASTERISK = ALLEGRO_KEY_PAD_ASTERISK;
const int Keyboard::Key_MINUS_PAD = ALLEGRO_KEY_PAD_MINUS;
const int Keyboard::Key_PLUS_PAD = ALLEGRO_KEY_PAD_PLUS;
const int Keyboard::Key_DEL_PAD = ALLEGRO_KEY_PAD_DELETE;
const int Keyboard::Key_ENTER_PAD = ALLEGRO_KEY_PAD_ENTER;
const int Keyboard::Key_PRTSCR = ALLEGRO_KEY_PRINTSCREEN;
const int Keyboard::Key_PAUSE = ALLEGRO_KEY_PAUSE;
const int Keyboard::Key_ABNT_C1 = ALLEGRO_KEY_ABNT_C1;
const int Keyboard::Key_YEN = ALLEGRO_KEY_YEN;
const int Keyboard::Key_KANA = ALLEGRO_KEY_KANA;
const int Keyboard::Key_CONVERT = ALLEGRO_KEY_CONVERT;
const int Keyboard::Key_NOCONVERT = ALLEGRO_KEY_NOCONVERT;
const int Keyboard::Key_AT = ALLEGRO_KEY_AT;
const int Keyboard::Key_CIRCUMFLEX = ALLEGRO_KEY_CIRCUMFLEX;
const int Keyboard::Key_COLON2 = ALLEGRO_KEY_COLON2;
const int Keyboard::Key_KANJI = ALLEGRO_KEY_KANJI;
const int Keyboard::Key_EQUALS_PAD = ALLEGRO_KEY_PAD_EQUALS;
const int Keyboard::Key_BACKQUOTE = ALLEGRO_KEY_BACKQUOTE;
const int Keyboard::Key_SEMICOLON = ALLEGRO_KEY_SEMICOLON;
const int Keyboard::Key_COMMAND = ALLEGRO_KEY_COMMAND;
/*
const int Keyboard::Key_UNKNOWN1 = ALLEGRO_KEY_UNKNOWN1;
const int Keyboard::Key_UNKNOWN2 = ALLEGRO_KEY_UNKNOWN2;
const int Keyboard::Key_UNKNOWN3 = ALLEGRO_KEY_UNKNOWN3;
const int Keyboard::Key_UNKNOWN4 = ALLEGRO_KEY_UNKNOWN4;
const int Keyboard::Key_UNKNOWN5 = ALLEGRO_KEY_UNKNOWN5;
const int Keyboard::Key_UNKNOWN6 = ALLEGRO_KEY_UNKNOWN6;
const int Keyboard::Key_UNKNOWN7 = ALLEGRO_KEY_UNKNOWN7;
const int Keyboard::Key_UNKNOWN8 = ALLEGRO_KEY_UNKNOWN8;
*/
const int Keyboard::Key_MODIFIERS = ALLEGRO_KEY_MODIFIERS;
const int Keyboard::Key_LSHIFT = ALLEGRO_KEY_LSHIFT;
const int Keyboard::Key_RSHIFT = ALLEGRO_KEY_RSHIFT;
const int Keyboard::Key_LCONTROL = ALLEGRO_KEY_LCTRL;
const int Keyboard::Key_RCONTROL = ALLEGRO_KEY_RCTRL;
const int Keyboard::Key_ALT = ALLEGRO_KEY_ALT;
const int Keyboard::Key_ALTGR = ALLEGRO_KEY_ALTGR;
const int Keyboard::Key_LWIN = ALLEGRO_KEY_LWIN;
const int Keyboard::Key_RWIN = ALLEGRO_KEY_RWIN;
const int Keyboard::Key_MENU = ALLEGRO_KEY_MENU;
const int Keyboard::Key_SCRLOCK = ALLEGRO_KEY_SCROLLLOCK;
const int Keyboard::Key_NUMLOCK = ALLEGRO_KEY_NUMLOCK;
const int Keyboard::Key_CAPSLOCK = ALLEGRO_KEY_CAPSLOCK;
Keyboard::Keyboard():
enableBuffer(false){
}
void Keyboard::disableKeyRepeat(){
/* TODO */
}
void Keyboard::enableKeyRepeat(){
/* TODO */
}
void Keyboard::readKeys(std::vector<int> & all_keys){
- /* TODO */
+ for (std::map<KeyType, KeyData>::iterator it = keyState.begin(); it != keyState.end(); it++){
+ KeyType key = it->first;
+ const KeyData & data = it->second;
+ if (data.enabled){
+ all_keys.push_back(key);
+ }
+ }
}
bool Keyboard::keypressed(){
/* TODO */
return false;
}
+
+void Keyboard::poll(){
+ buffer.clear();
+}
void Keyboard::clear(){
- /* TODO */
+ buffer.clear();
+ keyState.clear();
}
diff --git a/util/input/input-manager.cpp b/util/input/input-manager.cpp
index 031b3bc9..2ea5059a 100644
--- a/util/input/input-manager.cpp
+++ b/util/input/input-manager.cpp
@@ -1,236 +1,235 @@
#include "input-manager.h"
#include "configuration.h"
/* FIXME: break this dependancy */
#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){
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 */
- Util::EventManager eventManager;
/*
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());
}
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::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;
*/
}
diff --git a/util/input/input-manager.h b/util/input/input-manager.h
index 97a32fb8..f37bb09c 100644
--- a/util/input/input-manager.h
+++ b/util/input/input-manager.h
@@ -1,285 +1,287 @@
#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 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);
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);
if (state != NULL){
events.push_back(typename InputMap<X>::InputEvent(state->out, -1, true));
}
}
}
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
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 10:59 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68596
Default Alt Text
(36 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline