Change HitTest to public

Oystein Bjorke hace 10 años 0
This discussion was imported from CodePlex

sharethl wrote at 2014-03-28 00:00:

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.
    }
}

objo wrote at 2014-03-28 15:36:

I agree on this. Done!
Note: I renamed the virtual method to HitTestOverride (following naming pattern from FrameworkElement.ArrangeOverride at MSDN)

sharethl wrote at 2014-03-28 16:18:

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);
            }
        }