Files
TrackTrends/Test_Merge/Settings.cs

261 lines
7.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test_Merge
{
public partial class Settings : Form
{
public const int EXPECTED_WINDOWS_AMOUNT = 7;
private string _grandPrixUrl = "";
private string _grandPrixName = "";
private int _grandPrixYear = 2000;
private List<string> _driverList = new List<string>();
private bool CreatingZone = false;
private Point ZoneP1;
private Point ZoneP2;
private bool CreatingWindow = false;
private Point WindowP1;
private Point WindowP2;
List<Rectangle> WindowsToAdd = new List<Rectangle>();
public string GrandPrixUrl { get => _grandPrixUrl; private set => _grandPrixUrl = value; }
public string GrandPrixName { get => _grandPrixName; private set => _grandPrixName = value; }
public int GrandPrixYear { get => _grandPrixYear; private set => _grandPrixYear = value; }
public List<string> DriverList { get => _driverList; private set => _driverList = value; }
public Settings()
{
InitializeComponent();
Load();
}
private void Load()
{
RefreshUI();
}
private void RefreshUI()
{
lsbDrivers.DataSource = null;
lsbDrivers.DataSource = DriverList;
if (CreatingZone)
{
if (ZoneP1 == new Point(-1, -1))
{
lblZonePointsRemaning.Text = "2 points Remaining";
}
else
{
lblZonePointsRemaning.Text = "1 point Remaining";
}
}
else
{
lblZonePointsRemaning.Text = "";
}
if (CreatingWindow)
{
if (WindowP1 == new Point(-1, -1))
{
lblWindowPointsRemaining.Text = "2 points Remaining";
}
else
{
lblWindowPointsRemaining.Text = "1 point Remaining";
}
lblWindowPointsRemaining.Text = EXPECTED_WINDOWS_AMOUNT - WindowsToAdd.Count() + " Windows remaining";
}
else
{
lblWindowPointsRemaining.Text = "";
lblWindowsRemaining.Text = "";
}
}
private void tbxGpUrl_TextChanged(object sender, EventArgs e)
{
GrandPrixUrl = tbxGpUrl.Text;
}
private void tbxGpName_TextChanged(object sender, EventArgs e)
{
GrandPrixName = tbxGpName.Text;
}
private void tbxGpYear_TextChanged(object sender, EventArgs e)
{
int year;
try
{
year = Convert.ToInt32(tbxGpYear.Text);
}
catch
{
year = 1545;
}
GrandPrixYear = year;
}
private void btnAddDriver_Click(object sender, EventArgs e)
{
string newDriver = tbxDriverName.Text;
DriverList.Add(newDriver);
RefreshUI();
}
private void btnRemoveDriver_Click(object sender, EventArgs e)
{
if (lsbDrivers.SelectedIndex >= 0)
{
DriverList.RemoveAt(lsbDrivers.SelectedIndex);
}
RefreshUI();
}
private void SwitchZoneCreation()
{
if (CreatingZone)
{
CreatingZone = false;
lblZonePointsRemaning.Text = "";
}
else
{
CreatingZone = true;
if (CreatingWindow)
SwitchWindowCreation();
ZoneP1 = new Point(-1, -1);
ZoneP2 = new Point(-1, -1);
lblZonePointsRemaning.Text = "2 Points left";
}
RefreshUI();
}
private void SwitchWindowCreation()
{
if (CreatingWindow)
{
CreatingWindow = false;
}
else
{
CreatingWindow = true;
if (CreatingZone)
SwitchZoneCreation();
WindowP1 = new Point(-1, -1);
WindowP2 = new Point(-1, -1);
WindowsToAdd = new List<Rectangle>();
}
RefreshUI();
}
private void btnCreatZone_Click(object sender, EventArgs e)
{
SwitchZoneCreation();
}
private void btnCreateWindow_Click(object sender, EventArgs e)
{
SwitchWindowCreation();
}
private void pbxMain_Click(object sender, EventArgs e)
{
if (CreatingZone && pbxMain.Image != null)
{
Point coordinates = pbxMain.PointToClient(new Point(MousePosition.X, MousePosition.Y));
float xOffset = (float)pbxMain.Image.Width / (float)pbxMain.Width;
float yOffset = (float)pbxMain.Image.Height / (float)pbxMain.Height;
Point newPoint = new Point(Convert.ToInt32((float)coordinates.X * xOffset), Convert.ToInt32((float)coordinates.Y * yOffset));
if (ZoneP1 == new Point(-1, -1))
{
ZoneP1 = newPoint;
}
else
{
ZoneP2 = newPoint;
//CreateNewZone();
SwitchZoneCreation();
}
RefreshUI();
}
}
private void pbxDriverZone_Click(object sender, EventArgs e)
{
if (CreatingWindow && pbxDriverZone.Image != null)
{
Point coordinates = pbxDriverZone.PointToClient(new Point(MousePosition.X, MousePosition.Y));
float xOffset = (float)pbxDriverZone.Image.Width / (float)pbxDriverZone.Width;
float yOffset = (float)pbxDriverZone.Image.Height / (float)pbxDriverZone.Height;
Point newPoint = new Point(Convert.ToInt32((float)coordinates.X * xOffset), Convert.ToInt32((float)coordinates.Y * yOffset));
if (WindowP1 == new Point(-1, -1))
{
WindowP1 = newPoint;
}
else
{
WindowP2 = newPoint;
WindowsToAdd.Add(createAbsoluteRectangle(WindowP1,WindowP2));
if (WindowsToAdd.Count < EXPECTED_WINDOWS_AMOUNT)
{
WindowP1 = new Point(-1, -1);
WindowP2 = new Point(-1, -1);
}
else
{
//CreateWindows();
}
}
RefreshUI();
}
}
private Rectangle createAbsoluteRectangle(Point p1, Point p2)
{
Point newP1 = new Point();
Point newP2 = new Point();
if (p1.X < p2.X)
{
newP1.X = p1.X;
newP2.X = p2.X;
}
else
{
newP1.X = p2.X;
newP2.X = p1.X;
}
if (p1.Y < p2.Y)
{
newP1.Y = p1.Y;
newP2.Y = p2.Y;
}
else
{
newP1.Y = p2.Y;
newP2.Y = p1.Y;
}
return new Rectangle(newP1.X, newP1.Y,newP2.X-newP1.X,newP2.Y -newP2.Y);
}
}
}