GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/input/inputmanager.h Lines: 2 4 50.0 %
Date: 2017-11-29 Branches: 2 2 100.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2017  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/inputfunction.h"
25
26
#include "events/inputevent.h"
27
28
#include "utils/cast.h"
29
#include "utils/stringmap.h"
30
31
#include <list>
32
33
PRAGMA48(GCC diagnostic push)
34
PRAGMA48(GCC diagnostic ignored "-Wshadow")
35
#include <SDL_events.h>
36
PRAGMA48(GCC diagnostic pop)
37
38
class Setup_Input;
39
40
struct InputActionData;
41
42
typedef std::map<std::string, InputActionT> StringInpActionMap;
43
typedef StringInpActionMap::const_iterator StringInpActionMapCIter;
44
45
8
class InputManager final
46
{
47
    public:
48
        InputManager();
49
50
        A_DELETE_COPY(InputManager)
51
52
        void init() restrict2;
53
54
        bool handleEvent(const SDL_Event &restrict event) restrict2;
55
56
        bool checkKey(const InputActionData *restrict const key) const
57
                      restrict2 A_WARN_UNUSED;
58
59
        void retrieve() restrict2;
60
61
        void store() const restrict2;
62
63
        void resetKeys() restrict2;
64
65
        void makeDefault(const InputActionT i) restrict2;
66
67
        bool hasConflicts(InputActionT &restrict key1,
68
                          InputActionT &restrict key2) const
69
                          restrict2 A_WARN_UNUSED;
70
71
        void callbackNewKey() restrict2;
72
73
        InputFunction &getKey(InputActionT index)
74
                              restrict2 A_CONST A_WARN_UNUSED;
75
76
        std::string getKeyValueString(const InputActionT index)
77
                                      const restrict2 A_WARN_UNUSED;
78
79
        std::string getKeyStringLong(const InputActionT index)
80
                                     const restrict2 A_WARN_UNUSED;
81
82
        std::string getKeyValueByName(const std::string &restrict keyName)
83
                                      restrict2;
84
85
        std::string getKeyValueByNameLong(const std::string &restrict keyName)
86
                                          restrict2;
87
88
        void addActionKey(const InputActionT action,
89
                          const InputTypeT type,
90
                          const int val) restrict2;
91
92
        void setNewKey(const SDL_Event &event,
93
                       const InputTypeT type) restrict2;
94
95
        void unassignKey() restrict2;
96
97
        bool isActionActive(const InputActionT index) const
98
                            restrict2 A_WARN_UNUSED;
99
100
        /**
101
         * Set the index of the new key to be assigned.
102
         */
103
        void setNewKeyIndex(const InputActionT value) restrict2 noexcept2
104
        { mNewKeyIndex = value; }
105
106
        /**
107
         * Set a reference to the key setup window.
108
         */
109
        void setSetupInput(Setup_Input *restrict const setupInput)
110
                           restrict2 noexcept2 A_NONNULL(2)
111
4
        { mSetupInput = setupInput; }
112
113
        /**
114
         * Get the index of the new key to be assigned.
115
         */
116
        InputActionT getNewKeyIndex() const restrict2 noexcept2 A_WARN_UNUSED
117
        { return mNewKeyIndex; }
118
119
        void updateKeyActionMap(KeyToActionMap &restrict actionMap,
120
                                KeyToIdMap &restrict idMap,
121
                                KeyTimeMap &restrict keyTimeMap,
122
                                const InputTypeT type) const restrict2;
123
124
        bool invokeKey(const InputActionData *restrict const key,
125
                       const InputActionT keyNum) restrict2;
126
127
        bool handleAssignKey(const SDL_Event &restrict event,
128
                             const InputTypeT type) restrict2;
129
130
        static void handleRepeat();
131
132
        bool triggerAction(const KeysVector *restrict const ptrs) restrict2;
133
134
        InputActionT getKeyIndex(const int value,
135
                                 const int grp,
136
                                 const InputTypeT type) const
137
                                 restrict2 A_WARN_UNUSED;
138
139
        static void update();
140
141
        void updateConditionMask(const bool pressed) restrict2;
142
143
        InputActionT getActionByKey(const SDL_Event &restrict event)
144
                                    const restrict2 A_WARN_UNUSED;
145
146
        InputActionT getActionByConfigField(const std::string &field)
147
                                            const restrict2 A_WARN_UNUSED;
148
149
        void executeAction(const InputActionT keyNum) restrict2;
150
151
        bool executeChatCommand(const std::string &restrict cmd,
152
                                const std::string &restrict args,
153
                                ChatTab *restrict const tab) restrict2;
154
155
        bool executeRemoteChatCommand(const std::string &restrict cmd,
156
                                      const std::string &restrict args,
157
                                      ChatTab *restrict const tab) restrict2;
158
159
        bool executeChatCommand(const InputActionT keyNum,
160
                                const std::string &restrict args,
161
                                ChatTab *restrict const tab) restrict2;
162
163
        void addChatCommands(std::list<std::string> &restrict arr) restrict;
164
165
    protected:
166
        void resetKey(const InputActionT i) restrict2;
167
168
        static bool isActionActive0(const InputActionT index) A_WARN_UNUSED;
169
170
        void updateKeyString(const InputFunction &ki,
171
                             const size_t actionIdx) restrict2;
172
173
        /** Reference to setup window */
174
        Setup_Input *mSetupInput A_NONNULLPOINTER;
175
        /** Index of new key to be assigned */
176
        InputActionT mNewKeyIndex;
177
178
        int mMask;
179
180
        StringInpActionMap mNameMap;
181
        StringIntMap mChatMap;
182
183
        InputFunction mKey[CAST_SIZE(InputAction::TOTAL)];
184
        std::string mKeyStr[CAST_SIZE(InputAction::TOTAL)];
185
};
186
187
extern InputManager inputManager;
188
189
#endif  // INPUT_INPUTMANAGER_H