Exemplo n.º 1
0
        private int GetRowUnderMouse(UpdateState update)
        {
            var mouse = this.GetMousePosition(update.MouseState);
            var estRow = (int)Math.Floor(mouse.Y / RowHeight) + ScrollOffset;
            if (estRow >= 0 && estRow < Items.Count)
            {
                /** Is this row enabled? **/
                var row = Items[estRow];
                if (ValuePointer.Get<Boolean>(row.Disabled) && AllowDisabledSelection == false) { return -1; }

                return estRow;
            }
            return -1;
        }
Exemplo n.º 2
0
        private void _Draw(UISpriteBatch batch)
        {
            for (var i = 0; i < NumVisibleRows; i++)
            {
                var rowIndex = i + ScrollOffset;
                if (!(rowIndex < m_Items.Count))
                {
                    /** Out of bounds **/
                    continue;
                }

                var row     = m_Items[rowIndex];
                var rowY    = i * RowHeight;
                var columnX = 0;

                var selected = rowIndex == m_SelectedRow;
                var hover    = rowIndex == m_HoverRow;
                if (selected)
                {
                    /** Draw selection background **/
                    var white = TextureGenerator.GetPxWhite(batch.GraphicsDevice);
                    DrawLocalTexture(batch, white, null, new Vector2(0, rowY), new Vector2(m_Width, RowHeight), m_SelectionFillColor);
                }

                var ts = TextStyle;
                if (row.CustomStyle != null)
                {
                    ts = row.CustomStyle;
                }
                TextStyle style      = null;
                var       isDisabled = ValuePointer.Get <Boolean>(row.Disabled);

                if (ts != null)
                {
                    style = ts.Normal;
                    if (ValuePointer.Get <Boolean>(row.UseDisabledStyleByDefault))
                    {
                        style = ts.Disabled;
                    }
                    if (selected)
                    {
                        style = ts.Selected;
                    }
                    else if (hover)
                    {
                        style = ts.Highlighted;
                    }
                    else if (isDisabled)
                    {
                        style = ts.Disabled;
                    }
                }

                for (var x = 0; x < row.Columns.Length; x++)
                {
                    var columnValue  = row.Columns[x];
                    var columnSpec   = m_Columns[x];
                    var columnBounds = new Rectangle(0, 0, columnSpec.Width, RowHeight);

                    if (columnValue is FSO.Content.Model.ITextureRef)
                    {
                        columnValue = ((FSO.Content.Model.ITextureRef)columnValue).Get(batch.GraphicsDevice);
                    }

                    if (columnValue is string)
                    {
                        DrawLocalString(batch, style.TruncateToWidth((string)columnValue, columnSpec.Width), new Vector2(columnX, rowY), style, columnBounds, columnSpec.Alignment);
                    }
                    else if (columnValue is Texture2D)
                    {
                        var tex          = (Texture2D)columnValue;
                        var texWidthDiv4 = tex.Width / 4;
                        /** We assume its a 4 state button **/
                        Rectangle from = new Rectangle(texWidthDiv4 * columnSpec.TextureDefaultFrame, 0, texWidthDiv4, tex.Height);
                        if (selected)
                        {
                            from.X = texWidthDiv4 * columnSpec.TextureSelectedFrame;
                        }
                        else if (hover)
                        {
                            from.X = texWidthDiv4 * columnSpec.TextureHoverFrame;
                        }
                        else if (isDisabled)
                        {
                            from.X = texWidthDiv4 * columnSpec.TextureDisabledFrame;
                        }

                        var destWidth  = texWidthDiv4;
                        var destHeight = tex.Height;


                        if (columnSpec.TextureBounds != null && columnSpec.TextureBounds.HasValue)
                        {
                            var boundsX = columnSpec.TextureBounds.Value.X;
                            var boundsY = columnSpec.TextureBounds.Value.Y;

                            if (!columnSpec.TextureMaintainAspectRatio)
                            {
                                destWidth  = (int)boundsX;
                                destHeight = (int)boundsY;
                            }
                            else
                            {
                                if (destWidth > destHeight)
                                {
                                    destWidth  = (int)boundsX;
                                    destHeight = (int)(((float)tex.Height / (float)texWidthDiv4) * destWidth);
                                }
                                else
                                {
                                    destHeight = (int)boundsY;
                                    destWidth  = (int)(((float)texWidthDiv4 / (float)tex.Height) * destHeight);
                                }
                            }
                        }

                        var to = new Vector2(columnX, rowY);
                        if ((columnSpec.Alignment & TextAlignment.Middle) == TextAlignment.Middle)
                        {
                            to.Y = rowY + ((RowHeight - destHeight) / 2);
                        }
                        else if ((columnSpec.Alignment & TextAlignment.Bottom) == TextAlignment.Bottom)
                        {
                            to.Y = rowY + ((RowHeight - destHeight));
                        }

                        if ((columnSpec.Alignment & TextAlignment.Center) == TextAlignment.Center)
                        {
                            to.X = columnX + ((columnBounds.Width - destWidth) / 2);
                        }
                        else if ((columnSpec.Alignment & TextAlignment.Right) == TextAlignment.Right)
                        {
                            to.X = columnX + (columnBounds.Width - destWidth);
                        }

                        DrawLocalTexture(batch, (Texture2D)columnValue, from, to, new Vector2((float)destWidth / (float)texWidthDiv4, (float)destHeight / (float)tex.Height));
                    }
                    else if (columnValue is UIContainer)
                    {
                        var container = (UIContainer)columnValue;
                        container.Position = this.Position + new Vector2(columnX, rowY);
                        container.Parent   = this.Parent;
                        container.InvalidateMatrix();
                        container.PreDraw(batch);
                        container.Draw(batch);
                        container.Parent = null;
                    }
                    else if (columnValue != null)
                    {
                        //Convert it to a string
                        DrawLocalString(batch, (string)columnValue.ToString(), new Vector2(columnX, rowY), style, columnBounds, columnSpec.Alignment);
                    }

                    columnX += columnSpec.Width;
                }
            }
        }