-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainActivity.cs
143 lines (123 loc) · 4.23 KB
/
MainActivity.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
using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Drench
{
[Activity (Label = "Zyan Drench", MainLauncher = true)]
public class MainActivity : Activity
{
private CustomApplication App { get; set; }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
RequestWindowFeature(WindowFeatures.CustomTitle);
SetContentView(Resource.Layout.Main);
App = (CustomApplication)Application;
// Set custom title
Window.SetFeatureInt(WindowFeatures.CustomTitle, Resource.Layout.CustomTitle);
var title = FindViewById<RelativeLayout>(Resource.Id.titleLayout);
var parentView = title.Parent as View;
if (parentView != null)
{
// the following line generates strange artifacts sometimes:
parentView.SetBackgroundColor(Settings.CustomTitleColor);
title.SetBackgroundColor(Settings.CustomTitleColor);
}
// Get our buttons from the layout resource,
// and attach events to them
var singlePlayerGameButton = FindViewById<Button>(Resource.Id.singlePlayerGameButton);
singlePlayerGameButton.Click += (s, e) =>
{
App.DrenchGame = new SinglePlayerGame();
StartActivity(typeof(DrenchBoardActivity));
};
var twoPlayersGameButton = FindViewById<Button>(Resource.Id.twoPlayersGameButton);
twoPlayersGameButton.Click += (s, e) =>
{
App.DrenchGame = Settings.CreateAndroidGame();
StartActivity(typeof(DrenchBoardActivity));
};
var startNetworkGameButton = FindViewById<Button>(Resource.Id.startNetworkGameButton);
startNetworkGameButton.Click += (sender, e) =>
{
App.StartServer();
StartActivity(typeof(ServerStartedActivity));
};
var joinNetworkGameButton = FindViewById<Button>(Resource.Id.joinNetworkGameButton);
joinNetworkGameButton.Click += (sender, e) =>
{
StartActivity(typeof(ConnectFormActivity));
};
var settingsButton = FindViewById<Button>(Resource.Id.settingsButton);
settingsButton.Click += (sender, e) =>
{
StartActivity(typeof(SettingsActivity));
};
var aboutZyanDrenchButton = FindViewById<Button>(Resource.Id.aboutZyanButton);
aboutZyanDrenchButton.Click += (sender, e) =>
{
var builder = new AlertDialog.Builder(this);
builder.SetTitle("About Zyan Drench");
builder.SetMessage("Zyan Drench is a sample Android game built using Zyan Framework.\n\n" +
"Zyan is an easy to use network communication framework for Microsoft.NET, Mono and Xamarin.Android.");
builder.SetPositiveButton("How to play", (s, args) =>
{
StartActivity(typeof(HelpActivity));
});
builder.SetNegativeButton("Visit zyan.com.de", (s, args) =>
{
var homepage = Android.Net.Uri.Parse("http://zyan.com.de");
var intent = new Intent(Intent.ActionView, homepage);
StartActivity(intent);
});
builder.Show();
};
// set custom palette for buttons
var buttons = new[]
{
twoPlayersGameButton, singlePlayerGameButton, startNetworkGameButton,
joinNetworkGameButton, settingsButton, aboutZyanDrenchButton
};
var vibrator = (Vibrator)GetSystemService(Android.Content.Context.VibratorService);
// set Touch event handlers to adjust buttons in pressed state
for (var i = 0; i < buttons.Length; i++)
{
var currentColor = Settings.TileColors[i];
buttons[i].SetBackgroundColor(currentColor);
buttons[i].Touch += (sender, e) =>
{
e.Handled = false;
var button = (Button)sender;
switch (e.Event.Action)
{
case MotionEventActions.Down:
button.SetBackgroundColor(Settings.CustomTitleColor);
vibrator.Vibrate(Settings.VibrateDuration);
break;
case MotionEventActions.Move:
var r = new Rect();
button.GetLocalVisibleRect(r);
if (!r.Contains((int)e.Event.GetX(), (int)e.Event.GetY()))
{
button.SetBackgroundColor(currentColor);
}
break;
case MotionEventActions.Outside:
case MotionEventActions.Cancel:
case MotionEventActions.Up:
button.SetBackgroundColor(currentColor);
break;
}
};
}
}
}
}