IEventReceiver.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "ILogger.h"
  6. #include "Keycodes.h"
  7. #include "irrString.h"
  8. namespace irr
  9. {
  10. //! Enumeration for all event types there are.
  11. enum EEVENT_TYPE
  12. {
  13. //! An event of the graphical user interface.
  14. /** GUI events are created by the GUI environment or the GUI elements in response
  15. to mouse or keyboard events. When a GUI element receives an event it will either
  16. process it and return true, or pass the event to its parent. If an event is not absorbed
  17. before it reaches the root element then it will then be passed to the user receiver. */
  18. EET_GUI_EVENT = 1,
  19. //! A mouse input event.
  20. /** Mouse events are created by the device and passed to IrrlichtDevice::postEventFromUser
  21. in response to mouse input received from the operating system.
  22. Mouse events are first passed to the user receiver, then to the GUI environment and its elements,
  23. then finally the input receiving scene manager where it is passed to the active camera.
  24. */
  25. EET_MOUSE_INPUT_EVENT,
  26. //! A key input event.
  27. /** Like mouse events, keyboard events are created by the device and passed to
  28. IrrlichtDevice::postEventFromUser. They take the same path as mouse events. */
  29. EET_KEY_INPUT_EVENT,
  30. //! A string input event.
  31. /** This event is created when multiple characters are sent at a time (e.g. using an IME). */
  32. EET_STRING_INPUT_EVENT,
  33. //! A touch input event.
  34. EET_TOUCH_INPUT_EVENT,
  35. //! A accelerometer event.
  36. EET_ACCELEROMETER_EVENT,
  37. //! A gyroscope event.
  38. EET_GYROSCOPE_EVENT,
  39. //! A device motion event.
  40. EET_DEVICE_MOTION_EVENT,
  41. //! A joystick (joypad, gamepad) input event.
  42. /** Joystick events are created by polling all connected joysticks once per
  43. device run() and then passing the events to IrrlichtDevice::postEventFromUser.
  44. They take the same path as mouse events.
  45. Windows, SDL: Implemented.
  46. Linux: Implemented, with POV hat issues.
  47. MacOS / Other: Not yet implemented.
  48. */
  49. EET_JOYSTICK_INPUT_EVENT,
  50. //! A log event
  51. /** Log events are only passed to the user receiver if there is one. If they are absorbed by the
  52. user receiver then no text will be sent to the console. */
  53. EET_LOG_TEXT_EVENT,
  54. //! A user event with user data.
  55. /** This is not used by Irrlicht and can be used to send user
  56. specific data though the system. The Irrlicht 'window handle'
  57. can be obtained from IrrlichtDevice::getExposedVideoData()
  58. The usage and behavior depends on the operating system:
  59. Windows: send a WM_USER message to the Irrlicht Window; the
  60. wParam and lParam will be used to populate the
  61. UserData1 and UserData2 members of the SUserEvent.
  62. Linux: send a ClientMessage via XSendEvent to the Irrlicht
  63. Window; the data.l[0] and data.l[1] members will be
  64. cast to s32 and used as UserData1 and UserData2.
  65. MacOS: Not yet implemented
  66. */
  67. EET_USER_EVENT,
  68. //! Application state events like a resume, pause etc.
  69. EET_APPLICATION_EVENT,
  70. //! This enum is never used, it only forces the compiler to
  71. //! compile these enumeration values to 32 bit.
  72. EGUIET_FORCE_32_BIT = 0x7fffffff
  73. };
  74. //! Enumeration for all mouse input events
  75. enum EMOUSE_INPUT_EVENT
  76. {
  77. //! Left mouse button was pressed down.
  78. EMIE_LMOUSE_PRESSED_DOWN = 0,
  79. //! Right mouse button was pressed down.
  80. EMIE_RMOUSE_PRESSED_DOWN,
  81. //! Middle mouse button was pressed down.
  82. EMIE_MMOUSE_PRESSED_DOWN,
  83. //! Left mouse button was left up.
  84. EMIE_LMOUSE_LEFT_UP,
  85. //! Right mouse button was left up.
  86. EMIE_RMOUSE_LEFT_UP,
  87. //! Middle mouse button was left up.
  88. EMIE_MMOUSE_LEFT_UP,
  89. //! The mouse cursor changed its position.
  90. EMIE_MOUSE_MOVED,
  91. //! The mouse wheel was moved. Use Wheel value in event data to find out
  92. //! in what direction and how fast.
  93. EMIE_MOUSE_WHEEL,
  94. //! Left mouse button double click.
  95. //! This event is generated after the second EMIE_LMOUSE_PRESSED_DOWN event.
  96. EMIE_LMOUSE_DOUBLE_CLICK,
  97. //! Right mouse button double click.
  98. //! This event is generated after the second EMIE_RMOUSE_PRESSED_DOWN event.
  99. EMIE_RMOUSE_DOUBLE_CLICK,
  100. //! Middle mouse button double click.
  101. //! This event is generated after the second EMIE_MMOUSE_PRESSED_DOWN event.
  102. EMIE_MMOUSE_DOUBLE_CLICK,
  103. //! Left mouse button triple click.
  104. //! This event is generated after the third EMIE_LMOUSE_PRESSED_DOWN event.
  105. EMIE_LMOUSE_TRIPLE_CLICK,
  106. //! Right mouse button triple click.
  107. //! This event is generated after the third EMIE_RMOUSE_PRESSED_DOWN event.
  108. EMIE_RMOUSE_TRIPLE_CLICK,
  109. //! Middle mouse button triple click.
  110. //! This event is generated after the third EMIE_MMOUSE_PRESSED_DOWN event.
  111. EMIE_MMOUSE_TRIPLE_CLICK,
  112. //! Mouse enters canvas used for rendering.
  113. //! Only generated on emscripten
  114. EMIE_MOUSE_ENTER_CANVAS,
  115. //! Mouse leaves canvas used for rendering.
  116. //! Only generated on emscripten
  117. EMIE_MOUSE_LEAVE_CANVAS,
  118. //! No real event. Just for convenience to get number of events
  119. EMIE_COUNT
  120. };
  121. //! Masks for mouse button states
  122. enum E_MOUSE_BUTTON_STATE_MASK
  123. {
  124. EMBSM_LEFT = 0x01,
  125. EMBSM_RIGHT = 0x02,
  126. EMBSM_MIDDLE = 0x04,
  127. //! currently only on windows
  128. EMBSM_EXTRA1 = 0x08,
  129. //! currently only on windows
  130. EMBSM_EXTRA2 = 0x10,
  131. EMBSM_FORCE_32_BIT = 0x7fffffff
  132. };
  133. //! Enumeration for all touch input events
  134. enum ETOUCH_INPUT_EVENT
  135. {
  136. //! Touch was pressed down.
  137. ETIE_PRESSED_DOWN = 0,
  138. //! Touch was left up.
  139. ETIE_LEFT_UP,
  140. //! The touch changed its position.
  141. ETIE_MOVED,
  142. //! No real event. Just for convenience to get number of events
  143. ETIE_COUNT
  144. };
  145. //! Enumeration for a commonly used application state events (it's useful mainly for mobile devices)
  146. enum EAPPLICATION_EVENT_TYPE
  147. {
  148. //! The application will be resumed.
  149. EAET_WILL_RESUME = 0,
  150. //! The application has been resumed.
  151. EAET_DID_RESUME,
  152. //! The application will be paused.
  153. EAET_WILL_PAUSE,
  154. //! The application has been paused.
  155. EAET_DID_PAUSE,
  156. //! The application will be terminated.
  157. EAET_WILL_TERMINATE,
  158. //! The application received a memory warning.
  159. EAET_MEMORY_WARNING,
  160. //! The display density changed (only works on SDL).
  161. EAET_DPI_CHANGED,
  162. //! No real event, but to get number of event types.
  163. EAET_COUNT
  164. };
  165. namespace gui
  166. {
  167. class IGUIElement;
  168. //! Enumeration for all events which are sendable by the gui system
  169. enum EGUI_EVENT_TYPE
  170. {
  171. //! A gui element has lost its focus.
  172. /** GUIEvent.Caller is losing the focus to GUIEvent.Element.
  173. If the event is absorbed then the focus will not be changed. */
  174. EGET_ELEMENT_FOCUS_LOST = 0,
  175. //! A gui element has got the focus.
  176. /** If the event is absorbed then the focus will not be changed. */
  177. EGET_ELEMENT_FOCUSED,
  178. //! The mouse cursor hovered over a gui element.
  179. /** If an element has sub-elements you also get this message for the subelements */
  180. EGET_ELEMENT_HOVERED,
  181. //! The mouse cursor left the hovered element.
  182. /** If an element has sub-elements you also get this message for the subelements */
  183. EGET_ELEMENT_LEFT,
  184. //! An element would like to close.
  185. /** Windows and context menus use this event when they would like to close,
  186. this can be canceled by absorbing the event. */
  187. EGET_ELEMENT_CLOSED,
  188. //! A button was clicked.
  189. EGET_BUTTON_CLICKED,
  190. //! A scrollbar has changed its position.
  191. EGET_SCROLL_BAR_CHANGED,
  192. //! A checkbox has changed its check state.
  193. EGET_CHECKBOX_CHANGED,
  194. //! A listbox would like to open.
  195. /** You can prevent the listbox from opening by absorbing the event. */
  196. EGET_LISTBOX_OPENED,
  197. //! A new item in a listbox was selected.
  198. /** NOTE: You also get this event currently when the same item was clicked again after more than 500 ms. */
  199. EGET_LISTBOX_CHANGED,
  200. //! An item in the listbox was selected, which was already selected.
  201. /** NOTE: You get the event currently only if the item was clicked again within 500 ms or selected by "enter" or "space". */
  202. EGET_LISTBOX_SELECTED_AGAIN,
  203. //! A file has been selected in the file dialog
  204. EGET_FILE_SELECTED,
  205. //! A directory has been selected in the file dialog
  206. EGET_DIRECTORY_SELECTED,
  207. //! A file open dialog has been closed without choosing a file
  208. EGET_FILE_CHOOSE_DIALOG_CANCELLED,
  209. //! In an editbox 'ENTER' was pressed
  210. EGET_EDITBOX_ENTER,
  211. //! The text in an editbox was changed. This does not include automatic changes in text-breaking.
  212. EGET_EDITBOX_CHANGED,
  213. //! The marked area in an editbox was changed.
  214. EGET_EDITBOX_MARKING_CHANGED,
  215. //! The tab was changed in an tab control
  216. EGET_TAB_CHANGED,
  217. //! The selection in a combo box has been changed
  218. EGET_COMBO_BOX_CHANGED,
  219. //! A table has changed
  220. EGET_TABLE_CHANGED,
  221. EGET_TABLE_HEADER_CHANGED,
  222. EGET_TABLE_SELECTED_AGAIN,
  223. //! No real event. Just for convenience to get number of events
  224. EGET_COUNT
  225. };
  226. } // end namespace gui
  227. //! SEvents hold information about an event. See irr::IEventReceiver for details on event handling.
  228. struct SEvent
  229. {
  230. //! Any kind of GUI event.
  231. struct SGUIEvent
  232. {
  233. //! IGUIElement who called the event
  234. gui::IGUIElement *Caller;
  235. //! If the event has something to do with another element, it will be held here.
  236. gui::IGUIElement *Element;
  237. //! Type of GUI Event
  238. gui::EGUI_EVENT_TYPE EventType;
  239. };
  240. //! Any kind of mouse event.
  241. struct SMouseInput
  242. {
  243. //! X position of mouse cursor
  244. s32 X;
  245. //! Y position of mouse cursor
  246. s32 Y;
  247. //! mouse wheel delta, often 1.0 or -1.0, but can have other values < 0.f or > 0.f;
  248. /** Only valid if event was EMIE_MOUSE_WHEEL */
  249. f32 Wheel;
  250. //! True if shift was also pressed
  251. bool Shift : 1;
  252. //! True if ctrl was also pressed
  253. bool Control : 1;
  254. //! Is this a simulated mouse event generated by the engine itself?
  255. bool Simulated : 1;
  256. //! A bitmap of button states. You can use isButtonPressed() to determine
  257. //! if a button is pressed or not.
  258. u32 ButtonStates;
  259. //! Is the left button pressed down?
  260. bool isLeftPressed() const { return 0 != (ButtonStates & EMBSM_LEFT); }
  261. //! Is the right button pressed down?
  262. bool isRightPressed() const { return 0 != (ButtonStates & EMBSM_RIGHT); }
  263. //! Is the middle button pressed down?
  264. bool isMiddlePressed() const { return 0 != (ButtonStates & EMBSM_MIDDLE); }
  265. //! Type of mouse event
  266. EMOUSE_INPUT_EVENT Event;
  267. };
  268. //! Any kind of keyboard event.
  269. struct SKeyInput
  270. {
  271. //! Character corresponding to the key (0, if not a character, value undefined in key releases)
  272. wchar_t Char;
  273. //! Key which has been pressed or released
  274. EKEY_CODE Key;
  275. //! System dependent code. Only set for systems which are described below, otherwise undefined.
  276. //! Android: int32_t with physical key as returned by AKeyEvent_getKeyCode
  277. u32 SystemKeyCode;
  278. //! If not true, then the key was left up
  279. bool PressedDown : 1;
  280. //! True if shift was also pressed
  281. bool Shift : 1;
  282. //! True if ctrl was also pressed
  283. bool Control : 1;
  284. };
  285. //! String input event.
  286. struct SStringInput
  287. {
  288. //! The string that is entered
  289. core::stringw *Str;
  290. };
  291. //! Any kind of touch event.
  292. struct STouchInput
  293. {
  294. // Touch ID.
  295. size_t ID;
  296. // X position of simple touch.
  297. s32 X;
  298. // Y position of simple touch.
  299. s32 Y;
  300. // number of current touches
  301. s32 touchedCount;
  302. //! Type of touch event.
  303. ETOUCH_INPUT_EVENT Event;
  304. };
  305. //! Any kind of accelerometer event.
  306. struct SAccelerometerEvent
  307. {
  308. // X acceleration.
  309. f64 X;
  310. // Y acceleration.
  311. f64 Y;
  312. // Z acceleration.
  313. f64 Z;
  314. };
  315. //! Any kind of gyroscope event.
  316. struct SGyroscopeEvent
  317. {
  318. // X rotation.
  319. f64 X;
  320. // Y rotation.
  321. f64 Y;
  322. // Z rotation.
  323. f64 Z;
  324. };
  325. //! Any kind of device motion event.
  326. struct SDeviceMotionEvent
  327. {
  328. // X angle - roll.
  329. f64 X;
  330. // Y angle - pitch.
  331. f64 Y;
  332. // Z angle - yaw.
  333. f64 Z;
  334. };
  335. //! A joystick event.
  336. /** Unlike other events, joystick events represent the result of polling
  337. * each connected joystick once per run() of the device. Joystick events will
  338. * not be generated by default. If joystick support is available for the
  339. * active device, and @ref irr::IrrlichtDevice::activateJoysticks() has been
  340. * called, an event of this type will be generated once per joystick per
  341. * @ref IrrlichtDevice::run() regardless of whether the state of the joystick
  342. * has actually changed. */
  343. struct SJoystickEvent
  344. {
  345. enum
  346. {
  347. NUMBER_OF_BUTTONS = 32,
  348. AXIS_X = 0, // e.g. analog stick 1 left to right
  349. AXIS_Y, // e.g. analog stick 1 top to bottom
  350. AXIS_Z, // e.g. throttle, or analog 2 stick 2 left to right
  351. AXIS_R, // e.g. rudder, or analog 2 stick 2 top to bottom
  352. AXIS_U,
  353. AXIS_V,
  354. NUMBER_OF_AXES = 18 // (please tell Irrlicht maintainers if you absolutely need more axes)
  355. };
  356. /** A bitmap of button states. You can use IsButtonPressed() to
  357. ( check the state of each button from 0 to (NUMBER_OF_BUTTONS - 1) */
  358. u32 ButtonStates;
  359. /** For AXIS_X, AXIS_Y, AXIS_Z, AXIS_R, AXIS_U and AXIS_V
  360. * Values are in the range -32768 to 32767, with 0 representing
  361. * the center position. You will receive the raw value from the
  362. * joystick, and so will usually want to implement a dead zone around
  363. * the center of the range. Axes not supported by this joystick will
  364. * always have a value of 0. On Linux, POV hats are represented as axes,
  365. * usually the last two active axis.
  366. */
  367. s16 Axis[NUMBER_OF_AXES];
  368. /** The POV represents the angle of the POV hat in degrees * 100,
  369. * from 0 to 35,900. A value of 65535 indicates that the POV hat
  370. * is centered (or not present).
  371. * This value is only supported on Windows. On Linux, the POV hat
  372. * will be sent as 2 axes instead. */
  373. u16 POV;
  374. //! The ID of the joystick which generated this event.
  375. /** This is an internal Irrlicht index; it does not map directly
  376. * to any particular hardware joystick. */
  377. u8 Joystick;
  378. //! A helper function to check if a button is pressed.
  379. bool IsButtonPressed(u32 button) const
  380. {
  381. if (button >= (u32)NUMBER_OF_BUTTONS)
  382. return false;
  383. return (ButtonStates & (1 << button)) ? true : false;
  384. }
  385. };
  386. //! Any kind of log event.
  387. struct SLogEvent
  388. {
  389. //! Pointer to text which has been logged
  390. const c8 *Text;
  391. //! Log level in which the text has been logged
  392. ELOG_LEVEL Level;
  393. };
  394. //! Any kind of user event.
  395. struct SUserEvent
  396. {
  397. //! Some user specified data as int
  398. size_t UserData1;
  399. //! Another user specified data as int
  400. size_t UserData2;
  401. };
  402. // Application state event
  403. struct SApplicationEvent
  404. {
  405. EAPPLICATION_EVENT_TYPE EventType;
  406. };
  407. EEVENT_TYPE EventType;
  408. union
  409. {
  410. struct SGUIEvent GUIEvent;
  411. struct SMouseInput MouseInput;
  412. struct SKeyInput KeyInput;
  413. struct SStringInput StringInput;
  414. struct STouchInput TouchInput;
  415. struct SAccelerometerEvent AccelerometerEvent;
  416. struct SGyroscopeEvent GyroscopeEvent;
  417. struct SDeviceMotionEvent DeviceMotionEvent;
  418. struct SJoystickEvent JoystickEvent;
  419. struct SLogEvent LogEvent;
  420. struct SUserEvent UserEvent;
  421. struct SApplicationEvent ApplicationEvent;
  422. };
  423. SEvent() {
  424. EventType = static_cast<EEVENT_TYPE>(0);
  425. // zero the biggest union member we have, which clears all others too
  426. memset(&JoystickEvent, 0, sizeof(JoystickEvent));
  427. }
  428. };
  429. //! Interface of an object which can receive events.
  430. /** Many of the engine's classes inherit IEventReceiver so they are able to
  431. process events. Events usually start at a postEventFromUser function and are
  432. passed down through a chain of event receivers until OnEvent returns true. See
  433. irr::EEVENT_TYPE for a description of where each type of event starts, and the
  434. path it takes through the system. */
  435. class IEventReceiver
  436. {
  437. public:
  438. //! Destructor
  439. virtual ~IEventReceiver() {}
  440. //! Called if an event happened.
  441. /** Please take care that you should only return 'true' when you want to _prevent_ Irrlicht
  442. * from processing the event any further. So 'true' does mean that an event is completely done.
  443. * Therefore your return value for all unprocessed events should be 'false'.
  444. \return True if the event was processed.
  445. */
  446. virtual bool OnEvent(const SEvent &event) = 0;
  447. };
  448. //! Information on a joystick, returned from @ref irr::IrrlichtDevice::activateJoysticks()
  449. struct SJoystickInfo
  450. {
  451. //! The ID of the joystick
  452. /** This is an internal Irrlicht index; it does not map directly
  453. * to any particular hardware joystick. It corresponds to the
  454. * irr::SJoystickEvent Joystick ID. */
  455. u8 Joystick;
  456. //! The name that the joystick uses to identify itself.
  457. core::stringc Name;
  458. //! The number of buttons that the joystick has.
  459. u32 Buttons;
  460. //! The number of axes that the joystick has, i.e. X, Y, Z, R, U, V.
  461. /** Note: with a Linux device, the POV hat (if any) will use two axes. These
  462. * will be included in this count. */
  463. u32 Axes;
  464. //! An indication of whether the joystick has a POV hat.
  465. /** A Windows device will identify the presence or absence of the POV hat.
  466. * A Linux device cannot, and will always return POV_HAT_UNKNOWN. */
  467. enum
  468. {
  469. //! A hat is definitely present.
  470. POV_HAT_PRESENT,
  471. //! A hat is definitely not present.
  472. POV_HAT_ABSENT,
  473. //! The presence or absence of a hat cannot be determined.
  474. POV_HAT_UNKNOWN
  475. } PovHat;
  476. }; // struct SJoystickInfo
  477. } // end namespace irr