Skip to content

Commit 9477d08

Browse files
committed
Corrected column width basis to resolve initial chatbox sizing issues, concludes the fixes for AscensionGameDev#2539
1 parent 1a060c0 commit 9477d08

File tree

5 files changed

+80
-19
lines changed

5 files changed

+80
-19
lines changed

Intersect.Client.Core/Interface/Game/Menu.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ private void MenuButtonClicked(Base sender, MouseButtonState arguments)
370370
}
371371
else
372372
{
373-
Interface.GameUi?.EscapeMenu?.ToggleHidden();
373+
Interface.GameUi?.EscapeMenu?.Show();
374374
}
375375
}
376376

Intersect.Client.Framework/Gwen/Control/Base.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,8 @@ private bool HasNamedChildren()
14411441
/// Invoked when control's bounds have been changed.
14421442
/// </summary>
14431443
public event GwenEventHandler<ValueChangedEventArgs<Rectangle>>? BoundsChanged;
1444+
public event GwenEventHandler<ValueChangedEventArgs<Point>>? PositionChanged;
1445+
public event GwenEventHandler<ValueChangedEventArgs<Point>>? SizeChanged;
14441446

14451447
public virtual event GwenEventHandler<MouseButtonState>? MouseDown;
14461448

@@ -2308,19 +2310,36 @@ public virtual bool SetBounds(int x, int y, int width, int height)
23082310
if (oldBounds.Position != newBounds.Position)
23092311
{
23102312
OnPositionChanged(oldBounds.Position, newBounds.Position);
2313+
PositionChanged?.Invoke(
2314+
this,
2315+
new ValueChangedEventArgs<Point>
2316+
{
2317+
Value = newBounds.Position,
2318+
OldValue = oldBounds.Position,
2319+
}
2320+
);
23112321
}
23122322

23132323
if (oldBounds.Size != newBounds.Size)
23142324
{
23152325
OnSizeChanged(oldBounds.Size, newBounds.Size);
2326+
SizeChanged?.Invoke(
2327+
this,
2328+
new ValueChangedEventArgs<Point>
2329+
{
2330+
Value = newBounds.Size,
2331+
OldValue = oldBounds.Size,
2332+
}
2333+
);
23162334
ProcessAlignments();
23172335
}
23182336

23192337
BoundsChanged?.Invoke(
23202338
this,
23212339
new ValueChangedEventArgs<Rectangle>
23222340
{
2323-
Value = newBounds, OldValue = oldBounds,
2341+
Value = newBounds,
2342+
OldValue = oldBounds,
23242343
}
23252344
);
23262345

Intersect.Client.Framework/Gwen/Control/Layout/Table.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,13 @@ protected virtual void OnColumnCountChanged(int oldValue, int columnCount)
287287
}
288288

289289
_computedColumnWidths.Capacity = Math.Max(_computedColumnWidths.Capacity, columnCount);
290+
var currentComputedColumnWidthSum = _computedColumnWidths.Sum();
291+
var remainingColumnWidth = Math.Max(0, InnerWidth - currentComputedColumnWidthSum);
292+
var columnsToAdd = columnCount - _computedColumnWidths.Count;
293+
var dynamicColumnWidth = remainingColumnWidth / Math.Max(1, columnsToAdd);
290294
while (_computedColumnWidths.Count < columnCount)
291295
{
292-
_computedColumnWidths.Add(0);
296+
_computedColumnWidths.Add(dynamicColumnWidth);
293297
}
294298

295299
var rows = Children.OfType<TableRow>().ToArray();
@@ -636,7 +640,12 @@ protected virtual Point ComputeColumnWidths(TableRow[]? rows = null)
636640
var actualWidth = 0;
637641
var actualHeight = 0;
638642
var columnLimit = Math.Min(columnCount, requestedWidths.Length);
639-
var computedColumnWidths = new int[Math.Max(_computedColumnWidths.Capacity, columnLimit)];
643+
var computedColumnWidths = new int[Math.Max(_computedColumnWidths.Count, columnLimit)];
644+
var defaultColumnWidth = rows.Length > 0
645+
? 0
646+
: (availableWidth - _cellSpacing.X * computedColumnWidths.Length) /
647+
Math.Max(1, computedColumnWidths.Length);
648+
Array.Fill(computedColumnWidths, defaultColumnWidth);
640649
foreach (var row in rows)
641650
{
642651
var rowWidth = 0;
@@ -678,20 +687,22 @@ protected virtual Point ComputeColumnWidths(TableRow[]? rows = null)
678687
return new Point(actualWidth, actualHeight);
679688
}
680689

681-
public void DoSizeToContents(bool width = false, bool height = true)
690+
public void DoSizeToContents(bool resizeX = false, bool resizeY = true)
682691
{
683692
var rows = Children.OfType<TableRow>().ToArray();
684693
if (_fitRowHeightToContents)
685694
{
686695
foreach (var row in rows)
687696
{
688-
row.SizeToChildren(resizeX: width, resizeY: height, recursive: true);
697+
row.SizeToChildren(resizeX: resizeX, resizeY: resizeY, recursive: true);
689698
}
690699
}
691700

692701
var size = ComputeColumnWidths(rows);
693702

694-
SetSize(size.X, size.Y);
703+
var width = resizeX ? size.X : Width;
704+
var height = resizeY ? size.Y : Height;
705+
SetSize(width, height);
695706

696707
InvalidateParent();
697708
}

