public LauncherViewModel(ConfigHandler configHandler)
        {
            BrowseOverlayImageCommand = new RelayCommand((p) => {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter         = "";

                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                string format           = "{0}{1}{2} ({3})|{3}";
                openFileDialog.Filter   = string.Format(format, openFileDialog.Filter, string.Empty, "All Files", "*.*");

                foreach (var c in codecs)
                {
                    string codecName      = c.CodecName.Substring(8).Replace("Codec", "Files").Trim();
                    openFileDialog.Filter = string.Format(format, openFileDialog.Filter, "|", codecName, c.FilenameExtension);
                }

                if (openFileDialog.ShowDialog() == true)
                {
                    OverlayImagePath = openFileDialog.FileName;
                }
            });

            ConfigureCommand = new RelayCommand((p) => {
                new EditDisplayConfigurationViewModel(configHandler);
            });

            LaunchDisplayCommand = new RelayCommand(
                (p) => !string.IsNullOrWhiteSpace(OverlayImagePath) &&
                !string.IsNullOrWhiteSpace(ServerPort) &&
                !string.IsNullOrWhiteSpace(ServerAddress),
                (p) => {
                if (!IsValidIP(ServerAddress))
                {
                    DialogFactory.CreateErrorDialog(Constants.C_LaunchError, "The IP address is invalid.");
                    return;
                }

                if (!int.TryParse(ServerPort, out int port))
                {
                    DialogFactory.CreateErrorDialog(Constants.C_LaunchError, "Port must only contain numbers.");
                    return;
                }

                if (!Uri.TryCreate(OverlayImagePath, UriKind.RelativeOrAbsolute, out Uri imageUri))
                {
                    DialogFactory.CreateErrorDialog(Constants.C_LaunchError, "The image path is incorrect.");
                    return;
                }

                ImageBrush brush;
                try {
                    brush = new ImageBrush(new BitmapImage(imageUri));
                } catch (NotSupportedException) {
                    DialogFactory.CreateErrorDialog(Constants.C_LaunchError, "Image file type not supported.");
                    return;
                }

                new PreviewDisplayConfigurationViewModel(configHandler.GetActiveConfig(), ServerAddress, port, brush);
            });
        }
Ejemplo n.º 2
0
        private void AttemptToSaveConfig()
        {
            var saveRc = _configHandler.SaveConfig();

            if (saveRc.ReturnCode != ConfigHandlerReturnCodeType.Success)
            {
                DialogFactory.CreateErrorDialog(Constants.C_ConfigErrorTitle, saveRc.Reason);
            }
        }
        public EditDisplayConfigurationViewModel(ConfigHandler configHandler) : base(configHandler.GetActiveConfig())
        {
            _configHandler = configHandler;

            SaveCommand = new RelayCommand(x => HasConfigChanged(), x => {
                _configHandler.SetActiveConfig(Rows, Columns, Buttons.Select(y => y.Message).ToList());
                _configHandler.SaveConfig();
            });

            CloseCommand = new RelayCommand(x => {
                var hasConfigChanged = HasConfigChanged();
                if (!hasConfigChanged || (hasConfigChanged && DialogFactory.CreateYesNoDialog("Exit Configuration", "Are you sure you would like to exit? You will lose any changes you have made since the last time you saved!")))
                {
                    base.CloseCommand.Execute(null);
                }
            });

            ButtonClickCommand = new RelayCommand(x => {
                CustomWindow window = null;
                var button          = (ButtonViewModel)x;
                var vm       = new EditButtonMessageViewModel(button.Message, () => window.Close());
                window       = new CustomWindow(vm, "Edit Button Message");
                window.Owner = Application.Current.MainWindow;
                window.MinimizeVisibility = Visibility.Collapsed;
                window.XVisibility        = Visibility.Collapsed;
                window.Height             = 160;

                window.ShowDialog();

                if (vm.Result)
                {
                    button.Message = vm.ButtonMessage;
                }
            });

            DisplayLines         = true;
            DisplayWindow.Cursor = null;
            DisplayWindow.Show();
        }
Ejemplo n.º 4
0
        public void AppStartup(object sender, StartupEventArgs e)
        {
            _configHandler = new ConfigHandler();

            //The showing of error dialogs must be done after the main window shows since dialogs set the owner to the main window which wouldn't have been created yet
            var rc = _configHandler.LoadConfig();

            var vm = new LauncherViewModel(_configHandler);

            _window = new CustomWindow(vm);
            _window.Show();

            if (rc.ReturnCode != ConfigHandlerReturnCodeType.Success)
            {
                _configHandler.CreateNewConfig();
                if (rc.ReturnCode == ConfigHandlerReturnCodeType.InvalidConfiguration)
                {
                    var result = DialogFactory.CreateYesNoDialog(
                        Constants.C_ConfigErrorTitle,
                        "Could not load configuration file due to it being invalid.\n\nUsing a new configuration for now. Would you like to overwrite the new configuration with the new configuration?"
                        );
                    if (result)
                    {
                        AttemptToSaveConfig();
                    }
                }
                else if (rc.ReturnCode == ConfigHandlerReturnCodeType.FileNotFound)
                {
                    AttemptToSaveConfig();
                }
                else
                {
                    DialogFactory.CreateErrorDialog(Constants.C_ConfigErrorTitle, rc.Reason);
                }
            }
        }
        public PreviewDisplayConfigurationViewModel(Config activeConfig, string address, int port, ImageBrush backgroundBrush) : base(activeConfig)
        {
            DisplayWindow.Background = backgroundBrush;
            DisplayWindow.Cursor     = Cursors.None;

            DisplayWindow.InputBindings.Add(new KeyBinding(new RelayCommand((param) => DisplayLines = !DisplayLines), Key.M, ModifierKeys.Control));

            ToggleButtonMapCommand = new RelayCommand(x => DisplayLines = !DisplayLines);

            ButtonClickCommand = new RelayCommand(async p => {
                int t = await Task <int> .Factory.StartNew(() => {
                    try {
                        using (TcpClient client = new TcpClient()) {
                            if (!client.ConnectAsync(address, port).Wait(2000))
                            {
                                return(2);
                            }

                            byte[] data = Encoding.ASCII.GetBytes(((ButtonViewModel)p).Message + "\r");

                            using (NetworkStream stream = client.GetStream()) {
                                stream.Write(data, 0, data.Length);
                            }
                            return(0);
                        }
                    } catch (AggregateException e) {
                        int exceptionCode = -1;
                        e.Handle(ex => {
                            if (exceptionCode != -1)
                            {
                                return(true);
                            }
                            if (ex is IOException)
                            {
                                exceptionCode = 1;
                            }
                            else if (ex is SocketException)
                            {
                                exceptionCode = 2;
                            }
                            else
                            {
                                return(false);
                            }
                            return(true);
                        });
                        return(exceptionCode);
                    }
                });

                if (!_isExceptionBeingAcknowledged && t == 1)
                {
                    _isExceptionBeingAcknowledged = true;
                    DialogFactory.CreateErrorDialog(Constants.C_ConnectionError, "Connection lost, message not sent.", DisplayWindow);
                    _isExceptionBeingAcknowledged = false;
                }
                else if (!_isExceptionBeingAcknowledged && t == 2)
                {
                    _isExceptionBeingAcknowledged = true;
                    DialogFactory.CreateErrorDialog(Constants.C_ConnectionError, "Unable to connect to host.", DisplayWindow);
                    _isExceptionBeingAcknowledged = false;
                }
            });

            DisplayWindow.ShowDialog();
        }