• 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/component/WidgetScrollBar.java
1
package org.cyclops.cyclopscore.client.gui.component;
2

3
import com.mojang.blaze3d.vertex.PoseStack;
4
import net.minecraft.client.gui.components.AbstractWidget;
5
import net.minecraft.client.gui.narration.NarrationElementOutput;
6
import net.minecraft.resources.ResourceLocation;
7
import net.minecraft.util.Mth;
8
import net.minecraft.network.chat.Component;
9
import org.cyclops.cyclopscore.helper.RenderHelpers;
10

11
import javax.annotation.Nullable;
12
import java.awt.*;
13

14
/**
15
 * A reusable scrollbar for screens.
16
 *
17
 * The using screen must add this as a child
18
 * and call the following method from its respective method:
19
 * * {@link #drawGuiContainerBackgroundLayer(PoseStack, float, int, int)}}
20
 * * {@link #mouseDragged(double, double, int, double, double)} (@see ContainerScreenScrolling for an example)
21
 *
22
 * @author rubensworks
23
 */
24
public class WidgetScrollBar extends AbstractWidget {
25

26
    private static final ResourceLocation SCROLLBUTTON = new ResourceLocation("textures/gui/container/creative_inventory/tabs.png");
×
27
    private static final int SCROLL_BUTTON_HEIGHT = 15;
28
    private static final int SCROLL_BUTTON_WIDTH = 12;
29

30
    private final int height;
31
    @Nullable
32
    private final IScrollCallback scrollCallback;
33
    @Nullable
34
    private Rectangle scollRegion;
35

36
    private int totalRows;
37
    private int visibleRows;
38
    private float currentScroll; // (0 = top, 1 = bottom)
39
    private boolean isScrolling; // if the scrollbar is being dragged
40
    private boolean wasClicking; // if the left mouse button was held down last time drawScreen was called
41

42
    public WidgetScrollBar(int x, int y, int height, Component narrationMessage,
43
                           @Nullable IScrollCallback scrollCallback, int visibleRows) {
44
        this(x, y, height, narrationMessage, scrollCallback, visibleRows, null);
×
45
    }
×
46

47
    public WidgetScrollBar(int x, int y, int height, Component narrationMessage,
48
                           @Nullable IScrollCallback scrollCallback, int visibleRows, Rectangle scollRegion) {
49
        super(x, y, WidgetScrollBar.SCROLL_BUTTON_WIDTH, height, narrationMessage);
×
50
        this.height = height;
×
51
        this.scrollCallback = scrollCallback;
×
52
        this.scollRegion = scollRegion;
×
53

54
        this.currentScroll = 0;
×
55
        this.isScrolling = false;
×
56
        this.wasClicking = false;
×
57
        setVisibleRows(visibleRows);
×
58
    }
×
59

60
    public void setX(int x) {
NEW
61
        this.x = x;
×
NEW
62
    }
×
63

64
    public void setY(int y) {
NEW
65
        this.y = y;
×
NEW
66
    }
×
67

68
    public void setScollRegion(@Nullable Rectangle scollRegion) {
NEW
69
        this.scollRegion = scollRegion;
×
NEW
70
    }
×
71

72
    @Override
73
    public boolean isMouseOver(double x, double y) {
74
        if (scollRegion != null) {
×
75
            if (RenderHelpers.isPointInRegion(scollRegion, new Point((int) x, (int) y))) {
×
76
                return true;
×
77
            }
78
        }
79
        return super.isMouseOver(x, y);
×
80
    }
81

82
    /**
83
     * @return The current scroll position.
84
     */
85
    public float getCurrentScroll() {
86
        return currentScroll;
×
87
    }
88

89
    @Override
90
    public boolean mouseScrolled(double mouseX, double mouseY, double scroll) {
91
        if (scroll != 0 && this.needsScrollBars()) {
×
92
            scrollRelative(scroll);
×
93
            return true;
×
94
        }
95
        return false;
×
96
    }
97

98
    @Override
99
    public boolean mouseDragged(double mouseX, double mouseY, int mouseButton, double offsetX, double offsetY) {
100
        boolean flag = mouseButton == 0 || mouseButton == 1;
×
101
        int xMax = x + 14;
×
102
        int yMax = y + height;
×
103

104
        // Reset scroll if too big for current view
105
        if (!needsScrollBars()) {
×
106
            scrollTo(0);
×
107
            return true;
×
108
        }
109

110
        if (!this.wasClicking && flag && mouseX >= x && mouseY >= y && mouseX < xMax && mouseY < yMax) {
×
111
            this.isScrolling = this.needsScrollBars();
×
112
        }
113

114
        if (!flag) {
×
115
            this.isScrolling = false;
×
116
        }
117

118
        this.wasClicking = flag;
×
119

120
        if (this.isScrolling) {
×
121
            this.currentScroll = ((float)(mouseY - y) - 7.5F) / ((float)(yMax - y) - 15.0F);
×
122
            this.currentScroll = Mth.clamp(this.currentScroll, 0.0F, 1.0F);
×
123
            scrollTo(this.currentScroll);
×
124
            return true;
×
125
        }
126

127
        return false;
×
128
    }
129

130
    public void drawGuiContainerBackgroundLayer(PoseStack matrixStack, float partialTicks, int mouseX, int mouseY) {
131
        int scrollX = x;
×
132
        int scrollMinY = y;
×
133
        int scrollMaxY = scrollMinY + height;
×
134
        RenderHelpers.bindTexture(SCROLLBUTTON);
×
135
        this.blit(
×
136
                matrixStack,
137
                scrollX,
138
                scrollMinY + (int)((float)(scrollMaxY - scrollMinY - SCROLL_BUTTON_HEIGHT - 2) * this.currentScroll),
139
                232 + (this.needsScrollBars() ? 0 : SCROLL_BUTTON_WIDTH),
×
140
                0,
141
                SCROLL_BUTTON_WIDTH,
142
                SCROLL_BUTTON_HEIGHT
143
        );
144
    }
×
145

146
    protected boolean needsScrollBars() {
147
        return getTotalRows() > getVisibleRows();
×
148
    }
149

150
    protected int getScrollStep() {
151
        return getTotalRows() - getVisibleRows();
×
152
    }
153

154
    public void scrollRelative(double step) {
155
        float scroll = (float)(this.currentScroll - step / getScrollStep());
×
156
        scroll = Mth.clamp(scroll, 0.0F, 1.0F);
×
157
        scrollTo(scroll);
×
158
    }
×
159

160
    public void scrollTo(float scroll) {
161
        scrollTo(scroll, true);
×
162
    }
×
163

164
    public void scrollTo(float scroll, boolean invokeCallback) {
165
        this.currentScroll = Math.max(0, scroll);
×
166
        if (invokeCallback && scrollCallback != null) {
×
167
            int firstRow = (int) ((double) (scroll * getScrollStep()) + 0.5D);
×
168
            scrollCallback.onScroll(firstRow);
×
169
        }
170
    }
×
171

172
    public void setFirstRow(int firstRow, boolean invokeCallback) {
173
        float scroll = ((float) firstRow) / getScrollStep();
×
174
        scroll = Mth.clamp(scroll, 0.0F, 1.0F);
×
175
        scrollTo(scroll, invokeCallback);
×
176
    }
×
177

178
    public int getTotalRows() {
179
        return totalRows;
×
180
    }
181

182
    public void setTotalRows(int totalRows) {
183
        this.totalRows = totalRows;
×
184
    }
×
185

186
    public int getVisibleRows() {
187
        return visibleRows;
×
188
    }
189

190
    public void setVisibleRows(int visibleRows) {
191
        this.visibleRows = visibleRows;
×
192
    }
×
193

194
    @Override
195
    public void updateNarration(NarrationElementOutput narrationElementOutput) {
196

197
    }
×
198

199
    public static interface IScrollCallback {
200

201
        public void onScroll(int firstRow);
202

203
    }
204
}
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