summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorPiotr Dziwinski <piotrdz@gmail.com>2012-09-19 18:32:18 +0200
committerPiotr Dziwinski <piotrdz@gmail.com>2012-09-19 18:32:18 +0200
commit51884cef8e015bccbe1fa96dc56dc2f32439ccc5 (patch)
tree53974a01650d52101c96dd11da5e2235c61a02d6 /src/common
parent4a639cf543c15d45a37674d7eadaf09c23c2203d (diff)
downloadcolobot-51884cef8e015bccbe1fa96dc56dc2f32439ccc5.tar.gz
colobot-51884cef8e015bccbe1fa96dc56dc2f32439ccc5.tar.bz2
colobot-51884cef8e015bccbe1fa96dc56dc2f32439ccc5.zip
Input bindings rewrite
- moved input bindings to CRobotMain - added virtual keymod and joystick button key presses - fixed putenv error; other minor fixes
Diffstat (limited to 'src/common')
-rw-r--r--src/common/event.h26
-rw-r--r--src/common/global.h57
-rw-r--r--src/common/key.h25
-rw-r--r--src/common/restext.cpp61
-rw-r--r--src/common/restext.h2
5 files changed, 103 insertions, 68 deletions
diff --git a/src/common/event.h b/src/common/event.h
index 4df1e6d..73950af 100644
--- a/src/common/event.h
+++ b/src/common/event.h
@@ -44,7 +44,9 @@ struct KeyEventData
{
//! STATE_PRESSED or STATE_RELEASED */
PressState state;
- //! Key symbol: KEY(...) macro value (from common/key.h)
+ //! If true, the key is a virtual code generated by key modifier press or joystick button press
+ bool virt;
+ //! Key symbol: KEY(...) macro value or virtual key VIRTUAL_... (from common/key.h)
unsigned int key;
//! Keyboard modifiers: a bitmask made of KEY_MOD(...) macro values (from common/key.h)
unsigned int mod;
@@ -52,7 +54,7 @@ struct KeyEventData
unsigned int unicode;
KeyEventData()
- : state(STATE_PRESSED), key(0), mod(0), unicode(0) {}
+ : state(STATE_PRESSED), virt(false), key(0), mod(0), unicode(0) {}
};
/** \struct MouseMotionEventData
@@ -60,7 +62,7 @@ struct KeyEventData
struct MouseMoveEventData
{
//! Current button state
- unsigned char state;
+ PressState state;
//! Position of mouse in normalized coordinates (0..1)
Math::Point pos;
@@ -171,17 +173,25 @@ struct Event
ActiveEventData active;
};
+ //! State of tracked keys (mask of TrackedKey enum values)
+ unsigned int trackedKeys;
+
+ //! Mouse position is provided also for other types of events besides mouse events
+ Math::Point mousePos;
+
+ // TODO: remove and replace references with trackedKeys
+ short keyState;
+
+ // TODO: remove and replace references with mousePos
+ Math::Point pos;
+
// TODO: remove
long param; // parameter
- // TODO: remove?
- Math::Point pos; // mouse position (0 .. 1)
-
// TODO: ?
float axeX; // control the X axis (-1 .. 1)
float axeY; // control of the Y axis (-1 .. 1)
float axeZ; // control the Z axis (-1 .. 1)
- short keyState; // state of the keyboard (KS_ *)
// TODO: remove in longer term (use CApplication's new time functions instead)
float rTime; // relative time
@@ -190,10 +200,10 @@ struct Event
{
type = aType;
systemEvent = false;
+ trackedKeys = 0;
param = 0;
axeX = axeY = axeZ = 0.0f;
- keyState = 0;
rTime = 0.0f;
}
};
diff --git a/src/common/global.h b/src/common/global.h
index 9cb3dd9..addb56d 100644
--- a/src/common/global.h
+++ b/src/common/global.h
@@ -82,38 +82,37 @@ enum ResearchType
};
/**
- * \enum KeyRank
- * \brief Slots for key assignment of user controls
+ * \enum InputSlot
+ * \brief Available slots for input bindings
*/
-// TODO: remove (use the new InputSlot enum from app/app.h)
-enum KeyRank
+enum InputSlot
{
- KEYRANK_LEFT = 0,
- KEYRANK_RIGHT = 1,
- KEYRANK_UP = 2,
- KEYRANK_DOWN = 3,
- KEYRANK_GUP = 4,
- KEYRANK_GDOWN = 5,
- KEYRANK_CAMERA = 6,
- KEYRANK_DESEL = 7,
- KEYRANK_ACTION = 8,
- KEYRANK_NEAR = 9,
- KEYRANK_AWAY = 10,
- KEYRANK_NEXT = 11,
- KEYRANK_HUMAN = 12,
- KEYRANK_QUIT = 13,
- KEYRANK_HELP = 14,
- KEYRANK_PROG = 15,
- KEYRANK_VISIT = 16,
- KEYRANK_SPEED10 = 17,
- KEYRANK_SPEED15 = 18,
- KEYRANK_SPEED20 = 19,
- KEYRANK_SPEED30 = 20,
- KEYRANK_AIMUP = 21,
- KEYRANK_AIMDOWN = 22,
- KEYRANK_CBOT = 23,
+ INPUT_SLOT_LEFT = 0,
+ INPUT_SLOT_RIGHT = 1,
+ INPUT_SLOT_UP = 2,
+ INPUT_SLOT_DOWN = 3,
+ INPUT_SLOT_GUP = 4,
+ INPUT_SLOT_GDOWN = 5,
+ INPUT_SLOT_CAMERA = 6,
+ INPUT_SLOT_DESEL = 7,
+ INPUT_SLOT_ACTION = 8,
+ INPUT_SLOT_NEAR = 9,
+ INPUT_SLOT_AWAY = 10,
+ INPUT_SLOT_NEXT = 11,
+ INPUT_SLOT_HUMAN = 12,
+ INPUT_SLOT_QUIT = 13,
+ INPUT_SLOT_HELP = 14,
+ INPUT_SLOT_PROG = 15,
+ INPUT_SLOT_VISIT = 16,
+ INPUT_SLOT_SPEED10 = 17,
+ INPUT_SLOT_SPEED15 = 18,
+ INPUT_SLOT_SPEED20 = 19,
+ INPUT_SLOT_SPEED30 = 20,
+ INPUT_SLOT_AIMUP = 21,
+ INPUT_SLOT_AIMDOWN = 22,
+ INPUT_SLOT_CBOT = 23,
- KEYRANK_MAX
+ INPUT_SLOT_MAX
};
// TODO: move to CRobotMain
diff --git a/src/common/key.h b/src/common/key.h
index 1d03a47..11076a3 100644
--- a/src/common/key.h
+++ b/src/common/key.h
@@ -33,3 +33,28 @@
// Key modifier defined as concatenation to KMOD_...
// If need arises, it can be changed to custom function or anything else
#define KEY_MOD(x) KMOD_ ## x
+
+/**
+ * \enum VirtualKmod
+ * \brief Virtual key codes generated on kmod presses
+ *
+ * These are provided here because left and right pair of keys generate different codes.
+ */
+enum VirtualKmod
+{
+ VIRTUAL_KMOD_CTRL = SDLK_LAST + 100, //! < control (left or right)
+ VIRTUAL_KMOD_SHIFT = SDLK_LAST + 101, //! < shift (left or right)
+ VIRTUAL_KMOD_ALT = SDLK_LAST + 102, //! < alt (left or right)
+ VIRTUAL_KMOD_META = SDLK_LAST + 103 //! < win key (left or right)
+};
+
+// Just syntax sugar
+// So it is the same as other macros
+#define VIRTUAL_KMOD(x) VIRTUAL_KMOD_ ## x
+
+// Virtual key code generated on joystick button presses
+// num is number of joystick button
+#define VIRTUAL_JOY(num) (SDLK_LAST + 200 + num)
+
+//! Special value for invalid key bindings
+const unsigned int KEY_INVALID = SDLK_LAST + 1000;
diff --git a/src/common/restext.cpp b/src/common/restext.cpp
index 5a8e6ba..40d11b7 100644
--- a/src/common/restext.cpp
+++ b/src/common/restext.cpp
@@ -21,6 +21,7 @@
#include "common/logger.h"
#include "CBot/resource.h"
#include "object/object.h"
+#include "object/robotmain.h"
#include <libintl.h>
#include <SDL/SDL_keyboard.h>
@@ -37,39 +38,38 @@ void SetGlobalGamerName(char *name)
struct KeyDesc
{
- KeyRank key;
+ InputSlot key;
char name[20];
};
static KeyDesc keyTable[22] =
{
- { KEYRANK_LEFT, "left;" },
- { KEYRANK_RIGHT, "right;" },
- { KEYRANK_UP, "up;" },
- { KEYRANK_DOWN, "down;" },
- { KEYRANK_GUP, "gup;" },
- { KEYRANK_GDOWN, "gdown;" },
- { KEYRANK_CAMERA, "camera;" },
- { KEYRANK_DESEL, "desel;" },
- { KEYRANK_ACTION, "action;" },
- { KEYRANK_NEAR, "near;" },
- { KEYRANK_AWAY, "away;" },
- { KEYRANK_NEXT, "next;" },
- { KEYRANK_HUMAN, "human;" },
- { KEYRANK_QUIT, "quit;" },
- { KEYRANK_HELP, "help;" },
- { KEYRANK_PROG, "prog;" },
- { KEYRANK_CBOT, "cbot;" },
- { KEYRANK_VISIT, "visit;" },
- { KEYRANK_SPEED10, "speed10;" },
- { KEYRANK_SPEED15, "speed15;" },
- { KEYRANK_SPEED20, "speed20;" },
- { KEYRANK_SPEED30, "speed30;" },
+ { INPUT_SLOT_LEFT, "left;" },
+ { INPUT_SLOT_RIGHT, "right;" },
+ { INPUT_SLOT_UP, "up;" },
+ { INPUT_SLOT_DOWN, "down;" },
+ { INPUT_SLOT_GUP, "gup;" },
+ { INPUT_SLOT_GDOWN, "gdown;" },
+ { INPUT_SLOT_CAMERA, "camera;" },
+ { INPUT_SLOT_DESEL, "desel;" },
+ { INPUT_SLOT_ACTION, "action;" },
+ { INPUT_SLOT_NEAR, "near;" },
+ { INPUT_SLOT_AWAY, "away;" },
+ { INPUT_SLOT_NEXT, "next;" },
+ { INPUT_SLOT_HUMAN, "human;" },
+ { INPUT_SLOT_QUIT, "quit;" },
+ { INPUT_SLOT_HELP, "help;" },
+ { INPUT_SLOT_PROG, "prog;" },
+ { INPUT_SLOT_CBOT, "cbot;" },
+ { INPUT_SLOT_VISIT, "visit;" },
+ { INPUT_SLOT_SPEED10, "speed10;" },
+ { INPUT_SLOT_SPEED15, "speed15;" },
+ { INPUT_SLOT_SPEED20, "speed20;" }
};
// Seeks a key.
-bool SearchKey(const char *cmd, KeyRank &key)
+bool SearchKey(const char *cmd, InputSlot &key)
{
int i;
@@ -88,9 +88,10 @@ bool SearchKey(const char *cmd, KeyRank &key)
static void PutKeyName(char* dst, const char* src)
{
- KeyRank key;
+ InputSlot key;
char name[50];
- int s, d, n, res;
+ int s, d, n;
+ unsigned int res;
s = d = 0;
while ( src[s] != 0 )
@@ -103,9 +104,8 @@ static void PutKeyName(char* dst, const char* src)
{
if ( SearchKey(src+s+5, key) )
{
- // FIXME: res = g_engine->RetKey(key, 0);
- res = 0;
- if ( res != 0 )
+ res = CRobotMain::GetInstancePointer()->GetInputBinding(key).key;
+ if (res != KEY_INVALID)
{
if ( GetResource(RES_KEY, res, name) )
{
@@ -144,7 +144,7 @@ static const char* GetResourceBase(ResType type, int num)
// assert(num < strings_event_len);
if (num >= strings_event_len)
{
- GetLogger()->Error("GetResource invalid event num: %d\n", num);
+ GetLogger()->Warn("GetResource invalid event num: %d\n", num);
return "";
}
str = strings_event[num];
@@ -165,6 +165,7 @@ static const char* GetResourceBase(ResType type, int num)
break;
case RES_KEY:
assert(num < SDLK_LAST);
+ // TODO: virtual keys
str = SDL_GetKeyName(static_cast<SDLKey>(num));
break;
default:
diff --git a/src/common/restext.h b/src/common/restext.h
index 4b565ba..6abb7f5 100644
--- a/src/common/restext.h
+++ b/src/common/restext.h
@@ -43,7 +43,7 @@ enum ResType
// TODO: move to CRobotMain
extern void SetGlobalGamerName(char *name);
-extern bool SearchKey(const char *cmd, KeyRank &key);
+extern bool SearchKey(const char *cmd, InputSlot& slot);
extern bool GetResource(ResType type, int num, char* text);
extern const char * const strings_text[];