Inherited GridColumn width

This commit is contained in:
Frank 2021-08-24 14:22:14 +02:00
parent 939fe9ddca
commit 16f4451803
4 changed files with 104 additions and 54 deletions

View file

@ -9,43 +9,64 @@ import java.util.List;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class GridColumn extends GridContainer { public class GridColumn extends GridContainer {
GridColumn(double width) { GridColumn(double width) {
super(width); super(width);
} }
GridColumn(double width, GridLayout.GridValueType widthType) { GridColumn(double width, GridLayout.GridValueType widthType) {
super(width, widthType); super(width, widthType);
} }
public GridRow addRow() { public GridRow addRow() {
return addRow(VerticalAlignment.TOP); return addRow(VerticalAlignment.TOP);
} }
public GridRow addRow(VerticalAlignment align) { public GridRow addRow(VerticalAlignment align) {
GridRow row = new GridRow(1.0, GridLayout.GridValueType.PERCENTAGE, align); GridRow row = new GridRow(1.0, widthType==GridValueType.INHERIT?GridValueType.INHERIT:GridLayout.GridValueType.PERCENTAGE, align);
this.cells.add(row); this.cells.add(row);
return row; return row;
} }
public void addSpacerRow(){
this.addSpacerRow(4); public void addSpacerRow() {
} this.addSpacerRow(4);
}
public void addSpacerRow(int height){
GridCell cell = new GridCell(1.0, height, GridValueType.PERCENTAGE, null, null); public void addSpacerRow(int height) {
this.cells.add(cell); GridCell cell = new GridCell(1.0, height, GridValueType.PERCENTAGE, null, null);
} this.cells.add(cell);
}
@Override
protected GridElement buildElementAt(int left, int inTop, int width, final List<GridElement> collector){ @Override
int height = 0; public int calculateWidth(final int parentWidth){
int top = inTop; if (widthType == GridValueType.INHERIT) {
for (GridCellDefinition row : cells) { return cells.stream()
GridElement element = row.buildElement(width, 0, 1, left, top, collector); .filter(row->row.widthType == GridValueType.INHERIT)
top += element.height; .map(row -> row.buildElement(0, 0, 1, 0, 0, null).width)
height += element.height; .reduce(0, (p, c) -> Math.max(p, c));
} else {
return super.calculateWidth(parentWidth);
}
}
@Override
protected GridElement buildElementAt(int left, int inTop, int width, final List<GridElement> collector) {
int height = 0;
int top = inTop;
if (widthType == GridValueType.INHERIT) {
width = calculateWidth(width);
} }
return new GridElement(left, inTop, width, height); for (GridCellDefinition row : cells) {
} GridElement element = row.buildElement(width, 0, 1, left, top, collector);
top += element.height;
height += element.height;
}
return new GridElement(left, inTop, width, height);
}
} }

View file

