Skip to content

Commit

Permalink
Set a minimum size on the main form
Browse files Browse the repository at this point in the history
  • Loading branch information
kythyria committed Aug 28, 2023
1 parent 246706f commit 50043f4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 51 deletions.
7 changes: 4 additions & 3 deletions PD2ModelParser/UI/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions PD2ModelParser/UI/ImportPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 70 additions & 48 deletions PD2ModelParser/UI/ImportPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,90 @@ public partial class ImportPanel : UserControl
{
class ImportPanelLayoutEngine : LayoutEngine
{
struct TableRow {
public Label label;
public Control field;
public int labelWidth;
public int minFieldWidth;
public int height;

public TableRow(Label label, Control field) {
this.label = label;
this.field = field;

var labelSize = label?.GetPreferredSize(new Size(1,1)) ?? new Size(0,0);
var fieldSize = this.field.GetPreferredSize(new Size(1,1));

this.height = Math.Max(labelSize.Height + (label?.Margin.Vertical ?? 0), fieldSize.Height + this.field.Margin.Vertical);
this.minFieldWidth = fieldSize.Width;
this.labelWidth = labelSize.Width + (label?.Margin.Horizontal ?? 0);
}
}

Label[] labels;
Control[] fields;
TableRow[] rows;

int maxLabelWidth;
int minFieldWidth;

public ImportPanelLayoutEngine(ImportPanel panel)
{

}

private void InitialAnalysis(ImportPanel panel)
{
if(this.rows != null) { return; }

this.rows = new TableRow[] {
new TableRow(panel.labelSelBaseModel, panel.baseModelFileBrowser),
new TableRow(null, panel.createNewModel),
new TableRow(panel.lblScript, panel.scriptFile),
new TableRow(panel.labelObj, panel.objectFile),
new TableRow(panel.labelPatternUV, panel.patternUVFile),
new TableRow(panel.labelAnimations, panel.animationFiles),
new TableRow(null, panel.createNewObjectsBox),
new TableRow(null, panel.importTransformsBox),
new TableRow(panel.labelRootPoint, panel.rootPoints),
new TableRow(null, panel.labelRootPointHint),
new TableRow(panel.labelSaveTo, panel.outputBox)
};

this.maxLabelWidth = this.rows.Select(i => i.labelWidth).Max();

var currY = 0;
for (var i = 0; i < rows.Length; i++)
{
var label = rows[i].label;
var labelSize = label?.GetPreferredSize(new Size(1,1)) ?? new Size(0,0);
var rowHeight = this.rows[i].height;
if (label != null)
{
var labelOffsY = (rowHeight - labelSize.Height) / 2;
label.SetBounds(this.maxLabelWidth - (labelSize.Width + label.Margin.Right), currY + labelOffsY, labelSize.Width, labelSize.Height);
}
currY += rowHeight;
}
}

public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
{
//return base.Layout(container, layoutEventArgs);
var panel = (ImportPanel)container;

//Console.WriteLine("Layout");

this.labels = new Label[] {
panel.labelSelBaseModel,
null,
panel.lblScript,
panel.labelObj,
panel.labelPatternUV,
panel.labelAnimations,
null,
null,
panel.labelRootPoint,
null,
panel.labelSaveTo
};
var panel = (ImportPanel)container;

var fields = new Control[]
{
panel.baseModelFileBrowser,
panel.createNewModel,
panel.scriptFile,
panel.objectFile,
panel.patternUVFile,
panel.animationFiles,
panel.createNewObjectsBox,
panel.importTransformsBox,
panel.rootPoints,
panel.labelRootPointHint,
panel.outputBox
};
this.InitialAnalysis(panel);

var maxLabelWidth = this.labels
.Where(i => i != null)
.Select(i => i.PreferredSize.Width + i.Margin.Horizontal ).Max();
var currY = 0;

for (var i = 0; i < fields.Length; i++)
for (var i = 0; i < rows.Length; i++)
{
var label = labels[i];
var labelSize = label?.GetPreferredSize(new Size(1,1)) ?? new Size(0,0);
var field = fields[i];
var field = rows[i].field;
var fieldSize = field.GetPreferredSize(new Size(1,1));

var rowHeight = Math.Max(labelSize.Height + (label?.Margin.Vertical ?? 0), fieldSize.Height + field.Margin.Vertical);

if (label != null)
{
var labelOffsY = (rowHeight - labelSize.Height) / 2;
label.SetBounds(maxLabelWidth - (labelSize.Width + label.Margin.Right), currY + labelOffsY, labelSize.Width, labelSize.Height);
}
var rowHeight = this.rows[i].height;

var fieldX = maxLabelWidth + field.Margin.Left;
var fieldOffsY = currY + (rowHeight - fieldSize.Height) / 2;
Expand All @@ -85,7 +107,7 @@ public override bool Layout(object container, LayoutEventArgs layoutEventArgs)

var buttonSize = panel.convert.GetPreferredSize(new Size(1, 1));
panel.convert.SetBounds(panel.convert.Margin.Left, currY + panel.convert.Margin.Top, panel.Width - panel.convert.Margin.Horizontal, buttonSize.Height);
currY += panel.convert.Bounds.Bottom + panel.convert.Margin.Bottom;
currY = panel.convert.Bounds.Bottom + panel.convert.Margin.Bottom;

//Console.WriteLine("End Layout");

Expand All @@ -94,10 +116,10 @@ public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
return true;
}

public Size GetMinimumSize()
{
throw new NotImplementedException();
}
//public Size GetMinimumSize()
//{
// panel
//}
}

private List<RootPointItem> root_point_items = new List<RootPointItem>();
Expand Down

0 comments on commit 50043f4

Please sign in to comment.