Skip to content

Commit 1f7367b

Browse files
committed
Add Widget Action API
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.
1 parent 74c7065 commit 1f7367b

File tree

6 files changed

+118
-2
lines changed

6 files changed

+118
-2
lines changed

src/main/java/de/geolykt/starloader/api/gui/DialogCloseCause.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public enum DialogCloseCause {
1212
BUTTON_CLICK,
1313

1414
/**
15-
* Called when the User closed the Dialog willingly. not yet implemented.
15+
* Called when the User closed the Dialog willingly.
1616
*/
1717
MANUAL_CLOSE;
1818
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package de.geolykt.starloader.api.gui;
2+
3+
/**
4+
* Our version agnostic contemporary to Widget messages, which is an odd way to communicate within the UI System within the
5+
* snoddasmannen.galimulator.ui package
6+
*/
7+
public enum WidgetAction {
8+
CLOSE,
9+
RESIZE,
10+
REDRAW;
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.geolykt.starloader.api.gui;
2+
3+
/**
4+
* Listens for {@link WidgetAction} on Widgets the listener has been registered to.
5+
*/
6+
@FunctionalInterface
7+
public interface WidgetActionListener {
8+
public void onAction(WidgetAction action);
9+
}

src/main/java/de/geolykt/starloader/impl/BasicDialog.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public BasicDialog(@NotNull String title, @NotNull String description, @Nullable
3232
@NotNull ArrayList<BasicDialogCloseListener> listeners, int duration, boolean playSFX) {
3333
dialog = Space.a(title, description, choices, duration, null, true);
3434
dialog.a(new DialogCloseListenerWrapper(listeners, playSFX));
35+
dialog.a(new WidgetActionListenerWrapper(this, listeners, new ArrayList<>()));
3536
// Luckily the close time is final, so we only have to get it once
3637
try {
3738
Field field = this.dialog.getClass().getField("d");

src/main/java/de/geolykt/starloader/impl/DialogCloseListenerWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void a(Object var1) {
3535
}
3636
}
3737
if (playSFX) {
38-
AudioSampleWrapper.UI_BIG_SELECT.play();
38+
AudioSampleWrapper.UI_SMALL_SELECT.play();
3939
}
4040
}
4141

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package de.geolykt.starloader.impl;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import de.geolykt.starloader.DebugNagException;
10+
import de.geolykt.starloader.api.gui.AutocloseableDialog;
11+
import de.geolykt.starloader.api.gui.BasicDialogCloseListener;
12+
import de.geolykt.starloader.api.gui.DialogCloseCause;
13+
import de.geolykt.starloader.api.gui.WidgetAction;
14+
import de.geolykt.starloader.api.gui.WidgetActionListener;
15+
import snoddasmannen.galimulator.ui.Widget$WIDGET_MESSAGE;
16+
17+
public class WidgetActionListenerWrapper implements snoddasmannen.galimulator.hg {
18+
19+
private final List<BasicDialogCloseListener> closeListeners;
20+
private final List<WidgetActionListener> actionListeners;
21+
private final AutocloseableDialog parent;
22+
23+
/**
24+
* Initiates the wrapper with the parent and listeners known. The listeners can be modified later however, provided
25+
* the list implementation supports that.
26+
* @param parent The parent that is closable automatically, a null value means that it will never be closed automatically.
27+
* @param closeListeners The close listeners to have initially
28+
* @param actionListeners The widget action listeners to have initially
29+
*/
30+
WidgetActionListenerWrapper(@Nullable AutocloseableDialog parent,
31+
@NotNull List<BasicDialogCloseListener> closeListeners, @NotNull List<WidgetActionListener> actionListeners) {
32+
this.parent = parent;
33+
this.closeListeners = closeListeners;
34+
this.actionListeners = actionListeners;
35+
}
36+
37+
/**
38+
* Initiates the wrapper with a parent that is closable automatically. If that parameter is null it should behave the
39+
* same way as {@link #WidgetActionListenerWrapper()}
40+
* @param parent The parent dialog
41+
*/
42+
public WidgetActionListenerWrapper(@Nullable AutocloseableDialog parent) {
43+
this(parent, new ArrayList<>(), new ArrayList<>());
44+
}
45+
46+
/**
47+
* Initiates the wrapper with no further information. If the parent Widget is an automatically closing dialog, then
48+
* {@link #WidgetActionListenerWrapper(AutoCloseable)} should be preferred as
49+
* otherwise the {@link BasicDialogCloseListener} might get multiple automatic close notifications.
50+
*/
51+
public WidgetActionListenerWrapper() {
52+
this(null, new ArrayList<>(), new ArrayList<>());
53+
}
54+
55+
@Override
56+
public void a(Widget$WIDGET_MESSAGE var1) {
57+
if (var1 == Widget$WIDGET_MESSAGE.a) {
58+
// Check if it was closed automatically due to timeout, if it was, then ignore this request
59+
if (parent == null) {
60+
notifyClose(DialogCloseCause.MANUAL_CLOSE);
61+
} else {
62+
long timeout = parent.getAutocloseTime();
63+
if (timeout == -1 || timeout < System.currentTimeMillis()) {
64+
notifyClose(DialogCloseCause.MANUAL_CLOSE);
65+
}
66+
}
67+
for (WidgetActionListener listener : actionListeners) {
68+
listener.onAction(WidgetAction.CLOSE);
69+
}
70+
} else {
71+
if (var1 == null) {
72+
DebugNagException.nag("Null widget message!");
73+
}
74+
WidgetAction action = var1 == Widget$WIDGET_MESSAGE.b ? WidgetAction.RESIZE : WidgetAction.REDRAW;
75+
for (WidgetActionListener listener : actionListeners) {
76+
listener.onAction(action);
77+
}
78+
}
79+
}
80+
81+
private void notifyClose(DialogCloseCause cause) {
82+
for (BasicDialogCloseListener listener : closeListeners) {
83+
listener.onClose(cause, null);
84+
}
85+
}
86+
87+
public void addListener(@NotNull BasicDialogCloseListener listener) {
88+
closeListeners.add(listener);
89+
}
90+
91+
public void addListener(@NotNull WidgetActionListener listener) {
92+
actionListeners.add(listener);
93+
}
94+
95+
}

0 commit comments

Comments
 (0)