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/Buttons.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 <Gosu/Buttons.hpp>
00024 #include <X11/Xlib.h>
00025 #include <X11/Xutil.h>
00026 #endif
00027 
00028 #include <Gosu/Platform.hpp>
00029 #include <Gosu/Fwd.hpp>
00030 #include <Gosu/TR1.hpp>
00031 #include <vector>
00032 
00033 namespace Gosu
00034 {
00036     class Button
00037     {
00038         unsigned id_;
00039         
00040     public:
00042         explicit Button(unsigned id) : id_(id) {}
00044         unsigned id() const { return id_; }
00045 
00047         Button() : id_(noButton) {}
00048 
00050         Button(ButtonName name) : id_(name) {}
00051     };
00052     
00054     inline bool operator==(Button lhs, Button rhs)
00055     {
00056         return lhs.id() == rhs.id();
00057     }
00058     inline bool operator!=(Button lhs, Button rhs)
00059     {
00060         return !(lhs == rhs);
00061     }
00062     inline bool operator<(Button lhs, Button rhs)
00063     {
00064         return lhs.id() < rhs.id();
00065     }
00066     
00071     struct Touch
00072     {
00074         void* id;
00076         float x, y;
00077     };
00078     typedef std::vector<Touch> Touches;
00079     
00082     class Input
00083     {
00084         struct Impl;
00085         const std::auto_ptr<Impl> pimpl;
00086 
00087     public:
00088         #ifdef GOSU_IS_WIN
00089         Input(HWND window);
00090         #endif
00091         
00092         #ifdef GOSU_IS_MAC
00093         #ifdef GOSU_IS_IPHONE
00094         Input(void* view, float updateInterval);
00095         void feedTouchEvent(int type, void* touches);
00096         #else
00097         Input(void* window);
00098         bool feedNSEvent(void* event);
00099         #endif
00100         #endif
00101         
00102         #ifdef GOSU_IS_X
00103         Input(::Display* display, ::Window window);
00104         bool feedXEvent(::XEvent& event);
00105         #endif
00106         
00107         ~Input();
00108         
00110         static wchar_t idToChar(Button btn);
00113         static Button charToId(wchar_t ch);
00114         
00117         bool down(Button btn) const;
00118         
00121         double mouseX() const;
00123         double mouseY() const;
00124         
00128         void setMousePosition(double x, double y);
00129 
00130         // Undocumented for the moment. Also applies to currentTouches().
00131         void setMouseFactors(double factorX, double factorY);
00132         
00134         const Touches& currentTouches() const;
00135         
00137         double accelerometerX() const;
00138         double accelerometerY() const;
00139         double accelerometerZ() const;
00140         
00143         void update();
00144         
00147         std::tr1::function<void (Button)> onButtonDown, onButtonUp;
00148         
00151         std::tr1::function<void (Touch)> onTouchBegan, onTouchMoved, onTouchEnded;
00152         
00154         TextInput* textInput() const;
00156         void setTextInput(TextInput* input);
00157     };
00158 }
00159 
00160 #endif