Пример #1
0
        public async void LcdPrint(ScreenMessage lcdt)
        {
            var isText = lcdt.Service.Equals("LCDT");
            FrameworkElement element        = null;
            var             expandToEdge    = false;
            SolidColorBrush backgroundBrush = null;

            if (lcdt.Action != null)
            {
                if (!string.IsNullOrWhiteSpace(lcdt.ARGB))
                {
                    if (lcdt.ARGB[0] == '#')
                    {
                        var hex = lcdt.ARGB.ToByteArray();
                        if (hex.Length > 3)
                        {
                            backgroundBrush =
                                new SolidColorBrush(
                                    Color.FromArgb(hex[0] == 0 ? (byte)255 : hex[0], hex[1], hex[2], hex[3]));
                        }
                    }
                    else
                    {
                        uint color;
                        if (uint.TryParse(lcdt.ARGB, out color))
                        {
                            var argb = new ArgbUnion {
                                Value = color
                            };
                            backgroundBrush =
                                new SolidColorBrush(
                                    Color.FromArgb(argb.A == 0 ? (byte)255 : argb.A, argb.R, argb.G, argb.B));
                        }
                    }
                }

                var action = lcdt.Action.ToUpperInvariant();
                switch (action)
                {
                case "ORIENTATION":
                {
                    var current = DisplayInformation.AutoRotationPreferences;
                    if (lcdt.Value.HasValue)
                    {
                        DisplayInformation.AutoRotationPreferences = (DisplayOrientations)lcdt.Value.Value;
                    }

                    await this.mainPage.SendResult(new ScreenResultMessage(lcdt) { ResultId = (int)current });

                    break;
                }

                case "ENABLE":
                {
                    this.mainPage.sensors[lcdt.Service + ":" + lcdt.Message] = 1;
                    return;
                }

                case "DISABLE":
                {
                    if (this.mainPage.sensors.ContainsKey(lcdt.Service + ":" + lcdt.Message))
                    {
                        this.mainPage.sensors.Remove(lcdt.Service + ":" + lcdt.Message);
                    }

                    return;
                }

                case "CLEAR":
                {
                    if (lcdt.Y.HasValue)
                    {
                        this.RemoveLine(lcdt.Y.Value);
                    }
                    else if (lcdt.Pid.HasValue)
                    {
                        this.RemoveId(lcdt.Pid.Value);
                    }
                    else
                    {
                        this.mainPage.canvas.Children.Clear();

                        if (backgroundBrush != null)
                        {
                            this.mainPage.canvas.Background = backgroundBrush;
                        }

                        this.lastY = -1;
                        this.mainPage.player.Stop();
                        this.mainPage.player.Source = null;
                    }

                    break;
                }

                case "BUTTON":
                {
                    element = new Button
                    {
                        Content    = lcdt.Message,
                        FontSize   = lcdt.Size ?? this.DefaultFontSize,
                        Tag        = lcdt.Tag,
                        Foreground = this.textForgroundBrush,
                        Background = new SolidColorBrush(Colors.Gray)
                    };

                    element.Tapped += async(s, a) => await this.mainPage.SendEvent(s, a, "tapped");

                    ((Button)element).Click += async(s, a) => await this.mainPage.SendEvent(s, a, "click");

                    element.PointerPressed += async(s, a) => await this.mainPage.SendEvent(s, a, "pressed");

                    element.PointerReleased += async(s, a) => await this.mainPage.SendEvent(s, a, "released");

                    break;
                }

                case "IMAGE":
                {
                    var imageBitmap = new BitmapImage(new Uri(lcdt.Path, UriKind.Absolute));

                    // imageBitmap.CreateOptions = Windows.UI.Xaml.Media.Imaging.BitmapCreateOptions.IgnoreImageCache;
                    if (lcdt.Width.HasValue)
                    {
                        imageBitmap.DecodePixelWidth = lcdt.Width.Value;
                    }

                    if (lcdt.Height.HasValue)
                    {
                        imageBitmap.DecodePixelHeight = lcdt.Height.Value;
                    }

                    element = new Image {
                        Tag = lcdt.Tag
                    };

                    ((Image)element).Source = imageBitmap;

                    element.Tapped += async(s, a) => await this.mainPage.SendEvent(s, a, "tapped");

                    break;
                }

                case "LINE":
                {
                    var line = new Line
                    {
                        X1 = lcdt.X.Value,
                        Y1 = lcdt.Y.Value,
                        X2 = lcdt.X2.Value,
                        Y2 = lcdt.Y2.Value,
                        StrokeThickness = lcdt.Width ?? 1,
                        Stroke          = this.foreground
                    };

                    element = line;

                    break;
                }

                case "INPUT":
                {
                    element = new TextBox
                    {
                        Text          = lcdt.Message ?? string.Empty,
                        FontSize      = lcdt.Size ?? this.DefaultFontSize,
                        TextWrapping  = TextWrapping.Wrap,
                        Foreground    = this.textForgroundBrush,
                        AcceptsReturn = lcdt.Multi ?? false
                    };

                    expandToEdge = true;

                    element.SetValue(Canvas.LeftProperty, lcdt.X);
                    element.SetValue(Canvas.TopProperty, lcdt.Y);

                    element.LostFocus +=
                        async(s, a) =>
                        await this.mainPage.SendEvent(s, a, "lostfocus", lcdt, ((TextBox)s).Text);

                    ((TextBox)element).TextChanged +=
                        async(s, a) => await this.mainPage.SendEvent(s, a, "changed", lcdt, ((TextBox)s).Text);

                    break;
                }

                case "RECTANGLE":
                {
                    var rect = new Rectangle {
                        Tag = lcdt.Tag, Fill = backgroundBrush ?? this.gray
                    };

                    if (lcdt.Width.HasValue)
                    {
                        rect.Width = lcdt.Width.Value;
                    }

                    if (lcdt.Height.HasValue)
                    {
                        rect.Height = lcdt.Height.Value;
                    }

                    element = rect;

                    element.Tapped += async(s, a) => await this.mainPage.SendEvent(s, a, "tapped", lcdt);

                    rect.PointerEntered += async(s, a) => await this.mainPage.SendEvent(s, a, "entered", lcdt);

                    rect.PointerExited += async(s, a) => await this.mainPage.SendEvent(s, a, "exited", lcdt);

                    break;
                }

                case "TEXT":
                {
                    var textBlock = new TextBlock
                    {
                        Text         = lcdt.Message,
                        FontSize     = lcdt.Size ?? this.DefaultFontSize,
                        TextWrapping = TextWrapping.Wrap,
                        Tag          = lcdt.Tag,
                        Foreground   = this.textForgroundBrush
                    };

                    expandToEdge = true;

                    element = textBlock;
                    element.SetValue(Canvas.LeftProperty, lcdt.X);
                    element.SetValue(Canvas.TopProperty, lcdt.Y);
                    break;
                }

                default:
                    break;
                }
            }

            if (element == null && isText && lcdt.Message != null)
            {
                var x = lcdt.X ?? 0;
                var y = lcdt.Y ?? this.lastY + 1;

                expandToEdge = true;

                element = new TextBlock
                {
                    Text         = lcdt.Message,
                    FontSize     = lcdt.Size ?? this.DefaultFontSize,
                    TextWrapping = TextWrapping.Wrap,
                    Tag          = y.ToString(),
                    Foreground   = this.textForgroundBrush
                };

                var textblock = (TextBlock)element;

                textblock.FontFamily = this.fixedFont;

                if (lcdt.Foreground != null)
                {
                    textblock.Foreground = HexColorToBrush(lcdt.Foreground);
                }

                if (lcdt.HorizontalAlignment != null)
                {
                    if (lcdt.HorizontalAlignment.Equals("Center"))
                    {
                        textblock.TextAlignment = TextAlignment.Center;
                    }
                }

                element.SetValue(Canvas.LeftProperty, isText ? x * textblock.FontSize : x);
                element.SetValue(Canvas.TopProperty, isText ? y * textblock.FontSize : y);
            }
            else if (element != null && element.GetType() != typeof(Line))
            {
                element.SetValue(Canvas.LeftProperty, lcdt.X);
                element.SetValue(Canvas.TopProperty, lcdt.Y);
            }

            if (element != null)
            {
                var x = lcdt.X ?? 0;
                var y = lcdt.Y ?? this.lastY + 1;

                if (lcdt.HorizontalAlignment != null)
                {
                    if (lcdt.HorizontalAlignment.Equals("Center"))
                    {
                        element.HorizontalAlignment = HorizontalAlignment.Center;
                        element.Width = this.mainPage.canvas.Width;
                    }
                }

                if (lcdt.FlowDirection != null)
                {
                    if (lcdt.FlowDirection.Equals("RightToLeft"))
                    {
                        element.FlowDirection = FlowDirection.RightToLeft;
                    }
                    else if (lcdt.FlowDirection.Equals("LeftToRight"))
                    {
                        element.FlowDirection = FlowDirection.LeftToRight;
                    }
                }

                if (lcdt.Width.HasValue)
                {
                    element.Width = lcdt.Width.Value;
                }
                else if (expandToEdge)
                {
                    element.Width = this.mainPage.canvas.ActualWidth;
                }

                if (lcdt.Height.HasValue)
                {
                    element.Height = lcdt.Height.Value;
                }

                // TODO: add optional/extra properties in a later version here.
                if (isText && x == 0)
                {
                    this.RemoveLine(y);
                }

                element.SetValue(RemoteIdProperty, lcdt.Id);

                this.mainPage.canvas.Children.Add(element);

                if (isText)
                {
                    this.lastY = y;
                }
            }
        }
