-
Notifications
You must be signed in to change notification settings - Fork 16
Hud screen edit #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hud screen edit #77
Changes from all commits
0b58770
0144398
ea93020
5fc9907
00a9d1d
5d85d9a
cf9d466
adfee8b
a864571
4b40416
110424b
9b2da5c
0ab7459
6b59230
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,54 @@ | ||
| package fr.alexdoru.configlib.lib.gui; | ||
|
|
||
| import fr.alexdoru.configlib.api.ColorPalette; | ||
| import fr.alexdoru.configlib.api.IRenderer; | ||
| import fr.alexdoru.configlib.api.RendererPosition; | ||
| import fr.alexdoru.configlib.lib.RendererManager; | ||
| import fr.alexdoru.configlib.lib.gui.elements.ClickGuiButton; | ||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.client.gui.Gui; | ||
| import net.minecraft.client.gui.GuiScreen; | ||
| import net.minecraft.client.gui.ScaledResolution; | ||
| import net.minecraft.client.renderer.GlStateManager; | ||
| import net.minecraft.util.ResourceLocation; | ||
| import org.lwjgl.opengl.GL11; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
|
|
||
| public class RendererEditGuiScreen extends GuiScreen { | ||
|
|
||
| private static final int BUTTON_SIZE = 10; | ||
|
|
||
| private final RendererManager rendererManager; | ||
| private final IRenderer renderer; | ||
| private final RendererPosition rendererPosition; | ||
| private final GuiScreen parent; | ||
| private final ColorPalette colorPalette; | ||
| private final Button[] buttons; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't make an array but create mutliple fields |
||
| private final double originalRelativeX, originalRelativeY; | ||
| private boolean dragging; | ||
| private int prevX, prevY; | ||
|
|
||
| public RendererEditGuiScreen(RendererManager rendererManager, IRenderer renderer, GuiScreen parent) { | ||
| public RendererEditGuiScreen(RendererManager rendererManager, IRenderer renderer, GuiScreen parent, ColorPalette colorPalette) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't need to add ColorPalette as a parameter just change the type of the parent parameter to ConfigGuiScreen |
||
| this.rendererManager = rendererManager; | ||
| this.renderer = renderer; | ||
| this.rendererPosition = renderer.getPosition(); | ||
| this.parent = parent; | ||
| this.colorPalette = colorPalette; | ||
| this.originalRelativeX = rendererPosition.getRelativeX(); | ||
| this.originalRelativeY = rendererPosition.getRelativeY(); | ||
| this.buttons = new Button[]{ | ||
| new Button(new ResourceLocation("configlib", "reload.png"), "Reset to Default Position"), | ||
| new Button(new ResourceLocation("configlib", "undo.png"), "Undo Changes") | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void initGui() { | ||
| this.rendererPosition.updateAbsolutePosition(); | ||
| this.adjustBounds(); | ||
| final ScaledResolution res = new ScaledResolution(mc); | ||
| this.rendererPosition.updateAbsolutePosition(res); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mark deprecated the method |
||
| this.adjustBounds(res); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -39,27 +59,74 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) { | |
| this.rendererManager.renderEditScreenBackground(this.renderer); | ||
| this.rendererPosition.setEnabled(prevEnabled); | ||
| super.drawDefaultBackground(); | ||
| this.renderer.renderDummy(); | ||
| if (this.dragging) { | ||
| this.rendererPosition.setAbsolutePositionForRender( | ||
| this.rendererPosition.getAbsoluteRenderX() + mouseX - this.prevX, | ||
| this.rendererPosition.getAbsoluteRenderY() + mouseY - this.prevY | ||
| ); | ||
| } | ||
| this.renderer.renderDummy(); | ||
| if (!this.dragging) { | ||
| final int buttonX = this.width - BUTTON_SIZE - 2; | ||
| int buttonY = 2; | ||
| String hoveringText = null; | ||
| GlStateManager.enableAlpha(); | ||
| GlStateManager.enableBlend(); | ||
| GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0); | ||
| for (final Button button : buttons) { | ||
| button.xPosition = buttonX; | ||
| button.yPosition = buttonY; | ||
| button.drawButton(colorPalette, mc, mouseX, mouseY); | ||
| if (button.isMouseOver() && (hoveringText == null || hoveringText.isEmpty())) { | ||
| hoveringText = button.getHoveringText(); | ||
| } | ||
| buttonY += BUTTON_SIZE + 2; | ||
| } | ||
| GlStateManager.disableBlend(); | ||
| GlStateManager.color(1f, 1f, 1f, 1f); | ||
| if (hoveringText != null && !hoveringText.isEmpty()) { | ||
| drawHoveringText(Collections.singletonList(hoveringText), mouseX, mouseY + fontRendererObj.FONT_HEIGHT + 6); | ||
| } | ||
| } | ||
| this.prevX = mouseX; | ||
| this.prevY = mouseY; | ||
| } | ||
|
|
||
| @Override | ||
| protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException { | ||
| if (mouseButton == GuiUtil.MOUSE_LEFT) { | ||
| if (!this.dragging) { | ||
| int clickedButtonIdx = -1; | ||
| for (int i = 0; i < buttons.length; i++) { | ||
| if (buttons[i].mousePressed(mc, mouseX, mouseY)) { | ||
| clickedButtonIdx = i; | ||
| buttons[i].playPressSound(mc.getSoundHandler()); | ||
| break; | ||
| } | ||
| } | ||
| if (clickedButtonIdx != -1) { | ||
| success: { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you shouldn't use labels and break and also this is bugged because if clickedButtonIdx is not 0 or not 1 it will loop infinitely
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are mistaken, this is not how either works. now the label after it is just for simplicity, because if if clickedButtonIdx isn't There is nothing in this code that can cause an infinite loop and the only reason its written this way is because if I do decide to add more buttons I can write the logic easily at the end of the
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand your reason for not wanting to use labels so I wont. but you are mistaken in your image. if clickButton is 2, it will log: |
||
| if (clickedButtonIdx == 0) rendererPosition.resetToDefault(); | ||
| else if (clickedButtonIdx == 1) rendererPosition.setRelativePosition(originalRelativeX, originalRelativeY); | ||
| else break success; | ||
| final ScaledResolution res = new ScaledResolution(mc); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. save the scaled resolution as a field that is initializd in initGui |
||
| this.rendererPosition.updateAbsolutePosition(res); | ||
| this.adjustBounds(res); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| this.dragging = true; | ||
| } | ||
| super.mouseClicked(mouseX, mouseY, mouseButton); | ||
| this.dragging = true; | ||
| } | ||
|
|
||
| @Override | ||
| protected void mouseReleased(int mouseX, int mouseY, int state) { | ||
| super.mouseReleased(mouseX, mouseY, state); | ||
| this.dragging = false; | ||
| if (state == GuiUtil.MOUSE_LEFT) { | ||
| this.dragging = false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -82,8 +149,7 @@ public boolean doesGuiPauseGame() { | |
| /** | ||
| * Makes sure the HUD can't get out of the screen | ||
| */ | ||
| private void adjustBounds() { | ||
| final ScaledResolution res = new ScaledResolution(mc); | ||
| private void adjustBounds(ScaledResolution res) { | ||
| final int screenWidth = res.getScaledWidth(); | ||
| final int screenHeight = res.getScaledHeight(); | ||
| final int absoluteX = Math.max(0, Math.min(rendererPosition.getAbsoluteRenderX(), screenWidth)); | ||
|
|
@@ -96,4 +162,26 @@ private void renderCrosshair() { | |
| drawTexturedModalRect(this.width / 2 - 7, this.height / 2 - 7, 0, 0, 16, 16); | ||
| } | ||
|
|
||
| private static final class Button extends ClickGuiButton { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a standalone class named TexturedButton that draws the icon |
||
| private final ResourceLocation icon; | ||
| private final String hoveringText; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the hovering text feature to the parrent class click gui button directly
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will do this. |
||
|
|
||
| public Button(ResourceLocation icon, String hoveringText) { | ||
| super(-1, 0, 0, BUTTON_SIZE, BUTTON_SIZE, ""); | ||
| this.icon = icon; | ||
| this.hoveringText = hoveringText; | ||
| } | ||
|
|
||
| @Override | ||
| public void drawButton(ColorPalette colorPalette, Minecraft mc, int mouseX, int mouseY) { | ||
| super.drawButton(colorPalette, mc, mouseX, mouseY); | ||
| GlStateManager.color(1f, 1f, 1f, 1f); | ||
| mc.getTextureManager().bindTexture(icon); | ||
| final int iconSize = 6; | ||
| final int iconOffset = (BUTTON_SIZE - iconSize) / 2; | ||
| GuiUtil.drawFullTextureWithCustomSize(xPosition + iconOffset, yPosition + iconOffset, iconSize, iconSize); | ||
| } | ||
|
|
||
| public String getHoveringText() { return hoveringText; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,27 +7,19 @@ | |
| import fr.alexdoru.configlib.lib.RendererManager; | ||
| import fr.alexdoru.configlib.lib.gui.ConfigGuiScreen; | ||
| import fr.alexdoru.configlib.lib.gui.RendererEditGuiScreen; | ||
| import net.minecraft.client.gui.Gui; | ||
| import net.minecraft.client.renderer.GlStateManager; | ||
| import net.minecraft.util.EnumChatFormatting; | ||
| import net.minecraft.util.ResourceLocation; | ||
| import org.lwjgl.opengl.GL11; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| public class RendererGuiButton extends ConfigGuiButton { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree with the changes in this class, please roll back your changes and keep the two buttons like it was before
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand but I very much disagree with this.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok can you change the text to something else than "Position", "Move HUD" would be more explicit, also you might need to add a color to the palette for the text color |
||
|
|
||
| private static final ResourceLocation MOVE_ICON = new ResourceLocation("configlib", "move_icon_64x64.png"); | ||
| private static final ResourceLocation RESET_ICON = new ResourceLocation("configlib", "reset_icon_64x64.png"); | ||
|
|
||
| private final ConfigGuiScreen parentScreen; | ||
| private final RendererManager rendererManager; | ||
| private final RendererPosition rendererPosition; | ||
| private boolean toggled; | ||
| private final ClickGuiButton buttonEnabled; | ||
| private final ClickGuiButton buttonMoveHud; | ||
| private final ClickGuiButton buttonResetPos; | ||
|
|
||
| public RendererGuiButton( | ||
| ConfigGuiScreen configGuiScreen, | ||
|
|
@@ -40,14 +32,14 @@ public RendererGuiButton( | |
| this.rendererManager = rendererManager; | ||
| this.rendererPosition = ((RendererPosition) field.get(null)); | ||
| this.toggled = this.rendererPosition.isEnabled(); | ||
| this.buttonEnabled = new ClickGuiButton(0, 0, 0, mc.fontRendererObj.getStringWidth(" Disabled "), 20, getButtonText()); | ||
| this.buttonMoveHud = new ClickGuiButton(0, 0, 0, 20, 20, ""); | ||
| this.buttonResetPos = new ClickGuiButton(0, 0, 0, 20, 20, ""); | ||
| final int btnWidth = mc.fontRendererObj.getStringWidth(" Disabled "); | ||
| this.buttonEnabled = new ClickGuiButton(0, 0, 0, btnWidth, 20, getButtonText()); | ||
| this.buttonMoveHud = new ClickGuiButton(0, 0, 0, btnWidth, 20, "Position"); | ||
| } | ||
|
|
||
| @Override | ||
| public void setBoxWidth(int boxWidth) { | ||
| super.setBoxWidth(boxWidth - mc.fontRendererObj.getStringWidth("Reset Position") - 10); | ||
| super.setBoxWidth(boxWidth); | ||
| this.boxWidth = boxWidth; | ||
| } | ||
|
|
||
|
|
@@ -60,21 +52,6 @@ public void draw(ColorPalette colorPalette, int drawX, int drawY, int mouseX, in | |
| buttonMoveHud.xPosition = buttonEnabled.xPosition; | ||
| buttonMoveHud.yPosition = buttonEnabled.yPosition + buttonEnabled.height + 1; | ||
| buttonMoveHud.drawButton(colorPalette, mc, mouseX, mouseY); | ||
| buttonResetPos.xPosition = buttonEnabled.xPosition + buttonEnabled.width - buttonResetPos.width - 1; | ||
| buttonResetPos.yPosition = buttonMoveHud.yPosition; | ||
| buttonResetPos.drawButton(colorPalette, mc, mouseX, mouseY); | ||
| drawIcon(MOVE_ICON, buttonMoveHud.xPosition, buttonMoveHud.yPosition); | ||
| drawIcon(RESET_ICON, buttonResetPos.xPosition, buttonResetPos.yPosition); | ||
| if (buttonMoveHud.isMouseOver()) { | ||
| final int textX = buttonEnabled.xPosition - 4 - mc.fontRendererObj.getStringWidth("Move HUD"); | ||
| final int textY = buttonMoveHud.yPosition + mc.fontRendererObj.FONT_HEIGHT / 2 + 1; | ||
| mc.fontRendererObj.drawStringWithShadow("Move HUD", textX, textY, colorPalette.HUD_BUTTON_HINT_TEXT); | ||
| } | ||
| if (buttonResetPos.isMouseOver()) { | ||
| final int textX = buttonEnabled.xPosition - 4 - mc.fontRendererObj.getStringWidth("Reset Position"); | ||
| final int textY = buttonResetPos.yPosition + mc.fontRendererObj.FONT_HEIGHT / 2 + 1; | ||
| mc.fontRendererObj.drawStringWithShadow("Reset Position", textX, textY, colorPalette.HUD_BUTTON_HINT_TEXT); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -89,15 +66,11 @@ public boolean mouseClicked(int mouseX, int mouseY, int mouseButton) { | |
| buttonEnabled.playPressSound(mc.getSoundHandler()); | ||
| final IRenderer renderer = this.rendererManager.getRendererFromPosition(rendererPosition); | ||
| if (renderer != null) { | ||
| mc.displayGuiScreen(new RendererEditGuiScreen(this.rendererManager, renderer, parentScreen)); | ||
| mc.displayGuiScreen(new RendererEditGuiScreen(this.rendererManager, renderer, parentScreen, parentScreen.getColorPalette())); | ||
| } else { | ||
| throw new RuntimeException("No registered renderer associated to " + field.getName()); | ||
| } | ||
| return true; | ||
| } else if (buttonResetPos.mousePressed(mc, mouseX, mouseY)) { | ||
| rendererPosition.resetToDefault(); | ||
| buttonEnabled.playPressSound(mc.getSoundHandler()); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
|
|
@@ -108,20 +81,6 @@ public int getHeight() { | |
| return Math.max(super.getHeight(), 8 + buttonEnabled.height + 1 + buttonMoveHud.height + 8 - 1); | ||
| } | ||
|
|
||
| private void drawIcon(ResourceLocation icon, int drawX, int drawY) { | ||
| drawX += 3; | ||
| drawY += 3; | ||
| GlStateManager.pushMatrix(); | ||
| GlStateManager.enableAlpha(); | ||
| GlStateManager.enableBlend(); | ||
| GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0); | ||
| GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | ||
| parentScreen.mc.getTextureManager().bindTexture(icon); | ||
| GlStateManager.color(1, 1, 1); | ||
| Gui.drawModalRectWithCustomSizedTexture(drawX, drawY, 0f, 0f, 14, 14, 14f, 14f); | ||
| GlStateManager.popMatrix(); | ||
| } | ||
|
|
||
| private void flipBooleanConfig() { | ||
| rendererPosition.setEnabled(!rendererPosition.isEnabled()); | ||
| toggled = rendererPosition.isEnabled(); | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete this field
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field should be used throughtout the codebase instead of the magic value when comparing with
mouseButton.This is not just for this pull request but for further onces aswell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok but I dislike it being here, just make it a local variable in the RendererEditGuiScreen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the ConfigGuiScreen I can add an MouseClick enum or something to remove the magic numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand this and I will make it a local variable for now.
but think of it like this, many different classes check
mouseButton:ConfigGuiScreen,RendererEditGuiScreen,ColorEditScreen,ConfigGuiButtons, etc...and it would be convinient and easy to read if this value was shared.
also for developers who create their own elements.
and there is no need to create an enum for this, this is simple enough and very understandable to me and I assume other devs.
later on you can also add
MOUSE_RIGHT = 1if you want