Bridges-C++  3.1.1
Bridges(C++API)
InputHelper.h
Go to the documentation of this file.
1 #ifndef INPUTHELPER_GAME_H
2 #define INPUTHELPER_GAME_H
3 
4 #include <GameBase.h>
5 
6 namespace bridges {
7  namespace game {
8 
10  class InputHelper: public KeypressListener {
11  bool debug = false;
12 
13  bool up = false;
14  bool down = false;
15  bool right = false;
16  bool left = false;
17  bool w = false;
18  bool a = false;
19  bool s = false;
20  bool d = false;
21  bool q = false;
22  bool space = false;
23 
24 
25  private:
26  void handleKey(std::string JSONmessage) {
27  using namespace rapidjson;
28  // ...
29  rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> alloc;
30  Document msg(&alloc);
31  msg.Parse(JSONmessage.c_str());
32 
33  std::string type = msg["type"].GetString();
34 
35  bool setto = (type.compare("keydown") == 0);
36 
37  std::string key = msg["key"].GetString();
38 
39  if (key.compare("ArrowUp") == 0)
40  up = setto;
41  else if (key.compare("ArrowDown") == 0)
42  down = setto;
43  else if (key.compare("ArrowLeft") == 0)
44  left = setto;
45  else if (key.compare("ArrowRight") == 0)
46  right = setto;
47  else if (key.compare("w") == 0)
48  w = setto;
49  else if (key.compare("a") == 0)
50  a = setto;
51  else if (key.compare("s") == 0)
52  s = setto;
53  else if (key.compare("d") == 0)
54  d = setto;
55  else if (key.compare("q") == 0)
56  q = setto;
57  else if (key.compare(" ") == 0)
58  space = setto;
59  }
60 
61 
62  protected:
63  virtual void keyup(std::string JSONmessage) override {
64  if (debug)
65  std::cerr << "InputHelper::keyup(\"" << JSONmessage << "\")\n";
66  handleKey(JSONmessage);
67  }
68 
69  virtual void keydown(std::string JSONmessage) override {
70  if (debug)
71  std::cerr << "InputHelper::keydown(\"" << JSONmessage << "\")\n";
72  handleKey(JSONmessage);
73  }
74 
75 
76  public:
77 
78  bool keyUp() const {
79  return up;
80  }
81 
82  bool keyDown() const {
83  return down;
84  }
85 
86  bool keyLeft() const {
87  return left;
88  }
89 
90  bool keyRight() const {
91  return right;
92  }
93 
94  bool keyW() const {
95  return w;
96  }
97  bool keyA() const {
98  return a;
99  }
100  bool keyS() const {
101  return s;
102  }
103  bool keyD() const {
104  return d;
105  }
106  bool keyQ() const {
107  return q;
108  }
109  bool keySpace() const {
110  return space;
111  }
112 
113  };
114 
115  }
116 }
117 #endif
virtual void keydown(std::string JSONmessage) override
Definition: InputHelper.h:69
bool keyD() const
Definition: InputHelper.h:103
bool keySpace() const
Definition: InputHelper.h:109
virtual void keyup(std::string JSONmessage) override
Definition: InputHelper.h:63
bool keyUp() const
Definition: InputHelper.h:78
bool keyA() const
Definition: InputHelper.h:97
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
bool keyQ() const
Definition: InputHelper.h:106
bool keyDown() const
Definition: InputHelper.h:82
bool keyS() const
Definition: InputHelper.h:100
this is meant to be an internal class, not something that the library user will use ...
Definition: InputHelper.h:10
bool keyW() const
Definition: InputHelper.h:94
bool keyRight() const
Definition: InputHelper.h:90
bool keyLeft() const
Definition: InputHelper.h:86
Definition: SocketConnection.h:20