Skip to content

Commit

Permalink
Implement some of the config ui
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Oct 22, 2023
1 parent 48a3083 commit b84f4d0
Show file tree
Hide file tree
Showing 18 changed files with 523 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,37 @@
import dev.architectury.utils.value.IntValue;
import me.shedaniel.math.Point;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.client.gui.widgets.Widget;
import me.shedaniel.rei.api.client.gui.widgets.Widgets;
import me.shedaniel.rei.impl.client.config.ConfigManagerImpl;
import me.shedaniel.rei.impl.client.config.ConfigObjectImpl;
import me.shedaniel.rei.impl.client.gui.config.components.ConfigCategoriesListWidget;
import me.shedaniel.rei.impl.client.gui.config.components.ConfigEntriesListWidget;
import me.shedaniel.rei.impl.client.gui.config.options.AllREIConfigCategories;
import me.shedaniel.rei.impl.client.gui.config.options.CompositeOption;
import me.shedaniel.rei.impl.client.gui.config.options.OptionCategory;
import me.shedaniel.rei.impl.client.gui.config.options.OptionGroup;
import me.shedaniel.rei.impl.client.gui.credits.CreditsScreen;
import me.shedaniel.rei.impl.client.gui.widget.HoleWidget;
import me.shedaniel.rei.impl.client.gui.modules.Menu;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Widget;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.TranslatableComponent;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class REIConfigScreen extends Screen {
private final Screen parent;
private final List<OptionCategory> categories;
private final List<Widget> widgets = new ArrayList<>();
private final Map<CompositeOption<?>, ?> options = new HashMap<>();
private OptionCategory activeCategory;
@Nullable
private Menu menu;

public REIConfigScreen(Screen parent) {
this(parent, AllREIConfigCategories.CATEGORIES);
Expand All @@ -59,6 +70,15 @@ public REIConfigScreen(Screen parent, List<OptionCategory> categories) {
this.categories = categories;
Preconditions.checkArgument(!categories.isEmpty(), "Categories cannot be empty!");
this.activeCategory = categories.get(0);

ConfigObjectImpl config = ConfigManagerImpl.getInstance().getConfig();
for (OptionCategory category : categories) {
for (OptionGroup group : category.getGroups()) {
for (CompositeOption<?> option : group.getOptions()) {
((Map<CompositeOption<?>, Object>) this.options).put(option, option.getBind().apply(config));
}
}
}
}

@Override
Expand All @@ -71,18 +91,24 @@ public void init() {
}));
this.widgets.add(Widgets.createLabel(new Point(width / 2, 12), this.title));
int sideWidth = (int) (width / 3.8);
Widget[] list = {ConfigEntriesListWidget.create(new Rectangle(12 + sideWidth, 32, width - 20 - sideWidth, height - 32 - 32), activeCategory.getGroups())};
this.widgets.add(ConfigCategoriesListWidget.create(new Rectangle(8, 32, sideWidth, height - 32 - 32), categories, new IntValue() {
@Override
public void accept(int i) {
REIConfigScreen.this.activeCategory = categories.get(i);
list[0] = ConfigEntriesListWidget.create(new Rectangle(12 + sideWidth, 32, width - 20 - sideWidth, height - 32 - 32), activeCategory.getGroups());
}

@Override
public int getAsInt() {
return categories.indexOf(activeCategory);
}
}));
this.widgets.add(HoleWidget.create(new Rectangle(12 + sideWidth, 32, width - 20 - sideWidth, height - 32 - 32), () -> 0, 32));
this.widgets.add(Widgets.delegate(() -> list[0]));
}

public Map<CompositeOption<?>, ?> getOptions() {
return options;
}