Intersect.Client.Framework/Gwen/Control/Layout/TableRow.cs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public partial class TableRow : Base, IColorableText
2626

2727
private bool mEvenRow;
2828

29-
private int mMaximumColumns;
29+
private int _maximumColumns;
3030

3131
private GameFont? _font;
3232

@@ -147,8 +147,8 @@ public GameFont? Font
147147

148148
public int MaximumColumns
149149
{
150-
get => mMaximumColumns;
151-
set => SetAndDoIfChanged(ref mMaximumColumns, value, ComputeColumns);
150+
get => _maximumColumns;
151+
set => SetAndDoIfChanged(ref _maximumColumns, value, ComputeColumns);
152152
}
153153

154154
/// <summary>
@@ -290,6 +290,13 @@ protected override void OnSizeChanged(Point oldSize, Point newSize)
290290
{
291291
base.OnSizeChanged(oldSize, newSize);
292292

293+
ApplicationContext.CurrentContext.Logger.LogTrace(
294+
"Table row {CanonicalName} resized from {OldSize} to {NewSize}",
295+
CanonicalName,
296+
oldSize,
297+
newSize
298+
);
299+
293300
if (oldSize.X == newSize.X)
294301
{
295302
return;
@@ -332,23 +339,42 @@ protected override void OnChildSizeChanged(Base child, Point oldChildSize, Point
332339
return;
333340
}
334341

335-
// ApplicationContext.CurrentContext.Logger.LogTrace(
336-
// "Table row child {CanonicalName} resized from {OldChildSize} to {NewChildSize}",
337-
// childCanonicalName,
338-
// oldChildSize,
339-
// newChildSize
340-
// );
342+
ApplicationContext.CurrentContext.Logger.LogTrace(
343+
"Table row child {CanonicalName} resized from {OldChildSize} to {NewChildSize}",
344+
childCanonicalName,
345+
oldChildSize,
346+
newChildSize
347+
);
341348
}
342349

343350
protected virtual void ComputeColumns(int oldValue, int columnCount)
344351
{
345352
_columns.Capacity = Math.Max(_columns.Capacity, columnCount);
346353
_columnTextAlignments.Capacity = Math.Max(_columnTextAlignments.Capacity, columnCount);
347354

355+
var computedColumnWidths = _computedColumnWidths.ToArray();
356+
if (computedColumnWidths.Length < columnCount)
357+
{
358+
var originalLength = computedColumnWidths.Length;
359+
Array.Resize(ref computedColumnWidths, columnCount);
360+
Array.Fill(computedColumnWidths, 0, originalLength, columnCount - originalLength);
361+
}
362+
363+
var fixedColumnWidthSum = computedColumnWidths.Sum();
364+
var fixedWithColumnCount = computedColumnWidths.Count(width => width > 0);
365+
var remainingWidth = InnerWidth - fixedColumnWidthSum;
366+
var dynamicWidthColumnCount = columnCount - fixedWithColumnCount;
367+
var dynamicColumnWidth = remainingWidth / Math.Max(1, dynamicWidthColumnCount);
368+
348369
while (_columns.Count < columnCount)
349370
{
350371
var columnIndex = _columns.Count;
351-
var computedColumnWidth = _computedColumnWidths.Skip(columnIndex).FirstOrDefault();
372+
var computedColumnWidth = computedColumnWidths.Skip(columnIndex).FirstOrDefault();
373+
if (computedColumnWidth < 1)
374+
{
375+
computedColumnWidth = dynamicColumnWidth;
376+
}
377+
352378
var column = new TableCell(this, name: $"Column{_columns.Count}")
353379
{
354380
AutoSizeToContents = false,
@@ -497,6 +523,11 @@ public void SetColumnWidth(int columnIndex, int width)
497523
/// <param name="text">Text to set.</param>
498524
public void SetCellText(int column, string text)
499525
{
526+
if (_columns.Count < _columnCount)
527+
{
528+
ComputeColumns(0, _columnCount);
529+
}
530+
500531
var label = _columns[column];
501532
label.Text = text;
502533
}

Intersect.Client.Framework/Gwen/ControlInternal/Text.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Text(Base parent, string? name = default) : base(parent, name)
4242
MarginOutlineColor = DefaultMarginOutlineColor;
4343

4444
_lastParentInnerWidth = SelectWidthFrom(parent);
45-
parent.BoundsChanged += ParentOnBoundsChanged;
45+
parent.SizeChanged += ParentOnSizeChanged;
4646
}
4747

4848
protected override void OnSizeChanged(Point oldSize, Point newSize)
@@ -55,7 +55,7 @@ protected override void OnBoundsChanged(Rectangle oldBounds, Rectangle newBounds
5555
base.OnBoundsChanged(oldBounds, newBounds);
5656
}
5757

58-
private void ParentOnBoundsChanged(Base @base, ValueChangedEventArgs<Rectangle> eventArgs)
58+
private void ParentOnSizeChanged(Base @base, ValueChangedEventArgs<Point> eventArgs)
5959
{
6060
var newSelectedWidth = SelectWidthFrom(@base);
6161
if (_lastParentInnerWidth == newSelectedWidth)

0 commit comments

Comments
 (0)