-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5231af8
commit 2c5b72d
Showing
27 changed files
with
691 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ient/java/com/mcmoddev/mmdbot/dashboard/client/controller/config/ConfigBoxController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.mcmoddev.mmdbot.dashboard.client.controller.config; | ||
|
||
import com.mcmoddev.mmdbot.dashboard.BotTypeEnum; | ||
import com.mcmoddev.mmdbot.dashboard.util.DashConfigType; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public abstract class ConfigBoxController { | ||
|
||
protected final BotTypeEnum botType; | ||
protected final DashConfigType type; | ||
protected final String configName; | ||
protected final String path; | ||
protected final String[] comments; | ||
|
||
public abstract void init(); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...va/com/mcmoddev/mmdbot/dashboard/client/controller/config/DefaultConfigBoxController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.mcmoddev.mmdbot.dashboard.client.controller.config; | ||
|
||
import com.mcmoddev.mmdbot.dashboard.BotTypeEnum; | ||
import com.mcmoddev.mmdbot.dashboard.client.DashboardClient; | ||
import com.mcmoddev.mmdbot.dashboard.client.util.Constants; | ||
import com.mcmoddev.mmdbot.dashboard.common.packet.PacketID; | ||
import com.mcmoddev.mmdbot.dashboard.packets.UpdateConfigPacket; | ||
import com.mcmoddev.mmdbot.dashboard.packets.requests.RequestConfigValuePacket; | ||
import com.mcmoddev.mmdbot.dashboard.util.DashConfigType; | ||
import javafx.event.Event; | ||
import javafx.fxml.FXML; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.TextField; | ||
import javafx.scene.text.Font; | ||
|
||
public class DefaultConfigBoxController extends ConfigBoxController { | ||
|
||
public DefaultConfigBoxController(final BotTypeEnum botType, final DashConfigType type, final String configName, final String path, final String[] comments) { | ||
super(botType, type, configName, path, comments); | ||
} | ||
|
||
@FXML | ||
private Label configNameLabel; | ||
@FXML | ||
private Label commentsLabel; | ||
@FXML | ||
private TextField valueField; | ||
|
||
@Override | ||
public void init() { | ||
configNameLabel.setText(configName); | ||
commentsLabel.setText(Constants.LINE_JOINER.join(comments)); | ||
configNameLabel.setFont(Font.font(20)); | ||
DashboardClient.sendAndAwaitResponseWithID((PacketID id) -> | ||
new RequestConfigValuePacket(id, botType, type, configName, path)) | ||
.withPlatformAction(pkt -> valueField.setText(pkt.value().toString())) | ||
.queue(); | ||
} | ||
|
||
@FXML | ||
public void onUpdateValueClick(Event e) { | ||
DashboardClient.sendAndAwaitGenericResponse(id -> new UpdateConfigPacket(id, botType, type, configName, path, valueField.getText())) | ||
.queue(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
dashboard/src/client/java/com/mcmoddev/mmdbot/dashboard/client/scenes/bot/ConfigEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.mcmoddev.mmdbot.dashboard.client.scenes.bot; | ||
|
||
import com.mcmoddev.mmdbot.dashboard.util.DashConfigType; | ||
|
||
record ConfigEntry(DashConfigType type, String configName, String path, String[] comments) { | ||
} |
112 changes: 112 additions & 0 deletions
112
...oard/src/client/java/com/mcmoddev/mmdbot/dashboard/client/scenes/bot/DefaultBotStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.mcmoddev.mmdbot.dashboard.client.scenes.bot; | ||
|
||
import com.google.common.base.Suppliers; | ||
import com.mcmoddev.mmdbot.dashboard.BotTypeEnum; | ||
import com.mcmoddev.mmdbot.dashboard.client.DashboardApplication; | ||
import com.mcmoddev.mmdbot.dashboard.client.controller.config.ConfigBoxController; | ||
import com.mcmoddev.mmdbot.dashboard.util.BotUserData; | ||
import com.mcmoddev.mmdbot.dashboard.util.DashConfigType; | ||
import javafx.fxml.FXMLLoader; | ||
import javafx.geometry.Insets; | ||
import javafx.geometry.Side; | ||
import javafx.scene.Node; | ||
import javafx.scene.Scene; | ||
import javafx.scene.control.ScrollPane; | ||
import javafx.scene.control.Tab; | ||
import javafx.scene.control.TabPane; | ||
import javafx.scene.image.Image; | ||
import javafx.scene.layout.BorderPane; | ||
import javafx.scene.layout.HBox; | ||
import javafx.scene.layout.VBox; | ||
import javafx.stage.Stage; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.util.Comparator; | ||
import java.util.EnumMap; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DefaultBotStage implements BotStage { | ||
|
||
private final Map<String, List<ConfigEntry>> configs; | ||
private final BotTypeEnum botType; | ||
|
||
public DefaultBotStage(final Map<String, List<ConfigEntry>> configs, final BotTypeEnum botType) { | ||
this.configs = configs; | ||
this.botType = botType; | ||
} | ||
|
||
@Override | ||
public void createAndShowStage(final BotUserData botUserData) { | ||
final var configTab = new Tab("Configs"); | ||
configTab.setContent(Suppliers.memoize(() -> { | ||
final var configNamesPane = new TabPane(configs.entrySet().stream() | ||
.map(e -> { | ||
final var tab = new Tab(e.getKey()); | ||
final var box = new VBox(4); | ||
box.getChildren().addAll(e.getValue().stream() | ||
.sorted(Comparator.comparing(ConfigEntry::path)) | ||
.map(this::makeConfigBox).toList()); | ||
|
||
final var scrollPane = new ScrollPane(box); | ||
scrollPane.setFitToHeight(true); | ||
|
||
final var root = new BorderPane(scrollPane); | ||
root.setPadding(new Insets(15)); | ||
tab.setContent(root); | ||
|
||
return tab; | ||
}).toArray(Tab[]::new)); | ||
configNamesPane.setSide(Side.LEFT); | ||
configNamesPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); | ||
configNamesPane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER); | ||
return new VBox(configNamesPane); | ||
}).get()); | ||
|
||
final var pane = new TabPane(configTab); | ||
pane.setMaxWidth(-1); | ||
final var stage = new Stage(); | ||
pane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE); | ||
pane.setTabDragPolicy(TabPane.TabDragPolicy.REORDER); | ||
stage.setTitle("%s configuration".formatted(botType.getNiceName())); | ||
stage.getIcons().add(new Image(botUserData.avatarUrl())); | ||
stage.setScene(new Scene(new HBox(pane))); | ||
stage.setResizable(true); | ||
stage.setFullScreen(true); | ||
stage.showAndWait(); | ||
} | ||
|
||
public final Node makeConfigBox(ConfigEntry entry) { | ||
try { | ||
FXMLLoader fxmlLoader = new FXMLLoader(); | ||
fxmlLoader.setLocation(DashboardApplication.class.getResource("/guis/config_box/" + entry.type().getConfigBoxName())); | ||
final var constructor = getConstructor(entry.type().getControllerClassName()); | ||
final var controller = constructor.invoke(botType, entry.type(), entry.configName(), entry.path(), entry.comments()); | ||
fxmlLoader.setController(controller); | ||
final Node toRet = fxmlLoader.load(); | ||
((ConfigBoxController) fxmlLoader.getController()).init(); | ||
return toRet; | ||
} catch (Throwable e) { | ||
// If something is wrong, let it crash | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static final Map<String, MethodHandle> CONSTRUCTORS = new HashMap<>(); | ||
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); | ||
|
||
public static MethodHandle getConstructor(String className) { | ||
return CONSTRUCTORS.computeIfAbsent(className, k -> { | ||
try { | ||
final var clz = Class.forName(className); | ||
final var methodType = MethodType.methodType(void.class, BotTypeEnum.class, DashConfigType.class, String.class, String.class, String[].class); | ||
return LOOKUP.findConstructor(clz, methodType); | ||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} |
24 changes: 19 additions & 5 deletions
24
dashboard/src/client/java/com/mcmoddev/mmdbot/dashboard/client/scenes/bot/MMDBotStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
package com.mcmoddev.mmdbot.dashboard.client.scenes.bot; | ||
|
||
import com.mcmoddev.mmdbot.dashboard.util.BotUserData; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.mcmoddev.mmdbot.dashboard.BotTypeEnum; | ||
import com.mcmoddev.mmdbot.dashboard.util.DashConfigType; | ||
|
||
public final class MMDBotStage implements BotStage { | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
MMDBotStage() {} | ||
public final class MMDBotStage extends DefaultBotStage { | ||
|
||
@Override | ||
public void createAndShowStage(BotUserData botUserData) { | ||
private static final ConfigEntry THING = new ConfigEntry(DashConfigType.STRING, "prefix", "commands.prefix.main", new String[] {"The prefix of the bot"}); | ||
|
||
public static final Map<String, List<ConfigEntry>> CONFIGS = new ImmutableMap.Builder<String, List<ConfigEntry>>() | ||
.put("someConfig", List.of( | ||
new ConfigEntry(DashConfigType.STRING, "botOwner", "bot.owner", new String[] {"Something, yes"}) | ||
)) | ||
.put("anotherConfig", List.of( | ||
THING, THING, THING, THING,THING, THING, THING, THING, THING, THING, THING, THING, THING | ||
)) | ||
.build(); | ||
|
||
MMDBotStage() { | ||
super(CONFIGS, BotTypeEnum.MMDBOT); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
dashboard/src/client/java/com/mcmoddev/mmdbot/dashboard/client/util/Constants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mcmoddev.mmdbot.dashboard.client.util; | ||
|
||
import com.google.common.base.Joiner; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class Constants { | ||
|
||
public static final Joiner LINE_JOINER = Joiner.on("\n"); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
dashboard/src/client/resources/guis/config_box/default.fxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?import javafx.scene.text.*?> | ||
<?import java.net.*?> | ||
<?import javafx.scene.control.*?> | ||
<?import java.lang.*?> | ||
<?import javafx.scene.layout.*?> | ||
|
||
<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="141.0" stylesheets="@../../themes/dark.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> | ||
<children> | ||
<Label fx:id="configNameLabel" alignment="CENTER" prefHeight="27.0" prefWidth="600.0" text="Cfg name" textAlignment="CENTER"> | ||
<font> | ||
<Font size="23.0" /> | ||
</font></Label> | ||
<Label fx:id="commentsLabel" text="Comments" textAlignment="CENTER" /> | ||
<TextField fx:id="valueField" promptText="Value" text="Current Value" /> | ||
<Button mnemonicParsing="false" onAction="#onUpdateValueClick" text="Update value" /> | ||
</children> | ||
<styleClass> | ||
<String fx:value="config_box" /> | ||
<String fx:value="default" /> | ||
</styleClass> | ||
</VBox> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.