aboutsummaryrefslogtreecommitdiff
path: root/xbmc/input/touch/generic/GenericTouchRotateDetector.cpp
blob: 6462e06c0d891aba45152863a525512ee4d76a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *      Copyright (C) 2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include <math.h>

#include "GenericTouchRotateDetector.h"

#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795028842
#endif

CGenericTouchRotateDetector::CGenericTouchRotateDetector(ITouchActionHandler *handler, float dpi)
  : IGenericTouchGestureDetector(handler, dpi),
    m_angle(0.0f)
{ }

bool CGenericTouchRotateDetector::OnTouchDown(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;

  if (m_done)
    return true;

  m_pointers[index] = pointer;
  m_angle = 0.0f;
  return true;
}

bool CGenericTouchRotateDetector::OnTouchUp(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;

  if (m_done)
    return true;

  // after lifting the primary pointer, the secondary pointer will
  // become the primary pointer in the next event
  if (index == 0)
  {
    m_pointers[0] = m_pointers[1];
    index = 1;
  }

  m_pointers[index].reset();

  if (!m_pointers[0].valid() && !m_pointers[1].valid())
    m_done = true;

  return true;
}

bool CGenericTouchRotateDetector::OnTouchMove(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;

  if (m_done)
    return true;

  // update the internal pointers
  m_pointers[index] = pointer;

  Pointer& primaryPointer = m_pointers[0];
  Pointer& secondaryPointer = m_pointers[1];

  if (!primaryPointer.valid() || !secondaryPointer.valid() ||
     (!primaryPointer.moving && !secondaryPointer.moving))
    return false;

  CVector last = primaryPointer.last - secondaryPointer.last;
  CVector current = primaryPointer.current - secondaryPointer.current;

  float length = last.length() * current.length();
  if (length != 0.0f)
  {
    float centerX = (primaryPointer.current.x + secondaryPointer.current.x) / 2;
    float centerY = (primaryPointer.current.y + secondaryPointer.current.y) / 2;
    float scalar = last.scalar(current);
    float angle = acos(scalar / length) * 180.0f / M_PI;

    // make sure the result of acos is a valid number
    if (angle == angle)
    {
      // calculate the direction of the rotation using the
      // z-component of the cross-product of last and current
      float direction = last.x * current.y - current.x * last.y;
      if (direction < 0.0f)
        m_angle -= angle;
      else
        m_angle += angle;

      OnRotate(centerX, centerY, m_angle);
    }
  }
  
  return true;
}

bool CGenericTouchRotateDetector::OnTouchUpdate(unsigned int index, const Pointer &pointer)
{
  if (index < 0 || index >= TOUCH_MAX_POINTERS)
    return false;
  
  if (m_done)
    return true;
  
  // update the internal pointers
  m_pointers[index] = pointer;
  return true;
}