Enable programmatically referencing Window's components
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
package com.bartlomiejpluta.base.api.gui;
|
package com.bartlomiejpluta.base.api.gui;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface Component extends Widget {
|
public interface Component extends Widget {
|
||||||
Iterable<Component> getChildren();
|
List<Component> getChildren();
|
||||||
|
|
||||||
void add(Component component);
|
void add(Component component);
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,12 @@ public interface GUI extends Renderable, Updatable, Disposable {
|
|||||||
|
|
||||||
Component inflateComponent(String widgetUid);
|
Component inflateComponent(String widgetUid);
|
||||||
|
|
||||||
|
<T extends Component> T inflateComponent(String widgetUid, Class<T> type);
|
||||||
|
|
||||||
Window inflateWindow(String widgetUid);
|
Window inflateWindow(String widgetUid);
|
||||||
|
|
||||||
|
<T extends Window> T inflateWindow(String widgetUid, Class<T> type);
|
||||||
|
|
||||||
boolean isVisible();
|
boolean isVisible();
|
||||||
|
|
||||||
void setVisible(boolean visible);
|
void setVisible(boolean visible);
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ public interface Window extends Widget {
|
|||||||
|
|
||||||
void setWindowPosition(WindowPosition windowPosition);
|
void setWindowPosition(WindowPosition windowPosition);
|
||||||
|
|
||||||
|
Component reference(String ref);
|
||||||
|
|
||||||
|
<T extends Component> T reference(String ref, Class<T> type);
|
||||||
|
|
||||||
default void onOpen(WindowManager manager) {
|
default void onOpen(WindowManager manager) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.bartlomiejpluta.base.api.context.Context;
|
|||||||
import com.bartlomiejpluta.base.api.gui.Component;
|
import com.bartlomiejpluta.base.api.gui.Component;
|
||||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
|
|
||||||
public abstract class BaseComponent extends BaseWidget implements Component {
|
public abstract class BaseComponent extends BaseWidget implements Component {
|
||||||
@@ -17,7 +19,7 @@ public abstract class BaseComponent extends BaseWidget implements Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<Component> getChildren() {
|
public List<Component> getChildren() {
|
||||||
return emptyList();
|
return emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public abstract class BaseContainer extends BaseComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<Component> getChildren() {
|
public List<Component> getChildren() {
|
||||||
return readOnlyChildren;
|
return readOnlyChildren;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,12 @@ import com.bartlomiejpluta.base.api.context.Context;
|
|||||||
import com.bartlomiejpluta.base.api.event.Event;
|
import com.bartlomiejpluta.base.api.event.Event;
|
||||||
import com.bartlomiejpluta.base.api.gui.*;
|
import com.bartlomiejpluta.base.api.gui.*;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
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;
|
import static java.util.Objects.requireNonNull;
|
||||||
|
|
||||||
public abstract class BaseWindow extends BaseWidget implements Window {
|
public abstract class BaseWindow extends BaseWidget implements Window {
|
||||||
@@ -12,11 +17,16 @@ public abstract class BaseWindow extends BaseWidget implements Window {
|
|||||||
protected GUI gui;
|
protected GUI gui;
|
||||||
protected WindowManager manager;
|
protected WindowManager manager;
|
||||||
protected Component content;
|
protected Component content;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final Map<String, Component> refs;
|
||||||
|
|
||||||
protected WindowPosition windowPosition = WindowPosition.CENTER;
|
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.context = context;
|
||||||
this.gui = gui;
|
this.gui = gui;
|
||||||
|
this.refs = refs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
@Override
|
||||||
public WindowPosition getWindowPosition() {
|
public WindowPosition getWindowPosition() {
|
||||||
return windowPosition;
|
return windowPosition;
|
||||||
|
|||||||
@@ -133,5 +133,6 @@ public class Label extends BaseComponent {
|
|||||||
gui.setFillColor(color);
|
gui.setFillColor(color);
|
||||||
gui.fill();
|
gui.fill();
|
||||||
gui.putTextBox(x + paddingLeft, y + paddingTop, getWidth() - paddingLeft - paddingRight, text, bounds);
|
gui.putTextBox(x + paddingLeft, y + paddingTop, getWidth() - paddingLeft - paddingRight, text, bounds);
|
||||||
|
gui.closePath();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,6 +84,13 @@ public class NanoVGGUI implements GUI {
|
|||||||
return inflater.inflateComponent(is, context, this);
|
return inflater.inflateComponent(is, context, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Component> T inflateComponent(String widgetUid, Class<T> type) {
|
||||||
|
log.info("Inflating component by widget definition with UID: [{}]", widgetUid);
|
||||||
|
var is = widgetDefinitionManager.loadObject(widgetUid);
|
||||||
|
return type.cast(inflater.inflateComponent(is, context, this));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Window inflateWindow(String widgetUid) {
|
public Window inflateWindow(String widgetUid) {
|
||||||
log.info("Inflating window by widget definition with UID: [{}]", widgetUid);
|
log.info("Inflating window by widget definition with UID: [{}]", widgetUid);
|
||||||
@@ -91,6 +98,12 @@ public class NanoVGGUI implements GUI {
|
|||||||
return inflater.inflateWindow(is, context, this);
|
return inflater.inflateWindow(is, context, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Window> T inflateWindow(String widgetUid, Class<T> type) {
|
||||||
|
log.info("Inflating window by widget definition with UID: [{}]", widgetUid);
|
||||||
|
var is = widgetDefinitionManager.loadObject(widgetUid);
|
||||||
|
return type.cast(inflater.inflateWindow(is, context, this));
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public boolean isVisible() {
|
public boolean isVisible() {
|
||||||
return visible;
|
return visible;
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ public class DefaultInflater implements Inflater {
|
|||||||
|
|
||||||
var windowClass = loader.loadClass(canonicalName);
|
var windowClass = loader.loadClass(canonicalName);
|
||||||
|
|
||||||
var window = (Window) windowClass.getConstructor(Context.class, GUI.class).newInstance(context, gui);
|
var window = (Window) windowClass.getConstructor(Context.class, GUI.class, Map.class).newInstance(context, gui, refs);
|
||||||
var attributes = root.getAttributes();
|
var attributes = root.getAttributes();
|
||||||
|
|
||||||
// Set attributes via setter methods
|
// Set attributes via setter methods
|
||||||
|
|||||||
Reference in New Issue
Block a user