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