Touch.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /******************************************************************************
  2. Use 'Touches' container to access all active touches.
  3. /******************************************************************************/
  4. struct Touch // Single Touch on a Touch-Screen
  5. {
  6. UInt user_type; // user data, default=0 , this can be freely modified, for example you can set this to a custom type when the touch is pushed ('pd' method)
  7. Ptr user_ptr ; // user data, default=null, this can be freely modified, for example you can set this to a custom pointer when the touch is pushed ('pd' method)
  8. C Vec2& startPos()C {return _start_pos;} // starting position, this is equal to the touch position at the moment of its start
  9. C Vec2& pos()C {return _pos ;} // current position
  10. C Vec2& smoothPos()C {return _sm_pos ;} // current position with smoothing applied
  11. C Vec2& d()C {return _delta ;} // position delta since last frame
  12. C Vec2& ad()C {return _abs_delta;} // absolute position delta since last frame (unaffected by current display scale)
  13. C Vec2& vel()C {return _vel ;} // absolute velocity (unaffected by current display scale), it's calculated based on few last positions
  14. C VecI2& pixelDelta()C {return _deltai;} // cursor position delta in Pixel Coordinates
  15. Byte state ()C {return _state ;} // get BS_FLAG state
  16. Bool on ()C {return ButtonOn(_state) ;} // if is on
  17. Bool pd ()C {return ButtonPd(_state) ;} // if pushed in this frame
  18. Bool rs ()C {return ButtonRs(_state) ;} // if released in this frame
  19. Bool db ()C {return ButtonDb(_state) ;} // if double clicked
  20. Bool tapped ()C {return ButtonTp(_state) ;} // if tapped, tapping is a single quick push and release without any movement, this can be true at the moment of the release with the condition that there was no movement and the touch life was very short
  21. Bool tappedFirst()C {return tapped() && _first;} // if tapped which was caused by first click of a double-click, double-clicks generate two taps, you can use this method to detect only the first one
  22. Flt force()C {return _force;} // force of the touch in range >=0, 0=no force (hover), 1=average touch
  23. Dbl startTime()C {return _start_time;} // time of when the touch has started, obtained using "Time.appTime()"
  24. Flt life ()C {return Time.appTime()-_start_time;} // how long the touch is active
  25. Bool selecting()C {return _selecting;} // if enough movement occurred since the touch start to consider it selecting
  26. Bool dragging ()C {return _dragging ;} // if enough time has passed and movement occurred since the touch start to consider it dragging
  27. Bool scrolling()C {return _scrolling;} // if this touch is currently being used to scroll a Region
  28. GuiObj* guiObj( )C {return _gui_obj;} // get gui object at touch position when the touch was started
  29. void guiObj(GuiObj *obj) { _gui_obj=obj;} // manually change the gui object for this touch
  30. UInt id()C {return _id;} // get unique id which was initialized at the start of the touch
  31. Bool stylus()C {return _stylus;} // if this touch is generated with a stylus
  32. // operations
  33. void eat(); // eat input state of this touch from this frame so it will not be processed by the remaining codes in frame
  34. Touch();
  35. #if !EE_PRIVATE
  36. private:
  37. #endif
  38. Bool _selecting, _dragging, _scrolling, _stylus, _first, _remove;
  39. Byte _state, _axis_moved;
  40. UInt _id;
  41. Flt _force;
  42. Dbl _start_time;
  43. VecI2 _posi, _deltai;
  44. Vec2 _start_pos, _prev_pos, _pos, _sm_pos, _delta, _abs_delta, _vel;
  45. CPtr _handle;
  46. GuiObj *_gui_obj;
  47. SmoothValue2 _sv_pos;
  48. SmoothValueTime2 _sv_vel;
  49. #if EE_PRIVATE
  50. Touch& init(C VecI2 &posi, C Vec2 &pos, CPtr handle, Bool stylus);
  51. Touch& reinit(C VecI2 &posi, C Vec2 &pos);
  52. #endif
  53. };
  54. extern MemtN<Touch, 10> Touches; // container of active Touches
  55. Touch* FindTouch (UInt id); // find touch in 'Touches' container according to its 'id', null on fail
  56. void SimulateTouches(Bool on); // if enabled then pressing mouse button on the screen will simulate the 'Touches' behavior, enabling you to develop touch based applications (for example iPhone) without the need of a touch screen but just a mouse (on a PC/Mac). In the iPhone SDK simulator the touches are always simulated using the mouse (there's no need to call this function). default=false
  57. Bool SupportedTouches( ); // if touch input is supported
  58. #if EE_PRIVATE
  59. extern Bool TouchesSupported;
  60. Touch* FindTouchByHandle(CPtr handle); // find touch in 'Touches' container according to its '_handle', null on fail
  61. void TouchesUpdate();
  62. void TouchesClear ();
  63. #endif
  64. /******************************************************************************/
  65. struct MouseTouch // Mouse and Touch input combined into one class
  66. {
  67. static C Vec2 & startPos(Int i) {return InRange(i, Touches) ? Touches[i]. startPos() : Ms. startPos();} // get starting position of i-th touch or mouse
  68. static C Vec2 & pos(Int i) {return InRange(i, Touches) ? Touches[i]. pos() : Ms. pos();} // get current position of i-th touch or mouse
  69. static C Vec2 & smoothPos(Int i) {return InRange(i, Touches) ? Touches[i]. smoothPos() : Ms. pos();} // get current position with smoothing applied of i-th touch or mouse
  70. static C Vec2 & d(Int i) {return InRange(i, Touches) ? Touches[i]. d() : Ms. d();} // get position delta of i-th touch or mouse
  71. static C Vec2 & ad(Int i) {return InRange(i, Touches) ? Touches[i]. ad() : Ms. d();} // get absolute position delta of i-th touch or mouse
  72. static C Vec2 & dc(Int i) {return InRange(i, Touches) ? Touches[i]. d() : Ms. dc();} // get position delta clipped of i-th touch or mouse
  73. static C Vec2 & vel(Int i) {return InRange(i, Touches) ? Touches[i]. vel() : Ms. vel();} // get velocity of i-th touch or mouse
  74. static C VecI2& pixelDelta(Int i) {return InRange(i, Touches) ? Touches[i].pixelDelta() : Ms.pixelDelta();} // get position delta in Pixel Coordinates of i-th touch or mouse
  75. static Byte state (Int i, Int b=0) {return InRange(i, Touches) ? (b==0 ? Touches[i].state () : 0) : Ms.state (b);} // get button 'b' BS_FLAG state of i-th touch or mouse
  76. static Bool b (Int i, Int b=0) {return InRange(i, Touches) ? (b==0 && Touches[i].on () ) : Ms.b (b);} // get if button 'b' on of i-th touch or mouse
  77. static Bool bp (Int i, Int b=0) {return InRange(i, Touches) ? (b==0 && Touches[i].pd () ) : Ms.bp (b);} // get if button 'b' pushed of i-th touch or mouse
  78. static Bool br (Int i, Int b=0) {return InRange(i, Touches) ? (b==0 && Touches[i].rs () ) : Ms.br (b);} // get if button 'b' released of i-th touch or mouse
  79. static Bool bd (Int i, Int b=0) {return InRange(i, Touches) ? (b==0 && Touches[i].db () ) : Ms.bd (b);} // get if button 'b' double clicked of i-th touch or mouse
  80. static Bool tapped (Int i, Int b=0) {return InRange(i, Touches) ? (b==0 && Touches[i].tapped () ) : Ms.tapped (b);} // if tapped, tapping is a single quick push and release without any movement, this can be true at the moment of the release with the condition that there was no movement and the push life was very short
  81. static Bool tappedFirst(Int i, Int b=0) {return InRange(i, Touches) ? (b==0 && Touches[i].tappedFirst() ) : Ms.tappedFirst(b);} // if tapped which was caused by first click of a double-click, double-clicks generate two taps, you can use this method to detect only the first one
  82. static Dbl startTime(Int i) {return InRange(i, Touches) ? Touches[i].startTime() : Ms.startTime();} // time of when the main button was pressed, obtained using "Time.appTime()"
  83. static Flt life (Int i) {return InRange(i, Touches) ? Touches[i].life () : Ms.life ();} // how long the main button is active
  84. static Bool selecting(Int i) {return InRange(i, Touches) ? Touches[i].selecting() : Ms.selecting();} // if enough movement occurred since the button was pressed to consider it selecting
  85. static Bool dragging (Int i) {return InRange(i, Touches) ? Touches[i].dragging () : Ms.dragging ();} // if enough time has passed and movement occurred since the button was pressed to consider it dragging
  86. static Bool mouse (Int i) {return InRange(i, Touches) ? false : true;} // if input is from a mouse
  87. static Touch* touch (Int i) {return InRange(i, Touches) ? &Touches[i] : null;} // get touch linked with i-th input
  88. static UInt id (Int i) {return InRange(i, Touches) ? Touches[i].id () : 0 ;} // get unique id which was initialized at the start of the touch
  89. static Bool hoverable(Int i) {return InRange(i, Touches) ? Touches[i].stylus() : true;} // if input is hoverable (it's a stylus or a mouse)
  90. static GuiObj* guiObj(Int i ) {return InRange(i, Touches) ? Touches[i].guiObj() : Gui.ms();} // get gui object focus of i-th touch or mouse
  91. static void guiObj(Int i, GuiObj *obj); // manually change the gui object for i-th touch or mouse
  92. static void eat(Int i ) {return InRange(i, Touches) ? Touches[i].eat() : Ms.eat( );} // eat any button input of i-th touch or mouse from this frame so it will not be processed by the remaining codes in frame
  93. static void eat(Int i, Int b) {return InRange(i, Touches) ? Touches[i].eat() : Ms.eat(b);} // eat 'button' input of i-th touch or mouse from this frame so it will not be processed by the remaining codes in frame
  94. static Int elms() {return Touches.elms()+Ms.detected();} // all touches + mouse
  95. }extern
  96. MT;
  97. inline Int Elms(C MouseTouch &mt) {return mt.elms();}
  98. /******************************************************************************/