Skip to content

Commit

Permalink
feat: Settings smooth scrolling [skip ci] (#2472)
Browse files Browse the repository at this point in the history
* feat: Settings smooth scrolling

* fix: Prevent interacting with and showing tooltips for configs outside the mask

* refactor: Get mask positions from screen

---------

Co-authored-by: Kristof Kovacs <49001742+kristofbolyai@users.noreply.github.com>
  • Loading branch information
ShadowCat117 and kristofbolyai committed May 10, 2024
1 parent 79805be commit b9843a0
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 434 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* Copyright © Wynntils 2023.
* Copyright © Wynntils 2023-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.settings.widgets;

import com.wynntils.core.components.Managers;
import com.wynntils.screens.settings.WynntilsBookSettingsScreen;
import com.wynntils.utils.mc.McUtils;
import com.wynntils.utils.render.Texture;
import java.util.List;
import net.minecraft.ChatFormatting;
Expand All @@ -22,7 +23,9 @@ public ApplyButton(WynntilsBookSettingsScreen screen) {
14,
Component.translatable("screens.wynntils.settingsScreen.apply"),
List.of(Component.translatable("screens.wynntils.settingsScreen.apply.description")
.withStyle(ChatFormatting.GREEN)));
.withStyle(ChatFormatting.GREEN)),
0,
McUtils.mc().screen.height);
this.screen = screen;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2023.
* Copyright © Wynntils 2022-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.settings.widgets;
Expand All @@ -16,14 +16,16 @@
public class BooleanSettingsButton extends GeneralSettingsButton {
private final Config<Boolean> config;

public BooleanSettingsButton(Config<Boolean> config) {
public BooleanSettingsButton(int x, int y, Config<Boolean> config, int maskTopY, int maskBottomY) {
super(
0,
7,
x,
y,
50,
FontRenderer.getInstance().getFont().lineHeight + 8,
getTitle(config),
ComponentUtils.wrapTooltips(List.of(Component.literal(config.getDescription())), 150));
ComponentUtils.wrapTooltips(List.of(Component.literal(config.getDescription())), 150),
maskTopY,
maskBottomY);
this.config = config;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
* Copyright © Wynntils 2023.
* Copyright © Wynntils 2023-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.settings.widgets;

import com.wynntils.screens.settings.WynntilsBookSettingsScreen;
import com.wynntils.utils.mc.McUtils;
import com.wynntils.utils.render.Texture;
import java.util.List;
import net.minecraft.ChatFormatting;
Expand All @@ -21,7 +22,9 @@ public CloseButton(WynntilsBookSettingsScreen screen) {
14,
Component.translatable("screens.wynntils.settingsScreen.close"),
List.of(Component.translatable("screens.wynntils.settingsScreen.close.description")
.withStyle(ChatFormatting.DARK_RED)));
.withStyle(ChatFormatting.DARK_RED)),
0,
McUtils.mc().screen.height);
this.screen = screen;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,26 @@
public class ConfigTile extends WynntilsButton {
private final WynntilsBookSettingsScreen settingsScreen;
private final Config<?> config;

private final int maskTopY;
private final int maskBottomY;
private final GeneralSettingsButton resetButton;
private AbstractWidget configOptionElement;

public ConfigTile(
int x, int y, int width, int height, WynntilsBookSettingsScreen settingsScreen, Config<?> config) {
super(x, y, width, height, Component.literal(config.getJsonName()));
this.settingsScreen = settingsScreen;
this.maskTopY = settingsScreen.getConfigMaskTopY();
this.maskBottomY = settingsScreen.getConfigMaskBottomY();
this.config = config;
this.configOptionElement = getWidgetFromConfig(config);
this.resetButton = new ResetButton(
config, () -> configOptionElement = getWidgetFromConfig(config), x + width - 40, getRenderY());
config,
() -> configOptionElement = getWidgetFromConfig(config),
x + width - 40,
getRenderY(),
maskTopY,
maskBottomY);
}

