dinamic bounds

This commit is contained in:
LivelyPuer 2024-02-14 23:32:35 +04:00
parent 3db77b7282
commit e0c1010701
3 changed files with 34 additions and 12 deletions

View File

@ -49,6 +49,7 @@ public class DrawingElectroTrans
/// <param name="horns">Признак активности усов</param>
/// <param name="wheels">Признак колличества колес</param>
/// <param name="battery">Признак наличия бптпрей</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool horns, int wheels, bool battery)
{
@ -59,7 +60,6 @@ public class DrawingElectroTrans
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Установка границ поля
/// </summary>
@ -68,6 +68,12 @@ public class DrawingElectroTrans
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
if (width < 100 || height < 100)
{
_pictureWidth = Config.screenSize.Width;
_pictureHeight = Config.screenSize.Height;
return false;
}
_pictureWidth = width;
_pictureHeight = height;
return true;
@ -88,18 +94,18 @@ public class DrawingElectroTrans
if (x < 40)
{
x = 40;
}else if (x > Config.screenSize.Width - 40)
}else if (x > _pictureWidth - 40)
{
x = Config.screenSize.Width - 40;
x = _pictureWidth.Value - 40;
}
if (y < 40)
{
y = 40;
}
else if (y > Config.screenSize.Height - 40)
else if (y > _pictureHeight - 40)
{
y = Config.screenSize.Height - 40;
y = _pictureHeight.Value - 40;
}
_startPosX = x;
@ -118,7 +124,7 @@ public class DrawingElectroTrans
{
return false;
}
if (_startPosX.Value < 40 || _startPosY.Value < 40 || _startPosX > Config.screenSize.Width - 40 || _startPosY > Config.screenSize.Height - 40)
if (_startPosX.Value < 40 || _startPosY.Value < 40 || _startPosX > _pictureWidth - 40 || _startPosY > _pictureHeight - 40)
{
return false;
}
@ -142,14 +148,14 @@ public class DrawingElectroTrans
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityElectroTrans.Step < Config.screenSize.Width - 40)
if (_startPosX.Value + EntityElectroTrans.Step < _pictureWidth - 40)
{
_startPosX += (int)EntityElectroTrans.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityElectroTrans.Step < Config.screenSize.Height - 40)
if (_startPosY.Value + EntityElectroTrans.Step < _pictureHeight - 40)
{
_startPosY += (int)EntityElectroTrans.Step;
}

View File

@ -53,7 +53,7 @@
buttonCreateSportCar.Name = "buttonCreateSportCar";
buttonCreateSportCar.Size = new Size(75, 23);
buttonCreateSportCar.TabIndex = 1;
buttonCreateSportCar.Text = "Создать";
buttonCreateSportCar.Text = pictureBoxSportCar.ClientSize.ToString();
buttonCreateSportCar.UseVisualStyleBackColor = true;
buttonCreateSportCar.Click += ButtonCreateElectroTrans_Click;
//

View File

@ -5,11 +5,12 @@ namespace Lab1
/// <summary>
/// Ïîëå-îáúåêò äëÿ ïðîðèñîâêè îáúåêòà
/// </summary>
private DrawingElectroTrans? _drawningElectroTrans;
private DrawingElectroTrans _drawningElectroTrans;
public Form1()
{
InitializeComponent();
_drawningElectroTrans = new DrawingElectroTrans();
}
/// <summary>
@ -30,15 +31,30 @@ namespace Lab1
private void ButtonCreateElectroTrans_Click(object sender, EventArgs e)
{
Random random = new();
_drawningElectroTrans = new DrawingElectroTrans();
_drawningElectroTrans.Init(random.Next(500, 1500), random.Next(1000, 3000),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
Convert.ToBoolean(random.Next(0, 2)), random.Next(3, 8), Convert.ToBoolean(random.Next(0, 2)));
_drawningElectroTrans.SetPictureSize(pictureBoxSportCar.Width, pictureBoxSportCar.Height);
SetSize();
_drawningElectroTrans.SetPosition(50, 50);
Resize += Form1_Resize;
Draw();
}
private void Form1_Resize(object? sender, EventArgs e)
{
SetSize();
}
private void SetSize()
{
if (!_drawningElectroTrans.SetPictureSize(ClientSize.Width, ClientSize.Height))
{
Size = Config.screenSize;
}
pictureBoxSportCar.Size = ClientSize;
buttonCreateSportCar.Text = ClientSize.ToString();
}
/// <summary>
/// Ïåðåìåùåíèå îáúåêòà ïî ôîðìå (íàæàòèå êíîïîê íàâèãàöèè)
/// </summary>