Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make channel selection smoother for all channel configuration dialogs #373

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 66 additions & 39 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 All @@ -1270,13 +1292,16 @@ private void ToggleSelectedContact(ContactTag tag)
SetSelectedContact(tag, !GetContactStatus(tag));
}

private int GetContactIndex(ContactTag tag)
{
return tag.ProbeIndex == 0
? tag.ContactIndex
: tag.ContactIndex + ProbeGroup.Probes.Take(tag.ProbeIndex).Aggregate(0, (total, next) => total + next.NumberOfContacts);
}

private void SetSelectedContact(ContactTag contactTag, bool status)
{
var index = contactTag.ProbeIndex == 0
? contactTag.ContactIndex
: contactTag.ContactIndex + ProbeGroup.Probes
.Take(contactTag.ProbeIndex)
.Aggregate(0, (total, next) => total + next.NumberOfContacts);
var index = GetContactIndex(contactTag);

SetSelectedContact(index, status);
}
Expand All @@ -1303,7 +1328,9 @@ private bool GetContactStatus(ContactTag tag)
MessageBox.Show($"Error: Attempted to check status of an object that is not a contact.", "Invalid Object Selected");
}

return SelectedContacts[tag.ContactIndex];
var index = GetContactIndex(tag);

return SelectedContacts[index];
}

private static PointD TransformPixelsToCoordinates(Point pixels, GraphPane graphPane)
Expand Down