Enable CompletableFuture support in WindowManager

This commit is contained in:
2022-08-31 11:46:30 +02:00
parent 44b32daa17
commit ec28a22b72
3 changed files with 41 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
package com.bartlomiejpluta.base.api.gui; package com.bartlomiejpluta.base.api.gui;
import java.util.concurrent.CompletableFuture;
public interface Window extends Widget { public interface Window extends Widget {
void setContent(Component component); void setContent(Component component);
@@ -11,6 +13,10 @@ public interface Window extends Widget {
<T extends Component> T reference(String ref, Class<T> type); <T extends Component> T reference(String ref, Class<T> type);
CompletableFuture<Window> getFuture();
void setFuture(CompletableFuture<Window> future);
default void onOpen(WindowManager manager, Object[] args) { default void onOpen(WindowManager manager, Object[] args) {
// do nothing // do nothing
} }

View File

@@ -10,8 +10,7 @@ import lombok.Setter;
import java.util.Deque; import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.concurrent.CompletableFuture;
import static java.util.Objects.requireNonNull;
public final class WindowManager extends BaseWidget { public final class WindowManager extends BaseWidget {
private final Input input; private final Input input;
@@ -29,7 +28,7 @@ public final class WindowManager extends BaseWidget {
this(context, DisplayMode.DISPLAY_STACK, UpdateMode.UPDATE_ALL); this(context, DisplayMode.DISPLAY_STACK, UpdateMode.UPDATE_ALL);
} }
public WindowManager(Context context, DisplayMode displayMode, UpdateMode updateMode) { public WindowManager(@NonNull Context context, @NonNull DisplayMode displayMode, @NonNull UpdateMode updateMode) {
this.input = context.getInput(); this.input = context.getInput();
super.setSizeMode(SizeMode.RELATIVE, SizeMode.RELATIVE); super.setSizeMode(SizeMode.RELATIVE, SizeMode.RELATIVE);
@@ -63,8 +62,8 @@ public final class WindowManager extends BaseWidget {
throw new UnsupportedOperationException("Window Manager is hardcoded to be of MATCH_PARENT mode"); throw new UnsupportedOperationException("Window Manager is hardcoded to be of MATCH_PARENT mode");
} }
public void open(Window window, Object... args) { public CompletableFuture<Window> open(@NonNull Window window, Object... args) {
requireNonNull(window, "Window cannot be null"); window.setFuture(new CompletableFuture<>());
if (windows.isEmpty()) { if (windows.isEmpty()) {
input.addKeyEventHandler(this::forwardKeyEventToTopWindow); input.addKeyEventHandler(this::forwardKeyEventToTopWindow);
@@ -72,7 +71,9 @@ public final class WindowManager extends BaseWidget {
windows.addLast(window); windows.addLast(window);
window.setParent(this); window.setParent(this);
window.onOpen(this, args != null ? args : new Object[] {}); window.onOpen(this, args != null ? args : new Object[]{});
return window.getFuture();
} }
private void forwardKeyEventToTopWindow(KeyEvent event) { private void forwardKeyEventToTopWindow(KeyEvent event) {
@@ -101,6 +102,8 @@ public final class WindowManager extends BaseWidget {
if (windows.isEmpty()) { if (windows.isEmpty()) {
input.removeKeyEventHandler(this::forwardKeyEventToTopWindow); input.removeKeyEventHandler(this::forwardKeyEventToTopWindow);
} }
window.getFuture().complete(window);
} }
public int size() { public int size() {
@@ -154,48 +157,48 @@ public final class WindowManager extends BaseWidget {
private void drawWindow(Screen screen, Window window, GUI gui) { private void drawWindow(Screen screen, Window window, GUI gui) {
switch (window.getWindowPosition()) { switch (window.getWindowPosition()) {
case TOP -> window.setPosition( case TOP -> window.setPosition(
(screen.getWidth() - window.getWidth()) / 2, (screen.getWidth() - window.getWidth()) / 2,
window.getMarginTop() window.getMarginTop()
); );
case TOP_RIGHT -> window.setPosition( case TOP_RIGHT -> window.setPosition(
screen.getWidth() - window.getWidth() - window.getMarginRight(), screen.getWidth() - window.getWidth() - window.getMarginRight(),
window.getMarginTop() window.getMarginTop()
); );
case RIGHT -> window.setPosition( case RIGHT -> window.setPosition(
screen.getWidth() - window.getWidth() - window.getMarginRight(), screen.getWidth() - window.getWidth() - window.getMarginRight(),
(screen.getHeight() - window.getHeight()) / 2 (screen.getHeight() - window.getHeight()) / 2
); );
case BOTTOM_RIGHT -> window.setPosition( case BOTTOM_RIGHT -> window.setPosition(
screen.getWidth() - window.getWidth() - window.getMarginRight(), screen.getWidth() - window.getWidth() - window.getMarginRight(),
screen.getHeight() - window.getHeight() - window.getMarginBottom() screen.getHeight() - window.getHeight() - window.getMarginBottom()
); );
case BOTTOM -> window.setPosition( case BOTTOM -> window.setPosition(
(screen.getWidth() - window.getWidth()) / 2, (screen.getWidth() - window.getWidth()) / 2,
screen.getHeight() - window.getHeight() - window.getMarginBottom() screen.getHeight() - window.getHeight() - window.getMarginBottom()
); );
case BOTTOM_LEFT -> window.setPosition( case BOTTOM_LEFT -> window.setPosition(
window.getMarginLeft(), window.getMarginLeft(),
screen.getHeight() - window.getHeight() - window.getMarginBottom() screen.getHeight() - window.getHeight() - window.getMarginBottom()
); );
case LEFT -> window.setPosition( case LEFT -> window.setPosition(
window.getMarginLeft(), window.getMarginLeft(),
(screen.getHeight() - window.getHeight()) / 2 (screen.getHeight() - window.getHeight()) / 2
); );
case TOP_LEFT -> window.setPosition( case TOP_LEFT -> window.setPosition(
window.getMarginLeft(), window.getMarginLeft(),
window.getMarginTop() window.getMarginTop()
); );
case CENTER -> window.setPosition( case CENTER -> window.setPosition(
(screen.getWidth() - window.getWidth()) / 2, (screen.getWidth() - window.getWidth()) / 2,
(screen.getHeight() - window.getHeight()) / 2 (screen.getHeight() - window.getHeight()) / 2
); );
} }

View File

@@ -6,8 +6,10 @@ 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.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture;
import static java.lang.String.format; import static java.lang.String.format;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
@@ -15,12 +17,16 @@ import static java.util.Objects.requireNonNull;
public abstract class BaseWindow extends BaseWidget implements Window { public abstract class BaseWindow extends BaseWidget implements Window {
@Getter @Getter
private final Map<String, Component> refs; private final Map<String, Component> refs;
protected Context context; protected final Context context;
protected GUI gui; protected final GUI gui;
protected WindowManager manager; protected WindowManager manager;
protected Component content; protected Component content;
protected WindowPosition windowPosition = WindowPosition.CENTER; protected WindowPosition windowPosition = WindowPosition.CENTER;
@Getter
@Setter
protected CompletableFuture<Window> future;
protected BaseWindow(Context context, GUI gui, Map<String, Component> refs) { protected BaseWindow(Context context, GUI gui, Map<String, Component> refs) {
this.context = context; this.context = context;
this.gui = gui; this.gui = gui;