Skip to content

Commit

Permalink
Redesign imagetext GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Zailer43 committed Jul 18, 2024
1 parent ce8d9bb commit 6beaf70
Show file tree
Hide file tree
Showing 23 changed files with 1,045 additions and 346 deletions.
24 changes: 16 additions & 8 deletions src/main/java/fzmm/zailer/me/client/gui/BaseFzmmScreen.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package fzmm.zailer.me.client.gui;

import fzmm.zailer.me.client.FzmmClient;
import fzmm.zailer.me.client.gui.components.BooleanButton;
import fzmm.zailer.me.client.gui.components.EnumWidget;
import fzmm.zailer.me.client.gui.components.SliderWidget;
import fzmm.zailer.me.client.gui.components.*;
import fzmm.zailer.me.client.gui.components.image.ImageButtonComponent;
import fzmm.zailer.me.client.gui.components.image.ScreenshotZoneComponent;
import fzmm.zailer.me.client.gui.components.row.*;
Expand Down Expand Up @@ -130,20 +128,24 @@ protected void restoreMementoTabs(HashMap<String, IMementoObject> mementoTabs, H
}

public <T extends Enum<? extends IScreenTabIdentifier>> T selectScreenTab(FlowLayout rootComponent, IScreenTabIdentifier selectedTab, T tabs) {
return this.selectScreenTab(rootComponent, selectedTab, tabs, this.tabs);
return this.selectScreenTab(rootComponent, selectedTab, tabs, this.tabs, true);
}

