예제 #1
0
        private void RepositionHorizontal()
        {
            Size cardSize = this.CardSize;
            int  width    = Math.Min(cardSize.Width * m_cardLabels.Count, this.Width);
            int  startX   = (this.Width - width) / 2;

            int offset = (m_cardLabels.Count == 1) ? 0 : (width - cardSize.Width) / (m_cardLabels.Count - 1);

            for (int i = 0; i < m_cardLabels.Count; i++)
            {
                CardLabel cardLabel = m_cardLabels[i];
                cardLabel.Left = startX + offset * i;
                cardLabel.Top  = 0;
                cardLabel.Size = cardSize;
            }
        }
예제 #2
0
        public void RecreateCards()
        {
            bool isVisible = this.Visible;

            if (isVisible)
            {
                this.Visible = false;
            }

            try
            {
                this.SuspendLayout();
                foreach (CardLabel cardLabel in m_cardLabels)
                {
                    cardLabel.SelectedChanged -= OnCardLabel_SelectedChanged;
                }

                //m_cardLabels.Clear();
                if (m_style != LayoutStyle.Horizontal)
                {
                    m_deck.Sort();
                }
                var cardsByNumber = m_deck.ToReducedList();

                if (m_cardLabels.Count > m_deck.Count)
                {
                    int over = m_cardLabels.Count - m_deck.Count;

                    for (int i = 0; i < over; i++)
                    {
                        m_cardLabels.RemoveAt(m_cardLabels.Count - 1);
                        this.Controls.RemoveAt(Controls.Count - 1);
                    }
                }

                int cardsCount = 0;
                //validation
                if (cardsByNumber.Count != 0)
                {
                    //foreach number
                    int xOffset = 0;
                    foreach (var cards in cardsByNumber)
                    {
                        //for each card
                        for (int i = cards.Count - 1; i >= 0; i--)
                        {
                            CardLabel cardLabel;

                            if (cardsCount >= m_cardLabels.Count)
                            {
                                cardLabel = new CardLabel();
                                m_cardLabels.Add(cardLabel);
                                this.Controls.Add(cardLabel);
                            }
                            else
                            {
                                cardLabel = m_cardLabels[cardsCount];
                            }
                            this.Controls.SetChildIndex(cardLabel, cardsCount);
                            cardsCount++;

                            cardLabel.AutoSize    = false;
                            cardLabel.Card        = cards[i];
                            cardLabel.SelectClick = this.SelectClick;
                            cardLabel.FacingUp    = m_facingUp;
                            cardLabel.Tag         = new Point(xOffset, i);

                            cardLabel.SelectedChanged += OnCardLabel_SelectedChanged;
                        }
                        xOffset++;
                    }
                    RepositionCards();
                }

                //
                //this.Controls.Clear();
                //this.Controls.AddRange(m_cardLabels.ToArray());
                //this.ResumeLayout(true);
                this.ResumeLayout(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            finally
            {
                if (isVisible)
                {
                    this.Visible = true;
                }
            }
        }