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