/
TiltBehavior.cs
172 lines (145 loc) · 7.27 KB
/
TiltBehavior.cs
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
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
namespace MahApps.Metro.Controls
{
public class TiltBehavior : Behavior<FrameworkElement>
{
public static readonly DependencyProperty KeepDraggingProperty =
DependencyProperty.Register("KeepDragging", typeof(bool), typeof(TiltBehavior), new PropertyMetadata(true));
public static readonly DependencyProperty TiltFactorProperty =
DependencyProperty.Register("TiltFactor", typeof(Int32), typeof(TiltBehavior), new PropertyMetadata(20));
private bool _isPressed;
private Thickness _originalMargin;
private Panel _originalPanel;
private Size _originalSize;
private FrameworkElement _attachedElement;
private Point _current = new Point(-99, -99);
private Int32 _times = -1;
public bool KeepDragging
{
get { return (bool)GetValue(KeepDraggingProperty); }
set { SetValue(KeepDraggingProperty, value); }
}
public Int32 TiltFactor
{
get { return (Int32)GetValue(TiltFactorProperty); }
set { SetValue(TiltFactorProperty, value); }
}
public Planerator RotatorParent { get; set; }
protected override void OnAttached()
{
_attachedElement = AssociatedObject;
if (_attachedElement as Panel != null)
{
(_attachedElement as Panel).Loaded += (sl, el) =>
{
var elements = (_attachedElement as Panel).Children.Cast<UIElement>().ToList();
elements.ForEach(
element =>
Interaction.GetBehaviors(element).Add(
new TiltBehavior
{
KeepDragging = KeepDragging,
TiltFactor = TiltFactor
}));
};
return;
}
_originalPanel = _attachedElement.Parent as Panel;
_originalMargin = _attachedElement.Margin;
_originalSize = new Size(_attachedElement.Width, _attachedElement.Height);
double left = Canvas.GetLeft(_attachedElement);
double right = Canvas.GetRight(_attachedElement);
double top = Canvas.GetTop(_attachedElement);
double bottom = Canvas.GetBottom(_attachedElement);
int z = Panel.GetZIndex(_attachedElement);
VerticalAlignment va = _attachedElement.VerticalAlignment;
HorizontalAlignment ha = _attachedElement.HorizontalAlignment;
#region Setting Container Properties
RotatorParent = new Planerator
{
Margin = _originalMargin,
Width = _originalSize.Width,
Height = _originalSize.Height,
VerticalAlignment = va,
HorizontalAlignment = ha
};
RotatorParent.SetValue(Canvas.LeftProperty, left);
RotatorParent.SetValue(Canvas.RightProperty, right);
RotatorParent.SetValue(Canvas.TopProperty, top);
RotatorParent.SetValue(Canvas.BottomProperty, bottom);
RotatorParent.SetValue(Panel.ZIndexProperty, z);
#endregion
#region Removing Child Properties
_originalPanel.Children.Remove(_attachedElement);
_attachedElement.Margin = new Thickness();
_attachedElement.Width = double.NaN;
_attachedElement.Height = double.NaN;
#endregion
_originalPanel.Children.Add(RotatorParent);
RotatorParent.Child = _attachedElement;
CompositionTarget.Rendering += CompositionTargetRendering;
}
private void CompositionTargetRendering(object sender, EventArgs e)
{
if (KeepDragging)
{
_current = Mouse.GetPosition(RotatorParent.Child);
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
if (_current.X > 0 && _current.X < (_attachedElement).ActualWidth && _current.Y > 0 &&
_current.Y < (_attachedElement).ActualHeight)
{
RotatorParent.RotationY = -1 * TiltFactor + _current.X * 2 * TiltFactor / (_attachedElement).ActualWidth;
RotatorParent.RotationX = -1 * TiltFactor + _current.Y * 2 * TiltFactor / (_attachedElement).ActualHeight;
}
}
else
{
RotatorParent.RotationY = RotatorParent.RotationY - 5 < 0 ? 0 : RotatorParent.RotationY - 5;
RotatorParent.RotationX = RotatorParent.RotationX - 5 < 0 ? 0 : RotatorParent.RotationX - 5;
}
}
else
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
if (!_isPressed)
{
_current = Mouse.GetPosition(RotatorParent.Child);
if (_current.X > 0 && _current.X < (_attachedElement).ActualWidth && _current.Y > 0 &&
_current.Y < (_attachedElement).ActualHeight)
{
RotatorParent.RotationY = -1 * TiltFactor +
_current.X * 2 * TiltFactor / (_attachedElement).ActualWidth;
RotatorParent.RotationX = -1 * TiltFactor +
_current.Y * 2 * TiltFactor / (_attachedElement).ActualHeight;
}
_isPressed = true;
}
if (_isPressed && _times == 7)
{
RotatorParent.RotationY = RotatorParent.RotationY - 5 < 0 ? 0 : RotatorParent.RotationY - 5;
RotatorParent.RotationX = RotatorParent.RotationX - 5 < 0 ? 0 : RotatorParent.RotationX - 5;
}
else if (_isPressed && _times < 7)
{
_times++;
}
}
else
{
_isPressed = false;
_times = -1;
RotatorParent.RotationY = RotatorParent.RotationY - 5 < 0 ? 0 : RotatorParent.RotationY - 5;
RotatorParent.RotationX = RotatorParent.RotationX - 5 < 0 ? 0 : RotatorParent.RotationX - 5;
}
}
}
}
}