From fcae486f6e61f42bc5119f4ee5933d6804b66b84 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 14 Jul 2019 18:11:51 +0200 Subject: [PATCH] Clean up FeatureButton --- .../wurstclient/clickgui/FeatureButton.java | 275 ++++++++++-------- .../wurstclient/clickgui/SettingsWindow.java | 47 +++ .../java/net/wurstclient/clickgui/Window.java | 90 +++--- 3 files changed, 253 insertions(+), 159 deletions(-) create mode 100644 src/main/java/net/wurstclient/clickgui/SettingsWindow.java diff --git a/src/main/java/net/wurstclient/clickgui/FeatureButton.java b/src/main/java/net/wurstclient/clickgui/FeatureButton.java index e6d8d45a..f038f6eb 100644 --- a/src/main/java/net/wurstclient/clickgui/FeatureButton.java +++ b/src/main/java/net/wurstclient/clickgui/FeatureButton.java @@ -15,7 +15,13 @@ import net.wurstclient.WurstClient; public final class FeatureButton extends Component { + private static final ClickGui GUI = WurstClient.INSTANCE.getGui(); + private static final TextRenderer TEXT_RENDERER = + WurstClient.MC.textRenderer; + private final Feature feature; + private final boolean hasSettings; + private Window settingsWindow; public FeatureButton(Feature feature) @@ -23,6 +29,7 @@ public final class FeatureButton extends Component this.feature = feature; setWidth(getDefaultWidth()); setHeight(getDefaultHeight()); + hasSettings = !feature.getSettings().isEmpty(); } @Override @@ -30,72 +37,84 @@ public final class FeatureButton extends Component { if(mouseButton != 0) return; + + if(hasSettings && mouseX > getX() + getWidth() - 12) + { + if(isSettingsWindowOpen()) + closeSettingsWindow(); + else + openSettingsWindow(); - // if(!feature.getSettings().isEmpty() - // && mouseX > getX() + getWidth() - 12) - // { - // if(settingsWindow != null && !settingsWindow.isClosing()) - // { - // settingsWindow.close(); - // settingsWindow = null; - // return; - // } - // - // settingsWindow = new Window(feature.getName() + " Settings"); - // for(Setting setting : feature.getSettings()) - // settingsWindow.add(setting.getComponent()); - // - // settingsWindow.setClosable(true); - // settingsWindow.setMinimizable(false); - // settingsWindow.pack(); - // - // int scroll = getParent().isScrollingEnabled() - // ? getParent().getScrollOffset() : 0; - // int x = getParent().getX() + getParent().getWidth() + 5; - // int y = getParent().getY() + 12 + getY() + scroll; - // ScaledResolution sr = - // new ScaledResolution(Minecraft.getMinecraft()); - // if(x + settingsWindow.getWidth() > sr.getScaledWidth()) - // x = getParent().getX() - settingsWindow.getWidth() - 5; - // if(y + settingsWindow.getHeight() > sr.getScaledHeight()) - // y -= settingsWindow.getHeight() - 14; - // settingsWindow.setX(x); - // settingsWindow.setY(y); - // - // ClickGui gui = WurstClient.INSTANCE.getGui(); - // gui.addWindow(settingsWindow); - // return; - // } + return; + } feature.doPrimaryAction(); } + private boolean isSettingsWindowOpen() + { + return settingsWindow != null && !settingsWindow.isClosing(); + } + + private void openSettingsWindow() + { + settingsWindow = new SettingsWindow(feature, getParent(), getY()); + GUI.addWindow(settingsWindow); + } + + private void closeSettingsWindow() + { + settingsWindow.close(); + settingsWindow = null; + } + @Override public void render(int mouseX, int mouseY, float partialTicks) { - ClickGui gui = WurstClient.INSTANCE.getGui(); - float[] bgColor = gui.getBgColor(); - float[] acColor = gui.getAcColor(); - float opacity = gui.getOpacity(); - // boolean settings = !feature.getSettings().isEmpty(); - boolean settings = !"".isEmpty(); - int x1 = getX(); int x2 = x1 + getWidth(); - int x3 = settings ? x2 - 11 : x2; + int x3 = hasSettings ? x2 - 11 : x2; int y1 = getY(); int y2 = y1 + getHeight(); - int scroll = getParent().isScrollingEnabled() - ? getParent().getScrollOffset() : 0; - boolean hovering = mouseX >= x1 && mouseY >= y1 && mouseX < x2 - && mouseY < y2 && mouseY >= -scroll - && mouseY < getParent().getHeight() - 13 - scroll; + boolean hovering = isHovering(mouseX, mouseY, x1, x2, y1, y2); boolean hHack = hovering && mouseX < x3; boolean hSettings = hovering && mouseX >= x3; - // tooltip + if(hHack) + setTooltip(); + + drawButtonBackground(x1, x3, y1, y2, hHack); + + if(hasSettings) + drawSettingsBackground(x2, x3, y1, y2, hSettings); + + drawOutline(x1, x2, y1, y2); + + if(hasSettings) + { + drawSeparator(x3, y1, y2); + drawSettingsArrow(x2, x3, y1, y2, hSettings); + } + + drawName(x1, x3, y1); + } + + private boolean isHovering(int mouseX, int mouseY, int x1, int x2, int y1, + int y2) + { + Window parent = getParent(); + boolean scrollEnabled = parent.isScrollingEnabled(); + int scroll = scrollEnabled ? parent.getScrollOffset() : 0; + + return mouseX >= x1 && mouseY >= y1 && mouseX < x2 && mouseY < y2 + && mouseY >= -scroll && mouseY < parent.getHeight() - 13 - scroll; + } + + private void setTooltip() + { String tooltip = feature.getDescription(); + // if(feature.isBlocked()) // { // if(tooltip == null) @@ -106,10 +125,17 @@ public final class FeatureButton extends Component // "Your current YesCheat+ profile is blocking this feature."; // } - if(hHack) - gui.setTooltip(tooltip); + GUI.setTooltip(tooltip); + } + + private void drawButtonBackground(int x1, int x3, int y1, int y2, + boolean hHack) + { + float[] bgColor = GUI.getBgColor(); + float opacity = GUI.getOpacity(); + + GL11.glBegin(GL11.GL_QUADS); - // color if(feature.isEnabled()) // if(feature.isBlocked()) // glColor4f(1, 0, 0, hHack ? opacity * 1.5F : opacity); @@ -119,92 +145,113 @@ public final class FeatureButton extends Component GL11.glColor4f(bgColor[0], bgColor[1], bgColor[2], hHack ? opacity * 1.5F : opacity); - // background - GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2i(x1, y1); GL11.glVertex2i(x1, y2); GL11.glVertex2i(x3, y2); GL11.glVertex2i(x3, y1); - if(settings) - { - GL11.glColor4f(bgColor[0], bgColor[1], bgColor[2], - hSettings ? opacity * 1.5F : opacity); - GL11.glVertex2i(x3, y1); - GL11.glVertex2i(x3, y2); - GL11.glVertex2i(x2, y2); - GL11.glVertex2i(x2, y1); - } - GL11.glEnd(); - // outline - GL11.glColor4f(acColor[0], acColor[1], acColor[2], 0.5F); + GL11.glEnd(); + } + + private void drawSettingsBackground(int x2, int x3, int y1, int y2, + boolean hSettings) + { + float[] bgColor = GUI.getBgColor(); + float opacity = GUI.getOpacity(); + + GL11.glBegin(GL11.GL_QUADS); + GL11.glColor4f(bgColor[0], bgColor[1], bgColor[2], + hSettings ? opacity * 1.5F : opacity); + GL11.glVertex2i(x3, y1); + GL11.glVertex2i(x3, y2); + GL11.glVertex2i(x2, y2); + GL11.glVertex2i(x2, y1); + GL11.glEnd(); + } + + private void drawOutline(int x1, int x2, int y1, int y2) + { + float[] acColor = GUI.getAcColor(); + GL11.glBegin(GL11.GL_LINE_LOOP); + GL11.glColor4f(acColor[0], acColor[1], acColor[2], 0.5F); GL11.glVertex2i(x1, y1); GL11.glVertex2i(x1, y2); GL11.glVertex2i(x2, y2); GL11.glVertex2i(x2, y1); GL11.glEnd(); + } + + private void drawSeparator(int x3, int y1, int y2) + { + // separator + GL11.glBegin(GL11.GL_LINES); + GL11.glVertex2i(x3, y1); + GL11.glVertex2i(x3, y2); + GL11.glEnd(); + } + + private void drawSettingsArrow(int x2, int x3, int y1, int y2, + boolean hSettings) + { + double xa1 = x3 + 1; + double xa2 = (x3 + x2) / 2.0; + double xa3 = x2 - 1; + double ya1; + double ya2; - if(settings) + if(isSettingsWindowOpen()) { - // separator - GL11.glBegin(GL11.GL_LINES); - GL11.glVertex2i(x3, y1); - GL11.glVertex2i(x3, y2); - GL11.glEnd(); + ya1 = y2 - 3.5; + ya2 = y1 + 3; + GL11.glColor4f(hSettings ? 1 : 0.85F, 0, 0, 1); - double xa1 = x3 + 1; - double xa2 = (x3 + x2) / 2.0; - double xa3 = x2 - 1; - double ya1; - double ya2; - - if(settingsWindow != null && !settingsWindow.isClosing()) - { - ya1 = y2 - 3.5; - ya2 = y1 + 3; - GL11.glColor4f(hSettings ? 1 : 0.85F, 0, 0, 1); - }else - { - ya1 = y1 + 3.5; - ya2 = y2 - 3; - GL11.glColor4f(0, hSettings ? 1 : 0.85F, 0, 1); - } - - // arrow - GL11.glBegin(GL11.GL_TRIANGLES); - GL11.glVertex2d(xa1, ya1); - GL11.glVertex2d(xa3, ya1); - GL11.glVertex2d(xa2, ya2); - GL11.glEnd(); - - // outline - GL11.glColor4f(0.0625F, 0.0625F, 0.0625F, 0.5F); - GL11.glBegin(GL11.GL_LINE_LOOP); - GL11.glVertex2d(xa1, ya1); - GL11.glVertex2d(xa3, ya1); - GL11.glVertex2d(xa2, ya2); - GL11.glEnd(); + }else + { + ya1 = y1 + 3.5; + ya2 = y2 - 3; + GL11.glColor4f(0, hSettings ? 1 : 0.85F, 0, 1); } - // hack name + // arrow + GL11.glBegin(GL11.GL_TRIANGLES); + GL11.glVertex2d(xa1, ya1); + GL11.glVertex2d(xa3, ya1); + GL11.glVertex2d(xa2, ya2); + GL11.glEnd(); + + // outline + GL11.glColor4f(0.0625F, 0.0625F, 0.0625F, 0.5F); + GL11.glBegin(GL11.GL_LINE_LOOP); + GL11.glVertex2d(xa1, ya1); + GL11.glVertex2d(xa3, ya1); + GL11.glVertex2d(xa2, ya2); + GL11.glEnd(); + } + + private void drawName(int x1, int x3, int y1) + { GL11.glColor4f(1, 1, 1, 1); GL11.glEnable(GL11.GL_TEXTURE_2D); - TextRenderer fr = WurstClient.MC.textRenderer; - int fx = x1 + ((settings ? getWidth() - 11 : getWidth()) - - fr.getStringWidth(feature.getName())) / 2; - int fy = y1 + 2; - fr.draw(feature.getName(), fx, fy, 0xf0f0f0); + + String name = feature.getName(); + int nameWidth = TEXT_RENDERER.getStringWidth(name); + int tx = x1 + (x3 - x1 - nameWidth) / 2; + int ty = y1 + 2; + + TEXT_RENDERER.draw(name, tx, ty, 0xF0F0F0); + GL11.glDisable(GL11.GL_TEXTURE_2D); } @Override public int getDefaultWidth() { - TextRenderer fr = WurstClient.MC.textRenderer; - int width = fr.getStringWidth(feature.getName()) + 2; - // if(!feature.getSettings().isEmpty()) - // width += 11; + String name = feature.getName(); + int width = TEXT_RENDERER.getStringWidth(name) + 2; + if(hasSettings) + width += 11; + return width; } diff --git a/src/main/java/net/wurstclient/clickgui/SettingsWindow.java b/src/main/java/net/wurstclient/clickgui/SettingsWindow.java new file mode 100644 index 00000000..13c9fc36 --- /dev/null +++ b/src/main/java/net/wurstclient/clickgui/SettingsWindow.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved. + * + * This source code is subject to the terms of the GNU General Public + * License, version 3. If a copy of the GPL was not distributed with this + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt + */ +package net.wurstclient.clickgui; + +import java.util.stream.Stream; + +import net.wurstclient.Feature; +import net.wurstclient.WurstClient; +import net.wurstclient.settings.Setting; + +public final class SettingsWindow extends Window +{ + public SettingsWindow(Feature feature, Window parent, int buttonY) + { + super(feature.getName() + " Settings"); + + Stream settings = feature.getSettings().values().stream(); + settings.map(Setting::getComponent).forEach(c -> add(c)); + + setClosable(true); + setMinimizable(false); + pack(); + + setInitialPosition(parent, buttonY); + } + + private void setInitialPosition(Window parent, int buttonY) + { + int scroll = parent.isScrollingEnabled() ? parent.getScrollOffset() : 0; + int x = parent.getX() + parent.getWidth() + 5; + int y = parent.getY() + 12 + buttonY + scroll; + + net.minecraft.client.util.Window mcWindow = WurstClient.MC.window; + if(x + getWidth() > mcWindow.getScaledWidth()) + x = parent.getX() - getWidth() - 5; + if(y + getHeight() > mcWindow.getScaledHeight()) + y -= getHeight() - 14; + + setX(x); + setY(y); + } +} diff --git a/src/main/java/net/wurstclient/clickgui/Window.java b/src/main/java/net/wurstclient/clickgui/Window.java index 0a87793a..8e3eb449 100644 --- a/src/main/java/net/wurstclient/clickgui/Window.java +++ b/src/main/java/net/wurstclient/clickgui/Window.java @@ -12,7 +12,7 @@ import java.util.ArrayList; import net.minecraft.client.font.TextRenderer; import net.wurstclient.WurstClient; -public final class Window +public class Window { private String title; private int x; @@ -50,42 +50,42 @@ public final class Window this.title = title; } - public String getTitle() + public final String getTitle() { return title; } - public void setTitle(String title) + public final void setTitle(String title) { this.title = title; } - public int getX() + public final int getX() { return x; } - public void setX(int x) + public final void setX(int x) { this.x = x; } - public int getY() + public final int getY() { return y; } - public void setY(int y) + public final void setY(int y) { this.y = y; } - public int getWidth() + public final int getWidth() { return width; } - public void setWidth(int width) + public final void setWidth(int width) { if(this.width != width) invalidate(); @@ -93,12 +93,12 @@ public final class Window this.width = width; } - public int getHeight() + public final int getHeight() { return height; } - public void setHeight(int height) + public final void setHeight(int height) { if(this.height != height) invalidate(); @@ -106,7 +106,7 @@ public final class Window this.height = height; } - public void pack() + public final void pack() { int maxChildWidth = 0; for(Component c : children) @@ -142,7 +142,7 @@ public final class Window validate(); } - public void validate() + public final void validate() { if(valid) return; @@ -169,163 +169,163 @@ public final class Window valid = true; } - public void invalidate() + public final void invalidate() { valid = false; } - public int countChildren() + public final int countChildren() { return children.size(); } - public Component getChild(int index) + public final Component getChild(int index) { return children.get(index); } - public void add(Component component) + public final void add(Component component) { children.add(component); component.setParent(this); invalidate(); } - public void remove(int index) + public final void remove(int index) { children.get(index).setParent(null); children.remove(index); invalidate(); } - public void remove(Component component) + public final void remove(Component component) { children.remove(component); component.setParent(null); invalidate(); } - public boolean isDragging() + public final boolean isDragging() { return dragging; } - public void startDragging(int mouseX, int mouseY) + public final void startDragging(int mouseX, int mouseY) { dragging = true; dragOffsetX = x - mouseX; dragOffsetY = y - mouseY; } - public void dragTo(int mouseX, int mouseY) + public final void dragTo(int mouseX, int mouseY) { x = mouseX + dragOffsetX; y = mouseY + dragOffsetY; } - public void stopDragging() + public final void stopDragging() { dragging = false; dragOffsetX = 0; dragOffsetY = 0; } - public boolean isMinimized() + public final boolean isMinimized() { return minimized; } - public void setMinimized(boolean minimized) + public final void setMinimized(boolean minimized) { this.minimized = minimized; } - public boolean isMinimizable() + public final boolean isMinimizable() { return minimizable; } - public void setMinimizable(boolean minimizable) + public final void setMinimizable(boolean minimizable) { this.minimizable = minimizable; } - public boolean isPinned() + public final boolean isPinned() { return pinned; } - public void setPinned(boolean pinned) + public final void setPinned(boolean pinned) { this.pinned = pinned; } - public boolean isPinnable() + public final boolean isPinnable() { return pinnable; } - public void setPinnable(boolean pinnable) + public final void setPinnable(boolean pinnable) { this.pinnable = pinnable; } - public boolean isClosable() + public final boolean isClosable() { return closable; } - public void setClosable(boolean closable) + public final void setClosable(boolean closable) { this.closable = closable; } - public boolean isClosing() + public final boolean isClosing() { return closing; } - public void close() + public final void close() { closing = true; } - public boolean isInvisible() + public final boolean isInvisible() { return invisible; } - public void setInvisible(boolean invisible) + public final void setInvisible(boolean invisible) { this.invisible = invisible; } - public int getInnerHeight() + public final int getInnerHeight() { return innerHeight; } - public int getScrollOffset() + public final int getScrollOffset() { return scrollOffset; } - public void setScrollOffset(int scrollOffset) + public final void setScrollOffset(int scrollOffset) { this.scrollOffset = scrollOffset; } - public boolean isScrollingEnabled() + public final boolean isScrollingEnabled() { return scrollingEnabled; } - public boolean isDraggingScrollbar() + public final boolean isDraggingScrollbar() { return draggingScrollbar; } - public void startDraggingScrollbar(int mouseY) + public final void startDraggingScrollbar(int mouseY) { draggingScrollbar = true; double outerHeight = height - 13; @@ -334,7 +334,7 @@ public final class Window scrollbarDragOffsetY = (int)(scrollbarY - mouseY); } - public void dragScrollbarTo(int mouseY) + public final void dragScrollbarTo(int mouseY) { int scrollbarY = mouseY + scrollbarDragOffsetY; double outerHeight = height - 13; @@ -343,7 +343,7 @@ public final class Window scrollOffset = Math.max(scrollOffset, -innerHeight + height - 13); } - public void stopDraggingScrollbar() + public final void stopDraggingScrollbar() { draggingScrollbar = false; scrollbarDragOffsetY = 0;