void setContainerPosition(float x, float y) { var stage = _targetElement.getStage(); if (stage == null) { return; } _container.pack(); float offsetX = _manager.offsetX, offsetY = _manager.offsetY, dist = _manager.edgeDistance; var point = _targetElement.localToStageCoordinates(new Vector2(x + offsetX - _container.getWidth() / 2, y - offsetY - _container.getHeight())); if (point.Y < dist) { point = _targetElement.localToStageCoordinates(new Vector2(x + offsetX, y + offsetY)); } if (point.X < dist) { point.X = dist; } if (point.X + _container.getWidth() > stage.getWidth() - dist) { point.X = stage.getWidth() - dist - _container.getWidth(); } if (point.Y + _container.getHeight() > stage.getHeight() - dist) { point.Y = stage.getHeight() - dist - _container.getHeight(); } _container.setPosition(point.X, point.Y); point = _targetElement.localToStageCoordinates(new Vector2(_targetElement.getWidth() / 2, _targetElement.getHeight() / 2)); point -= new Vector2(_container.getX(), _container.getY()); _container.setOrigin(point.X, point.Y); }
public override void layout() { var bg = _style.background; var hScrollKnob = _style.hScrollKnob; var vScrollKnob = _style.vScrollKnob; float bgLeftWidth = 0, bgRightWidth = 0, bgTopHeight = 0, bgBottomHeight = 0; if (bg != null) { bgLeftWidth = bg.leftWidth; bgRightWidth = bg.rightWidth; bgTopHeight = bg.topHeight; bgBottomHeight = bg.bottomHeight; } var width = getWidth(); var height = getHeight(); var scrollbarHeight = 0f; if (hScrollKnob != null) { scrollbarHeight = hScrollKnob.minHeight; } if (_style.hScroll != null) { scrollbarHeight = Math.Max(scrollbarHeight, _style.hScroll.minHeight); } var scrollbarWidth = 0f; if (vScrollKnob != null) { scrollbarWidth = vScrollKnob.minWidth; } if (_style.vScroll != null) { scrollbarWidth = Math.Max(scrollbarWidth, _style.vScroll.minWidth); } // Get available space size by subtracting background's padded area. _areaWidth = width - bgLeftWidth - bgRightWidth; _areaHeight = height - bgTopHeight - bgBottomHeight; if (_widget == null) { return; } // Get widget's desired width. float widgetWidth, widgetHeight; if (_widget is ILayout) { var layout = _widget as ILayout; widgetWidth = layout.preferredWidth; widgetHeight = layout.preferredHeight; } else { widgetWidth = _widget.getWidth(); widgetHeight = _widget.getHeight(); } // Determine if horizontal/vertical scrollbars are needed. _scrollX = _forceScrollX || (widgetWidth > _areaWidth && !_disableX); _scrollY = _forceScrollY || (widgetHeight > _areaHeight && !_disableY); var fade = _fadeScrollBars; if (!fade) { // Check again, now taking into account the area that's taken up by any enabled scrollbars. if (_scrollY) { _areaWidth -= scrollbarWidth; if (!_scrollX && widgetWidth > _areaWidth && !_disableX) { _scrollX = true; } } if (_scrollX) { _areaHeight -= scrollbarHeight; if (!_scrollY && widgetHeight > _areaHeight && !_disableY) { _scrollY = true; _areaWidth -= scrollbarWidth; } } } // the bounds of the scrollable area for the widget. _widgetAreaBounds = RectangleExt.fromFloats(bgLeftWidth, bgBottomHeight, _areaWidth, _areaHeight); if (fade) { // Make sure widget is drawn under fading scrollbars. if (_scrollX && _scrollY) { _areaHeight -= scrollbarHeight; _areaWidth -= scrollbarWidth; } } else { if (_scrollbarsOnTop) { // Make sure widget is drawn under non-fading scrollbars. if (_scrollX) { _widgetAreaBounds.Height += (int)scrollbarHeight; } if (_scrollY) { _widgetAreaBounds.Width += (int)scrollbarWidth; } } else { // Offset widget area y for horizontal scrollbar at bottom. if (_scrollX && _hScrollOnBottom) { _widgetAreaBounds.Y += (int)scrollbarHeight; } // Offset widget area x for vertical scrollbar at left. if (_scrollY && !_vScrollOnRight) { _widgetAreaBounds.X += (int)scrollbarWidth; } } } // If the widget is smaller than the available space, make it take up the available space. widgetWidth = _disableX ? _areaWidth : Math.Max(_areaWidth, widgetWidth); widgetHeight = _disableY ? _areaHeight : Math.Max(_areaHeight, widgetHeight); _maxX = widgetWidth - _areaWidth; _maxY = widgetHeight - _areaHeight; if (fade) { // Make sure widget is drawn under fading scrollbars. if (_scrollX && _scrollY) { _maxY -= scrollbarHeight; _maxX -= scrollbarWidth; } } setScrollX(Mathf.clamp(_amountX, 0, _maxX)); setScrollY(Mathf.clamp(_amountY, 0, _maxY)); // Set the bounds and scroll knob sizes if scrollbars are needed. if (_scrollX) { if (hScrollKnob != null) { var hScrollHeight = _style.hScroll != null ? _style.hScroll.minHeight : hScrollKnob.minHeight; // The corner gap where the two scroll bars intersect might have to flip from right to left. var boundsX = _vScrollOnRight ? bgLeftWidth : bgLeftWidth + scrollbarWidth; // Scrollbar on the top or bottom. var boundsY = _hScrollOnBottom ? bgBottomHeight : height - bgTopHeight - hScrollHeight; _hScrollBounds = RectangleExt.fromFloats(boundsX, boundsY, _areaWidth, hScrollHeight); if (_variableSizeKnobs) { _hKnobBounds.Width = (int)Math.Max(hScrollKnob.minWidth, (int)(_hScrollBounds.Width * _areaWidth / widgetWidth)); } else { _hKnobBounds.Width = (int)hScrollKnob.minWidth; } _hKnobBounds.Height = (int)hScrollKnob.minHeight; _hKnobBounds.X = _hScrollBounds.X + (int)((_hScrollBounds.Width - _hKnobBounds.Width) * getScrollPercentX()); _hKnobBounds.Y = _hScrollBounds.Y; } else { _hScrollBounds = Rectangle.Empty; _hKnobBounds = Rectangle.Empty; } } if (_scrollY) { if (vScrollKnob != null) { var vScrollWidth = _style.vScroll != null ? _style.vScroll.minWidth : vScrollKnob.minWidth; // the small gap where the two scroll bars intersect might have to flip from bottom to top float boundsX, boundsY; if (_hScrollOnBottom) { boundsY = height - bgTopHeight - _areaHeight; } else { boundsY = bgBottomHeight; } // bar on the left or right if (_vScrollOnRight) { boundsX = width - bgRightWidth - vScrollWidth; } else { boundsX = bgLeftWidth; } _vScrollBounds = RectangleExt.fromFloats(boundsX, boundsY, vScrollWidth, _areaHeight); _vKnobBounds.Width = (int)vScrollKnob.minWidth; if (_variableSizeKnobs) { _vKnobBounds.Height = (int)Math.Max(vScrollKnob.minHeight, (int)(_vScrollBounds.Height * _areaHeight / widgetHeight)); } else { _vKnobBounds.Height = (int)vScrollKnob.minHeight; } if (_vScrollOnRight) { _vKnobBounds.X = (int)(width - bgRightWidth - vScrollKnob.minWidth); } else { _vKnobBounds.X = (int)bgLeftWidth; } _vKnobBounds.Y = _vScrollBounds.Y + (int)((_vScrollBounds.Height - _vKnobBounds.Height) * (1 - getScrollPercentY())); } else { _vScrollBounds = Rectangle.Empty; _vKnobBounds = Rectangle.Empty; } } _widget.setSize(widgetWidth, widgetHeight); if (_widget is ILayout) { ((ILayout)_widget).validate(); } }