@Override
Expand All @@ -56,12 +64,7 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float
0,
1);

poseStack.pushPose();
final int renderX = getRenderX();
final int renderY = getRenderY();
poseStack.translate(renderX, renderY, 0);
configOptionElement.render(guiGraphics, mouseX - renderX, mouseY - renderY, partialTick);
poseStack.popPose();
configOptionElement.render(guiGraphics, mouseX, mouseY, partialTick);
}

private void renderDisplayName(PoseStack poseStack) {
Expand All @@ -86,29 +89,34 @@ private void renderDisplayName(PoseStack poseStack) {

@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
double actualMouseX = mouseX - getRenderX();
double actualMouseY = mouseY - getRenderY();
// Prevent interaction when the tile is outside of the mask from the screen, same applies to drag and released
if ((mouseY <= maskTopY || mouseY >= maskBottomY)) return false;

return resetButton.mouseClicked(mouseX, mouseY, button)
|| configOptionElement.mouseClicked(actualMouseX, actualMouseY, button);
|| configOptionElement.mouseClicked(mouseX, mouseY, button);
}

@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
double actualMouseX = mouseX - getRenderX();
double actualMouseY = mouseY - getRenderY();
if ((mouseY <= maskTopY || mouseY >= maskBottomY)) return false;

return configOptionElement.mouseDragged(actualMouseX, actualMouseY, button, deltaX, deltaY)
|| super.mouseDragged(actualMouseX, actualMouseY, button, deltaX, deltaY);
return configOptionElement.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)
|| super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
}

@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
double actualMouseX = mouseX - getRenderX();
double actualMouseY = mouseY - getRenderY();
if ((mouseY <= maskTopY || mouseY >= maskBottomY)) return false;

return configOptionElement.mouseReleased(mouseX, mouseY, button) || super.mouseReleased(mouseX, mouseY, button);
}

@Override
public void setY(int y) {
super.setY(y);

return configOptionElement.mouseReleased(actualMouseX, actualMouseY, button)
|| super.mouseReleased(actualMouseX, actualMouseY, button);
configOptionElement.setY(getRenderY());
resetButton.setY(getRenderY());
}

@Override
Expand All @@ -117,7 +125,7 @@ public void onPress() {
}

private int getRenderY() {
return this.getY() + 12;
return this.getY() + 19;
}

