Skip to content

Commit

Permalink
Stop all mouse inputs from propagating beyond panel header
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Jan 18, 2025
1 parent d78b3b3 commit 01ad727
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions Blish HUD/Controls/Panel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using Microsoft.Xna.Framework.Input;

namespace Blish_HUD.Controls {

Expand All @@ -18,21 +19,21 @@ public class Panel : Container, IAccordion {
/* ControlOffset */ Control.ControlStandard.ControlOffset);

// Used when border is enabled
public const int TOP_PADDING = 7;
public const int RIGHT_PADDING = 4;
public const int TOP_PADDING = 7;
public const int RIGHT_PADDING = 4;
public const int BOTTOM_PADDING = 7;
public const int LEFT_PADDING = 4;
public const int LEFT_PADDING = 4;

public const int HEADER_HEIGHT = 36;
private const int ARROW_SIZE = 32;
public const int HEADER_HEIGHT = 36;
private const int ARROW_SIZE = 32;
private const int MAX_ACCENT_WIDTH = 256;

#region Textures

private readonly AsyncTexture2D _texturePanelHeader = AsyncTexture2D.FromAssetId(1032325);
private readonly AsyncTexture2D _texturePanelHeader = AsyncTexture2D.FromAssetId(1032325);
private readonly AsyncTexture2D _texturePanelHeaderActive = AsyncTexture2D.FromAssetId(1032324);

private readonly AsyncTexture2D _textureCornerAccent = AsyncTexture2D.FromAssetId(1002144);
private readonly AsyncTexture2D _textureCornerAccent = AsyncTexture2D.FromAssetId(1002144);
private readonly AsyncTexture2D _textureLeftSideAccent = AsyncTexture2D.FromAssetId(605025);

private readonly AsyncTexture2D _textureAccordionArrow = AsyncTexture2D.FromAssetId(155953);
Expand All @@ -50,7 +51,7 @@ public bool CanScroll {
UpdateScrollbar();
}
}

protected string _title;
public string Title {
get => _title;
Expand Down Expand Up @@ -121,7 +122,7 @@ public override SizingMode HeightSizingMode {
[JsonIgnore] public float AccentOpacity { get; set; } = 1f;

private Glide.Tween _collapseAnim;
private Scrollbar _panelScrollbar;
private Scrollbar _panelScrollbar;

/// <inheritdoc />
public bool ToggleAccordionState() {
Expand All @@ -144,17 +145,22 @@ public void NavigateToBuiltPanel(BuildUIDelegate buildCall, object obj) {

/// <inheritdoc />
protected override void OnClick(MouseEventArgs e) {
if (!string.IsNullOrEmpty(_title) && _layoutHeaderBounds.Contains(this.RelativeMousePosition)) {
if (_canCollapse) {
this.ToggleAccordionState();
}

e.StopPropagation();
if (_canCollapse && _layoutHeaderBounds.Contains(this.RelativeMousePosition)) {
this.ToggleAccordionState();
}

base.OnClick(e);
}

/// <inheritdoc />
public override Control TriggerMouseInput(MouseEventArgs args, MouseState ms) {
if (!string.IsNullOrEmpty(_title) && _layoutHeaderBounds.Contains(this.RelativeMousePosition)) {
args.StopPropagation();
}

return base.TriggerMouseInput(args, ms);
}

protected override void OnChildAdded(ChildChangedEventArgs e) {
base.OnChildAdded(e);

Expand All @@ -166,7 +172,7 @@ protected override void OnChildRemoved(ChildChangedEventArgs e) {
base.OnChildRemoved(e);

e.ChangedChild.Resized -= UpdateContentRegionBounds;
e.ChangedChild.Moved -= UpdateContentRegionBounds;
e.ChangedChild.Moved -= UpdateContentRegionBounds;
}

/// <inheritdoc />
Expand Down Expand Up @@ -198,7 +204,7 @@ public void Collapse() {
}

SetProperty(ref _canCollapse, true);
SetProperty(ref _collapsed, true);
SetProperty(ref _collapsed, true);

_collapseAnim = Animation.Tweener
.Tween(this,
Expand All @@ -215,7 +221,7 @@ private void UpdateContentRegionBounds(object sender, EventArgs e) {
private Rectangle _layoutHeaderIconBounds;
private Rectangle _layoutHeaderTextBounds;

private Vector2 _layoutAccordionArrowOrigin;
private Vector2 _layoutAccordionArrowOrigin;
private Rectangle _layoutAccordionArrowBounds;

private Rectangle _layoutTopLeftAccentBounds;
Expand All @@ -229,16 +235,16 @@ private void UpdateContentRegionBounds(object sender, EventArgs e) {
public override void RecalculateLayout() {
bool showsHeader = !string.IsNullOrEmpty(_title);

int topOffset = showsHeader ? HEADER_HEIGHT : 0;
int rightOffset = 0;
int topOffset = showsHeader ? HEADER_HEIGHT : 0;
int rightOffset = 0;
int bottomOffset = 0;
int leftOffset = 0;
int leftOffset = 0;

if (this.ShowBorder) {
topOffset = Math.Max(TOP_PADDING, topOffset);
rightOffset = RIGHT_PADDING;
topOffset = Math.Max(TOP_PADDING, topOffset);
rightOffset = RIGHT_PADDING;
bottomOffset = BOTTOM_PADDING;
leftOffset = LEFT_PADDING;
leftOffset = LEFT_PADDING;

// Corner accents
int cornerAccentWidth = Math.Min(_size.X, MAX_ACCENT_WIDTH);
Expand All @@ -250,15 +256,15 @@ public override void RecalculateLayout() {

// Left side accent
_layoutLeftAccentBounds = new Rectangle(leftOffset - 7, topOffset, _textureLeftSideAccent.Width, Math.Min(_size.Y - topOffset - bottomOffset, _textureLeftSideAccent.Height));
_layoutLeftAccentSrc = new Rectangle(0, 0, _textureLeftSideAccent.Width, _layoutLeftAccentBounds.Height);
_layoutLeftAccentSrc = new Rectangle(0, 0, _textureLeftSideAccent.Width, _layoutLeftAccentBounds.Height);
}

this.ContentRegion = new Rectangle(leftOffset,
topOffset,
_size.X - leftOffset - rightOffset,
_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);

if (_icon?.HasTexture == true) {

Expand All @@ -283,18 +289,18 @@ 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.Top = this.Top + this.ContentRegion.Top + 10;
_panelScrollbar.Parent = this.Parent;
_panelScrollbar.Height = this.ContentRegion.Height - 20;
_panelScrollbar.Right = this.Right - _panelScrollbar.Width / 2;
_panelScrollbar.Top = this.Top + this.ContentRegion.Top + 10;
_panelScrollbar.Visible = this.Visible;
_panelScrollbar.ZIndex = this.ZIndex + 2;
_panelScrollbar.ZIndex = this.ZIndex + 2;
} else {
this.PropertyChanged -= UpdatePanelScrollbarOnOwnPropertyChanged;
_panelScrollbar?.Dispose();
Expand Down Expand Up @@ -417,10 +423,10 @@ 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;
control.Moved -= UpdateContentRegionBounds;
}

base.DisposeControl();
Expand Down

0 comments on commit 01ad727

Please sign in to comment.