src/Graphics/Input.cc
author Tero Marttila <terom@fixme.fi>
Thu, 22 Jan 2009 00:28:26 +0200
branchnew_graphics
changeset 416 38cba347a3a9
parent 414 cede5463b845
permissions -rw-r--r--
clean up InputHandler/GameView to use signals for input
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     1
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     2
#include "Input.hh"
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
     3
#include "../Error.hh"
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
     4
#include "../Config.hh"
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
     5
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
     6
#include <cassert>
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
     7
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
     8
namespace graphics
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
     9
{
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
    10
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    11
InputKeymapEntry<PlayerInputBit> INPUT_PLAYER_KEYMAP[] = {
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    12
    {   INPUT_AIM_UP,       INPUT_FLAG_UNLIMITED,   { -CL_KEY_ENTER,    CL_KEY_UP       } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    13
    {   INPUT_AIM_DOWN,     INPUT_FLAG_UNLIMITED,   { -CL_KEY_ENTER,    CL_KEY_DOWN     } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    14
    {   INPUT_MOVE_LEFT,    INPUT_FLAG_UNLIMITED,   { -CL_KEY_ENTER,    CL_KEY_LEFT     } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    15
    {   INPUT_MOVE_RIGHT,   INPUT_FLAG_UNLIMITED,   { -CL_KEY_ENTER,    CL_KEY_RIGHT    } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    16
    {   INPUT_JUMP,         INPUT_FLAG_SLOWREPEAT,  { -CL_KEY_ENTER,    CL_KEY_RSHIFT   } },
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    17
    {   INPUT_DIG,          INPUT_FLAG_NOREPEAT,    { CL_KEY_LEFT,      CL_KEY_RIGHT    } },
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    18
    {   INPUT_SHOOT,        INPUT_FLAG_UNLIMITED,   { CL_KEY_RCONTROL,  0               } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    19
    {   INPUT_CHANGE_PREV,  INPUT_FLAG_SLOWREPEAT,  { CL_KEY_ENTER,     CL_KEY_LEFT     } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    20
    {   INPUT_CHANGE_NEXT,  INPUT_FLAG_SLOWREPEAT,  { CL_KEY_ENTER,     CL_KEY_RIGHT    } },
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    21
    {   INPUT_ROPE,         INPUT_FLAG_NOREPEAT,    { CL_KEY_ENTER,     CL_KEY_RSHIFT   } },
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    22
    {   INPUT_UNROPE,       INPUT_FLAG_SLOWREPEAT,  { -CL_KEY_ENTER,    CL_KEY_RSHIFT   } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    23
    {   INPUT_ROPE_UP,      INPUT_FLAG_UNLIMITED,   { CL_KEY_ENTER,     CL_KEY_UP       } },
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    24
    {   INPUT_ROPE_DOWN,    INPUT_FLAG_UNLIMITED,   { CL_KEY_ENTER,     CL_KEY_DOWN     } },
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    25
    {   INPUT_SUICIDE,      INPUT_FLAG_NOREPEAT,    { CL_KEY_LCONTROL,  CL_KEY_K        } },
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    26
    {   INPUT_NONE,         0,                      { 0,                0               } }
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    27
};
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    28
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    29
InputKeymapEntry<GuiInputBit> INPUT_GUI_KEYMAP[] = {
389
e74c1820fbd2 implement --help, --fullscreen, --resolution and --list-modes
Tero Marttila <terom@fixme.fi>
parents: 319
diff changeset
    30
    {   GUI_INPUT_QUIT,                 0,                  { CL_KEY_ESCAPE,    0               } },
e74c1820fbd2 implement --help, --fullscreen, --resolution and --list-modes
Tero Marttila <terom@fixme.fi>
parents: 319
diff changeset
    31
    {   GUI_INPUT_DISPLAY_WEAPON,       0,                  { CL_KEY_ENTER,     0               } },
e74c1820fbd2 implement --help, --fullscreen, --resolution and --list-modes
Tero Marttila <terom@fixme.fi>
parents: 319
diff changeset
    32
    {   GUI_INPUT_DEBUG_PLAYER,         0,                  { CL_KEY_I,         0               } },
e74c1820fbd2 implement --help, --fullscreen, --resolution and --list-modes
Tero Marttila <terom@fixme.fi>
parents: 319
diff changeset
    33
    {   GUI_INPUT_TOGGLE_FULLSCREEN,    INPUT_FLAG_NOREPEAT,{ CL_KEY_LCONTROL,  CL_KEY_F        } },
e74c1820fbd2 implement --help, --fullscreen, --resolution and --list-modes
Tero Marttila <terom@fixme.fi>
parents: 319
diff changeset
    34
    {   GUI_INPUT_NONE,                 0,                  { 0,                0,              } }
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    35
};
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    36
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    37
/*
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    38
 * InputKeyRepeatEntry
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    39
 */
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    40
template <typename BitEnumType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    41
InputKeyRepeatEntry<BitEnumType>::InputKeyRepeatEntry (BitEnumType value, TimeMS expire) : 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    42
    value(value), expire(expire) 
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    43
{
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    44
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    45
}
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    46
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    47
template <typename BitEnumType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    48
bool InputKeyRepeatEntry<BitEnumType>::operator< (const struct InputKeyRepeatEntry &other) {
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    49
    return other.expire > expire;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    50
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    51
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    52
template <typename BitEnumType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    53
bool InputKeyRepeatEntry<BitEnumType>::updateExpired (TimeMS dt) {
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    54
    if (expire == 0)
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    55
        return false;
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    56
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    57
    expire -= dt;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    58
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    59
    return (expire <= 0);
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    60
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    61
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    62
/*
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    63
 * InputKeyRepeatQueue
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    64
 */
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    65
template <typename BitEnumType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    66
InputKeyRepeatQueue<BitEnumType>::InputKeyRepeatQueue (TimeMS expire) :
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    67
    expire(expire)
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    68
{
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    69
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    70
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    71
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    72
template <typename BitEnumType> 
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    73
void InputKeyRepeatQueue<BitEnumType>::push (BitEnumType bit, bool expire) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    74
    list.push_back(InputKeyRepeatEntry<BitEnumType>(bit, expire ? this->expire : 0));
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    75
}
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    76
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    77
template <typename BitEnumType> 
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    78
void InputKeyRepeatQueue<BitEnumType>::forget (BitEnumType bit) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    79
    // go through the list, looking for it
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    80
    for (list_iterator it = list.begin(); it != list.end(); it++) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    81
        if (it->value == bit) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    82
            // found, erase it and return
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    83
            list.erase(it);
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    84
            
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    85
            return;
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    86
        }
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
    87
    }
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    88
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    89
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    90
template <typename BitEnumType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    91
bool InputKeyRepeatQueue<BitEnumType>::find (BitEnumType bit) {
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    92
    for (list_iterator it = list.begin(); it != list.end(); it++) {
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    93
        if (it->value == bit)
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    94
            return true;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    95
    }
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    96
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    97
    return false;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    98
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
    99
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   100
template <typename BitEnumType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   101
void InputKeyRepeatQueue<BitEnumType>::update (TimeMS dt) {
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   102
    list_iterator it = list.begin();
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   103
    
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   104
    // go through each entry, updateExpired and remove if expired
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   105
    while (it != list.end()) {
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   106
        if (it->updateExpired(dt))
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   107
            it = list.erase(it);
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   108
        else
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   109
            it++;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   110
    }
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   111
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   112
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   113
template <typename BitEnumType> 
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   114
void InputKeyRepeatQueue<BitEnumType>::clear (void) {
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   115
    // just clear our list of events
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   116
    list.clear();
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   117
}
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   118
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   119
/*
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   120
 * InputHandler
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   121
 */
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   122
template <typename BitEnumType, typename BitMaskType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   123
InputHandler<BitEnumType, BitMaskType>::InputHandler (CL_InputDevice &keyboard, InputKeymapEntry<BitEnumType> *keymap, TimeMS keyrepeat_expire) :
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   124
    keyboard(keyboard), 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   125
    keymap(keymap), 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   126
    value(0), 
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   127
    prev_value(0),
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   128
    dt(0),
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   129
    queue(keyrepeat_expire),
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   130
    _enabled(false)
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   131
{
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   132
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   133
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   134
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   135
template <typename BitEnumType, typename BitMaskType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   136
bool InputHandler<BitEnumType, BitMaskType>::checkKeycode (int keycode) {
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   137
    if (keycode > 0)
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   138
        return keyboard.get_keycode(keycode);
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   139
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   140
    else if (keycode < 0)
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   141
        return !keyboard.get_keycode(-keycode);
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   142
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   143
    else // == 0
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   144
        return true;
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
   145
}
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   146
 
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   147
template <typename BitEnumType, typename BitMaskType> 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   148
void InputHandler<BitEnumType, BitMaskType>::update (TimeMS dt) {
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   149
    // ignore if not enabled
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   150
    if (!_enabled)
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   151
        return;
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   152
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   153
    // all bits that are held down, even those ignored
416
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   154
    BitMaskType raw_value = 0, this_value = 0;
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   155
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   156
    // update the key-repeat queue
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   157
    queue.update(dt);
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   158
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   159
    // then go through the keymap
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   160
    for (InputKeymapEntry<BitEnumType> *e = keymap; e->input != 0; e++) {
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   161
        // check if we've got the correct keycodes
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   162
        if (checkKeycode(e->keycodes[0]) && checkKeycode(e->keycodes[1])) {
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   163
            // set raw_value
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   164
            raw_value |= e->input;
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   165
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   166
            if (e->flags & INPUT_FLAG_SLOWREPEAT) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   167
                // repeat, but slowly
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   168
                if (!(prev_value & e->input)) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   169
                    // we've released the key earlier, move it to the back of the queue
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   170
                    queue.forget(e->input);
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   171
                    queue.push(e->input);
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   172
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   173
                } else if (queue.find(e->input)) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   174
                    // it's still in the queue, so ignore, but set it in ignore_value
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   175
                    continue;
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   176
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   177
                } else {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   178
                    // ok, but add it to the queue
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   179
                    queue.push(e->input);
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   180
                }
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   181
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   182
            } else if (e->flags & INPUT_FLAG_NOREPEAT) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   183
                // do not repeat at all
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   184
                if (prev_value & e->input) {
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   185
                    // ignore repeats
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   186
                    continue;
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   187
                }
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   188
            }
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   189
416
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   190
            // set bit in value mask
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   191
            this_value |= e->input;
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   192
        }
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   193
    }
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   194
416
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   195
    // signal unless value was and remains zero
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   196
    if (this_value || prev_value) {
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   197
        // trigger signal
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   198
        _sig_input(this_value, dt);
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   199
    } 
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   200
319
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   201
    // update prev_value, also adding ignored values
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   202
    prev_value = raw_value;
9f6a838d58c4 improve input handling further, ROPE_THROW and DIG don't repeat at all now
terom
parents: 311
diff changeset
   203
416
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   204
    // update our collective value + dt for use with readValue
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   205
    // XXX: remove this functionality?
38cba347a3a9 clean up InputHandler/GameView to use signals for input
Tero Marttila <terom@fixme.fi>
parents: 414
diff changeset
   206
    value |= this_value;
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   207
    this->dt += dt;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   208
}
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   209
 
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   210
template <typename BitEnumType, typename BitMaskType> 
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   211
void InputHandler<BitEnumType, BitMaskType>::readValue (BitMaskType &mask, TimeMS &dt) {
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   212
    // throw exception if disabled
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   213
    if (!_enabled)
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   214
        throw Error("InputHandler is disabled");
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   215
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   216
    // copy to args
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   217
    mask = this->value;
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   218
    dt = this->dt;
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   219
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   220
    this->value = 0;
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   221
    this->dt = 0;
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   222
}
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   223
       
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   224
/**
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   225
 * Input
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   226
 */
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   227
Input::Input (CL_InputDevice &keyboard) :
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   228
    keyboard(keyboard),
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   229
    update_timer(INPUT_POLL_INTERVAL),
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   230
    player(keyboard, INPUT_PLAYER_KEYMAP, INPUT_REPEAT_DELAY),
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   231
    gui(keyboard, INPUT_GUI_KEYMAP, INPUT_REPEAT_DELAY)
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   232
{
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   233
    // connect timer 
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   234
    slots.connect(update_timer.sig_tick(), &player, &InputHandler<PlayerInputBit, PlayerInput>::update);
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   235
    slots.connect(update_timer.sig_tick(), &gui, &InputHandler<GuiInputBit, GuiInput>::update);
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   236
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   237
    // enable timer
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   238
    update_timer.start();
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   239
}
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   240
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   241
void Input::readPlayerInput (PlayerInput &mask, TimeMS &dt) {
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   242
    player.readValue(mask, dt);
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
   243
}
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
   244
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   245
GuiInput Input::readGuiInput (void) {
311
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   246
    GuiInput mask;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   247
    TimeMS dt;
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   248
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   249
    gui.readValue(mask, dt);
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   250
440763821484 make Input have its own timer, and add key-repeat handling, and fix some warnings
terom
parents: 300
diff changeset
   251
    return mask;
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
   252
}
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
   253
414
cede5463b845 port Input to new Graphics, still a bit hacky, but everything seems to work now
Tero Marttila <terom@fixme.fi>
parents: 389
diff changeset
   254
}