private int getRenderX() {
Expand All @@ -126,13 +134,22 @@ private int getRenderX() {

private <E extends Enum<E>> AbstractWidget getWidgetFromConfig(Config<?> configOption) {
if (configOption.getType().equals(Boolean.class)) {
return new BooleanSettingsButton((Config<Boolean>) configOption);
return new BooleanSettingsButton(
getRenderX(), getRenderY(), (Config<Boolean>) configOption, maskTopY, maskBottomY);
} else if (configOption.isEnum()) {
return new EnumSettingsButton<>((Config<E>) configOption);
return new EnumSettingsButton<>(
getRenderX(), getRenderY(), (Config<E>) configOption, maskTopY, maskBottomY);
} else if (configOption.getType().equals(CustomColor.class)) {
return new CustomColorSettingsButton((Config<CustomColor>) configOption, settingsScreen);
return new CustomColorSettingsButton(
getRenderX(),
getRenderY(),
(Config<CustomColor>) configOption,
settingsScreen,
maskTopY,
maskBottomY);
} else {
return new TextInputBoxSettingsWidget<>(configOption, settingsScreen);
return new TextInputBoxSettingsWidget<>(
getRenderX(), getRenderY(), configOption, settingsScreen, maskTopY, maskBottomY);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2023.
* Copyright © Wynntils 2022-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.settings.widgets;
Expand All @@ -12,8 +12,9 @@
import net.minecraft.client.gui.GuiGraphics;

public class CustomColorSettingsButton extends TextInputBoxSettingsWidget<CustomColor> {
public CustomColorSettingsButton(Config<CustomColor> config, TextboxScreen textboxScreen) {
super(config, textboxScreen, 80);
public CustomColorSettingsButton(
int x, int y, Config<CustomColor> config, TextboxScreen textboxScreen, int maskTopY, int maskBottomY) {
super(x, y, config, textboxScreen, 80, maskTopY, maskBottomY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2023.
* Copyright © Wynntils 2022-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.settings.widgets;
Expand All @@ -20,14 +20,16 @@ public class EnumSettingsButton<E extends Enum<E>> extends GeneralSettingsButton
private final Config<E> config;
private final List<E> enumConstants;

public EnumSettingsButton(Config<E> config) {
public EnumSettingsButton(int x, int y, Config<E> config, int maskTopY, int maskBottomY) {
super(
0,
7,
x,
y,
getWidth(config.getType()),
FontRenderer.getInstance().getFont().lineHeight + 8,
Component.literal(config.getValueString()),
ComponentUtils.wrapTooltips(List.of(Component.literal(config.getDescription())), 150));
ComponentUtils.wrapTooltips(List.of(Component.literal(config.getDescription())), 150),
maskTopY,
maskBottomY);
this.config = config;
enumConstants = EnumSet.allOf((Class<E>) config.getType()).stream().toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2023.
* Copyright © Wynntils 2022-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.settings.widgets;
Expand All @@ -23,11 +23,23 @@
public abstract class GeneralSettingsButton extends WynntilsButton {
public static final CustomColor BACKGROUND_COLOR = new CustomColor(98, 34, 8);
public static final CustomColor HOVER_BACKGROUND_COLOR = new CustomColor(158, 52, 16);
private final int maskTopY;
private final int maskBottomY;
private final List<Component> tooltip;

protected GeneralSettingsButton(int x, int y, int width, int height, Component title, List<Component> tooltip) {
protected GeneralSettingsButton(
int x,
int y,
int width,
int height,
Component title,
List<Component> tooltip,
int maskTopY,
int maskBottomY) {
super(x, y, width, height, title);
this.tooltip = tooltip;
this.maskTopY = maskTopY;
this.maskBottomY = maskBottomY;
}

@Override
Expand Down Expand Up @@ -60,6 +72,11 @@ public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float
VerticalAlignment.MIDDLE,
TextShadow.OUTLINE);

// Don't want to display tooltip when the tile is outside the mask from the screen
if (isHovered && (mouseY <= maskTopY || mouseY >= maskBottomY)) {
isHovered = false;
}

if (isHovered) {
McUtils.mc().screen.setTooltipForNextRenderPass(Lists.transform(tooltip, Component::getVisualOrderText));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* Copyright © Wynntils 2023.
* Copyright © Wynntils 2023-2024.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.screens.settings.widgets;

import com.wynntils.core.persisted.config.Config;
import com.wynntils.utils.colors.CommonColors;
import com.wynntils.utils.colors.CustomColor;
import com.wynntils.utils.render.FontRenderer;
import java.util.List;
import net.minecraft.client.sounds.SoundManager;
import net.minecraft.network.chat.Component;
Expand All @@ -15,14 +16,16 @@ public class ResetButton extends GeneralSettingsButton {
private final Config<?> config;
private final Runnable onClick;

ResetButton(Config<?> config, Runnable onClick, int x, int y) {
ResetButton(Config<?> config, Runnable onClick, int x, int y, int maskTopY, int maskBottomY) {
super(
x,
y,
35,
12,
FontRenderer.getInstance().getFont().lineHeight + 8,
Component.translatable("screens.wynntils.settingsScreen.reset.name"),
List.of(Component.translatable("screens.wynntils.settingsScreen.reset.description")));
List.of(Component.translatable("screens.wynntils.settingsScreen.reset.description")),
maskTopY,
maskBottomY);
this.config = config;
this.onClick = onClick;
}
Expand Down

0 comments on commit b9843a0

Please sign in to comment.