From 1066642b8dade5953c1514f814abe6c22985b873 Mon Sep 17 00:00:00 2001 From: frog24 Date: Sat, 18 Nov 2023 02:35:37 +0400 Subject: [PATCH] Lab5 WarmlyShip Barsukov --- .../ProjectWarmlyShip/EntityShip.cs | 2 +- .../ProjectWarmlyShip/EntityWarmlyShip.cs | 2 +- .../ProjectWarmlyShip/FormShipCollection.cs | 41 +- .../FormShipConfig.Designer.cs | 360 ++++++++++++++++++ .../ProjectWarmlyShip/FormShipConfig.cs | 131 +++++++ .../ProjectWarmlyShip/FormShipConfig.resx | 60 +++ 6 files changed, 583 insertions(+), 13 deletions(-) create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.Designer.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.cs create mode 100644 ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.resx diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/EntityShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/EntityShip.cs index b55c0db..1b2dc53 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/EntityShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/EntityShip.cs @@ -10,7 +10,7 @@ namespace ProjectWarmlyShip.Entities { public int Speed { get; private set; } public double Weight { get; private set; } - public Color MainColor { get; private set; } + public Color MainColor { get; set; } public double Step => (double)Speed * 100 / Weight; public EntityShip(int speed, double weight, Color mainColor) { diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs b/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs index 4ff179b..48af336 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/EntityWarmlyShip.cs @@ -8,7 +8,7 @@ namespace ProjectWarmlyShip.Entities { public class EntityWarmlyShip : EntityShip { - public Color OptionalColor { get; private set; } + public Color OptionalColor { get; set; } public bool Pipes { get; private set; } public bool FuelCompartment { get; private set; } public EntityWarmlyShip(int speed, double weight, diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs index 22bb76f..2a03159 100644 --- a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipCollection.cs @@ -18,6 +18,14 @@ namespace ProjectWarmlyShip { public partial class FormShipCollection : Form { + public int GetWidth() + { + return pictureBoxCollection.Width; + } + public int GetHeight() + { + return pictureBoxCollection.Height; + } private readonly ShipsGenericStorage _storage; public FormShipCollection() { @@ -83,18 +91,29 @@ namespace ProjectWarmlyShip { return; } - WarmlyShipForm form = new(); - if (form.ShowDialog() == DialogResult.OK) + var formShipConfig = new FormShipConfig(); + formShipConfig.Show(); + formShipConfig.AddEvent(AddShip); + } + private void AddShip(DrawingShip ship) + { + if (listBoxStorage.SelectedIndex == -1) { - if (obj + form.SelectedShip) - { - MessageBox.Show("Object Inserted"); - pictureBoxCollection.Image = obj.ShowShips(); - } - else - { - MessageBox.Show("Object Not Inserted"); - } + return; + } + var obj = _storage[listBoxStorage.SelectedItem.ToString() ?? string.Empty]; + if (obj == null) + { + return; + } + if (obj + ship) + { + MessageBox.Show("Object Inserted"); + pictureBoxCollection.Image = obj.ShowShips(); + } + else + { + MessageBox.Show("Object Not Inserted"); } } private void ButtonRemoveShip_Click(Object sender, EventArgs e) diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.Designer.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.Designer.cs new file mode 100644 index 0000000..db3a3a8 --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.Designer.cs @@ -0,0 +1,360 @@ +namespace ProjectWarmlyShip +{ + partial class FormShipConfig + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + groupBoxParameters = new GroupBox(); + labelAdvancedObject = new Label(); + labelSimpleObject = new Label(); + groupBoxColors = new GroupBox(); + panelGray = new Panel(); + panelWhite = new Panel(); + panelPurple = new Panel(); + panelBlack = new Panel(); + panelYellow = new Panel(); + panelGreen = new Panel(); + panelRed = new Panel(); + panelBlue = new Panel(); + checkBoxFuelCompartment = new CheckBox(); + checkBoxPipes = new CheckBox(); + numericUpDownWeight = new NumericUpDown(); + numericUpDownSpeed = new NumericUpDown(); + labelWeight = new Label(); + labelSpeed = new Label(); + panelObject = new Panel(); + labelMainColor = new Label(); + labelOptionalColor = new Label(); + pictureBoxObject = new PictureBox(); + buttonAdd = new Button(); + buttonCancel = new Button(); + groupBoxParameters.SuspendLayout(); + groupBoxColors.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)numericUpDownWeight).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownSpeed).BeginInit(); + panelObject.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)pictureBoxObject).BeginInit(); + SuspendLayout(); + // + // groupBoxParameters + // + groupBoxParameters.Controls.Add(labelAdvancedObject); + groupBoxParameters.Controls.Add(labelSimpleObject); + groupBoxParameters.Controls.Add(groupBoxColors); + groupBoxParameters.Controls.Add(checkBoxFuelCompartment); + groupBoxParameters.Controls.Add(checkBoxPipes); + groupBoxParameters.Controls.Add(numericUpDownWeight); + groupBoxParameters.Controls.Add(numericUpDownSpeed); + groupBoxParameters.Controls.Add(labelWeight); + groupBoxParameters.Controls.Add(labelSpeed); + groupBoxParameters.Location = new Point(10, 10); + groupBoxParameters.Name = "groupBoxParameters"; + groupBoxParameters.Size = new Size(450, 220); + groupBoxParameters.TabIndex = 7; + groupBoxParameters.TabStop = false; + groupBoxParameters.Text = "Parameters"; + // + // labelAdvancedObject + // + labelAdvancedObject.BorderStyle = BorderStyle.FixedSingle; + labelAdvancedObject.Font = new Font("Segoe UI", 15.75F, FontStyle.Regular, GraphicsUnit.Point); + labelAdvancedObject.Location = new Point(320, 173); + labelAdvancedObject.Name = "labelAdvancedObject"; + labelAdvancedObject.Size = new Size(110, 35); + labelAdvancedObject.TabIndex = 8; + labelAdvancedObject.Text = "Advanced"; + labelAdvancedObject.TextAlign = ContentAlignment.MiddleCenter; + labelAdvancedObject.MouseDown += LabelObject_MouseDown; + // + // labelSimpleObject + // + labelSimpleObject.BorderStyle = BorderStyle.FixedSingle; + labelSimpleObject.Font = new Font("Segoe UI", 15.75F, FontStyle.Regular, GraphicsUnit.Point); + labelSimpleObject.Location = new Point(200, 173); + labelSimpleObject.Name = "labelSimpleObject"; + labelSimpleObject.Size = new Size(110, 35); + labelSimpleObject.TabIndex = 7; + labelSimpleObject.Text = "Simple"; + labelSimpleObject.TextAlign = ContentAlignment.MiddleCenter; + labelSimpleObject.MouseDown += LabelObject_MouseDown; + // + // groupBoxColors + // + groupBoxColors.Controls.Add(panelGray); + groupBoxColors.Controls.Add(panelWhite); + groupBoxColors.Controls.Add(panelPurple); + groupBoxColors.Controls.Add(panelBlack); + groupBoxColors.Controls.Add(panelYellow); + groupBoxColors.Controls.Add(panelGreen); + groupBoxColors.Controls.Add(panelRed); + groupBoxColors.Controls.Add(panelBlue); + groupBoxColors.Location = new Point(200, 30); + groupBoxColors.Name = "groupBoxColors"; + groupBoxColors.Size = new Size(230, 140); + groupBoxColors.TabIndex = 6; + groupBoxColors.TabStop = false; + groupBoxColors.Text = "Colors"; + // + // panelGray + // + panelGray.BackColor = Color.Gray; + panelGray.Location = new Point(170, 80); + panelGray.Name = "panelGray"; + panelGray.Size = new Size(50, 50); + panelGray.TabIndex = 1; + // + // panelWhite + // + panelWhite.BackColor = Color.White; + panelWhite.Location = new Point(115, 80); + panelWhite.Name = "panelWhite"; + panelWhite.Size = new Size(50, 50); + panelWhite.TabIndex = 1; + // + // panelPurple + // + panelPurple.BackColor = Color.Purple; + panelPurple.Location = new Point(60, 80); + panelPurple.Name = "panelPurple"; + panelPurple.Size = new Size(50, 50); + panelPurple.TabIndex = 2; + // + // panelBlack + // + panelBlack.BackColor = Color.Black; + panelBlack.Location = new Point(5, 80); + panelBlack.Name = "panelBlack"; + panelBlack.Size = new Size(50, 50); + panelBlack.TabIndex = 1; + // + // panelYellow + // + panelYellow.BackColor = Color.Yellow; + panelYellow.Location = new Point(170, 20); + panelYellow.Name = "panelYellow"; + panelYellow.Size = new Size(50, 50); + panelYellow.TabIndex = 1; + // + // panelGreen + // + panelGreen.BackColor = Color.Green; + panelGreen.Location = new Point(115, 20); + panelGreen.Name = "panelGreen"; + panelGreen.Size = new Size(50, 50); + panelGreen.TabIndex = 1; + // + // panelRed + // + panelRed.BackColor = Color.Red; + panelRed.Location = new Point(60, 20); + panelRed.Name = "panelRed"; + panelRed.Size = new Size(50, 50); + panelRed.TabIndex = 1; + // + // panelBlue + // + panelBlue.BackColor = Color.Blue; + panelBlue.Location = new Point(5, 20); + panelBlue.Name = "panelBlue"; + panelBlue.Size = new Size(50, 50); + panelBlue.TabIndex = 0; + // + // checkBoxFuelCompartment + // + checkBoxFuelCompartment.AutoSize = true; + checkBoxFuelCompartment.Location = new Point(20, 145); + checkBoxFuelCompartment.Name = "checkBoxFuelCompartment"; + checkBoxFuelCompartment.Size = new Size(124, 19); + checkBoxFuelCompartment.TabIndex = 5; + checkBoxFuelCompartment.Text = "Fuel compartment"; + checkBoxFuelCompartment.UseVisualStyleBackColor = true; + // + // checkBoxPipes + // + checkBoxPipes.AutoSize = true; + checkBoxPipes.Location = new Point(20, 120); + checkBoxPipes.Name = "checkBoxPipes"; + checkBoxPipes.Size = new Size(54, 19); + checkBoxPipes.TabIndex = 4; + checkBoxPipes.Text = "Pipes"; + checkBoxPipes.UseVisualStyleBackColor = true; + // + // numericUpDownWeight + // + numericUpDownWeight.Location = new Point(75, 80); + numericUpDownWeight.Maximum = new decimal(new int[] { 1000, 0, 0, 0 }); + numericUpDownWeight.Minimum = new decimal(new int[] { 100, 0, 0, 0 }); + numericUpDownWeight.Name = "numericUpDownWeight"; + numericUpDownWeight.Size = new Size(120, 23); + numericUpDownWeight.TabIndex = 3; + numericUpDownWeight.Value = new decimal(new int[] { 100, 0, 0, 0 }); + // + // numericUpDownSpeed + // + numericUpDownSpeed.Location = new Point(75, 40); + numericUpDownSpeed.Maximum = new decimal(new int[] { 1000, 0, 0, 0 }); + numericUpDownSpeed.Minimum = new decimal(new int[] { 100, 0, 0, 0 }); + numericUpDownSpeed.Name = "numericUpDownSpeed"; + numericUpDownSpeed.Size = new Size(120, 23); + numericUpDownSpeed.TabIndex = 2; + numericUpDownSpeed.Value = new decimal(new int[] { 100, 0, 0, 0 }); + // + // labelWeight + // + labelWeight.AutoSize = true; + labelWeight.Location = new Point(20, 85); + labelWeight.Name = "labelWeight"; + labelWeight.Size = new Size(48, 15); + labelWeight.TabIndex = 1; + labelWeight.Text = "Weight:"; + // + // labelSpeed + // + labelSpeed.AutoSize = true; + labelSpeed.Location = new Point(20, 45); + labelSpeed.Name = "labelSpeed"; + labelSpeed.Size = new Size(42, 15); + labelSpeed.TabIndex = 0; + labelSpeed.Text = "Speed:"; + // + // panelObject + // + panelObject.AllowDrop = true; + panelObject.Controls.Add(labelMainColor); + panelObject.Controls.Add(labelOptionalColor); + panelObject.Controls.Add(pictureBoxObject); + panelObject.Location = new Point(465, 20); + panelObject.Name = "panelObject"; + panelObject.Size = new Size(350, 180); + panelObject.TabIndex = 8; + panelObject.DragDrop += panelObject_DragDrop; + panelObject.DragEnter += PanelObject_DragEnter; + // + // labelMainColor + // + labelMainColor.AllowDrop = true; + labelMainColor.BorderStyle = BorderStyle.FixedSingle; + labelMainColor.Font = new Font("Segoe UI", 15.75F, FontStyle.Regular, GraphicsUnit.Point); + labelMainColor.Location = new Point(5, 0); + labelMainColor.Name = "labelMainColor"; + labelMainColor.Size = new Size(165, 50); + labelMainColor.TabIndex = 10; + labelMainColor.Text = "Main Color"; + labelMainColor.TextAlign = ContentAlignment.MiddleCenter; + labelMainColor.DragDrop += LabelMainColor_DragDrop; + labelMainColor.DragEnter += LabelColor_DragEnter; + // + // labelOptionalColor + // + labelOptionalColor.AllowDrop = true; + labelOptionalColor.BorderStyle = BorderStyle.FixedSingle; + labelOptionalColor.Font = new Font("Segoe UI", 15.75F, FontStyle.Regular, GraphicsUnit.Point); + labelOptionalColor.Location = new Point(180, 0); + labelOptionalColor.Name = "labelOptionalColor"; + labelOptionalColor.Size = new Size(165, 50); + labelOptionalColor.TabIndex = 9; + labelOptionalColor.Text = "Optional Color"; + labelOptionalColor.TextAlign = ContentAlignment.MiddleCenter; + labelOptionalColor.DragDrop += LabelOptionalColor_DragDrop; + labelOptionalColor.DragEnter += LabelColor_DragEnter; + // + // pictureBoxObject + // + pictureBoxObject.Location = new Point(5, 55); + pictureBoxObject.Name = "pictureBoxObject"; + pictureBoxObject.Size = new Size(340, 120); + pictureBoxObject.TabIndex = 1; + pictureBoxObject.TabStop = false; + // + // buttonAdd + // + buttonAdd.Location = new Point(465, 205); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(170, 40); + buttonAdd.TabIndex = 9; + buttonAdd.Text = "Add"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(645, 205); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(170, 40); + buttonCancel.TabIndex = 10; + buttonCancel.Text = "Cancel"; + buttonCancel.UseVisualStyleBackColor = true; + // + // FormShipConfig + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(824, 251); + Controls.Add(buttonCancel); + Controls.Add(buttonAdd); + Controls.Add(panelObject); + Controls.Add(groupBoxParameters); + Name = "FormShipConfig"; + Text = "FormShipConfig"; + groupBoxParameters.ResumeLayout(false); + groupBoxParameters.PerformLayout(); + groupBoxColors.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)numericUpDownWeight).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDownSpeed).EndInit(); + panelObject.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)pictureBoxObject).EndInit(); + ResumeLayout(false); + } + + #endregion + + private GroupBox groupBoxParameters; + private Label labelAdvancedObject; + private Label labelSimpleObject; + private GroupBox groupBoxColors; + private Panel panelGray; + private Panel panelWhite; + private Panel panelPurple; + private Panel panelBlack; + private Panel panelYellow; + private Panel panelGreen; + private Panel panelRed; + private Panel panelBlue; + private CheckBox checkBoxFuelCompartment; + private CheckBox checkBoxPipes; + private NumericUpDown numericUpDownWeight; + private NumericUpDown numericUpDownSpeed; + private Label labelWeight; + private Label labelSpeed; + private Panel panelObject; + private Label labelMainColor; + private Label labelOptionalColor; + private PictureBox pictureBoxObject; + private Button buttonAdd; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.cs b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.cs new file mode 100644 index 0000000..541ec9e --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.cs @@ -0,0 +1,131 @@ +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; +using ProjectWarmlyShip.Entities; +using ProjectWarmlyShip.DrawingObjects; +using static System.Windows.Forms.DataFormats; + +namespace ProjectWarmlyShip +{ + public partial class FormShipConfig : Form + { + DrawingShip? _ship = null; + private event Action EventAddShip; + public FormShipConfig() + { + InitializeComponent(); + panelBlack.MouseDown += PanelColor_MouseDown; + panelPurple.MouseDown += PanelColor_MouseDown; + panelGray.MouseDown += PanelColor_MouseDown; + panelGreen.MouseDown += PanelColor_MouseDown; + panelRed.MouseDown += PanelColor_MouseDown; + panelWhite.MouseDown += PanelColor_MouseDown; + panelYellow.MouseDown += PanelColor_MouseDown; + panelBlue.MouseDown += PanelColor_MouseDown; + buttonCancel.Click += (object sender, EventArgs a) => Close(); + } + private void DrawShip() + { + Bitmap bmp = new(pictureBoxObject.Width, pictureBoxObject.Height); + Graphics gr = Graphics.FromImage(bmp); + _ship?.SetPosition(5, 5); + _ship?.DrawTrasport(gr); + pictureBoxObject.Image = bmp; + } + public void AddEvent(Action ev) + { + if (EventAddShip == null) + { + EventAddShip = ev; + } + else + { + EventAddShip += ev; + } + } + private void LabelObject_MouseDown(object sender, MouseEventArgs e) + { + (sender as Label)?.DoDragDrop((sender as Label)?.Name, DragDropEffects.Move | DragDropEffects.Copy); + } + private void PanelObject_DragEnter(object sender, DragEventArgs e) + { + if (e.Data?.GetDataPresent(DataFormats.Text) ?? false) + { + e.Effect = DragDropEffects.Copy; + } + else + { + e.Effect = DragDropEffects.None; + } + } + private void panelObject_DragDrop(object sender, DragEventArgs e) + { + + FormShipCollection form = new FormShipCollection(); + switch (e.Data?.GetData(DataFormats.Text).ToString()) + { + case "labelSimpleObject": + labelOptionalColor.AllowDrop = false; + + _ship = new DrawingShip((int)numericUpDownSpeed.Value, + (int)numericUpDownWeight.Value, Color.White, + form.GetWidth(), form.GetHeight()); + break; + case "labelAdvancedObject": + labelOptionalColor.AllowDrop = true; + + _ship = new DrawingWarmlyShip((int)numericUpDownSpeed.Value, + (int)numericUpDownWeight.Value, Color.White, Color.Black, + checkBoxPipes.Checked, checkBoxFuelCompartment.Checked, + form.GetWidth(), form.GetHeight()); + break; + } + DrawShip(); + } + private void PanelColor_MouseDown(object sender, MouseEventArgs e) + { + (sender as Panel)?.DoDragDrop((sender as Panel)?.BackColor, DragDropEffects.Move | DragDropEffects.Copy); + } + private void LabelColor_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(typeof(Color))) + { + e.Effect = DragDropEffects.Copy; + } + else + { + e.Effect = DragDropEffects.None; + } + } + private void LabelMainColor_DragDrop(object sender, DragEventArgs e) + { + var color = e.Data.GetData(typeof(Color)); + if (_ship != null && color != null) + { + _ship.EntityShip.MainColor = (Color)color; + DrawShip(); + } + } + private void LabelOptionalColor_DragDrop(object sender, DragEventArgs e) + { + var color = e.Data.GetData(typeof(Color)); + + if (_ship != null && color != null && _ship.EntityShip is EntityWarmlyShip entityWarmlyShip) + { + entityWarmlyShip.OptionalColor = (Color)color; + DrawShip(); + } + } + private void buttonAdd_Click(object sender, EventArgs e) + { + EventAddShip?.Invoke(_ship); + Close(); + } + } +} diff --git a/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.resx b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ProjectWarmlyShip/ProjectWarmlyShip/FormShipConfig.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file