forked from WFoundation/WF.Player.Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScreenActivity.cs
578 lines (495 loc) · 15.3 KB
/
ScreenActivity.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Locations;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using ActionbarSherlock.App;
using WF.Player.Core;
namespace WF.Player.Android
{
[Activity (Label = "Screen", Theme = "@style/Theme.Wfplayer")]
public class ScreenActivity : SherlockFragmentActivity
{
private ScreenType activeScreen = ScreenType.MainScreen;
private int activeObject;
private Cartridge cartridge;
private Engine engine;
private StreamWriter logFile;
private LogLevel logLevel = LogLevel.LogCartridge;
private LocListener locListener;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.ScreenActivity);
var layoutMain = FindViewById<LinearLayout> (Resource.Id.layoutMain);
// Create your application here
Intent intent = this.Intent;
string cartFilename = intent.GetStringExtra ("cartridge");
bool cartRestore = intent.GetBooleanExtra ("restore", false);
if (File.Exists (cartFilename)) {
cartridge = new Cartridge(cartFilename);
}
if (cartridge == null)
Finish ();
// Create engine
engine = createEngine (cartridge);
// Set correct icon
if (cartridge.Icon != null) {
Bitmap bm = BitmapFactory.DecodeByteArray (cartridge.Icon.Data, 0, cartridge.Icon.Data.Length);
this.SupportActionBar.SetIcon(new BitmapDrawable(this.Resources, bm));
}
// Show main screen
var ft = this.SupportFragmentManager.BeginTransaction ();
ft.SetBreadCrumbTitle (cartridge.Name);
ft.SetTransition (FragmentTransaction.TransitNone);
ft.AddToBackStack (null);
ft.Replace (Resource.Id.fragment, new ScreenMain (engine));
ft.Commit ();
layoutMain.Invalidate ();
// Start cartridge
if (cartRestore)
Restore ();
else
Start ();
}
public override void OnBackPressed()
{
if (activeScreen == ScreenType.LocationScreen || activeScreen == ScreenType.ItemScreen
|| activeScreen == ScreenType.InventoryScreen
|| activeScreen == ScreenType.TaskScreen
|| activeScreen == ScreenType.DetailScreen)
RemoveScreen (activeScreen);
else if (activeScreen == ScreenType.MainScreen)
;
else
base.OnBackPressed ();
}
protected override void OnResume()
{
base.OnResume ();
// Start LocListener
if (locListener == null)
{
locListener = new LocListener (GetSystemService (Context.LocationService) as LocationManager);
locListener.LocationChanged += OnRefreshLocation;
}
}
protected override void OnPause()
{
base.OnPause ();
}
protected override void OnStart()
{
base.OnStart ();
// Start LocListener
if (locListener == null)
{
locListener = new LocListener (GetSystemService (Context.LocationService) as LocationManager);
locListener.LocationChanged += OnRefreshLocation;
}
}
protected override void OnStop()
{
base.OnStop ();
// Stop LocListener
if (locListener != null)
{
locListener.LocationChanged -= OnRefreshLocation;
locListener = null;
}
}
#region Properties
public Cartridge Cartridge {
get {
return cartridge;
}
}
#endregion
#region Methods
public void Start()
{
// ShowScreen (ScreenType.MainScreen, -1);
engine.Start ();
}
public void Restore()
{
// ShowScreen (ScreenType.MainScreen, -1);
engine.Restore(new FileStream(cartridge.SaveFilename,FileMode.Open));
}
public void RemoveScreen(ScreenType last)
{
switch (last) {
case ScreenType.MainScreen:
// ToDo: Main screen is the last screen to show, so stop the cartridge
ShowScreen (ScreenType.MainScreen, -1);
break;
case ScreenType.LocationScreen:
case ScreenType.ItemScreen:
case ScreenType.InventoryScreen:
case ScreenType.TaskScreen:
ShowScreen (ScreenType.MainScreen, -1);
break;
case ScreenType.DetailScreen:
// Show correct list for this zone/item/character/task
if (engine.IsUIObject (engine.GetObject (activeObject))) {
UIObject obj = (UIObject)engine.GetObject (activeObject);
activeObject = -1;
if (obj is Zone)
ShowScreen (ScreenType.LocationScreen, -1);
if (obj is Task)
ShowScreen (ScreenType.TaskScreen, -1);
if (obj is Item || obj is Character) {
if (engine.IsInInventory (obj))
ShowScreen (ScreenType.InventoryScreen, -1);
else
ShowScreen (ScreenType.ItemScreen, -1);
}
}
break;
case ScreenType.DialogScreen:
// Which screen to show
if (activeScreen == ScreenType.DetailScreen && activeObject != -1 && !((UIObject)engine.GetObject (activeObject)).Visible)
RemoveScreen (ScreenType.DetailScreen);
else
ShowScreen (activeScreen, activeObject);
break;
}
}
public void ShowScreen(ScreenType screen, int objIndex)
{
var bar = this.SupportActionBar;
var ft = this.SupportFragmentManager.BeginTransaction ();
// if (screen != activeScreen) {
// If the old screen is unknown, than use the last active screen
switch (screen) {
case ScreenType.MainScreen:
ft.SetBreadCrumbTitle (cartridge.Name);
ft.SetTransition (FragmentTransaction.TransitNone);
ft.Replace (Resource.Id.fragment, new ScreenMain (engine));
ft.Commit ();
break;
case ScreenType.LocationScreen:
case ScreenType.ItemScreen:
case ScreenType.InventoryScreen:
case ScreenType.TaskScreen:
bar.SetDisplayHomeAsUpEnabled (true);
ft.SetTransition (FragmentTransaction.TransitNone);
ft.Replace (Resource.Id.fragment, new ScreenList (engine, screen));
// ft.AddToBackStack (null);
ft.Commit ();
break;
case ScreenType.DetailScreen:
bar.SetDisplayHomeAsUpEnabled (true);
ft.SetTransition (FragmentTransaction.TransitNone);
ft.Replace (Resource.Id.fragment, new ScreenDetail (engine, objIndex));
// ft.AddToBackStack (null);
ft.Commit ();
break;
}
activeScreen = screen;
activeObject = objIndex;
// }
}
#endregion
#region Events of Engine
/// <summary>
/// Get the input e.Input from the player.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">Event arguments.</param>
public void OnGetInput (Object sender, GetInputEventArgs e)
{
var bar = this.SupportActionBar;
var ft = this.SupportFragmentManager.BeginTransaction ();
bar.SetDisplayHomeAsUpEnabled (false);
ft.SetTransition (FragmentTransaction.TransitNone);
ft.Replace (Resource.Id.fragment, new ScreenDialog (engine, e.Input));
ft.Commit ();
}
/// <summary>
/// Log the message e.MessageRaises the log message event.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">Event arguments.</param>
public void OnLogMessage (Object sender, LogMessageEventArgs e)
{
if (e.Level <= logLevel)
{
if (logFile != null)
{
// Create log entry
logFile.WriteLine (engine.CreateLogMessage (e.Message));
}
}
}
/// <summary>
/// Execute special command specific for this player.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">Event arguments.</param>
public void OnNotifyOS (Object sender, NotifyOSEventArgs e)
{
switch (e.Command) {
}
}
/// <summary>
/// Play the media e.Media on device.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">Event arguments.</param>
public void OnPlayMedia (Object sender, PlayMediaEventArgs e)
{
if (e.Media.Data != null)
{
switch (e.Media.Type) {
case MediaTypes.MP3:
break;
case MediaTypes.WAV:
break;
case MediaTypes.FDL:
break;
}
}
}
/// <summary>
/// Show message e.Text with media e.Media to player.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">Event arguments.</param>
public void OnShowMessage(Object sender, ShowMessageEventArgs e)
{
var bar = this.SupportActionBar;
var ft = this.SupportFragmentManager.BeginTransaction ();
bar.SetDisplayHomeAsUpEnabled (false);
ft.SetTransition (FragmentTransaction.TransitNone);
ft.Replace (Resource.Id.fragment, new ScreenDialog (engine, e.Text, e.Media, e.ButtonLabel1, e.ButtonLabel2, e.Callback));
ft.Commit ();
}
/// <summary>
/// Show screen e.Screen to player. If needed, there is an object index in e.IndexObject.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">Event arguments.</param>
public void OnShowScreen(Object sender, ShowScreenEventArgs e)
{
ShowScreen (e.Screen, e.IndexObject);
}
/// <summary>
/// Show
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">E.</param>
public void OnShowStatusText (Object sender, ShowStatusTextEventArgs e)
{
Toast.MakeText (this, e.Text, ToastLength.Long);
}
public void OnSynchronize(Object sender, SynchronizeEventArgs e)
{
this.RunOnUiThread (() => e.Func(e.Source));
}
#endregion
#region Events
private void OnRefreshLocation(Object sender, LocationChangedEventArgs e)
{
if (engine != null)
engine.RefreshLocation (locListener.Latitude, locListener.Longitude, locListener.Altitude, locListener.Accuracy);
}
#endregion
#region Drawing Directions
public Bitmap DrawCenter ()
{
int w = 48;
int h = 48;
Bitmap b = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
Canvas c = new Canvas(b);
Paint light = new Paint (PaintFlags.AntiAlias);
Paint dark = new Paint (PaintFlags.AntiAlias);
light.Color = new Color (128, 0, 0, 255);
light.StrokeWidth = 0f;
light.SetStyle (Paint.Style.Stroke);
dark.Color = new Color (255, 0, 0, 255);
dark.StrokeWidth = 0f;
dark.SetStyle (Paint.Style.FillAndStroke);
int cx = c.Width / 2 - 1;
int cy = c.Height / 2 - 1;
c.DrawCircle (cx, cy, w / 8, light);
c.DrawCircle (cx, cy, w / 32 * 3, dark);
// UIGraphics.BeginImageContext (new SizeF(64, 64));
//
// using (CGContext cont = UIGraphics.GetCurrentContext()) {
//
// using (CGPath path = new CGPath()) {
//
// cont.SetLineWidth (1f);
// cont.SetRGBStrokeColor (1f, 0, 0, 1);
// cont.SetRGBFillColor (0.5f, 0, 0, 1);
// path.AddElipseInRect(new RectangleF(24,24,16,16));
// path.CloseSubpath ();
//
// cont.AddPath (path);
// cont.DrawPath (CGPathDrawingMode.FillStroke);
//
// }
//
// using (CGPath path = new CGPath()) {
//
// cont.SetRGBStrokeColor (1f, 0, 0, 1);
// cont.SetLineWidth(3f);
// // cont.SetRGBFillColor (.5f, 0, 0, 1);
// path.AddElipseInRect(new RectangleF(16,16,32,32));
// path.CloseSubpath ();
//
// cont.AddPath (path);
// cont.DrawPath (CGPathDrawingMode.Stroke);
//
// }
//
// return UIGraphics.GetImageFromCurrentImageContext ();
//
// }
//
// UIGraphics.EndImageContext ();
return b;
}
public Bitmap DrawArrow (double direction)
{
int w = 48;
int h = 48;
int w2 = w / 2;
int h2 = w / 2;
double rad1 = direction / 180.0 * Math.PI;
double rad2 = (direction + 180.0 + 30.0) / 180.0 * Math.PI;
double rad3 = (direction + 180.0 - 30.0) / 180.0 * Math.PI;
double rad4 = (direction + 180.0) / 180.0 * Math.PI;
PointF p1 = new PointF((float) (w2 + w2 * Math.Sin (rad1)), (float) (h2 + h2 * Math.Cos (rad1)));
PointF p2 = new PointF((float) (w2 + w2 * Math.Sin (rad2)), (float) (h2 + h2 * Math.Cos (rad2)));
PointF p3 = new PointF((float) (w2 + w2 * Math.Sin (rad3)), (float) (h2 + h2 * Math.Cos (rad3)));
PointF p4 = new PointF((float) (w2 + w / 3 * Math.Sin (rad4)), (float) (h2 + h / 3 * Math.Cos (rad4)));
Bitmap b = Bitmap.CreateBitmap(w, h, Bitmap.Config.Argb8888);
Canvas c = new Canvas(b);
Paint light = new Paint (PaintFlags.AntiAlias);
Paint dark = new Paint (PaintFlags.AntiAlias);
Paint black = new Paint (PaintFlags.AntiAlias);
light.Color = new Color (128, 0, 0, 255);
light.StrokeWidth = 0f;
light.SetStyle (Paint.Style.FillAndStroke);
dark.Color = new Color (255, 0, 0, 255);
dark.StrokeWidth = 0f;
dark.SetStyle (Paint.Style.FillAndStroke);
black.Color = new Color (0, 0, 0, 255);
black.StrokeWidth = 0f;
black.SetStyle (Paint.Style.Stroke);
int cx = c.Width / 2 - 1;
int cy = c.Height / 2 - 1;
global::Android.Graphics.Path path1 = new global::Android.Graphics.Path ();
global::Android.Graphics.Path path2 = new global::Android.Graphics.Path ();
global::Android.Graphics.Path path3 = new global::Android.Graphics.Path ();
path1.MoveTo (p1.X,p1.Y);
path1.LineTo (p2.X,p2.Y);
path1.LineTo (p4.X,p4.Y);
path1.LineTo (p1.X,p1.Y);
path2.MoveTo (p1.X,p1.Y);
path2.LineTo (p3.X,p3.Y);
path2.LineTo (p4.X,p4.Y);
path2.LineTo (p1.X,p1.Y);
path3.MoveTo (p1.X,p1.Y);
path3.LineTo (p2.X,p2.Y);
path3.LineTo (p4.X,p4.Y);
path3.LineTo (p1.X,p1.Y);
path3.LineTo (p3.X,p3.Y);
path3.LineTo (p4.X,p4.Y);
c.DrawPath (path1, light);
c.DrawPath (path2, dark);
c.DrawPath (path3, black);
// UIGraphics.BeginImageContext (new SizeF(64, 64));
//
// using (CGContext cont = UIGraphics.GetCurrentContext()) {
//
// using (CGPath path = new CGPath()) {
//
// cont.SetLineWidth (1f);
// cont.SetRGBStrokeColor (0f, 0, 0, 1);
// cont.SetRGBFillColor (1f, 0, 0, 1);
// path.AddLines (new PointF[] { p1, p2, p4 });
// path.CloseSubpath ();
//
// cont.AddPath (path);
// cont.DrawPath (CGPathDrawingMode.FillStroke);
//
// }
//
// using (CGPath path = new CGPath()) {
//
// cont.SetRGBStrokeColor (0f, 0, 0, 1);
// cont.SetRGBFillColor (.5f, 0, 0, 1);
// path.AddLines (new PointF[] { p1, p3, p4 });
// path.CloseSubpath ();
//
// cont.AddPath (path);
// cont.DrawPath (CGPathDrawingMode.FillStroke);
//
// }
//
// return UIGraphics.GetImageFromCurrentImageContext ();
//
// }
//
// UIGraphics.EndImageContext ();
return b;
}
#endregion
#region Private functions
private Engine createEngine (Cartridge cart)
{
Engine result;
result = new Engine ();
// Set all events for engine
result.GetInputEvent += OnGetInput;
result.LogMessageEvent += OnLogMessage;
result.NotifyOSEvent += OnNotifyOS;
result.PlayMediaEvent += OnPlayMedia;
result.ShowMessageEvent += OnShowMessage;
result.ShowScreenEvent += OnShowScreen;
result.ShowStatusTextEvent += OnShowStatusText;
result.SynchronizeEvent += OnSynchronize;
// If there is a old logFile, close it
if (logFile != null) {
logFile.Flush ();
logFile.Close ();
}
// Open logFile first time
logFile = new StreamWriter(cart.LogFilename, true, Encoding.UTF8);
logFile.AutoFlush = true;
result.Init (new FileStream (cart.Filename,FileMode.Open), cart);
return result;
}
private void closeEngine()
{
engine.GetInputEvent -= OnGetInput;
engine.LogMessageEvent -= OnLogMessage;
engine.NotifyOSEvent -= OnNotifyOS;
engine.PlayMediaEvent -= OnPlayMedia;
engine.ShowMessageEvent -= OnShowMessage;
engine.ShowScreenEvent -= OnShowScreen;
engine.ShowStatusTextEvent -= OnShowStatusText;
engine.SynchronizeEvent -= OnSynchronize;
engine = null;
// If there is a old logFile, close it
if (logFile != null) {
logFile.Flush ();
logFile.Close ();
}
}
#endregion
}
}