Изменение SetPictureSize и SetPosition

This commit is contained in:
victinass 2024-02-18 16:48:15 +04:00
parent 337cc0b7e4
commit e89bd91679
5 changed files with 171 additions and 159 deletions

View File

@ -1,6 +1,6 @@
namespace Battleship; namespace Battleship;
/// <summary> /// <summary>
/// /// класс, отвечающий за лево/право/верх/низ
/// </summary> /// </summary>
public enum DirectionType public enum DirectionType
{ {
@ -24,4 +24,4 @@ public enum DirectionType
/// </summary> /// </summary>
Right = 4 Right = 4
} }

View File

@ -1,10 +1,9 @@
namespace Battleship; namespace Battleship;
/// <summary> /// <summary>
/// Класс, отвечающий за отрисовку и перемещение объекта-сущности /// Класс, отвечающий за отрисовку и перемещение объекта-сущности
/// </summary> /// </summary>
public class DrawingBattleship public class DrawingBattleship
{ {
/// <summary> /// <summary>
/// Класс-сущность /// Класс-сущность
/// </summary> /// </summary>
@ -33,12 +32,12 @@ public class DrawingBattleship
/// <summary> /// <summary>
/// Ширина прорисовки линкора /// Ширина прорисовки линкора
/// </summary> /// </summary>
private readonly int _drawingBattleshipWidth = 120; private readonly int _drawingBattleshipWidth = 129;
/// <summary> /// <summary>
/// Высота прорисовки линкора /// Высота прорисовки линкора
/// </summary> /// </summary>
private readonly int _drawingBattleshipHeight = 80; private readonly int _drawingBattleshipHeight = 40;
/// <summary> /// <summary>
/// ///
@ -72,23 +71,30 @@ public class DrawingBattleship
{ {
//TODO проверка, что объект "влезает" в размеры поля //TODO проверка, что объект "влезает" в размеры поля
//если влезает, сохраняем границы и корректируем позицию объекта, если она уже была установлена //если влезает, сохраняем границы и корректируем позицию объекта, если она уже была установлена
_pictureWidth = width;
_pictureHeight = height; if (_drawingBattleshipWidth > width || _drawingBattleshipHeight > height)
if (_pictureWidth <= _drawingBattleshipWidth || _pictureHeight <= _drawingBattleshipHeight)
{ {
_pictureHeight = null;
_pictureWidth = null;
return false; return false;
} }
if (_startPosX + _drawingBattleshipWidth > _pictureWidth) _pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue || _startPosY.HasValue)
{ {
_startPosX = _pictureWidth.Value - _drawingBattleshipWidth;
} if (_startPosX + _drawingBattleshipWidth > _pictureWidth)
if (_startPosY + _drawingBattleshipHeight > _pictureHeight) {
{ _startPosX = _pictureWidth - _drawingBattleshipWidth;
_startPosY = _pictureHeight.Value - _drawingBattleshipHeight; }
else if (_startPosX < 0) _startPosX = 0;
if (_startPosY + _drawingBattleshipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingBattleshipHeight;
}
else if (_startPosY < 0) _startPosY = 0;
} }
return true; return true;
} }
/// <summary> /// <summary>
@ -99,16 +105,28 @@ public class DrawingBattleship
public void SetPosition(int x, int y, int width, int height) public void SetPosition(int x, int y, int width, int height)
{ {
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах // то надо изменить координаты, чтобы он оставался в этих границах
if (x >= 0 && x + _drawingBattleshipWidth <= width && y >= 0 && y + _drawingBattleshipWidth <= height)
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
if (x + _drawingBattleshipWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawingBattleshipWidth;
}
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y + _drawingBattleshipHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawingBattleshipHeight;
}
else if (y < 0) _startPosY = 0;
else _startPosY = y;
} }
/// <summary> /// <summary>

View File

@ -38,9 +38,6 @@ public class EntityBattleship
/// </summary> /// </summary>
public double Step => Speed * 100 / Weight; public double Step => Speed * 100 / Weight;
/// <summary>
///
/// </summary>
/// <param name="speed"></param> /// <param name="speed"></param>
/// <param name="weight"></param> /// <param name="weight"></param>
/// <param name="bodyColor"></param> /// <param name="bodyColor"></param>
@ -58,6 +55,4 @@ public class EntityBattleship
Compartment = compartment; Compartment = compartment;
Tower = tower; Tower = tower;
} }
} }

View File

@ -1,133 +1,132 @@
namespace Battleship namespace Battleship;
partial class FormBattleship
{ {
partial class FormBattleship /// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{ {
/// <summary> if (disposing && (components != null))
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{ {
if (disposing && (components != null)) components.Dispose();
{
components.Dispose();
}
base.Dispose(disposing);
} }
base.Dispose(disposing);
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
pictureBoxBattleship = new PictureBox();
buttonCreate = new Button();
buttonLeft = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonUp = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
SuspendLayout();
//
// pictureBoxBattleship
//
pictureBoxBattleship.Location = new Point(0, 0);
pictureBoxBattleship.Name = "pictureBoxBattleship";
pictureBoxBattleship.Size = new Size(957, 559);
pictureBoxBattleship.TabIndex = 0;
pictureBoxBattleship.TabStop = false;
//
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 518);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 0;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(819, 512);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 3;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click;
//
// buttonDown
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(860, 512);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 2;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(901, 512);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(860, 471);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 1;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click;
//
// FormBattleship
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(957, 559);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
Controls.Add(pictureBoxBattleship);
Name = "FormBattleship";
Text = "Линкор";
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).EndInit();
ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxBattleship;
private Button buttonCreate;
private Button buttonDown;
private Button buttonRight;
private Button buttonUp;
private Button buttonLeft;
} }
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
pictureBoxBattleship = new PictureBox();
buttonCreate = new Button();
buttonLeft = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonUp = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).BeginInit();
SuspendLayout();
//
// pictureBoxBattleship
//
pictureBoxBattleship.Location = new Point(0, 0);
pictureBoxBattleship.Name = "pictureBoxBattleship";
pictureBoxBattleship.Size = new Size(957, 559);
pictureBoxBattleship.TabIndex = 0;
pictureBoxBattleship.TabStop = false;
//
// buttonCreate
//
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
buttonCreate.Location = new Point(12, 518);
buttonCreate.Name = "buttonCreate";
buttonCreate.Size = new Size(94, 29);
buttonCreate.TabIndex = 0;
buttonCreate.Text = "Создать";
buttonCreate.UseVisualStyleBackColor = true;
buttonCreate.Click += buttonCreate_Click;
//
// buttonLeft
//
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
buttonLeft.Location = new Point(819, 512);
buttonLeft.Name = "buttonLeft";
buttonLeft.Size = new Size(35, 35);
buttonLeft.TabIndex = 3;
buttonLeft.UseVisualStyleBackColor = true;
buttonLeft.Click += buttonMove_Click;
//
// buttonDown
//
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
buttonDown.Location = new Point(860, 512);
buttonDown.Name = "buttonDown";
buttonDown.Size = new Size(35, 35);
buttonDown.TabIndex = 2;
buttonDown.UseVisualStyleBackColor = true;
buttonDown.Click += buttonMove_Click;
//
// buttonRight
//
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
buttonRight.Location = new Point(901, 512);
buttonRight.Name = "buttonRight";
buttonRight.Size = new Size(35, 35);
buttonRight.TabIndex = 4;
buttonRight.UseVisualStyleBackColor = true;
buttonRight.Click += buttonMove_Click;
//
// buttonUp
//
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
buttonUp.Location = new Point(860, 471);
buttonUp.Name = "buttonUp";
buttonUp.Size = new Size(35, 35);
buttonUp.TabIndex = 1;
buttonUp.UseVisualStyleBackColor = true;
buttonUp.Click += buttonMove_Click;
//
// FormBattleship
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(957, 559);
Controls.Add(buttonUp);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
Controls.Add(pictureBoxBattleship);
Name = "FormBattleship";
Text = "Линкор";
((System.ComponentModel.ISupportInitialize)pictureBoxBattleship).EndInit();
ResumeLayout(false);
}
#endregion
private PictureBox pictureBoxBattleship;
private Button buttonCreate;
private Button buttonDown;
private Button buttonRight;
private Button buttonUp;
private Button buttonLeft;
} }

View File

@ -5,7 +5,7 @@
public partial class FormBattleship : Form public partial class FormBattleship : Form
{ {
/// <summary> /// <summary>
/// Поле\объект для прорисовки объекта /// Поле/объект для прорисовки объекта
/// </summary> /// </summary>
private DrawingBattleship? _drawingBattleship; //поля с нижнего подчеркивания private DrawingBattleship? _drawingBattleship; //поля с нижнего подчеркивания
@ -76,4 +76,4 @@ public partial class FormBattleship : Form
Draw(); Draw();
} }
} }
} }