/// Author : Maxime Rohmer /// Date : 30/05/2023 /// File : Zone.cs /// Brief : Class that contains all the methods and infos for a zone. This is designed to be potentially be inherited. /// Version : Alpha 1.0 using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TrackTrends { public class Zone { private Rectangle _bounds; private List _zones; private List _windows; private Bitmap _image; private string _name; public Bitmap ZoneImage { get { //This little trickery lets you have the image that the zone sees Bitmap sample = new Bitmap(Bounds.Width, Bounds.Height); Graphics g = Graphics.FromImage(sample); g.DrawImage(Image, new Rectangle(0, 0, sample.Width, sample.Height), Bounds, GraphicsUnit.Pixel); return sample; } } public Bitmap Image { get { return _image; } set { //It automatically sets the image for the contained windows and zones _image = value; foreach (Window w in Windows) { w.Image = ZoneImage; } foreach (Zone z in Zones) { z.Image = Image; } } } public Rectangle Bounds { get => _bounds; protected set => _bounds = value; } public List Zones { get => _zones; protected set => _zones = value; } public List Windows { get => _windows; protected set => _windows = value; } public string Name { get => _name; protected set => _name = value; } /// /// Creates a new Zone /// /// Image of the parent zone /// The position and size of the zone /// THe name of the zone (usefull for the JSON formatting) public Zone(Bitmap image, Rectangle bounds, string name) { Windows = new List(); Zones = new List(); Name = name; //You cant set the image in the CTOR because the processing is impossible at first initiation _image = image; Bounds = bounds; } /// /// Adds a zone to the list of zones /// /// The zone you want to add public virtual void AddZone(Zone zone) { Zones.Add(zone); } /// /// Add a window to the list of windows /// /// the window you want to add public virtual void AddWindow(Window window) { Windows.Add(window); } /// /// Calls all the windows to do OCR and to give back the results so we can send them to the model /// /// A list of all the driver in the race to help with text recognition /// A driver data object that contains all the infos about a driver public virtual DriverData Decode(List driverList) { int sectorCount = 0; DriverData result = new DriverData(); foreach(Window w in Windows) { // A switch would be prettier but I dont think its supported in this C# version if (w is DriverNameWindow) result.Name = (string)(w as DriverNameWindow).DecodePng(driverList); if (w is DriverDrsWindow) result.DRS = (bool)(w as DriverDrsWindow).DecodePng(); if (w is DriverGapToLeaderWindow) result.GapToLeader = (int)(w as DriverGapToLeaderWindow).DecodePng(); if (w is DriverLapTimeWindow) result.LapTime = (int)(w as DriverLapTimeWindow).DecodePng(); if (w is DriverPositionWindow) result.Position = (int)(w as DriverPositionWindow).DecodePng(); if (w is DriverSectorWindow) { sectorCount++; if (sectorCount == 1) result.Sector1 = (int)(w as DriverSectorWindow).DecodePng(); if (sectorCount == 2) result.Sector2 = (int)(w as DriverSectorWindow).DecodePng(); if (sectorCount == 3) result.Sector3 = (int)(w as DriverSectorWindow).DecodePng(); } if (w is DriverTyresWindow) result.CurrentTyre = (Tyre)(w as DriverTyresWindow).DecodePng(); } return result; } public virtual Bitmap Draw() { Bitmap img; //If its the main zone we want to see everything if (Zones.Count > 0) { img = Image; } else { img = ZoneImage; } Graphics g = Graphics.FromImage(img); //If its the main zone we need to visualize the Zone bounds displayed if (Zones.Count > 0) g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(249,194,46)), 5), Bounds); foreach (Zone z in Zones) { Rectangle newBounds = new Rectangle(z.Bounds.X, z.Bounds.Y + Bounds.Y, z.Bounds.Width, z.Bounds.Height); g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(249, 194, 46)), 5), newBounds); } foreach (Window w in Windows) { g.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(252, 252, 252)), 5), w.Bounds); } return img; } public void ResetZones() { Zones.Clear(); } public void ResetWindows() { foreach (Zone z in Zones) { z.ResetWindows(); } Windows.Clear(); } /// /// Checks if the given Rectangle fits in the current zone /// /// The Rectangle you want to check the fittment /// protected bool Fits(Rectangle inputRectangle) { if (inputRectangle.X + inputRectangle.Width > Bounds.Width || inputRectangle.Y + inputRectangle.Height > Bounds.Height || inputRectangle.X < 0 || inputRectangle.Y < 0) { return false; } else { return true; } } } }