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 #include <Gosu/Buttons.hpp>
00010 #include <Gosu/TR1.hpp>
00011 
00012 #ifdef GOSU_IS_WIN
00013 #ifndef NOMINMAX
00014 #define NOMINMAX
00015 #endif
00016 #include <windows.h>
00017 #endif
00018 
00019 #ifdef GOSU_IS_X
00020 #include <X11/Xlib.h>
00021 #include <X11/Xutil.h>
00022 #endif
00023 
00024 #include <vector>
00025 
00026 namespace Gosu
00027 {
00029     class Button
00030     {
00031         unsigned id_;
00032         
00033     public:
00035         explicit Button(unsigned id) : id_(id) {}
00037         unsigned id() const { return id_; }
00038 
00040         Button() : id_(noButton) {}
00041 
00043         Button(ButtonName name) : id_(name) {}
00044     };
00045     
00047     inline bool operator==(Button lhs, Button rhs)
00048     {
00049         return lhs.id() == rhs.id();
00050     }
00051     inline bool operator!=(Button lhs, Button rhs)
00052     {
00053         return !(lhs == rhs);
00054     }
00055     inline bool operator<(Button lhs, Button rhs)
00056     {
00057         return lhs.id() < rhs.id();
00058     }
00059     
00064     struct Touch
00065     {
00067         void* id;
00069         float x, y;
00070     };
00071     typedef std::vector<Touch> Touches;
00072     
00075     class Input
00076     {
00077         struct Impl;
00078         const std::auto_ptr<Impl> pimpl;
00079 
00080     public:
00081         #ifdef GOSU_IS_WIN
00082         Input(HWND window);
00083         #endif
00084         
00085         #ifdef GOSU_IS_MAC
00086         #ifdef GOSU_IS_IPHONE
00087         Input(void* view, float updateInterval);
00088         void feedTouchEvent(int type, void* touches);
00089         #else
00090         Input(void* window);
00091         bool feedNSEvent(void* event);
00092         #endif
00093         #endif
00094         
00095         #ifdef GOSU_IS_X
00096         Input(::Display* display, ::Window window);
00097         bool feedXEvent(::XEvent& event);
00098         #endif
00099         
00100         ~Input();
00101         
00103         static wchar_t idToChar(Button btn);
00106         static Button charToId(wchar_t ch);
00107         
00110         bool down(Button btn) const;
00111         
00114         double mouseX() const;
00116         double mouseY() const;
00117         
00121         void setMousePosition(double x, double y);
00122 
00123         // Undocumented for the moment. Also applies to currentTouches().
00124         void setMouseFactors(double factorX, double factorY);
00125         
00127         const Touches& currentTouches() const;
00128         
00130         double accelerometerX() const;
00131         double accelerometerY() const;
00132         double accelerometerZ() const;
00133         
00136         void update();
00137         
00140         std::tr1::function<void (Button)> onButtonDown, onButtonUp;
00141         
00144         std::tr1::function<void (Touch)> onTouchBegan, onTouchMoved, onTouchEnded;
00145         
00147         TextInput* textInput() const;
00149         void setTextInput(TextInput* input);
00150     };
00151 }
00152 
00153 #endif