@SuppressWarnings("unchecked")
public <T extends Enum<? extends IScreenTabIdentifier>> T selectScreenTab(FlowLayout rootComponent, IScreenTabIdentifier selectedTab, T tabs, HashMap<String, IScreenTab> tabsHashMap) {
public <T extends Enum<? extends IScreenTabIdentifier>> T selectScreenTab(FlowLayout rootComponent, IScreenTabIdentifier selectedTab,
T tabs, HashMap<String, IScreenTab> tabsHashMap, boolean addLabel) {
for (var tabId : tabsHashMap.keySet()) {
ScreenTabContainer screenTabContainer = rootComponent.childById(ScreenTabContainer.class, ScreenTabContainer.getScreenTabId(tabId));
ButtonWidget screenTabButton = rootComponent.childById(ButtonWidget.class, ScreenTabRow.getScreenTabButtonId(tabId));
boolean isSelectedTab = selectedTab.getId().equals(tabId);
if (screenTabContainer != null)
screenTabContainer.setSelected(isSelectedTab);

if (screenTabButton != null)
if (screenTabContainer != null) {
screenTabContainer.setSelected(isSelectedTab, addLabel);
}

if (screenTabButton != null) {
screenTabButton.active = !isSelectedTab;
}
}


Expand Down Expand Up @@ -242,8 +244,10 @@ public SymbolChatCompat getSymbolChatCompat() {

// these are necessary in case you want to create the fields manually with XML
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "boolean-button"), BooleanButton::parse);
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "context-menu-button"), element -> new ContextMenuButton(Text.empty()));
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "number-slider"), element -> new SliderWidget());
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "text-option"), element -> new ConfigTextBox());
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "suggest-text-option"), element -> new SuggestionTextBox());
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "image-option"), element -> new ImageButtonComponent());
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "enum-option"), element -> new EnumWidget());
UIParsing.registerFactory(Identifier.of(FzmmClient.MOD_ID, "screen-tab"), ScreenTabContainer::parse);
Expand All @@ -262,4 +266,8 @@ public static void checkNull(Component component, String componentTagName, Strin
public UIModel getModel() {
return this.model;
}

public FlowLayout getRoot() {
return this.uiAdapter.rootComponent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package fzmm.zailer.me.client.gui.components;

import fzmm.zailer.me.client.gui.BaseFzmmScreen;
import io.wispforest.owo.ui.component.ButtonComponent;
import io.wispforest.owo.ui.component.DropdownComponent;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.Component;
import io.wispforest.owo.ui.core.Sizing;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Consumer;

public class ContextMenuButton extends ButtonComponent {
@Nullable
private DropdownComponent contextMenu = null;
protected int additionalZIndex = 200;
private Consumer<DropdownComponent> contextMenuOptionsConsumer = dropdownComponent -> {};

public ContextMenuButton(Text text) {
super(text, button -> {
});
this.verticalSizing(Sizing.fixed(20));
}

@Override
public void onPress() {
Screen screen = MinecraftClient.getInstance().currentScreen;
if (!(screen instanceof BaseFzmmScreen baseScreen)) {
return;
}

//TODO: close contextMenu if it's already open
if (this.contextMenu == null) {
DropdownComponent.openContextMenu(baseScreen, baseScreen.getRoot(), FlowLayout::child,
this.x(), this.y() + this.height(), contextMenu -> {
this.contextMenu = contextMenu;
this.contextMenuOptionsConsumer.accept(contextMenu) ;

List<Component> dropdownChildren = contextMenu.children();
if (!dropdownChildren.isEmpty()) {
dropdownChildren.get(0).horizontalSizing(Sizing.fixed(this.width()));
}

contextMenu.zIndex(this.zIndex() + this.additionalZIndex);
// fixes that if you click on the margins zone it clicks on the component behind the dropdown
contextMenu.mouseDown().subscribe((mouseX1, mouseY1, button1) -> true);
});
} else {
this.removeContextMenu();
}
}

public void setContextMenuOptions(Consumer<DropdownComponent> contextMenuOptionsConsumer) {
this.contextMenuOptionsConsumer = contextMenuOptionsConsumer;
}

public void additionalZIndex(int additionalZIndex) {
this.additionalZIndex = additionalZIndex;
}

public void removeContextMenu() {
if (this.contextMenu != null) {
this.contextMenu.remove();
this.contextMenu = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import fzmm.zailer.me.client.gui.components.style.StyledComponents;
import fzmm.zailer.me.client.gui.components.style.StyledContainers;
import fzmm.zailer.me.client.gui.components.style.container.StyledFlowLayout;
import io.wispforest.owo.config.ui.component.ConfigTextBox;
import fzmm.zailer.me.compat.symbol_chat.font.FontTextBoxComponent;
import io.wispforest.owo.ui.component.DropdownComponent;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.container.Containers;
Expand All @@ -27,8 +27,7 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;

@SuppressWarnings("UnstableApiUsage")
public class SuggestionTextBox extends ConfigTextBox {
public class SuggestionTextBox extends FontTextBoxComponent {
private static final int SUGGESTION_HEIGHT = 16;
private final SuggestionPosition suggestionPosition;
private SuggestionProvider<?> suggestionProvider;
Expand All @@ -44,9 +43,12 @@ public class SuggestionTextBox extends ConfigTextBox {
@Nullable
private FlowLayout suggestionsLayout = null;

public SuggestionTextBox() {
this(Sizing.content(), SuggestionPosition.BOTTOM, 5);
}

public SuggestionTextBox(Sizing horizontalSizing, SuggestionPosition position, int maxSuggestionLines) {
super();
this.horizontalSizing(horizontalSizing);
super(horizontalSizing);
this.suggestionProvider = (nul, builder) -> CompletableFuture.completedFuture(builder.build());
this.suggestionPosition = position;

Expand All @@ -61,18 +63,18 @@ private void openContextMenu() {
return;
}

this.suggestionsContextMenu = DropdownComponent.openContextMenu(screen, parent, FlowLayout::child, this.x(), this.y(), categoryDropdown -> {
categoryDropdown.clearChildren();
this.suggestionsContextMenu = DropdownComponent.openContextMenu(screen, parent, FlowLayout::child, this.x(), this.y(), suggestionDropdown -> {
suggestionDropdown.clearChildren();

this.suggestionsLayout = StyledContainers.verticalFlow(Sizing.fill(100), Sizing.content());
this.suggestionsContainer = Containers.verticalScroll(this.horizontalSizing().get(),
this.suggestionsContainer = Containers.verticalScroll(Sizing.fixed(this.width()),
Sizing.expand(100), this.suggestionsLayout);


categoryDropdown.child(this.suggestionsContainer);
suggestionDropdown.child(this.suggestionsContainer);
});

this.suggestionsContextMenu.zIndex(200);
this.suggestionsContextMenu.zIndex(this.zIndex() + 100);
this.updateSuggestions(this.getText());
}

Expand All @@ -93,8 +95,7 @@ private void closeContextMenu() {
private boolean contextMenuIsOpen() {
return this.suggestionsContextMenu != null &&
this.suggestionsContainer != null &&
this.suggestionsLayout != null &&
this.suggestionsContextMenu.hasParent();
this.suggestionsLayout != null;
}

private boolean updateSelectedSuggestionIndex(int addIndex) {
Expand Down Expand Up @@ -135,7 +136,6 @@ private void updateSuggestions(String newMessage) {
this.openContextMenu();
return;
}
assert this.suggestionsLayout != null;
this.suggestionsLayout.clearChildren();

String newMessageToLowerCase = newMessage.toLowerCase();
Expand Down Expand Up @@ -290,7 +290,6 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
this.openContextMenu();
yield true;
}
assert this.suggestionsLayout != null;
if (this.suggestionsLayout.children().isEmpty()) {
this.updateSuggestions(this.getText());
yield !this.suggestionsLayout.children().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,28 @@ public static SliderWidget setup(FlowLayout rootComponent, String id, double def
ButtonComponent resetButton = rootComponent.childById(ButtonComponent.class, getResetButtonId(id));

BaseFzmmScreen.checkNull(numberSlider, "number-slider", getSliderId(id));
BaseFzmmScreen.checkNull(resetButton, "button", getResetButtonId(id));

numberSlider.decimalPlaces(decimalPlaces);
numberSlider.valueType(numberType);
numberSlider.onChanged().subscribe(aDouble -> {
double discreteValue = numberSlider.discreteValue();
resetButton.active = discreteValue != defaultValue;
if (callback != null)
if (resetButton != null) {
resetButton.active = discreteValue != defaultValue;
}
if (callback != null) {
callback.accept(discreteValue);
}
});
numberSlider.min(min);
numberSlider.max(max);
numberSlider.setFromDiscreteValue(defaultValue);
numberSlider.updateMessage();
numberSlider.scrollStep(scrollStep / (max - min));

resetButton.onPress(button -> numberSlider.setFromDiscreteValue(defaultValue));
resetButton.active = false;
if (resetButton != null) {
resetButton.onPress(button -> numberSlider.setFromDiscreteValue(defaultValue));
resetButton.active = false;
}
return numberSlider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TextBoxRow(String baseTranslationKey, String id, String tooltipId, boolea

FontTextBoxComponent fontTextBoxComponent = this.childById(FontTextBoxComponent.class, getTextBoxId(id));
if (fontTextBoxComponent != null)
fontTextBoxComponent.setEnabled(symbolChatButtons);
fontTextBoxComponent.enableFontProcess(symbolChatButtons);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ public static void setup(FlowLayout rootComponent, String id, IImageGetter defau

@SuppressWarnings("UnstableApiUsage")
public static void setupSuggestionTextBox(SuggestionTextBox suggestionTextBox, IImageGetter imageGetter) {
if (imageGetter instanceof IImageLoaderFromText imageLoaderFromText)
if (imageGetter instanceof IImageLoaderFromText imageLoaderFromText) {
suggestionTextBox.applyPredicate(imageLoaderFromText::predicate);
}

suggestionTextBox.setSuggestionProvider(imageGetter instanceof IImageSuggestion imageSuggestion ?
imageSuggestion.getSuggestionProvider() :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
public class ImageRows extends StyledFlowLayout {
public static int TOTAL_HEIGHT = AbstractRow.TOTAL_HEIGHT * 2;

public ImageRows(String baseTranslationKey, String buttonId, String imageModeId, boolean translate) {
this(baseTranslationKey, buttonId, buttonId, imageModeId, imageModeId, translate);
}

public ImageRows(String baseTranslationKey, String buttonId, String buttonTooltipId, String imageModeId, String imageTooltipId, boolean translate) {
super(Sizing.fill(100), Sizing.fixed(TOTAL_HEIGHT), Algorithm.HORIZONTAL);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import fzmm.zailer.me.client.gui.components.style.StyledContainers;
import io.wispforest.owo.ui.container.ScrollContainer;
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.parsing.UIModel;
import io.wispforest.owo.ui.parsing.UIParsing;
import io.wispforest.owo.ui.util.NinePatchTexture;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.math.MathHelper;
import org.w3c.dom.Element;

import java.util.Map;

public class StyledScrollContainer<C extends Component> extends ScrollContainer<C> {
public static final int SCROLLBAR_THICCNESS = 5;
protected boolean preventShiftScroll = false;

public StyledScrollContainer(ScrollDirection direction, Sizing horizontalSizing, Sizing verticalSizing, C child) {
super(direction, horizontalSizing, verticalSizing, child);
Expand All @@ -33,6 +39,29 @@ public ScrollContainer.Scrollbar styledScrollbar() {
return isOverrideScrollbar ? vanillaFlat() : flat(Color.WHITE);
}

public boolean preventShiftScroll() {
return preventShiftScroll;
}

public void preventShiftScroll(boolean isShiftRequired) {
this.preventShiftScroll = isShiftRequired;
}

@Override
public boolean onMouseScroll(double mouseX, double mouseY, double amount) {
if (this.preventShiftScroll && Screen.hasShiftDown()) {
return false;
}
return super.onMouseScroll(mouseX, mouseY, amount);
}

@Override
public void parseProperties(UIModel model, Element element, Map<String, Element> children) {
super.parseProperties(model, element, children);
UIParsing.apply(children, "prevent-shift-scroll", UIParsing::parseBool, this::preventShiftScroll);
//TODO flip scrollbar (vertical from right side to left side, horizontal from bottom to top)
}

public static StyledScrollContainer<?> parse(Element element) {
return element.getAttribute("direction").equals("vertical")
? StyledContainers.verticalScroll(Sizing.content(), Sizing.content(), null)
Expand Down
Loading

0 comments on commit 6beaf70

Please sign in to comment.