@Override
Expand All @@ -106,6 +132,8 @@ public List<? extends GuiEventListener> children() {

@Override
public boolean charTyped(char character, int modifiers) {
if (menu != null && menu.charTyped(character, modifiers))
return true;
for (GuiEventListener listener : children())
if (listener.charTyped(character, modifiers))
return true;
Expand All @@ -114,14 +142,25 @@ public boolean charTyped(char character, int modifiers) {

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
if (menu != null && menu.mouseDragged(mouseX, mouseY, button, deltaX, deltaY))
return true;
for (GuiEventListener entry : children())
if (entry.mouseDragged(mouseX, mouseY, button, deltaX, deltaY))
return true;
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
}

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (menu != null && menu.mouseClicked(mouseX, mouseY, button))
return true;
return super.mouseClicked(mouseX, mouseY, button);
}

@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (menu != null && menu.mouseReleased(mouseX, mouseY, button))
return true;
for (GuiEventListener entry : children())
if (entry.mouseReleased(mouseX, mouseY, button))
return true;
Expand All @@ -130,9 +169,21 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) {

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
if (menu != null && menu.mouseScrolled(mouseX, mouseY, amount))
return true;
for (GuiEventListener listener : children())
if (listener.mouseScrolled(mouseX, mouseY, amount))
return true;
return super.mouseScrolled(mouseX, mouseY, amount);
}

public void openMenu(Menu menu) {
this.menu = menu;
this.widgets.add(menu);
}

