Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panel scrollbar update #726

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions Blish HUD/Controls/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,60 @@ public Point AutoSizePadding {
set => SetProperty(ref _autoSizePadding, value);
}

protected Scrollbar _panelScrollbar;

protected Vector2 _scrollPadding = new Vector2(4, 0);
/// <summary>
/// Padding on the left side of the <see cref="Scrollbar"/>.
/// </summary>
public Vector2 ScrollPadding {
get => _scrollPadding;
set => SetProperty(ref _scrollPadding, value, true);
}

/// <summary>
/// Indicates whether or not there is a <see cref="Scrollbar"/> drawn inside the <see cref="Panel"/>.
/// </summary>
public bool ScrollbarVisible {
//get => _panelScrollbar != null && _panelScrollbar.Drawn;
//get => _panelScrollbar != null;
get => _scrollbarVisible;
protected set => SetProperty(ref _scrollbarVisible, value, true);
}
protected bool _scrollbarVisible = false;

/// <summary>
/// Space which is taken from the <see cref="Scrollbar"/> to be drawn inside the <see cref="Panel"/>.
/// </summary>
public int ScrollbarWidth {
get => _panelScrollbar != null ? (_panelScrollbar.ScrollbarWidth + (int) ScrollPadding.X) : 0;
}

protected Point _maxSize = Point.Zero;
/// <summary>
/// Maximum Size the <see cref="Container"/> can autoresize to.
/// </summary>
public Point MaxSize {
get => _maxSize;
protected set => SetProperty(ref _maxSize, value, true);
}

/// <summary>
/// Maximum Width the <see cref="Container"/> can autoresize to.
/// </summary>
public int MaxWidth {
get => _maxSize.X;
protected set => SetProperty(ref _maxSize.X, value, true);
}

/// <summary>
/// Maximum Height the <see cref="Container"/> can autoresize to.
/// </summary>
public int MaxHeight{
get => _maxSize.Y;
protected set => SetProperty(ref _maxSize.Y, value, true);
}

protected override CaptureType CapturesInput() => CaptureType.Mouse | CaptureType.MouseWheel;

