Files
MonoGame.Extended/Source/MonoGame.Extended.Gui/Controls/GuiUniformGrid.cs
T
Dylan WilsonandGitHub b3a631aff5 Gui dialogs (#410)
* started refactoring for gui windows

* a bit more work on gui windows

* fixed some bugs and broken build stuff

* fixed some bugs in gui controls

* a little code cleanup

* got gui dialogs positioning and experimenting with named styles by type

* apply control styles directly in the constructor of controls

* got everything compiling again

* all gui controls have skinnable constructors

* rework uniform grid desired size calculations

* turns out gui layouts are complicated

* include pdb files in nuget packages

* fixed a bug in find nearest glyph

* custom control styling

* added a range constructor overload that takes one value

* rethinking control desired size behaviour

* made particle emitters more editable at runtime

* moved the auto trigger code to the particle effect instead of the emitters

* tweaks

* changed the particle modifer interface to an abstract base with an optional name

* changed the particle modifer interface to an abstract base with an optional name

* an attempt at creating a gui combo box

* rolled back the nested control experiment

* pretty good (but not perfect) combo boxes

* tweaks

* name property bindings on lists

* removed and cleaned up the hacks from the gui combo box

* minor combo box styling tweaks

* minor bug fix in gui combo box

* experiments with data binding

* fixed a mistake on the texture region service interface

* working on the ability to serialize particles

* finished particle serialization

* more robust range deserialization

* added editor browable attributes

* fixed some broken demos

* made the particle modifiers editable at runtime

* fixed some issues with particle serialization

* fixed some issues with interpolators
2017-07-22 21:22:13 +10:00

85 lines
3.0 KiB
C#

using System;
using System.Linq;
namespace MonoGame.Extended.Gui.Controls
{
public class GuiUniformGrid : GuiLayoutControl
{
public GuiUniformGrid()
: base(null)
{
}
public GuiUniformGrid(GuiSkin skin)
: base(skin)
{
}
public int Columns { get; set; }
public int Rows { get; set; }
protected override Size2 CalculateDesiredSize(IGuiContext context, Size2 availableSize)
{
var desiredSize = CalculateGridInfo(context, availableSize).MinCellSize;
return desiredSize;
}
public override void Layout(IGuiContext context, RectangleF rectangle)
{
var gridInfo = CalculateGridInfo(context, rectangle.Size);
var columnIndex = 0;
var rowIndex = 0;
var cellWidth = HorizontalAlignment == HorizontalAlignment.Stretch ? gridInfo.MaxCellWidth : gridInfo.MinCellWidth;
var cellHeight = VerticalAlignment == VerticalAlignment.Stretch ? gridInfo.MaxCellHeight : gridInfo.MinCellHeight;
foreach (var control in Controls)
{
var x = columnIndex * cellWidth;
var y = rowIndex * cellHeight;
PlaceControl(context, control, x, y, cellWidth, cellHeight);
columnIndex++;
if (columnIndex > gridInfo.Columns - 1)
{
columnIndex = 0;
rowIndex++;
}
}
}
private struct GridInfo
{
public float MinCellWidth;
public float MinCellHeight;
public float MaxCellWidth;
public float MaxCellHeight;
public float Columns;
public float Rows;
public Size2 MinCellSize => new Size2(MinCellWidth * Columns, MinCellHeight * Rows);
}
private GridInfo CalculateGridInfo(IGuiContext context, Size2 availableSize)
{
var columns = Columns == 0 ? (int)Math.Ceiling(Math.Sqrt(Controls.Count)) : Columns;
var rows = Rows == 0 ? (int)Math.Ceiling((float)Controls.Count / columns) : Rows;
var maxCellWidth = availableSize.Width / columns;
var maxCellHeight = availableSize.Height / rows;
var sizes = Controls
.Select(control => GuiLayoutHelper.GetSizeWithMargins(control, context, new Size2(maxCellWidth, maxCellHeight)))
.ToArray();
var maxControlWidth = sizes.Length == 0 ? 0 : sizes.Max(s => s.Width);
var maxControlHeight = sizes.Length == 0 ? 0 : sizes.Max(s => s.Height);
return new GridInfo
{
Columns = columns,
Rows = rows,
MinCellWidth = Math.Min(maxControlWidth, maxCellWidth),
MinCellHeight = Math.Min(maxControlHeight, maxCellHeight),
MaxCellWidth = maxCellWidth,
MaxCellHeight = maxCellHeight
};
}
}
}