Touch.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // Copyright (c) 2008-2015 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/Urho3D.h>
  23. #include <Urho3D/Network/Controls.h>
  24. #include <Urho3D/Graphics/Graphics.h>
  25. #include <Urho3D/Input/Input.h>
  26. #include <Urho3D/Graphics/Renderer.h>
  27. #include "Character.h"
  28. #include "Touch.h"
  29. const float GYROSCOPE_THRESHOLD = 0.1f;
  30. Touch::Touch(Context* context, float touchSensitivity) :
  31. Object(context),
  32. touchSensitivity_(touchSensitivity),
  33. cameraDistance_(CAMERA_INITIAL_DIST),
  34. zoom_(false),
  35. useGyroscope_(false)
  36. {
  37. }
  38. Touch::~Touch()
  39. {
  40. }
  41. void Touch::UpdateTouches(Controls& controls) // Called from HandleUpdate
  42. {
  43. zoom_ = false; // reset bool
  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. }