Skip to content

Commit

Permalink
Make channel selection smoother
Browse files Browse the repository at this point in the history
- If the selection rectangle is smaller than any given contact, try to select a single contact based on the mouse position instead
- Added support for `ContactShape.Rect`
  • Loading branch information
bparks13 committed Nov 12, 2024
1 parent ac1a5bc commit 18c2cbc
Showing 1 changed file with 55 additions and 33 deletions.
88 changes: 55 additions & 33 deletions OpenEphys.Onix1.Design/ChannelConfigurationDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ internal void SetEqualAspectRatio()
zedGraphChannels.GraphPane.YAxis.Scale.Max = maxY;
}

private float contactSize = 0.0f; // NB: Store the size of a contact (radius or width, depending on the shape). Assumes that all contacts are uniform.

internal void DrawContacts()
{
if (ProbeGroup == null)
Expand All @@ -571,45 +573,57 @@ internal void DrawContacts()
{
Contact contact = probe.GetContact(j);

BoxObj contactObj;

if (contact.Shape.Equals(ContactShape.Circle))
{
var size = contact.ShapeParams.Radius.Value * 2;

EllipseObj contactObj = new(contact.PosX - size / 2, contact.PosY + size / 2, size, size, SelectedContactBorder, DisabledContactFill)
if (contactSize == 0.0f) contactSize = contact.ShapeParams.Radius.Value;

contactObj = new EllipseObj(contact.PosX - size / 2, contact.PosY + size / 2, size, size, SelectedContactBorder, DisabledContactFill)
{
ZOrder = ZOrder.B_BehindLegend,
Tag = new ContactTag(probeNumber, contact.Index)
};

contactObj.Border.Width = borderWidth;
contactObj.Border.IsVisible = false;
contactObj.Location.AlignV = AlignV.Center;
contactObj.Location.AlignH = AlignH.Center;

zedGraphChannels.GraphPane.GraphObjList.Add(contactObj);
}
else if (contact.Shape.Equals(ContactShape.Square))
{
var size = contact.ShapeParams.Width.Value;

BoxObj contactObj = new(contact.PosX - size / 2, contact.PosY + size / 2, size, size, SelectedContactBorder, DisabledContactFill)
if (contactSize == 0.0f) contactSize = size / 2;

contactObj = new BoxObj(contact.PosX - size / 2, contact.PosY + size / 2, size, size, SelectedContactBorder, DisabledContactFill)
{
ZOrder = ZOrder.B_BehindLegend,
Tag = new ContactTag(probeNumber, contact.Index)
};
}
else if (contact.Shape.Equals(ContactShape.Rect))
{
var width = contact.ShapeParams.Width.Value;
var height = contact.ShapeParams.Height.Value;

contactObj.Border.Width = borderWidth;
contactObj.Border.IsVisible = false;
contactObj.Location.AlignV = AlignV.Bottom;
contactObj.Location.AlignH = AlignH.Left;
if (contactSize == 0.0f) contactSize = width >= height ? width / 2 : height / 2;

zedGraphChannels.GraphPane.GraphObjList.Add(contactObj);
contactObj = new BoxObj(contact.PosX - width / 2, contact.PosY + height / 2, width, height, SelectedContactBorder, DisabledContactFill)
{
ZOrder = ZOrder.B_BehindLegend,
Tag = new ContactTag(probeNumber, contact.Index)
};
}
else
{
MessageBox.Show("Contact shapes other than 'circle' and 'square' not implemented yet.");
MessageBox.Show("Invalid ContactShape value. Check the contact shape parameter.");
return;
}

contactObj.Border.Width = borderWidth;
contactObj.Border.IsVisible = false;
contactObj.Location.AlignV = AlignV.Center;
contactObj.Location.AlignH = AlignH.Center;

zedGraphChannels.GraphPane.GraphObjList.Add(contactObj);
}
}
}
Expand Down Expand Up @@ -1197,9 +1211,29 @@ private bool MouseMoveEvent(ZedGraphControl sender, MouseEventArgs e)
return false;
}

private void FindNearestContactToMouseClick(PointF mouseClick)
{
if (zedGraphChannels.GraphPane.FindNearestObject(mouseClick, CreateGraphics(), out object nearestObject, out int _))
{
if (nearestObject is TextObj textObj)
{
ToggleSelectedContact(textObj.Tag as ContactTag);
}
else if (nearestObject is BoxObj boxObj)
{
ToggleSelectedContact(boxObj.Tag as ContactTag);
}
}
else
{
SetAllSelections(false);
}
}

private bool MouseUpEvent(ZedGraphControl sender, MouseEventArgs e)
{
sender.Cursor = Cursors.Arrow;

if (e.Button == MouseButtons.Left)
{
if (sender.GraphPane.GraphObjList[SelectionAreaTag] is BoxObj selectionArea && selectionArea != null && ProbeGroup != null)
Expand All @@ -1208,7 +1242,7 @@ private bool MouseUpEvent(ZedGraphControl sender, MouseEventArgs e)

sender.GraphPane.GraphObjList.Remove(selectionArea);

if (!rect.IsEmpty)
if (!rect.IsEmpty && (rect.Width > contactSize || rect.Height > contactSize))
{
var selectedContacts = sender.GraphPane.GraphObjList.OfType<BoxObj>()
.Where(c =>
Expand All @@ -1227,29 +1261,17 @@ private bool MouseUpEvent(ZedGraphControl sender, MouseEventArgs e)
SetSelectedContact((ContactTag)contact.Tag, true);
}
}
else
{
FindNearestContactToMouseClick(new PointF(e.X, e.Y));
}

clickStart.X = default;
clickStart.Y = default;
}
else
{
PointF mouseClick = new(e.X, e.Y);

if (zedGraphChannels.GraphPane.FindNearestObject(mouseClick, CreateGraphics(), out object nearestObject, out int _))
{
if (nearestObject is TextObj textObj)
{
ToggleSelectedContact(textObj.Tag as ContactTag);
}
else if (nearestObject is BoxObj boxObj)
{
ToggleSelectedContact(boxObj.Tag as ContactTag);
}
}
else
{
SetAllSelections(false);
}
FindNearestContactToMouseClick(new PointF(e.X, e.Y));
}

HighlightSelectedContacts();
Expand Down

0 comments on commit 18c2cbc

Please sign in to comment.