Gosu
Input.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <Gosu/Fwd.hpp>
7 #include <Gosu/Buttons.hpp>
8 #include <Gosu/Platform.hpp>
9 #include <functional>
10 #include <memory>
11 #include <vector>
12 
13 namespace Gosu
14 {
16  class Button
17  {
18  unsigned id_;
19 
20  public:
22  explicit Button(unsigned id) : id_(id) {}
24  unsigned id() const { return id_; }
25 
27  Button() : id_(NO_BUTTON) {}
28 
30  Button(ButtonName name) : id_(name) {}
31  };
32 
34  inline bool operator==(Button lhs, Button rhs) { return lhs.id() == rhs.id(); }
35  inline bool operator!=(Button lhs, Button rhs) { return !(lhs == rhs); }
36  inline bool operator<(Button lhs, Button rhs) { return lhs.id() < rhs.id(); }
37 
42  struct Touch
43  {
45  void* id;
47  float x, y;
48  };
49  typedef std::vector<Touch> Touches;
50 
53  class Input
54  {
55  struct Impl;
56  // Non-movable (const) to avoid dangling internal references.
57  const std::unique_ptr<Impl> pimpl;
58 
59  public:
60  #ifdef GOSU_IS_IPHONE
61  Input(void* view, float update_interval);
62  void feed_touch_event(int type, void* touches);
63  #else
64  Input(void* window);
65  bool feed_sdl_event(void* event);
66  #endif
67 
68  ~Input();
69 
72  static std::string id_to_char(Button btn);
75  static Button char_to_id(std::string ch);
76 
79  static bool down(Button btn);
80 
83  double mouse_x() const;
85  double mouse_y() const;
86 
90  void set_mouse_position(double x, double y);
91 
92  // Undocumented for the moment. Also applies to current_touches().
93  void set_mouse_factors(double factor_x, double factor_y,
94  double offset_x = 0, double offset_y = 0);
95 
97  const Touches& current_touches() const;
98 
101  GOSU_DEPRECATED double accelerometer_x() const;
102  GOSU_DEPRECATED double accelerometer_y() const;
103  GOSU_DEPRECATED double accelerometer_z() const;
104 
107  void update();
108 
111  std::function<void (Button)> on_button_down, on_button_up;
112 
115  std::function<void (Touch)> on_touch_began, on_touch_moved, on_touch_ended,
116  on_touch_cancelled;
117 
119  TextInput* text_input() const;
121  void set_text_input(TextInput* input);
122  };
123 }
#define GOSU_DEPRECATED
Definition: Platform.hpp:69
The library&#39;s main namespace.
Definition: Audio.hpp:12
Manages initialization and shutdown of the input system.
Definition: Input.hpp:53
float y
Definition: Input.hpp:47
Contains forward declarations of all of Gosu&#39;s available classes.
Struct that saves information about a touch on the surface of a multi- touch device.
Definition: Input.hpp:42
std::function< void(Touch)> on_touch_moved
Definition: Input.hpp:115
Very lightweight class that identifies a button (keyboard, mouse or other device).
Definition: Input.hpp:16
std::function< void(Button)> on_button_up
Definition: Input.hpp:111
unsigned id() const
For internal use.
Definition: Input.hpp:24
std::vector< Touch > Touches
Definition: Input.hpp:49
ButtonName
List of button ids that can be used with Gosu::Input.
Definition: Buttons.hpp:10
bool operator!=(Color a, Color b)
Definition: Color.hpp:150
double offset_x(double angle, double radius)
Returns the horizontal distance between the origin and the point to which you would get if you moved ...
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:45
TextInput instances are invisible objects that build a text string from input, using the current oper...
Definition: TextInput.hpp:21
bool operator==(Color a, Color b)
Definition: Color.hpp:149
Button(unsigned id)
For internal use.
Definition: Input.hpp:22
Button()
Default constructor; == no_button.
Definition: Input.hpp:27
Button(ButtonName name)
Conversion from ButtonName constants.
Definition: Input.hpp:30
double offset_y(double angle, double radius)
Returns the vertical distance between the origin and the point to which you would get if you moved ra...
bool operator<(Color a, Color b)
Definition: Color.hpp:148