2024-02-08 15:59:56 +08:00
|
|
|
|
using System.Diagnostics;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
namespace WpfMaiTouchEmulator;
|
2024-02-08 18:02:54 +08:00
|
|
|
|
|
2024-02-08 12:41:21 +08:00
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
2024-02-08 16:46:57 +08:00
|
|
|
|
private readonly MaiTouchSensorButtonStateManager buttonState;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
private MaiTouchComConnector connector;
|
2024-02-08 17:55:04 +08:00
|
|
|
|
private readonly VirtualComPortManager comPortManager;
|
|
|
|
|
private TouchPanel _touchPanel;
|
2024-02-08 12:41:21 +08:00
|
|
|
|
|
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
Title = "Mai Touch Emulator";
|
|
|
|
|
buttonState = new MaiTouchSensorButtonStateManager(buttonStateValue);
|
|
|
|
|
connector = new MaiTouchComConnector(buttonState);
|
2024-02-08 17:55:04 +08:00
|
|
|
|
comPortManager = new VirtualComPortManager();
|
|
|
|
|
connector.OnConnectStatusChange = (status) =>
|
|
|
|
|
{
|
|
|
|
|
connectionStateLabel.Content = status;
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-08 12:41:21 +08:00
|
|
|
|
connector.OnDataSent = (data) =>
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
SentLogBox.AppendText(data + Environment.NewLine);
|
|
|
|
|
SentLogBox.ScrollToEnd();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
connector.OnDataRecieved = (data) =>
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
RecievedLogBox.AppendText(data + Environment.NewLine);
|
|
|
|
|
RecievedLogBox.ScrollToEnd();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-08 17:55:04 +08:00
|
|
|
|
Loaded += (s, e) => {
|
|
|
|
|
_touchPanel = new TouchPanel();
|
|
|
|
|
_touchPanel.onTouch = (value) => { buttonState.PressButton(value); };
|
|
|
|
|
_touchPanel.onRelease = (value) => { buttonState.ReleaseButton(value); };
|
|
|
|
|
_touchPanel.Show();
|
|
|
|
|
_touchPanel.Owner = this;
|
|
|
|
|
DataContext = new MainWindowViewModel()
|
|
|
|
|
{
|
|
|
|
|
IsDebugEnabled = Properties.Settings.Default.IsDebugEnabled,
|
|
|
|
|
IsAutomaticPortConnectingEnabled = Properties.Settings.Default.IsAutomaticPortConnectingEnabled,
|
|
|
|
|
IsAutomaticPositioningEnabled = Properties.Settings.Default.IsAutomaticPositioningEnabled,
|
|
|
|
|
IsExitWithSinmaiEnabled = Properties.Settings.Default.IsExitWithSinmaiEnabled
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
_touchPanel.SetDebugMode(dataContext.IsDebugEnabled);
|
|
|
|
|
AutomaticTouchPanelPositioningLoop();
|
|
|
|
|
AutomaticcPortConnectingLoop();
|
|
|
|
|
ExitWithSinmaiLoop();
|
2024-02-08 16:46:57 +08:00
|
|
|
|
};
|
2024-02-08 17:55:04 +08:00
|
|
|
|
}
|
2024-02-08 16:46:57 +08:00
|
|
|
|
|
2024-02-08 17:55:04 +08:00
|
|
|
|
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
foreach (Window childWindow in OwnedWindows)
|
|
|
|
|
{
|
|
|
|
|
childWindow.Close();
|
|
|
|
|
}
|
2024-02-08 15:59:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void ExitWithSinmaiLoop()
|
|
|
|
|
{
|
|
|
|
|
Process? sinamiProcess = null;
|
|
|
|
|
while (sinamiProcess == null)
|
|
|
|
|
{
|
|
|
|
|
var processes = Process.GetProcessesByName("Sinmai");
|
|
|
|
|
if (processes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
sinamiProcess = processes[0];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await sinamiProcess.WaitForExitAsync();
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
if (dataContext.IsExitWithSinmaiEnabled)
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void AutomaticTouchPanelPositioningLoop()
|
|
|
|
|
{
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (dataContext.IsAutomaticPositioningEnabled)
|
|
|
|
|
{
|
|
|
|
|
_touchPanel.PositionTouchPanel();
|
|
|
|
|
}
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
}
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 17:55:04 +08:00
|
|
|
|
private async void AutomaticcPortConnectingLoop()
|
|
|
|
|
{
|
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (dataContext.IsAutomaticPortConnectingEnabled)
|
|
|
|
|
{
|
|
|
|
|
await connector.StartTouchSensorPolling();
|
|
|
|
|
}
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-08 12:41:21 +08:00
|
|
|
|
private async void ConnectToPortButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2024-02-08 17:55:04 +08:00
|
|
|
|
await connector.StartTouchSensorPolling();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void debugMode_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsDebugEnabled;
|
|
|
|
|
dataContext.IsDebugEnabled = !enabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.IsDebugEnabled = dataContext.IsDebugEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 15:59:56 +08:00
|
|
|
|
_touchPanel.SetDebugMode(dataContext.IsDebugEnabled);
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void automaticTouchPanelPositioning_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsAutomaticPositioningEnabled;
|
|
|
|
|
dataContext.IsAutomaticPositioningEnabled = !enabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.IsAutomaticPositioningEnabled = enabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void automaticPortConnecting_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsAutomaticPortConnectingEnabled;
|
|
|
|
|
dataContext.IsAutomaticPortConnectingEnabled = !enabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.IsAutomaticPortConnectingEnabled = dataContext.IsAutomaticPortConnectingEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 15:59:56 +08:00
|
|
|
|
private void exitWithSinmai_Click(object sender, RoutedEventArgs e)
|
2024-02-08 12:41:21 +08:00
|
|
|
|
{
|
2024-02-08 15:59:56 +08:00
|
|
|
|
var dataContext = (MainWindowViewModel)DataContext;
|
|
|
|
|
var enabled = !dataContext.IsExitWithSinmaiEnabled;
|
|
|
|
|
dataContext.IsExitWithSinmaiEnabled = !enabled;
|
2024-02-08 16:46:57 +08:00
|
|
|
|
Properties.Settings.Default.IsExitWithSinmaiEnabled = dataContext.IsExitWithSinmaiEnabled;
|
|
|
|
|
Properties.Settings.Default.Save();
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|
2024-02-08 17:55:04 +08:00
|
|
|
|
|
|
|
|
|
private async void buttonInstallComPort_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var output = await comPortManager.InstallComPort();
|
|
|
|
|
MessageBox.Show(output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void buttonUninstallComPorts_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var output = await comPortManager.UninstallVirtualPorts();
|
|
|
|
|
MessageBox.Show(output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void buttonListComPorts_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var output = await comPortManager.CheckInstalledPortsAsync();
|
|
|
|
|
MessageBox.Show(output);
|
|
|
|
|
}
|
2024-02-08 12:41:21 +08:00
|
|
|
|
}
|