Create VScrollable container
This commit is contained in:
@@ -113,4 +113,6 @@ public interface GUI extends Renderable, Disposable, KeyEventHandler {
|
||||
void imagePattern(float x, float y, float width, float height, float angle, float alpha, Image image, Paint target);
|
||||
|
||||
void clip(float x, float y, float width, float height);
|
||||
|
||||
void resetClip();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Collections.unmodifiableList;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public abstract class BaseContainer extends BaseComponent implements Container {
|
||||
protected final List<Component> children = new LinkedList<>();
|
||||
@@ -24,13 +25,13 @@ public abstract class BaseContainer extends BaseComponent implements Container {
|
||||
|
||||
@Override
|
||||
public void add(Component component) {
|
||||
this.children.add(component);
|
||||
this.children.add(requireNonNull(component));
|
||||
component.setParent(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Component component) {
|
||||
this.children.remove(component);
|
||||
this.children.remove(requireNonNull(component));
|
||||
component.setParent(null);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
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;
|
||||
import com.bartlomiejpluta.base.api.game.gui.base.SizeMode;
|
||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyAction;
|
||||
import com.bartlomiejpluta.base.api.game.input.KeyEvent;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
|
||||
import static com.bartlomiejpluta.base.api.util.math.MathUtil.clamp;
|
||||
import static java.lang.Math.*;
|
||||
|
||||
public class VScrollable extends SingleChildContainer {
|
||||
private float scroll = 0.0f;
|
||||
private float scrollStep = 0.25f;
|
||||
private float scrollSpeed = 0.1f;
|
||||
|
||||
private float scrollTarget = 0.0f;
|
||||
private float scrollPosition = 0.0f;
|
||||
|
||||
public VScrollable(Context context, GUI gui) {
|
||||
super(context, gui);
|
||||
}
|
||||
|
||||
public float getScroll() {
|
||||
return scroll;
|
||||
}
|
||||
|
||||
public void setScroll(Float scroll) {
|
||||
this.scroll = clamp(scroll, 0, 1);
|
||||
}
|
||||
|
||||
public float getScrollStep() {
|
||||
return scrollStep;
|
||||
}
|
||||
|
||||
public void setScrollStep(Float scrollStep) {
|
||||
this.scrollStep = clamp(scrollStep, 0, 1);
|
||||
}
|
||||
|
||||
public float getScrollSpeed() {
|
||||
return scrollSpeed;
|
||||
}
|
||||
|
||||
public void setScrollSpeed(Float scrollSpeed) {
|
||||
this.scrollSpeed = clamp(scrollSpeed, 0, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Component component) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSizeMode(SizeMode widthMode, SizeMode heightMode) {
|
||||
if (widthMode == SizeMode.AUTO || heightMode == SizeMode.AUTO) {
|
||||
throw new IllegalStateException("Scrollable component size mode must be other than AUTO");
|
||||
}
|
||||
|
||||
super.setSizeMode(widthMode, heightMode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWidthMode(SizeMode mode) {
|
||||
if (mode == SizeMode.AUTO) {
|
||||
throw new IllegalStateException("Scrollable component size mode must be other than AUTO");
|
||||
}
|
||||
|
||||
super.setWidthMode(mode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHeightMode(SizeMode mode) {
|
||||
if (mode == SizeMode.AUTO) {
|
||||
throw new IllegalStateException("Scrollable component size mode must be other than AUTO");
|
||||
}
|
||||
|
||||
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
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
super.handleKeyEvent(event);
|
||||
|
||||
if (event.isConsumed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getKey() == Key.KEY_DOWN && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) {
|
||||
scroll = min(scroll + scrollStep, 1);
|
||||
scrollTarget = scroll * max(getContentHeight() - getHeight(), 0);
|
||||
event.consume();
|
||||
}
|
||||
|
||||
if (event.getKey() == Key.KEY_UP && (event.getAction() == KeyAction.PRESS || event.getAction() == KeyAction.REPEAT)) {
|
||||
scroll = max(scroll - scrollStep, 0);
|
||||
scrollTarget = scroll * max(getContentHeight() - getHeight(), 0);
|
||||
event.consume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Screen screen, GUI gui) {
|
||||
var remainingDistance = scrollTarget - scrollPosition;
|
||||
if (abs(remainingDistance) > scrollSpeed) {
|
||||
scrollPosition += scrollSpeed * remainingDistance;
|
||||
}
|
||||
|
||||
gui.clip(x, y, getWidth(), getHeight());
|
||||
child.setPosition(x + paddingLeft + child.getMarginLeft(), y + paddingTop + child.getMarginTop() - scrollPosition);
|
||||
child.draw(screen, gui);
|
||||
gui.resetClip();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.bartlomiejpluta.base.engine.util.math;
|
||||
package com.bartlomiejpluta.base.api.util.math;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
import static java.lang.Math.min;
|
||||
@@ -389,6 +389,11 @@ public class NanoVGGUI implements GUI {
|
||||
nvgScissor(nvg, x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetClip() {
|
||||
nvgResetScissor(nvg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleKeyEvent(KeyEvent event) {
|
||||
screenWidget.handleKeyEvent(event);
|
||||
|
||||
@@ -4,9 +4,9 @@ import com.bartlomiejpluta.base.api.game.entity.Direction;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||
import com.bartlomiejpluta.base.api.game.entity.Movement;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.api.util.math.MathUtil;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.mesh.Mesh;
|
||||
import com.bartlomiejpluta.base.engine.util.math.MathUtil;
|
||||
import com.bartlomiejpluta.base.engine.world.movement.MovableSprite;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.bartlomiejpluta.base.engine.world.image.manager;
|
||||
|
||||
import com.bartlomiejpluta.base.api.game.image.Image;
|
||||
import com.bartlomiejpluta.base.api.util.math.MathUtil;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.material.Material;
|
||||
import com.bartlomiejpluta.base.engine.core.gl.object.texture.TextureManager;
|
||||
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||
import com.bartlomiejpluta.base.engine.project.config.ProjectConfiguration;
|
||||
import com.bartlomiejpluta.base.engine.util.math.MathUtil;
|
||||
import com.bartlomiejpluta.base.engine.util.mesh.MeshManager;
|
||||
import com.bartlomiejpluta.base.engine.util.res.ResourcesManager;
|
||||
import com.bartlomiejpluta.base.engine.world.image.asset.ImageAsset;
|
||||
|
||||
Reference in New Issue
Block a user