f763beb8202882921eac46b66614f72228c3343b
[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("192.168.0.5"));
35             con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[1]);    //localhost
36
37
38             Entry ent = new Entry();
39             ButtonLayout.Children.Add(ent);
40             ent.TextChanged += Ent_TextChanged;
41
42
43             for (int i = 0; i < 3; i++)
44             {
45                 StackLayout horizontalLayout = new StackLayout();
46                 horizontalLayout.Orientation = StackOrientation.Horizontal;
47                 ButtonLayout.Children.Add(horizontalLayout);
48
49                 Button dummy = new Button();
50                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
51                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
52
53                 dummy.Text = "<";
54
55
56                 switch (i)
57                 {
58                     case 0:
59                         horizontalLayout.Children.Add(dummy);
60                         Button up = new Button();
61                         up.Pressed += Up_Pressed;
62                         up.Released += Up_Released;
63                         up.Text = "^";
64                         horizontalLayout.Children.Add(up);
65                         horizontalLayout.Children.Add(dummy);
66                         break;
67
68                     case 1:
69                         Button left = new Button();
70                         left.Text = "<";
71                         left.Pressed += Left_Pressed;
72                         left.Released += Left_Released;
73                         horizontalLayout.Children.Add(left);
74
75                         Button bomb = new Button();
76                         bomb.Pressed += Bomb_Pressed;
77                         bomb.Released += Bomb_Released;
78                         bomb.Text = "*";
79                         horizontalLayout.Children.Add(bomb);
80
81                         Button right = new Button();
82                         right.Pressed += Right_Pressed;
83                         right.Released += Right_Released;
84                         right.Text = ">";
85                         horizontalLayout.Children.Add(right);
86                         break;
87
88                     case 2:
89                         horizontalLayout.Children.Add(dummy);
90                         Button down = new Button();
91                         down.Pressed += Down_Pressed;
92                         down.Released += Down_Released;
93                         down.Text = "V";
94                         horizontalLayout.Children.Add(down);
95                         horizontalLayout.Children.Add(dummy);
96                         break;
97
98
99                 }
100
101             }
102         }
103
104         private void Down_Released(object sender, EventArgs e)
105         {
106             con.SendEvent(TcpJsonConnection.InputState.Idle);
107         }
108
109         private void Right_Released(object sender, EventArgs e)
110         {
111             con.SendEvent(TcpJsonConnection.InputState.Idle);
112         }
113
114         private void Bomb_Released(object sender, EventArgs e)
115         {
116             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
117         }
118
119         private void Left_Released(object sender, EventArgs e)
120         {
121             con.SendEvent(TcpJsonConnection.InputState.Idle);
122         }
123
124         private void Up_Released(object sender, EventArgs e)
125         {
126             con.SendEvent(TcpJsonConnection.InputState.Idle);
127         }
128
129         private void MainPage_SizeChanged(object sender, EventArgs e)
130         {
131             CreateInitializePlayground(c_defaultPlaygroundSize);
132         }
133
134         void CreateInitializePlayground(int size)
135         {
136             fullLayout = new StackLayout();
137             playground = new BombermanPlayground.Playground(size);
138             BombermanPlayground.Playground.c_ImageNamespace = "ConnectionTest";
139             BombermanPlayground.BombermanBlock.BlockSize = Width / (size + 1);
140
141             fullLayout.Children.Add(ButtonLayout);
142             fullLayout.Children.Add(playground);
143             Content = fullLayout;
144
145         }
146
147         void ServerPkgReceived(ServerPackage pkg)
148         {
149             Device.BeginInvokeOnMainThread(() =>
150             {
151                 if(playground == null)
152                 {
153                     return;
154                 }
155                 if (playground.FieldSize != pkg.Field.Count)
156                 {
157                     CreateInitializePlayground(pkg.Field.Count);
158                 }
159                 playground.Update(pkg);
160
161             });
162         }
163
164
165         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
166         {
167             switch(((Entry)sender).Text)
168             {
169                 case "w":
170                     con.SendEvent(TcpJsonConnection.InputState.Up);
171                     break;
172                 case "a":
173                     con.SendEvent(TcpJsonConnection.InputState.Left);
174                     break;
175                 case "s":
176                     con.SendEvent(TcpJsonConnection.InputState.Down);
177                     break;
178                 case "d":
179                     con.SendEvent(TcpJsonConnection.InputState.Right);
180                     break;
181                 case " ":
182                     con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
183                     break;
184             }
185             ((Entry)sender).Text = "";
186             con.SendEvent(TcpJsonConnection.InputState.Idle);
187             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
188
189
190         }
191
192         private void Bomb_Pressed(object sender, EventArgs e)
193         {
194             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
195         }
196
197         private void Right_Pressed(object sender, EventArgs e)
198         {
199             con.SendEvent(TcpJsonConnection.InputState.Right);
200         }
201
202         private void Left_Pressed(object sender, EventArgs e)
203         {
204             con.SendEvent(TcpJsonConnection.InputState.Left);
205         }
206
207         private void Down_Pressed(object sender, EventArgs e)
208         {
209             con.SendEvent(TcpJsonConnection.InputState.Down);
210         }
211
212
213         private void Up_Pressed(object sender, EventArgs e)
214         {
215             con.SendEvent(TcpJsonConnection.InputState.Up);
216         }
217     }
218 }