Mouse.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /******************************************************************************
  2. Use 'Ms' to access Mouse input.
  3. /******************************************************************************/
  4. struct Mouse // Mouse Input
  5. {
  6. C Vec2& pos()C {return _pos ;} void pos(C Vec2 &pos); // get/set cursor position (in Screen Coordinates)
  7. C Vec2& startPos()C {return _start_pos;} // get cursor position of first button push (in Screen Coordinates), this is equal to the most recent cursor position at the moment of first button push - "bp(0)"
  8. C Vec2& d ()C {return _delta ;} // get cursor position delta (in Screen Coordinates), delta is not affected by mouse clipping or display scale, use this for smooth unlimited mouse movement deltas (for example rotate the player)
  9. C Vec2& dc ()C {return _delta_clp;} // get cursor position delta clipped (in Screen Coordinates), delta is affected by mouse clipping and display scale, use this for 2D gui object movement, limited by mouse cursor position
  10. C Vec2& vel()C {return _vel ;} // get cursor velocity (in Screen Coordinates), velocity is not affected by mouse clipping or display scale, it's calculated based on few last positions
  11. Byte state (Int x)C {return InRange(x, _button) ? _button[x] : 0;} // get button BS_FLAG state
  12. Bool b (Int x)C {return InRange(x, _button) ? ButtonOn(_button[x]) : false;} // if button 'x' is on
  13. Bool bp (Int x)C {return InRange(x, _button) ? ButtonPd(_button[x]) : false;} // if button 'x' pushed in this frame
  14. Bool br (Int x)C {return InRange(x, _button) ? ButtonRs(_button[x]) : false;} // if button 'x' released in this frame
  15. Bool bd (Int x)C {return InRange(x, _button) ? ButtonDb(_button[x]) : false;} // if button 'x' double clicked
  16. Bool tapped (Int x)C {return InRange(x, _button) ? ButtonTp(_button[x]) : false;} // if button 'x' tapped, tapping is a single quick push and release of the button 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
  17. Bool tappedFirst(Int x)C {return tapped(x) && _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
  18. Flt wheel ()C {return _wheel.y;} // get vertical mouse wheel delta
  19. Flt wheelX()C {return _wheel.x;} // get horizontal mouse wheel delta
  20. Int wheelI ()C {return _wheel_i.y;} // get vertical mouse wheel delta in integer steps
  21. Int wheelIX()C {return _wheel_i.x;} // get horizontal mouse wheel delta in integer steps
  22. Dbl startTime()C {return _start_time;} // get time of when the latest button was pushed, obtained using "Time.appTime()"
  23. Flt life ()C {return Time.appTime()-_start_time;} // get how long the latest button is pushed
  24. Bool selecting()C {return _selecting;} // if enough movement occurred since the latest button was pushed to consider it selecting
  25. Bool dragging ()C {return _dragging ;} // if enough time has passed and movement occurred since the latest button was pushed to consider it dragging
  26. C VecI2& windowPos()C {return _window_posi;} // cursor position in Application Window in Pixel Coordinates
  27. C VecI2& desktopPos()C {return _desktop_posi;} // cursor position in System Desktop in Pixel Coordinates
  28. C VecI2& pixelDelta()C {return _deltai;} // cursor position delta in Pixel Coordinates
  29. Bool detected()C {return _detected;} // if mouse was detected in the system
  30. Flt speed()C; void speed(Flt speed); // get/set mouse movement speed, this affects only 'Ms.d', default=1
  31. SMOOTH_VALUE_MODE smooth( )C {return _sv_delta.mode( );} // get position delta smoothing, default=SV_WEIGHT4
  32. void smooth(SMOOTH_VALUE_MODE mode) { _sv_delta.mode(mode);} // set position delta smoothing
  33. CChar8* buttonName(Int x)C; // get button name, buttonName(0) -> "Mouse1", buttonName(1) -> "Mouse2", ..
  34. // test if mouse cursor is in rectangle
  35. Bool test(C Rect &rect)C {return Cuts(pos(), rect);}
  36. // cursor movement clipping
  37. Mouse& clip (C Rect *rect=null, Int window=-1); // clip mouse cursor to given rectangle, 'window'=if additionally clip to the application window client rectangle (-1=don't change, 0=off, 1=on)
  38. Mouse& freeze( ); // call this in each frame when you want to freeze the mouse cursor position
  39. // cursor visuals
  40. #if EE_PRIVATE
  41. void resetVisibility();
  42. #endif
  43. Bool visible( )C {return _visible ;} // if cursor is visible
  44. Bool hidden ( )C {return !_visible ;} // if cursor is hidden
  45. Mouse& visible(Bool visible); // set cursor visibility
  46. Mouse& toggle ( ) {return visible(!visible());} // toggle cursor visibility
  47. Mouse& show ( ) {return visible(true );} // show cursor
  48. Mouse& hide ( ) {return visible(false );} // hide cursor
  49. Mouse& cursor (C ImagePtr &image, C VecI2 &hot_spot=VecI2(0, 0), Bool hardware=true, Bool reset=false); // set cursor image, 'hot_spot'=point in image coordinates, 'hardware'=if use hardware cursor (this allows to draw the mouse cursor with full display speed, regardless of the game speed), 'reset'=if reset cursor even if the parameters are same as before
  50. // operations
  51. void eat ( ); // eat any button input from this frame so it will not be processed by the remaining codes in frame
  52. void eat (Int button); // eat 'button' input from this frame so it will not be processed by the remaining codes in frame
  53. void eatWheel( ); // eat 'wheel' input from this frame so it will not be processed by the remaining codes in frame
  54. #if EE_PRIVATE
  55. // manage
  56. void del ();
  57. void create ();
  58. void acquire(Bool on);
  59. // operations
  60. void resetCursor();
  61. void clear ();
  62. void push (Byte b, Flt double_click_time=DoubleClickTime);
  63. void release (Byte b);
  64. void update ();
  65. void clipUpdate ();
  66. void draw ();
  67. #endif
  68. #if !EE_PRIVATE
  69. private:
  70. #endif
  71. Byte _button[8];
  72. Bool _selecting, _dragging, _first, _detected, _on_client, _visible, _freezed, _clip_rect_on, _clip_window, _freeze, _action, _want_cur_hw, _locked;
  73. Int _cur;
  74. Flt _speed;
  75. Dbl _start_time, _wheel_time;
  76. Vec2 _pos, _delta, _delta_clp, _delta_relative, _vel, _start_pos, _wheel, _wheel_f;
  77. VecI2 _window_posi, _desktop_posi, _deltai, _hot_spot, _wheel_i;
  78. Rect _clip_rect;
  79. SmoothValue2 _sv_delta;
  80. SmoothValueTime2 _sv_vel;
  81. ImagePtr _image;
  82. CChar8 *_button_name[8];
  83. #if EE_PRIVATE
  84. #if WINDOWS_OLD
  85. IDirectInputDevice8 *_did;
  86. #else
  87. Ptr _did;
  88. #endif
  89. #else
  90. Ptr _did;
  91. #endif
  92. Mouse();
  93. NO_COPY_CONSTRUCTOR(Mouse);
  94. }extern
  95. Ms;
  96. /******************************************************************************/
  97. #define MOUSE_IMAGE_SIZE 0.05f
  98. /******************************************************************************/