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 pathGlide.cs
360 lines (308 loc) · 12.6 KB
/
Glide.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) GHI Electronics, LLC.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.Glide.Display;
using GHI.Glide.Geom;
using GHI.Glide.UI;
namespace GHI.Glide
{
/// <summary>
/// The Glide class provides core functionality.
/// </summary>
public static class Glide
{
internal static Bitmap screen;
private static Window _mainWindow;
private static Keyboard _keyboard;
private static KeyboardText _keyboardText;
private static InputBox _inputBox;
private static Dropdown _dropdown;
private static List _list;
private static Size screenSize;
static Glide()
{
int width, height, bitsPerPixel, orientationDeg;
HardwareProvider.HwProvider.GetLCDMetrics(out width, out height, out bitsPerPixel, out orientationDeg);
LCD = new Size() { Width = width, Height = height };
screen = new Bitmap(width, height);
// Are we in the emulator?
if (SystemInfo.SystemID.SKU == 3)
IsEmulator = true;
else
IsEmulator = false;
FitToScreen = false;
Keyboard = DefaultKeyboard();
MessageBoxManager = new MessageBoxManager();
// Show loading
Bitmap loading = Resources.GetBitmap(Resources.BitmapResources.loading);
screen.DrawImage((LCD.Width - loading.Width) / 2, (LCD.Height - loading.Height) / 2, loading, 0, 0, loading.Width, loading.Height);
screen.Flush();
}
/// <summary>
/// Returns the screen resolution.
/// </summary>
public static Size LCD
{
get
{
return Glide.screenSize;
}
private set
{
Glide.screenSize = value;
}
}
/// <summary>
/// Returns a reference to the bitmap that represents the current screen.
/// This is only useful for drawing the bitmap to a display that does not
/// support bitmap.flush().
/// </summary>
public static Bitmap Screen
{
get
{
return Glide.screen;
}
}
/// <summary>
/// This method changes the underlying size of the bitmap that is drawn to
/// the screen. Do not call this method if you are using a regular display.
/// It is only useful when you are using a non-native display such as a
/// SPI display like our DisplayN18.
/// </summary>
/// <param name="width">The width of the display.</param>
/// <param name="height">The height of the display.</param>
public static void SetScreenSize(int width, int height)
{
if (width <= 0 || height <= 0)
throw new ArgumentException("Width and height must be positive.");
Glide.screenSize.Width = width;
Glide.screenSize.Height = height;
Glide.screen.Dispose();
Glide.screen = new Bitmap(width, height);
Glide.MessageBoxManager = new MessageBoxManager();
}
/// <summary>
/// Opens the keyboard.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
public static void OpenKeyboard(object sender)
{
_inputBox = (InputBox)sender;
// Make objects non-interactive
for (int i = 0; i < MainWindow.NumChildren; i++)
MainWindow[i].Interactive = false;
_keyboardText.Text = _inputBox.Text;
_keyboardText.IsPassword = (sender is PasswordBox) ? true : false;
_keyboard.Start();
MainWindow.AddChild(_keyboard);
MainWindow.AddChild(_keyboardText);
MainWindow.Invalidate();
}
/// <summary>
/// Closes the keyboard.
/// </summary>
public static void CloseKeyboard()
{
_keyboard.Stop();
MainWindow.RemoveChild(_keyboard);
MainWindow.RemoveChild(_keyboardText);
// Make objects interactive
for (int i = 0; i < MainWindow.NumChildren; i++)
MainWindow[i].Interactive = true;
_inputBox.Text = _keyboardText.Text;
MainWindow.Invalidate();
}
private static void KeyboardTapKey(object sender, TapKeyEventArgs args)
{
switch (args.Value.ToLower())
{
case Keyboard.ActionKey.Backspace:
if (_keyboardText.Text.Length > 0)
{
_keyboardText.Text = _keyboardText.Text.Substring(0, _keyboardText.Text.Length - 1);
_keyboardText.Invalidate();
}
if (_keyboardText.Text.Length == 0 && !_keyboard.Uppercase)
{
_keyboard.Uppercase = true;
_keyboard.CurrentView = Keyboard.View.Letters;
_keyboard.CalculateKeys();
_keyboard.Invalidate();
_keyboard.DrawKeyDown(args.Index);
}
break;
case Keyboard.ActionKey.Return:
CloseKeyboard();
break;
case Keyboard.ActionKey.Space:
_keyboardText.Text += " ";
_keyboardText.Invalidate();
break;
case Keyboard.ActionKey.Tab:
_keyboardText.Text += " ";
_keyboardText.Invalidate();
break;
default:
_keyboardText.Text += args.Value;
_keyboardText.Invalidate();
/*
if (_keyboardText.Text.Length == 1)
{
if (_keyboard.CurrentView == Keyboard.View.Letters)
{
_keyboard.Uppercase = false;
_keyboard.CurrentView = Keyboard.View.Letters;
}
}
*/
break;
}
}
/// <summary>
/// Opens a List component.
/// </summary>
/// <param name="sender">Object associated with the event.</param>
/// <param name="list">List component that needs to be opened.</param>
public static void OpenList(object sender, List list)
{
if (_list == null)
{
_dropdown = (Dropdown)sender;
_list = list;
_list.TapOptionEvent += new OnTapOption(list_TapOptionEvent);
for (int i = 0; i < MainWindow.NumChildren; i++)
MainWindow[i].Interactive = false;
MainWindow.AddChild(list);
MainWindow.Invalidate();
}
else
throw new SystemException("You already have a List open.");
}
/// <summary>
/// Closes a List component.
/// </summary>
public static void CloseList()
{
if (_list != null)
{
_list.TapOptionEvent -= new OnTapOption(list_TapOptionEvent);
MainWindow.RemoveChild(_list);
_list = null;
for (int i = 0; i < MainWindow.NumChildren; i++)
MainWindow[i].Interactive = true;
MainWindow.Invalidate();
}
}
private static void list_TapOptionEvent(object sender, TapOptionEventArgs args)
{
_dropdown.Text = args.Label;
_dropdown.Value = args.Value;
_dropdown.TriggerValueChangedEvent(_dropdown);
CloseList();
}
/// <summary>
/// Flushes specified area to the screen.
/// </summary>
/// <param name="x">X-axis position.</param>
/// <param name="y">Y-axis position.</param>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
public static void Flush(int x, int y, int width, int height)
{
// Ignore flushes if no MainWindow is set.
if (_mainWindow == null)
return;
int offsetY = y - _mainWindow.ListY;
screen.DrawImage(x, offsetY, _mainWindow.Graphics.GetBitmap(), x, y, width, height);
// Object must be partially visible so flushing doesn't error.
if (_mainWindow.Rect.Contains(x, offsetY, width, height))
Glide.screen.Flush(x, offsetY, width, height);
}
/// <summary>
/// Flushes the rectangular area the DisplayObject occupies to the screen.
/// </summary>
/// <param name="displayObject">DisplayObject</param>
public static void Flush(DisplayObject displayObject)
{
Flush(displayObject.Parent.X + displayObject.X, displayObject.Parent.Y + displayObject.Y, displayObject.Width, displayObject.Height);
}
/// <summary>
/// Flushes the rectangular area to the screen.
/// </summary>
/// <param name="rect">Rectangle</param>
public static void Flush(Rectangle rect)
{
Flush(rect.X, rect.Y, rect.Width, rect.Height);
}
/// <summary>
/// Returns a keyboard used by 320x240 LCDs.
/// </summary>
public static Keyboard DefaultKeyboard()
{
Keyboard keyboard = new Keyboard(320, 128, 3, 32, 0);
keyboard.BitmapUp = new Bitmap[4]
{
Resources.GetBitmap(Resources.BitmapResources.Keyboard_320x128_Up_Uppercase),
Resources.GetBitmap(Resources.BitmapResources.Keyboard_320x128_Up_Lowercase),
Resources.GetBitmap(Resources.BitmapResources.Keyboard_320x128_Up_Numbers),
Resources.GetBitmap(Resources.BitmapResources.Keyboard_320x128_Up_Symbols)
};
return keyboard;
}
/// <summary>
/// Indicates whether or not we're using the emulator.
/// </summary>
public static readonly bool IsEmulator;
/// <summary>
/// Indicates whether or not to resize windows to the LCD's resolution.
/// </summary>
/// <remarks>This does not affect component placement. They will remain in their assigned position.</remarks>
public static bool FitToScreen;
/// <summary>
/// Current Keyboard used by Glide.
/// </summary>
public static Keyboard Keyboard
{
get { return _keyboard; }
set
{
_keyboard = value;
_keyboard.TapKeyEvent += new OnTapKey(KeyboardTapKey);
_keyboardText = new KeyboardText(0, 0, LCD.Width, LCD.Height - _keyboard.Height);
}
}
/// <summary>
/// A message box that helps inform and instruct the user.
/// </summary>
public static MessageBoxManager MessageBoxManager;
/// <summary>
/// The window currently in focus.
/// </summary>
public static Window MainWindow
{
get { return _mainWindow; }
set
{
// Ignore events on the current window
if (_mainWindow != null)
_mainWindow.IgnoreEvents();
// Change to the new window
_mainWindow = value;
// Begin handling events
if (_mainWindow != null)
{
_mainWindow.HandleEvents();
// Call render after because windows only flush if they're handling events
_mainWindow.Invalidate();
}
}
}
}
}