Change HitTest to public

Mar 28, 2014 at 12:00 AM
Edited Mar 28, 2014 at 1:11 AM
Hi, Objo, Can you change HitTest in UIPlotElement from protected to public?

This way will made it possible to add mouse tool. like: annotation delete tool, re-size tool, etc..
see DeleteAnnotationMouseTool's OnMouseDown event.

What do you think?

// attach even in chrome, which extends plotmodel.
this.MouseDown += OnMouseDown;
this.MouseMove += OnMouseMove;
this.MouseUp += OnMouseUp;
public MouseTool MouseTool{get;set;} // moues tool property.

//-------------------------------------------
// inside the chrome. and this mouse tool can be any tool people create.
private void OnMouseDown(object sender, OxyMouseDownEventArgs e) {
            if (_mouseTool != null) {
                _mouseTool.OnMouseDown(this, e);
            }
        }

//--------------------------------------------
// example of delete mouse tool, click an annotation, remove it from annotations collection.

public class DeleteAnnotationMouseTool : MouseTool {
     public override void OnMouseDown(ChromeModel chrome, OxyMouseDownEventArgs e) {
        HitTestResult hitResult;
        ScreenPoint screenPoint = e.Position;
        
        foreach (Annotation a in chrome.Annotations) {
            // here needs HitTest. 
            hitResult = a.HitTest(screenPoint, HIT_TEST_TOLERANCE);
            if (hitResult != null) {
                // add to collection for later to delete.
                break;
            }
        }
        // delete annotation.
    }
}
Coordinator
Mar 28, 2014 at 3:36 PM
I agree on this. Done!
Note: I renamed the virtual method to HitTestOverride (following naming pattern from FrameworkElement.ArrangeOverride at MSDN)
Marked as answer by sharethl on 3/28/2014 at 8:43 AM
Mar 28, 2014 at 4:18 PM
Edited Mar 28, 2014 at 4:50 PM
I hope I could push the mouse tool to source code that could help people.
But will mouse tool duplicate Manipulator? or duplicate "HandleMouseDown" in "PlotModel".

The idea will be like the following code, start from the 3rd line.
Will this be ok and compatible?
if so, I am going to fork it out and submit with different built-in mouse tools.

PS: change HitTest to public will let people to create their own mouse tool.
Public MouseTool MouseTool{get;set;} // include this property in PlotModel.
 public void HandleMouseDown(object sender, OxyMouseDownEventArgs e)
        {
            // handle mouse tool first.
            // pass plotmodel instead of plotControl. so mouse tool could modify annotations collection or other plotmodel's properties.
            if (mouseTool!=null){
               mouseTool.OnMouseDown(this, e)); 
               if(e.handled) return;
            }
            
            // Revert the order to handle the top-level elements first
            foreach (var element in this.GetElements().Reverse())
            {
                var uiElement = element as UIPlotElement;
                if (uiElement == null)
                {
                    continue;
                }

                var result = uiElement.HitTest(e.Position, MouseHitTolerance);
                if (result != null)
                {
                    e.HitTestResult = result;
                    uiElement.OnMouseDown(sender, e);
                    if (e.Handled)
                    {
                        this.currentMouseEventElement = uiElement;
                    }
                }

                if (e.Handled)
                {
                    break;
                }
            }

            if (!e.Handled)
            {
                this.OnMouseDown(sender, e);
            }
        }