Input related generalisations
[oweals/minetest.git] / src / game.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef GAME_HEADER
21 #define GAME_HEADER
22
23 #include "irrlichttypes_extrabloated.h"
24 #include <string>
25 #include "client/keys.h"
26 #include "keycode.h"
27 #include <list>
28
29 class KeyList : protected std::list<KeyPress>
30 {
31         typedef std::list<KeyPress> super;
32         typedef super::iterator iterator;
33         typedef super::const_iterator const_iterator;
34
35         virtual const_iterator find(const KeyPress &key) const
36         {
37                 const_iterator f(begin());
38                 const_iterator e(end());
39
40                 while (f != e) {
41                         if (*f == key)
42                                 return f;
43
44                         ++f;
45                 }
46
47                 return e;
48         }
49
50         virtual iterator find(const KeyPress &key)
51         {
52                 iterator f(begin());
53                 iterator e(end());
54
55                 while (f != e) {
56                         if (*f == key)
57                                 return f;
58
59                         ++f;
60                 }
61
62                 return e;
63         }
64
65 public:
66         void clear()
67         {
68                 super::clear();
69         }
70
71         void set(const KeyPress &key)
72         {
73                 if (find(key) == end())
74                         push_back(key);
75         }
76
77         void unset(const KeyPress &key)
78         {
79                 iterator p(find(key));
80
81                 if (p != end())
82                         erase(p);
83         }
84
85         void toggle(const KeyPress &key)
86         {
87                 iterator p(this->find(key));
88
89                 if (p != end())
90                         erase(p);
91                 else
92                         push_back(key);
93         }
94
95         bool operator[](const KeyPress &key) const
96         {
97                 return find(key) != end();
98         }
99 };
100
101 class InputHandler
102 {
103 public:
104         InputHandler()
105         {
106         }
107         virtual ~InputHandler()
108         {
109         }
110
111         virtual bool isKeyDown(const KeyPress &keyCode) = 0;
112         virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
113
114         virtual void listenForKey(const KeyPress &keyCode) {}
115         virtual void dontListenForKeys() {}
116
117         virtual v2s32 getMousePos() = 0;
118         virtual void setMousePos(s32 x, s32 y) = 0;
119
120         virtual bool getLeftState() = 0;
121         virtual bool getRightState() = 0;
122
123         virtual bool getLeftClicked() = 0;
124         virtual bool getRightClicked() = 0;
125         virtual void resetLeftClicked() = 0;
126         virtual void resetRightClicked() = 0;
127
128         virtual bool getLeftReleased() = 0;
129         virtual bool getRightReleased() = 0;
130         virtual void resetLeftReleased() = 0;
131         virtual void resetRightReleased() = 0;
132
133         virtual s32 getMouseWheel() = 0;
134
135         virtual void step(float dtime) {}
136
137         virtual void clear() {}
138 };
139
140 class ChatBackend;  /* to avoid having to include chat.h */
141 struct SubgameSpec;
142
143 void the_game(bool *kill,
144                 bool random_input,
145                 InputHandler *input,
146                 IrrlichtDevice *device,
147                 const std::string &map_dir,
148                 const std::string &playername,
149                 const std::string &password,
150                 const std::string &address, // If "", local server is used
151                 u16 port,
152                 std::string &error_message,
153                 ChatBackend &chat_backend,
154                 bool *reconnect_requested,
155                 const SubgameSpec &gamespec, // Used for local game
156                 bool simple_singleplayer_mode);
157
158 #endif
159