Inherited GridColumn width
This commit is contained in:
parent
939fe9ddca
commit
16f4451803
4 changed files with 104 additions and 54 deletions
|
@ -9,43 +9,64 @@ import java.util.List;
|
|||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class GridColumn extends GridContainer {
|
||||
GridColumn(double width) {
|
||||
super(width);
|
||||
}
|
||||
|
||||
GridColumn(double width, GridLayout.GridValueType widthType) {
|
||||
super(width, widthType);
|
||||
}
|
||||
|
||||
public GridRow addRow() {
|
||||
return addRow(VerticalAlignment.TOP);
|
||||
}
|
||||
|
||||
public GridRow addRow(VerticalAlignment align) {
|
||||
GridRow row = new GridRow(1.0, GridLayout.GridValueType.PERCENTAGE, align);
|
||||
this.cells.add(row);
|
||||
return row;
|
||||
}
|
||||
|
||||
public void addSpacerRow(){
|
||||
this.addSpacerRow(4);
|
||||
}
|
||||
|
||||
public void addSpacerRow(int height){
|
||||
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){
|
||||
int height = 0;
|
||||
int top = inTop;
|
||||
for (GridCellDefinition row : cells) {
|
||||
GridElement element = row.buildElement(width, 0, 1, left, top, collector);
|
||||
top += element.height;
|
||||
height += element.height;
|
||||
GridColumn(double width) {
|
||||
super(width);
|
||||
}
|
||||
|
||||
GridColumn(double width, GridLayout.GridValueType widthType) {
|
||||
super(width, widthType);
|
||||
}
|
||||
|
||||
public GridRow addRow() {
|
||||
return addRow(VerticalAlignment.TOP);
|
||||
}
|
||||
|
||||
public GridRow addRow(VerticalAlignment align) {
|
||||
GridRow row = new GridRow(1.0, widthType==GridValueType.INHERIT?GridValueType.INHERIT:GridLayout.GridValueType.PERCENTAGE, align);
|
||||
this.cells.add(row);
|
||||
return row;
|
||||
}
|
||||
|
||||
|
||||
public void addSpacerRow() {
|
||||
this.addSpacerRow(4);
|
||||
}
|
||||
|
||||
public void addSpacerRow(int height) {
|
||||
GridCell cell = new GridCell(1.0, height, GridValueType.PERCENTAGE, null, null);
|
||||
this.cells.add(cell);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int calculateWidth(final int parentWidth){
|
||||
if (widthType == GridValueType.INHERIT) {
|
||||
return cells.stream()
|
||||
.filter(row->row.widthType == GridValueType.INHERIT)
|
||||
.map(row -> row.buildElement(0, 0, 1, 0, 0, null).width)
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,18 +22,19 @@ abstract class GridCellDefinition {
|
|||
this.widthType = widthType;
|
||||
}
|
||||
|
||||
public final int calculateWidth(final int parentWidth){
|
||||
public
|
||||
int calculateWidth(final int parentWidth){
|
||||
if (widthType == GridLayout.GridValueType.CONSTANT) {
|
||||
return (int) this.width;
|
||||
} else if (widthType == GridValueType.PERCENTAGE) {
|
||||
return (int) (this.width * parentWidth);
|
||||
} else {
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
if (collector!=null) {
|
||||
|
@ -82,11 +83,11 @@ abstract class GridContainer extends GridCellDefinition{
|
|||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class GridLayout extends GridColumn {
|
||||
public static final int COLOR_WHITE = 0x00FFFFFF;
|
||||
public static final int COLOR_RED = 0x00FF0000;
|
||||
public static final int COLOR_GREEN = 0x0000FF00;
|
||||
public static final int COLOR_BLUE = 0x000000FF;
|
||||
public static final int COLOR_GRAY = 0x007F7F7F;
|
||||
public static final int COLOR_WHITE = 0xFFFFFFFF;
|
||||
public static final int COLOR_RED = 0xFFFF0000;
|
||||
public static final int COLOR_GREEN = 0xFF00FF00;
|
||||
public static final int COLOR_BLUE = 0xFF0000FF;
|
||||
public static final int COLOR_GRAY = 0xFF7F7F7F;
|
||||
|
||||
public final GridScreen screen;
|
||||
public final int screenHeight;
|
||||
|
@ -169,8 +170,27 @@ public class GridLayout extends GridColumn {
|
|||
public static enum Alignment {
|
||||
LEFT, CENTER, RIGHT
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines how a measurement value is interpreted
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -159,7 +159,7 @@ public class GridRow extends GridContainer {
|
|||
}
|
||||
|
||||
public GridColumn addFiller(float portion) {
|
||||
GridColumn cell = new GridColumn(portion, GridValueType.AUTO);
|
||||
GridColumn cell = new GridColumn(portion, GridValueType.FILL);
|
||||
this.cells.add(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) {
|
||||
int height = 0;
|
||||
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()
|
||||
.filter(col -> col.widthType != GridValueType.AUTO)
|
||||
.map(col -> col.calculateWidth(width))
|
||||
.filter(col -> col.widthType != GridValueType.FILL)
|
||||
.map(col -> col.calculateWidth(inheritedWidth))
|
||||
.reduce(0, (p, c) -> p + c);
|
||||
final float autoWidthSum = cells.stream()
|
||||
.filter(col -> col.widthType == GridValueType.AUTO)
|
||||
.filter(col -> col.widthType == GridValueType.FILL)
|
||||
.map(col -> col.width)
|
||||
.reduce(0.0f, (p, c) -> p + c);
|
||||
final int autoWidth = width - fixedWidth;
|
||||
|
|
|
@ -109,7 +109,7 @@ public class ProgressScreen extends GridScreen implements ProgressListener {
|
|||
private ProgressLogoRender progressImage;
|
||||
private int currentProgress = 0;
|
||||
public boolean shouldCloseOnEsc() {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public Component getProgressComponent(){
|
||||
|
@ -131,7 +131,7 @@ public class ProgressScreen extends GridScreen implements ProgressListener {
|
|||
row.addSpacer();
|
||||
|
||||
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.addSpacerRow();
|
||||
progress = textCol.addRow().addString(getProgressComponent(), GridLayout.COLOR_GRAY, Alignment.LEFT, this);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue