Advanced Distributed Systems module at HSLU
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

110 lines
3.8 KiB

using RobotClientWpf.Utilities;
using System.Windows.Controls;
using System.Windows.Input;
namespace RobotClientWpf.Views
{
/// <summary>
/// Interaction logic for MainView.xaml
/// </summary>
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;
}
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);
}
}
}