Input.hpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 #ifndef GOSU_INPUT_HPP
00005 #define GOSU_INPUT_HPP
00006 
00007 #include <Gosu/Fwd.hpp>
00008 #include <Gosu/Platform.hpp>
00009 
00010 #ifdef GOSU_IS_WIN
00011 #include <Gosu/ButtonsWin.hpp>
00012 #ifndef NOMINMAX
00013 #define NOMINMAX
00014 #endif
00015 #include <windows.h>
00016 #endif
00017 
00018 #ifdef GOSU_IS_MAC
00019 #include <Gosu/ButtonsMac.hpp>
00020 #endif
00021 
00022 #ifdef GOSU_IS_X
00023 #include <X11/Xlib.h>
00024 #include <X11/Xutil.h>
00025 #include <X11/keysym.h>
00026 #include <Gosu/ButtonsX.hpp>
00027 #endif
00028 
00029 #include <Gosu/Platform.hpp>
00030 #include <Gosu/Fwd.hpp>
00031 #include <Gosu/TR1.hpp>
00032 #include <vector>
00033 
00034 namespace Gosu
00035 {
00037     class Button
00038     {
00039         unsigned id_;
00040         
00041     public:
00043         explicit Button(unsigned id) : id_(id) {}
00045         unsigned id() const { return id_; }
00046 
00048         Button() : id_(noButton) {}
00049 
00051         Button(ButtonName name) : id_(name) {}
00052     };
00053     
00055     inline bool operator==(Button lhs, Button rhs)
00056     {
00057         return lhs.id() == rhs.id();
00058     }
00059     inline bool operator!=(Button lhs, Button rhs)
00060     {
00061         return !(lhs == rhs);
00062     }
00063     inline bool operator<(Button lhs, Button rhs)
00064     {
00065         return lhs.id() < rhs.id();
00066     }
00067     
00072     struct Touch
00073     {
00075         void* id;
00077         float x, y;
00078     };
00079     typedef std::vector<Touch> Touches;
00080     
00083     class Input
00084     {
00085         struct Impl;
00086         const std::auto_ptr<Impl> pimpl;
00087 
00088     public:
00089         #ifdef GOSU_IS_WIN
00090         Input(HWND window);
00091         #endif
00092         
00093         #ifdef GOSU_IS_MAC
00094         #ifdef GOSU_IS_IPHONE
00095         Input(void* view, float updateInterval);
00096         void feedTouchEvent(int type, void* touches);
00097         #else
00098         Input(void* window);
00099         bool feedNSEvent(void* event);
00100         #endif
00101         #endif
00102         
00103         #ifdef GOSU_IS_X
00104         Input(::Display* display, ::Window window);
00105         bool feedXEvent(::XEvent& event);
00106         #endif
00107         
00108         ~Input();
00109         
00111         static wchar_t idToChar(Button btn);
00114         static Button charToId(wchar_t ch);
00115         
00118         bool down(Button btn) const;
00119         
00122         double mouseX() const;
00124         double mouseY() const;
00125         
00129         void setMousePosition(double x, double y);
00130 
00131         // Undocumented for the moment. Also applies to currentTouches().
00132         void setMouseFactors(double factorX, double factorY);
00133         
00135         const Touches& currentTouches() const;
00136         
00138         double accelerometerX() const;
00139         double accelerometerY() const;
00140         double accelerometerZ() const;
00141         
00144         void update();
00145         
00148         std::tr1::function<void (Button)> onButtonDown, onButtonUp;
00149         
00152         std::tr1::function<void (Touch)> onTouchBegan, onTouchMoved, onTouchEnded;
00153         
00155         TextInput* textInput() const;
00157         void setTextInput(TextInput* input);
00158     };
00159 }
00160 
00161 #endif