aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2020-04-01 20:01:34 +0800
committershedaniel <daniel@shedaniel.me>2020-04-01 20:01:34 +0800
commit66abc317e5fc36a397ca1cc919e388fbe143956b (patch)
tree915cc4799d89297b8d4bd2dbe1046c177f4d1627 /src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java
parent3919ec1e15d6eb9a8aa4564bb2d4e4dfdbeb54e3 (diff)
downloadRoughlyEnoughItems-66abc317e5fc36a397ca1cc919e388fbe143956b.tar.gz
RoughlyEnoughItems-66abc317e5fc36a397ca1cc919e388fbe143956b.tar.bz2
RoughlyEnoughItems-66abc317e5fc36a397ca1cc919e388fbe143956b.zip
ScrollingContainer & SubsetsMenu && 20w18b
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java')
-rw-r--r--src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java132
1 files changed, 30 insertions, 102 deletions
diff --git a/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java b/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java
index b6f28ee09..6c5e91278 100644
--- a/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java
+++ b/src/main/java/me/shedaniel/rei/gui/VillagerRecipeViewingScreen.java
@@ -35,6 +35,7 @@ import me.shedaniel.rei.api.widgets.Button;
import me.shedaniel.rei.api.widgets.Tooltip;
import me.shedaniel.rei.api.widgets.Widgets;
import me.shedaniel.rei.gui.entries.RecipeEntry;
+import me.shedaniel.rei.gui.widget.ScrollingContainer;
import me.shedaniel.rei.gui.widget.TabWidget;
import me.shedaniel.rei.gui.widget.Widget;
import me.shedaniel.rei.impl.ClientHelperImpl;
@@ -44,9 +45,6 @@ import me.shedaniel.rei.utils.CollectionUtils;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
-import net.minecraft.client.render.BufferBuilder;
-import net.minecraft.client.render.Tessellator;
-import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.NarratorManager;
@@ -76,14 +74,24 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
private int tabsPerPage = 8;
private int selectedCategoryIndex = 0;
private int selectedRecipeIndex = 0;
- private double scrollAmount = 0;
- private double target;
- private long start;
- private long duration;
+ private final ScrollingContainer scrolling = new ScrollingContainer() {
+ @Override
+ public Rectangle getBounds() {
+ return new Rectangle(scrollListBounds.x + 1, scrollListBounds.y + 1, scrollListBounds.width - 2, scrollListBounds.height - 2);
+ }
+
+ @Override
+ public int getMaxScrollHeight() {
+ int i = 0;
+ for (Button button : buttonList) {
+ i += button.getBounds().height;
+ }
+ return i;
+ }
+ };
private float scrollBarAlpha = 0;
private float scrollBarAlphaFuture = 0;
private long scrollBarAlphaFutureTime = -1;
- private boolean draggingScrollBar = false;
private int tabsPage = -1;
private EntryStack ingredientStackToNotice = EntryStack.empty();
private EntryStack resultStackToNotice = EntryStack.empty();
@@ -132,7 +140,7 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
super.init();
boolean isCompactTabs = ConfigObject.getInstance().isUsingCompactTabs();
int tabSize = isCompactTabs ? 24 : 28;
- this.draggingScrollBar = false;
+ scrolling.draggingScrollBar = false;
this.children.clear();
this.widgets.clear();
this.buttonList.clear();
@@ -246,23 +254,12 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
ScreenHelper.getLastOverlay().init();
}
- private double getMaxScroll() {
- return Math.max(0, this.getMaxScrollPosition() - (scrollListBounds.height - 2));
- }
-
@Override
public boolean mouseClicked(double mouseX, double mouseY, int int_1) {
- double height = getMaxScrollPosition();
- int actualHeight = scrollListBounds.height - 2;
- if (height > actualHeight && scrollBarAlpha > 0 && mouseY >= scrollListBounds.y + 1 && mouseY <= scrollListBounds.getMaxY() - 1) {
- double scrollbarPositionMinX = scrollListBounds.getMaxX() - 6;
- if (mouseX >= scrollbarPositionMinX & mouseX <= scrollbarPositionMinX + 8) {
- this.draggingScrollBar = true;
- scrollBarAlpha = 1;
- return false;
- }
+ if (scrolling.updateDraggingState(mouseX, mouseY, int_1)) {
+ scrollBarAlpha = 1;
+ return true;
}
- this.draggingScrollBar = false;
return super.mouseClicked(mouseX, mouseY, int_1);
}
@@ -274,29 +271,11 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
return super.charTyped(char_1, int_1);
}
- public void offset(double value, boolean animated) {
- scrollTo(target + value, animated);
- }
-
- public void scrollTo(double value, boolean animated) {
- scrollTo(value, animated, ClothConfigInitializer.getScrollDuration());
- }
-
- public void scrollTo(double value, boolean animated, long duration) {
- target = ClothConfigInitializer.clamp(value, getMaxScroll());
-
- if (animated) {
- start = System.currentTimeMillis();
- this.duration = duration;
- } else
- scrollAmount = target;
- }
-
@Override
public boolean mouseScrolled(double double_1, double double_2, double double_3) {
- double height = CollectionUtils.sumInt(buttonList, b -> b.getBounds().getHeight());
+ double height = scrolling.getMaxScrollHeight();
if (scrollListBounds.contains(double_1, double_2) && height > scrollListBounds.height - 2) {
- offset(ClothConfigInitializer.getScrollStep() * -double_3, true);
+ scrolling.offset(ClothConfigInitializer.getScrollStep() * -double_3, true);
if (scrollBarAlphaFuture == 0)
scrollBarAlphaFuture = 1f;
if (System.currentTimeMillis() - scrollBarAlphaFutureTime > 300f)
@@ -323,10 +302,6 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
return super.mouseScrolled(double_1, double_2, double_3);
}
- private double getMaxScrollPosition() {
- return CollectionUtils.sumInt(buttonList, b -> b.getBounds().getHeight());
- }
-
@Override
public void render(int mouseX, int mouseY, float delta) {
if (ConfigObject.getInstance().doesVillagerScreenHavePermanentScrollBar()) {
@@ -351,7 +326,7 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
scrollBarAlpha = Math.max(Math.min(1f, l / 300f), scrollBarAlpha);
}
}
- updatePosition(delta);
+ scrolling.updatePosition(delta);
this.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
int yOffset = 0;
for (Widget widget : widgets) {
@@ -359,9 +334,9 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
}
ScreenHelper.getLastOverlay().render(mouseX, mouseY, delta);
RenderSystem.pushMatrix();
- ScissorsHandler.INSTANCE.scissor(new Rectangle(0, scrollListBounds.y + 1, width, scrollListBounds.height - 2));
+ ScissorsHandler.INSTANCE.scissor(scrolling.getBounds());
for (Button button : buttonList) {
- button.getBounds().y = scrollListBounds.y + 1 + yOffset - (int) scrollAmount;
+ button.getBounds().y = scrollListBounds.y + 1 + yOffset - (int) scrolling.scrollAmount;
if (button.getBounds().getMaxY() > scrollListBounds.getMinY() && button.getBounds().getMinY() < scrollListBounds.getMaxY()) {
button.render(mouseX, mouseY, delta);
}
@@ -374,65 +349,18 @@ public class VillagerRecipeViewingScreen extends Screen implements RecipeScreen
Optional.ofNullable(recipeRenderers.get(i).getTooltip(new Point(mouseX, mouseY))).ifPresent(Tooltip::queue);
}
}
- double maxScroll = getMaxScrollPosition();
- if (maxScroll > scrollListBounds.height - 2) {
- Tessellator tessellator = Tessellator.getInstance();
- BufferBuilder buffer = tessellator.getBuffer();
- int height = (int) (((scrollListBounds.height - 2) * (scrollListBounds.height - 2)) / this.getMaxScrollPosition());
- height = MathHelper.clamp(height, 32, scrollListBounds.height - 2 - 8);
- height -= Math.min((scrollAmount < 0 ? (int) -scrollAmount : scrollAmount > getMaxScroll() ? (int) scrollAmount - getMaxScroll() : 0), height * .95);
- height = Math.max(10, height);
- int minY = (int) Math.min(Math.max((int) scrollAmount * (scrollListBounds.height - 2 - height) / getMaxScroll() + scrollListBounds.y + 1, scrollListBounds.y + 1), scrollListBounds.getMaxY() - 1 - height);
- int scrollbarPositionMinX = scrollListBounds.getMaxX() - 6, scrollbarPositionMaxX = scrollListBounds.getMaxX() - 1;
- boolean hovered = (new Rectangle(scrollbarPositionMinX, minY, scrollbarPositionMaxX - scrollbarPositionMinX, height)).contains(PointHelper.ofMouse());
- float bottomC = (hovered ? .67f : .5f) * (REIHelper.getInstance().isDarkThemeEnabled() ? 0.8f : 1f);
- float topC = (hovered ? .87f : .67f) * (REIHelper.getInstance().isDarkThemeEnabled() ? 0.8f : 1f);
- RenderSystem.disableTexture();
- RenderSystem.enableBlend();
- RenderSystem.disableAlphaTest();
- RenderSystem.blendFuncSeparate(770, 771, 1, 0);
- RenderSystem.shadeModel(7425);
- buffer.begin(7, VertexFormats.POSITION_COLOR);
- buffer.vertex(scrollbarPositionMinX, minY + height, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
- buffer.vertex(scrollbarPositionMaxX, minY + height, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
- buffer.vertex(scrollbarPositionMaxX, minY, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
- buffer.vertex(scrollbarPositionMinX, minY, 800).color(bottomC, bottomC, bottomC, scrollBarAlpha).next();
- tessellator.draw();
- buffer.begin(7, VertexFormats.POSITION_COLOR);
- buffer.vertex(scrollbarPositionMinX, minY + height - 1, 800).color(topC, topC, topC, scrollBarAlpha).next();
- buffer.vertex(scrollbarPositionMaxX - 1, minY + height - 1, 800).color(topC, topC, topC, scrollBarAlpha).next();
- buffer.vertex(scrollbarPositionMaxX - 1, minY, 800).color(topC, topC, topC, scrollBarAlpha).next();
- buffer.vertex(scrollbarPositionMinX, minY, 800).color(topC, topC, topC, scrollBarAlpha).next();
- tessellator.draw();
- RenderSystem.shadeModel(7424);
- RenderSystem.disableBlend();
- RenderSystem.enableAlphaTest();
- RenderSystem.enableTexture();
- }
+ scrolling.renderScrollBar(0, scrollBarAlpha);
ScissorsHandler.INSTANCE.removeLastScissor();
RenderSystem.popMatrix();
ScreenHelper.getLastOverlay().lateRender(mouseX, mouseY, delta);
}
- private void updatePosition(float delta) {
- double[] target = new double[]{this.target};
- this.scrollAmount = ClothConfigInitializer.handleScrollingPosition(target, this.scrollAmount, this.getMaxScroll(), delta, this.start, this.duration);
- this.target = target[0];
- }
-
@Override
public boolean mouseDragged(double mouseX, double mouseY, int int_1, double double_3, double double_4) {
- if (int_1 == 0 && scrollBarAlpha > 0 && draggingScrollBar) {
- double height = CollectionUtils.sumInt(buttonList, b -> b.getBounds().getHeight());
- int actualHeight = scrollListBounds.height - 2;
- if (height > actualHeight && mouseY >= scrollListBounds.y + 1 && mouseY <= scrollListBounds.getMaxY() - 1) {
- int int_3 = MathHelper.clamp((int) ((actualHeight * actualHeight) / height), 32, actualHeight - 8);
- double double_6 = Math.max(1.0D, Math.max(1d, height) / (double) (actualHeight - int_3));
- scrollBarAlphaFutureTime = System.currentTimeMillis();
- scrollBarAlphaFuture = 1f;
- scrollAmount = target = MathHelper.clamp(scrollAmount + double_4 * double_6, 0, height - scrollListBounds.height + 2);
- return true;
- }
+ if (scrolling.mouseDragged(mouseX, mouseY, int_1, double_3, double_4)) {
+ scrollBarAlphaFutureTime = System.currentTimeMillis();
+ scrollBarAlphaFuture = 1f;
+ return true;
}
return super.mouseDragged(mouseX, mouseY, int_1, double_3, double_4);
}