led区修改

This commit is contained in:
zqm
2026-03-25 16:37:52 +08:00
parent 72b96782d0
commit 3971b68011

View File

@@ -23,6 +23,7 @@ namespace Camera
private Rectangle _originalZone; private Rectangle _originalZone;
private bool _isEditingDetectionZone = false; private bool _isEditingDetectionZone = false;
private bool _isEditingLedZone = false; private bool _isEditingLedZone = false;
private bool _isDrawingLedMode = false;
private int _ledZoneState = 0; private int _ledZoneState = 0;
private Color _detectionZoneColor = Color.Black; private Color _detectionZoneColor = Color.Black;
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>(); private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
@@ -240,8 +241,8 @@ namespace Camera
if (_camera != null) if (_camera != null)
{ {
var ledZones = _camera.GetLedZones(); var allLedZones = _camera.GetLedZones();
foreach (var kvp in ledZones) foreach (var kvp in allLedZones)
{ {
int index = kvp.Key; int index = kvp.Key;
Rectangle ledZone = kvp.Value; Rectangle ledZone = kvp.Value;
@@ -317,7 +318,7 @@ namespace Camera
isEditing = true; isEditing = true;
editColor = _detectionZoneColor; editColor = _detectionZoneColor;
} }
else if (_isEditingLedZone && _currentLedIndex >= 0) else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{ {
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex); Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
currentEditZone = new Rectangle( currentEditZone = new Rectangle(
@@ -330,7 +331,7 @@ namespace Camera
editColor = _camera.GetLedZoneColor(_currentLedIndex); editColor = _camera.GetLedZoneColor(_currentLedIndex);
} }
if (_isDrawing && _isEditingLedZone && _startPoint != null && _currentMousePoint != null) if (_isDrawing && (_isEditingLedZone || _isDrawingLedMode) && _startPoint != null && _currentMousePoint != null)
{ {
using (Pen pen = new Pen(_detectionZoneColor, 2)) using (Pen pen = new Pen(_detectionZoneColor, 2))
{ {
@@ -354,7 +355,7 @@ namespace Camera
} }
} }
} }
else if (_isEditingLedZone && _currentLedIndex >= 0) else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{ {
using (Pen pen = new Pen(_detectionZoneColor, 2)) using (Pen pen = new Pen(_detectionZoneColor, 2))
{ {
@@ -362,8 +363,8 @@ namespace Camera
g.DrawRectangle(pen, scaledDetectionZone); g.DrawRectangle(pen, scaledDetectionZone);
} }
var ledZones = _camera.GetLedZones(); var allLedZones = _camera.GetLedZones();
foreach (var kvp in ledZones) foreach (var kvp in allLedZones)
{ {
int index = kvp.Key; int index = kvp.Key;
Rectangle ledZone = kvp.Value; Rectangle ledZone = kvp.Value;
@@ -416,8 +417,8 @@ namespace Camera
if (_camera != null) if (_camera != null)
{ {
var ledZones = _camera.GetLedZones(); var allLedZones = _camera.GetLedZones();
foreach (var kvp in ledZones) foreach (var kvp in allLedZones)
{ {
int index = kvp.Key; int index = kvp.Key;
Rectangle ledZone = kvp.Value; Rectangle ledZone = kvp.Value;
@@ -517,11 +518,11 @@ namespace Camera
(int)(detectionZone.Height * scaleY) (int)(detectionZone.Height * scaleY)
); );
var ledZones = _camera.GetLedZones(); var allLedZones = _camera.GetLedZones();
bool clickedOnLedZone = false; bool clickedOnLedZone = false;
int clickedLedIndex = -1; int clickedLedIndex = -1;
foreach (var kvp in ledZones) foreach (var kvp in allLedZones)
{ {
Rectangle led = kvp.Value; Rectangle led = kvp.Value;
Rectangle scaledLed = new Rectangle( Rectangle scaledLed = new Rectangle(
@@ -533,14 +534,14 @@ namespace Camera
if (scaledLed.Width > 0 && scaledLed.Height > 0 && scaledLed.Contains(controlPoint)) if (scaledLed.Width > 0 && scaledLed.Height > 0 && scaledLed.Contains(controlPoint))
{ {
if (!_isEditingDetectionZone && !_isEditingLedZone) if (!_isEditingDetectionZone && !_isEditingLedZone && !_isDrawingLedMode)
{ {
clickedOnLedZone = true; clickedOnLedZone = true;
clickedLedIndex = kvp.Key; clickedLedIndex = kvp.Key;
ledZone = led; ledZone = led;
break; break;
} }
else if (_isEditingLedZone) else if (_isEditingLedZone || _isDrawingLedMode)
{ {
clickedOnLedZone = true; clickedOnLedZone = true;
clickedLedIndex = kvp.Key; clickedLedIndex = kvp.Key;
@@ -553,10 +554,10 @@ namespace Camera
if (clickedOnLedZone) if (clickedOnLedZone)
{ {
_isEditingLedZone = true; _isEditingLedZone = true;
_isDrawingLedMode = false;
_currentLedIndex = clickedLedIndex; _currentLedIndex = clickedLedIndex;
_selectedZoneIndex = clickedLedIndex; _selectedZoneIndex = clickedLedIndex;
_ledZoneState = 1; // 选中状态 toolStripButton2.Checked = false;
toolStripButton2.Checked = true;
toolStripButton2.Visible = false; toolStripButton2.Visible = false;
toolStripButton1.Visible = false; toolStripButton1.Visible = false;
UpdateLedZoneButtonsVisibility(1); UpdateLedZoneButtonsVisibility(1);
@@ -565,12 +566,13 @@ namespace Camera
_startPoint = imagePoint; _startPoint = imagePoint;
_resizeStartPoint = controlPoint; _resizeStartPoint = controlPoint;
_originalZone = ledZone; _originalZone = ledZone;
_isDrawing = false;
picBoxCamera.Invalidate(); picBoxCamera.Invalidate();
return; return;
} }
Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0); Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0);
if (_isEditingLedZone && _currentLedIndex >= 0) if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{ {
Rectangle led = _camera.GetLedZone(_currentLedIndex); Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle( scaledLedZone = new Rectangle(
@@ -581,7 +583,7 @@ namespace Camera
); );
} }
if (_isEditingDetectionZone || _isEditingLedZone) if (_isEditingDetectionZone || _isEditingLedZone || _isDrawingLedMode)
{ {
Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone; Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
_hoveredHandle = GetHoveredHandle(editRect, controlPoint); _hoveredHandle = GetHoveredHandle(editRect, controlPoint);
@@ -599,13 +601,14 @@ namespace Camera
_resizeStartPoint = controlPoint; _resizeStartPoint = controlPoint;
_originalZone = _isEditingDetectionZone ? detectionZone : ledZone; _originalZone = _isEditingDetectionZone ? detectionZone : ledZone;
} }
else if (_isEditingLedZone) else if (_isDrawingLedMode)
{ {
_currentLedIndex = -1; _currentLedIndex = -1;
_selectedZoneIndex = -1; _selectedZoneIndex = -1;
_ledZoneState = 2; // 切换到绘制状态 _isDrawing = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
UpdateLedZoneButtonsVisibility(2); UpdateLedZoneButtonsVisibility(2);
picBoxCamera.Invalidate();
} }
} }
else else
@@ -672,7 +675,7 @@ namespace Camera
); );
} }
if (_isEditingDetectionZone || _isEditingLedZone) if (_isEditingDetectionZone || _isEditingLedZone || _isDrawingLedMode)
{ {
Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone; Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
int handle = GetHoveredHandle(editRect, e.Location); int handle = GetHoveredHandle(editRect, e.Location);
@@ -702,7 +705,7 @@ namespace Camera
{ {
_camera.SetDetectionZone(newZone); _camera.SetDetectionZone(newZone);
} }
else else if (_isEditingLedZone)
{ {
_camera.SetLedZone(_currentLedIndex, newZone); _camera.SetLedZone(_currentLedIndex, newZone);
} }
@@ -731,7 +734,7 @@ namespace Camera
{ {
_camera.SetDetectionZone(newZone); _camera.SetDetectionZone(newZone);
} }
else else if (_isEditingLedZone)
{ {
_camera.SetLedZone(_currentLedIndex, newZone); _camera.SetLedZone(_currentLedIndex, newZone);
} }
@@ -803,14 +806,15 @@ namespace Camera
picBoxCamera.Invalidate(); picBoxCamera.Invalidate();
return; return;
} }
if (_isEditingLedZone) if (_isDrawingLedMode || _isEditingLedZone)
{ {
_isDrawingLedMode = false;
_isEditingLedZone = false; _isEditingLedZone = false;
_isMoving = false; _isMoving = false;
_isResizing = false; _isResizing = false;
_currentLedIndex = -1; _currentLedIndex = -1;
_selectedZoneIndex = -1; _selectedZoneIndex = -1;
_ledZoneState = 0; // 就绪状态 _isDrawing = false;
toolStripButton2.Checked = false; toolStripButton2.Checked = false;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)"; toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true; toolStripButton1.Visible = true;
@@ -847,14 +851,14 @@ namespace Camera
if (width > 10 && height > 10) if (width > 10 && height > 10)
{ {
if (toolStripButton2.Checked) if (_isDrawingLedMode)
{ {
int newIndex = _camera.GetLedZoneCount() + 1; int newIndex = _camera.GetLedZoneCount() + 1;
_camera.AddLedZone(newIndex, new Rectangle(x, y, width, height), Color.Lime); _camera.AddLedZone(newIndex, new Rectangle(x, y, width, height), Color.Lime);
_selectedZoneIndex = newIndex; _selectedZoneIndex = newIndex;
_currentLedIndex = newIndex; _currentLedIndex = newIndex;
_isEditingLedZone = true; _isEditingLedZone = true;
_ledZoneState = 1; // 选中状态 _isDrawingLedMode = true;
UpdateLedZoneButtonsVisibility(1); UpdateLedZoneButtonsVisibility(1);
UpdateColorButtonIcon(); UpdateColorButtonIcon();
} }
@@ -1007,11 +1011,10 @@ namespace Camera
{ {
if (toolStripButton2.Checked) if (toolStripButton2.Checked)
{ {
_isEditingLedZone = true; _isDrawingLedMode = true;
_isEditingDetectionZone = false; _isEditingDetectionZone = false;
_selectedZoneIndex = -1; _selectedZoneIndex = -1;
_currentLedIndex = -1; _currentLedIndex = -1;
_ledZoneState = 2; // 绘制状态
toolStripButton2.ToolTipText = "绘制Led区(点击关闭)"; toolStripButton2.ToolTipText = "绘制Led区(点击关闭)";
toolStripButton1.Checked = false; toolStripButton1.Checked = false;
toolStripButton1.Visible = false; toolStripButton1.Visible = false;
@@ -1021,13 +1024,14 @@ namespace Camera
} }
else else
{ {
_isDrawingLedMode = false;
_isEditingLedZone = false; _isEditingLedZone = false;
_currentLedIndex = -1; _currentLedIndex = -1;
_selectedZoneIndex = -1; _selectedZoneIndex = -1;
_ledZoneState = 0; // 就绪状态
toolStripButton2.ToolTipText = "绘制Led区(点击开启)"; toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true; toolStripButton1.Visible = true;
UpdateLedZoneButtonsVisibility(0); UpdateLedZoneButtonsVisibility(0);
_isDrawing = false;
} }
picBoxCamera.Invalidate(); picBoxCamera.Invalidate();
UpdateDataGridView(); UpdateDataGridView();
@@ -1042,7 +1046,7 @@ namespace Camera
{ {
colorDialog.Color = _detectionZoneColor; colorDialog.Color = _detectionZoneColor;
} }
else if (_isEditingLedZone && _currentLedIndex >= 0) else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{ {
colorDialog.Color = _camera.GetLedZoneColor(_currentLedIndex); colorDialog.Color = _camera.GetLedZoneColor(_currentLedIndex);
} }
@@ -1053,7 +1057,7 @@ namespace Camera
{ {
_detectionZoneColor = colorDialog.Color; _detectionZoneColor = colorDialog.Color;
} }
else if (_isEditingLedZone && _currentLedIndex >= 0) else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{ {
_camera.SetLedZoneColor(_currentLedIndex, colorDialog.Color); _camera.SetLedZoneColor(_currentLedIndex, colorDialog.Color);
} }
@@ -1065,13 +1069,13 @@ namespace Camera
private void ToolStripButton4_Click(object sender, EventArgs e) private void ToolStripButton4_Click(object sender, EventArgs e)
{ {
if (_isEditingLedZone && _currentLedIndex >= 0) if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{ {
_camera.RemoveLedZone(_currentLedIndex); _camera.RemoveLedZone(_currentLedIndex);
_currentLedIndex = -1; _currentLedIndex = -1;
_selectedZoneIndex = -1; _selectedZoneIndex = -1;
_isEditingLedZone = false; _isEditingLedZone = false;
_ledZoneState = 0; // 就绪状态 _isDrawingLedMode = false;
toolStripButton2.Checked = false; toolStripButton2.Checked = false;
toolStripButton2.Visible = true; toolStripButton2.Visible = true;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)"; toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
@@ -1089,7 +1093,7 @@ namespace Camera
{ {
currentColor = _detectionZoneColor; currentColor = _detectionZoneColor;
} }
else if (_isEditingLedZone && _currentLedIndex >= 0) else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{ {
currentColor = _camera.GetLedZoneColor(_currentLedIndex); currentColor = _camera.GetLedZoneColor(_currentLedIndex);
} }
@@ -1111,6 +1115,7 @@ namespace Camera
private void UpdateLedZoneButtonsVisibility(int state) private void UpdateLedZoneButtonsVisibility(int state)
{ {
_ledZoneState = state;
switch (state) switch (state)
{ {
case 0: // 就绪状态 case 0: // 就绪状态