Input.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /******************************************************************************
  2. Use 'Accelerometer' to access Accelerometer input.
  3. Use 'Gyroscope' to access Gyroscope input.
  4. Use 'Magnetometer' to access Magnetometer input.
  5. Use 'Location*' functions to access device world location.
  6. Use 'InputButton' and 'Input' as general inputs.
  7. Use 'InputCombo' to check for keyboard button combinations.
  8. /******************************************************************************/
  9. #define INPUT_COMBO_NUM 8 // number of remembered input buttons
  10. #define LongPressTime 0.55f // amount of time to consider any button press a long press
  11. #define DoubleClickTime 0.25f // amount of time to consider Keyboard/Mouse/JoyPad button press a double click
  12. #define TouchDoubleClickTime 0.33f // amount of time to consider Touch press a double click
  13. #if EE_PRIVATE
  14. #define DragTime 0.15f
  15. #endif
  16. /******************************************************************************/
  17. // ACCELEROMETER, GYROSCOPE, LOCATION
  18. /******************************************************************************/
  19. enum LOCATION_AUTHORIZATION_STATUS
  20. {
  21. LAS_UNKNOWN , // The user has not yet made a choice regarding whether this app can use location services
  22. LAS_RESTRICTED, // The app cannot make use of Location because the device has this restricted (for example due to Parental Controls)
  23. LAS_DENIED , // The user explicitly denied the use of location services for this app or location services are currently disabled in Settings.
  24. LAS_FOREGROUND, // The app is authorized to make use of Location while running in foreground
  25. LAS_BACKGROUND, // The app is authorized to make use of Location while running in foreground and background
  26. };
  27. C Vec& Accelerometer(); // get current value of the accelerometer , if not supported then (0, 0, 0) is returned
  28. C Vec& Gyroscope (); // get current value of the gyroscope (radians/second), if not supported then (0, 0, 0) is returned
  29. C Vec& Magnetometer (); // get current value of the magnetometer , if not supported then (0, 0, 0) is returned
  30. Dbl LocationLatitude (Bool gps=true); // get last known location latitude (in degrees , 0 if unknown), 'gps'=if obtain location using GPS (more precision) or Network (cell towers and internet access points)
  31. Dbl LocationLongitude(Bool gps=true); // get last known location longitude (in degrees , 0 if unknown), 'gps'=if obtain location using GPS (more precision) or Network (cell towers and internet access points)
  32. Flt LocationAltitude (Bool gps=true); // get last known location altitude (in meters above sea level, 0 if unknown), 'gps'=if obtain location using GPS (more precision) or Network (cell towers and internet access points)
  33. Flt LocationAccuracy (Bool gps=true); // get last known location precision (in meters , 0 if unknown), 'gps'=if obtain location using GPS (more precision) or Network (cell towers and internet access points)
  34. Flt LocationSpeed (Bool gps=true); // get last known location speed (in meters per second , 0 if unknown), 'gps'=if obtain location using GPS (more precision) or Network (cell towers and internet access points)
  35. C DateTime& LocationTimeUTC (Bool gps=true); // get last known location time (in UTC time zone , zero if unknown), 'gps'=if obtain location using GPS (more precision) or Network (cell towers and internet access points)
  36. void LocationRefresh(Flt interval, Bool gps=true, Bool background=false); // after calling this function the device will start refreshing its location continuously each 'interval' seconds, 'gps'=if refresh the GPS (more precision) or Network (cell towers and internet access points), 'interval'=seconds between sequential updates (0..Inf) - small interval results in more frequent updates while big interval conserves the battery better. 'background'=if allow location updates even when the application is in background mode. This function can be called just once and the location will start refreshing itself as long as the app is active or until 'LocationDisable' is called. This and all other Location functions operate on GPS and Network separately, which means that you can start and stop refreshing both GPS and the Network independently.
  37. void LocationDisable( Bool gps=true ); // after calling this function the device will stop refreshing its location , 'gps'=if disable the GPS or Network
  38. LOCATION_AUTHORIZATION_STATUS LocationAuthorizationStatus(); // get the current Location Authorization Status (this is used only on Apple platforms)
  39. void MagnetometerRefresh(Flt interval=1.0f/30); // after calling this function the device will start refreshing direction of the magnetic field continuously each 'interval' seconds
  40. void MagnetometerDisable( ); // after calling this function the device will stop refreshing direction of the magnetic field
  41. /******************************************************************************/
  42. // VIRTUAL REALITY
  43. /******************************************************************************/
  44. struct VirtualReality // Virtual Reality - Head Mounted Display (HMD)
  45. {
  46. Bool draw_2d; // if draw 2D layer on top of 3D (this includes manually drawn 2D graphics, Gui and mouse cursor), if you don't use these, then disable this parameter for better performance, default=true
  47. // OpenVR SDK (usage of these methods requires linking to OpenVR SDK, increasing your executable file size and requiring the OpenVR DLL file to be present in the app folder)
  48. Bool OpenVRDetected()C; // if OpenVR-compatible HMD is currently connected to the computer
  49. Bool OpenVRInit () ; // initialize OpenVR-compatible HMD, this must be called in 'InitPre' if you want to use the device in your app, returns false if failed to initialize, true is returned if succeeded to initialize even if device is not currently connected
  50. // Oculus Rift SDK (usage of these methods requires linking to Oculus Rift SDK and increasing your executable file size)
  51. Bool OculusRiftDetected()C; // if Oculus Rift is currently connected to the computer
  52. Bool OculusRiftInit () ; // initialize Oculus Rift, this must be called in 'InitPre' if you want to use Oculus Rift in your app, returns false if failed to initialize, true is returned if succeeded to initialize even if device is not currently connected
  53. // Dummy
  54. void DummyInit(); // this will set the engine to think as if it was connected to a VR headset, which you can use for testing your app without a headset
  55. // get / set
  56. Bool active ()C; // if HMD is currently connected to the computer and if one of the '*Init' methods were called with success
  57. CChar8* name ()C {return _name ;} // get HMD name
  58. C VecI2& res ()C {return _res ;} // get HMD screen resolution
  59. Flt eyeDistance ()C {return _eye_dist ;} // get HMD recommended eye distance as set in default profile
  60. Flt refreshRate ()C {return _refresh ;} // get HMD refresh rate
  61. C Vec2& fov ()C {return _fov ;} // get HMD horizontal and vertical Field of View (in radians)
  62. Matrix matrixCur ()C; // get HMD pose at current moment , if not supported then 'MatrixIdentity' is returned
  63. C Matrix& matrix ()C {return _matrix ;} // get HMD pose at the start of current frame , if not supported then 'MatrixIdentity' is returned
  64. C Matrix& leftHand ()C {return _left ;} // get Left Hand pose at the start of current frame, if not supported then 'MatrixIdentity' is returned
  65. C Matrix& rightHand ()C {return _right ;} // get Right Hand pose at the start of current frame, if not supported then 'MatrixIdentity' is returned
  66. Flt pixelDensity()C {return _density ;} VirtualReality& pixelDensity(Flt density ); // get/set HMD rendering pixel density, 0..2, default=1
  67. C VecI2& guiRes ()C {return _gui_res ;} VirtualReality& guiRes (Int w, Int h); // get/set HMD gui resolution, 1x1 .. Inf x Inf, default=1024x1024
  68. Flt guiDepth ()C {return _gui_depth;} VirtualReality& guiDepth (Flt depth ); // get/set HMD gui depth (distance from camera in meters), 0..Inf, default=1, changing 'guiDepth' does not affect visible gui size
  69. Flt guiSize ()C {return _gui_size ;} VirtualReality& guiSize (Flt size ); // get/set HMD gui size (size when seen at 1 m distance), 0..Inf, default=1
  70. // operations
  71. void shut (); // manually shut down HMD, normally you don't need to call this, as it will be called by the engine automatically
  72. void recenter(); // reset HMD yaw orientation angle and position offset
  73. #if !EE_PRIVATE
  74. private:
  75. #endif
  76. Bool _has_render;
  77. Char8 _name[64];
  78. Flt _eye_dist, _density, _refresh, _gui_depth, _gui_size, _left_eye_tex_aspect;
  79. Vec2 _fov;
  80. VecI2 _res, _gui_res;
  81. Rect _left_eye_tex_rect;
  82. Matrix _matrix, _left, _right;
  83. U64 _adapter_id;
  84. VirtualRealityApi *_api;
  85. #if EE_PRIVATE
  86. Bool init (VirtualRealityApi &api);
  87. void update ();
  88. void draw ();
  89. void drawMain ();
  90. Bool connected();
  91. void disconnected();
  92. void setFOVTan (Flt left, Flt right, Flt up, Flt down);
  93. void delImages();
  94. Bool createImages();
  95. Bool createGuiImage ();
  96. Bool createRenderImage ();
  97. ImageRC* getNewRender ();
  98. ImageRC* getNewGui ();
  99. ImageRC* getLastRender();
  100. ImageRC* getLastGui ();
  101. #endif
  102. VirtualReality();
  103. NO_COPY_CONSTRUCTOR(VirtualReality);
  104. }extern
  105. VR;
  106. #if EE_PRIVATE
  107. struct VirtualRealityApi
  108. {
  109. virtual Bool init() {return false;}
  110. virtual void shut() {}
  111. virtual Bool active ()C {return false;}
  112. virtual Matrix matrixCur ()C {return VR._matrix;} // return last known matrix
  113. virtual void recenter () {}
  114. virtual void changedGuiDepth() {}
  115. virtual void changedGuiSize () {}
  116. virtual void update () {}
  117. virtual void draw () {}
  118. virtual void delImages() {}
  119. virtual Bool createGuiImage () {return false;}
  120. virtual Bool createRenderImage () {return false;}
  121. virtual ImageRC* getNewRender () {return null;}
  122. virtual ImageRC* getNewGui () {return null;}
  123. virtual ImageRC* getLastRender() {return null;}
  124. virtual ImageRC* getLastGui () {return null;}
  125. virtual ~VirtualRealityApi() {shut();}
  126. }extern
  127. *VrApi;
  128. #endif
  129. /******************************************************************************/
  130. // GENERAL INPUT
  131. /******************************************************************************/
  132. enum INPUT_TYPE : Byte // Input Device Type
  133. {
  134. INPUT_NONE , // None
  135. INPUT_KEYBOARD, // Keyboard
  136. INPUT_MOUSE , // Mouse
  137. INPUT_JOYPAD , // Joypad
  138. };
  139. /******************************************************************************/
  140. struct InputButton // universal Input Button, may be a button from Keyboard, Mouse, Joypad
  141. {
  142. INPUT_TYPE type ; // input type
  143. Byte button, // button index (KB_KEY for keyboard or index for Mouse/Joypad)
  144. device; // device index (for example Joypad device index, unused for Keyboard and Mouse)
  145. Bool operator==(C InputButton &b)C; // if input buttons are equal
  146. Bool operator!=(C InputButton &b)C; // if input buttons are different
  147. Bool on()C; // if button on
  148. Bool pd()C; // if button pushed
  149. Bool rs()C; // if button released
  150. Bool db()C; // if button double clicked
  151. Str name()C; // get button name, can be one of the following : Kb.buttonName(button), Ms.buttonName(button), Joypads[device].buttonName(button), null
  152. explicit InputButton(INPUT_TYPE type=INPUT_NONE, Byte button=0, Byte device=0) {T.type=type; T.button=button; T.device=device;}
  153. };
  154. /******************************************************************************/
  155. struct Input // Action Input (for example "walk_forward"), may be defined by maximum 3 custom buttons
  156. {
  157. UInt group; // custom input group (for example Movement, Interaction, Combat, ...)
  158. Str name ; // action name
  159. InputButton b[3] ; // 3 custom buttons
  160. Bool on()C; // if action on (checks if any of the 3 buttons is on )
  161. Bool pd()C; // if action pushed (checks if any of the 3 buttons is pushed )
  162. Bool rs()C; // if action released (checks if any of the 3 buttons is released )
  163. Bool db()C; // if action double clicked (checks if any of the 3 buttons is double clicked)
  164. void set(C Str &name=S, UInt group=0 ); // set action properties
  165. void set(C Str &name , UInt group, C InputButton &b0, C InputButton *b1=null, C InputButton *b2=null); // set action properties
  166. Bool operator()(C InputButton &b)C; // if 'b' button enables action, this checks if any of the 3 buttons is equal to 'b'
  167. Input() {group=0;}
  168. };
  169. /******************************************************************************/
  170. // INPUT COMBINATION
  171. /******************************************************************************/
  172. struct InputComboClass // Input Combination, used for detecting special moves/attacks
  173. {
  174. void clear(); // clear stored combination
  175. Bool operator()(C Input &i0, C Input &i1 )C; // if action 'i0' was pressed, followed by 'i1'
  176. Bool operator()(C Input &i0, C Input &i1, C Input &i2 )C; // if action 'i0' was pressed, followed by 'i1', 'i2'
  177. Bool operator()(C Input &i0, C Input &i1, C Input &i2, C Input &i3 )C; // if action 'i0' was pressed, followed by 'i1', 'i2', 'i3'
  178. Bool operator()(C Input &i0, C Input &i1, C Input &i2, C Input &i3, C Input &i4)C; // if action 'i0' was pressed, followed by 'i1', 'i2', 'i3', 'i4'
  179. #if EE_PRIVATE
  180. void add(C InputButton &inp_btn);
  181. #endif
  182. InputComboClass();
  183. private:
  184. Byte pos, length;
  185. Flt dt;
  186. Dbl t[INPUT_COMBO_NUM];
  187. InputButton b[INPUT_COMBO_NUM];
  188. }extern
  189. InputCombo;
  190. /******************************************************************************/
  191. #if EE_PRIVATE
  192. struct InputDevicesClass // Input Devices
  193. {
  194. #if WINDOWS_OLD
  195. IDirectInput8 *DI;
  196. #else
  197. Ptr DI;
  198. #endif
  199. void del ();
  200. Bool create ();
  201. void update ();
  202. void clear ();
  203. void acquire(Bool on);
  204. InputDevicesClass() {DI=null;}
  205. NO_COPY_CONSTRUCTOR(InputDevicesClass);
  206. }extern
  207. InputDevices;
  208. #endif
  209. /******************************************************************************/
  210. struct TextEdit // Text Edit Settings
  211. {
  212. Bool overwrite, // if cursor is in overwriting mode
  213. password ; // if editing is in password mode from which you can't copy the text
  214. Int cur , // cursor position
  215. sel ; // selection position, -1=none
  216. void reset() {overwrite=password=false; cur=0; sel=-1;}
  217. TextEdit() {reset();}
  218. };
  219. Bool EditText(Str &str, TextEdit &edit, Bool multi_line=false); // edit 'str' text according to keyboard input, returns true if text was changed
  220. /******************************************************************************/
  221. #if EE_PRIVATE
  222. extern Vec AccelerometerValue, GyroscopeValue, MagnetometerValue;
  223. extern Bool LocationBackground[2];
  224. extern Dbl LocationLat[2], LocationLon[2];
  225. extern Flt LocationAlt[2], LocationAcc[2], LocationSpd[2], LocationInterval[2], MagnetometerInterval;
  226. extern DateTime LocationTim[2];
  227. #if ANDROID
  228. extern ASensorEventQueue *SensorEventQueue;
  229. #elif IOS
  230. extern CLLocationManager *LocationManager[2];
  231. #endif
  232. void SetMagnetometerRefresh(Flt interval);
  233. void SetLocationRefresh(Flt interval, Bool gps=true);
  234. #if ANDROID
  235. void UpdateLocation(jobject location, Bool gps, JNI &jni);
  236. void UpdateLocation( Bool gps, JNI &jni);
  237. void UpdateLocation( JNI &jni);
  238. #endif
  239. #endif
  240. /******************************************************************************/