using RobotClientWpf.Utilities; using System.Windows.Controls; using System.Windows.Input; namespace RobotClientWpf.Views { /// /// Interaction logic for MainView.xaml /// public partial class MainView : UserControl { private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); private ChallengeFactory? challenge; private MainWindow? mainWindow; private const int AMOUNT_SPEED_ADDED_PER_CLICK = 200; private const int TURN_ANGLE_PER_CLICK = 30; public MainView() { InitializeComponent(); } public void InitializeChildView(ChallengeFactory challenge, MainWindow parent) { this.challenge = challenge; this.mainWindow = parent; // subscribe to events this.challenge.RobotStationary.SplitFlap.SplitFlapDisplayChanged += this.SplitFlap_SplitFlapDisplayChanged; this.challenge.RobotMobile.Battery.BatteryChanged += Battery_BatteryChanged; this.challenge.RobotMobile.Battery.SecondsSinceLastResponseUpdate += Battery_SecondsSinceLastResponseUpdate; } private void Battery_SecondsSinceLastResponseUpdate(object? sender, int e) { if(e > 15) { UIAccessHelpers.SetShapeVisibility(ellipseRed, true); UIAccessHelpers.SetShapeVisibility(ellipseGreen, false); } else { UIAccessHelpers.SetShapeVisibility(ellipseRed, false); UIAccessHelpers.SetShapeVisibility(ellipseGreen, true); } } private void Battery_BatteryChanged(object? sender, RobotLib.Battery.BatteryEventArgs e) { UIAccessHelpers.SetTextboxText(tbRoboVoltage, e.Voltage.ToString()); } public void SplitFlap_SplitFlapDisplayChanged(object? sender, RobotLib.SplitFlap.SplitFlapDisplayEventArgs e) { UIAccessHelpers.SetTextboxText(this.tbSplitflapText, e.DisplayMessage); } private void btnModeAuto_Click(object sender, System.Windows.RoutedEventArgs e) { this.challenge?.RobotMobile.Movement.SetMobilityMode(true); } private void btnModeManual_Click(object sender, System.Windows.RoutedEventArgs e) { this.challenge?.RobotMobile.Movement.SetMobilityMode(false); } private void btnRoboFwd_Click(object sender, System.Windows.RoutedEventArgs e) { this.challenge?.RobotMobile.Movement.AddSpeed(AMOUNT_SPEED_ADDED_PER_CLICK); } private void btnRoboBwd_Click(object sender, System.Windows.RoutedEventArgs e) { this.challenge?.RobotMobile.Movement.AddSpeed(-AMOUNT_SPEED_ADDED_PER_CLICK); } private void btnRoboRight_Click(object sender, System.Windows.RoutedEventArgs e) { this.challenge?.RobotMobile.Movement.Turn(TURN_ANGLE_PER_CLICK); } private void btnRoboLeft_Click(object sender, System.Windows.RoutedEventArgs e) { this.challenge?.RobotMobile.Movement.Turn(-TURN_ANGLE_PER_CLICK); } private void btnStopMove_Click(object sender, System.Windows.RoutedEventArgs e) { this.challenge?.RobotMobile.Movement.Stop(); } public void HandleKeyDownEvent(object sender, KeyEventArgs e) { if (e.Key == Key.Up) { this.challenge?.RobotMobile.Movement.AddSpeed(AMOUNT_SPEED_ADDED_PER_CLICK); e.Handled = true; } else if (e.Key == Key.Down) { this.challenge?.RobotMobile.Movement.AddSpeed(-AMOUNT_SPEED_ADDED_PER_CLICK); e.Handled = true; } else if (e.Key == Key.Left) { this.challenge?.RobotMobile.Movement.Turn(-TURN_ANGLE_PER_CLICK); e.Handled = true; } else if (e.Key == Key.Right) { this.challenge?.RobotMobile.Movement.Turn(TURN_ANGLE_PER_CLICK); e.Handled = true; } else if (e.Key == Key.End) { this.challenge?.RobotMobile.Movement.Stop(); e.Handled = true; } } private void UserControl_KeyDown(object sender, KeyEventArgs e) { this.HandleKeyDownEvent(sender, e); } } }