浏览代码

Put the search bar to the side automatically if there is no space.

Signed-off-by: shedaniel <daniel@shedaniel.me>
shedaniel 4 年之前
父节点
当前提交
71d8d61705

+ 4 - 0
RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/DisplayHelper.java

@@ -189,10 +189,14 @@ public interface DisplayHelper {
          * @param rectangle the overlay bounds
          * @return the item list bounds
          */
+        @Deprecated
+        @ApiStatus.ScheduledForRemoval
         default Rectangle getItemListArea(Rectangle rectangle) {
             return new Rectangle(rectangle.x + 1, rectangle.y + 2 + (ConfigObject.getInstance().getSearchFieldLocation() == SearchFieldLocation.TOP_SIDE ? 24 : 0) + (ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22), rectangle.width - 2, rectangle.height - (ConfigObject.getInstance().getSearchFieldLocation() != SearchFieldLocation.CENTER ? 27 + 22 : 27) + (!ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22));
         }
         
+        @Deprecated
+        @ApiStatus.ScheduledForRemoval
         default Rectangle getFavoritesListArea(Rectangle rectangle) {
             int offset = 31 + (ConfigObject.getInstance().doesShowUtilsButtons() ? 25 : 0);
             return new Rectangle(rectangle.x + 1, rectangle.y + 2 + offset, rectangle.width - 2, rectangle.height - 5 - offset);

+ 30 - 11
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/gui/ContainerScreenOverlay.java

@@ -435,19 +435,38 @@ public class ContainerScreenOverlay extends WidgetWithBounds implements REIOverl
     }
     
     private Rectangle getSearchFieldArea() {
-        int widthRemoved = 1 + (ConfigObject.getInstance().isCraftableFilterEnabled() ? 22 : 0) + (ConfigObject.getInstance().isLowerConfigButton() ? 22 : 0);
-        SearchFieldLocation searchFieldLocation = ConfigObject.getInstance().getSearchFieldLocation();
-        if (searchFieldLocation == SearchFieldLocation.BOTTOM_SIDE)
-            return new Rectangle(bounds.x + 2, window.getGuiScaledHeight() - 22, bounds.width - 6 - widthRemoved, 18);
-        if (searchFieldLocation == SearchFieldLocation.TOP_SIDE)
-            return new Rectangle(bounds.x + 2, 4, bounds.width - 6 - widthRemoved, 18);
-        for (OverlayDecider decider : DisplayHelper.getInstance().getSortedOverlayDeciders(Minecraft.getInstance().screen.getClass())) {
-            if (decider instanceof DisplayHelper.DisplayBoundsProvider) {
-                Rectangle containerBounds = ((DisplayHelper.DisplayBoundsProvider<Screen>) decider).getScreenBounds(Minecraft.getInstance().screen);
-                return new Rectangle(containerBounds.x, window.getGuiScaledHeight() - 22, containerBounds.width - widthRemoved, 18);
+        int widthRemoved = 1;
+        if (ConfigObject.getInstance().isCraftableFilterEnabled()) widthRemoved += 22;
+        if (ConfigObject.getInstance().isLowerConfigButton()) widthRemoved += 22;
+        SearchFieldLocation searchFieldLocation = ScreenHelper.getContextualSearchFieldLocation();
+        switch (searchFieldLocation) {
+            case TOP_SIDE:
+                return getTopSideSearchFieldArea(widthRemoved);
+            case BOTTOM_SIDE:
+                return getBottomSideSearchFieldArea(widthRemoved);
+            default:
+            case CENTER: {
+                for (OverlayDecider decider : DisplayHelper.getInstance().getSortedOverlayDeciders(Minecraft.getInstance().screen.getClass())) {
+                    if (decider instanceof DisplayHelper.DisplayBoundsProvider) {
+                        Rectangle containerBounds = ((DisplayHelper.DisplayBoundsProvider<Screen>) decider).getScreenBounds(Minecraft.getInstance().screen);
+                        return getBottomCenterSearchFieldArea(containerBounds, widthRemoved);
+                    }
+                }
+                return new Rectangle();
             }
         }
-        return new Rectangle();
+    }
+    
+    private Rectangle getTopSideSearchFieldArea(int widthRemoved) {
+        return new Rectangle(bounds.x + 2, 4, bounds.width - 6 - widthRemoved, 18);
+    }
+    
+    private Rectangle getBottomSideSearchFieldArea(int widthRemoved) {
+        return new Rectangle(bounds.x + 2, window.getGuiScaledHeight() - 22, bounds.width - 6 - widthRemoved, 18);
+    }
+    
+    private Rectangle getBottomCenterSearchFieldArea(Rectangle containerBounds, int widthRemoved) {
+        return new Rectangle(containerBounds.x, window.getGuiScaledHeight() - 22, containerBounds.width - widthRemoved, 18);
     }
     
     private Rectangle getCraftableToggleArea() {

+ 27 - 7
RoughlyEnoughItems-runtime/src/main/java/me/shedaniel/rei/impl/ScreenHelper.java

@@ -33,10 +33,7 @@ import me.shedaniel.math.Point;
 import me.shedaniel.math.Rectangle;
 import me.shedaniel.math.api.Executor;
 import me.shedaniel.rei.RoughlyEnoughItemsState;
-import me.shedaniel.rei.api.ConfigManager;
-import me.shedaniel.rei.api.ConfigObject;
-import me.shedaniel.rei.api.REIHelper;
-import me.shedaniel.rei.api.REIOverlay;
+import me.shedaniel.rei.api.*;
 import me.shedaniel.rei.api.widgets.Tooltip;
 import me.shedaniel.rei.gui.ContainerScreenOverlay;
 import me.shedaniel.rei.gui.OverlaySearchField;
@@ -48,6 +45,7 @@ import net.fabricmc.api.ClientModInitializer;
 import net.fabricmc.api.EnvType;
 import net.fabricmc.api.Environment;
 import net.fabricmc.fabric.api.event.client.ClientTickCallback;
+import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
 import net.fabricmc.loader.api.FabricLoader;
 import net.minecraft.client.Minecraft;
 import net.minecraft.client.gui.screens.Screen;
@@ -227,13 +225,35 @@ public class ScreenHelper implements ClientModInitializer, REIHelper {
         attachInstance(instance, REIHelper.class);
     }
     
+    public static SearchFieldLocation getContextualSearchFieldLocation() {
+        Window window = Minecraft.getInstance().getWindow();
+        for (OverlayDecider decider : DisplayHelper.getInstance().getSortedOverlayDeciders(Minecraft.getInstance().screen.getClass())) {
+            if (decider instanceof DisplayHelper.DisplayBoundsProvider) {
+                Rectangle containerBounds = ((DisplayHelper.DisplayBoundsProvider<Screen>) decider).getScreenBounds(Minecraft.getInstance().screen);
+                if (window.getGuiScaledHeight() - 20 <= containerBounds.getMaxY())
+                    return SearchFieldLocation.BOTTOM_SIDE;
+                else break;
+            }
+        }
+        return ConfigObject.getInstance().getSearchFieldLocation();
+    }
+    
     public static Rectangle getItemListArea(Rectangle bounds) {
-        return new Rectangle(bounds.x, bounds.y + 2 + (ConfigObject.getInstance().getSearchFieldLocation() == SearchFieldLocation.TOP_SIDE ? 24 : 0) + (ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22), bounds.width, bounds.height - (ConfigObject.getInstance().getSearchFieldLocation() != SearchFieldLocation.CENTER ? 27 + 22 : 27) + (!ConfigObject.getInstance().isEntryListWidgetScrolled() ? 0 : 22));
+        SearchFieldLocation searchFieldLocation = ScreenHelper.getContextualSearchFieldLocation();
+        
+        int yOffset = 2;
+        if (searchFieldLocation == SearchFieldLocation.TOP_SIDE) yOffset += 24;
+        if (!ConfigObject.getInstance().isEntryListWidgetScrolled()) yOffset += 22;
+        int heightOffset = 0;
+        if (searchFieldLocation == SearchFieldLocation.BOTTOM_SIDE) heightOffset += 24;
+        return new Rectangle(bounds.x, bounds.y + yOffset, bounds.width, bounds.height - 1 - yOffset - heightOffset);
     }
     
     public static Rectangle getFavoritesListArea(Rectangle bounds) {
-        int offset = 6 + (!ConfigObject.getInstance().isLowerConfigButton() ? 25 : 0) + (ConfigObject.getInstance().doesShowUtilsButtons() ? 25 : 0);
-        return new Rectangle(bounds.x, bounds.y + 2 + offset, bounds.width, bounds.height - 5 - offset);
+        int yOffset = 8;
+        if (ConfigObject.getInstance().doesShowUtilsButtons()) yOffset += 50;
+        else if (!ConfigObject.getInstance().isLowerConfigButton()) yOffset += 25;
+        return new Rectangle(bounds.x, bounds.y + yOffset, bounds.width, bounds.height - 3 - yOffset);
     }
     
     @Override

+ 1 - 1
gradle.properties

@@ -1,5 +1,5 @@
 org.gradle.jvmargs=-Xmx3G
-mod_version=5.2.7
+mod_version=5.2.8
 supported_version=1.16.2
 minecraft_version=1.16.2-rc1
 yarn_version=1.16.2-rc1+build.4+legacy.20w09a+build.8