~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/input/inputmanager.h

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  The ManaPlus Client
 
3
 *  Copyright (C) 2012-2013  The ManaPlus Developers
 
4
 *
 
5
 *  This file is part of The ManaPlus Client.
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#ifndef INPUT_INPUTMANAGER_H
 
22
#define INPUT_INPUTMANAGER_H
 
23
 
 
24
#include "input/inputevent.h"
 
25
#include "input/keydata.h"
 
26
 
 
27
#include <string>
 
28
#include <map>
 
29
 
 
30
#include <SDL_events.h>
 
31
 
 
32
const unsigned int KeyFunctionSize = 3;
 
33
 
 
34
// hack to avoid conflicts with windows headers.
 
35
#ifdef INPUT_KEYBOARD
 
36
#undef INPUT_KEYBOARD
 
37
#endif
 
38
#ifdef INPUT_MOUSE
 
39
#undef INPUT_MOUSE
 
40
#endif
 
41
 
 
42
class Setup_Input;
 
43
 
 
44
enum KeyTypes
 
45
{
 
46
    INPUT_UNKNOWN = 0,
 
47
    INPUT_KEYBOARD = 1,
 
48
    INPUT_MOUSE = 2,
 
49
    INPUT_JOYSTICK = 3
 
50
};
 
51
 
 
52
struct KeyItem final
 
53
{
 
54
    KeyItem() :
 
55
        type(-1), value(-1)
 
56
    { }
 
57
 
 
58
    KeyItem(const int type0, const int value0) :
 
59
        type(type0), value(value0)
 
60
    { }
 
61
 
 
62
    int type;
 
63
 
 
64
    int value;
 
65
};
 
66
 
 
67
struct KeyFunction final
 
68
{
 
69
    KeyItem values[KeyFunctionSize];
 
70
};
 
71
 
 
72
enum KeyCondition
 
73
{
 
74
    COND_DEFAULT = 1,                         // default condition
 
75
    COND_ENABLED = 2,                         // keyboard must be enabled
 
76
    COND_NOINPUT = 4,                         // input items must be unfocused
 
77
    COND_NOAWAY = 8,                          // player not in away mode
 
78
    COND_NOSETUP = 16,                        // setup window is hidde
 
79
    COND_VALIDSPEED = 32,                     // valid speed
 
80
    COND_NOMODAL = 64,                        // modal windows inactive
 
81
    COND_NONPCINPUT = 128,                    // npc input field inactive
 
82
    COND_EMODS = 256,                         // game modifiers enabled
 
83
    COND_NOTARGET = 512,                      // no target/untarget keys
 
84
                                              // pressed
 
85
    COND_NOFOLLOW = 1024,                     // follow mode disabled
 
86
    COND_INGAME = 2048,                       // game must be started
 
87
    COND_SHORTCUT = 2 + 4 + 16 + 512 + 2048,  // flags for shortcut keys
 
88
    COND_SHORTCUT0 = 2 + 4 + 16 + 512,        // flags for shortcut keys
 
89
    COND_GAME = 2 + 4 + 8 + 16 + 64 + 2048,   // main game key
 
90
    COND_GAME2 = 2 + 8 + 16 + 64 + 2048
 
91
};
 
92
 
 
93
class InputManager final
 
94
{
 
95
    public:
 
96
        InputManager();
 
97
 
 
98
        A_DELETE_COPY(InputManager)
 
99
 
 
100
        void init();
 
101
 
 
102
        bool handleEvent(const SDL_Event &event);
 
103
 
 
104
        bool checkKey(const KeyData *const key) const A_WARN_UNUSED;
 
105
 
 
106
        void retrieve();
 
107
 
 
108
        void store() const;
 
109
 
 
110
        void resetKeys();
 
111
 
 
112
        void makeDefault(const int i);
 
113
 
 
114
        bool hasConflicts(int &key1, int &key2) const A_WARN_UNUSED;
 
115
 
 
116
        void callbackNewKey();
 
117
 
 
118
        KeyFunction &getKey(int index) A_WARN_UNUSED;
 
119
 
 
120
        std::string getKeyValueString(const int index) const A_WARN_UNUSED;
 
121
 
 
122
        std::string getKeyStringLong(const int index) const A_WARN_UNUSED;
 
123
 
 
124
        std::string getKeyValueByName(const std::string &keyName);
 
125
 
 
126
        void addActionKey(const int action, const int type, const int val);
 
127
 
 
128
        void setNewKey(const SDL_Event &event, const int type);
 
129
 
 
130
        void unassignKey();
 
131
 
 
132
        static bool isActionActive(const int index) A_WARN_UNUSED;
 
133
 
 
134
        /**
 
135
         * Set the index of the new key to be assigned.
 
136
         */
 
137
        void setNewKeyIndex(const int value)
 
138
        { mNewKeyIndex = value; }
 
139
 
 
140
        /**
 
141
         * Set a reference to the key setup window.
 
142
         */
 
143
        void setSetupInput(Setup_Input *const setupInput)
 
144
        { mSetupInput = setupInput; }
 
145
 
 
146
        /**
 
147
         * Get the index of the new key to be assigned.
 
148
         */
 
149
        int getNewKeyIndex() const A_WARN_UNUSED
 
150
        { return mNewKeyIndex; }
 
151
 
 
152
        void updateKeyActionMap(KeyToActionMap &actionMap,
 
153
                                KeyToIdMap &idMap, KeyTimeMap &keyTimeMap,
 
154
                                const int type) const;
 
155
 
 
156
        bool invokeKey(const KeyData *const key, const int keyNum);
 
157
 
 
158
        bool handleAssignKey(const SDL_Event &event, const int type);
 
159
 
 
160
        void handleRepeat() const;
 
161
 
 
162
        bool triggerAction(const KeysVector *const ptrs);
 
163
 
 
164
        int getKeyIndex(const int value, const int grp,
 
165
                        const int type) const A_WARN_UNUSED;
 
166
 
 
167
        void update() const;
 
168
 
 
169
        void updateConditionMask();
 
170
 
 
171
        int getActionByKey(const SDL_Event &event) const A_WARN_UNUSED;
 
172
 
 
173
        void executeAction(const int keyNum);
 
174
 
 
175
    protected:
 
176
        Setup_Input *mSetupInput;      /**< Reference to setup window */
 
177
 
 
178
        int mNewKeyIndex;              /**< Index of new key to be assigned */
 
179
 
 
180
        int mMask;
 
181
 
 
182
        std::map<std::string, int> mNameMap;
 
183
 
 
184
        KeyFunction mKey[Input::KEY_TOTAL]; /**< Pointer to all the key data */
 
185
};
 
186
 
 
187
extern InputManager inputManager;
 
188
 
 
189
#endif  // INPUT_INPUTMANAGER_H