Пример #2
0
        public async void LcdPrint(ScreenMessage lcdt)
        {
            var isText = lcdt.Service.Equals("LCDT");
            FrameworkElement element = null;
            var expandToEdge = false;
            SolidColorBrush backgroundBrush = null;

            if (lcdt.Action != null)
            {
                if (!string.IsNullOrWhiteSpace(lcdt.ARGB))
                {
                    if (lcdt.ARGB[0] == '#')
                    {
                        var hex = lcdt.ARGB.ToByteArray();
                        if (hex.Length > 3)
                        {
                            backgroundBrush =
                                new SolidColorBrush(Color.FromArgb((hex[0] == 0 ? (byte) 255 : hex[0]), hex[1], hex[2],
                                    hex[3]));
                        }
                    }
                    else
                    {
                        UInt32 color;
                        if (UInt32.TryParse(lcdt.ARGB, out color))
                        {
                            var argb = new ArgbUnion {Value = color};
                            backgroundBrush = new SolidColorBrush(Color.FromArgb(argb.A == 0 ? (byte) 255 : argb.A, argb.R, argb.G, argb.B));
                        }
                    }   
                }

                var action = lcdt.Action.ToUpperInvariant();
                switch (action)
                {
                    case "ORIENTATION":
                        {
                            var current = DisplayInformation.AutoRotationPreferences;
                            if (lcdt.Value.HasValue)
                            {
                                DisplayInformation.AutoRotationPreferences = (DisplayOrientations)lcdt.Value.Value;
                            }

                            await mainPage.SendResult(new ScreenResultMessage(lcdt) { ResultId = (int)current });

                            break;
                        }
                    case "ENABLE":
                        {
                            mainPage.sensors[lcdt.Service + ":" + lcdt.Message] = 1;
                            return;
                        }
                    case "DISABLE":
                        {
                            if (mainPage.sensors.ContainsKey(lcdt.Service + ":" + lcdt.Message))
                            {
                                mainPage.sensors.Remove(lcdt.Service + ":" + lcdt.Message);
                            }

                            return;
                        }
                    case "CLEAR":
                        {
                            if (lcdt.Y.HasValue)
                            {
                                RemoveLine(lcdt.Y.Value);
                            }
                            else if (lcdt.Pid.HasValue)
                            {
                                RemoveId(lcdt.Pid.Value);
                            }
                            else
                            {
                                mainPage.canvas.Children.Clear();

                                if (backgroundBrush != null)
                                {
                                    mainPage.canvas.Background = backgroundBrush;
                                }

                                lastY = -1;
                                mainPage.player.Stop();
                                mainPage.player.Source = null;
                            }

                            break;
                        }

                    case "BUTTON":
                        {
                            element = new Button
                            {
                                Content = lcdt.Message,
                                FontSize = lcdt.Size ?? DefaultFontSize,
                                Tag = lcdt.Tag,
                                Foreground = textForgroundBrush,
                                Background =  new SolidColorBrush(Colors.Gray)

                            };

                            element.Tapped += async (s, a) => await mainPage.SendEvent(s, a, "tapped");
                            ((Button)element).Click += async (s, a) => await mainPage.SendEvent(s, a, "click");
                            element.PointerPressed += async (s, a) => await mainPage.SendEvent(s, a, "pressed");
                            element.PointerReleased += async (s, a) => await mainPage.SendEvent(s, a, "released");

                            break;
                        }

                    case "IMAGE":
                        {
                            var imageBitmap = new BitmapImage(new Uri(lcdt.Path, UriKind.Absolute));
                            //imageBitmap.CreateOptions = Windows.UI.Xaml.Media.Imaging.BitmapCreateOptions.IgnoreImageCache;

                            if (lcdt.Width.HasValue)
                            {
                                imageBitmap.DecodePixelWidth = lcdt.Width.Value;
                            }

                            if (lcdt.Height.HasValue)
                            {
                                imageBitmap.DecodePixelHeight = lcdt.Height.Value;
                            }

                            element = new Image
                            {
                                Tag = lcdt.Tag
                            };

                            ((Image)element).Source = imageBitmap;

                            element.Tapped += async (s, a) => await mainPage.SendEvent(s, a, "tapped");
                            break;
                        }
                    case "LINE":
                    {
                        var line = new Line
                        {
                            X1 = lcdt.X.Value,
                            Y1 = lcdt.Y.Value,
                            X2 = lcdt.X2.Value,
                            Y2 = lcdt.Y2.Value,
                            StrokeThickness = lcdt.Width ?? 1,
                            Stroke = foreground
                        };

                        element = line;

                        break;
                    }

                    case "INPUT":
                        {
                            element = new TextBox
                            {
                                Text = lcdt.Message,
                                FontSize = lcdt.Size ?? DefaultFontSize,
                                TextWrapping = TextWrapping.Wrap,
                                Foreground = textForgroundBrush,
                                AcceptsReturn = lcdt.Multi ?? false
                            };

                            expandToEdge = true;

                            element.SetValue(Canvas.LeftProperty, lcdt.X);
                            element.SetValue(Canvas.TopProperty, lcdt.Y);

                            element.LostFocus += async (s, a) => await mainPage.SendEvent(s, a, "lostfocus", lcdt, ((TextBox)s).Text);
                            ((TextBox)element).TextChanged += async (s, a) => await mainPage.SendEvent(s, a, "changed", lcdt, ((TextBox)s).Text);

                            break;
                        }
                    case "CHANGE":
                        {
                            var retrievedElement = GetId(lcdt.Pid.Value);
                            if (retrievedElement != null)
                            {
                                if (lcdt.ARGB != null)
                                {
                                    var i = 0;
                                }
                            }

                            break;
                        }
                    case "RECTANGLE":
                        {
                            var rect = new Rectangle
                            {
                                Tag = lcdt.Tag,
                                Fill = backgroundBrush ?? gray
                            };

                            if (lcdt.Width.HasValue)
                            {
                                rect.Width = lcdt.Width.Value;
                            }

                            if (lcdt.Height.HasValue)
                            {
                                rect.Height = lcdt.Height.Value;
                            }

                            element = rect;

                            element.Tapped += async (s, a) => await mainPage.SendEvent(s, a, "tapped", lcdt);
                            rect.PointerEntered += async (s, a) => await mainPage.SendEvent(s, a, "entered", lcdt);
                            rect.PointerExited += async (s, a) => await mainPage.SendEvent(s, a, "exited", lcdt);

                            break;
                        }
                    case "TEXT":
                        {
                            TextBlock textBlock = new TextBlock
                            {
                                Text = lcdt.Message,
                                FontSize = lcdt.Size ?? DefaultFontSize,
                                TextWrapping = TextWrapping.Wrap,
                                Tag = lcdt.Tag,
                                Foreground = textForgroundBrush
                            };

                            expandToEdge = true;

                            element = textBlock;
                            element.SetValue(Canvas.LeftProperty, lcdt.X);
                            element.SetValue(Canvas.TopProperty, lcdt.Y);
                            break;
                        }

                    default:
                        break;
                }
            }

            if (element == null && isText && lcdt.Message != null)
            {
                var x = lcdt.X ?? 0;
                var y = lcdt.Y ?? lastY + 1;

                expandToEdge = true;

                element = new TextBlock
                {
                    Text = lcdt.Message,
                    FontSize = lcdt.Size ?? DefaultFontSize,
                    TextWrapping = TextWrapping.Wrap,
                    Tag = y.ToString(),
                    Foreground = textForgroundBrush
                };

                var textblock = (TextBlock)element;

                textblock.FontFamily = fixedFont;

                if (lcdt.Foreground != null)
                {
                    textblock.Foreground = HexColorToBrush(lcdt.Foreground);
                }

                if (lcdt.HorizontalAlignment != null)
                {
                    if (lcdt.HorizontalAlignment.Equals("Center"))
                    {
                        textblock.TextAlignment = TextAlignment.Center;
                    }
                }

                element.SetValue(Canvas.LeftProperty, isText ? x * textblock.FontSize : x);
                element.SetValue(Canvas.TopProperty, isText ? y * textblock.FontSize : y);
            }
            else if (element != null && element.GetType() != typeof(Line))
            {
                element.SetValue(Canvas.LeftProperty, lcdt.X);
                element.SetValue(Canvas.TopProperty, lcdt.Y);
            }

            if (element != null)
            {
                var x = lcdt.X ?? 0;
                var y = lcdt.Y ?? lastY + 1;

                if (lcdt.HorizontalAlignment != null)
                {
                    if (lcdt.HorizontalAlignment.Equals("Center"))
                    {
                        element.HorizontalAlignment = HorizontalAlignment.Center;
                        element.Width = mainPage.canvas.Width;
                    }
                }

                if (lcdt.FlowDirection != null)
                {
                    if (lcdt.FlowDirection.Equals("RightToLeft"))
                    {
                        element.FlowDirection = FlowDirection.RightToLeft;
                    } else if (lcdt.FlowDirection.Equals("LeftToRight"))
                    {
                        element.FlowDirection = FlowDirection.LeftToRight;
                    }
                }

                if (lcdt.Width.HasValue)
                {
                    element.Width = lcdt.Width.Value;
                }
                else if (expandToEdge)
                {
                    element.Width = mainPage.canvas.ActualWidth;
                }

                if (lcdt.Height.HasValue)
                {
                    element.Height = lcdt.Height.Value;
                }

                //TODO: add optional/extra properties in a later version here.
                if (isText && x == 0)
                {
                    RemoveLine(y);
                }

                element.SetValue(RemoteIdProperty, lcdt.Id);

                mainPage.canvas.Children.Add(element);

                if (isText)
                {
                    lastY = y;
                }
            }
        }