From cbd35c5c37efac06af66d64c0e2809941bcf14c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Thu, 11 Mar 2021 13:19:06 +0100 Subject: [PATCH] Create Size Mode which includes modes: NORMAL, WRAP_CONTENT and MATCH_PARENT --- .../base/api/game/gui/base/SizeMode.java | 7 ++ .../base/api/game/gui/base/Widget.java | 16 ++++ .../api/game/gui/component/BaseContainer.java | 4 +- .../api/game/gui/component/Component.java | 75 +++++++++++++++++++ .../base/api/game/gui/component/HLayout.java | 8 +- .../base/api/game/gui/component/VLayout.java | 8 +- 6 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/SizeMode.java diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/SizeMode.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/SizeMode.java new file mode 100644 index 00000000..f283c45e --- /dev/null +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/SizeMode.java @@ -0,0 +1,7 @@ +package com.bartlomiejpluta.base.api.game.gui.base; + +public enum SizeMode { + NORMAL, + MATCH_PARENT, + WRAP_CONTENT +} diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/Widget.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/Widget.java index d6363d76..e94a549c 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/Widget.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/base/Widget.java @@ -19,6 +19,22 @@ public interface Widget { 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 rightLeft, float bottom); diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/BaseContainer.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/BaseContainer.java index d3a6b778..4b449f93 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/BaseContainer.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/BaseContainer.java @@ -22,7 +22,7 @@ public abstract class BaseContainer extends Component implements Container { var theWidestChild = 0.0f; for (var child : children) { - var width = child.getMarginLeft() + child.getWidth() + child.getMarginRight(); + var width = child.getMarginLeft() + child.getActualWidth() + child.getMarginRight(); if (width > theWidestChild) { theWidestChild = width; } @@ -35,7 +35,7 @@ public abstract class BaseContainer extends Component implements Container { var theHighestChild = 0.0f; for (var child : children) { - var height = child.getMarginTop() + child.getHeight() + child.getMarginBottom(); + var height = child.getMarginTop() + child.getActualHeight() + child.getMarginBottom(); if (height > theHighestChild) { theHighestChild = height; } diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/Component.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/Component.java index 3aa10f9f..6b90343b 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/Component.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/Component.java @@ -1,13 +1,20 @@ 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; public abstract class Component implements Widget { protected Widget parent; + protected SizeMode widthMode = SizeMode.NORMAL; + protected SizeMode heightMode = SizeMode.NORMAL; + protected float x; protected float y; + protected float width; + protected float height; + protected float marginTop; protected float marginRight; protected float marginBottom; @@ -18,6 +25,74 @@ public abstract class Component implements Widget { protected float paddingBottom; 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 public Widget getParent() { return parent; diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/HLayout.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/HLayout.java index 901d865f..d607774d 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/HLayout.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/HLayout.java @@ -6,13 +6,13 @@ import com.bartlomiejpluta.base.api.game.screen.Screen; public class HLayout extends BaseContainer { @Override - public float getWidth() { - return paddingLeft + sumChildrenWidth() + paddingRight; + protected float getContentWidth() { + return sumChildrenWidth(); } @Override - public float getHeight() { - return paddingTop + maxChildrenHeight() + paddingBottom; + protected float getContentHeight() { + return maxChildrenHeight(); } @Override diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/VLayout.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/VLayout.java index d11746eb..d7656464 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/VLayout.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/gui/component/VLayout.java @@ -6,13 +6,13 @@ import com.bartlomiejpluta.base.api.game.screen.Screen; public class VLayout extends BaseContainer { @Override - public float getWidth() { - return paddingLeft + maxChildrenWidth() + paddingRight; + protected float getContentWidth() { + return maxChildrenWidth(); } @Override - public float getHeight() { - return paddingTop + sumChildrenHeight() + paddingBottom; + protected float getContentHeight() { + return sumChildrenHeight(); } @Override