Skip to content

Commit

Permalink
Add Widget Action API
Browse files Browse the repository at this point in the history
A simple wrapper around Widget Messages; however this is currently only
partly implemented. Additionally this will be the last commit for
Galimulator version 4.8-beta.2 and I will be working on bumping the
galimulator version to 4.8-stable soon. Hopefully it will not really
fail as hard.
  • Loading branch information
Geolykt committed Mar 7, 2021
1 parent 74c7065 commit 1f7367b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum DialogCloseCause {
BUTTON_CLICK,

/**
* Called when the User closed the Dialog willingly. not yet implemented.
* Called when the User closed the Dialog willingly.
*/
MANUAL_CLOSE;
}
11 changes: 11 additions & 0 deletions src/main/java/de/geolykt/starloader/api/gui/WidgetAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.geolykt.starloader.api.gui;

/**
* Our version agnostic contemporary to Widget messages, which is an odd way to communicate within the UI System within the
* snoddasmannen.galimulator.ui package
*/
public enum WidgetAction {
CLOSE,
RESIZE,
REDRAW;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.geolykt.starloader.api.gui;

/**
* Listens for {@link WidgetAction} on Widgets the listener has been registered to.
*/
@FunctionalInterface
public interface WidgetActionListener {
public void onAction(WidgetAction action);
}
1 change: 1 addition & 0 deletions src/main/java/de/geolykt/starloader/impl/BasicDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public BasicDialog(@NotNull String title, @NotNull String description, @Nullable
@NotNull ArrayList<BasicDialogCloseListener> listeners, int duration, boolean playSFX) {
dialog = Space.a(title, description, choices, duration, null, true);
dialog.a(new DialogCloseListenerWrapper(listeners, playSFX));
dialog.a(new WidgetActionListenerWrapper(this, listeners, new ArrayList<>()));
// Luckily the close time is final, so we only have to get it once
try {
Field field = this.dialog.getClass().getField("d");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void a(Object var1) {
}
}
if (playSFX) {
AudioSampleWrapper.UI_BIG_SELECT.play();
AudioSampleWrapper.UI_SMALL_SELECT.play();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package de.geolykt.starloader.impl;

import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import de.geolykt.starloader.DebugNagException;
import de.geolykt.starloader.api.gui.AutocloseableDialog;
import de.geolykt.starloader.api.gui.BasicDialogCloseListener;
import de.geolykt.starloader.api.gui.DialogCloseCause;
import de.geolykt.starloader.api.gui.WidgetAction;
import de.geolykt.starloader.api.gui.WidgetActionListener;
import snoddasmannen.galimulator.ui.Widget$WIDGET_MESSAGE;

public class WidgetActionListenerWrapper implements snoddasmannen.galimulator.hg {

private final List<BasicDialogCloseListener> closeListeners;
private final List<WidgetActionListener> actionListeners;
private final AutocloseableDialog parent;

/**
* Initiates the wrapper with the parent and listeners known. The listeners can be modified later however, provided
* the list implementation supports that.
* @param parent The parent that is closable automatically, a null value means that it will never be closed automatically.
* @param closeListeners The close listeners to have initially
* @param actionListeners The widget action listeners to have initially
*/
WidgetActionListenerWrapper(@Nullable AutocloseableDialog parent,
@NotNull List<BasicDialogCloseListener> closeListeners, @NotNull List<WidgetActionListener> actionListeners) {
this.parent = parent;
this.closeListeners = closeListeners;
this.actionListeners = actionListeners;
}

/**
* Initiates the wrapper with a parent that is closable automatically. If that parameter is null it should behave the
* same way as {@link #WidgetActionListenerWrapper()}
* @param parent The parent dialog
*/
public WidgetActionListenerWrapper(@Nullable AutocloseableDialog parent) {
this(parent, new ArrayList<>(), new ArrayList<>());
}

/**
* Initiates the wrapper with no further information. If the parent Widget is an automatically closing dialog, then
* {@link #WidgetActionListenerWrapper(AutoCloseable)} should be preferred as
* otherwise the {@link BasicDialogCloseListener} might get multiple automatic close notifications.
*/
public WidgetActionListenerWrapper() {
this(null, new ArrayList<>(), new ArrayList<>());
}

@Override
public void a(Widget$WIDGET_MESSAGE var1) {
if (var1 == Widget$WIDGET_MESSAGE.a) {
// Check if it was closed automatically due to timeout, if it was, then ignore this request
if (parent == null) {
notifyClose(DialogCloseCause.MANUAL_CLOSE);
} else {
long timeout = parent.getAutocloseTime();
if (timeout == -1 || timeout < System.currentTimeMillis()) {
notifyClose(DialogCloseCause.MANUAL_CLOSE);
}
}
for (WidgetActionListener listener : actionListeners) {
listener.onAction(WidgetAction.CLOSE);
}
} else {
if (var1 == null) {
DebugNagException.nag("Null widget message!");
}
WidgetAction action = var1 == Widget$WIDGET_MESSAGE.b ? WidgetAction.RESIZE : WidgetAction.REDRAW;
for (WidgetActionListener listener : actionListeners) {
listener.onAction(action);
}
}
}

private void notifyClose(DialogCloseCause cause) {
for (BasicDialogCloseListener listener : closeListeners) {
listener.onClose(cause, null);
}
}

public void addListener(@NotNull BasicDialogCloseListener listener) {
closeListeners.add(listener);
}

public void addListener(@NotNull WidgetActionListener listener) {
actionListeners.add(listener);
}

}

0 comments on commit 1f7367b

Please sign in to comment.