Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Gui system update
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkKronicle committed Jan 26, 2022
1 parent 461c1e7 commit 4365614
Show file tree
Hide file tree
Showing 13 changed files with 565 additions and 134 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod_version=1.4.0-beta
maven_group=io.github.darkkronicle
archives_base_name=AdvancedChatCore

kommandlib_version=1.0.0-build1
kommandlib_version=1.0.0-build2
malilib_version = 0.10.0-dev.26
konstruct_version=2.0.2-build1
mxparser_version=4.4.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package io.github.darkkronicle.advancedchatcore;

import fi.dy.masa.malilib.config.ConfigManager;
import fi.dy.masa.malilib.config.IConfigBase;
import fi.dy.masa.malilib.interfaces.IInitializationHandler;
import io.github.darkkronicle.advancedchatcore.chat.ChatHistoryProcessor;
import io.github.darkkronicle.advancedchatcore.chat.ChatScreenSectionHolder;
Expand Down Expand Up @@ -40,16 +41,21 @@ public void registerModHandlers() {
// Setup chat history
MessageDispatcher.getInstance().register(new ChatHistoryProcessor(), -1);

GuiConfigHandler.getInstance()
.addGuiSection(
GuiConfigHandler.createGuiConfigSection(
"advancedchat.config.tab.general", ConfigStorage.General.OPTIONS));

GuiConfigHandler.getInstance()
.addGuiSection(
GuiConfigHandler.createGuiConfigSection(
"advancedchat.config.tab.chatscreen",
ConfigStorage.ChatScreen.OPTIONS));
GuiConfigHandler.getInstance().addTab(
GuiConfigHandler.children(
"advancedchatcore",
"advancedchat.tab.advancedchatcore",
GuiConfigHandler.wrapOptions(
"core_general",
"advancedchatcore.tab.general",
ConfigStorage.General.OPTIONS.stream().map((saveableConfig) -> (IConfigBase) saveableConfig.config).toList()
),
GuiConfigHandler.wrapOptions(
"chatscreen",
"advancedchatcore.tab.chatscreen",
ConfigStorage.ChatScreen.OPTIONS.stream().map((saveableConfig) -> (IConfigBase) saveableConfig.config).toList()
))
);

ProfanityUtil.getInstance().loadConfigs();
MessageDispatcher.getInstance().registerPreFilter(text -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@
package io.github.darkkronicle.advancedchatcore.chat;

import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.gui.button.ButtonBase;
import fi.dy.masa.malilib.util.KeyCodes;
import fi.dy.masa.malilib.util.StringUtils;
import io.github.darkkronicle.advancedchatcore.AdvancedChatCore;
import io.github.darkkronicle.advancedchatcore.config.ConfigStorage;
import io.github.darkkronicle.advancedchatcore.config.gui.GuiConfigHandler;
import io.github.darkkronicle.advancedchatcore.gui.CleanButton;
import io.github.darkkronicle.advancedchatcore.gui.IconButton;
import io.github.darkkronicle.advancedchatcore.interfaces.AdvancedChatScreenSection;
import io.github.darkkronicle.advancedchatcore.util.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import io.github.darkkronicle.advancedchatcore.util.RowList;
import lombok.Getter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;

public class AdvancedChatScreen extends GuiBase {
Expand All @@ -41,6 +45,12 @@ public class AdvancedChatScreen extends GuiBase {
private static String last = "";
private final List<AdvancedChatScreenSection> sections = new ArrayList<>();

@Getter
private final RowList<ButtonBase> rightSideButtons = new RowList<>();

@Getter
private final RowList<ButtonBase> leftSideButtons = new RowList<>();

@Override
protected void closeGui(boolean showParent) {
if (ConfigStorage.ChatScreen.PERSISTENT_TEXT.config.getBooleanValue()) {
Expand Down Expand Up @@ -95,24 +105,8 @@ protected MutableText getNarrationMessage() {
}
this.chatField.setChangedListener(this::onChatFieldUpdate);

Color baseColor = getColor();
int x = client.getWindow().getScaledWidth() - 1;
// Add settings button
String settings = StringUtils.translate("advancedchat.gui.button.settings");
int settingsWidth = StringUtils.getStringWidth(settings) + 5;
x -= settingsWidth + 5;
CleanButton settingsButton =
new CleanButton(
x,
client.getWindow().getScaledHeight() - 27,
settingsWidth,
11,
baseColor,
settings);
this.addButton(
settingsButton,
(button, mouseButton) ->
GuiBase.openGui(GuiConfigHandler.getInstance().getDefaultScreen()));
rightSideButtons.add("settings", new IconButton(0, 0, 14, 64, new Identifier(AdvancedChatCore.MOD_ID, "textures/gui/settings.png"), (button) -> GuiBase.openGui(GuiConfigHandler.getInstance().getDefaultScreen())));

this.addSelectableChild(this.chatField);

Expand All @@ -121,6 +115,35 @@ protected MutableText getNarrationMessage() {
for (AdvancedChatScreenSection section : sections) {
section.initGui();
}

int originalX = client.getWindow().getScaledWidth() - 1;
int y = client.getWindow().getScaledHeight() - 30;
for (int i = 0; i < rightSideButtons.rowSize(); i++) {
List<ButtonBase> buttonList = rightSideButtons.get(i);
int maxHeight = 0;
int x = originalX;
for (ButtonBase button : buttonList) {
maxHeight = Math.max(maxHeight, button.getHeight());
x -= button.getWidth() + 1;
button.setPosition(x, y);
addButton(button, null);
}
y -= maxHeight + 1;
}
originalX = 1;
y = client.getWindow().getScaledHeight() - 30;
for (int i = 0; i < leftSideButtons.rowSize(); i++) {
List<ButtonBase> buttonList = leftSideButtons.get(i);
int maxHeight = 0;
int x = originalX;
for (ButtonBase button : buttonList) {
maxHeight = Math.max(maxHeight, button.getHeight());
button.setPosition(x, y);
addButton(button, null);
x += button.getWidth() + 1;
}
y -= maxHeight + 1;
}
}

public void resize(MinecraftClient client, int width, int height) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public class ChatHistoryProcessor implements IMessageProcessor {

private static boolean sendToHud(Text text) {
if (AdvancedChatCore.FORWARD_TO_HUD) {
((MixinChatHudInvoker) MinecraftClient.getInstance().inGameHud.getChatHud())
.invokeAddMessage(
text, 0, MinecraftClient.getInstance().inGameHud.getTicks(), false);
((MixinChatHudInvoker) MinecraftClient.getInstance().inGameHud.getChatHud()).invokeAddMessage(
text, 0, MinecraftClient.getInstance().inGameHud.getTicks(), false);
return true;
}
return false;
Expand Down Expand Up @@ -59,17 +58,16 @@ public boolean process(FluidText text, @Nullable FluidText unfiltered) {
MessageOwner player =
SearchUtils.getAuthor(
MinecraftClient.getInstance().getNetworkHandler(), unfiltered.getString());
ChatMessage line =
ChatMessage.builder()
.displayText(text)
.originalText(original)
.owner(player)
.id(0)
.width(width)
.creationTick(MinecraftClient.getInstance().inGameHud.getTicks())
.time(time)
.backgroundColor(backcolor)
.build();
ChatMessage line = ChatMessage.builder()
.displayText(text)
.originalText(original)
.owner(player)
.id(0)
.width(width)
.creationTick(MinecraftClient.getInstance().inGameHud.getTicks())
.time(time)
.backgroundColor(backcolor)
.build();
if (ChatHistory.getInstance().add(line)) {
sendToHud(line.getDisplayText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,170 @@
import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.gui.GuiConfigsBase;
import fi.dy.masa.malilib.gui.button.ButtonBase;
import fi.dy.masa.malilib.gui.button.ButtonGeneric;
import fi.dy.masa.malilib.gui.button.IButtonActionListener;
import fi.dy.masa.malilib.util.Color4f;
import io.github.darkkronicle.advancedchatcore.AdvancedChatCore;
import net.minecraft.client.gui.screen.Screen;

import java.util.List;

// Based off of
// https://github.com/maruohon/minihud/blob/fabric_1.16_snapshots_temp/src/main/java/fi/dy/masa/minihud/gui/GuiConfigs.java
// Released under GNU LGPL
public class GuiConfig extends GuiConfigsBase {

private List<GuiConfigHandler.TabButton> tabButtons;
private List<IConfigBase> configs;
public static TabSupplier TAB = null;

@Deprecated
public GuiConfig(List<GuiConfigHandler.TabButton> tabButtons, List<IConfigBase> configs) {
this();
}

public GuiConfig() {
super(10, 50, AdvancedChatCore.MOD_ID, null, "advancedchat.screen.main");
this.tabButtons = tabButtons;
this.configs = configs;
}

@Override
public void initGui() {
super.initGui();
this.clearOptions();
if (TAB == null) {
// Should be general
for (int i = 0; i < GuiConfigHandler.getInstance().getTabs().size(); i++) {
TabSupplier tab = GuiConfigHandler.getInstance().getTabs().get(i);
if (tab.isSelectable()) {
TAB = tab;
break;
}
}
if (TAB == null) {
// sucks to suck lol
TAB = GuiConfigHandler.getInstance().getTabs().get(0);
}
}
boolean children = TAB.getChildren() != null && TAB.getChildren().size() != 0;

Screen child = getFullyNestedSupplier(TAB).getScreen(this);
if (child != null) {
GuiBase.openGui(child);
return;
}

clearElements();
int x = 10;
int y = 26;
int rows = 1;
int rows = addTabButtons(this, x, y);
y += rows * 22;
if (children) {
y += (addAllChildrenButtons(this, TAB, x, y) * 22);
}
setListPosition(getListX(), y + 10);
if (this.getListWidget() != null) {
this.getListWidget().setSize(this.getBrowserWidth(), this.getBrowserHeight());
this.getListWidget().initGui();
}

}

@Override
public List<ConfigOptionWrapper> getConfigs() {
return ConfigOptionWrapper.createFor(getFullyNestedSupplier(TAB).getOptions());
}

public static TabSupplier getFullyNestedSupplier(TabSupplier supplier) {
if (supplier.getChildren() == null || supplier.getChildren().size() == 0) {
return supplier;
}
return getFullyNestedSupplier(supplier.getNestedSelection());
}

for (GuiConfigHandler.TabButton tab : tabButtons) {
int newY = this.createButton(tab, y);
if (newY != y) {
rows++;
y = newY;
public static int addAllChildrenButtons(GuiBase screen, TabSupplier supplier, int x, int y) {
int rows = 0;
if (supplier.getChildren() != null && supplier.getChildren().size() != 0) {
x += 2;
screen.addLabel(x, y, 10, 22, new Color4f(1, 1, 1, 1).intValue, ">");
x += 8;
addNestedTabButtons(screen, supplier, x, y);
y += 22;
rows++;
if (supplier.getNestedSelection() != null) {
rows += addAllChildrenButtons(screen, supplier.getNestedSelection(), x, y);
}
}
return rows;
}

/**
* Adds the category buttons to the selected screen
* @param screen Screen to apply to
* @return Amount of rows it created
*/
public static int addTabButtons(GuiBase screen, int x, int y) {
int rows = 1;
for (TabSupplier tab : GuiConfigHandler.getInstance().getTabs()) {
int width = screen.getStringWidth(tab.getDisplayName()) + 10;

if (rows > 1) {
int scrollbarPosition = this.getListWidget().getScrollbar().getValue();
this.setListPosition(this.getListX(), 50 + (rows - 1) * 22);
this.reCreateListWidget();
this.getListWidget().getScrollbar().setValue(scrollbarPosition);
this.getListWidget().refreshEntries();
if (x >= screen.width - width - 10)
{
x = 10;
y += 22;
++rows;
}

x += createTabButton(screen, x, y, width, tab);
}
return rows;
}

private int createButton(GuiConfigHandler.TabButton button, int y) {
this.addButton(button.getButton(), new ButtonListenerConfigTabs(button));
return button.getButton().getY();
public static int addNestedTabButtons(GuiBase screen, TabSupplier supplier, int x, int y) {
int rows = 1;
for (TabSupplier tab : supplier.getChildren()) {
int width = screen.getStringWidth(tab.getDisplayName()) + 10;

if (x >= screen.width - width - 10)
{
x = 10;
y += 22;
++rows;
}

x += createTabButton(screen, x, y, width, tab, supplier);
}
return rows;
}

@Override
public List<ConfigOptionWrapper> getConfigs() {
return ConfigOptionWrapper.createFor(configs);
private static int createTabButton(GuiBase screen, int x, int y, int width, TabSupplier tab) {
return createTabButton(screen, x, y, width, tab, null);
}

private static class ButtonListenerConfigTabs implements IButtonActionListener {
private static int createTabButton(GuiBase screen, int x, int y, int width, TabSupplier tab, TabSupplier parent) {
ButtonGeneric button = new ButtonGeneric(x, y, width, 20, tab.getDisplayName());
if (parent == null) {
button.setEnabled(GuiConfig.TAB != tab);
} else {
button.setEnabled(parent.getNestedSelection() != tab);
}
screen.addButton(button, new ButtonListenerTab(tab, parent));

return button.getWidth() + 2;
}

private final GuiConfigHandler.TabButton tabButton;
public static class ButtonListenerTab implements IButtonActionListener {
private final TabSupplier tab;
private final TabSupplier parent;

public ButtonListenerConfigTabs(GuiConfigHandler.TabButton tabButton) {
this.tabButton = tabButton;
public ButtonListenerTab(TabSupplier tab, TabSupplier parent) {
this.tab = tab;
this.parent = parent;
}

@Override
public void actionPerformedWithButton(ButtonBase button, int mouseButton) {
GuiConfigHandler.getInstance().activeTab = this.tabButton.getTab().getName();
GuiBase.openGui(
this.tabButton.getTab().getScreen(GuiConfigHandler.getInstance().getButtons()));
if (parent == null) {
GuiConfig.TAB = this.tab;
} else {
parent.setNestedSelection(this.tab);
}
GuiBase.openGui(new GuiConfig());
}
}
}
Loading

0 comments on commit 4365614

Please sign in to comment.