Create *Grid layouts

This commit is contained in:
2022-08-25 00:59:02 +02:00
parent 18010cbb0f
commit cbf0eefb3f
4 changed files with 221 additions and 0 deletions

View File

@@ -107,6 +107,14 @@ public final class WindowManager extends BaseWidget {
return windows.size();
}
public boolean isEmpty() {
return windows.isEmpty();
}
public Window top() {
return windows.peekLast();
}
@Override
public void update(float dt) {
switch (updateMode) {

View File

@@ -0,0 +1,102 @@
package com.bartlomiejpluta.base.lib.gui;
import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.gui.GUI;
import com.bartlomiejpluta.base.api.screen.Screen;
import lombok.NonNull;
public class HGridLayout extends BaseContainer {
protected float offsetX = 0.0f;
protected float offsetY = 0.0f;
private int rows = 2;
private float[] heights = new float[rows];
public HGridLayout(Context context, GUI gui) {
super(context, gui);
}
public void setRows(@NonNull Integer rows) {
this.rows = rows;
heights = new float[rows];
}
@Override
protected float getContentWidth() {
int lastId = children.size() - 1;
var totalWidth = 0f;
var maxWidth = 0f;
var width = 0f;
var i = 0;
for (var child : children) {
width = child.getMarginLeft() + child.getWidth() + child.getMarginRight();
if (maxWidth < width) {
maxWidth = width;
}
if (i % rows == rows - 1 || i == lastId) {
totalWidth += maxWidth;
maxWidth = 0f;
}
++i;
}
return totalWidth;
}
@Override
protected float getContentHeight() {
var maxHeight = 0f;
for (var height : heights) {
maxHeight += height;
}
return maxHeight;
}
@Override
public void draw(Screen screen, GUI gui) {
var currentX = x + paddingLeft + offsetX;
var currentY = y + paddingTop + offsetY;
var maxWidth = 0f;
var i = 0;
for (var child : children) {
var index = i % rows;
var height = child.getMarginTop() + child.getHeight() + child.getMarginBottom();
if (heights[index] < height) {
heights[index] = height;
}
++i;
}
var row = 0;
for (var child : children) {
var childAbsX = currentX + child.getMarginLeft();
var childAbsY = currentY + child.getMarginTop();
child.setX(childAbsX);
child.setY(childAbsY);
if (row < rows - 1) {
currentY += heights[row];
var currentWidth = child.getMarginLeft() + child.getWidth() + child.getMarginRight();
if (maxWidth < currentWidth) {
maxWidth = currentWidth;
}
++row;
} else {
row = 0;
currentY = y + paddingTop + offsetY;
currentX += maxWidth;
maxWidth = 0f;
}
child.draw(screen, gui);
}
}
}

View File

@@ -57,6 +57,15 @@ public class IconView extends BaseComponent {
iconSetColumn = icon.getIconSetColumn();
}
public void setIconSet(String iconSetUid) {
if (iconSetUid == null) {
iconSet = null;
return;
}
iconSet = gui.getIconSet(iconSetUid);
}
public void setScale(@NonNull Float scale) {
this.scaleX = scale;
this.scaleY = scale;

View File

@@ -0,0 +1,102 @@
package com.bartlomiejpluta.base.lib.gui;
import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.gui.GUI;
import com.bartlomiejpluta.base.api.screen.Screen;
import lombok.NonNull;
public class VGridLayout extends BaseContainer {
protected float offsetX = 0.0f;
protected float offsetY = 0.0f;
private int columns = 2;
private float[] widths = new float[columns];
public VGridLayout(Context context, GUI gui) {
super(context, gui);
}
public void setColumns(@NonNull Integer columns) {
this.columns = columns;
widths = new float[columns];
}
@Override
protected float getContentHeight() {
int lastId = children.size() - 1;
var totalHeight = 0f;
var maxHeight = 0f;
var height = 0f;
var i = 0;
for (var child : children) {
height = child.getMarginTop() + child.getHeight() + child.getMarginBottom();
if (maxHeight < height) {
maxHeight = height;
}
if (i % columns == columns - 1 || i == lastId) {
totalHeight += maxHeight;
maxHeight = 0f;
}
++i;
}
return totalHeight;
}
@Override
protected float getContentWidth() {
var maxWidth = 0f;
for (var width : widths) {
maxWidth += width;
}
return maxWidth;
}
@Override
public void draw(Screen screen, GUI gui) {
var currentX = x + paddingLeft + offsetX;
var currentY = y + paddingTop + offsetY;
var maxHeight = 0f;
var i = 0;
for (var child : children) {
var index = i % columns;
var width = child.getMarginLeft() + child.getWidth() + child.getMarginRight();
if (widths[index] < width) {
widths[index] = width;
}
++i;
}
var column = 0;
for (var child : children) {
var childAbsX = currentX + child.getMarginLeft();
var childAbsY = currentY + child.getMarginTop();
child.setX(childAbsX);
child.setY(childAbsY);
if (column < columns - 1) {
currentX += widths[column];
var currentHeight = child.getMarginTop() + child.getHeight() + child.getMarginBottom();
if (maxHeight < currentHeight) {
maxHeight = currentHeight;
}
++column;
} else {
column = 0;
currentX = x + paddingLeft + offsetX;
currentY += maxHeight;
maxHeight = 0f;
}
child.draw(screen, gui);
}
}
}