Mouse down between two pieces of a puzzle

前端 未结 3 815
时光说笑
时光说笑 2021-01-28 18:30

I am building a puzzle game in winforms, and i want to recognize the mousedown over any piece, and move it with the mousemove. The issue is that when i touch the transparent par

3条回答
  •  伪装坚强ぢ
    2021-01-28 18:54

    Pieces can be represent as:

    public class Piece
    {
        public Point Location {get; set;}
        public int Z {get; set;}
        public int ID {get; set;} // to be bound to control or a control itself?
        public Image Image {get; set;} // texture?
        public DockStyle PlusArea {get; set;}
        public DockStyle MinusArea {get; set;}  // can be None
        ...
    
        public bool HitTest(Point point)
        {
            // assuming all of same size
            if((new Rectangle(Location, new Size(...)).Contains(point))
            {
                switch(MinusArea)
                {
                    case Top:
                        if((new Rectangle(...)).Contains(point))
                            return false;
                    ...
                }
            }
            switch(MinusArea)
            {
                case Top:
                    if((new Rectangle(...)).Contains(point))
                        return true;
                ...
            }
            return false;
        }
    

    Then puzzle is

    public class Puzzle
    {
        public List Pieces {get; set;}
    
        public void Draw(Graphics graphics)
        {
            // draw all pictures with respect to z order
        }
    
        public Piece HitTest(Point point)
        {
            ... // hittest all pieces, return highest z-order or null
        }
    }
    

    It is not a complete solution, but should give you idea.

    Basically:

    • In mouse event you call Figure.HitTest() to get figure to start moving (that's what you need).
    • You draw everything into owner drawn control by calling Figure.Draw().
    • Obviously, drag-n-drop operations are calling Invalidate().
    • You may have special flag to indicate figure being dragged and draw it differently (with shadows, a bit offsetted to simulate it's pulled over other pieces, etc).
    • Each figure is represented as rectangle and PlusArea or MinusArea (don't know how to call them better, it's this extra or missing area of piece connectors), this is simplification, you can improve it.

提交回复
热议问题