diff --git a/Locomotive/Locomotive/DrawningLocomotive.cs b/Locomotive/Locomotive/DrawningLocomotive.cs index 827d7d2..9df50a5 100644 --- a/Locomotive/Locomotive/DrawningLocomotive.cs +++ b/Locomotive/Locomotive/DrawningLocomotive.cs @@ -38,6 +38,21 @@ namespace Locomotive _locomotiveHeight = locomotiveHeight; } + public void SetBaseColor(Color color) + { + if (Locomotive is EntityWarmlyLocomotive) + { + var locomotive = Locomotive as EntityWarmlyLocomotive; + if (locomotive is not null) + { + locomotive = new EntityWarmlyLocomotive(locomotive.Speed, locomotive.Weight, color, locomotive.ExtraColor, locomotive.Pipe, locomotive.FuelStorage); + Locomotive = locomotive; + return; + } + } + Locomotive = new EntityLocomotive(Locomotive.Speed, Locomotive.Weight, color); + } + /// Установка позиции локомотива public void SetPosition(int x, int y, int width, int height) { diff --git a/Locomotive/Locomotive/DrawningWarmlyLocomotive.cs b/Locomotive/Locomotive/DrawningWarmlyLocomotive.cs index dbb2b78..ca5c0d5 100644 --- a/Locomotive/Locomotive/DrawningWarmlyLocomotive.cs +++ b/Locomotive/Locomotive/DrawningWarmlyLocomotive.cs @@ -14,6 +14,16 @@ namespace Locomotive Locomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor, extraColor, pipe, storage); } + public void SetExtraColor(Color color) + { + var LocomotiveWarmly = Locomotive as EntityWarmlyLocomotive; + if (LocomotiveWarmly is not null) + { + LocomotiveWarmly = new EntityWarmlyLocomotive(LocomotiveWarmly.Speed, LocomotiveWarmly.Weight, LocomotiveWarmly.BodyColor, color, LocomotiveWarmly.Pipe, LocomotiveWarmly.FuelStorage); + Locomotive = LocomotiveWarmly; + } + } + public override void DrawTransport(Graphics g) { if (Locomotive is not EntityWarmlyLocomotive warmlyLocomotive) diff --git a/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs b/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs index 5072c7a..b17967a 100644 --- a/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs +++ b/Locomotive/Locomotive/FormLocomotiveConfig.Designer.cs @@ -267,6 +267,7 @@ this.labelDopColor.Text = "Extra Color"; this.labelDopColor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.labelDopColor.DragDrop += new System.Windows.Forms.DragEventHandler(this.labelDopColor_DragDrop); + this.labelDopColor.DragEnter += new System.Windows.Forms.DragEventHandler(this.labelDopColor_DragEnter); // // labelBaseColor // @@ -297,6 +298,7 @@ this.buttonAdd.TabIndex = 2; this.buttonAdd.Text = "Add"; this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); // // buttonCancel // diff --git a/Locomotive/Locomotive/FormLocomotiveConfig.cs b/Locomotive/Locomotive/FormLocomotiveConfig.cs index b5f52ec..98cebd5 100644 --- a/Locomotive/Locomotive/FormLocomotiveConfig.cs +++ b/Locomotive/Locomotive/FormLocomotiveConfig.cs @@ -14,6 +14,25 @@ namespace Locomotive { DrawningLocomotive _locomotive = null; + private event LocomotiveDelegate eventAddLocomotive; + + public void AddEvent(LocomotiveDelegate ev) + { + if (eventAddLocomotive == null) + { + eventAddLocomotive = new LocomotiveDelegate(ev); + } + else + { + eventAddLocomotive += ev; + } + } + private void buttonAdd_Click(object sender, EventArgs e) + { + eventAddLocomotive?.Invoke(_locomotive); + Close(); + } + public FormLocomotiveConfig() { InitializeComponent(); @@ -25,6 +44,9 @@ namespace Locomotive panelWhite.MouseDown += PanelColor_MouseDown; panelYellow.MouseDown += PanelColor_MouseDown; panelBlue.MouseDown += PanelColor_MouseDown; + + // ! - TODO buttonCancel.Click with lambda + buttonCancel.Click += (object sender, EventArgs e) => Close(); } private void labelObject_MouseDown(object sender, MouseEventArgs e) @@ -87,13 +109,38 @@ namespace Locomotive } private void labelBaseColor_DragDrop(object sender, DragEventArgs e) + { + // ! - TODO Call method from object _car and set color + + _locomotive.SetBaseColor((Color)e.Data.GetData(typeof(Color))); + DrawLocomotive(); + + } + private void labelDopColor_DragEnter(object sender, DragEventArgs e) { // ! - TODO + + if (e.Data.GetDataPresent(typeof(Color))) + { + e.Effect = DragDropEffects.Copy; + } + else + { + e.Effect = DragDropEffects.None; + } } private void labelDopColor_DragDrop(object sender, DragEventArgs e) { // ! - TODO + if (_locomotive is DrawningWarmlyLocomotive) + { + var locomotive = _locomotive as DrawningWarmlyLocomotive; + locomotive.SetExtraColor((Color)e.Data.GetData(typeof(Color))); + } + DrawLocomotive(); + } + } } diff --git a/Locomotive/Locomotive/FormMapWithSetLocomotives.cs b/Locomotive/Locomotive/FormMapWithSetLocomotives.cs index a4dcc05..b8b342b 100644 --- a/Locomotive/Locomotive/FormMapWithSetLocomotives.cs +++ b/Locomotive/Locomotive/FormMapWithSetLocomotives.cs @@ -87,7 +87,7 @@ namespace Locomotive /// Добавление объекта private void buttonAddLocomotive_Click(object sender, EventArgs e) { - if (listBoxMaps.SelectedIndex == -1) + /*if (listBoxMaps.SelectedIndex == -1) { return; } @@ -104,8 +104,34 @@ namespace Locomotive { MessageBox.Show("Failed to add object"); } - } + }*/ + + var formCarConfig = new FormLocomotiveConfig(); + // TODO Call method AddEvent from formCarConfig + formCarConfig.AddEvent(new (AddLocomotive)); + formCarConfig.Show(); } + + private void AddLocomotive(DrawningLocomotive locomotive) + { + if (listBoxMaps.SelectedIndex == -1) + { + return; + } + + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + new DrawningObjectLocomotive(locomotive) != -1) + { + MessageBox.Show("Object added"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + + else + { + MessageBox.Show("Failed to add object"); + } + + } + /// Удаление объекта private void buttonRemoveLocomotive_Click(object sender, EventArgs e) { diff --git a/Locomotive/Locomotive/LocomotiveDelegate.cs b/Locomotive/Locomotive/LocomotiveDelegate.cs new file mode 100644 index 0000000..813f2bc --- /dev/null +++ b/Locomotive/Locomotive/LocomotiveDelegate.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Locomotive +{ + public delegate void LocomotiveDelegate(DrawningLocomotive locomotive); +}