Touch.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Copyright (c) 2008-2014 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 "Controls.h"
  23. #include "Graphics.h"
  24. #include "Input.h"
  25. #include "Renderer.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()
  38. {
  39. }
  40. void Touch::UpdateTouches(Controls& controls) // Called from HandleUpdate
  41. {
  42. zoom_ = false; // reset bool
  43. Graphics* graphics = GetSubsystem<Graphics>();
  44. Input* input = GetSubsystem<Input>();
  45. // Zoom in/out
  46. if (input->GetNumTouches() == 2)
  47. {
  48. TouchState* touch1 = input->GetTouch(0);
  49. TouchState* touch2 = input->GetTouch(1);
  50. // Check for zoom pattern (touches moving in opposite directions and on empty space)
  51. if (!touch1->touchedElement_ && !touch2->touchedElement_ && ((touch1->delta_.y_ > 0 && touch2->delta_.y_ < 0) || (touch1->delta_.y_ < 0 && touch2->delta_.y_ > 0)))
  52. zoom_ = true;
  53. else
  54. zoom_ = false;
  55. if (zoom_)
  56. {
  57. int sens = 0;
  58. // Check for zoom direction (in/out)
  59. if (Abs(touch1->position_.y_ - touch2->position_.y_) > Abs(touch1->lastPosition_.y_ - touch2->lastPosition_.y_))
  60. sens = -1;
  61. else
  62. sens = 1;
  63. cameraDistance_ += Abs(touch1->delta_.y_ - touch2->delta_.y_) * sens * touchSensitivity_ / 50.0f;
  64. cameraDistance_ = Clamp(cameraDistance_, CAMERA_MIN_DIST, CAMERA_MAX_DIST); // Restrict zoom range to [1;20]
  65. }
  66. }
  67. // Gyroscope (emulated by SDL through a virtual joystick)
  68. if (useGyroscope_ && input->GetNumJoysticks() > 0) // numJoysticks = 1 on iOS & Android
  69. {
  70. JoystickState* joystick = input->GetJoystickByIndex(0);
  71. if (joystick->GetNumAxes() >= 2)
  72. {
  73. if (joystick->GetAxisPosition(0) < -GYROSCOPE_THRESHOLD)
  74. controls.Set(CTRL_LEFT, true);
  75. if (joystick->GetAxisPosition(0) > GYROSCOPE_THRESHOLD)
  76. controls.Set(CTRL_RIGHT, true);
  77. if (joystick->GetAxisPosition(1) < -GYROSCOPE_THRESHOLD)
  78. controls.Set(CTRL_FORWARD, true);
  79. if (joystick->GetAxisPosition(1) > GYROSCOPE_THRESHOLD)
  80. controls.Set(CTRL_BACK, true);
  81. }
  82. }
  83. }