Input.hpp
Go to the documentation of this file.
1 
4 #ifndef GOSU_INPUT_HPP
5 #define GOSU_INPUT_HPP
6 
7 #include <Gosu/Fwd.hpp>
8 #include <Gosu/Platform.hpp>
9 #include <Gosu/Buttons.hpp>
10 #include <Gosu/TR1.hpp>
11 
12 #ifdef GOSU_IS_WIN
13 #ifndef NOMINMAX
14 #define NOMINMAX
15 #endif
16 #include <windows.h>
17 #endif
18 
19 #ifdef GOSU_IS_X
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #endif
23 
24 #include <vector>
25 
26 namespace Gosu
27 {
29  class Button
30  {
31  unsigned id_;
32 
33  public:
35  explicit Button(unsigned id) : id_(id) {}
37  unsigned id() const { return id_; }
38 
40  Button() : id_(noButton) {}
41 
43  Button(ButtonName name) : id_(name) {}
44  };
45 
47  inline bool operator==(Button lhs, Button rhs)
48  {
49  return lhs.id() == rhs.id();
50  }
51  inline bool operator!=(Button lhs, Button rhs)
52  {
53  return !(lhs == rhs);
54  }
55  inline bool operator<(Button lhs, Button rhs)
56  {
57  return lhs.id() < rhs.id();
58  }
59 
64  struct Touch
65  {
67  void* id;
69  float x, y;
70  };
71  typedef std::vector<Touch> Touches;
72 
75  class Input
76  {
77  struct Impl;
78  const GOSU_UNIQUE_PTR<Impl> pimpl;
79 #if defined(GOSU_CPP11_ENABLED)
80  // explicitly forbid copying and moving
81  Input(Input&&) = delete;
82  Input& operator=(Input&&) = delete;
83  Input(const Input&) = delete;
84  Input& operator=(const Input&) = delete;
85 #endif
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
bool down(Button btn) const
Returns true if a button is currently pressed.
Includes all parts of C++03 (TR1) that are relevant for Gosu.
double accelerometerX() const
Accelerometer positions in all three dimensions (smoothened).
static wchar_t idToChar(Button btn)
Returns the character a button usually produces, or 0.
void setTextInput(TextInput *input)
Sets the currently active TextInput, or clears it (input = 0).
Convenient all-in-one class that serves as the foundation of a standard Gosu application.
Definition: Window.hpp:28
Manages initialization and shutdown of the input system.
Definition: Input.hpp:75
float y
Definition: Input.hpp:69
Contains declarations of all of Gosu&#39;s available classes.
std::tr1::function< void(Touch)> onTouchMoved
Definition: Input.hpp:151
Struct that saves information about a touch on the surface of a multi- touch device.
Definition: Input.hpp:64
const Touches & currentTouches() const
Currently known touches.
double accelerometerZ() const
void setMouseFactors(double factorX, double factorY)
unsigned id() const
For internal use.
Definition: Input.hpp:37
Very lightweight class that identifies a button (keyboard, mouse or other device).
Definition: Input.hpp:29
std::tr1::function< void(Touch)> onTouchBegan
Assignable events that are called by update.
Definition: Input.hpp:151
std::vector< Touch > Touches
Definition: Input.hpp:71
ButtonName
List of button ids that can be used with Gosu::Input.
Definition: Buttons.hpp:23
float x
Position of a touch on the touch screen.
Definition: Input.hpp:69
bool operator!=(Color a, Color b)
Definition: Color.hpp:172
double mouseY() const
See mouseX.
double mouseX() const
Returns the horizontal position of the mouse relative to the top left corner of the window given to I...
static Button charToId(wchar_t ch)
Returns the button that has to be pressed to produce the given character, or noButton.
std::tr1::function< void(Button)> onButtonDown
Assignable events that are called by update.
Definition: Input.hpp:147
Macros and utility functions to facilitate programming on all of Gosu&#39;s supported platforms...
void * id
Allows for identification of a touch across calls.
Definition: Input.hpp:67
std::tr1::function< void(Touch)> onTouchEnded
Definition: Input.hpp:151
TextInput instances are invisible objects that build a text string from input, using the current oper...
Definition: TextInput.hpp:23
bool operator==(Color a, Color b)
Definition: Color.hpp:167
Button(unsigned id)
For internal use.
Definition: Input.hpp:35
TextInput * textInput() const
Returns the currently active TextInput instance, or 0.
Button()
Default constructor; == noButton.
Definition: Input.hpp:40
void setMousePosition(double x, double y)
Immediately moves the mouse as far towards the desired position as possible.
Button(ButtonName name)
Conversion from ButtonName constants.
Definition: Input.hpp:43
void update()
Collects new information about which buttons are pressed, where the mouse is and calls onButtonUp/onB...
std::tr1::function< void(Button)> onButtonUp
Definition: Input.hpp:147
double accelerometerY() const
bool operator<(Color a, Color b)
Definition: Color.hpp:162