コード例 #1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public Children(XAMLiteGridNew parent)
 {
     _parent = parent;
 }
コード例 #2
0
        /// <summary>
        /// Loads the content for the control.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

            var ch = Child as XAMLiteTextBlockNew;
            var bt = 0;

            if (ch != null)
            {
                bt = ch.StrokeThickness;
            }

            switch (Orientation)
            {
            case Orientation.Vertical:
                Width  = 21;
                Height = Child != null && Height == 0 ? Child.Height - (bt * 2) : 100;
                break;

            case Orientation.Horizontal:
                Width  = Child != null && Width == 0 ? Child.Width - (bt * 2) : 100;
                Height = 21;
                break;
            }

            // If there is a child linked to the Scroll Bar, set up a grid that imitates the
            // child's location so that the scroll bar will be positioned within its child's
            // boundaries. Also, modify the padding of the child to accommodate the space
            // that the scroll bar needs.
            if (ch != null)
            {
                DrawOrder = Child.DrawOrder + 1;
                var m = Child.Margin;
                _grid = new XAMLiteGridNew(Game)
                {
                    Width  = Child.Width,
                    Height = Child.Height,
                    Margin = new Thickness(m.Left, m.Top, m.Right + bt, m.Bottom),
                    HorizontalAlignment = Child.HorizontalAlignment,
                    VerticalAlignment   = Child.VerticalAlignment,
                };
                Game.Components.Add(_grid);
                _grid.Children.Add(this);

                ModifyChildPadding(ch);
            }

            var backDrop = new XAMLiteImageNew(Game)
            {
                SourceName = "Icons/ScrollBackDrop",
                Height     = Height - (bt * 2),
                Width      = Width,
                DrawOrder  = DrawOrder
            };

            Children.Add(backDrop);

            var t = Game.Content.Load <Texture2D>("Icons/ArrowButton");

            var upArrowNormalButton = new XAMLiteImageNew(Game, t)
            {
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                DrawOrder           = DrawOrder
            };

            var downArrowNormalButton = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ArrowButton",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.FlipVertical : RenderTransform.RotateClockwise90,
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Bottom,
                DrawOrder           = DrawOrder
            };

            var c = Child as XAMLiteTextBlockNew;

            if (c == null)
            {
                return;
            }

            c.MouseDown += ChildOnMouseDown;
            // if the text height is less than the height of the block, do not load
            // scroll bar nor set the event handlers.

            if (Child is XAMLiteRichTextBlock)
            {
                var rtb = Child as XAMLiteRichTextBlock;
                _childTextHeight = rtb.MeasureTextHeight();
            }
            else
            {
                _childTextHeight = c.MeasureText().Y;
            }

            if (_childTextHeight <= Child.Height)
            {
                Children.Add(upArrowNormalButton);
                Children.Add(downArrowNormalButton);

                return;
            }

            _upArrow = new XAMLiteGridNew(Game)
            {
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                Width     = t.Width,
                Height    = t.Height,
                DrawOrder = DrawOrder
            };
            Children.Add(_upArrow);

            _upArrow.Children.Add(upArrowNormalButton);

            var upArrowHoverButton = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ArrowButtonHover",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _upArrow.Children.Add(upArrowHoverButton);

            _upArrowButtonMouseDown = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ArrowButtonMouseDown",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };
            _upArrow.Children.Add(_upArrowButtonMouseDown);

            _downArrow = new XAMLiteGridNew(Game)
            {
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Bottom,
                Width     = t.Width,
                Height    = t.Height,
                Margin    = new Thickness(0, 0, 0, bt),
                DrawOrder = DrawOrder
            };
            Children.Add(_downArrow);

            _downArrow.Children.Add(downArrowNormalButton);

            var downArrowHoverButton = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ArrowButtonHover",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.FlipVertical : RenderTransform.RotateClockwise90,
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Bottom,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _downArrow.Children.Add(downArrowHoverButton);

            _downArrowButtonMouseDown = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ArrowButtonMouseDown",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.FlipVertical : RenderTransform.RotateClockwise90,
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Bottom,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };
            _downArrow.Children.Add(_downArrowButtonMouseDown);

            // load the event handlers for the arrow buttons.
            _upArrow.MouseEnter += UpArrowOnMouseEnter;
            _upArrow.MouseLeave += UpArrowOnMouseLeave;
            _upArrow.MouseDown  += UpArrowOnMouseDown;

            _downArrow.MouseEnter += DownArrowOnMouseEnter;
            _downArrow.MouseLeave += DownArrowOnMouseLeave;
            _downArrow.MouseDown  += DownArrowOnMouseDown;

            _scrollBarNormal    = new List <XAMLiteImageNew>();
            _scrollBarHover     = new List <XAMLiteImageNew>();
            _scrollBarMouseDown = new List <XAMLiteImageNew>();

            // percent of the text height versus the child height.
            var childToTextRatio = Child.Height / _childTextHeight;

            // the difference between the text height and the height of its
            // container.
            var textHeightToChildHeightDifference = _childTextHeight - Child.Height;

            // the maximum scroll bar height
            _maxScrollBarHeight = Height - (t.Height * 2);

            // the minimum scroll bar height.
            _minScrollBarHeight = 10;

            var scrollHeight = 0;

            if (textHeightToChildHeightDifference + _minScrollBarHeight < _maxScrollBarHeight)
            {
                scrollHeight = (int)(_maxScrollBarHeight - textHeightToChildHeightDifference);
            }
            else
            {
                _isLargeTextBlock = true;
                var difference = textHeightToChildHeightDifference + _minScrollBarHeight + 1 - _maxScrollBarHeight;
                _scrollValueAdjuster = difference / textHeightToChildHeightDifference;
                scrollHeight         = (int)_minScrollBarHeight;
                _textValueAdjuster   = (_childTextHeight - Height) / (_maxScrollBarHeight - scrollHeight);
            }

            // TODO: this is not truly set up yet.  Focus is currently on vertical scrolling.
            var scrollWidth = Orientation == Orientation.Vertical ? Width : (int)((Width - (t.Width * 2)) * childToTextRatio);

            SetInitialScrollValues(new Vector2(scrollWidth, scrollHeight));

            _scrollBar = new XAMLiteGridNew(Game)
            {
                Width  = scrollWidth,
                Height = scrollHeight,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                Margin    = Orientation == Orientation.Vertical ? new Thickness(0, t.Height, 0, 0) : new Thickness(t.Width, 0, 0, 0),
                DrawOrder = DrawOrder
            };
            Children.Add(_scrollBar);
            _scrollBar.MouseEnter += ScrollBarOnMouseEnter;
            _scrollBar.MouseLeave += ScrollBarOnMouseLeave;
            _scrollBar.MouseDown  += ScrollBarOnMouseDown;

            var tTop    = Game.Content.Load <Texture2D>("Icons/ScrollButtonTopNoHover");
            var tBottom = Game.Content.Load <Texture2D>("Icons/ScrollButtonBottomNoHover");

            var scrollBarTopNoHover = new XAMLiteImageNew(Game, tTop)
            {
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarTopNoHover);
            _scrollBarNormal.Add(scrollBarTopNoHover);

            var scrollBarTopHover = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonTopHover",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarTopHover);
            _scrollBarHover.Add(scrollBarTopHover);

            var scrollBarTopMouseDown = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonTopMouseDown",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment   = VerticalAlignment.Top,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarTopMouseDown);
            _scrollBarMouseDown.Add(scrollBarTopMouseDown);

            var scrollBarBodyNoHover = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonBodyNoHover",
                Width               = Orientation == Orientation.Vertical ? _scrollBar.Width : _scrollBar.Width - tTop.Width - tBottom.Width,
                Height              = Orientation == Orientation.Vertical ? _scrollBar.Height - tTop.Height - tBottom.Height : _scrollBar.Height,
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Center,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarBodyNoHover);
            _scrollBarNormal.Add(scrollBarBodyNoHover);

            var scrollBarBodyHover = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonBodyHover",
                Width               = Orientation == Orientation.Vertical ? _scrollBar.Width : _scrollBar.Width - tTop.Width - tBottom.Width,
                Height              = Orientation == Orientation.Vertical ? _scrollBar.Height - tTop.Height - tBottom.Height : _scrollBar.Height,
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Center,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarBodyHover);
            _scrollBarHover.Add(scrollBarBodyHover);

            var scrollBarBodyMouseDown = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonBodyMouseDown",
                Width               = Orientation == Orientation.Vertical ? _scrollBar.Width : _scrollBar.Width - tTop.Width - tBottom.Width,
                Height              = Orientation == Orientation.Vertical ? _scrollBar.Height - tTop.Height - tBottom.Height : _scrollBar.Height,
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateCounterClockwise90,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Center,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarBodyMouseDown);
            _scrollBarMouseDown.Add(scrollBarBodyMouseDown);

            var scrollBarBottomNoHover = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonBottomNoHover",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateClockwise90,
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Bottom,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarBottomNoHover);
            _scrollBarNormal.Add(scrollBarBottomNoHover);

            var scrollBarBottomHover = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonBottomHover",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateClockwise90,
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Bottom,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarBottomHover);
            _scrollBarHover.Add(scrollBarBottomHover);

            var scrollBarBottomMouseDown = new XAMLiteImageNew(Game)
            {
                SourceName          = "Icons/ScrollButtonBottomMouseDown",
                RenderTransform     = Orientation == Orientation.Vertical ? RenderTransform.Normal : RenderTransform.RotateClockwise90,
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment   = VerticalAlignment.Bottom,
                Visibility          = Visibility.Hidden,
                DrawOrder           = DrawOrder
            };

            _scrollBar.Children.Add(scrollBarBottomMouseDown);
            _scrollBarMouseDown.Add(scrollBarBottomMouseDown);
        }