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/ButtonsWin.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 <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/keysym.h>
26 #include <Gosu/ButtonsX.hpp>
27 #endif
28 
29 #include <Gosu/Platform.hpp>
30 #include <Gosu/Fwd.hpp>
31 #include <Gosu/TR1.hpp>
32 #include <vector>
33 
34 namespace Gosu
35 {
37  class Button
38  {
39  unsigned id_;
40 
41  public:
43  explicit Button(unsigned id) : id_(id) {}
45  unsigned id() const { return id_; }
46 
48  Button() : id_(noButton) {}
49 
51  Button(ButtonName name) : id_(name) {}
52  };
53 
55  inline bool operator==(Button lhs, Button rhs)
56  {
57  return lhs.id() == rhs.id();
58  }
59  inline bool operator!=(Button lhs, Button rhs)
60  {
61  return !(lhs == rhs);
62  }
63  inline bool operator<(Button lhs, Button rhs)
64  {
65  return lhs.id() < rhs.id();
66  }
67 
72  struct Touch
73  {
75  void* id;
77  float x, y;
78  };
79  typedef std::vector<Touch> Touches;
80 
83  class Input
84  {
85  struct Impl;
86  const std::auto_ptr<Impl> pimpl;
87 
88  public:
89  #ifdef GOSU_IS_WIN
90  Input(HWND window);
91  #endif
92 
93  #ifdef GOSU_IS_MAC
94  #ifdef GOSU_IS_IPHONE
95  Input(void* view, float updateInterval);
96  void feedTouchEvent(int type, void* touches);
97  #else
98  Input(void* window);
99  bool feedNSEvent(void* event);
100  #endif
101  #endif
102 
103  #ifdef GOSU_IS_X
104  Input(::Display* display, ::Window window);
105  bool feedXEvent(::XEvent& event);
106  #endif
107 
108  ~Input();
109 
111  static wchar_t idToChar(Button btn);
114  static Button charToId(wchar_t ch);
115 
118  bool down(Button btn) const;
119 
122  double mouseX() const;
124  double mouseY() const;
125 
129  void setMousePosition(double x, double y);
130 
131  // Undocumented for the moment. Also applies to currentTouches().
132  void setMouseFactors(double factorX, double factorY);
133 
135  const Touches& currentTouches() const;
136 
138  double accelerometerX() const;
139  double accelerometerY() const;
140  double accelerometerZ() const;
141 
144  void update();
145 
148  std::tr1::function<void (Button)> onButtonDown, onButtonUp;
149 
152  std::tr1::function<void (Touch)> onTouchBegan, onTouchMoved, onTouchEnded;
153 
155  TextInput* textInput() const;
157  void setTextInput(TextInput* input);
158  };
159 }
160 
161 #endif