Files
CustomNPC-Plus/src/main/java/noppes/npcs/client/gui/util/GuiCustomScroll.java
T

381 lines
12 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.client.gui.util;
2025-01-29 13:20:04 -05:00
import kamkeel.npcs.util.TextSplitter;
2018-09-13 12:03:28 -04:00
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
2025-07-23 14:22:52 -04:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
2023-01-12 21:33:06 -05:00
2025-02-03 01:47:47 -05:00
import static noppes.npcs.client.gui.util.GuiNPCInterface.resizingActive;
2025-03-12 20:12:01 -04:00
public class GuiCustomScroll extends GuiScreen {
public static final ResourceLocation resource = new ResourceLocation("customnpcs", "textures/gui/misc.png");
2025-02-20 00:31:52 -05:00
public List<String> list;
2025-03-12 20:12:01 -04:00
public final HashMap<String, Integer> colors = new HashMap<>();
2018-09-13 12:03:28 -04:00
public int id;
public int guiLeft = 0;
public int guiTop = 0;
2022-01-12 04:44:08 -06:00
public int xSize, ySize;
2018-09-13 12:03:28 -04:00
public int selected;
2023-03-21 03:16:52 -05:00
protected HashSet<String> selectedList;
protected int hover;
2023-11-06 19:19:17 -05:00
protected int oldHover;
private int hoverCount = 0;
protected boolean hoverableText = false;
2025-06-26 17:48:24 -04:00
public int listHeight;
2025-06-29 14:32:24 -04:00
public int scrollY;
public int maxScrollY;
public int scrollHeight;
2018-09-13 12:03:28 -04:00
private boolean isScrolling;
2023-03-21 03:16:52 -05:00
public boolean multipleSelection = false;
2018-09-13 12:03:28 -04:00
private ICustomScrollListener listener;
private boolean isSorted = true;
2025-03-12 20:12:01 -04:00
public boolean visible = true;
private boolean selectable = true;
private boolean hasSubGUI = false;
2024-02-15 17:54:28 -05:00
2024-03-12 01:58:35 -04:00
private int lastClickedItem;
private long lastClickedTime = 0;
2025-03-12 20:12:01 -04:00
public GuiCustomScroll(GuiScreen parent, int id) {
2018-09-13 12:03:28 -04:00
width = 176;
height = 166;
xSize = 176;
ySize = 159;
selected = -1;
hover = -1;
selectedList = new HashSet<String>();
listHeight = 0;
scrollY = 0;
scrollHeight = 0;
isScrolling = false;
2025-03-12 20:12:01 -04:00
if (parent instanceof ICustomScrollListener)
listener = (ICustomScrollListener) parent;
2018-09-13 12:03:28 -04:00
this.list = new ArrayList<String>();
this.id = id;
}
2025-03-12 20:12:01 -04:00
public GuiCustomScroll(GuiScreen parent, int id, boolean multipleSelection) {
this(parent, id);
this.multipleSelection = multipleSelection;
2018-09-13 12:03:28 -04:00
}
2023-11-06 19:19:17 -05:00
2025-03-12 20:12:01 -04:00
public GuiCustomScroll(GuiScreen parent, int id, int allowHover) {
this(parent, id);
2023-11-06 19:19:17 -05:00
this.hoverableText = true;
}
2025-03-12 20:12:01 -04:00
public void setSize(int x, int y) {
ySize = y;
xSize = x;
2018-09-13 12:03:28 -04:00
listHeight = 14 * list.size();
2024-02-15 17:54:28 -05:00
2025-03-12 20:12:01 -04:00
if (listHeight > 0)
scrollHeight = (int) (((double) (ySize - 8) / (double) listHeight) * (ySize - 8));
2018-09-13 12:03:28 -04:00
else
2025-03-12 20:12:01 -04:00
scrollHeight = Integer.MAX_VALUE;
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
maxScrollY = listHeight - (ySize - 8) - 1;
}
2025-03-12 20:12:01 -04:00
public void drawScreen(int i, int j, float f, int mouseScrolled) {
if (!visible)
return;
2025-03-12 20:12:01 -04:00
drawGradientRect(guiLeft, guiTop, xSize + guiLeft, ySize + guiTop, 0xc0101010, 0xd0101010);
2018-09-13 12:03:28 -04:00
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.renderEngine.bindTexture(resource);
2025-03-12 20:12:01 -04:00
if (scrollHeight < ySize - 8) {
2018-09-13 12:03:28 -04:00
drawScrollBar();
}
GL11.glPushMatrix();
GL11.glRotatef(180F, 1.0F, 0.0F, 0.0F);
GL11.glPopMatrix();
GL11.glPushMatrix();
GL11.glTranslatef(guiLeft, guiTop, 0.0F);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
2024-02-15 17:54:28 -05:00
2026-02-02 20:40:17 -05:00
if (selectable && !hasSubGUI)
2025-03-12 20:12:01 -04:00
hover = getMouseOver(i, j);
2026-02-02 20:40:17 -05:00
else if (hasSubGUI)
hover = -1;
2023-11-06 19:19:17 -05:00
2018-09-13 12:03:28 -04:00
drawItems();
2025-03-12 20:12:01 -04:00
if (oldHover != hover) {
oldHover = hover;
hoverCount = 0;
2025-03-12 20:12:01 -04:00
} else {
if (hoverCount < 110)
hoverCount++;
}
2025-03-12 20:12:01 -04:00
if (!hasSubGUI && hover != -1 && hoverableText && hoverCount > 100) {
String displayString = StatCollector.translateToLocal(list.get(hover));
GL11.glColor4f(1, 1, 1, 1);
List<String> lines = TextSplitter.splitText(displayString, 30);
2025-03-12 20:12:01 -04:00
super.drawHoveringText(lines, i - guiLeft, j - guiTop, this.fontRendererObj);
GL11.glDisable(GL11.GL_LIGHTING);
}
2018-09-13 12:03:28 -04:00
GL11.glPopMatrix();
2025-03-12 20:12:01 -04:00
if (scrollHeight < ySize - 8) {
i -= guiLeft;
j -= guiTop;
if (Mouse.isButtonDown(0) && !resizingActive) {
if (i >= xSize - 9 && i < xSize - 4 && j >= 4 && j < ySize) {
2018-09-13 12:03:28 -04:00
isScrolling = true;
}
2025-03-12 20:12:01 -04:00
} else
2018-09-13 12:03:28 -04:00
isScrolling = false;
2024-02-15 17:54:28 -05:00
2025-03-12 20:12:01 -04:00
if (isScrolling) {
scrollY = (((j - 8) * listHeight) / (ySize - 8)) - (scrollHeight);
if (scrollY < 0) {
2018-09-13 12:03:28 -04:00
scrollY = 0;
}
2025-03-12 20:12:01 -04:00
if (scrollY > maxScrollY) {
2018-09-13 12:03:28 -04:00
scrollY = maxScrollY;
}
}
2024-02-15 17:54:28 -05:00
2025-03-12 20:12:01 -04:00
if (mouseScrolled != 0) {
scrollY += mouseScrolled > 0 ? -14 : 14;
if (scrollY > maxScrollY)
2018-09-13 12:03:28 -04:00
scrollY = maxScrollY;
2025-03-12 20:12:01 -04:00
if (scrollY < 0)
2018-09-13 12:03:28 -04:00
scrollY = 0;
2024-02-15 17:54:28 -05:00
}
2018-09-13 12:03:28 -04:00
}
}
2025-03-12 20:12:01 -04:00
public void drawHover(int i, int j) {
if (!visible)
return;
2025-03-12 20:12:01 -04:00
if (!hoverableText)
return;
GL11.glPushMatrix();
GL11.glTranslatef(guiLeft, guiTop, 0.0F);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
2025-03-12 20:12:01 -04:00
if (oldHover != hover) {
oldHover = hover;
hoverCount = 0;
2025-03-12 20:12:01 -04:00
} else {
if (hoverCount < 110)
hoverCount++;
}
2025-03-12 20:12:01 -04:00
if (!hasSubGUI && hover != -1 && hoverableText && hoverCount > 100) {
String displayString = StatCollector.translateToLocal(list.get(hover));
GL11.glColor4f(1, 1, 1, 1);
List<String> lines = TextSplitter.splitText(displayString, 30);
2025-03-12 20:12:01 -04:00
super.drawHoveringText(lines, i - guiLeft, j - guiTop, this.fontRendererObj);
GL11.glDisable(GL11.GL_LIGHTING);
}
GL11.glPopMatrix();
}
2025-03-12 20:12:01 -04:00
public boolean mouseInOption(int i, int j, int k) {
2018-09-13 12:03:28 -04:00
int l = 4;
int i1 = ((14 * k + 4) - scrollY);
return i >= l - 1 && i < l + xSize - 11 && j >= i1 - 1 && j < i1 + 8;
}
2025-03-12 20:12:01 -04:00
protected void drawItems() {
for (int i = 0; i < list.size(); i++) {
2018-09-13 12:03:28 -04:00
int j = 4;
int k = (14 * i + 4) - scrollY;
2025-03-12 20:12:01 -04:00
if (k >= 4 && k + 12 < ySize) {
int xOffset = scrollHeight < ySize - 8 ? 0 : 10;
String displayString = StatCollector.translateToLocal(list.get(i));
2018-09-13 12:03:28 -04:00
String text = "";
float maxWidth = (xSize + xOffset - 8) * 0.8f;
2025-03-12 20:12:01 -04:00
if (fontRendererObj.getStringWidth(displayString) > maxWidth) {
for (int h = 0; h < displayString.length(); h++) {
char c = displayString.charAt(h);
text += c;
if (fontRendererObj.getStringWidth(text) > maxWidth)
break;
}
if (displayString.length() > text.length())
text += "...";
} else
text = displayString;
if ((multipleSelection && selectedList.contains(text)) || (!multipleSelection && selected == i)) {
drawVerticalLine(j - 2, k - 4, k + 10, 0xffffffff);
drawVerticalLine(j + xSize - 18 + xOffset, k - 4, k + 10, 0xffffffff);
drawHorizontalLine(j - 2, j + xSize - 18 + xOffset, k - 3, 0xffffffff);
drawHorizontalLine(j - 2, j + xSize - 18 + xOffset, k + 10, 0xffffffff);
fontRendererObj.drawString(text, j, k, 0xffffff);
} else if (i == hover) {
fontRendererObj.drawString(text, j, k, 0x00ff00);
} else
fontRendererObj.drawString(text, j, k, 0xffffff);
2018-09-13 12:03:28 -04:00
}
}
}
2023-11-06 19:19:17 -05:00
2025-03-12 20:12:01 -04:00
public String getSelected() {
if (selected == -1 || selected >= list.size())
return null;
return list.get(selected);
2018-09-13 12:03:28 -04:00
}
2025-03-12 20:12:01 -04:00
private int getMouseOver(int i, int j) {
i -= guiLeft;
j -= guiTop;
//fontRenderer.drawString(".", i , j, 0xffffff);
if (i >= 4 && i < xSize - 4 && j >= 4 && j < ySize) {
for (int j1 = 0; j1 < list.size(); j1++) {
if (!mouseInOption(i, j, j1)) {
2018-09-13 12:03:28 -04:00
continue;
}
2024-02-15 17:54:28 -05:00
2018-09-13 12:03:28 -04:00
return j1;
}
}
2024-02-15 17:54:28 -05:00
2025-03-12 20:12:01 -04:00
return -1;
2018-09-13 12:03:28 -04:00
}
2025-03-12 20:12:01 -04:00
public void mouseClicked(int i, int j, int k) {
if (resizingActive)
2025-02-03 01:47:47 -05:00
return;
2025-03-12 20:12:01 -04:00
if (k != 0 || hover < 0)
return;
if (multipleSelection) {
if (selectedList.contains(list.get(hover))) {
selectedList.remove(list.get(hover));
} else {
selectedList.add(list.get(hover));
}
} else {
if (hover >= 0)
selected = hover;
hover = -1;
}
if (listener != null) {
2024-03-12 01:58:35 -04:00
long time = System.currentTimeMillis();
listener.customScrollClicked(i, j, k, this);
2025-03-12 20:12:01 -04:00
if (selected >= 0 && selected == lastClickedItem && time - lastClickedTime < 500) {
2024-03-12 01:58:35 -04:00
listener.customScrollDoubleClicked(list.get(selected), this);
}
lastClickedTime = time;
lastClickedItem = selected;
}
2018-09-13 12:03:28 -04:00
}
2025-03-12 20:12:01 -04:00
private void drawScrollBar() {
2018-09-13 12:03:28 -04:00
int i = guiLeft + xSize - 9;
2025-03-12 20:12:01 -04:00
int j = guiTop + (int) (((double) scrollY / (double) listHeight) * (double) (ySize - 8)) + 4;
2018-09-13 12:03:28 -04:00
int k = j;
drawTexturedModalRect(i, k, xSize, 9, 5, 1);
2025-03-12 20:12:01 -04:00
for (k++; k < (j + scrollHeight) - 1; k++) {
2018-09-13 12:03:28 -04:00
drawTexturedModalRect(i, k, xSize, 10, 5, 1);
}
drawTexturedModalRect(i, k, xSize, 11, 5, 1);
}
2025-02-10 14:58:45 -05:00
2025-03-12 20:12:01 -04:00
public boolean hasSelected() {
2023-03-23 21:26:47 -04:00
2025-03-12 20:12:01 -04:00
return selected >= 0;
}
public void setList(List<String> list) {
isSorted = true;
2023-11-06 22:20:30 -05:00
list.removeAll(Collections.singleton(null));
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
2025-03-12 20:12:01 -04:00
this.list = list;
setSize(xSize, ySize);
}
2023-03-23 21:26:47 -04:00
2025-03-12 20:12:01 -04:00
public void setList(List<String> list, boolean ascending, boolean caseOrder) {
2023-03-23 21:26:47 -04:00
isSorted = true;
2025-03-12 20:12:01 -04:00
if (caseOrder) {
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
}
2025-03-12 20:12:01 -04:00
if (!ascending) {
2023-03-23 21:26:47 -04:00
Collections.reverse(list);
}
this.list = list;
2025-03-12 20:12:01 -04:00
setSize(xSize, ySize);
2023-03-23 21:26:47 -04:00
}
2025-03-12 20:12:01 -04:00
public void setUnsortedList(List<String> list) {
isSorted = false;
this.list = list;
setSize(xSize, ySize);
}
public void replace(String old, String name) {
String select = getSelected();
list.remove(old);
list.add(name);
if (isSorted)
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
if (old.equals(select))
select = name;
selected = list.indexOf(select);
setSize(xSize, ySize);
}
public void setSelected(String name) {
selected = list.indexOf(name);
}
public void clear() {
2018-09-13 12:03:28 -04:00
list = new ArrayList<String>();
2025-03-12 20:12:01 -04:00
selected = -1;
scrollY = 0;
setSize(xSize, ySize);
}
public List<String> getList() {
return list;
}
public HashSet<String> getSelectedList() {
return selectedList;
}
public void setSelectedList(HashSet<String> selectedList) {
this.selectedList = selectedList;
}
public GuiCustomScroll setUnselectable() {
selectable = false;
return this;
}
2018-09-13 12:03:28 -04:00
public boolean isMouseOver(int x, int y) {
return x >= this.guiLeft && x <= this.guiLeft + this.xSize && y >= this.guiTop && y <= this.guiTop + this.ySize;
}
2025-03-12 20:12:01 -04:00
public void resetScroll() {
scrollY = 0;
}
2025-03-12 20:12:01 -04:00
public void updateSubGUI(boolean hasSubGUI) {
this.hasSubGUI = hasSubGUI;
}
2018-09-13 12:03:28 -04:00
}