GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/input/inputmanager.h Lines: 2 4 50.0 %
Date: 2021-03-17 Branches: 3 4 75.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2012-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#ifndef INPUT_INPUTMANAGER_H
23
#define INPUT_INPUTMANAGER_H
24
25
#include "input/inputfunction.h"
26
27
#include "events/inputevent.h"
28
29
#include "utils/cast.h"
30
#include "utils/stringmap.h"
31
32
#include <list>
33
34
PRAGMA48(GCC diagnostic push)
35
PRAGMA48(GCC diagnostic ignored "-Wshadow")
36
#include <SDL_events.h>
37
PRAGMA48(GCC diagnostic pop)
38
39
class Setup_Input;
40
41
struct InputActionData;
42
43
typedef std::map<std::string, InputActionT> StringInpActionMap;
44
typedef StringInpActionMap::const_iterator StringInpActionMapCIter;
45
46

4
class InputManager final
47
{
48
    public:
49
        InputManager();
50
51
        A_DELETE_COPY(InputManager)
52
53
        void init() restrict2;
54
55
        bool handleEvent(const SDL_Event &restrict event) restrict2;
56
57
        bool checkKey(const InputActionData *restrict const key) const
58
                      restrict2 A_WARN_UNUSED;
59
60
        void retrieve() restrict2;
61
62
        void store() const restrict2;
63
64
        void resetKeys() restrict2;
65
66
        void makeDefault(const InputActionT i) restrict2;
67
68
        bool hasConflicts(InputActionT &restrict key1,
69
                          InputActionT &restrict key2) const
70
                          restrict2 A_WARN_UNUSED;
71
72
        void callbackNewKey() restrict2;
73
74
        InputFunction &getKey(InputActionT index)
75
                              restrict2 A_CONST A_WARN_UNUSED;
76
77
        std::string getKeyValueString(const InputActionT index)
78
                                      const restrict2 A_WARN_UNUSED;
79
80
        std::string getKeyStringLong(const InputActionT index)
81
                                     const restrict2 A_WARN_UNUSED;
82
83
        std::string getKeyValueByName(const std::string &restrict keyName)
84
                                      restrict2;
85
86
        std::string getKeyValueByNameLong(const std::string &restrict keyName)
87
                                          restrict2;
88
89
        void addActionKey(const InputActionT action,
90
                          const InputTypeT type,
91
                          const int val) restrict2;
92
93
        void setNewKey(const SDL_Event &event,
94
                       const InputTypeT type) restrict2;
95
96
        void unassignKey() restrict2;
97
98
        bool isActionActive(const InputActionT index) const
99
                            restrict2 A_WARN_UNUSED;
100
101
        /**
102
         * Set the index of the new key to be assigned.
103
         */
104
        void setNewKeyIndex(const InputActionT value) restrict2 noexcept2
105
        { mNewKeyIndex = value; }
106
107
        /**
108
         * Set a reference to the key setup window.
109
         */
110
        void setSetupInput(Setup_Input *restrict const setupInput)
111
                           restrict2 noexcept2 A_NONNULL(2)
112
2
        { mSetupInput = setupInput; }
113
114
        /**
115
         * Get the index of the new key to be assigned.
116
         */
117
        InputActionT getNewKeyIndex() const restrict2 noexcept2 A_WARN_UNUSED
118
        { return mNewKeyIndex; }
119
120
        void updateKeyActionMap(KeyToActionMap &restrict actionMap,
121
                                KeyToIdMap &restrict idMap,
122
                                KeyTimeMap &restrict keyTimeMap,
123
                                const InputTypeT type) const restrict2;
124
125
        bool invokeKey(const InputActionData *restrict const key,
126
                       const InputActionT keyNum) restrict2;
127
128
        bool handleAssignKey(const SDL_Event &restrict event,
129
                             const InputTypeT type) restrict2;
130
131
        static void handleRepeat();
132
133
        bool triggerAction(const KeysVector *restrict const ptrs) restrict2;
134
135
        InputActionT getKeyIndex(const int value,
136
                                 const int grp,
137
                                 const InputTypeT type) const
138
                                 restrict2 A_WARN_UNUSED;
139
140
        static void update();
141
142
        void updateConditionMask(const bool pressed) restrict2;
143
144
        InputActionT getActionByKey(const SDL_Event &restrict event)
145
                                    const restrict2 A_WARN_UNUSED;
146
147
        static InputActionT getActionByConfigField(const std::string &field);
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
        static void addChatCommands(std::list<std::string> &restrict arr);
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