Touch.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Copyright (c) 2008-2021 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Graphics/Graphics.h>
  23. #include <Urho3D/Graphics/Renderer.h>
  24. #include <Urho3D/Input/Controls.h>
  25. #include <Urho3D/Input/Input.h>
  26. #include "Character.h"
  27. #include "Touch.h"
  28. const float GYROSCOPE_THRESHOLD = 0.1f;
  29. Touch::Touch(Context* context, float touchSensitivity) :
  30. Object(context),
  31. touchSensitivity_(touchSensitivity),
  32. cameraDistance_(CAMERA_INITIAL_DIST),
  33. zoom_(false),
  34. useGyroscope_(false)
  35. {
  36. }
  37. Touch::~Touch() = default;
  38. void Touch::UpdateTouches(Controls& controls) // Called from HandleUpdate
  39. {
  40. zoom_ = false; // reset bool
  41. auto* input = GetSubsystem<Input>();
  42. // Zoom in/out
  43. if (input->GetNumTouches() == 2)
  44. {
  45. TouchState* touch1 = input->GetTouch(0);
  46. TouchState* touch2 = input->GetTouch(1);
  47. // Check for zoom pattern (touches moving in opposite directions and on empty space)
  48. if (!touch1->touchedElement_ && !touch2->touchedElement_ && ((touch1->delta_.y_ > 0 && touch2->delta_.y_ < 0) || (touch1->delta_.y_ < 0 && touch2->delta_.y_ > 0)))
  49. zoom_ = true;
  50. else
  51. zoom_ = false;
  52. if (zoom_)
  53. {
  54. int sens = 0;
  55. // Check for zoom direction (in/out)
  56. if (Abs(touch1->position_.y_ - touch2->position_.y_) > Abs(touch1->lastPosition_.y_ - touch2->lastPosition_.y_))
  57. sens = -1;
  58. else
  59. sens = 1;
  60. cameraDistance_ += Abs(touch1->delta_.y_ - touch2->delta_.y_) * sens * touchSensitivity_ / 50.0f;
  61. cameraDistance_ = Clamp(cameraDistance_, CAMERA_MIN_DIST, CAMERA_MAX_DIST); // Restrict zoom range to [1;20]
  62. }
  63. }
  64. // Gyroscope (emulated by SDL through a virtual joystick)
  65. if (useGyroscope_ && input->GetNumJoysticks() > 0) // numJoysticks = 1 on iOS & Android
  66. {
  67. JoystickState* joystick = input->GetJoystickByIndex(0);
  68. if (joystick->GetNumAxes() >= 2)
  69. {
  70. if (joystick->GetAxisPosition(0) < -GYROSCOPE_THRESHOLD)
  71. controls.Set(CTRL_LEFT, true);
  72. if (joystick->GetAxisPosition(0) > GYROSCOPE_THRESHOLD)
  73. controls.Set(CTRL_RIGHT, true);
  74. if (joystick->GetAxisPosition(1) < -GYROSCOPE_THRESHOLD)
  75. controls.Set(CTRL_FORWARD, true);
  76. if (joystick->GetAxisPosition(1) > GYROSCOPE_THRESHOLD)
  77. controls.Set(CTRL_BACK, true);
  78. }
  79. }
  80. }