This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 821
/
ManipulatableObject.cpp
178 lines (156 loc) · 6.38 KB
/
ManipulatableObject.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
#include "pch.h"
#include "ManipulatableObject.h"
///////////////////////////////////////////////////////////////////////////////
// ManipulatableObject base class.
//
ManipulatableObject::ManipulatableObject(_In_opt_ ManipulatableObject^ parent)
: _parent(parent),
_mxManip(D2D1::Matrix3x2F::Identity()),
_ptInit(D2D1::Point2F())
{
}
///////////////////////////////////////////////////////////////////////////////
// Manipulation update calculations.
//
Windows::Foundation::Point ManipulatableObject::SingleFingerRotation(
_In_ Windows::UI::Input::GestureRecognizer^ gestureRecognizer,
_In_ Windows::Foundation::Point position,
_In_ float pivotRadius)
{
// Object's center in its own coordinate system is (0,0). In parent coordinate system it is:
D2D1_POINT_2F objectCenter = LocalTransform().TransformPoint(D2D1::Point2F());
if (gestureRecognizer->IsInertial)
{
// During the inertia phase, GestureRecognizer reports the same center point as it was on the last manipulation update.
// Override manipulation center point to be object's center.
position = Windows::Foundation::Point(objectCenter.x, objectCenter.y);
}
else
{
// Set pivot for single finger rotation to be object's center.
gestureRecognizer->PivotCenter = Windows::Foundation::Point(objectCenter.x, objectCenter.y);
gestureRecognizer->PivotRadius = pivotRadius;
}
return position;
}
Windows::UI::Input::ManipulationDelta ManipulatableObject::LimitManipulationScale(
_In_ Windows::UI::Input::ManipulationDelta delta,
_In_ float minScale,
_In_ float maxScale)
{
float scale = sqrt(fabs(_mxManip.Determinant()));
float scaleNew = scale * delta.Scale;
if ((minScale != 0.0f) && (scaleNew > maxScale) && (scale > 0))
{
delta.Scale = maxScale / scale;
}
if ((maxScale != 0.0f) && (scaleNew < minScale) && (scale > 0))
{
delta.Scale = minScale / scale;
}
return delta;
}
void ManipulatableObject::UpdateManipulationTransform(
_In_ Windows::UI::Input::ManipulationDelta delta,
_In_ Windows::Foundation::Point position)
{
// delta and position are in parent's coordinate system
D2D1_POINT_2F center = D2D1::Point2F(position.X, position.Y);
D2D1::Matrix3x2F mxDelta =
D2D1::Matrix3x2F::Scale(delta.Scale, delta.Scale, center) *
D2D1::Matrix3x2F::Rotation(delta.Rotation, center) *
D2D1::Matrix3x2F::Translation(delta.Translation.X, delta.Translation.Y);
_mxManip = _mxManip * mxDelta;
}
///////////////////////////////////////////////////////////////////////////////
// Serialization/deserialization methods.
//
void ManipulatableObject::Serialize(_In_ Windows::Foundation::Collections::IPropertySet^ properties)
{
properties->Insert("_11", Windows::Foundation::PropertyValue::CreateSingle(_mxManip._11));
properties->Insert("_12", Windows::Foundation::PropertyValue::CreateSingle(_mxManip._12));
properties->Insert("_21", Windows::Foundation::PropertyValue::CreateSingle(_mxManip._21));
properties->Insert("_22", Windows::Foundation::PropertyValue::CreateSingle(_mxManip._22));
properties->Insert("_31", Windows::Foundation::PropertyValue::CreateSingle(_mxManip._31));
properties->Insert("_32", Windows::Foundation::PropertyValue::CreateSingle(_mxManip._32));
}
void ManipulatableObject::Deserialize(_In_ Windows::Foundation::Collections::IPropertySet^ properties)
{
if (properties->HasKey("_11"))
{
_mxManip._11 = safe_cast<Windows::Foundation::IPropertyValue^>(properties->Lookup("_11"))->GetSingle();
}
if (properties->HasKey("_12"))
{
_mxManip._12 = safe_cast<Windows::Foundation::IPropertyValue^>(properties->Lookup("_12"))->GetSingle();
}
if (properties->HasKey("_21"))
{
_mxManip._21 = safe_cast<Windows::Foundation::IPropertyValue^>(properties->Lookup("_21"))->GetSingle();
}
if (properties->HasKey("_22"))
{
_mxManip._22 = safe_cast<Windows::Foundation::IPropertyValue^>(properties->Lookup("_22"))->GetSingle();
}
if (properties->HasKey("_31"))
{
_mxManip._31 = safe_cast<Windows::Foundation::IPropertyValue^>(properties->Lookup("_31"))->GetSingle();
}
if (properties->HasKey("_32"))
{
_mxManip._32 = safe_cast<Windows::Foundation::IPropertyValue^>(properties->Lookup("_32"))->GetSingle();
}
}
///////////////////////////////////////////////////////////////////////////////
// ManipulatableObjectTransform implementation.
//
ManipulatableObjectTransform::ManipulatableObjectTransform(_In_opt_ ManipulatableObject^ referentObject)
: _matrix(referentObject ? referentObject->Transform() : D2D1::Matrix3x2F::Identity())
{
// _matrix is IPointerPointTransform transform "input" transform: from client to local coordinate system
// Transform matrix used by ManipulatableObject is "output" transform: from local to client coordinate system
if (!_matrix.Invert())
{
_matrix = D2D1::Matrix3x2F::Identity();
}
}
bool ManipulatableObjectTransform::TryTransform(_In_ Windows::Foundation::Point inPoint, _Out_ Windows::Foundation::Point* outPoint)
{
D2D1_POINT_2F pt = _matrix.TransformPoint(D2D1::Point2F(inPoint.X, inPoint.Y));
outPoint->X = pt.x;
outPoint->Y = pt.y;
return TRUE;
}
Windows::Foundation::Rect ManipulatableObjectTransform::TransformBounds(_In_ Windows::Foundation::Rect rect)
{
Windows::Foundation::Point center(rect.X + rect.Width/2, rect.Y + rect.Height/2);
float scale = sqrt(fabs(_matrix.Determinant()));
if (TryTransform(center, ¢er) && (scale > 0))
{
rect.Width *= scale;
rect.Height *= scale;
rect.X = center.X - rect.Width/2;
rect.Y = center.Y - rect.Height/2;
}
return rect;
}
Windows::UI::Input::IPointerPointTransform^ ManipulatableObjectTransform::Inverse::get()
{
D2D1::Matrix3x2F matrixInv = _matrix;
if (matrixInv.Invert())
{
ManipulatableObjectTransform^ transform = ref new ManipulatableObjectTransform(nullptr);
transform->SetMatrix(matrixInv);
return transform;
}
else
{
return nullptr;
}
}