Improve and rename Scrollables to *ScrollableLayout

This commit is contained in:
2021-03-17 22:17:03 +01:00
parent dfe9d0cca4
commit d90be792bb
5 changed files with 46 additions and 85 deletions

View File

@@ -5,6 +5,8 @@ import com.bartlomiejpluta.base.api.game.gui.base.GUI;
import com.bartlomiejpluta.base.api.game.screen.Screen; import com.bartlomiejpluta.base.api.game.screen.Screen;
public class HLayout extends BaseContainer { public class HLayout extends BaseContainer {
protected float offsetX = 0.0f;
protected float offsetY = 0.0f;
public HLayout(Context context, GUI gui) { public HLayout(Context context, GUI gui) {
super(context, gui); super(context, gui);
@@ -22,8 +24,8 @@ public class HLayout extends BaseContainer {
@Override @Override
public void draw(Screen screen, GUI gui) { public void draw(Screen screen, GUI gui) {
var currentX = x + paddingLeft; var currentX = x + paddingLeft + offsetX;
var currentY = y + paddingTop; var currentY = y + paddingTop + offsetY;
for (var child : children) { for (var child : children) {
var childAbsX = currentX + child.getMarginLeft(); var childAbsX = currentX + child.getMarginLeft();

View File

@@ -11,23 +11,32 @@ import com.bartlomiejpluta.base.api.game.screen.Screen;
import static com.bartlomiejpluta.base.api.util.math.MathUtil.clamp; import static com.bartlomiejpluta.base.api.util.math.MathUtil.clamp;
import static java.lang.Math.*; import static java.lang.Math.*;
public class HScrollable extends SingleChildContainer { public class HScrollableLayout extends HLayout {
private float scroll = 0.0f; private float scroll = 0.0f;
private float scrollStep = 0.25f; private float scrollStep = 0.25f;
private float scrollSpeed = 0.1f; private float scrollSpeed = 0.1f;
private float scrollTarget = 0.0f; public HScrollableLayout(Context context, GUI gui) {
private float scrollPosition = 0.0f;
public HScrollable(Context context, GUI gui) {
super(context, gui); super(context, gui);
} }
public float getScroll() { public float getActualScroll() {
return getContentHeight() * -offsetX;
}
public int getCurrentPage() {
return (int) floor(scroll / scrollStep);
}
public int getPages() {
return (int) ceil(1 / scrollStep);
}
public float getTargetScroll() {
return scroll; return scroll;
} }
public void setScroll(Float scroll) { public void scrollTo(Float scroll) {
this.scroll = clamp(scroll, 0, 1); this.scroll = clamp(scroll, 0, 1);
} }
@@ -47,11 +56,6 @@ public class HScrollable extends SingleChildContainer {
this.scrollSpeed = clamp(scrollSpeed, 0, 1); this.scrollSpeed = clamp(scrollSpeed, 0, 1);
} }
@Override
public void remove(Component component) {
throw new UnsupportedOperationException();
}
@Override @Override
public void setSizeMode(SizeMode widthMode, SizeMode heightMode) { public void setSizeMode(SizeMode widthMode, SizeMode heightMode) {
if (widthMode == SizeMode.AUTO || heightMode == SizeMode.AUTO) { if (widthMode == SizeMode.AUTO || heightMode == SizeMode.AUTO) {
@@ -79,16 +83,6 @@ public class HScrollable extends SingleChildContainer {
super.setHeightMode(mode); super.setHeightMode(mode);
} }
@Override
protected float getContentWidth() {
return child.getMarginLeft() + child.getActualWidth() + child.getMarginRight();
}
@Override
protected float getContentHeight() {
return child.getMarginTop() + child.getActualHeight() + child.getMarginBottom();
}
@Override @Override
public void handleKeyEvent(KeyEvent event) { public void handleKeyEvent(KeyEvent event) {
super.handleKeyEvent(event); super.handleKeyEvent(event);
@@ -99,27 +93,24 @@ public class HScrollable extends SingleChildContainer {
if (event.getKey() == Key.KEY_RIGHT && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) { if (event.getKey() == Key.KEY_RIGHT && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) {
scroll = min(scroll + scrollStep, 1); scroll = min(scroll + scrollStep, 1);
scrollTarget = scroll * max(getContentWidth() - getWidth(), 0);
event.consume(); event.consume();
} }
if (event.getKey() == Key.KEY_LEFT && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) { if (event.getKey() == Key.KEY_LEFT && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) {
scroll = max(scroll - scrollStep, 0); scroll = max(scroll - scrollStep, 0);
scrollTarget = scroll * max(getContentWidth() - getWidth(), 0);
event.consume(); event.consume();
} }
} }
@Override @Override
public void draw(Screen screen, GUI gui) { public void draw(Screen screen, GUI gui) {
var remainingDistance = scrollTarget - scrollPosition; var remainingDistance = -scroll * max(getContentWidth() - getWidth(), 0) - offsetX;
if (abs(remainingDistance) > scrollSpeed) { if (abs(remainingDistance) > scrollSpeed) {
scrollPosition += scrollSpeed * remainingDistance; offsetX += scrollSpeed * remainingDistance;
} }
gui.clip(x, y, getWidth(), getHeight()); gui.clip(x, y, getWidth(), getHeight());
child.setPosition(x + paddingLeft + child.getMarginLeft() - scrollPosition, y + paddingTop + child.getMarginTop()); super.draw(screen, gui);
child.draw(screen, gui);
gui.resetClip(); gui.resetClip();
} }
} }

View File

@@ -1,25 +0,0 @@
package com.bartlomiejpluta.base.api.game.gui.component;
import com.bartlomiejpluta.base.api.game.context.Context;
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
public abstract class SingleChildContainer extends BaseContainer {
protected Component child;
protected SingleChildContainer(Context context, GUI gui) {
super(context, gui);
}
@Override
public void add(Component component) {
children.clear();
super.add(component);
this.child = component;
}
@Override
public void remove(Component component) {
super.remove(component);
this.child = null;
}
}

View File

@@ -5,6 +5,8 @@ import com.bartlomiejpluta.base.api.game.gui.base.GUI;
import com.bartlomiejpluta.base.api.game.screen.Screen; import com.bartlomiejpluta.base.api.game.screen.Screen;
public class VLayout extends BaseContainer { public class VLayout extends BaseContainer {
protected float offsetX = 0.0f;
protected float offsetY = 0.0f;
public VLayout(Context context, GUI gui) { public VLayout(Context context, GUI gui) {
super(context, gui); super(context, gui);
@@ -22,8 +24,8 @@ public class VLayout extends BaseContainer {
@Override @Override
public void draw(Screen screen, GUI gui) { public void draw(Screen screen, GUI gui) {
var currentX = x + paddingLeft; var currentX = x + paddingLeft + offsetX;
var currentY = y + paddingTop; var currentY = y + paddingTop + offsetY;
for (var child : children) { for (var child : children) {
var childAbsX = currentX + child.getMarginLeft(); var childAbsX = currentX + child.getMarginLeft();

View File

@@ -11,23 +11,32 @@ import com.bartlomiejpluta.base.api.game.screen.Screen;
import static com.bartlomiejpluta.base.api.util.math.MathUtil.clamp; import static com.bartlomiejpluta.base.api.util.math.MathUtil.clamp;
import static java.lang.Math.*; import static java.lang.Math.*;
public class VScrollable extends SingleChildContainer { public class VScrollableLayout extends VLayout {
private float scroll = 0.0f; private float scroll = 0.0f;
private float scrollStep = 0.25f; private float scrollStep = 0.25f;
private float scrollSpeed = 0.1f; private float scrollSpeed = 0.1f;
private float scrollTarget = 0.0f; public VScrollableLayout(Context context, GUI gui) {
private float scrollPosition = 0.0f;
public VScrollable(Context context, GUI gui) {
super(context, gui); super(context, gui);
} }
public float getScroll() { public float getActualScroll() {
return getContentHeight() * -offsetY;
}
public int getCurrentPage() {
return (int) floor(scroll / scrollStep);
}
public int getPages() {
return (int) ceil(1 / scrollStep);
}
public float getTargetScroll() {
return scroll; return scroll;
} }
public void setScroll(Float scroll) { public void scrollTo(Float scroll) {
this.scroll = clamp(scroll, 0, 1); this.scroll = clamp(scroll, 0, 1);
} }
@@ -47,11 +56,6 @@ public class VScrollable extends SingleChildContainer {
this.scrollSpeed = clamp(scrollSpeed, 0, 1); this.scrollSpeed = clamp(scrollSpeed, 0, 1);
} }
@Override
public void remove(Component component) {
throw new UnsupportedOperationException();
}
@Override @Override
public void setSizeMode(SizeMode widthMode, SizeMode heightMode) { public void setSizeMode(SizeMode widthMode, SizeMode heightMode) {
if (widthMode == SizeMode.AUTO || heightMode == SizeMode.AUTO) { if (widthMode == SizeMode.AUTO || heightMode == SizeMode.AUTO) {
@@ -79,16 +83,6 @@ public class VScrollable extends SingleChildContainer {
super.setHeightMode(mode); super.setHeightMode(mode);
} }
@Override
protected float getContentWidth() {
return child.getMarginLeft() + child.getActualWidth() + child.getMarginRight();
}
@Override
protected float getContentHeight() {
return child.getMarginTop() + child.getActualHeight() + child.getMarginBottom();
}
@Override @Override
public void handleKeyEvent(KeyEvent event) { public void handleKeyEvent(KeyEvent event) {
super.handleKeyEvent(event); super.handleKeyEvent(event);
@@ -99,27 +93,24 @@ public class VScrollable extends SingleChildContainer {
if (event.getKey() == Key.KEY_DOWN && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) { if (event.getKey() == Key.KEY_DOWN && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) {
scroll = min(scroll + scrollStep, 1); scroll = min(scroll + scrollStep, 1);
scrollTarget = scroll * max(getContentHeight() - getHeight(), 0);
event.consume(); event.consume();
} }
if (event.getKey() == Key.KEY_UP && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) { if (event.getKey() == Key.KEY_UP && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) {
scroll = max(scroll - scrollStep, 0); scroll = max(scroll - scrollStep, 0);
scrollTarget = scroll * max(getContentHeight() - getHeight(), 0);
event.consume(); event.consume();
} }
} }
@Override @Override
public void draw(Screen screen, GUI gui) { public void draw(Screen screen, GUI gui) {
var remainingDistance = scrollTarget - scrollPosition; var remainingDistance = -scroll * max(getContentHeight() - getHeight(), 0) - offsetY;
if (abs(remainingDistance) > scrollSpeed) { if (abs(remainingDistance) > scrollSpeed) {
scrollPosition += scrollSpeed * remainingDistance; offsetY += scrollSpeed * remainingDistance;
} }
gui.clip(x, y, getWidth(), getHeight()); gui.clip(x, y, getWidth(), getHeight());
child.setPosition(x + paddingLeft + child.getMarginLeft(), y + paddingTop + child.getMarginTop() - scrollPosition); super.draw(screen, gui);
child.draw(screen, gui);
gui.resetClip(); gui.resetClip();
} }
} }