Added Communication Implementation.
[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
10 namespace ConnectionTest
11 {
12     public partial class MainPage : ContentPage
13     {
14
15
16         TcpJsonConnection con;
17
18         public MainPage()
19         {
20             InitializeComponent();
21
22             con = new TcpJsonConnection((field) => { });
23
24             //con.Connect(IPAddress.Parse("fe80::c0a:44f1:63e7:bfb8%4"));
25             con.Connect(Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]);    //localhost
26
27
28             StackLayout verticalLayout = new StackLayout();
29
30             Entry ent = new Entry();
31             verticalLayout.Children.Add(ent);
32             ent.TextChanged += Ent_TextChanged;
33
34
35             for (int i = 0; i < 3; i++)
36             {
37                 StackLayout horizontalLayout = new StackLayout();
38                 horizontalLayout.Orientation = StackOrientation.Horizontal;
39                 verticalLayout.Children.Add(horizontalLayout);
40
41                 Button dummy = new Button();
42                 dummy.BackgroundColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
43                 dummy.TextColor = new Color(0xffffffff, 0xffffffff, 0xffffffff);
44
45                 dummy.Text = "<";
46                 
47
48                 switch (i) {
49                     case 0:
50                         horizontalLayout.Children.Add(dummy);
51                         Button up = new Button();
52                         up.Pressed += Up_Pressed;
53                         up.Text = "^";
54                         horizontalLayout.Children.Add(up);
55                         horizontalLayout.Children.Add(dummy);
56                         break;
57
58                     case 1:
59                         Button left = new Button();
60                         left.Text = "<";
61                         left.Pressed += Left_Pressed;
62                         horizontalLayout.Children.Add(left);
63
64                         Button bomb = new Button();
65                         bomb.Pressed += Bomb_Pressed;
66                         bomb.Text = "*";
67                         horizontalLayout.Children.Add(bomb);
68
69                         Button right = new Button();
70                         right.Pressed += Right_Pressed;
71                         right.Text = ">";                    
72                         horizontalLayout.Children.Add(right);
73                         break;
74
75                     case 2:
76                         horizontalLayout.Children.Add(dummy);
77                         Button down = new Button();
78                         down.Pressed += Down_Pressed;
79                         down.Text = "V";
80                         horizontalLayout.Children.Add(down);
81                         horizontalLayout.Children.Add(dummy);
82                         break;
83
84
85                 }
86
87
88
89
90             }
91
92             Content = verticalLayout;
93             
94
95         }
96
97         private void Ent_TextChanged(object sender, TextChangedEventArgs e)
98         {
99             switch(((Entry)sender).Text)
100             {
101                 case "w":
102                     con.SendEvent(TcpJsonConnection.InputState.Up);
103                     break;
104                 case "a":
105                     con.SendEvent(TcpJsonConnection.InputState.Left);
106                     break;
107                 case "s":
108                     con.SendEvent(TcpJsonConnection.InputState.Down);
109                     break;
110                 case "d":
111                     con.SendEvent(TcpJsonConnection.InputState.Right);
112                     break;
113                 case " ":
114                     con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
115                     break;
116             }
117             ((Entry)sender).Text = "";
118             con.SendEvent(TcpJsonConnection.InputState.Idle);
119             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
120
121
122         }
123
124         private void Bomb_Pressed(object sender, EventArgs e)
125         {
126             con.SendEvent(TcpJsonConnection.InputBomb.PlaceBomb);
127             con.SendEvent(TcpJsonConnection.InputBomb.Idle);
128         }
129
130         private void Right_Pressed(object sender, EventArgs e)
131         {
132             con.SendEvent(TcpJsonConnection.InputState.Right);
133             con.SendEvent(TcpJsonConnection.InputState.Idle);
134         }
135
136         private void Left_Pressed(object sender, EventArgs e)
137         {
138             con.SendEvent(TcpJsonConnection.InputState.Left);
139             con.SendEvent(TcpJsonConnection.InputState.Idle);
140         }
141
142         private void Down_Pressed(object sender, EventArgs e)
143         {
144             con.SendEvent(TcpJsonConnection.InputState.Down);
145             con.SendEvent(TcpJsonConnection.InputState.Idle);
146         }
147
148
149         private void Up_Pressed(object sender, EventArgs e)
150         {
151             con.SendEvent(TcpJsonConnection.InputState.Up);
152             con.SendEvent(TcpJsonConnection.InputState.Idle);
153         }
154     }
155 }