Forráskód Böngészése

Fix tooltips rendering underneath scrollbar and overlaps

Jared 4 éve
szülő
commit
6879f8ba8f

+ 62 - 47
src/main/java/com/blamejared/controlling/client/gui/GuiNewKeyBindingList.java

@@ -9,6 +9,7 @@ import net.minecraft.client.gui.widget.button.Button;
 import net.minecraft.client.gui.widget.list.KeyBindingList;
 import net.minecraft.client.resources.I18n;
 import net.minecraft.client.settings.KeyBinding;
+import net.minecraft.util.math.MathHelper;
 import net.minecraft.util.text.*;
 import net.minecraftforge.api.distmarker.*;
 import net.minecraftforge.fml.client.gui.GuiUtils;
@@ -18,11 +19,11 @@ import java.util.*;
 
 @OnlyIn(Dist.CLIENT)
 public class GuiNewKeyBindingList extends GuiCustomList {
-
+    
     private final ControlsScreen controlsScreen;
     private final Minecraft mc;
     private int maxListLabelWidth;
-
+    
     public GuiNewKeyBindingList(ControlsScreen controls, Minecraft mcIn) {
         super(controls, mcIn);
         this.width = controls.width + 45;
@@ -37,68 +38,86 @@ public class GuiNewKeyBindingList extends GuiCustomList {
         KeyBinding[] akeybinding = ArrayUtils.clone(mcIn.gameSettings.keyBindings);
         Arrays.sort(akeybinding);
         String s = null;
-
-        for (KeyBinding keybinding : akeybinding) {
+        
+        for(KeyBinding keybinding : akeybinding) {
             String s1 = keybinding.getKeyCategory();
-            if (!s1.equals(s)) {
+            if(!s1.equals(s)) {
                 s = s1;
-                if (!s1.endsWith(".hidden")) {
+                if(!s1.endsWith(".hidden")) {
                     add(new GuiNewKeyBindingList.CategoryEntry(s1));
                 }
             }
-
+            
             int i = mcIn.fontRenderer.getStringWidth(I18n.format(keybinding.getKeyDescription()));
-            if (i > this.maxListLabelWidth) {
+            if(i > this.maxListLabelWidth) {
                 this.maxListLabelWidth = i;
             }
-            if (!s1.endsWith(".hidden")) {
+            if(!s1.endsWith(".hidden")) {
                 add(new GuiNewKeyBindingList.KeyEntry(keybinding));
             }
         }
-
+        
     }
-
-
+    
+    @Override
+    protected void renderDecorations(MatrixStack matrixStack, int mouseX, int mouseY) {
+        Entry entry = this.getEntryAtPos(mouseY);
+        if(!(entry instanceof KeyEntry)) {
+            return;
+        }
+        KeyEntry keyEntry = (KeyEntry) entry;
+        GuiUtils.drawHoveringText(matrixStack, Collections.singletonList(new TranslationTextComponent(keyEntry.getKeybinding().getKeyCategory())), mouseX, mouseY, mc.currentScreen.width, mc.currentScreen.height, 0, mc.fontRenderer);
+    }
+    
+    public Entry getEntryAtPos(double mouseY) {
+        if(mouseY <= getTop() || mouseY >= getBottom()){
+            return null;
+        }
+        int i1 = MathHelper.floor(mouseY - (double) this.y0) - this.headerHeight + (int) this.getScrollAmount() - 4;
+        int j1 = i1 / this.itemHeight;
+        return i1 >= 0 && j1 < this.getItemCount() ? this.getEventListeners().get(j1) : null;
+    }
+    
     @Override
     protected int getScrollbarPosition() {
         return super.getScrollbarPosition() + 15 + 20;
     }
-
+    
     @Override
     public int getRowWidth() {
         return super.getRowWidth() + 32;
     }
-
+    
     @OnlyIn(Dist.CLIENT)
     public class CategoryEntry extends KeyBindingList.Entry {
-
+        
         private final String labelText;
         private final int labelWidth;
         private final String name;
-
+        
         public CategoryEntry(String name) {
             this.labelText = I18n.format(name);
             this.labelWidth = GuiNewKeyBindingList.this.mc.fontRenderer.getStringWidth(this.labelText);
             this.name = name;
         }
-
+        
         public String getName() {
             return name;
         }
-
+        
         @Override
         public List<? extends IGuiEventListener> getEventListeners() {
             return ImmutableList.of();
         }
-
+        
         public void render(MatrixStack stack, int p_render_1_, int p_render_2_, int p_render_3_, int p_render_4_, int p_render_5_, int p_render_6_, int p_render_7_, boolean p_render_8_, float p_render_9_) {
             GuiNewKeyBindingList.this.minecraft.fontRenderer.drawString(stack, this.labelText, (float) (GuiNewKeyBindingList.this.minecraft.currentScreen.width / 2 - this.labelWidth / 2), (float) (p_render_2_ + p_render_5_ - 9 - 1), 16777215);
         }
     }
-
+    
     @OnlyIn(Dist.CLIENT)
     public class KeyEntry extends KeyBindingList.Entry {
-
+        
         /**
          * The keybinding specified for this KeyEntry
          */
@@ -109,15 +128,15 @@ public class GuiNewKeyBindingList extends GuiCustomList {
         private final String keyDesc;
         private final Button btnChangeKeyBinding;
         private final Button btnResetKeyBinding;
-
-
+        
+        
         private KeyEntry(final KeyBinding name) {
             this.keybinding = name;
             this.keyDesc = I18n.format(name.getKeyDescription());
             this.btnChangeKeyBinding = new Button(0, 0, 75 + 20 /*Forge: add space*/, 20, new StringTextComponent(this.keyDesc), (p_214386_2_) -> {
                 GuiNewKeyBindingList.this.controlsScreen.buttonId = name;
             }) {
-            	@Override
+                @Override
                 protected IFormattableTextComponent getNarrationMessage() {
                     return name.isInvalid() ? new TranslationTextComponent("narrator.controls.unbound", GuiNewKeyBindingList.KeyEntry.this.keyDesc) : new TranslationTextComponent("narrator.controls.bound", GuiNewKeyBindingList.KeyEntry.this.keyDesc, super.getNarrationMessage());
                 }
@@ -127,13 +146,13 @@ public class GuiNewKeyBindingList extends GuiCustomList {
                 GuiNewKeyBindingList.this.minecraft.gameSettings.setKeyBindingCode(name, name.getDefault());
                 KeyBinding.resetKeyBindingArrayAndHash();
             }) {
-            	@Override
+                @Override
                 protected IFormattableTextComponent getNarrationMessage() {
                     return new TranslationTextComponent("narrator.controls.reset", GuiNewKeyBindingList.KeyEntry.this.keyDesc);
                 }
             };
         }
-
+        
         @Override
         public void render(MatrixStack stack, int slotIndex, int y, int x, int p_render_4_, int p_render_5_, int mouseX, int mouseY, boolean p_render_8_, float p_render_9_) {
             int i = y;
@@ -145,64 +164,60 @@ public class GuiNewKeyBindingList extends GuiCustomList {
             this.btnResetKeyBinding.y = y;
             this.btnResetKeyBinding.active = !this.keybinding.isDefault();
             this.btnResetKeyBinding.render(stack, mouseX, mouseY, p_render_9_);
-
-
+            
+            
             this.btnChangeKeyBinding.x = j + 105;
             this.btnChangeKeyBinding.y = i;
             this.btnChangeKeyBinding.setMessage(this.keybinding.func_238171_j_());
-
+            
             boolean flag1 = false;
             boolean keyCodeModifierConflict = true; // less severe form of conflict, like SHIFT conflicting with SHIFT+G
-            if (!this.keybinding.isInvalid()) {
-                for (KeyBinding keybinding : GuiNewKeyBindingList.this.mc.gameSettings.keyBindings) {
-                    if (keybinding != this.keybinding && this.keybinding.conflicts(keybinding)) {
+            if(!this.keybinding.isInvalid()) {
+                for(KeyBinding keybinding : GuiNewKeyBindingList.this.mc.gameSettings.keyBindings) {
+                    if(keybinding != this.keybinding && this.keybinding.conflicts(keybinding)) {
                         flag1 = true;
                         keyCodeModifierConflict &= keybinding.hasKeyCodeModifierConflict(this.keybinding);
                     }
                 }
             }
             ITextComponent message = this.btnChangeKeyBinding.getMessage();
-            if (flag) {
+            if(flag) {
                 this.btnChangeKeyBinding.setMessage(new StringTextComponent(TextFormatting.WHITE + "> " + TextFormatting.YELLOW + message.getString() + TextFormatting.WHITE + " <"));
-            } else if (flag1) {
+            } else if(flag1) {
                 IFormattableTextComponent modConflict = TextComponentUtils.func_240648_a_(message.copyRaw(), message.getStyle().setColor(Color.fromInt(16755200)));
                 IFormattableTextComponent keyConflict = TextComponentUtils.func_240648_a_(message.copyRaw(), message.getStyle().setColor(Color.fromInt(16755200)));
-
+                
                 this.btnChangeKeyBinding.setMessage(keyCodeModifierConflict ? modConflict : keyConflict);
             }
-
+            
             this.btnChangeKeyBinding.render(stack, mouseX, mouseY, p_render_9_);
-            if (mouseY >= y && mouseY <= y + p_render_5_) {
-                GuiUtils.drawHoveringText(stack, Collections.singletonList(new TranslationTextComponent(keybinding.getKeyCategory())), mouseX, mouseY, mc.currentScreen.width, mc.currentScreen.height, 0, mc.fontRenderer);
-//                mc.fontRenderer.drawString(stack, I18n.format(keybinding.getKeyCategory()), mouseX + 10, mouseY, 0xFFFFFF);
-            }
         }
-
+        
         public List<? extends IGuiEventListener> getEventListeners() {
             return ImmutableList.of(this.btnChangeKeyBinding, this.btnResetKeyBinding);
         }
-
+        
         @Override
         public boolean mouseClicked(double mouseX, double mouseY, int buttonId) {
-            if (this.btnChangeKeyBinding.mouseClicked(mouseX, mouseY, buttonId)) {
+            if(this.btnChangeKeyBinding.mouseClicked(mouseX, mouseY, buttonId)) {
                 return true;
             } else {
                 return this.btnResetKeyBinding.mouseClicked(mouseX, mouseY, buttonId);
             }
         }
-
+        
         @Override
         public boolean mouseReleased(double mouseX, double mouseY, int buttonId) {
             return this.btnChangeKeyBinding.mouseReleased(mouseX, mouseY, buttonId);
         }
-
+        
         public KeyBinding getKeybinding() {
             return keybinding;
         }
-
+        
         public String getKeyDesc() {
             return keyDesc;
         }
-
+        
     }
 }