-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysWindow.cs
81 lines (71 loc) · 2.92 KB
/
PhysWindow.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Microsoft.Samples.Kinect.HackISUName
{
public class PhysWindow
{
private static double MAX_SPEED = 30;
/// <summary>
/// Called when you start a drag sequence
/// </summary>
/// <param name="startingRect"></param>
public PhysWindow(IntPtr window)
{
Win32.RECT current;
Win32.GetWindowRect(window, out current);
topLeft = new Point(current.left, current.top);
Console.WriteLine("Initial topleft: " + topLeft);
this.windowPtr = window;
width = (current.right - current.left);
height = (current.bottom - current.top);
}
public void addGoalPoint(Point goal)
{
velocity = new Vector(goal.X - topLeft.X, goal.Y - topLeft.Y);
if (velocity.Length > 5)
velocity.Normalize();
else
velocity = new Vector(0, 0);
velocity *= MAX_SPEED;
}
public void setPoint(Point goal)
{
topLeft = goal;
velocity = new Vector(0, 0);
}
public void update()
{
Win32.RECT current;
Win32.GetWindowRect(windowPtr, out current);
width = (current.right - current.left);
height = (current.bottom - current.top);
topLeft.X += velocity.X;
topLeft.Y += velocity.Y;
for (int i = 0; i < MAX_SPEED && Math.Abs(velocity.X) > slowdown && velocity.X < 0; velocity.X += slowdown, i++) ;
for (int i = 0; i < MAX_SPEED && Math.Abs(velocity.X) > slowdown && velocity.X > 0; velocity.X -= slowdown, i++) ;
for (int i = 0; i < MAX_SPEED && Math.Abs(velocity.Y) > slowdown && velocity.Y < 0; velocity.Y += slowdown, i++) ;
for (int i = 0; i < MAX_SPEED && Math.Abs(velocity.Y) > slowdown && velocity.Y > 0; velocity.Y -= slowdown, i++) ;
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
if (topLeft.X + width > screenWidth && velocity.X > 0)
velocity.X *= -1;
if (topLeft.X < 0 && velocity.X < 0)
velocity.X *= -1;
if (topLeft.Y + width > screenHeight && velocity.Y > 0)
velocity.Y *= -1;
if (topLeft.Y < 0 && velocity.Y < 0)
velocity.Y *= -1;
Win32.SetWindowPos(windowPtr, new IntPtr(0), (int)topLeft.X, (int)topLeft.Y, -1, -1, Win32.SetWindowPosFlags.SWP_NOSIZE);
}
public Point topLeft;
private Vector velocity;
private IntPtr windowPtr;
private double width;
private double height;
private double slowdown = .01;
}
}