Input.hpp
Go to the documentation of this file.
1 
2 
3 
4 #ifndef GOSU_INPUT_HPP
5 #define GOSU_INPUT_HPP
6 
7 #include <Gosu/Fwd.hpp>
8 #include <Gosu/Platform.hpp>
9 
10 #ifdef GOSU_IS_WIN
11 #include <Gosu/Buttons.hpp>
12 #ifndef NOMINMAX
13 #define NOMINMAX
14 #endif
15 #include <windows.h>
16 #endif
17 
18 #ifdef GOSU_IS_MAC
19 #include <Gosu/ButtonsMac.hpp>
20 #endif
21 
22 #ifdef GOSU_IS_X
23 #include <Gosu/Buttons.hpp>
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #endif
27 
28 #include <Gosu/Platform.hpp>
29 #include <Gosu/Fwd.hpp>
30 #include <Gosu/TR1.hpp>
31 #include <vector>
32 
33 namespace Gosu
34 {
36  class Button
37  {
38  unsigned id_;
39 
40  public:
42  explicit Button(unsigned id) : id_(id) {}
44  unsigned id() const { return id_; }
45 
47  Button() : id_(noButton) {}
48 
50  Button(ButtonName name) : id_(name) {}
51  };
52 
54  inline bool operator==(Button lhs, Button rhs)
55  {
56  return lhs.id() == rhs.id();
57  }
58  inline bool operator!=(Button lhs, Button rhs)
59  {
60  return !(lhs == rhs);
61  }
62  inline bool operator<(Button lhs, Button rhs)
63  {
64  return lhs.id() < rhs.id();
65  }
66 
71  struct Touch
72  {
74  void* id;
76  float x, y;
77  };
78  typedef std::vector<Touch> Touches;
79 
82  class Input
83  {
84  struct Impl;
85  const std::auto_ptr<Impl> pimpl;
86 
87  public:
88  #ifdef GOSU_IS_WIN
89  Input(HWND window);
90  #endif
91 
92  #ifdef GOSU_IS_MAC
93  #ifdef GOSU_IS_IPHONE
94  Input(void* view, float updateInterval);
95  void feedTouchEvent(int type, void* touches);
96  #else
97  Input(void* window);
98  bool feedNSEvent(void* event);
99  #endif
100  #endif
101 
102  #ifdef GOSU_IS_X
103  Input(::Display* display, ::Window window);
104  bool feedXEvent(::XEvent& event);
105  #endif
106 
107  ~Input();
108 
110  static wchar_t idToChar(Button btn);
113  static Button charToId(wchar_t ch);
114 
117  bool down(Button btn) const;
118 
121  double mouseX() const;
123  double mouseY() const;
124 
128  void setMousePosition(double x, double y);
129 
130  // Undocumented for the moment. Also applies to currentTouches().
131  void setMouseFactors(double factorX, double factorY);
132 
134  const Touches& currentTouches() const;
135 
137  double accelerometerX() const;
138  double accelerometerY() const;
139  double accelerometerZ() const;
140 
143  void update();
144 
147  std::tr1::function<void (Button)> onButtonDown, onButtonUp;
148 
151  std::tr1::function<void (Touch)> onTouchBegan, onTouchMoved, onTouchEnded;
152 
154  TextInput* textInput() const;
156  void setTextInput(TextInput* input);
157  };
158 }
159 
160 #endif