Touch.cpp 3.5 KB

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