@ -22,18 +22,19 @@ abstract class GridCellDefinition {
this.widthType = widthType; this.widthType = widthType;
} }
public final int calculateWidth(final int parentWidth){ public
int calculateWidth(final int parentWidth){
if (widthType == GridLayout.GridValueType.CONSTANT) { if (widthType == GridLayout.GridValueType.CONSTANT) {
return (int) this.width; return (int) this.width;
} else if (widthType == GridValueType.PERCENTAGE) { } else if (widthType == GridValueType.PERCENTAGE) {
return (int) (this.width * parentWidth); return (int) (this.width * parentWidth);
} else { } else {
return 0; return 0;
} }
} }
final GridElement buildElement(final int parentWidth, final int autoWidth, final float autoWidthSum, int left, final int top, final List<GridElement> collector) { final GridElement buildElement(final int parentWidth, final int autoWidth, final float autoWidthSum, int left, final int top, final List<GridElement> collector) {
final int width = widthType == GridValueType.AUTO?(int)((this.width/autoWidthSum)*autoWidth):calculateWidth(parentWidth); final int width = widthType == GridValueType.FILL ?(int)((this.width/autoWidthSum)*autoWidth):calculateWidth(parentWidth);
final GridElement el = buildElementAt(left, top, width, collector); final GridElement el = buildElementAt(left, top, width, collector);
if (collector!=null) { if (collector!=null) {
@ -82,11 +83,11 @@ abstract class GridContainer extends GridCellDefinition{
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class GridLayout extends GridColumn { public class GridLayout extends GridColumn {
public static final int COLOR_WHITE = 0x00FFFFFF; public static final int COLOR_WHITE = 0xFFFFFFFF;
public static final int COLOR_RED = 0x00FF0000; public static final int COLOR_RED = 0xFFFF0000;
public static final int COLOR_GREEN = 0x0000FF00; public static final int COLOR_GREEN = 0xFF00FF00;
public static final int COLOR_BLUE = 0x000000FF; public static final int COLOR_BLUE = 0xFF0000FF;
public static final int COLOR_GRAY = 0x007F7F7F; public static final int COLOR_GRAY = 0xFF7F7F7F;
public final GridScreen screen; public final GridScreen screen;
public final int screenHeight; public final int screenHeight;
@ -169,8 +170,27 @@ public class GridLayout extends GridColumn {
public static enum Alignment { public static enum Alignment {
LEFT, CENTER, RIGHT LEFT, CENTER, RIGHT
} }
/**
* Determines how a measurement value is interpreted
*/
public static enum GridValueType { public static enum GridValueType {
CONSTANT, PERCENTAGE, AUTO; /**
* The value is a constant pixel size
*/
CONSTANT,
/**
* The Value is relative to the parent size
*/
PERCENTAGE,
/**
* The value will be set to fill up the remaining space (i.e. when this is applied to a width of a row element,
* a {@link #FILL}-type may be used to right align (FILL - CONSTANT) or center (FILL - CONSTANT - FILL) elements.
*/
FILL,
/**
* Calculate size based on child-elements
*/
INHERIT;
} }
} }

View file

@ -159,7 +159,7 @@ public class GridRow extends GridContainer {
} }
public GridColumn addFiller(float portion) { public GridColumn addFiller(float portion) {
GridColumn cell = new GridColumn(portion, GridValueType.AUTO); GridColumn cell = new GridColumn(portion, GridValueType.FILL);
this.cells.add(cell); this.cells.add(cell);
return cell; return cell;
} }
@ -224,12 +224,21 @@ public class GridRow extends GridContainer {
protected GridElement buildElementAt(int inLeft, int top, int width, final List<GridElement> collector) { protected GridElement buildElementAt(int inLeft, int top, int width, final List<GridElement> collector) {
int height = 0; int height = 0;
int left = inLeft; int left = inLeft;
if (widthType == GridValueType.INHERIT) {
final int originalWidth = width;
width = cells.stream()
.filter(row -> row.widthType == GridValueType.CONSTANT || row.widthType == GridValueType.INHERIT)
.map(row -> row.buildElement(0, 0, 1, 0, 0, null).width)
.reduce(0, (p, c) -> p+c);
}
final int inheritedWidth = width;
final int fixedWidth = cells.stream() final int fixedWidth = cells.stream()
.filter(col -> col.widthType != GridValueType.AUTO) .filter(col -> col.widthType != GridValueType.FILL)
.map(col -> col.calculateWidth(width)) .map(col -> col.calculateWidth(inheritedWidth))
.reduce(0, (p, c) -> p + c); .reduce(0, (p, c) -> p + c);
final float autoWidthSum = cells.stream() final float autoWidthSum = cells.stream()
.filter(col -> col.widthType == GridValueType.AUTO) .filter(col -> col.widthType == GridValueType.FILL)
.map(col -> col.width) .map(col -> col.width)
.reduce(0.0f, (p, c) -> p + c); .reduce(0.0f, (p, c) -> p + c);
final int autoWidth = width - fixedWidth; final int autoWidth = width - fixedWidth;

View file

@ -109,7 +109,7 @@ public class ProgressScreen extends GridScreen implements ProgressListener {
private ProgressLogoRender progressImage; private ProgressLogoRender progressImage;
private int currentProgress = 0; private int currentProgress = 0;
public boolean shouldCloseOnEsc() { public boolean shouldCloseOnEsc() {
return false; return true;
} }
public Component getProgressComponent(){ public Component getProgressComponent(){
@ -131,7 +131,7 @@ public class ProgressScreen extends GridScreen implements ProgressListener {
row.addSpacer(); row.addSpacer();
int textWidth = Math.max(getWidth(description), getWidth(getProgressComponent(100))); int textWidth = Math.max(getWidth(description), getWidth(getProgressComponent(100)));
GridColumn textCol = row.addColumn(textWidth, GridValueType.CONSTANT); GridColumn textCol = row.addColumn(0, GridValueType.INHERIT);
textCol.addRow().addString(description, this); textCol.addRow().addString(description, this);
textCol.addSpacerRow(); textCol.addSpacerRow();
progress = textCol.addRow().addString(getProgressComponent(), GridLayout.COLOR_GRAY, Alignment.LEFT, this); progress = textCol.addRow().addString(getProgressComponent(), GridLayout.COLOR_GRAY, Alignment.LEFT, this);