• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / CyclopsCore / #479033799

31 May 2025 02:56PM UTC coverage: 22.091% (-0.02%) from 22.106%
#479033799

push

github

rubensworks
Fix scrollbar not moving on screen resize

Closes CyclopsMC/IntegratedDynamics#1515

0 of 9 new or added lines in 2 files covered. (0.0%)

2297 of 10398 relevant lines covered (22.09%)

0.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/cyclops/cyclopscore/client/gui/container/ContainerScreenScrolling.java
1
package org.cyclops.cyclopscore.client.gui.container;
2

3
import com.google.common.collect.Lists;
4
import com.mojang.blaze3d.vertex.PoseStack;
5
import net.minecraft.client.gui.components.EditBox;
6
import net.minecraft.network.chat.Component;
7
import net.minecraft.world.entity.player.Inventory;
8
import net.minecraft.world.inventory.Slot;
9
import org.cyclops.cyclopscore.client.gui.component.WidgetScrollBar;
10
import org.cyclops.cyclopscore.client.gui.component.input.WidgetTextFieldExtended;
11
import org.cyclops.cyclopscore.inventory.container.ScrollingInventoryContainer;
12
import org.lwjgl.glfw.GLFW;
13

14
import java.awt.*;
15
import java.util.List;
16

17
/**
18
 * Gui for an inventory container that has a scrollbar and search field.
19
 * @author rubensworks
20
 */
