private void pnlMainPlayBoard_DragDrop(object sender, DragEventArgs e)
        {
            if (currentUcGame.playersBoards[currentUcGame.turn].name != mainForm.userName) //@@
            {
                pnlMainPlayBoard.AllowDrop = false;                                        //@@
                MessageBox.Show("you can't play now-it is not your turn!");                //@@
                pnlMainPlayBoard.AllowDrop = true;                                         //@@
            }
            else//@@
            {
                bool flag = false;
                ccCard.dragedCard.Width  = width_card_in_board;
                ccCard.dragedCard.Height = height_card_in_board;
                ccCard.dragedCard.Font   = new System.Drawing.Font("OCR A Extended", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
                Point pnt = PointToClient(Cursor.Position);
                pnt.X -= pnlMainPlayBoard.Location.X;
                pnt.Y -= pnlMainPlayBoard.Location.Y;
                bool flgsmall = false;

                foreach (Panel pnl in pnlMainPlayBoard.Controls)
                {
                    //מציאת הפאנל אליו נגרר הקלף
                    if (pnt.Y > pnl.Location.Y && pnt.Y < (pnl.Location.Y + pnl.Height) && pnt.X > pnl.Location.X && pnt.X < (pnl.Location.X + pnl.Width))
                    {
                        //במקרה בו המספר שהוכנס אינו הגדול ביותר
                        for (int j = 0; j < pnl.Controls.Count && !flgsmall; j++)
                        {
                            if (ccCard.dragedCard.Number < ((ccCard)pnl.Controls[j]).Number && ((ccCard)pnl.Controls[j]).Number != 14)
                            {
                                ccCard tmp;
                                int    x = space_card_in_board, y = 2, i;
                                Panel  newpnl = new Panel();
                                newpnl.Location            = pnl.Location;
                                newpnl.Size                = new System.Drawing.Size(width_card_in_board + space_card_in_board, height_card_in_board + 4);
                                ccCard.dragedCard.Location = ((ccCard)pnl.Controls[j]).Location;//new Point(x, y);
                                for (i = 0; i < j; i++)
                                {
                                    tmp           = new ccCard(((ccCard)pnl.Controls[i]).Number, ((ccCard)pnl.Controls[i]).getColor);
                                    tmp.Status    = ((ccCard)pnl.Controls[i]).Status;
                                    tmp.Size      = ((ccCard)pnl.Controls[i]).Size;
                                    tmp.Font      = ((ccCard)pnl.Controls[i]).Font;
                                    tmp.Location  = ((ccCard)pnl.Controls[i]).Location;
                                    newpnl.Width += width_card_in_board;
                                    newpnl.Controls.Add(tmp);
                                    //x = tmp.Location.X;
                                    //y = tmp.Location.Y;
                                }
                                ccCard.dragedCard.Location = ((ccCard)pnl.Controls[j]).Location;
                                x = ((ccCard)pnl.Controls[j]).Location.X;
                                y = ((ccCard)pnl.Controls[j]).Location.Y;
                                newpnl.Controls.Add(ccCard.dragedCard);
                                //להוריד את הכרטיס מהלוח
                                currentUcGame.playersBoards[currentUcGame.turn].listCards.Remove(ccCard.dragedCard);//@@
                                //int x = space_card_in_board, y = 2;
                                for (i = j; i < pnl.Controls.Count; i++)
                                {
                                    newpnl.Width += width_card_in_board + space_card_in_board;
                                    x            += width_card_in_board + space_card_in_board;
                                    tmp           = new ccCard(((ccCard)pnl.Controls[i]).Number, ((ccCard)pnl.Controls[i]).getColor);
                                    tmp.Status    = ((ccCard)pnl.Controls[i]).Status;
                                    tmp.Size      = ((ccCard)pnl.Controls[i]).Size;
                                    tmp.Font      = ((ccCard)pnl.Controls[i]).Font;
                                    //tmp = ((ccCard)pnl.Controls[i]).Clone();
                                    tmp.Location = new Point(x, y);
                                    newpnl.Controls.Add(tmp);
                                    newpnl.BackColor = System.Drawing.Color.Transparent;
                                }
                                pnlMainPlayBoard.Controls.Remove(pnl);
                                pnlMainPlayBoard.Controls.Add(newpnl);
                                flgsmall = true;
                            }
                        }

                        //שומר את פאנל האב
                        //ccCard.dragedCard.Parent = pnl;
                        if (!flgsmall)
                        {
                            ccCard.dragedCard.Location = new Point(pnl.Controls[pnl.Controls.Count - 1].Location.X + width_card_in_board + space_card_in_board, pnl.Controls[pnl.Controls.Count - 1].Location.Y);
                            pnl.Width += width_card_in_board + space_card_in_board;
                            pnl.Controls.Add(ccCard.dragedCard);
                        }
                        flag = true;
                        //continue;
                    }
                    if (!flag)
                    {
                        //מציאת הפאנל ממנו נגרר הקלף
                        //if (ccCard.dragedCard.Location.Y + pnl.Location.Y > pnl.Location.Y && ccCard.dragedCard.Location.Y + pnl.Location.Y < (pnl.Location.Y + pnl.Height) && ccCard.dragedCard.Location.X + pnl.Location.X > pnl.Location.X && ccCard.dragedCard.Location.X + pnl.Location.X < (pnl.Location.X + pnl.Width))
                        if (pnl.Controls.Contains(ccCard.dragedCard))
                        {
                            //הערה- להוסיף בדיקת מיקום גם ל-DRAGLEAVE
                            //האם הקלף שהוסר היה הראשון או האחרון
                            //אם האחרון-
                            // if (pnl.Controls.GetChildIndex(ccCard.dragedCard) == pnl.Controls.Count-1)
                            {
                                //for (int i = pnl.Controls.GetChildIndex(ccCard.dragedCard)-1; i < pnl.Controls.Count; i++)//אולי טוב יותר
                                for (int i = pnl.Controls.GetChildIndex(ccCard.dragedCard); i < pnl.Controls.Count; i++)
                                {
                                    pnl.Controls[i].Left -= (width_card_in_board + space_card_in_board);//(card.Width + 2);
                                }
                                pnl.Controls.Remove(ccCard.dragedCard);
                                //foreach (ccCard card in pnl.Controls)
                                //{
                                //    card.Left -= (card.Width + 2);//(ccCard.width_card + ccCard.l_space_between_cards);
                                //}
                                pnl.Width -= (width_card_in_board + space_card_in_board);
                            }
                        }
                        //else
                        //    break;
                    }
                }
                e.Effect = DragDropEffects.Move;
                if (!flag)
                {
                    Panel p = new Panel();
                    ccCard.dragedCard.Location = new Point(2, 2);
                    //שומר את פאנל האב
                    //ccCard.dragedCard.Parent = p;
                    p.Controls.Add(ccCard.dragedCard);
                    p.Location    = pnt;
                    p.BorderStyle = BorderStyle.None;
                    p.Height      = height_card_in_board + 6;
                    p.Width       = width_card_in_board + (4 * space_card_in_board);
                    p.BackColor   = System.Drawing.Color.Transparent;
                    pnlMainPlayBoard.Controls.Add(p);
                }

                //בדיקה אם ישנו פאנל ריק לאחר גרירה
                foreach (Panel pnl in pnlMainPlayBoard.Controls)
                {
                    if (pnl.Controls.Count == 0)
                    {
                        pnlMainPlayBoard.Controls.Remove(pnl);
                    }
                }
            }
        }
Example #2
0
        private void pnlMainPlayBoard_DragDrop(object sender, DragEventArgs e)
        {
            bool flag = false;

            ccCard.dragedCard.Width  = width_card_in_board;
            ccCard.dragedCard.Height = height_card_in_board;
            ccCard.dragedCard.Font   = new System.Drawing.Font("OCR A Extended", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
            Point pnt = PointToClient(Cursor.Position);

            pnt.X -= pnlMainPlayBoard.Location.X;
            pnt.Y -= pnlMainPlayBoard.Location.Y;
            bool flgsmall = false;

            foreach (Panel pnl in pnlMainPlayBoard.Controls)
            {
                //מציאת הפאנל אליו נגרר הקלף
                if (pnt.Y > pnl.Location.Y && pnt.Y < (pnl.Location.Y + pnl.Height) && pnt.X > pnl.Location.X && pnt.X < (pnl.Location.X + pnl.Width))

                {
                    //במקרה בו המספר שהוכנס אינו הגדול ביותר
                    for (int j = 0; j < pnl.Controls.Count && !flgsmall; j++)
                    {
                        if (ccCard.dragedCard.Number < ((ccCard)pnl.Controls[j]).Number)
                        {
                            ccCard tmp;
                            int    x = space_card_in_board, y = 2, i;
                            Panel  newpnl = new Panel();
                            newpnl.Location            = pnl.Location;
                            newpnl.Size                = new System.Drawing.Size(width_card_in_board + space_card_in_board, height_card_in_board + 4);
                            ccCard.dragedCard.Location = ((ccCard)pnl.Controls[j]).Location;//new Point(x, y);
                            for (i = 0; i < j; i++)
                            {
                                tmp           = new ccCard(((ccCard)pnl.Controls[i]).Number, ((ccCard)pnl.Controls[i]).getColor);
                                tmp.Status    = ((ccCard)pnl.Controls[i]).Status;
                                tmp.Size      = ((ccCard)pnl.Controls[i]).Size;
                                tmp.Font      = ((ccCard)pnl.Controls[i]).Font;
                                tmp.Location  = ((ccCard)pnl.Controls[i]).Location;
                                newpnl.Width += width_card_in_board + space_card_in_board;
                                newpnl.Controls.Add(tmp);
                                //x = tmp.Location.X;
                                //y = tmp.Location.Y;
                            }
                            ccCard.dragedCard.Location = ((ccCard)pnl.Controls[j]).Location;
                            x = ((ccCard)pnl.Controls[j]).Location.X;
                            y = ((ccCard)pnl.Controls[j]).Location.Y;
                            newpnl.Controls.Add(ccCard.dragedCard);
                            //int x = space_card_in_board, y = 2;
                            for (i = j; i < pnl.Controls.Count; i++)
                            {
                                newpnl.Width += width_card_in_board + space_card_in_board;
                                x            += width_card_in_board + space_card_in_board;
                                tmp           = new ccCard(((ccCard)pnl.Controls[i]).Number, ((ccCard)pnl.Controls[i]).getColor);
                                tmp.Status    = ((ccCard)pnl.Controls[i]).Status;
                                tmp.Size      = ((ccCard)pnl.Controls[i]).Size;
                                tmp.Font      = ((ccCard)pnl.Controls[i]).Font;
                                //tmp = ((ccCard)pnl.Controls[i]).Clone();
                                tmp.Location = new Point(x, y);
                                newpnl.Controls.Add(tmp);
                            }
                            pnlMainPlayBoard.Controls.Remove(pnl);
                            pnlMainPlayBoard.Controls.Add(newpnl);
                            flgsmall = true;
                        }
                    }
                    //אם המספר קטן מהראשון- יש להחליף את סדר הקונטרולים על הפאנל הנוכחי- אבל איך?
                    //for (int i = 0; i < pnl.Controls.Count; i++)
                    //{
                    //    if(((ccCard)pnl.Controls[i]).Number<ccCard.dragedCard.Number)
                    //        continue;
                    //    else
                    //        while (ccCard.dragedCard.Number < ((ccCard)pnl.Controls[i]).Number)
                    //        {
                    //            pnl.Controls[i].Left+=32;
                    //           // pnl.Controls.SetChildIndex(pnl.Controls[i],++i);
                    //        }
                    //}


                    //{
                    //    for (int i = 0; i > pnl.Controls.Count - 2; i++)
                    //    {
                    //        pnl.Controls.SetChildIndex(pnl.Controls[i], i + 1);
                    //        //pnl.Controls[i]=pnl.Controls[i - 1];
                    //    }
                    //}
                    //שומר את פאנל האב
                    //ccCard.dragedCard.Parent = pnl;
                    if (!flgsmall)
                    {
                        ccCard.dragedCard.Location = new Point(pnl.Controls[pnl.Controls.Count - 1].Location.X + width_card_in_board + space_card_in_board, pnl.Controls[pnl.Controls.Count - 1].Location.Y);
                        pnl.Width += width_card_in_board + space_card_in_board;
                        pnl.Controls.Add(ccCard.dragedCard);
                    }
                    flag = true;
                    //continue;
                }
                if (!flag)
                {
                    //מציאת הפאנל ממנו נגרר הקלף
                    //if (ccCard.dragedCard.Location.Y + pnl.Location.Y > pnl.Location.Y && ccCard.dragedCard.Location.Y + pnl.Location.Y < (pnl.Location.Y + pnl.Height) && ccCard.dragedCard.Location.X + pnl.Location.X > pnl.Location.X && ccCard.dragedCard.Location.X + pnl.Location.X < (pnl.Location.X + pnl.Width))
                    if (pnl.Controls.Contains(ccCard.dragedCard))
                    {
                        //הערה- להוסיף בדיקת מיקום גם ל-DRAGLEAVE
                        //האם הקלף שהוסר היה הראשון או האחרון
                        //אם האחרון-
                        // if (pnl.Controls.GetChildIndex(ccCard.dragedCard) == pnl.Controls.Count-1)
                        {
                            //for (int i = pnl.Controls.GetChildIndex(ccCard.dragedCard)-1; i < pnl.Controls.Count; i++)//אולי טוב יותר
                            for (int i = pnl.Controls.GetChildIndex(ccCard.dragedCard); i < pnl.Controls.Count; i++)
                            {
                                pnl.Controls[i].Left -= (width_card_in_board + space_card_in_board);//(card.Width + 2);
                            }
                            pnl.Controls.Remove(ccCard.dragedCard);
                            //foreach (ccCard card in pnl.Controls)
                            //{
                            //    card.Left -= (card.Width + 2);//(ccCard.width_card + ccCard.l_space_between_cards);
                            //}
                            pnl.Width -= (width_card_in_board + space_card_in_board);
                        }
                    }
                    //else
                    //    break;
                }
            }
            e.Effect = DragDropEffects.Move;
            if (!flag)
            {
                Panel p = new Panel();
                ccCard.dragedCard.Location = new Point(2, 2);
                //שומר את פאנל האב
                //ccCard.dragedCard.Parent = p;
                p.Controls.Add(ccCard.dragedCard);
                p.Location    = pnt;
                p.BorderStyle = BorderStyle.None;
                p.Height      = height_card_in_board + 6;
                p.Width       = width_card_in_board + (4 * space_card_in_board);
                pnlMainPlayBoard.Controls.Add(p);
            }

            //בדיקה אם ישנו פאנל ריק לאחר גרירה
            foreach (Panel pnl in pnlMainPlayBoard.Controls)
            {
                if (pnl.Controls.Count == 0)
                {
                    pnlMainPlayBoard.Controls.Remove(pnl);
                }
            }
        }