Изменение 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;
/// <summary>
///
/// класс, отвечающий за лево/право/верх/низ
/// </summary>
public enum DirectionType
{
@ -24,4 +24,4 @@ public enum DirectionType
/// </summary>
Right = 4
}
}

View File

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

View File

@ -38,9 +38,6 @@ public class EntityBattleship
/// </summary>
public double Step => Speed * 100 / Weight;
/// <summary>
///
/// </summary>
/// <param name="speed"></param>
/// <param name="weight"></param>
/// <param name="bodyColor"></param>
@ -58,6 +55,4 @@ public class EntityBattleship
Compartment = compartment;
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>
/// 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))
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
components.Dispose();
}
#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;
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;
}

View File

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