21
public abstract class ContainerScreenScrolling<T extends ScrollingInventoryContainer> extends ContainerScreenExtended<T> {
22

23
    private static final int SEARCH_WIDTH = 89;
24

25
    private WidgetTextFieldExtended searchField = null;
×
26
    private WidgetScrollBar scrollbar = null;
×
27

28
    public ContainerScreenScrolling(T container, Inventory playerInventory, Component title) {
29
        super(container, playerInventory, title);
×
30
    }
×
31

32
    @Override
33
    public void init() {
34
        super.init();
×
35
        this.minecraft.keyboardHandler.setSendRepeatsToGui(true);
×
36

37
        if(isSearchEnabled()) {
×
38
            int searchWidth = getSearchWidth();
×
39
            int searchX = getSearchX();
×
40
            int searchY = getSearchY();
×
41
            if(this.searchField == null) {
×
42
                this.searchField = new WidgetTextFieldExtended(this.font, this.leftPos + searchX, this.topPos + searchY, searchWidth, this.font.lineHeight, Component.translatable("gui.cyclopscore.search"));
×
43
                this.searchField.setMaxLength(64);
×
44
                this.searchField.setMaxLength(15);
×
45
                this.searchField.setBordered(false);
×
46
                this.searchField.setVisible(true);
×
47
                this.searchField.setTextColor(16777215);
×
48
                this.searchField.setCanLoseFocus(true);
×
49
                this.searchField.setValue("");
×
50
                this.searchField.setWidth(searchWidth);
×
51
                this.searchField.x = this.leftPos + (searchX + searchWidth) - this.searchField.getWidth();
×
52
            } else {
53
                this.searchField.setWidth(searchWidth);
×
54
                this.searchField.x = this.leftPos + (searchX + searchWidth) - this.searchField.getWidth();
×
55
                this.searchField.y = this.topPos + searchY;
×
56
            }
57
            this.addWidget(this.searchField);
×
58
        }
59

60
        // Initial element load.
61
        if (scrollbar == null) {
×
62
            getMenu().updateFilter("");
×
63
            this.scrollbar = new WidgetScrollBar(this.leftPos + getScrollX(), this.topPos + getScrollY(), getScrollHeight(),
×
64
                    Component.translatable("gui.cyclopscore.scrollbar"), getMenu(),
×
65
                    getMenu().getPageSize(), getScrollRegion());
×
66
            this.scrollbar.setTotalRows(getMenu().getFilteredItemCount() / getMenu().getColumns());
×
67
        } else {
NEW
68
            this.scrollbar.setX(this.leftPos + getScrollX());
×
NEW
69
            this.scrollbar.setY(this.topPos + getScrollY());
×
NEW
70
            this.scrollbar.setScollRegion(getScrollRegion());
×
71
        }
72

73
        this.addWidget(this.scrollbar);
×
74
        getScrollbar().scrollTo(this.scrollbar.getCurrentScroll());
×
75
    }
×
76

77
    /**
78
     * @return A custom region in which scrolling should also be allowed next to the scrollbar itself.
79
     */
80
    protected Rectangle getScrollRegion() {
81
        return null;
×
82
    }
83

84
    @Override
85
    public void removed() {
86
        super.removed();
×
87
        this.minecraft.keyboardHandler.setSendRepeatsToGui(false);
×
88
    }
×
89

90
    @Override
91
    public void containerTick() {
92
        super.containerTick();
×
93
        if (this.searchField != null) {
×
94
            this.searchField.tick();
×
95
        }
96
    }
×
97

98
    @Override
99
    public boolean charTyped(char typedChar, int keyCode) {
100
        if (isSearchEnabled() && this.searchField.isFocused()) {
×
101
            if (this.searchField.charTyped(typedChar, keyCode)) {
×
102
                this.updateSearch(searchField.getValue());
×
103
            }
104
            return true;
×
105
        } else {
106
            return super.charTyped(typedChar, keyCode);
×
107
        }
108
    }
109

110
    @Override
111
    public boolean keyPressed(int typedChar, int keyCode, int modifiers) {
112
        if (isSearchEnabled() && this.searchField.isFocused() && typedChar != GLFW.GLFW_KEY_ESCAPE) {
×
113
            if (this.searchField.keyPressed(typedChar, keyCode, modifiers)) {
×
114
                this.updateSearch(searchField.getValue());
×
115
            }
116
            return true;
×
117
        } else {
118
            return super.keyPressed(typedChar, keyCode, modifiers);
×
119
        }
120
    }
121

122
    @Override
123
    protected void drawCurrentScreen(PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) {
124
        if (isSubsetRenderSlots()) {
×
125
            // Temporarily swap slot list, to avoid rendering all slots (which would include the hidden ones)
126
            List<Slot> oldSlots = Lists.newArrayList(this.container.slots);
×
127
            int startIndex = getMenu().getFirstElement();
×
128
            List<Slot> newSlots = Lists.newArrayList();
×
129
            newSlots.addAll(oldSlots.subList(startIndex, Math.min(oldSlots.size(), startIndex
×
130
                    + (getMenu().getPageSize() * getMenu().getColumns()))));
×
131
            newSlots.addAll(oldSlots.subList(getMenu().getUnfilteredItemCount(), oldSlots.size()));
×
132
            this.container.slots.clear();
×
133
            this.container.slots.addAll(newSlots);
×
134
            super.drawCurrentScreen(matrixStack, mouseX, mouseY, partialTicks);
×
135
            this.container.slots.clear();
×
136
            this.container.slots.addAll(oldSlots);
×
137
        } else {
×
138
            super.drawCurrentScreen(matrixStack, mouseX, mouseY, partialTicks);
×
139
        }
140
    }
×
141

142
    /**
143
     * @return If the optimization should be done for only rendering the visible slots. Default: false
144
     */
145
    protected boolean isSubsetRenderSlots() {
146
        return false;
×
147
    }
148

149
    @Override
150
    protected void renderBg(PoseStack matrixStack, float partialTicks, int mouseX, int mouseY) {
151
        super.renderBg(matrixStack, partialTicks, mouseX, mouseY);
×
152
        if(isSearchEnabled()) this.searchField.render(matrixStack, mouseX, mouseY, partialTicks);
×
153
        this.scrollbar.drawGuiContainerBackgroundLayer(matrixStack, partialTicks, mouseX, mouseY);
×
154
    }
×
155

156
    @Override
157
    public boolean mouseDragged(double mouseX, double mouseY, int mouseButton, double mouseXPrev, double mouseYPrev) {
158
        if (this.getFocused() != null && this.isDragging() && mouseButton == 0
×
159
                && this.getFocused().mouseDragged(mouseX, mouseY, mouseButton, mouseXPrev, mouseYPrev)) {
×
160
            return true;
×
161
        }
162
        return super.mouseDragged(mouseX, mouseY, mouseButton, mouseXPrev, mouseYPrev);
×
163
    }
164

165
    protected void updateSearch(String searchString) {
166
        getMenu().updateFilter(searchString);
×
167
        this.scrollbar.setTotalRows(getMenu().getFilteredItemCount() / getMenu().getColumns());
×
168
        this.scrollbar.scrollTo(0);
×
169
    }
×
170

171
    public EditBox getSearchField() {
172
        return searchField;
×
173
    }
174

175
    public WidgetScrollBar getScrollbar() {
176
        return scrollbar;
×
177
    }
178

179
    protected int getScrollX() {
180
        return 175;
×
181
    }
182

183
    protected int getScrollY() {
184
        return 18;
×
185
    }
186

187
    protected int getScrollHeight() {
188
        return 112;
×
189
    }
190

191
    protected boolean isSearchEnabled() {
192
        return true;
×
193
    }
194

195
    protected int getSearchX() {
196
        return 82;
×
197
    }
198

199
    protected int getSearchY() {
200
        return 6;
×
201
    }
202

203
    protected int getSearchWidth() {
204
        return SEARCH_WIDTH;
×
205
    }
206

207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc