Skip to content

IHittable#1845

Merged
swharden merged 12 commits intomainfrom
1844-IHittable
Jun 2, 2022
Merged

IHittable#1845
swharden merged 12 commits intomainfrom
1844-IHittable

Conversation

@swharden
Copy link
Member

This PR creates an IHittable interface that plottable objects can implement so when they are clicked the control's OnLeftClickedPlottable event is fired. resolves #1844

@swharden swharden mentioned this pull request May 16, 2022
@StendProg
Copy link
Contributor

StendProg commented May 17, 2022

Hi @swharden,

I looked at your work and want to leave a few comments.

  1. Do we really need to support a unique cursor for each Plottable on the MouseOver event? It's hard for me to imagine such scenarios. Everything would be greatly simplified if this cursor were set the same in the Control itself.
  2. IHittable may contain the ability to disable clicks by analogy with IDraggable.
public interface IHittable
{
  bool ClickEnabled {get; set};
  //or
  bool HitTestEnabled{get;set;}
...
}
  1. You will not need to save the state in the plottable if you also transfer the current Plot.Dims to HitTest(), which are already saved. The hit calculation will be done every time HitTest() is called and it needs to be fast to be able to bind it to OnMouseMove events.
public bool HitTest(Coords c, PlotDimensions dims);
  1. We can play a bit with linq to shorten the code:
public IPlottable GetHittable(double xPixel, double yPixel)
{
    return GetPlottables()
               .Where(x => x is IHittable)
               .Reverse())
               .FirstOrDefault( p => 
                {
                    double xCoords = GetCoordinateX((float)xPixel, p.XAxisIndex);
                    double yCoords = GetCoordinateY((float)yPixel, p.YAxisIndex);
                    Coordinate c = new(xCoords, yCoords);
                    
                    return  (IHittable)p.HitTest(c);         
                });
}

@swharden
Copy link
Member Author

swharden commented Jun 2, 2022

Hi @StendProg, thank you for your feedback! I really value your input. I found this problem to be surprisingly difficult, and it I put it down for a few days and am now coming back to it.

1. Do hittable plottables require unique cursors?

I think so. Here are some potential use cases.

imageimageimage

2. HitTestEnabled

public interface IHittable
{
  bool HitTestEnabled { get; set; }
...
}

Great suggestion! I will implement this.

3&4. Instead of storing state, can you pass dims into HitTest()?

The hit calculation will be done every time HitTest() is called and it needs to be fast to be able to bind it to OnMouseMove events.

Unfortunately the size of bounding box requires string measurement, and that is costly enough that I don't think it is worth performing on every mouse move event. Not finding a better solution this problem is why I put this PR down and haven't picked it up in two weeks 😅 It is much more difficult than I initially thought it would be. I can't think of a better way to achieve this, but after I merge this I welcome additional thoughts you have or suggestions for improvements here or in #1844.

@swharden swharden marked this pull request as ready for review June 2, 2022 00:29
@swharden swharden merged commit 3c9cb08 into main Jun 2, 2022
@swharden swharden deleted the 1844-IHittable branch June 2, 2022 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plottable: IHittable

2 participants