Clients/Test/FormClientTest.cs
author StephaneLenclud
Wed, 31 Aug 2016 16:06:47 +0200
changeset 262 c4749a27966d
parent 191 7ada476f3fd7
child 274 920fea7a6427
permissions -rw-r--r--
Fix crash when trying to select Harmony command without configuration.
Consolidate Named event Trigger.
     1 //
     2 // Copyright (C) 2014-2015 Stéphane Lenclud.
     3 //
     4 // This file is part of SharpDisplayManager.
     5 //
     6 // SharpDisplayManager is free software: you can redistribute it and/or modify
     7 // it under the terms of the GNU General Public License as published by
     8 // the Free Software Foundation, either version 3 of the License, or
     9 // (at your option) any later version.
    10 //
    11 // SharpDisplayManager is distributed in the hope that it will be useful,
    12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14 // GNU General Public License for more details.
    15 //
    16 // You should have received a copy of the GNU General Public License
    17 // along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    18 //
    19 
    20 using System;
    21 using System.Collections.Generic;
    22 using System.ComponentModel;
    23 using System.Data;
    24 using System.Drawing;
    25 using System.Linq;
    26 using System.Text;
    27 using System.Threading.Tasks;
    28 using System.Windows.Forms;
    29 using System.ServiceModel;
    30 using System.ServiceModel.Channels;
    31 using System.Diagnostics;
    32 using SharpLib.Display;
    33 
    34 
    35 namespace SharpDisplayClient
    36 {
    37     public partial class FormClientTest : Form
    38     {
    39 		public StartParams Params { get; set; }
    40 
    41 		//
    42         Client iClient;
    43 		//
    44         ContentAlignment Alignment;
    45         TextField iTextFieldTop;
    46 
    47 		
    48 		/// <summary>
    49 		/// Constructor
    50 		/// </summary>
    51         public FormClientTest()
    52         {
    53             InitializeComponent();
    54             Alignment = ContentAlignment.MiddleLeft;
    55             iTextFieldTop = new TextField();
    56         }
    57 
    58         public void OnCloseOrder()
    59         {
    60             CloseThreadSafe();
    61         }
    62 
    63         /// <summary>
    64         /// 
    65         /// </summary>
    66         /// <param name="sender"></param>
    67         /// <param name="e"></param>
    68         private void MainForm_Load(object sender, EventArgs e)
    69         {
    70             iClient = new Client();
    71             iClient.CloseOrderEvent += OnCloseOrder;
    72             iClient.Open();
    73 
    74             //Connect using unique name
    75             //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
    76             string name = "Client-" + (iClient.ClientCount() - 1);
    77             iClient.SetName(name);
    78             //Text = Text + ": " + name;
    79             Text = "[[" + name + "]]  " + iClient.SessionId;
    80 
    81             //
    82             textBoxTop.Text = iClient.Name;
    83             textBoxBottom.Text = iClient.SessionId;
    84 
    85 			if (Params != null)
    86 			{
    87 				//Parameters where specified use them
    88 				if (Params.TopText != "")
    89 				{
    90 					textBoxTop.Text = Params.TopText;
    91 				}
    92 
    93 				if (Params.BottomText != "")
    94 				{
    95 					textBoxBottom.Text = Params.BottomText;
    96 				}
    97 
    98 				Location = Params.Location;
    99 				//
   100 				SetBasicLayoutAndText();
   101 			}
   102 
   103         }
   104 
   105 
   106 
   107         public delegate void CloseConnectionDelegate();
   108         public delegate void CloseDelegate();
   109 
   110         /// <summary>
   111         ///
   112         /// </summary>
   113         public void CloseConnectionThreadSafe()
   114         {
   115             if (this.InvokeRequired)
   116             {
   117                 //Not in the proper thread, invoke ourselves
   118                 CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
   119                 this.Invoke(d, new object[] { });
   120             }
   121             else
   122             {
   123                 //We are in the proper thread
   124                 if (IsClientReady())
   125                 {
   126                     string sessionId = iClient.SessionId;
   127                     Trace.TraceInformation("Closing client: " + sessionId);
   128                     iClient.Close();
   129                     Trace.TraceInformation("Closed client: " + sessionId);
   130                 }
   131 
   132                 iClient = null;
   133             }
   134         }
   135 
   136         /// <summary>
   137         ///
   138         /// </summary>
   139         public void CloseThreadSafe()
   140         {
   141             if (this.InvokeRequired)
   142             {
   143                 //Not in the proper thread, invoke ourselves
   144                 CloseDelegate d = new CloseDelegate(CloseThreadSafe);
   145                 this.Invoke(d, new object[] { });
   146             }
   147             else
   148             {
   149                 //We are in the proper thread
   150                 Close();
   151             }
   152         }
   153 
   154 
   155         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
   156         {
   157             CloseConnectionThreadSafe();
   158         }
   159 
   160         public bool IsClientReady()
   161         {
   162             return (iClient != null && iClient.IsReady());
   163         }
   164 
   165         private void buttonAlignLeft_Click(object sender, EventArgs e)
   166         {
   167             Alignment = ContentAlignment.MiddleLeft;
   168             textBoxTop.TextAlign = HorizontalAlignment.Left;
   169             textBoxBottom.TextAlign = HorizontalAlignment.Left;
   170         }
   171 
   172         private void buttonAlignCenter_Click(object sender, EventArgs e)
   173         {
   174             Alignment = ContentAlignment.MiddleCenter;
   175             textBoxTop.TextAlign = HorizontalAlignment.Center;
   176             textBoxBottom.TextAlign = HorizontalAlignment.Center;
   177         }
   178 
   179         private void buttonAlignRight_Click(object sender, EventArgs e)
   180         {
   181             Alignment = ContentAlignment.MiddleRight;
   182             textBoxTop.TextAlign = HorizontalAlignment.Right;
   183             textBoxBottom.TextAlign = HorizontalAlignment.Right;
   184         }
   185 
   186         private void buttonSetTopText_Click(object sender, EventArgs e)
   187         {
   188             //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
   189             iTextFieldTop.Text = textBoxTop.Text;
   190             iTextFieldTop.Alignment = Alignment;
   191             bool res = iClient.SetField(iTextFieldTop);
   192 
   193             if (!res)
   194             {
   195                 MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   196             }
   197 
   198 
   199         }
   200 
   201         private void buttonSetText_Click(object sender, EventArgs e)
   202         {
   203 			SetBasicLayoutAndText();
   204         }
   205 
   206 		void SetBasicLayoutAndText()
   207 		{
   208 			//Set one column two lines layout
   209 			TableLayout layout = new TableLayout(1, 2);
   210 			iClient.SetLayout(layout);
   211 
   212 			//Set our fields
   213 			iClient.CreateFields(new DataField[]
   214             {
   215                 new TextField(textBoxTop.Text, Alignment, 0, 0),
   216                 new TextField(textBoxBottom.Text, Alignment, 0, 1)
   217             });
   218 
   219 		}
   220 
   221         private void buttonLayoutUpdate_Click(object sender, EventArgs e)
   222         {
   223             //Define a 2 by 2 layout
   224             TableLayout layout = new TableLayout(2,2);
   225             //Second column only takes up 25%
   226             layout.Columns[1].Width = 25F;
   227             //Send layout to server
   228             iClient.SetLayout(layout);
   229 
   230             //
   231             RecordingField recording = new RecordingField();
   232             recording.IsActive = true;
   233             recording.Text = "Recording Tame of Gone until 22:05";
   234             //Set texts
   235             iClient.CreateFields(new DataField[]
   236             {                
   237                 new TextField(textBoxTop.Text, Alignment, 0, 0),
   238                 new TextField(textBoxBottom.Text, Alignment, 0, 1),
   239                 new TextField("Third text field", Alignment, 1, 0),
   240                 new TextField("Forth text field", Alignment, 1, 1),
   241                 recording
   242             });
   243 
   244         }
   245 
   246         private void buttonSetBitmap_Click(object sender, EventArgs e)
   247         {
   248             int x1 = 0;
   249             int y1 = 0;
   250             int x2 = 256;
   251             int y2 = 32;
   252 
   253             Bitmap bitmap = new Bitmap(x2,y2);
   254             Pen blackPen = new Pen(Color.Black, 3);
   255 
   256             // Draw line to screen.
   257             using (var graphics = Graphics.FromImage(bitmap))
   258             {
   259                 graphics.DrawLine(blackPen, x1, y1, x2, y2);
   260                 graphics.DrawLine(blackPen, x1, y2, x2, y1);
   261             }
   262 
   263             DataField field = new BitmapField(bitmap);
   264             //field.ColumnSpan = 2;
   265             iClient.SetField(field);
   266         }
   267 
   268         private void buttonBitmapLayout_Click(object sender, EventArgs e)
   269         {
   270             SetLayoutWithBitmap();
   271         }
   272 
   273         /// <summary>
   274         /// Define a layout with a bitmap field on the left and two lines of text on the right.
   275         /// </summary>
   276         private void SetLayoutWithBitmap()
   277         {
   278             //Define a 2 by 2 layout
   279             TableLayout layout = new TableLayout(2, 2);
   280             //First column only takes 25%
   281             layout.Columns[0].Width = 25F;
   282             //Second column takes up 75%
   283             layout.Columns[1].Width = 75F;
   284             //Send layout to server
   285             iClient.SetLayout(layout);
   286 
   287             //Set a bitmap for our first field
   288             int x1 = 0;
   289             int y1 = 0;
   290             int x2 = 64;
   291             int y2 = 64;
   292 
   293             Bitmap bitmap = new Bitmap(x2, y2);
   294             Pen blackPen = new Pen(Color.Black, 3);
   295 
   296             // Draw line to screen.
   297             using (var graphics = Graphics.FromImage(bitmap))
   298             {
   299                 graphics.DrawLine(blackPen, x1, y1, x2, y2);
   300                 graphics.DrawLine(blackPen, x1, y2, x2, y1);
   301             }
   302 
   303             //Create a bitmap field from the bitmap we just created
   304             //We want our bitmap field to span across two rows
   305             BitmapField bitmapField = new BitmapField(bitmap, 0, 0, 1, 2);
   306             
   307             //Set texts
   308             iClient.CreateFields(new DataField[]
   309             {
   310                 bitmapField,
   311                 new TextField(textBoxTop.Text, Alignment, 1, 0),
   312                 new TextField(textBoxBottom.Text, Alignment, 1, 1)
   313             });
   314 
   315         }
   316 
   317         private void buttonIndicatorsLayout_Click(object sender, EventArgs e)
   318         {
   319             //Define a 2 by 4 layout
   320             TableLayout layout = new TableLayout(2, 4);
   321             //First column
   322             layout.Columns[0].Width = 87.5F;
   323             //Second column
   324             layout.Columns[1].Width = 12.5F;
   325             //Send layout to server
   326             iClient.SetLayout(layout);
   327 
   328             //Create a bitmap for our indicators field
   329             int x1 = 0;
   330             int y1 = 0;
   331             int x2 = 32;
   332             int y2 = 16;
   333 
   334             Bitmap bitmap = new Bitmap(x2, y2);
   335             Pen blackPen = new Pen(Color.Black, 3);
   336 
   337             // Draw line to screen.
   338             using (var graphics = Graphics.FromImage(bitmap))
   339             {
   340                 graphics.DrawLine(blackPen, x1, y1, x2, y2);
   341                 graphics.DrawLine(blackPen, x1, y2, x2, y1);
   342             }
   343 
   344             //Create a bitmap field from the bitmap we just created
   345             DataField indicator1 = new BitmapField(bitmap, 1, 0);
   346             //Create a bitmap field from the bitmap we just created
   347             DataField indicator2 = new BitmapField(bitmap, 1, 1);
   348             //Create a bitmap field from the bitmap we just created
   349             DataField indicator3 = new BitmapField(bitmap, 1, 2);
   350             //Create a bitmap field from the bitmap we just created
   351             DataField indicator4 = new BitmapField(bitmap, 1, 3);
   352 
   353             //
   354             TextField textFieldTop = new TextField(textBoxTop.Text, Alignment, 0, 0, 1, 2);
   355             TextField textFieldBottom = new TextField(textBoxBottom.Text, Alignment, 0, 2, 1, 2);
   356 
   357             //Set texts
   358             iClient.CreateFields(new DataField[]
   359             {
   360                 textFieldTop,
   361                 textFieldBottom,
   362                 indicator1,
   363                 indicator2,                
   364                 indicator3,
   365                 indicator4
   366             });
   367 
   368         }
   369 
   370         private void buttonUpdateTexts_Click(object sender, EventArgs e)
   371         {
   372 
   373             bool res = iClient.SetFields(new DataField[]
   374             {
   375                 new TextField(textBoxTop.Text, Alignment,0,0),
   376                 new TextField(textBoxBottom.Text, Alignment,0,1)
   377             });
   378 
   379             if (!res)
   380             {
   381                 MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   382             }
   383 
   384         }
   385 
   386 		private void buttonLayoutOneTextField_Click(object sender, EventArgs e)
   387 		{
   388 			//Set one column one line layout
   389 			TableLayout layout = new TableLayout(1, 1);
   390 			iClient.SetLayout(layout);
   391 
   392 			//Set our fields
   393 			iClient.CreateFields(new DataField[]
   394             {
   395                 new TextField(textBoxTop.Text, Alignment)
   396             });
   397 		}
   398 
   399         private void numericUpDownPriority_ValueChanged(object sender, EventArgs e)
   400         {
   401             iClient.SetPriority((uint)numericUpDownPriority.Value);
   402         }
   403 
   404         private void buttonTriggerEvents_Click(object sender, EventArgs e)
   405         {
   406             iClient.TriggerEventsByName(textBoxEventName.Text);
   407         }
   408     }
   409 }