Create Size Mode which includes modes: NORMAL, WRAP_CONTENT and MATCH_PARENT

This commit is contained in:
2021-03-11 13:19:06 +01:00
parent d3d440e588
commit cbd35c5c37
6 changed files with 108 additions and 10 deletions

View File

@@ -0,0 +1,7 @@
package com.bartlomiejpluta.base.api.game.gui.base;
public enum SizeMode {
NORMAL,
MATCH_PARENT,
WRAP_CONTENT
}

View File

@@ -19,6 +19,22 @@ public interface Widget {
float getHeight(); float getHeight();
void setWidth(float width);
void setHeight(float height);
void setSize(float width, float height);
SizeMode getWidthMode();
void setWidthMode(SizeMode mode);
SizeMode getHeightMode();
void setHeightMode(SizeMode mode);
void setSizeMode(SizeMode widthMode, SizeMode heightMode);
void setMargin(float top, float right, float bottom, float left); void setMargin(float top, float right, float bottom, float left);
void setMargin(float top, float rightLeft, float bottom); void setMargin(float top, float rightLeft, float bottom);

View File

@@ -22,7 +22,7 @@ public abstract class BaseContainer extends Component implements Container {
var theWidestChild = 0.0f; var theWidestChild = 0.0f;
for (var child : children) { for (var child : children) {
var width = child.getMarginLeft() + child.getWidth() + child.getMarginRight(); var width = child.getMarginLeft() + child.getActualWidth() + child.getMarginRight();
if (width > theWidestChild) { if (width > theWidestChild) {
theWidestChild = width; theWidestChild = width;
} }
@@ -35,7 +35,7 @@ public abstract class BaseContainer extends Component implements Container {
var theHighestChild = 0.0f; var theHighestChild = 0.0f;
for (var child : children) { for (var child : children) {
var height = child.getMarginTop() + child.getHeight() + child.getMarginBottom(); var height = child.getMarginTop() + child.getActualHeight() + child.getMarginBottom();
if (height > theHighestChild) { if (height > theHighestChild) {
theHighestChild = height; theHighestChild = height;
} }

View File

@@ -1,13 +1,20 @@
package com.bartlomiejpluta.base.api.game.gui.component; package com.bartlomiejpluta.base.api.game.gui.component;
import com.bartlomiejpluta.base.api.game.gui.base.SizeMode;
import com.bartlomiejpluta.base.api.game.gui.base.Widget; import com.bartlomiejpluta.base.api.game.gui.base.Widget;
public abstract class Component implements Widget { public abstract class Component implements Widget {
protected Widget parent; protected Widget parent;
protected SizeMode widthMode = SizeMode.NORMAL;
protected SizeMode heightMode = SizeMode.NORMAL;
protected float x; protected float x;
protected float y; protected float y;
protected float width;
protected float height;
protected float marginTop; protected float marginTop;
protected float marginRight; protected float marginRight;
protected float marginBottom; protected float marginBottom;
@@ -18,6 +25,74 @@ public abstract class Component implements Widget {
protected float paddingBottom; protected float paddingBottom;
protected float paddingLeft; protected float paddingLeft;
protected abstract float getContentWidth();
protected abstract float getContentHeight();
@Override
public float getWidth() {
return widthMode == SizeMode.MATCH_PARENT
? (parent != null ? parent.getWidth() : 0)
: getActualWidth();
}
protected float getActualWidth() {
return paddingLeft + (widthMode == SizeMode.NORMAL ? width : getContentWidth()) + paddingRight;
}
@Override
public float getHeight() {
return heightMode == SizeMode.MATCH_PARENT
? (parent != null ? parent.getHeight() : 0)
: getActualHeight();
}
protected float getActualHeight() {
return paddingTop + (heightMode == SizeMode.NORMAL ? height : getContentHeight()) + paddingBottom;
}
@Override
public void setWidth(float width) {
this.width = width;
}
@Override
public void setHeight(float height) {
this.height = height;
}
@Override
public void setSize(float width, float height) {
this.width = width;
this.height = height;
}
@Override
public void setSizeMode(SizeMode widthMode, SizeMode heightMode) {
this.widthMode = widthMode;
this.heightMode = heightMode;
}
@Override
public SizeMode getWidthMode() {
return widthMode;
}
@Override
public void setWidthMode(SizeMode mode) {
this.widthMode = mode;
}
@Override
public SizeMode getHeightMode() {
return heightMode;
}
@Override
public void setHeightMode(SizeMode mode) {
this.heightMode = mode;
}
@Override @Override
public Widget getParent() { public Widget getParent() {
return parent; return parent;

View File

@@ -6,13 +6,13 @@ import com.bartlomiejpluta.base.api.game.screen.Screen;
public class HLayout extends BaseContainer { public class HLayout extends BaseContainer {
@Override @Override
public float getWidth() { protected float getContentWidth() {
return paddingLeft + sumChildrenWidth() + paddingRight; return sumChildrenWidth();
} }
@Override @Override
public float getHeight() { protected float getContentHeight() {
return paddingTop + maxChildrenHeight() + paddingBottom; return maxChildrenHeight();
} }
@Override @Override

View File

@@ -6,13 +6,13 @@ import com.bartlomiejpluta.base.api.game.screen.Screen;
public class VLayout extends BaseContainer { public class VLayout extends BaseContainer {
@Override @Override
public float getWidth() { protected float getContentWidth() {
return paddingLeft + maxChildrenWidth() + paddingRight; return maxChildrenWidth();
} }
@Override @Override
public float getHeight() { protected float getContentHeight() {
return paddingTop + sumChildrenHeight() + paddingBottom; return sumChildrenHeight();
} }
@Override @Override