This repository has been archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTween.cs
159 lines (145 loc) · 6.5 KB
/
Tween.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
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) GHI Electronics, LLC.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation.Media;
using GHI.Glide.Display;
namespace GHI.Glide
{
/// <summary>
/// Holds the steps for different animations.
/// </summary>
public struct Steps
{
/// <summary>
/// Steps to slide between windows.
/// </summary>
public int SlideWindow { get; set; }
}
/// <summary>
/// The Tween class gives you access to methods that move, resize, and fade objects over a number of steps.
/// </summary>
public static class Tween
{
/// <summary>
/// Indicates the number of steps an animation needs to tween from start to finish.
/// </summary>
public static Steps NumSteps;
/// <summary>
/// Initializes the Tween class.
/// </summary>
/// <remarks>Sets the best number of steps to tween from start to finish based on your GHI Electronics board model.</remarks>
static Tween()
{
// Default is 5
NumSteps.SlideWindow = 5;
switch (Microsoft.SPOT.Hardware.SystemInfo.SystemID.Model)
{
case 5: // EMX
NumSteps.SlideWindow = 7;
break;
case 6: // ChipworkX
NumSteps.SlideWindow = 50;
break;
default:
if (Glide.IsEmulator)
NumSteps.SlideWindow = 100;
break;
}
}
/// <summary>
/// Calculates a position for each step.
/// </summary>
/// <param name="start">Start position.</param>
/// <param name="end">End position.</param>
/// <param name="steps">Number of steps.</param>
/// <returns>An array of steps to move from start to end.</returns>
public static int[] GetSteps(int start, int end, int steps)
{
float step1, step2, step3, step4;
int[] values = new int[steps + 1];
for (int i = 0; i < steps + 1; i++)
{
step1 = end - start;
step2 = step1 / steps;
step3 = step2 * i;
step4 = start + step3;
values[i] = (int)step4;
}
return values;
}
/// <summary>
/// Slides between windows in a specified direction.
/// </summary>
/// <param name="fromWindow">From window.</param>
/// <param name="toWindow">To window.</param>
/// <param name="direction">Direction of movement.</param>
public static void SlideWindow(Window fromWindow, Window toWindow, Direction direction)
{
Glide.MainWindow = null;
int[] x1, x2, y1, y2;
int fromY = fromWindow.Y - fromWindow.ListY;
int toY = toWindow.Y - toWindow.ListY;
int index;
switch (direction)
{
case Direction.Left:
x1 = Tween.GetSteps(0, toWindow.Width, NumSteps.SlideWindow);
x2 = Tween.GetSteps(-fromWindow.Width, 0, NumSteps.SlideWindow);
index = 0;
while (index < x1.Length)
{
Glide.screen.DrawImage(x1[index], fromY, fromWindow.Graphics.GetBitmap(), 0, 0, fromWindow.Width, fromWindow.Height);
Glide.screen.DrawImage(x2[index], toY, toWindow.Graphics.GetBitmap(), 0, 0, toWindow.Width, toWindow.Height);
Glide.screen.Flush();
index++;
}
Glide.MainWindow = toWindow;
break;
case Direction.Right:
x1 = GetSteps(0, -fromWindow.Width, NumSteps.SlideWindow);
x2 = GetSteps(toWindow.Width, 0, NumSteps.SlideWindow);
index = 0;
while (index < x1.Length)
{
Glide.screen.DrawImage(x1[index], fromY, fromWindow.Graphics.GetBitmap(), 0, 0, fromWindow.Width, fromWindow.Height);
Glide.screen.DrawImage(x2[index], toY, toWindow.Graphics.GetBitmap(), 0, 0, toWindow.Width, toWindow.Height);
Glide.screen.Flush();
index++;
}
Glide.MainWindow = toWindow;
break;
case Direction.Up:
y1 = GetSteps(fromY, -fromWindow.Height, NumSteps.SlideWindow);
y2 = GetSteps(fromY + fromWindow.Height, toY, NumSteps.SlideWindow);
index = 0;
while (index < y1.Length)
{
Glide.screen.DrawImage(0, y1[index], fromWindow.Graphics.GetBitmap(), 0, 0, fromWindow.Width, fromWindow.Height);
Glide.screen.DrawImage(0, y2[index], toWindow.Graphics.GetBitmap(), 0, 0, toWindow.Width, toWindow.Height);
Glide.screen.Flush();
index++;
}
Glide.MainWindow = toWindow;
break;
case Direction.Down:
y1 = GetSteps(toY, fromWindow.Height, NumSteps.SlideWindow);
y2 = GetSteps(-fromWindow.Height, fromY, NumSteps.SlideWindow);
index = 0;
while (index < y1.Length)
{
Glide.screen.DrawImage(0, y1[index], fromWindow.Graphics.GetBitmap(), 0, 0, fromWindow.Width, fromWindow.Height);
Glide.screen.DrawImage(0, y2[index], toWindow.Graphics.GetBitmap(), 0, 0, toWindow.Width, toWindow.Height);
Glide.screen.Flush();
index++;
}
Glide.MainWindow = toWindow;
break;
}
}
}
}