seppuku/src/main/java/me/rigamortis/seppuku/impl/gui/hud/component/HubComponent.java

206 lines
11 KiB
Java

package me.rigamortis.seppuku.impl.gui.hud.component;
import com.mojang.realmsclient.gui.ChatFormatting;
import me.rigamortis.seppuku.Seppuku;
import me.rigamortis.seppuku.api.event.gui.hud.EventHubComponentClick;
import me.rigamortis.seppuku.api.gui.hud.component.HudComponent;
import me.rigamortis.seppuku.api.gui.hud.component.ResizableHudComponent;
import me.rigamortis.seppuku.api.texture.Texture;
import me.rigamortis.seppuku.api.util.RenderUtil;
import me.rigamortis.seppuku.impl.gui.hud.GuiHudEditor;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.util.math.MathHelper;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
/**
* created by noil on 9/29/2019 at 12:23 PM
*/
public final class HubComponent extends ResizableHudComponent {
private final int SCROLL_WIDTH = 5;
private final int BORDER = 2;
private final int TEXT_GAP = 1;
private final int TEXTURE_SIZE = 8;
private final int TITLE_BAR_HEIGHT = mc.fontRenderer.FONT_HEIGHT + 1;
private final Texture texture;
private int scroll;
private int totalHeight;
public HubComponent() {
super("Hub", 100, 120, 125, 1000);
this.texture = new Texture("module-hub.png");
this.setVisible(true);
this.setSnappable(false);
this.setW(100);
this.setH(120);
this.setX((mc.displayWidth / 2.0f) - (this.getW() / 2));
this.setY((mc.displayHeight / 2.0f) - (this.getH() / 2));
}
@Override
public void render(int mouseX, int mouseY, float partialTicks) {
super.render(mouseX, mouseY, partialTicks);
if (!(mc.currentScreen instanceof GuiHudEditor))
return;
final ScaledResolution sr = new ScaledResolution(mc);
// Render Y pos offset (make this all modular eventually...)
int offsetY = 0;
// Scrolling
this.handleScrolling(mouseX, mouseY);
// No dragging inside box
final boolean insideTitlebar = mouseY <= this.getY() + TITLE_BAR_HEIGHT + BORDER;
if (!insideTitlebar) {
this.setDragging(false);
}
// Is mouse inside
final boolean mouseInside = this.isMouseInside(mouseX, mouseY);
// clamp max width & height
if (this.isResizeDragging()) {
if (this.getH() > this.getTotalHeight()) {
this.setH(this.getTotalHeight());
this.setResizeDragging(false);
}
}
// Background & title
RenderUtil.drawRect(this.getX() - 1, this.getY() - 1, this.getX() + this.getW() + 1, this.getY() + this.getH() + 1, 0x99101010);
RenderUtil.drawRect(this.getX(), this.getY(), this.getX() + this.getW(), this.getY() + this.getH(), 0xFF202020);
texture.bind();
texture.render(this.getX() + BORDER, this.getY() + BORDER, TEXTURE_SIZE, TEXTURE_SIZE);
mc.fontRenderer.drawStringWithShadow(this.getName(), this.getX() + BORDER + /* texture width */ TEXTURE_SIZE + BORDER, this.getY() + BORDER, 0xFFFFFFFF);
final String components = ChatFormatting.DARK_GRAY + "" + (Seppuku.INSTANCE.getHudManager().getComponentList().size() - 1);
mc.fontRenderer.drawStringWithShadow(components, this.getX() + this.getW() - BORDER - mc.fontRenderer.getStringWidth(components), this.getY() + BORDER, 0xFFFFFFFF);
offsetY += mc.fontRenderer.FONT_HEIGHT + 1;
// Behind list
final float listTop = this.getY() + offsetY + BORDER;
RenderUtil.drawRect(this.getX() + BORDER, listTop, this.getX() + this.getW() - SCROLL_WIDTH - BORDER, this.getY() + this.getH() - BORDER, 0xFF101010);
// Scrollbar bg
RenderUtil.drawRect(this.getX() + this.getW() - SCROLL_WIDTH, this.getY() + offsetY + BORDER, this.getX() + this.getW() - BORDER, this.getY() + this.getH() - BORDER, 0xFF101010);
// Scrollbar highlights
if (mouseInside) {
if (mouseX >= (this.getX() + this.getW() - SCROLL_WIDTH) && mouseX <= (this.getX() + this.getW() - BORDER)) { // mouse is inside scroll area on x-axis
RenderUtil.drawGradientRect(this.getX() + this.getW() - SCROLL_WIDTH, this.getY() + offsetY + BORDER, this.getX() + this.getW() - BORDER, this.getY() + offsetY + 8 + BORDER, 0xFF909090, 0x00101010);
RenderUtil.drawGradientRect(this.getX() + this.getW() - SCROLL_WIDTH, this.getY() + this.getH() - 8 - BORDER, this.getX() + this.getW() - BORDER, this.getY() + this.getH() - BORDER, 0x00101010, 0xFF909090);
float diffY = this.getY() + TITLE_BAR_HEIGHT + ((this.getH() - TITLE_BAR_HEIGHT) / 2);
if (mouseY > diffY) {
RenderUtil.drawGradientRect(this.getX() + this.getW() - SCROLL_WIDTH, this.getY() + (this.getH() / 2) + BORDER + BORDER, this.getX() + this.getW() - BORDER, this.getY() + this.getH() - BORDER, 0x00101010, 0x90909090);
} else {
RenderUtil.drawGradientRect(this.getX() + this.getW() - SCROLL_WIDTH, this.getY() + offsetY + BORDER, this.getX() + this.getW() - BORDER, this.getY() + (this.getH() / 2) + BORDER + BORDER, 0x90909090, 0x00101010);
}
}
}
// Scrollbar
RenderUtil.drawRect(this.getX() + this.getW() - SCROLL_WIDTH, MathHelper.clamp((this.getY() + offsetY + BORDER) + ((this.getH() * this.scroll) / this.totalHeight), (this.getY() + offsetY + BORDER), (this.getY() + this.getH() - BORDER)), this.getX() + this.getW() - BORDER, MathHelper.clamp((this.getY() + this.getH() - BORDER) - (this.getH() * (this.totalHeight - this.getH() - this.scroll) / this.totalHeight), (this.getY() + offsetY + BORDER), (this.getY() + this.getH() - BORDER)), 0xFF909090);
// Begin scissoring and render the component "buttons"
GL11.glEnable(GL11.GL_SCISSOR_TEST);
RenderUtil.glScissor(this.getX() + BORDER, this.getY() + offsetY + BORDER, this.getX() + this.getW() - BORDER - SCROLL_WIDTH, this.getY() + this.getH() - BORDER, sr);
for (HudComponent component : Seppuku.INSTANCE.getHudManager().getComponentList()) {
if (component != this) {
RenderUtil.drawRect(this.getX() + BORDER + TEXT_GAP, this.getY() + offsetY + BORDER + TEXT_GAP - this.scroll, this.getX() + BORDER + TEXT_GAP + this.getW() - BORDER - SCROLL_WIDTH - BORDER - 2, this.getY() + offsetY + BORDER + TEXT_GAP + mc.fontRenderer.FONT_HEIGHT - this.scroll, component.isVisible() ? 0x45002e00 : 0x452e0000);
final boolean insideComponent = mouseX >= (this.getX() + BORDER) && mouseX <= (this.getX() + this.getW() - BORDER - SCROLL_WIDTH) && mouseY >= (this.getY() + BORDER + mc.fontRenderer.FONT_HEIGHT + 1 + offsetY - this.scroll - mc.fontRenderer.FONT_HEIGHT + 1) && mouseY <= (this.getY() + BORDER + (mc.fontRenderer.FONT_HEIGHT) + 1 + offsetY - this.scroll);
if (insideComponent) {
RenderUtil.drawGradientRect(this.getX() + BORDER + TEXT_GAP, this.getY() + offsetY + BORDER + TEXT_GAP - this.scroll, this.getX() + BORDER + TEXT_GAP + this.getW() - BORDER - SCROLL_WIDTH - BORDER - 2, this.getY() + offsetY + BORDER + TEXT_GAP + mc.fontRenderer.FONT_HEIGHT - this.scroll, 0x30909090, 0x00101010);
}
// draw button text
mc.fontRenderer.drawStringWithShadow(component.getName(), this.getX() + BORDER + TEXT_GAP + 1, this.getY() + offsetY + BORDER + TEXT_GAP - this.scroll, component.isVisible() ? 0xFF55FF55 : 0xFFFF5555);
offsetY += mc.fontRenderer.FONT_HEIGHT + TEXT_GAP;
}
}
GL11.glDisable(GL11.GL_SCISSOR_TEST);
// Over list
if (this.scroll > 6) {
RenderUtil.drawGradientRect(this.getX() + BORDER, listTop, this.getX() + this.getW() - SCROLL_WIDTH - BORDER, listTop + 8, 0xFF101010, 0x00000000);
}
if (this.getH() != this.getTotalHeight() && this.scroll != (this.totalHeight - this.getH())) {
RenderUtil.drawGradientRect(this.getX() + BORDER, this.getY() + this.getH() - BORDER - 8, this.getX() + this.getW() - SCROLL_WIDTH - BORDER, this.getY() + this.getH() - BORDER, 0x00000000, 0xFF101010);
}
// figures up a "total height (pixels)" of the inside of the list area (for calculating scroll height)
this.totalHeight = BORDER + TEXT_GAP + offsetY + BORDER;
}
@Override
public void mouseRelease(int mouseX, int mouseY, int button) {
super.mouseRelease(mouseX, mouseY, button);
final boolean inside = this.isMouseInside(mouseX, mouseY);
final boolean insideTitlebar = mouseY <= this.getY() + BORDER + TITLE_BAR_HEIGHT;
if (inside && button == 0 && !insideTitlebar) {
int offsetY = BORDER;
for (HudComponent component : Seppuku.INSTANCE.getHudManager().getComponentList()) {
if (component != this) {
final boolean insideComponent = mouseX >= (this.getX() + BORDER) && mouseX <= (this.getX() + this.getW() - BORDER - SCROLL_WIDTH) && mouseY >= (this.getY() + BORDER + mc.fontRenderer.FONT_HEIGHT + 1 + offsetY - this.scroll) && mouseY <= (this.getY() + BORDER + (mc.fontRenderer.FONT_HEIGHT * 2) + 1 + offsetY - this.scroll);
if (insideComponent) {
component.setVisible(!component.isVisible());
Seppuku.INSTANCE.getEventManager().dispatchEvent(new EventHubComponentClick(component.getName(), component.isVisible()));
}
offsetY += mc.fontRenderer.FONT_HEIGHT + TEXT_GAP;
}
}
if (mouseX >= (this.getX() + this.getW() - SCROLL_WIDTH) && mouseX <= (this.getX() + this.getW() - BORDER)) { // mouse is inside scroll area on x-axis
float diffY = this.getY() + TITLE_BAR_HEIGHT + ((this.getH() - TITLE_BAR_HEIGHT) / 2);
if (mouseY > diffY) {
scroll += 10;
} else {
scroll -= 10;
}
this.clampScroll();
}
}
}
@Override
public void mouseClick(int mouseX, int mouseY, int button) {
final boolean insideDragZone = mouseY <= this.getY() + TITLE_BAR_HEIGHT + BORDER || mouseY >= ((this.getY() + this.getH()) - CLICK_ZONE);
if (insideDragZone) {
super.mouseClick(mouseX, mouseY, button);
}
}
private void clampScroll() {
if (this.scroll < 0) {
this.scroll = 0;
}
if (this.scroll > this.totalHeight - this.getH()) {
this.scroll = this.totalHeight - (int) this.getH();
}
}
private void handleScrolling(int mouseX, int mouseY) {
if (this.isMouseInside(mouseX, mouseY) && Mouse.hasWheel()) {
this.scroll += -(Mouse.getDWheel() / 5);
this.clampScroll();
}
}
public int getScroll() {
return scroll;
}
public void setScroll(int scroll) {
this.scroll = scroll;
}
public int getTotalHeight() {
return totalHeight;
}
}