GUIEventAdapter 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGGA_GUIEVENTADAPTER
  14. #define OSGGA_GUIEVENTADAPTER 1
  15. #include <osg/Object>
  16. #include <osg/Matrix>
  17. #include <osg/GraphicsContext>
  18. #include <osgGA/Event>
  19. namespace osgGA{
  20. struct PointerData : public osg::Referenced
  21. {
  22. PointerData():
  23. object(0),
  24. x(0.0f),
  25. xMin(-1.0f),
  26. xMax(1.0f),
  27. y(0.0f),
  28. yMin(-1.0f),
  29. yMax(1.0f) {}
  30. PointerData(osg::Object* obj, float in_x, float in_xMin, float in_xMax, float in_y, float in_yMin, float in_yMax):
  31. object(obj),
  32. x(in_x),
  33. xMin(in_xMin),
  34. xMax(in_xMax),
  35. y(in_y),
  36. yMin(in_yMin),
  37. yMax(in_yMax) {}
  38. PointerData(const PointerData& pd):
  39. osg::Referenced(),
  40. object(pd.object),
  41. x(pd.x),
  42. xMin(pd.xMin),
  43. xMax(pd.xMax),
  44. y(pd.y),
  45. yMin(pd.yMin),
  46. yMax(pd.yMax) {}
  47. PointerData& operator = (const PointerData& pd)
  48. {
  49. if (&pd==this) return *this;
  50. object = pd.object;
  51. x = pd.x;
  52. xMin = pd.xMin;
  53. xMax = pd.xMax;
  54. y = pd.y;
  55. yMin = pd.yMin;
  56. yMax = pd.yMax;
  57. return *this;
  58. }
  59. osg::observer_ptr<osg::Object> object;
  60. float x, xMin, xMax;
  61. float y, yMin, yMax;
  62. float getXnormalized() const { return (x-xMin)/(xMax-xMin)*2.0f-1.0f; }
  63. float getYnormalized() const { return (y-yMin)/(yMax-yMin)*2.0f-1.0f; }
  64. };
  65. /** Event class for storing Keyboard, mouse and window events.
  66. */
  67. class OSGGA_EXPORT GUIEventAdapter : public Event
  68. {
  69. public:
  70. enum MouseButtonMask {
  71. LEFT_MOUSE_BUTTON = 1<<0,
  72. MIDDLE_MOUSE_BUTTON = 1<<1,
  73. RIGHT_MOUSE_BUTTON = 1<<2
  74. };
  75. enum EventType {
  76. NONE = 0,
  77. PUSH = 1<<0,
  78. RELEASE = 1<<1,
  79. DOUBLECLICK = 1<<2,
  80. DRAG = 1<<3,
  81. MOVE = 1<<4,
  82. KEYDOWN = 1<<5,
  83. KEYUP = 1<<6,
  84. FRAME = 1<<7,
  85. RESIZE = 1<<8,
  86. SCROLL = 1<<9,
  87. PEN_PRESSURE = 1<<10,
  88. PEN_ORIENTATION = 1<<11,
  89. PEN_PROXIMITY_ENTER = 1<<12,
  90. PEN_PROXIMITY_LEAVE = 1<<13,
  91. CLOSE_WINDOW = 1<<14,
  92. QUIT_APPLICATION = 1<<15,
  93. USER = 1<<16
  94. };
  95. enum KeySymbol
  96. {
  97. KEY_Space = 0x20,
  98. KEY_0 = '0',
  99. KEY_1 = '1',
  100. KEY_2 = '2',
  101. KEY_3 = '3',
  102. KEY_4 = '4',
  103. KEY_5 = '5',
  104. KEY_6 = '6',
  105. KEY_7 = '7',
  106. KEY_8 = '8',
  107. KEY_9 = '9',
  108. KEY_A = 'a',
  109. KEY_B = 'b',
  110. KEY_C = 'c',
  111. KEY_D = 'd',
  112. KEY_E = 'e',
  113. KEY_F = 'f',
  114. KEY_G = 'g',
  115. KEY_H = 'h',
  116. KEY_I = 'i',
  117. KEY_J = 'j',
  118. KEY_K = 'k',
  119. KEY_L = 'l',
  120. KEY_M = 'm',
  121. KEY_N = 'n',
  122. KEY_O = 'o',
  123. KEY_P = 'p',
  124. KEY_Q = 'q',
  125. KEY_R = 'r',
  126. KEY_S = 's',
  127. KEY_T = 't',
  128. KEY_U = 'u',
  129. KEY_V = 'v',
  130. KEY_W = 'w',
  131. KEY_X = 'x',
  132. KEY_Y = 'y',
  133. KEY_Z = 'z',
  134. KEY_Exclaim = 0x21,
  135. KEY_Quotedbl = 0x22,
  136. KEY_Hash = 0x23,
  137. KEY_Dollar = 0x24,
  138. KEY_Ampersand = 0x26,
  139. KEY_Quote = 0x27,
  140. KEY_Leftparen = 0x28,
  141. KEY_Rightparen = 0x29,
  142. KEY_Asterisk = 0x2A,
  143. KEY_Plus = 0x2B,
  144. KEY_Comma = 0x2C,
  145. KEY_Minus = 0x2D,
  146. KEY_Period = 0x2E,
  147. KEY_Slash = 0x2F,
  148. KEY_Colon = 0x3A,
  149. KEY_Semicolon = 0x3B,
  150. KEY_Less = 0x3C,
  151. KEY_Equals = 0x3D,
  152. KEY_Greater = 0x3E,
  153. KEY_Question = 0x3F,
  154. KEY_At = 0x40,
  155. KEY_Leftbracket = 0x5B,
  156. KEY_Backslash = 0x5C,
  157. KEY_Rightbracket = 0x5D,
  158. KEY_Caret = 0x5E,
  159. KEY_Underscore = 0x5F,
  160. KEY_Backquote = 0x60,
  161. KEY_BackSpace = 0xFF08, /* back space, back char */
  162. KEY_Tab = 0xFF09,
  163. KEY_Linefeed = 0xFF0A, /* Linefeed, LF */
  164. KEY_Clear = 0xFF0B,
  165. KEY_Return = 0xFF0D, /* Return, enter */
  166. KEY_Pause = 0xFF13, /* Pause, hold */
  167. KEY_Scroll_Lock = 0xFF14,
  168. KEY_Sys_Req = 0xFF15,
  169. KEY_Escape = 0xFF1B,
  170. KEY_Delete = 0xFFFF, /* Delete, rubout */
  171. /* Cursor control & motion */
  172. KEY_Home = 0xFF50,
  173. KEY_Left = 0xFF51, /* Move left, left arrow */
  174. KEY_Up = 0xFF52, /* Move up, up arrow */
  175. KEY_Right = 0xFF53, /* Move right, right arrow */
  176. KEY_Down = 0xFF54, /* Move down, down arrow */
  177. KEY_Prior = 0xFF55, /* Prior, previous */
  178. KEY_Page_Up = 0xFF55,
  179. KEY_Next = 0xFF56, /* Next */
  180. KEY_Page_Down = 0xFF56,
  181. KEY_End = 0xFF57, /* EOL */
  182. KEY_Begin = 0xFF58, /* BOL */
  183. /* Misc Functions */
  184. KEY_Select = 0xFF60, /* Select, mark */
  185. KEY_Print = 0xFF61,
  186. KEY_Execute = 0xFF62, /* Execute, run, do */
  187. KEY_Insert = 0xFF63, /* Insert, insert here */
  188. KEY_Undo = 0xFF65, /* Undo, oops */
  189. KEY_Redo = 0xFF66, /* redo, again */
  190. KEY_Menu = 0xFF67, /* On Windows, this is VK_APPS, the context-menu key */
  191. KEY_Find = 0xFF68, /* Find, search */
  192. KEY_Cancel = 0xFF69, /* Cancel, stop, abort, exit */
  193. KEY_Help = 0xFF6A, /* Help */
  194. KEY_Break = 0xFF6B,
  195. KEY_Mode_switch = 0xFF7E, /* Character set switch */
  196. KEY_Script_switch = 0xFF7E, /* Alias for mode_switch */
  197. KEY_Num_Lock = 0xFF7F,
  198. /* Keypad Functions, keypad numbers cleverly chosen to map to ascii */
  199. KEY_KP_Space = 0xFF80, /* space */
  200. KEY_KP_Tab = 0xFF89,
  201. KEY_KP_Enter = 0xFF8D, /* enter */
  202. KEY_KP_F1 = 0xFF91, /* PF1, KP_A, ... */
  203. KEY_KP_F2 = 0xFF92,
  204. KEY_KP_F3 = 0xFF93,
  205. KEY_KP_F4 = 0xFF94,
  206. KEY_KP_Home = 0xFF95,
  207. KEY_KP_Left = 0xFF96,
  208. KEY_KP_Up = 0xFF97,
  209. KEY_KP_Right = 0xFF98,
  210. KEY_KP_Down = 0xFF99,
  211. KEY_KP_Prior = 0xFF9A,
  212. KEY_KP_Page_Up = 0xFF9A,
  213. KEY_KP_Next = 0xFF9B,
  214. KEY_KP_Page_Down = 0xFF9B,
  215. KEY_KP_End = 0xFF9C,
  216. KEY_KP_Begin = 0xFF9D,
  217. KEY_KP_Insert = 0xFF9E,
  218. KEY_KP_Delete = 0xFF9F,
  219. KEY_KP_Equal = 0xFFBD, /* equals */
  220. KEY_KP_Multiply = 0xFFAA,
  221. KEY_KP_Add = 0xFFAB,
  222. KEY_KP_Separator = 0xFFAC, /* separator, often comma */
  223. KEY_KP_Subtract = 0xFFAD,
  224. KEY_KP_Decimal = 0xFFAE,
  225. KEY_KP_Divide = 0xFFAF,
  226. KEY_KP_0 = 0xFFB0,
  227. KEY_KP_1 = 0xFFB1,
  228. KEY_KP_2 = 0xFFB2,
  229. KEY_KP_3 = 0xFFB3,
  230. KEY_KP_4 = 0xFFB4,
  231. KEY_KP_5 = 0xFFB5,
  232. KEY_KP_6 = 0xFFB6,
  233. KEY_KP_7 = 0xFFB7,
  234. KEY_KP_8 = 0xFFB8,
  235. KEY_KP_9 = 0xFFB9,
  236. /*
  237. * Auxiliary Functions; note the duplicate definitions for left and right
  238. * function keys; Sun keyboards and a few other manufactures have such
  239. * function key groups on the left and/or right sides of the keyboard.
  240. * We've not found a keyboard with more than 35 function keys total.
  241. */
  242. KEY_F1 = 0xFFBE,
  243. KEY_F2 = 0xFFBF,
  244. KEY_F3 = 0xFFC0,
  245. KEY_F4 = 0xFFC1,
  246. KEY_F5 = 0xFFC2,
  247. KEY_F6 = 0xFFC3,
  248. KEY_F7 = 0xFFC4,
  249. KEY_F8 = 0xFFC5,
  250. KEY_F9 = 0xFFC6,
  251. KEY_F10 = 0xFFC7,
  252. KEY_F11 = 0xFFC8,
  253. KEY_F12 = 0xFFC9,
  254. KEY_F13 = 0xFFCA,
  255. KEY_F14 = 0xFFCB,
  256. KEY_F15 = 0xFFCC,
  257. KEY_F16 = 0xFFCD,
  258. KEY_F17 = 0xFFCE,
  259. KEY_F18 = 0xFFCF,
  260. KEY_F19 = 0xFFD0,
  261. KEY_F20 = 0xFFD1,
  262. KEY_F21 = 0xFFD2,
  263. KEY_F22 = 0xFFD3,
  264. KEY_F23 = 0xFFD4,
  265. KEY_F24 = 0xFFD5,
  266. KEY_F25 = 0xFFD6,
  267. KEY_F26 = 0xFFD7,
  268. KEY_F27 = 0xFFD8,
  269. KEY_F28 = 0xFFD9,
  270. KEY_F29 = 0xFFDA,
  271. KEY_F30 = 0xFFDB,
  272. KEY_F31 = 0xFFDC,
  273. KEY_F32 = 0xFFDD,
  274. KEY_F33 = 0xFFDE,
  275. KEY_F34 = 0xFFDF,
  276. KEY_F35 = 0xFFE0,
  277. /* Modifiers */
  278. KEY_Shift_L = 0xFFE1, /* Left shift */
  279. KEY_Shift_R = 0xFFE2, /* Right shift */
  280. KEY_Control_L = 0xFFE3, /* Left control */
  281. KEY_Control_R = 0xFFE4, /* Right control */
  282. KEY_Caps_Lock = 0xFFE5, /* Caps lock */
  283. KEY_Shift_Lock = 0xFFE6, /* Shift lock */
  284. KEY_Meta_L = 0xFFE7, /* Left meta */
  285. KEY_Meta_R = 0xFFE8, /* Right meta */
  286. KEY_Alt_L = 0xFFE9, /* Left alt */
  287. KEY_Alt_R = 0xFFEA, /* Right alt */
  288. KEY_Super_L = 0xFFEB, /* Left super */
  289. KEY_Super_R = 0xFFEC, /* Right super */
  290. KEY_Hyper_L = 0xFFED, /* Left hyper */
  291. KEY_Hyper_R = 0xFFEE /* Right hyper */
  292. };
  293. enum ModKeyMask
  294. {
  295. MODKEY_LEFT_SHIFT = 0x0001,
  296. MODKEY_RIGHT_SHIFT = 0x0002,
  297. MODKEY_LEFT_CTRL = 0x0004,
  298. MODKEY_RIGHT_CTRL = 0x0008,
  299. MODKEY_LEFT_ALT = 0x0010,
  300. MODKEY_RIGHT_ALT = 0x0020,
  301. MODKEY_LEFT_META = 0x0040,
  302. MODKEY_RIGHT_META = 0x0080,
  303. MODKEY_LEFT_SUPER = 0x0100,
  304. MODKEY_RIGHT_SUPER = 0x0200,
  305. MODKEY_LEFT_HYPER = 0x0400,
  306. MODKEY_RIGHT_HYPER = 0x0800,
  307. MODKEY_NUM_LOCK = 0x1000,
  308. MODKEY_CAPS_LOCK = 0x2000,
  309. MODKEY_CTRL = (MODKEY_LEFT_CTRL|MODKEY_RIGHT_CTRL),
  310. MODKEY_SHIFT = (MODKEY_LEFT_SHIFT|MODKEY_RIGHT_SHIFT),
  311. MODKEY_ALT = (MODKEY_LEFT_ALT|MODKEY_RIGHT_ALT),
  312. MODKEY_META = (MODKEY_LEFT_META|MODKEY_RIGHT_META),
  313. MODKEY_SUPER = (MODKEY_LEFT_SUPER|MODKEY_RIGHT_SUPER),
  314. MODKEY_HYPER = (MODKEY_LEFT_HYPER|MODKEY_RIGHT_HYPER)
  315. };
  316. enum MouseYOrientation
  317. {
  318. Y_INCREASING_UPWARDS,
  319. Y_INCREASING_DOWNWARDS
  320. };
  321. enum ScrollingMotion
  322. {
  323. SCROLL_NONE,
  324. SCROLL_LEFT,
  325. SCROLL_RIGHT,
  326. SCROLL_UP,
  327. SCROLL_DOWN,
  328. SCROLL_2D
  329. };
  330. enum TabletPointerType
  331. {
  332. UNKNOWN = 0,
  333. PEN,
  334. PUCK,
  335. ERASER
  336. };
  337. enum TouchPhase
  338. {
  339. TOUCH_UNKNOWN,
  340. TOUCH_BEGAN,
  341. TOUCH_MOVED,
  342. TOUCH_STATIONERY,
  343. TOUCH_ENDED
  344. };
  345. class TouchData : public osg::Object {
  346. public:
  347. struct TouchPoint {
  348. unsigned int id;
  349. TouchPhase phase;
  350. float x, y;
  351. unsigned int tapCount;
  352. TouchPoint() : id(0), phase(TOUCH_UNKNOWN), x(0.0f), y(0.0f), tapCount(0) {}
  353. TouchPoint(unsigned int in_id, TouchPhase in_phase, float in_x, float in_y, unsigned int in_tap_count)
  354. : id(in_id),
  355. phase(in_phase),
  356. x(in_x),
  357. y(in_y),
  358. tapCount(in_tap_count)
  359. {
  360. }
  361. };
  362. typedef std::vector<TouchPoint> TouchSet;
  363. typedef TouchSet::iterator iterator;
  364. typedef TouchSet::const_iterator const_iterator;
  365. TouchData() : osg::Object() {}
  366. TouchData(const TouchData& td, const osg::CopyOp& copyop):
  367. osg::Object(td,copyop),
  368. _touches(td._touches) {}
  369. META_Object(osgGA, TouchData);
  370. unsigned int getNumTouchPoints() const { return static_cast<unsigned int>(_touches.size()); }
  371. iterator begin() { return _touches.begin(); }
  372. const_iterator begin() const { return _touches.begin(); }
  373. iterator end() { return _touches.end(); }
  374. const_iterator end() const { return _touches.end(); }
  375. const TouchPoint get(unsigned int i) const { return _touches[i]; }
  376. protected:
  377. virtual ~TouchData() {}
  378. void addTouchPoint(unsigned int id, TouchPhase phase, float x, float y, unsigned int tap_count) {
  379. _touches.push_back(TouchPoint(id, phase, x, y, tap_count));
  380. }
  381. TouchSet _touches;
  382. friend class GUIEventAdapter;
  383. };
  384. public:
  385. GUIEventAdapter();
  386. GUIEventAdapter(const GUIEventAdapter& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  387. META_Object(osgGA, GUIEventAdapter);
  388. virtual GUIEventAdapter* asGUIEventAdapter() { return this; }
  389. virtual const GUIEventAdapter* asGUIEventAdapter() const { return this; }
  390. /** Get the accumulated event state singleton.
  391. * Typically all EventQueue will share this single GUIEventAdapter object for tracking
  392. * the mouse position, keyboard and mouse masks. */
  393. static osg::ref_ptr<GUIEventAdapter>& getAccumulatedEventState();
  394. /** set the event type. */
  395. void setEventType(EventType Type) { _eventType = Type; }
  396. /** get the event type. */
  397. virtual EventType getEventType() const { return _eventType; }
  398. /** deprecated function for getting time of event. */
  399. double time() const { return _time; }
  400. void setGraphicsContext(osg::GraphicsContext* context) { _context = context; }
  401. osg::GraphicsContext* getGraphicsContext() { return _context.get(); }
  402. const osg::GraphicsContext* getGraphicsContext() const { return _context.get(); }
  403. /** set window rectangle. */
  404. void setWindowRectangle(int x, int y, int width, int height, bool updateMouseRange = true);
  405. /** set window x origin.*/
  406. void setWindowX(int v) { _windowX = v; }
  407. /** get window x origin.*/
  408. int getWindowX() const { return _windowX; }
  409. /** set window x origin.*/
  410. void setWindowY(int v) { _windowY = v; }
  411. /** get window y origin.*/
  412. int getWindowY() const { return _windowY; }
  413. /** set window width.*/
  414. void setWindowWidth(int v) { _windowWidth = v; }
  415. /** get window width.*/
  416. int getWindowWidth() const { return _windowWidth; }
  417. /** set window height.*/
  418. void setWindowHeight(int v) { _windowHeight = v; }
  419. /** get window height.*/
  420. int getWindowHeight() const { return _windowHeight; }
  421. /** set key pressed. */
  422. inline void setKey(int key) { _key = key; }
  423. /** get key pressed, return -1 if inappropriate for this GUIEventAdapter. */
  424. virtual int getKey() const { return _key; }
  425. /** set virtual key pressed. */
  426. void setUnmodifiedKey(int key) { _unmodifiedKey = key; }
  427. /** get virtual key pressed. */
  428. int getUnmodifiedKey() const { return _unmodifiedKey; }
  429. /** set button pressed/released.*/
  430. void setButton(int button) { _button = button; }
  431. /** button pressed/released, return -1 if inappropriate for this GUIEventAdapter.*/
  432. int getButton() const { return _button; }
  433. /** set mouse input range. */
  434. void setInputRange(float Xmin, float Ymin, float Xmax, float Ymax);
  435. /** set mouse minimum x. */
  436. void setXmin(float v) { _Xmin = v; }
  437. /** get mouse minimum x. */
  438. float getXmin() const { return _Xmin; }
  439. /** set mouse maximum x. */
  440. void setXmax(float v) { _Xmax = v; }
  441. /** get mouse maximum x. */
  442. float getXmax() const { return _Xmax; }
  443. /** set mouse minimum x. */
  444. void setYmin(float v) { _Ymin = v; }
  445. /** get mouse minimum y. */
  446. float getYmin() const { return _Ymin; }
  447. /** set mouse maximum y. */
  448. void setYmax(float v) { _Ymax = v; }
  449. /** get mouse maximum y. */
  450. float getYmax() const { return _Ymax; }
  451. /** set current mouse x position.*/
  452. void setX(float x) { _mx = x; }
  453. /** get current mouse x position.*/
  454. float getX() const { return _mx; }
  455. /** set current mouse y position.*/
  456. void setY(float y) { _my = y; }
  457. /** get current mouse y position.*/
  458. float getY() const { return _my; }
  459. #if 1
  460. inline float getXnormalized() const
  461. {
  462. return _pointerDataList.size()>=1 ?
  463. _pointerDataList[_pointerDataList.size()-1]->getXnormalized():
  464. 2.0f*(getX()-getXmin())/(getXmax()-getXmin())-1.0f;
  465. }
  466. inline float getYnormalized() const
  467. {
  468. if (_pointerDataList.size()>=1) return _pointerDataList[_pointerDataList.size()-1]->getYnormalized();
  469. if (_mouseYOrientation==Y_INCREASING_UPWARDS) return 2.0f*(getY()-getYmin())/(getYmax()-getYmin())-1.0f;
  470. else return -(2.0f*(getY()-getYmin())/(getYmax()-getYmin())-1.0f);
  471. }
  472. #else
  473. /**
  474. * return the current mouse x value normalized to the range of -1 to 1.
  475. * -1 would be the left hand side of the window.
  476. * 0.0 would be the middle of the window.
  477. * +1 would be the right hand side of the window.
  478. */
  479. inline float getXnormalized() const { return 2.0f*(getX()-getXmin())/(getXmax()-getXmin())-1.0f; }
  480. /**
  481. * return the current mouse y value normalized to the range of -1 to 1.
  482. * -1 would be the bottom of the window.
  483. * 0.0 would be the middle of the window.
  484. * +1 would be the top of the window.
  485. */
  486. inline float getYnormalized() const
  487. {
  488. if (_mouseYOrientation==Y_INCREASING_UPWARDS) return 2.0f*(getY()-getYmin())/(getYmax()-getYmin())-1.0f;
  489. else return -(2.0f*(getY()-getYmin())/(getYmax()-getYmin())-1.0f);
  490. }
  491. #endif
  492. /// set mouse-Y orientation (mouse-Y increases upwards or downwards).
  493. void setMouseYOrientation(MouseYOrientation myo) { _mouseYOrientation = myo; }
  494. /// get mouse-Y orientation (mouse-Y increases upwards or downwards).
  495. MouseYOrientation getMouseYOrientation() const { return _mouseYOrientation; }
  496. /// set mouse-Y orientation (mouse-Y increases upwards or downwards) and recompute variables
  497. void setMouseYOrientationAndUpdateCoords(MouseYOrientation myo);
  498. /// set current mouse button state.
  499. void setButtonMask(int mask) { _buttonMask = mask; }
  500. /// get current mouse button state.
  501. int getButtonMask() const { return _buttonMask; }
  502. /// set modifier key mask.
  503. void setModKeyMask(int mask) { _modKeyMask = mask; }
  504. /// get modifier key mask.
  505. int getModKeyMask() const { return _modKeyMask; }
  506. /// set scrolling motion (for EventType::SCROLL).
  507. void setScrollingMotion(ScrollingMotion motion) { _scrolling.motion = motion; }
  508. /// get scrolling motion (for EventType::SCROLL).
  509. ScrollingMotion getScrollingMotion() const { return _scrolling.motion; }
  510. /// set the scrolling delta to x,y and the scrolling motion to SCROLL_2D.
  511. void setScrollingMotionDelta(float x, float y) {
  512. _scrolling.motion = SCROLL_2D;
  513. _scrolling.deltaX = x;
  514. _scrolling.deltaY = y;
  515. }
  516. /// set the scrolling x-delta.
  517. void setScrollingDeltaX(float v) { _scrolling.deltaX = v; }
  518. /// get the scrolling x-delta.
  519. float getScrollingDeltaX() const { return _scrolling.deltaX; }
  520. /// set the scrolling y-delta.
  521. void setScrollingDeltaY(float v) { _scrolling.deltaY = v; }
  522. /// get the scrolling y-delta.
  523. float getScrollingDeltaY() const { return _scrolling.deltaY; }
  524. /// set the tablet pen pressure (range 0..1).
  525. void setPenPressure(float pressure) { _tabletPen.pressure = pressure; }
  526. /// get the tablet pen pressure (range 0..1).
  527. float getPenPressure() const { return _tabletPen.pressure; }
  528. /// set the tablet pen tiltX in degrees.
  529. void setPenTiltX(float tiltX) { _tabletPen.tiltX = tiltX; }
  530. /// get the tablet pen tiltX in degrees.
  531. float getPenTiltX() const { return _tabletPen.tiltX; }
  532. /// set the tablet pen tiltY in degrees.
  533. void setPenTiltY(float tiltY) { _tabletPen.tiltY = tiltY; }
  534. /// get the tablet pen tiltY in degrees.
  535. float getPenTiltY() const { return _tabletPen.tiltY; }
  536. /// set the tablet pen rotation around the Z-axis in degrees.
  537. void setPenRotation(float rotation) { _tabletPen.rotation = rotation; }
  538. /// get the tablet pen rotation around the Z-axis in degrees.
  539. float getPenRotation() const { return _tabletPen.rotation; }
  540. /// set the tablet pointer type.
  541. void setTabletPointerType(TabletPointerType pt) { _tabletPen.tabletPointerType = pt; }
  542. /// get the tablet pointer type.
  543. TabletPointerType getTabletPointerType() const { return _tabletPen.tabletPointerType; }
  544. /// set the orientation from a tablet input device as a matrix.
  545. const osg::Matrix getPenOrientation() const;
  546. void addTouchPoint(unsigned int id, TouchPhase phase, float x, float y, unsigned int tapCount = 0);
  547. void setTouchData(TouchData* td) { _touchData = td; }
  548. TouchData* getTouchData() const { return _touchData.get(); }
  549. bool isMultiTouchEvent() const { return (_touchData.valid()); }
  550. inline float getTouchPointNormalizedX(unsigned int ndx) const {
  551. return (getTouchData()->get(ndx).x-_Xmin)/(_Xmax-_Xmin)*2.0f-1.0f;
  552. }
  553. inline float getTouchPointNormalizedY(unsigned int ndx) const {
  554. if (_mouseYOrientation==Y_INCREASING_UPWARDS)
  555. return (getTouchData()->get(ndx).y-_Ymin)/(_Ymax-_Ymin)*2.0f-1.0f;
  556. else
  557. return -((getTouchData()->get(ndx).y-_Ymin)/(_Ymax-_Ymin)*2.0f-1.0f);
  558. }
  559. typedef std::vector< osg::ref_ptr<PointerData> > PointerDataList;
  560. void setPointerDataList(const PointerDataList& pdl) { _pointerDataList = pdl; }
  561. PointerDataList& getPointerDataList() { return _pointerDataList; }
  562. const PointerDataList& getPointerDataList() const { return _pointerDataList; }
  563. unsigned int getNumPointerData() const { return static_cast<unsigned int>(_pointerDataList.size()); }
  564. PointerData* getPointerData(unsigned int i) { return _pointerDataList[i].get(); }
  565. const PointerData* getPointerData(unsigned int i) const { return _pointerDataList[i].get(); }
  566. PointerData* getPointerData(osg::Object* obj) { for(unsigned int i=0;i<_pointerDataList.size(); ++i) { if (_pointerDataList[i]->object==obj) return _pointerDataList[i].get(); } return 0; }
  567. const PointerData* getPointerData(osg::Object* obj) const { for(unsigned int i=0;i<_pointerDataList.size(); ++i) { if (_pointerDataList[i]->object==obj) return _pointerDataList[i].get(); } return 0; }
  568. void addPointerData(PointerData* pd) { _pointerDataList.push_back(pd); }
  569. void copyPointerDataFrom(const osgGA::GUIEventAdapter& sourceEvent);
  570. protected:
  571. /** Force users to create on heap, so that multiple referencing is safe.*/
  572. virtual ~GUIEventAdapter();
  573. EventType _eventType;
  574. osg::observer_ptr<osg::GraphicsContext> _context;
  575. int _windowX;
  576. int _windowY;
  577. int _windowWidth;
  578. int _windowHeight;
  579. int _key;
  580. int _unmodifiedKey;
  581. int _button;
  582. float _Xmin,_Xmax;
  583. float _Ymin,_Ymax;
  584. float _mx;
  585. float _my;
  586. int _buttonMask;
  587. int _modKeyMask;
  588. MouseYOrientation _mouseYOrientation;
  589. struct Scrolling {
  590. ScrollingMotion motion;
  591. float deltaX;
  592. float deltaY;
  593. Scrolling() : motion(SCROLL_NONE), deltaX(0), deltaY(0) {}
  594. Scrolling(const Scrolling& rhs) : motion(rhs.motion), deltaX(rhs.deltaX), deltaY(rhs.deltaY) {}
  595. };
  596. Scrolling _scrolling;
  597. struct TabletPen {
  598. float pressure;
  599. float tiltX;
  600. float tiltY;
  601. float rotation;
  602. TabletPointerType tabletPointerType;
  603. TabletPen() : pressure(0), tiltX(0), tiltY(0), rotation(0), tabletPointerType(UNKNOWN) {}
  604. TabletPen(const TabletPen& rhs) : pressure(rhs.pressure), tiltX(rhs.tiltX), tiltY(rhs.tiltY), rotation(rhs.rotation), tabletPointerType(rhs.tabletPointerType) {}
  605. };
  606. TabletPen _tabletPen;
  607. osg::ref_ptr<TouchData> _touchData;
  608. PointerDataList _pointerDataList;
  609. };
  610. }
  611. #endif