public IEnumerable<Control> GetDescendants() {
Expand Down Expand Up @@ -146,6 +200,7 @@ public IEnumerable<T> GetChildrenOfType<T>() {

public bool AddChild(Control child) {
if (_children.Contains(child)) return true;
if (_panelScrollbar != null && child == _panelScrollbar) return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be shortened to

return _panelScrollbar != null && child == _panelScrollbar

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that return always and not continue to add the child?


var resultingChildren = _children.ToList();
resultingChildren.Add(child);
Expand Down Expand Up @@ -234,20 +289,25 @@ public sealed override void DoUpdate(GameTime gameTime) {
UpdateContainer(gameTime);

Control[] children = _children.ToArray();
var filteredChildren = children.Where(c => c.GetType() != typeof(Scrollbar) && c.Visible).ToArray();

_contentBounds = ControlUtil.GetControlBounds(filteredChildren);

_contentBounds = ControlUtil.GetControlBounds(children);
var limitSize = _maxSize != Point.Zero;

// Update our size based on the sizing mode
var parent = this.Parent;
if (parent != null) {
if (parent != null) {
this.Size = new Point(GetUpdatedSizing(this.WidthSizingMode,
this.Width,
_contentBounds.X + (this.Width - this.ContentRegion.Width) + _autoSizePadding.X,
parent.ContentRegion.Width - this.Left),
limitSize ? Math.Min(_maxSize.X, this.Width) : this.Width,
limitSize ? Math.Min(_maxSize.X, _contentBounds.X + (this.Width - this.ContentRegion.Width) + _autoSizePadding.X) : _contentBounds.X + (this.Width - this.ContentRegion.Width) + _autoSizePadding.X,
limitSize ? Math.Min(_maxSize.X, parent.ContentRegion.Width - this.Left) : parent.ContentRegion.Width - this.Left),
GetUpdatedSizing(this.HeightSizingMode,
this.Height,
_contentBounds.Y + (this.Height - this.ContentRegion.Height) + _autoSizePadding.Y,
parent.ContentRegion.Height - this.Top));
limitSize ? Math.Min(_maxSize.Y, this.Height) : this.Height,
limitSize ? Math.Min(_maxSize.Y, _contentBounds.Y + (this.Height - this.ContentRegion.Height) + _autoSizePadding.Y) : _contentBounds.Y + (this.Height - this.ContentRegion.Height) + _autoSizePadding.Y,
limitSize ? Math.Min(_maxSize.Y, parent.ContentRegion.Height - this.Top) : parent.ContentRegion.Height - this.Top));

ScrollbarVisible = _contentBounds.Y > this.Height;
}

// Update our children
Expand Down Expand Up @@ -280,8 +340,9 @@ protected sealed override void Paint(SpriteBatch spriteBatch, Rectangle bounds)
public virtual void PaintBeforeChildren(SpriteBatch spriteBatch, Rectangle bounds) { /* NOOP */ }

protected void PaintChildren(SpriteBatch spriteBatch, Rectangle bounds, Rectangle scissor) {
var contentScissor = Rectangle.Intersect(scissor, ContentRegion.ToBounds(this.AbsoluteBounds));

var cR = ContentRegion.ToBounds(this.AbsoluteBounds);
var contentScissor = Rectangle.Intersect(scissor, cR);

var zSortedChildren = _children.ToArray().OrderBy(i => i.ZIndex);

// Render each visible child
Expand All @@ -303,6 +364,8 @@ protected override void DisposeControl() {
descendant.Dispose();
}

_panelScrollbar?.Dispose();

base.DisposeControl();
}

Expand Down
10 changes: 5 additions & 5 deletions Blish HUD/Controls/FlowPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void ReflowChildLayoutLeftToRight(IEnumerable<Control> allChildren) {

foreach (var child in allChildren.Where(c => c.Visible)) {
// Need to flow over to the next row
if (child.Width >= this.Width - lastRight) {
if (child.Width >= this.ContentRegion.Width - lastRight) {
currentBottom = nextBottom + _controlPadding.Y;
lastRight = outerPadX;
}
Expand All @@ -192,13 +192,13 @@ private void ReflowChildLayoutRightToLeft(IEnumerable<Control> allChildren) {

float nextBottom = outerPadY;
float currentBottom = outerPadY;
float lastLeft = this.Width - outerPadX;
float lastLeft = this.ContentRegion.Width - outerPadX;

foreach (var child in allChildren.Where(c => c.Visible)) {
// Need to flow over to the next row
if (outerPadX > lastLeft - child.Width) {
currentBottom = nextBottom + _controlPadding.Y;
lastLeft = this.Width - outerPadX;
lastLeft = this.ContentRegion.Width - outerPadX;
}

child.Location = new Point((int) (lastLeft - child.Width), (int) currentBottom);
Expand Down Expand Up @@ -275,7 +275,7 @@ private void ReflowChildLayoutSingleRightToLeft(IEnumerable<Control> allChildren
float outerPadX = _padLeftBeforeControl ? _controlPadding.X : _outerControlPadding.X;
float outerPadY = _padTopBeforeControl ? _controlPadding.Y : _outerControlPadding.Y;

var lastLeft = this.Width - outerPadX;
var lastLeft = this.ContentRegion.Width - outerPadX;

foreach (var child in allChildren) {
child.Location = new Point((int) (lastLeft - child.Width), (int) outerPadY);
Expand Down Expand Up @@ -350,4 +350,4 @@ protected override void DisposeControl() {
}

}
}
}
23 changes: 14 additions & 9 deletions Blish HUD/Controls/Panel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public override SizingMode HeightSizingMode {
[JsonIgnore] public float AccentOpacity { get; set; } = 1f;

private Glide.Tween _collapseAnim;
private Scrollbar _panelScrollbar;

/// <inheritdoc />
public bool ToggleAccordionState() {
Expand Down Expand Up @@ -242,34 +241,42 @@ public override void RecalculateLayout() {
_layoutLeftAccentSrc = new Rectangle(0, 0, _textureLeftSideAccent.Width, _layoutLeftAccentBounds.Height);
}

ScrollbarVisible = _contentBounds.Y > (_size.Y - topOffset - bottomOffset);

this.ContentRegion = new Rectangle(leftOffset,
topOffset,
_size.X - leftOffset - rightOffset,
_size.X - leftOffset - rightOffset - (ScrollbarVisible ? (ScrollbarWidth / 2) + (ScrollbarVisible ? 0 : RIGHT_PADDING) : 0),
_size.Y - topOffset - bottomOffset);

_layoutHeaderBounds = new Rectangle(this.ContentRegion.Left, 0, this.ContentRegion.Width, HEADER_HEIGHT);
_layoutHeaderBounds = new Rectangle(this.ContentRegion.Left, 0, this.ContentRegion.Width, HEADER_HEIGHT);
_layoutHeaderTextBounds = new Rectangle(_layoutHeaderBounds.Left + 10, 0, _layoutHeaderBounds.Width - 10, HEADER_HEIGHT);

_layoutAccordionArrowOrigin = new Vector2((float)ARROW_SIZE / 2, (float)ARROW_SIZE / 2);
_layoutAccordionArrowBounds = new Rectangle(_layoutHeaderBounds.Right - ARROW_SIZE,
(topOffset - ARROW_SIZE) / 2,
ARROW_SIZE,
ARROW_SIZE).OffsetBy(_layoutAccordionArrowOrigin.ToPoint());

if (_panelScrollbar != null) {
_panelScrollbar.Height = this.ContentRegion.Height - 20;
_panelScrollbar.Right = this.Right;
_panelScrollbar.Top = this.Top + this.ContentRegion.Top + 10;
}
}

private void UpdateScrollbar() {
/* TODO: Fix .CanScroll: currently you have to set it after you set other region changing settings for it
to work correctly */
if (this.CanScroll) {
if (_panelScrollbar == null)
if (_panelScrollbar == null)
_panelScrollbar = new Scrollbar(this);

this.PropertyChanged -= UpdatePanelScrollbarOnOwnPropertyChanged;
this.PropertyChanged += UpdatePanelScrollbarOnOwnPropertyChanged;

_panelScrollbar.Parent = this.Parent;
_panelScrollbar.Height = this.ContentRegion.Height - 20;
_panelScrollbar.Right = this.Right - _panelScrollbar.Width / 2;
_panelScrollbar.Right = this.Right;
_panelScrollbar.Top = this.Top + this.ContentRegion.Top + 10;
_panelScrollbar.Visible = this.Visible;
_panelScrollbar.ZIndex = this.ZIndex + 2;
Expand All @@ -291,7 +298,7 @@ private void UpdatePanelScrollbarOnOwnPropertyChanged(object? sender, PropertyCh
_panelScrollbar.Height = this.ContentRegion.Height - 20;
break;
case "Right":
_panelScrollbar.Right = this.Right - _panelScrollbar.Width / 2;
_panelScrollbar.Right = this.Right;
break;
case "Top":
_panelScrollbar.Top = this.Top + this.ContentRegion.Top + 10;
Expand Down Expand Up @@ -389,8 +396,6 @@ public override void PaintBeforeChildren(SpriteBatch spriteBatch, Rectangle boun
}

protected override void DisposeControl() {
_panelScrollbar?.Dispose();

foreach (var control in this._children) {
control.Resized -= UpdateContentRegionBounds;
control.Moved -= UpdateContentRegionBounds;
Expand All @@ -400,4 +405,4 @@ protected override void DisposeControl() {
}

}
}
}
18 changes: 17 additions & 1 deletion Blish HUD/Controls/Scrollbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ private int ScrollbarHeight {

private double _scrollbarPercent = 1.0;

/// <summary>
/// See if the <see cref="Scrollbar"/> is beeing drawn.
/// </summary>
public bool Drawn
{
get => Visible && _scrollbarPercent < 0.99;
}

/// <summary>
/// Width of the <see cref="Scrollbar"/>.
/// </summary>
public int ScrollbarWidth
{
get => _barBounds.Width;
}

private Container _associatedContainer;
public Container AssociatedContainer {
get => _associatedContainer;
Expand Down Expand Up @@ -298,4 +314,4 @@ protected override void Paint(SpriteBatch spriteBatch, Rectangle bounds) {
}

}
}
}
2 changes: 1 addition & 1 deletion Blish HUD/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
"commandLineArgs": "-p powershell -w ConsoleWindowClass"
}
}
}
}