Example #1
0
        void ProcessRec(PuzzleItem ccitem)
        {
            foreach (var item in ccitem.Connected)
            {
                item.Ang = ccitem.Ang;
                var    relativex = (item.Position.X - ccitem.Position.X) * ww;
                var    relativey = (item.Position.Y - ccitem.Position.Y) * ww;
                Matrix mtr       = new Matrix();
                mtr.Rotate(ccitem.Ang);
                PointF[] pp = new PointF[] { new PointF(relativex, relativey),
                                             new PointF(item.CenterShift.X, item.CenterShift.Y),
                                             new PointF(item.TopLeftShift.X, item.TopLeftShift.Y),
                                             new PointF(ccitem.CenterShift.X, ccitem.CenterShift.Y),
                                             new PointF(ccitem.TopLeftShift.X, ccitem.TopLeftShift.Y), };
                mtr.TransformPoints(pp);
                var tlx1 = ccitem.X - pp[3].X - pp[4].X;
                var tly1 = ccitem.Y - pp[3].Y - pp[4].Y;

                var targetx1 = tlx1 + pp[0].X;
                var targety1 = tly1 + pp[0].Y;


                item.X = targetx1 + pp[1].X + pp[2].X;
                item.Y = targety1 + pp[1].Y + pp[2].Y;
                ProcessRec(item);
            }
        }
Example #2
0
 List <PuzzleItem> GetAllChilds(PuzzleItem p, List <PuzzleItem> list = null)
 {
     if (list == null)
     {
         list = new List <PuzzleItem>();
     }
     list.Add(p);
     foreach (var pp in p.Connected)
     {
         GetAllChilds(pp, list);
     }
     return(list);
 }
Example #3
0
 internal void Attach(PuzzleItem puzzleItem)
 {
     Connected.Add(puzzleItem);
     puzzleItem.Parent = this;
 }
Example #4
0
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            var pos = pictureBox1.PointToClient(Cursor.Position);

            if (e.Button == MouseButtons.Right)
            {
                drag2     = true;
                startposx = scrollx;
                startposy = scrolly;
                startdx   = pos.X;
                startdy   = pos.Y;
            }
            if (e.Button == MouseButtons.Left)
            {
                selected = null;
                if (hovered != null)
                {
                    Matrix mtr  = new Matrix();
                    var    item = hovered;
                    mtr.Translate(item.X, item.Y);
                    mtr.Translate(-item.Bmp.Width / 2, -item.Bmp.Height / 2);
                    mtr.Translate(item.Bmp.Width / 2, item.Bmp.Height / 2);
                    mtr.Rotate(item.Ang);
                    mtr.Translate(-item.Bmp.Width / 2, -item.Bmp.Height / 2);
                    mtr.Invert();
                    PointF[] pnts = new PointF[] { new PointF(pos.X - scrollx, pos.Y - scrolly) };

                    mtr.TransformPoints(pnts);
                    var pos0 = pnts[0];


                    var d = Math.Sqrt(Math.Pow(pos0.X - item.Bmp.Width / 2, 2) + Math.Pow(pos0.Y - item.Bmp.Height / 2, 2));

                    var temp = hovered;
                    selected = temp;
                    propertyGrid2.SelectedObject = selected;

                    while (temp.Parent != null)
                    {
                        temp = temp.Parent;
                    }
                    foreach (var item0 in Items)
                    {
                        item0.Z++;
                    }


                    dragItem = temp;

                    dragItem.Z = 0;
                    var l = GetAllChilds(dragItem);
                    foreach (var uitem in l)
                    {
                        uitem.Z = 0;
                    }

                    Items     = Items.OrderByDescending(z => z.Z).ToList();
                    startdx   = pos.X;
                    startdy   = pos.Y;
                    startposx = dragItem.X;
                    startposy = dragItem.Y;
                    if (d < item.Bmp.Width / 2)
                    {
                        drag = true;
                    }
                    else
                    {
                        startang = dragItem.Ang;
                        drag3    = true;
                    }
                }
            }
        }
