Added documentation
[clanbomber-xamarin-client.git] / src / communication / ConnectionTest / ConnectionTest / ConnectionTest / MainPage.xaml.cs
1 ´╗┐using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using Xamarin.Forms;
7 using Newtonsoft.Json;
8 using System.Net;
9 using static Bomberman.TcpJsonConnection;
10
11 namespace Bomberman
12 {
13     public partial class MainPage : ContentPage
14     {
15
16         const int c_defaultPlaygroundSize = 13;
17
18         TcpJsonConnection con;
19
20         StackLayout fullLayout;
21         StackLayout ButtonLayout = new StackLayout();
22
23
24         BombermanPlayground.Playground playground;
25
26         public MainPage()
27         {
28             InitializeComponent();
29
30             SizeChanged += MainPage_SizeChanged;
31
32             con = new TcpJsonConnection(ServerPkgReceived);
33
34             //con.Connect(IPAddress.Parse("10.29.17.60"));
35             foreach (var addr in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
36             {\r
37                 if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)\r
38                 {\r
39                     con.Connect(addr);    //localhost\r
40                     break;\r
41                 }\r
42             }
43 \r
44 \r
45             Entry ent = new Entry();
46             ButtonLayout.Children.Add(ent);
47             ent.TextChanged += Ent_TextChanged;
48
49
50             for (int i = 0; i < 3; i++)
51             {
52                 StackLayout horizontalLayout = new StackLayout();
53                 horizontalLayout.Orientation = StackOrientation.Horizontal;
54                 ButtonLayout.Children.Add(horizontalLayout);
55
56                 Button dummy = new Button();
57                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
58                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
59
60                 dummy.Text = "<";
61
62
63                 switch (i)
64                 {
65                     case 0:
66                         horizontalLayout.Children.Add(dummy);
67                         Button up = new Button();
68                         up.Pressed += Up_Pressed;
69                         up.Released += Up_Released;
70                         up.Text = "^";
71                         horizontalLayout.Children.Add(up);
72                         horizontalLayout.Children.Add(dummy);
73                         break;
74
75                     case 1:
76                         Button left = new Button();
77                         left.Text = "<";
78                         left.Pressed += Left_Pressed;
79                         left.Released += Left_Released;
80                         horizontalLayout.Children.Add(left);
81
82                         Button bomb = new Button();
83                         bomb.Pressed += Bomb_Pressed;
84                         bomb.Released += Bomb_Released;
85                         bomb.Text = "*";
86                         horizontalLayout.Children.Add(bomb);
87
88                         Button right = new Button();
89                         right.Pressed += Right_Pressed;
90                         right.Released += Right_Released;
91                         right.Text = ">";
92                         horizontalLayout.Children.Add(right);
93                         break;
94
95                     case 2:
96                         horizontalLayout.Children.Add(dummy);
97                         Button down = new Button();
98                         down.Pressed += Down_Pressed;
99                         down.Released += Down_Released;
100                         down.Text = "V";
101                         horizontalLayout.Children.Add(down);
102                         horizontalLayout.Children.Add(dummy);
103                         break;
104
105
106                 }
107
108             }
109         }
110
111         private void Down_Released(object sender, EventArgs e)
112         {
113             con.SendEvent(TcpJsonConnection.InputState.Idle);
114         }
115
116         private void Right_Released(object sender, EventArgs e)
117         {
118             con.SendEvent(TcpJsonConnection.InputState.Idle);
119         }
120
121         private void Bomb_Released(object sender, EventArgs e)
122         {
123             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
124         }
125
126         private void Left_Released(object sender, EventArgs e)
127         {
128             con.SendEvent(TcpJsonConnection.InputState.Idle);
129         }
130
131         private void Up_Released(object sender, EventArgs e)
132         {
133             con.SendEvent(TcpJsonConnection.InputState.Idle);
134         }
135
136         private void MainPage_SizeChanged(object sender, EventArgs e)
137         {
138             CreateInitializePlayground(c_defaultPlaygroundSize);
139         }
140
141         void CreateInitializePlayground(int size)
142         {
143             fullLayout = new StackLayout();
144             playground = new BombermanPlayground.Playground(size);
145             BombermanPlayground.Playground.c_ImageNamespace = "ConnectionTest";
146             BombermanPlayground.BombermanBlock.BlockSize = Width / (size + 1);
147
148             fullLayout.Children.Add(ButtonLayout);
149             fullLayout.Children.Add(playground);
150             Content = fullLayout;
151
152         }
153
154         void ServerPkgReceived(ServerPackage pkg)
155         {
156             Device.BeginInvokeOnMainThread(() =>
157             {
158                 if(playground == null)
159                 {
160                     return;
161                 }
162                 if (playground.FieldSize != pkg.Field.Count)
163                 {
164                     CreateInitializePlayground(pkg.Field.Count);
165                 }
166                 playground.Update(pkg);
167
168             });
169         }
170
171
172         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
173         {
174             switch(((Entry)sender).Text)
175             {
176                 case "w":
177                     con.SendEvent(TcpJsonConnection.InputState.Up);
178                     break;
179                 case "a":
180                     con.SendEvent(TcpJsonConnection.InputState.Left);
181                     break;
182                 case "s":
183                     con.SendEvent(TcpJsonConnection.InputState.Down);
184                     break;
185                 case "d":
186                     con.SendEvent(TcpJsonConnection.InputState.Right);
187                     break;
188                 case " ":
189                     con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
190                     break;
191             }
192             ((Entry)sender).Text = "";
193             con.SendEvent(TcpJsonConnection.InputState.Idle);
194             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
195
196
197         }
198
199         private void Bomb_Pressed(object sender, EventArgs e)
200         {
201             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
202         }
203
204         private void Right_Pressed(object sender, EventArgs e)
205         {
206             con.SendEvent(TcpJsonConnection.InputState.Right);
207         }
208
209         private void Left_Pressed(object sender, EventArgs e)
210         {
211             con.SendEvent(TcpJsonConnection.InputState.Left);
212         }
213
214         private void Down_Pressed(object sender, EventArgs e)
215         {
216             con.SendEvent(TcpJsonConnection.InputState.Down);
217         }
218
219
220         private void Up_Pressed(object sender, EventArgs e)
221         {
222             con.SendEvent(TcpJsonConnection.InputState.Up);
223         }
224     }
225 }