mardi 25 octobre 2016

How can I simpify architecture of application?

I created custom ComplexControl classe, which redefines handling of events:

public class MyHitResult
{
   public enum HTArea
   {
       EMPTY,
       FOO,
       FOOBAR,
       FOOBAR_BORDER,
       // ...
   }
   MyHitResult(Point p) { HTPoint = p; }
   Point HTPoint{get; set;} = new Point(0, 0)
   BaseView Item {get; set;} = null;
}
public class ComplexControl : UIElement
{
    protected override void OnRender(DrawingContext drawing_context)
    {
       m_root_view.draw(drawing_context);
    }
    protected override void OnMouseDown(MouseButtonEventArgs args)
    {
        MyHitResult ht_res = new MyHitResult(args.getPosition(this));
        m_root_view.hit_test(ht_res);
        switch (ht_res.Area)
        {
           case MyHitResult.HTArea.Foo:
              Foo item = ht_res.Item as Foo;
              // Do specific operations on foo
              // invoke some private methods of ComplexControl
           break;
           case MyHitResult.HTArea.FooBar:
              Foo item = ht_res.Item as FooBar;
              // Do specific operations on foobar
              // invoke some private methods of ComplexControl
           break;
           // ....
        }
    }
   // other similar stuff
   private CustomView m_root_view;
}

CustomView is root view, which can contain some nested objects like Foo, FooBar, etc. All such objects are derived from BaseView class:

 public class BaseView
 {
     public abstract void hit_test(MyHitResult ht);
     public abstract void draw(DrawingContext dc);
 }

I organized event handling in following way (let they are mouseDown, mouseMove, mouseUp)

  1. Performing hit test to define current view object
  2. Perform specific operations, based on enum, which defines custom view class

But maintaining of HTArea enum, which defines hit test zones seems too ugly for me, more over, such way makes difficult handling of events. Maybe there is more comfortable way to organize program?

P.S. Of course, I can create methods OnMouseDown, ... in BaseView class, but as I understand, they should have access to private methods of ComplexControl (because only ComplexControl has knowledge how to hanlde events)

Aucun commentaire:

Enregistrer un commentaire