Example #5
0
        private void Timer1_Tick(object sender, EventArgs e)
        {
            var pos1 = pictureBox1.PointToClient(Cursor.Position);
            var pos  = new PointF(pos1.X, pos1.Y);

            /*pos.X -= scrollx;
            *  pos.Y -= scrolly;*/

            if (drag)
            {
                var dx = pos.X - startdx;
                var dy = pos.Y - startdy;
                dragItem.X = startposx + dx;
                dragItem.Y = startposy + dy;
                RecalcTree();
            }
            if (drag3)
            {
                //get sub of angs
                //var dx =( pos.X-scrollx) - startdx;
                //var dy = (pos.Y-scrolly) - startdy;

                var tx1  = (startdx - scrollx) - startposx;
                var ty1  = (startdy - scrolly) - startposy;
                var len1 = (float)Math.Sqrt(Math.Pow(tx1, 2) + Math.Pow(ty1, 2));
                tx1 /= len1;
                ty1 /= len1;
                var atan1 = Math.Atan2(ty1, tx1);

                var tx2  = (pos.X - scrollx) - startposx;
                var ty2  = (pos.Y - scrolly) - startposy;
                var len2 = (float)Math.Sqrt(Math.Pow(tx2, 2) + Math.Pow(ty2, 2));
                tx2 /= len2;
                ty2 /= len2;
                var atan2 = Math.Atan2(ty2, tx2);

                var diff = MinRotAng((float)(atan2 * 180f / Math.PI), (float)(atan1 * 180f / Math.PI));


                //var da = dx;
                dragItem.Ang  = startang + diff;
                dragItem.Ang %= 360;
                if (dragItem.Ang < 0)
                {
                    dragItem.Ang += 360;
                }
                RecalcTree();
            }
            if (drag2)
            {
                var dx = pos.X - startdx;
                var dy = pos.Y - startdy;
                scrollx = startposx + dx;
                scrolly = startposy + dy;
            }
            hovered      = null;
            rotateCursor = false;
            foreach (var item in Items)
            {
                Matrix mtr = new Matrix();
                mtr.Translate(item.X, item.Y);
                mtr.Translate(-item.Bmp.Width / 2, -item.Bmp.Height / 2);
                mtr.Translate(item.Bmp.Width / 2, item.Bmp.Height / 2);
                mtr.Rotate(item.Ang);
                mtr.Translate(-item.Bmp.Width / 2, -item.Bmp.Height / 2);
                mtr.Invert();
                PointF[] pnts = new PointF[] { new PointF(pos.X - scrollx, pos.Y - scrolly) };

                mtr.TransformPoints(pnts);
                var pos0 = pnts[0];


                if (pos0.X > 0 && pos0.Y > 0 && pos0.X < item.Bmp.Width && pos0.Y < item.Bmp.Height)
                {
                    hovered = item;
                    var d = Math.Sqrt(Math.Pow(pos0.X - item.Bmp.Width / 2, 2) + Math.Pow(pos0.Y - item.Bmp.Height / 2, 2));
                    if (d >= item.Bmp.Width / 2)
                    {
                        rotateCursor = true;
                    }
                }
            }

            if (loaded)
            {
                for (int zi = 0; zi < array.GetLength(0); zi++)
                {
                    for (int zj = 0; zj < array.GetLength(1); zj++)
                    {
                        var item = array[zi, zj];
                        for (int i = 0; i <= 1; i++)
                        {
                            for (int j = 0; j <= 1; j++)
                            {
                                var x1 = zi + i;
                                var y1 = zj + j;
                                if ((Math.Abs(i) + Math.Abs(j)) != 1)
                                {
                                    continue;
                                }
                                if (x1 < 0 || y1 < 0 || x1 >= array.GetLength(0) || y1 >= array.GetLength(1))
                                {
                                    continue;
                                }

                                var dang = MinRotAng(array[x1, y1].Ang, item.Ang);
                                var d    = Math.Sqrt(Math.Pow(array[x1, y1].X - item.X, 2) + Math.Pow(array[x1, y1].Y - item.Y, 2));
                                if (Math.Abs(dang) < 10 && d < 1.5 * ww)
                                {
                                    //get connected component tree from any
                                    PuzzleItem top1 = item;
                                    while (top1.Parent != null)
                                    {
                                        top1 = top1.Parent;
                                    }
                                    PuzzleItem top2 = array[x1, y1];
                                    while (top2.Parent != null)
                                    {
                                        top2 = top2.Parent;
                                    }
                                    //check if intersected
                                    if (top1 != top2)
                                    {
                                        top1.Attach(top2);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            gr.Clear(Color.Black);
            gr.TranslateTransform(scrollx, scrolly);
            gr.ScaleTransform(zoom, zoom);

            foreach (var item in Items)
            {
                var tr = gr.Transform;

                gr.TranslateTransform(item.X, item.Y);
                gr.TranslateTransform(-item.Bmp.Width / 2, -item.Bmp.Height / 2);
                gr.TranslateTransform(item.Bmp.Width / 2, item.Bmp.Height / 2);
                gr.RotateTransform(item.Ang);
                gr.TranslateTransform(-item.Bmp.Width / 2, -item.Bmp.Height / 2);

                gr.DrawImage(item.Bmp, 0, 0);
                //gr.DrawRectangle(new Pen(Color.Violet, 2), -item.TopLeftShift.X, -item.TopLeftShift.Y, 5, 5);
                //gr.DrawRectangle(new Pen(Color.Red, 2), item.CenterShift.X - 2, item.CenterShift.Y - 2, 4, 4);
                if (item == hovered)
                {
                    gr.DrawRectangle(new Pen(Color.Yellow, 2), 0, 0, item.Bmp.Width, item.Bmp.Height);
                }
                else
                {
                    if (selected == item)
                    {
                        //gr.DrawRectangle(new Pen(Color.Blue, 2), 0, 0, item.Bmp.Width, item.Bmp.Height);
                    }
                    else
                    {
                        //gr.DrawRectangle(Pens.Red, 0, 0, item.Bmp.Width, item.Bmp.Height);
                    }
                }

                gr.Transform = tr;
            }
            gr.ResetTransform();
            if (drag3 || (rotateCursor && hovered != null))
            {
                gr.DrawString("rotate", new Font("Arial", 8), Brushes.White, pos.X + 10, pos.Y);
                //gr.FillEllipse(Brushes.LightBlue, pos.X, pos.Y, 20, 20);
            }
            gr.ResetTransform();

            gr.DrawString((pos.X - scrollx) + "; " + (pos.Y - scrolly), new Font("Arial", 10), Brushes.White, 5, 5);
            pictureBox1.Image = bmp;
        }