Files
TrackTrends/Test_Merge/Form1.cs
T
2023-05-17 15:52:33 +02:00

116 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_Merge
{
public partial class Form1 : Form
{
Reader Reader = null;
F1TVEmulator Emulator = null;
private bool cancelRequested = false;
public Form1()
{
InitializeComponent();
}
public async void RefreshUI()
{
}
private async void btnSettings_Click(object sender, EventArgs e)
{
Emulator.Stop();
Settings settingsForm = new Settings();
settingsForm.ShowDialog();
//MessageBox.Show(settingsForm.GrandPrixUrl + Environment.NewLine + settingsForm.GrandPrixName + Environment.NewLine + settingsForm.GrandPrixYear);
Emulator = new F1TVEmulator(settingsForm.GrandPrixUrl);
try
{
await Emulator.Start();
Reader = new Reader(settingsForm.SelectedConfigFile, Emulator.Screenshot(), true);
}
catch
{
//Could not start the driver even with the new config
MessageBox.Show("The config is wrong or incomplete please try again");
}
}
private async void Form1_Load(object sender, EventArgs e)
{
string configFile = "./Presets/Clean_4K_2023.json";
string gpUrl = "https://f1tv.formula1.com/detail/1000006688/2023-azerbaijan-grand-prix?action=play";
Emulator = new F1TVEmulator(gpUrl);
await Emulator.Start();
Reader = new Reader(configFile, Emulator.Screenshot(), true);
}
private async void btnUpdate_Click(object sender, EventArgs e)
{
cancelRequested = false;
if (Emulator != null && Reader != null)
{
// Disable UI controls to prevent re-entrancy
btnStartDecoding.Enabled = false;
btnStopUpdating.Enabled = true;
btnSettings.Enabled = false;
while (!cancelRequested)
{
// Start the time-consuming task on a separate thread
await Task.Run(async () =>
{
Stopwatch sw = new Stopwatch();
sw.Start();
Bitmap screen = Emulator.Screenshot();
screen.Save("SCREEEEEEEEN.png");
Reader.ChangeImage(screen);
string result = await Reader.Decode(Reader.MainZones, Reader.Drivers);
sw.Stop();
// Task completed
Invoke((MethodInvoker)delegate
{
DisplayResults(result, sw, screen);
});
});
}
// Re-enable UI controls
btnStartDecoding.Enabled = true;
btnStopUpdating.Enabled = false;
btnSettings.Enabled = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (Emulator != null)
{
Emulator.Stop();
}
}
private void DisplayResults(string result,Stopwatch sw,Bitmap screen)
{
tbxResult.Text = "";
tbxResult.Text = "Decoding done in :" + sw.ElapsedMilliseconds + "ms" + Environment.NewLine;
tbxResult.Text += result;
pbxResult.Image = screen;
}
private void btnStopUpdating_Click(object sender, EventArgs e)
{
// Set the cancellation flag
cancelRequested = true;
}
}
}