Skip to content

Commit

Permalink
Implement boundaries config and add option for panel alignment
Browse files Browse the repository at this point in the history
Close #1502
  • Loading branch information
shedaniel committed Nov 3, 2023
1 parent f3b284c commit b0c1904
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ default boolean isLeftHandSidePanel() {
@ApiStatus.Experimental
double getVerticalEntriesBoundariesRows();

@ApiStatus.Experimental
double getHorizontalEntriesBoundariesAlignments();

@ApiStatus.Experimental
double getVerticalEntriesBoundariesAlignments();

@ApiStatus.Experimental
double getFavoritesHorizontalEntriesBoundariesPercentage();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is licensed under the MIT License, part of Roughly Enough Items.
* Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package me.shedaniel.rei.api.client.gui.config;

public record PanelBoundary(
double horizontalPercentage,
double verticalPercentage,
int horizontalLimit,
int verticalLimit,
double horizontalAlign,
double verticalAlign
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,16 @@ public double getVerticalEntriesBoundariesRows() {
return Mth.clamp(appearance.verticalEntriesBoundariesRows, 1, 1000);
}

@Override
public double getHorizontalEntriesBoundariesAlignments() {
return appearance.horizontalEntriesBoundariesAlignment;
}

@Override
public double getVerticalEntriesBoundariesAlignments() {
return appearance.verticalEntriesBoundariesAlignment;
}

@ApiStatus.Experimental
@Override
public double getFavoritesHorizontalEntriesBoundariesPercentage() {
Expand Down Expand Up @@ -665,6 +675,8 @@ public static class Layout {
@UsePercentage(min = 0.1, max = 1.0, prefix = "Limit: ") public double verticalEntriesBoundaries = 1.0;
public int horizontalEntriesBoundariesColumns = 50;
public int verticalEntriesBoundariesRows = 1000;
public double horizontalEntriesBoundariesAlignment = 1.0;
public double verticalEntriesBoundariesAlignment = 0.5;
@UsePercentage(min = 0.1, max = 1.0, prefix = "Limit: ") public double favoritesHorizontalEntriesBoundaries = 1.0;
public int favoritesHorizontalEntriesBoundariesColumns = 50;
@UseSpecialSearchFilterSyntaxHighlightingScreen public SyntaxHighlightingMode syntaxHighlightingMode = SyntaxHighlightingMode.COLORFUL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,13 @@ public Point getMousePosition() {
private static Rectangle calculateOverlayBounds() {
Rectangle bounds = ScreenRegistry.getInstance().getOverlayBounds(ConfigObject.getInstance().getDisplayPanelLocation(), Minecraft.getInstance().screen);

double hAlign = ConfigObject.getInstance().getDisplayPanelLocation() == DisplayPanelLocation.LEFT ? 1 - ConfigObject.getInstance().getHorizontalEntriesBoundariesAlignments() : ConfigObject.getInstance().getHorizontalEntriesBoundariesAlignments();
int widthReduction = (int) Math.round(bounds.width * (1 - ConfigObject.getInstance().getHorizontalEntriesBoundariesPercentage()));
if (ConfigObject.getInstance().getDisplayPanelLocation() == DisplayPanelLocation.RIGHT)
bounds.x += widthReduction;
bounds.x += (int) Math.round(widthReduction * hAlign);
bounds.width -= widthReduction;
int maxWidth = (int) Math.ceil(entrySize() * ConfigObject.getInstance().getHorizontalEntriesBoundariesColumns() + entrySize() * 0.75);
if (bounds.width > maxWidth) {
if (ConfigObject.getInstance().getDisplayPanelLocation() == DisplayPanelLocation.RIGHT)
bounds.x += bounds.width - maxWidth;
bounds.x += (int) Math.round((bounds.width - maxWidth) * hAlign);
bounds.width = maxWidth;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import me.shedaniel.rei.api.client.gui.widgets.Widgets;
import me.shedaniel.rei.api.client.util.MatrixUtils;
import me.shedaniel.rei.api.common.util.CollectionUtils;
import me.shedaniel.rei.api.common.util.FormattingUtils;
import me.shedaniel.rei.impl.client.gui.config.ConfigAccess;
import me.shedaniel.rei.impl.client.gui.config.REIConfigScreen;
import me.shedaniel.rei.impl.client.gui.config.options.CompositeOption;
Expand All @@ -44,14 +43,11 @@
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;

import java.lang.ref.Reference;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
Expand All @@ -68,7 +64,7 @@ public static <T> WidgetWithBounds create(ConfigAccess access, CompositeOption<T
text[0] = literal("> ").withStyle(ChatFormatting.YELLOW)
.append(t.copy().withStyle(ChatFormatting.YELLOW))
.append(literal(" <").withStyle(ChatFormatting.YELLOW));
} else if (access.get(option).equals(Objects.requireNonNullElseGet(option.getDefaultValue(), () -> access.getDefault(option)))) {
} else if (!(option.getEntry() instanceof OptionValueEntry.Configure<T>) && access.get(option).equals(Objects.requireNonNullElseGet(option.getDefaultValue(), () -> access.getDefault(option)))) {
text[0] = translatable("config.rei.value.default", t);

if (font.width(text[0]) > width) {
Expand Down Expand Up @@ -106,6 +102,13 @@ public static <T> WidgetWithBounds create(ConfigAccess access, CompositeOption<T
applySelection(access, option, selection, label, setText, matrix);
} else if (access.get(option) instanceof ModifierKeyCode) {
applyKeycode(access, option, label, setText, matrix);
} else if (option.getEntry() instanceof OptionValueEntry.Configure<T>) {
label.clickable().onClick($ -> {
((OptionValueEntry.Configure<T>) option.getEntry()).configure(access, option, () -> {
Minecraft.getInstance().setScreen((Screen) access);
setText.accept(option.getEntry().getOption(access.get(option)));
});
});
}

return Widgets.concatWithBounds(() -> new Rectangle(-label.getBounds().width, 0, label.getBounds().width + 8, 14),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import me.shedaniel.rei.api.client.gui.config.*;
import me.shedaniel.rei.impl.client.config.ConfigObjectImpl;
import me.shedaniel.rei.impl.client.gui.config.REIConfigScreen;
import me.shedaniel.rei.impl.client.gui.config.options.configure.PanelBoundariesConfiguration;
import net.minecraft.client.Minecraft;

import java.util.function.BiConsumer;
Expand Down Expand Up @@ -103,9 +104,18 @@ static <T> CompositeOption<T> make(String id, Function<ConfigObjectImpl, T> bind
}));
CompositeOption<Boolean> CRAFTABLE_FILTER = make("layout.craftable_filter", i -> i.appearance.layout.showCraftableOnlyButton, (i, v) -> i.appearance.layout.showCraftableOnlyButton = v)
.enabledDisabled();
// TODO: BOUNDARIES
CompositeOption<Boolean> BOUNDARIES = make("layout.boundaries", i -> true, (i, v) -> {
});
CompositeOption<PanelBoundary> BOUNDARIES = make("layout.boundaries", i -> {
return new PanelBoundary(i.appearance.horizontalEntriesBoundaries, i.appearance.verticalEntriesBoundaries,
i.appearance.horizontalEntriesBoundariesColumns, i.appearance.verticalEntriesBoundariesRows,
i.appearance.horizontalEntriesBoundariesAlignment, i.appearance.verticalEntriesBoundariesAlignment);
}, (i, v) -> {
i.appearance.horizontalEntriesBoundaries = v.horizontalPercentage();
i.appearance.verticalEntriesBoundaries = v.verticalPercentage();
i.appearance.horizontalEntriesBoundariesColumns = v.horizontalLimit();
i.appearance.verticalEntriesBoundariesRows = v.verticalLimit();
i.appearance.horizontalEntriesBoundariesAlignment = v.horizontalAlign();
i.appearance.verticalEntriesBoundariesAlignment = v.verticalAlign();
}).configure(PanelBoundariesConfiguration.INSTANCE);
CompositeOption<DisplayPanelLocation> LOCATION = make("layout.location", i -> i.advanced.accessibility.displayPanelLocation, (i, v) -> i.advanced.accessibility.displayPanelLocation = v)
.enumOptions();
CompositeOption<Boolean> HIDE_LIST_IF_IDLE = make("layout.hide_when_idle", i -> i.appearance.hideEntryPanelIfIdle, (i, v) -> i.appearance.hideEntryPanelIfIdle = v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public CompositeOption<T> options(T... entry) {
return this.entry(OptionValueEntry.options(entry));
}

public CompositeOption<T> configure(OptionValueEntry.Configurator<T> configurator) {
return this.entry(OptionValueEntry.configure(configurator));
}

public CompositeOption<T> previewer(ConfigPreviewer<T> previewer) {
this.previewer = previewer;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import me.shedaniel.clothconfig2.api.ModifierKeyCode;
import me.shedaniel.rei.api.common.util.CollectionUtils;
import me.shedaniel.rei.impl.client.gui.config.ConfigAccess;
import net.minecraft.network.chat.Component;

import java.util.Arrays;
Expand Down Expand Up @@ -95,10 +96,31 @@ public Component getOption(T value) {
};
}

static <T> OptionValueEntry.Configure<T> configure(Configurator<T> configurator) {
return new Configure<>() {
@Override
public void configure(ConfigAccess access, CompositeOption<T> option, Runnable onClose) {
configurator.configure(access, option, onClose);
}

@Override
public Component getOption(T value) {
return translatable("config.rei.texts.configure");
}
};
}

static OptionValueEntry<ModifierKeyCode> keybind() {
return ModifierKeyCode::getLocalizedName;
}

interface Configurator<T> {
void configure(ConfigAccess access, CompositeOption<T> option, Runnable onClose);
}

interface Configure<T> extends OptionValueEntry<T>, Configurator<T> {
}

interface Selection<T> extends OptionValueEntry<T> {
List<T> getOptions();

Expand Down
Loading

0 comments on commit b0c1904

Please sign in to comment.