public void closeMenu() {
this.widgets.remove(menu);
this.menu = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package me.shedaniel.rei.impl.client.gui.config.components;

import dev.architectury.utils.value.IntValue;
import me.shedaniel.clothconfig2.api.scroll.ScrollingContainer;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.client.gui.widgets.Widget;
import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

public class ConfigCategoryEntryWidget {
public static WidgetWithBounds create(OptionCategory category) {
Label label = Widgets.createLabel(new Point(21, 7), category.getName().copy().withStyle(style -> style.withColor(0xFFD0D0D0)))
Label label = Widgets.createLabel(new Point(21, 7), category.getName().copy().withStyle(style -> style.withColor(0xFFC0C0C0)))
.leftAligned();
Font font = Minecraft.getInstance().font;
Rectangle bounds = new Rectangle(0, 0, label.getBounds().getMaxX(), 7 * 3);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.impl.client.gui.config.components;

import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.client.gui.widgets.Widget;
import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds;
import me.shedaniel.rei.impl.client.gui.config.options.OptionGroup;
import me.shedaniel.rei.impl.client.gui.widget.ListWidget;
import me.shedaniel.rei.impl.client.gui.widget.ScrollableViewWidget;
import me.shedaniel.rei.impl.common.util.RectangleUtils;

import java.util.List;

public class ConfigEntriesListWidget {
public static Widget create(Rectangle bounds, List<OptionGroup> groups) {
WidgetWithBounds list = ListWidget.builderOf(RectangleUtils.inset(bounds, 6, 6), groups,
(index, entry) -> ConfigGroupWidget.create(entry, bounds.width - 12 - 6))
.gap(7)
.build();
return ScrollableViewWidget.create(bounds, list.withPadding(0, 5), true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.impl.client.gui.config.components;

import me.shedaniel.math.Point;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.client.gui.widgets.Widget;
import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds;
import me.shedaniel.rei.api.client.gui.widgets.Widgets;
import me.shedaniel.rei.impl.client.gui.config.options.CompositeOption;
import me.shedaniel.rei.impl.client.gui.config.options.OptionGroup;
import net.minecraft.client.gui.GuiComponent;

import java.util.ArrayList;
import java.util.List;

public class ConfigGroupWidget {
public static WidgetWithBounds create(OptionGroup entry, int width) {
List<Widget> widgets = new ArrayList<>();
int height = 0;
WidgetWithBounds groupTitle = Widgets.createLabel(new Point(0, 3), entry.getGroupName().copy().withStyle(style -> style.withColor(0xFFC0C0C0).withUnderlined(true)))
.leftAligned();
groupTitle = groupTitle.withPadding(0, 0, 0, 6);
widgets.add(groupTitle);
height = Math.max(height, groupTitle.getBounds().getMaxY());

for (CompositeOption<?> option : entry.getOptions()) {
WidgetWithBounds widget = ConfigOptionWidget.create(option, width);
widgets.add(Widgets.withTranslate(widget, 0, height, 0));
height = Math.max(height, height + widget.getBounds().getMaxY());

if (entry.getOptions().get(entry.getOptions().size() - 1) != option) {
int y = height;
widgets.add(Widgets.createDrawableWidget((helper, matrices, mouseX, mouseY, delta) -> {
for (int x = 0; x <= width; x += 4) {
GuiComponent.fill(matrices, x, y + 1, x + 2, y + 2, 0xFF757575);
}
}));
height += 7;
}
}

Rectangle bounds = new Rectangle(0, 0, width, height);
return Widgets.concatWithBounds(bounds, widgets);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.impl.client.gui.config.components;

import com.mojang.math.Matrix4f;
import me.shedaniel.math.Point;
import me.shedaniel.math.Rectangle;
import me.shedaniel.rei.api.client.gui.widgets.Label;
import me.shedaniel.rei.api.client.gui.widgets.WidgetWithBounds;
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.impl.client.gui.config.REIConfigScreen;
import me.shedaniel.rei.impl.client.gui.config.options.CompositeOption;
import me.shedaniel.rei.impl.client.gui.config.options.OptionValueEntry;
import me.shedaniel.rei.impl.client.gui.modules.Menu;
import me.shedaniel.rei.impl.client.gui.modules.entries.ToggleMenuEntry;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.Map;

import static me.shedaniel.rei.impl.client.gui.config.options.ConfigUtils.literal;

public class ConfigOptionValueWidget {
public static <T> WidgetWithBounds create(CompositeOption<T> option) {
Map<CompositeOption<?>, ?> options = ((REIConfigScreen) Minecraft.getInstance().screen).getOptions();
OptionValueEntry<T> entry = option.getEntry();
T value = (T) options.get(option);
Component text;

if (entry instanceof OptionValueEntry.Selection<T> selection) {
text = selection.getOption(value);
} else {
text = literal(value.toString());
}

Label label = Widgets.createLabel(new Point(), text).rightAligned()
.color(0xFFE0E0E0)
.hoveredColor(0xFFE0E0E0);
Matrix4f[] matrix = {new Matrix4f()};

if (entry instanceof OptionValueEntry.Selection<T> selection) {
int noOfOptions = selection.getOptions().size();
if (noOfOptions == 2) {
label.clickable().onClick($ -> {
((Map<CompositeOption<?>, Object>) options).put(option, selection.getOptions().get((selection.getOptions().indexOf((T) options.get(option)) + 1) % 2));
label.setMessage(selection.getOption((T) options.get(option)));
});
} else if (noOfOptions >= 2) {
label.clickable().onClick($ -> {
Menu menu = new Menu(MatrixUtils.transform(matrix[0], label.getBounds()), CollectionUtils.map(selection.getOptions(),
opt -> ToggleMenuEntry.of(selection.getOption(opt), () -> false, o -> {
((REIConfigScreen) Minecraft.getInstance().screen).closeMenu();
((Map<CompositeOption<?>, Object>) options).put(option, opt);
label.setMessage(selection.getOption(opt));
})
.withActive(() -> true)), false);
((REIConfigScreen) Minecraft.getInstance().screen).closeMenu();
((REIConfigScreen) Minecraft.getInstance().screen).openMenu(menu);
});
}
}

return Widgets.concatWithBounds(() -> new Rectangle(-label.getBounds().width, 0, label.getBounds().width + 8, 14),
label,
Widgets.createDrawableWidget((helper, matrices, mouseX, mouseY, delta) -> matrix[0] = matrices.last().pose()),
Widgets.withTranslate(Widgets.createTexturedWidget(new ResourceLocation("roughlyenoughitems:textures/gui/config/selector.png"),
new Rectangle(1, 1, 4, 6), 0, 0, 1, 1, 1, 1), 0, 0.5, 0)
);
}
}
Loading

0 comments on commit b84f4d0

Please sign in to comment.