| 1 | #ifndef FGEVENTHANDLER_H |
| 2 | #define FGEVENTHANDLER_H 1 |
| 3 | |
| 4 | #include <map> |
| 5 | #include <osg/Quat> |
| 6 | #include <osgGA/GUIEventHandler> |
| 7 | #include <osgViewer/ViewerEventHandlers> |
| 8 | |
| 9 | #include <Main/fg_os.hxx> |
| 10 | |
| 11 | namespace flightgear |
| 12 | { |
| 13 | class FGStatsHandler : public osgViewer::StatsHandler |
| 14 | { |
| 15 | public: |
| 16 | FGStatsHandler() |
| 17 | { |
| 18 | // Adjust font type/size for >=OSG3. |
| 19 | // OSG defaults aren't working/available for FG. |
| 20 | _font = "Fonts/helvetica_medium.txf"; |
| 21 | _characterSize = 12.0f; |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | class FGEventHandler : public osgGA::GUIEventHandler { |
| 26 | public: |
| 27 | FGEventHandler(); |
| 28 | |
| 29 | virtual ~FGEventHandler() {} |
| 30 | |
| 31 | virtual const char* className() const {return "FGEventHandler"; } |
| 32 | #if 0 |
| 33 | virtual void init(const osgGA::GUIEventAdapter& ea, |
| 34 | osgGA::GUIActionAdapter& us); |
| 35 | #endif |
| 36 | bool handle(const osgGA::GUIEventAdapter& ea, |
| 37 | osgGA::GUIActionAdapter& us) override; |
| 38 | |
| 39 | void setIdleHandler(fgIdleHandler idleHandler) |
| 40 | { |
| 41 | this->idleHandler = idleHandler; |
| 42 | } |
| 43 | |
| 44 | fgIdleHandler getIdleHandler() const |
| 45 | { |
| 46 | return idleHandler; |
| 47 | } |
| 48 | |
| 49 | void setKeyHandler(fgKeyHandler keyHandler) |
| 50 | { |
| 51 | this->keyHandler = keyHandler; |
| 52 | } |
| 53 | |
| 54 | fgKeyHandler getKeyHandler() const |
| 55 | { |
| 56 | return keyHandler; |
| 57 | } |
| 58 | |
| 59 | void setMouseClickHandler(fgMouseClickHandler mouseClickHandler) |
| 60 | { |
| 61 | this->mouseClickHandler = mouseClickHandler; |
| 62 | } |
| 63 | |
| 64 | fgMouseClickHandler getMouseClickHandler() |
| 65 | { |
| 66 | return mouseClickHandler; |
| 67 | } |
| 68 | |
| 69 | void setMouseMotionHandler(fgMouseMotionHandler mouseMotionHandler) |
| 70 | { |
| 71 | this->mouseMotionHandler = mouseMotionHandler; |
| 72 | } |
| 73 | |
| 74 | fgMouseMotionHandler getMouseMotionHandler() |
| 75 | { |
| 76 | return mouseMotionHandler; |
| 77 | } |
| 78 | |
| 79 | void setChangeStatsCameraRenderOrder(bool c) |
| 80 | { |
| 81 | changeStatsCameraRenderOrder = c; |
| 82 | } |
| 83 | |
| 84 | int getCurrentModifiers() const |
| 85 | { |
| 86 | return currentModifiers; |
| 87 | } |
| 88 | |
| 89 | void setMouseWarped() |
| 90 | { |
| 91 | mouseWarped = true; |
| 92 | } |
| 93 | |
| 94 | /** Whether or not resizing is supported. It might not be when |
| 95 | * using multiple displays. |
| 96 | */ |
| 97 | bool getResizable() { return resizable; } |
| 98 | void setResizable(bool _resizable) { resizable = _resizable; } |
| 99 | |
| 100 | void reset(); |
| 101 | void clear(); |
| 102 | |
| 103 | // Wrapper for osgViewer::GraphicsWindow::setWindowRectangle() that takes |
| 104 | // coordinates excluding window furniture. |
| 105 | // |
| 106 | void setWindowRectangleInteriorWithCorrection(osgViewer::GraphicsWindow* window, int x, int y, int width, int height); |
| 107 | |
| 108 | static int translateKey(const osgGA::GUIEventAdapter& ea); |
| 109 | static int translateModifiers(const osgGA::GUIEventAdapter& ea); |
| 110 | protected: |
| 111 | osg::ref_ptr<osg::Node> _node; |
| 112 | fgIdleHandler idleHandler; |
| 113 | fgKeyHandler keyHandler; |
| 114 | fgMouseClickHandler mouseClickHandler; |
| 115 | fgMouseMotionHandler mouseMotionHandler; |
| 116 | osg::ref_ptr<FGStatsHandler> statsHandler; |
| 117 | osg::ref_ptr<osgGA::GUIEventAdapter> statsEvent; |
| 118 | int statsType; |
| 119 | int currentModifiers; |
| 120 | |
| 121 | void handleKey(const osgGA::GUIEventAdapter& ea, int& key, int& modifiers); |
| 122 | bool resizable; |
| 123 | bool mouseWarped; |
| 124 | // workaround for osgViewer double scroll events |
| 125 | bool scrollButtonPressed; |
| 126 | int release_keys[128]; |
| 127 | void handleStats(osgGA::GUIActionAdapter& us); |
| 128 | bool changeStatsCameraRenderOrder; |
| 129 | SGPropertyNode_ptr _display, _print; |
| 130 | private: |
| 131 | bool m_setWindowRectangle_called = false; |
| 132 | int m_setWindowRectangle_called_x = 0; |
| 133 | int m_setWindowRectangle_called_y = 0; |
| 134 | int m_setWindowRectangle_delta_x = 0; |
| 135 | int m_setWindowRectangle_delta_y = 0; |
| 136 | |
| 137 | SGPropertyNode_ptr m_composite_viewer_enabled; |
| 138 | |
| 139 | enum WindowType |
| 140 | { |
| 141 | WindowType_NONE, |
| 142 | WindowType_MAIN, |
| 143 | WindowType_SVIEW |
| 144 | }; |
| 145 | WindowType |
| 146 | eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us, |
| 147 | int& x, int& y); |
| 148 | |
| 149 | bool isMainWindow(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us); |
| 150 | |
| 151 | }; |
| 152 | |
| 153 | bool eventToWindowCoords(const osgGA::GUIEventAdapter* ea, double& x, double& y); |
| 154 | } |
| 155 | #endif |