Enable programmatically referencing Window's components

This commit is contained in:
2022-08-25 17:30:05 +02:00
parent 9e416655e7
commit 43c36a30ec
9 changed files with 51 additions and 5 deletions

View File

@@ -1,7 +1,9 @@
package com.bartlomiejpluta.base.api.gui;
import java.util.List;
public interface Component extends Widget {
Iterable<Component> getChildren();
List<Component> getChildren();
void add(Component component);

View File

@@ -22,8 +22,12 @@ public interface GUI extends Renderable, Updatable, Disposable {
Component inflateComponent(String widgetUid);
<T extends Component> T inflateComponent(String widgetUid, Class<T> type);
Window inflateWindow(String widgetUid);
<T extends Window> T inflateWindow(String widgetUid, Class<T> type);
boolean isVisible();
void setVisible(boolean visible);

View File

@@ -7,6 +7,10 @@ public interface Window extends Widget {
void setWindowPosition(WindowPosition windowPosition);
Component reference(String ref);
<T extends Component> T reference(String ref, Class<T> type);
default void onOpen(WindowManager manager) {
// do nothing
}

View File

@@ -4,6 +4,8 @@ import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.gui.Component;
import com.bartlomiejpluta.base.api.gui.GUI;
import java.util.List;
import static java.util.Collections.emptyList;
public abstract class BaseComponent extends BaseWidget implements Component {
@@ -17,7 +19,7 @@ public abstract class BaseComponent extends BaseWidget implements Component {
}
@Override
public Iterable<Component> getChildren() {
public List<Component> getChildren() {
return emptyList();
}

View File

@@ -20,7 +20,7 @@ public abstract class BaseContainer extends BaseComponent {
}
@Override
public Iterable<Component> getChildren() {
public List<Component> getChildren() {
return readOnlyChildren;
}

View File

@@ -4,7 +4,12 @@ import com.bartlomiejpluta.base.api.context.Context;
import com.bartlomiejpluta.base.api.event.Event;
import com.bartlomiejpluta.base.api.gui.*;
import com.bartlomiejpluta.base.api.screen.Screen;
import lombok.Getter;
import lombok.NonNull;
import java.util.Map;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
public abstract class BaseWindow extends BaseWidget implements Window {
@@ -12,11 +17,16 @@ public abstract class BaseWindow extends BaseWidget implements Window {
protected GUI gui;
protected WindowManager manager;
protected Component content;
@Getter
private final Map<String, Component> refs;
protected WindowPosition windowPosition = WindowPosition.CENTER;
protected BaseWindow(Context context, GUI gui) {
protected BaseWindow(Context context, GUI gui, Map<String, Component> refs) {
this.context = context;
this.gui = gui;
this.refs = refs;
}
@Override
@@ -32,6 +42,16 @@ public abstract class BaseWindow extends BaseWidget implements Window {
}
}
@Override
public Component reference(@NonNull String ref) {
return requireNonNull(refs.get(ref), format("Referenced component (with ref=[%s]) does not exist", ref));
}
@Override
public <T extends Component> T reference(String ref, Class<T> type) {
return type.cast(requireNonNull(refs.get(ref), format("Referenced component (with ref=[%s]) does not exist", ref)));
}
@Override
public WindowPosition getWindowPosition() {
return windowPosition;

View File

@@ -133,5 +133,6 @@ public class Label extends BaseComponent {
gui.setFillColor(color);
gui.fill();
gui.putTextBox(x + paddingLeft, y + paddingTop, getWidth() - paddingLeft - paddingRight, text, bounds);
gui.closePath();
}
}