mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-18 11:29:35 +00:00
Create Recipe Widget; Recipe GUI: Infinity Scroll; Recipe GUI: Max Height Settings (#701)
Co-authored-by: Martin Robertz <[email protected]>
This commit is contained in:
1 parent
2ea9ae1eb9
commit
fe0c5d4e50
50 files changed
+4150
-2107
No files matched your search
@@ -0,0 +1,131 @@
|
||||
package codechicken.nei.scroll;
|
||||
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.Stack;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.ScaledResolution;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public class GuiHelper {
|
||||
|
||||
private static final Stack<int[]> scissorFrameStack = new Stack<>();
|
||||
|
||||
public static void useScissor(int x, int y, int width, int height, Runnable codeBlock) {
|
||||
pushScissorFrame(x, y, width, height);
|
||||
|
||||
try {
|
||||
codeBlock.run();
|
||||
} finally {
|
||||
popScissorFrame();
|
||||
}
|
||||
}
|
||||
|
||||
private static int[] peekFirstScissorOrFullScreen() {
|
||||
final int[] currentTopFrame = scissorFrameStack.isEmpty() ? null : scissorFrameStack.peek();
|
||||
if (currentTopFrame == null) {
|
||||
final Minecraft minecraft = Minecraft.getMinecraft();
|
||||
return new int[] { 0, 0, minecraft.displayWidth, minecraft.displayHeight };
|
||||
}
|
||||
return currentTopFrame;
|
||||
}
|
||||
|
||||
private static void pushScissorFrame(int x, int y, int width, int height) {
|
||||
int[] matrix = prepareMatrix(x, y, width, height);
|
||||
int[] parentScissor = peekFirstScissorOrFullScreen();
|
||||
int parentX = parentScissor[0];
|
||||
int parentY = parentScissor[1];
|
||||
int parentWidth = parentScissor[2];
|
||||
int parentHeight = parentScissor[3];
|
||||
boolean pushedFrame = false;
|
||||
|
||||
x = matrix[0];
|
||||
y = matrix[1];
|
||||
width = matrix[2];
|
||||
height = matrix[3];
|
||||
|
||||
if (x <= parentX + parentWidth && y <= parentY + parentHeight) {
|
||||
int newX = Math.max(x, parentX);
|
||||
int newY = Math.max(y, parentY);
|
||||
int newWidth = width - (newX - x);
|
||||
int newHeight = height - (newY - y);
|
||||
if (newWidth > 0 && newHeight > 0) {
|
||||
int maxWidth = parentWidth - (x - parentX);
|
||||
int maxHeight = parentHeight - (y - parentY);
|
||||
newWidth = Math.min(maxWidth, newWidth);
|
||||
newHeight = Math.min(maxHeight, newHeight);
|
||||
applyScissor(newX, newY, newWidth, newHeight);
|
||||
// finally, push applied scissor on top of scissor stack
|
||||
|
||||
if (scissorFrameStack.isEmpty()) {
|
||||
GL11.glEnable(GL11.GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
scissorFrameStack.push(new int[] { newX, newY, newWidth, newHeight });
|
||||
pushedFrame = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!pushedFrame) {
|
||||
|
||||
if (scissorFrameStack.isEmpty()) {
|
||||
GL11.glEnable(GL11.GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
scissorFrameStack.push(new int[] { parentX, parentY, parentWidth, parentHeight });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void popScissorFrame() {
|
||||
scissorFrameStack.pop();
|
||||
int[] parentScissor = peekFirstScissorOrFullScreen();
|
||||
int parentX = parentScissor[0];
|
||||
int parentY = parentScissor[1];
|
||||
int parentWidth = parentScissor[2];
|
||||
int parentHeight = parentScissor[3];
|
||||
applyScissor(parentX, parentY, parentWidth, parentHeight);
|
||||
|
||||
if (scissorFrameStack.isEmpty()) {
|
||||
GL11.glDisable(GL11.GL_SCISSOR_TEST);
|
||||
}
|
||||
}
|
||||
|
||||
// applies scissor with gui-space coordinates and sizes
|
||||
private static void applyScissor(int x, int y, int w, int h) {
|
||||
// translate upper-left to bottom-left
|
||||
final Minecraft mc = Minecraft.getMinecraft();
|
||||
final ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
|
||||
final int scaleFactor = res.getScaleFactor();
|
||||
final int translatedY = mc.displayHeight - (y + h) * scaleFactor;
|
||||
|
||||
GL11.glScissor(x * scaleFactor, translatedY, w * scaleFactor, h * scaleFactor);
|
||||
}
|
||||
|
||||
private static int[] prepareMatrix(int x, int y, int w, int h) {
|
||||
// Get the current translation component to support the Gui being drawn at any position on the screen
|
||||
final FloatBuffer matBuf = BufferUtils.createFloatBuffer(16);
|
||||
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, matBuf);
|
||||
final float xScale = matBuf.get(0);
|
||||
final float yScale = matBuf.get(5);
|
||||
final int xTranslate = (int) (matBuf.get(12) / xScale);
|
||||
final int yTranslate = (int) (matBuf.get(13) / yScale);
|
||||
|
||||
x += xTranslate;
|
||||
y += yTranslate;
|
||||
|
||||
x *= xScale;
|
||||
y *= yScale;
|
||||
|
||||
w *= xScale;
|
||||
h *= yScale;
|
||||
|
||||
return new int[] { x, y, w, h };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,398 @@
|
||||
package codechicken.nei.scroll;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.nei.NEIClientUtils;
|
||||
import codechicken.nei.drawable.DrawableBuilder;
|
||||
import codechicken.nei.drawable.DrawableResource;
|
||||
|
||||
public class ScrollBar {
|
||||
|
||||
/** The minimum length of the scrollbar cursor. */
|
||||
private static final int MINIMUM_THUMB_SIZE = 6;
|
||||
|
||||
private static final Color BACKGROUND_COLOR = new Color(0x90B1D4E0);
|
||||
private static final Color THUMB_COLOR = new Color(0xF0145DA0);
|
||||
private static final Color THUMB_SELECTED_COLOR = new Color(0xF02E8BC0);
|
||||
|
||||
public enum ScrollType {
|
||||
HORIZONTAL,
|
||||
VERTICAL
|
||||
}
|
||||
|
||||
public enum OverflowType {
|
||||
NONE,
|
||||
AUTO,
|
||||
SCROLL,
|
||||
OVERLAY
|
||||
}
|
||||
|
||||
public enum ScrollPlace {
|
||||
INSIDE,
|
||||
OUTSIDE
|
||||
}
|
||||
|
||||
private ScrollType scrollType = ScrollType.VERTICAL;
|
||||
private OverflowType overflowType = OverflowType.SCROLL;
|
||||
private ScrollPlace scrollPlace = ScrollPlace.INSIDE;
|
||||
private float handleClickOffset = -1;
|
||||
|
||||
private int scrollbarSize = 6;
|
||||
private int thumbPadding = 0;
|
||||
private int margin = 0;
|
||||
|
||||
private DrawableResource thumbTexture;
|
||||
private int thumbSliceStart;
|
||||
private int thumbSliceEnd;
|
||||
|
||||
private DrawableResource trackTexture;
|
||||
private int trackSliceStart;
|
||||
private int trackSliceEnd;
|
||||
|
||||
public static ScrollBar defaultScrollBar() {
|
||||
return new ScrollBar();
|
||||
}
|
||||
|
||||
public static ScrollBar defaultVerticalBar() {
|
||||
return new ScrollBar().setScrollbarSize(8)
|
||||
.setTrackTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 29, 95, 12, 11).build(), 3, 4)
|
||||
.setThumbTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 21, 96, 8, 9).build(), 3, 2)
|
||||
.setThumbPadding(1);
|
||||
}
|
||||
|
||||
public static ScrollBar defaultHorizontalBar() {
|
||||
return new ScrollBar().setScrollbarSize(8).setOverflowType(OverflowType.SCROLL)
|
||||
.setScrollPlace(ScrollPlace.INSIDE)
|
||||
.setTrackTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 0, 97, 11, 8).build(), 3, 4)
|
||||
.setThumbTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 12, 97, 9, 7).build(), 3, 2)
|
||||
.setThumbPadding(1);
|
||||
}
|
||||
|
||||
public ScrollBar setScrollbarSize(int scrollbarSize) {
|
||||
this.scrollbarSize = scrollbarSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ScrollBar setThumbPadding(int thumbPadding) {
|
||||
this.thumbPadding = thumbPadding;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ScrollBar setMargin(int margin) {
|
||||
this.margin = margin;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getMargin() {
|
||||
return this.margin;
|
||||
}
|
||||
|
||||
public int getScrollbarSize() {
|
||||
return this.scrollbarSize;
|
||||
}
|
||||
|
||||
public ScrollBar setThumbTexture(DrawableResource texture, int sliceStart, int sliceEnd) {
|
||||
this.thumbTexture = texture;
|
||||
this.thumbSliceStart = sliceStart;
|
||||
this.thumbSliceEnd = sliceEnd;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ScrollBar setTrackTexture(DrawableResource texture, int sliceStart, int sliceEnd) {
|
||||
this.trackTexture = texture;
|
||||
this.trackSliceStart = sliceStart;
|
||||
this.trackSliceEnd = sliceEnd;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ScrollBar setOverflowType(OverflowType overflowType) {
|
||||
this.overflowType = overflowType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OverflowType getOverflowType() {
|
||||
return this.overflowType;
|
||||
}
|
||||
|
||||
public ScrollBar setScrollPlace(ScrollPlace scrollPlace) {
|
||||
this.scrollPlace = scrollPlace;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ScrollPlace getScrollPlace() {
|
||||
return this.scrollPlace;
|
||||
}
|
||||
|
||||
public ScrollBar setScrollType(ScrollType scrollType) {
|
||||
this.scrollType = scrollType;
|
||||
return this;
|
||||
}
|
||||
|
||||
private int getActualSize(ScrollContainer container) {
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
return container.getActualWidth();
|
||||
} else {
|
||||
return container.getActualHeight();
|
||||
}
|
||||
}
|
||||
|
||||
public int getVisibleSize(ScrollContainer container) {
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
return container.getVisibleWidth();
|
||||
} else {
|
||||
return container.getVisibleHeight();
|
||||
}
|
||||
}
|
||||
|
||||
private int getMaxThumbSize(ScrollContainer container) {
|
||||
return getMaxTrackSize(container) - this.thumbPadding * 2;
|
||||
}
|
||||
|
||||
private int getMaxTrackSize(ScrollContainer container) {
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
int visibleSize = container.w;
|
||||
|
||||
if (container.canScrollVertical() && container.getVerticalScroll().getScrollPlace() == ScrollPlace.INSIDE) {
|
||||
visibleSize -= container.getVerticalScroll().getScrollbarSize();
|
||||
}
|
||||
|
||||
return visibleSize;
|
||||
} else {
|
||||
int visibleSize = container.h;
|
||||
|
||||
if (container.canScrollHorizontal()
|
||||
&& container.getHorizontalScroll().getScrollPlace() == ScrollPlace.INSIDE) {
|
||||
visibleSize -= container.getHorizontalScroll().getScrollbarSize();
|
||||
}
|
||||
|
||||
return visibleSize;
|
||||
}
|
||||
}
|
||||
|
||||
private int getMinThumbSize() {
|
||||
|
||||
if (this.thumbTexture != null) {
|
||||
return this.scrollType == ScrollType.HORIZONTAL ? this.thumbTexture.getWidth()
|
||||
: this.thumbTexture.getHeight();
|
||||
}
|
||||
|
||||
return MINIMUM_THUMB_SIZE;
|
||||
}
|
||||
|
||||
public float getScrollOffset(ScrollContainer container) {
|
||||
final float thumbSize = getMaxThumbSize(container) - calculateMainAxisSize(container);
|
||||
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
return container.getHorizontalScrollOffset()
|
||||
/ (float) (container.getActualWidth() - container.getVisibleWidth())
|
||||
* thumbSize;
|
||||
} else {
|
||||
return container.getVerticalScrollOffset()
|
||||
/ (float) (container.getActualHeight() - container.getVisibleHeight())
|
||||
* thumbSize;
|
||||
}
|
||||
}
|
||||
|
||||
public float calculateMainAxisSize(ScrollContainer container) {
|
||||
final int visibleSize = getVisibleSize(container);
|
||||
final int actualSize = getActualSize(container);
|
||||
final int trackSize = getMaxThumbSize(container);
|
||||
|
||||
return Math.max(Math.min(trackSize, (trackSize / (float) actualSize) * visibleSize), getMinThumbSize());
|
||||
}
|
||||
|
||||
public Rectangle4i trackBounds(ScrollContainer container) {
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
return new Rectangle4i(
|
||||
container.x,
|
||||
container.y + container.h
|
||||
+ this.margin
|
||||
- (this.scrollPlace == ScrollPlace.INSIDE ? scrollbarSize : 0),
|
||||
getMaxTrackSize(container),
|
||||
scrollbarSize);
|
||||
} else {
|
||||
return new Rectangle4i(
|
||||
container.x + container.w
|
||||
+ this.margin
|
||||
- (this.scrollPlace == ScrollPlace.INSIDE ? scrollbarSize : 0),
|
||||
container.y,
|
||||
scrollbarSize,
|
||||
getMaxTrackSize(container));
|
||||
}
|
||||
}
|
||||
|
||||
public Rectangle4i thumbBounds(ScrollContainer container) {
|
||||
final int mainAxisSize = (int) calculateMainAxisSize(container);
|
||||
final int offset = (int) Math.ceil(getScrollOffset(container));
|
||||
final Rectangle4i bounds = trackBounds(container);
|
||||
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
return new Rectangle4i(bounds.x + this.thumbPadding + offset, bounds.y, mainAxisSize, bounds.h);
|
||||
} else {
|
||||
return new Rectangle4i(bounds.x, bounds.y + this.thumbPadding + offset, bounds.w, mainAxisSize);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setScrollOffset(ScrollContainer container, int offset) {
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
return container.setHorizontalScrollOffset(offset);
|
||||
} else {
|
||||
return container.setVerticalScrollOffset(offset);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onClick(ScrollContainer container, int mx, int my, int button) {
|
||||
final Rectangle4i bounds = trackBounds(container);
|
||||
|
||||
if (bounds.contains(mx, my)) {
|
||||
final float mainAxisSize = calculateMainAxisSize(container);
|
||||
final float thumbSize = getMaxThumbSize(container) - mainAxisSize;
|
||||
final Rectangle4i thumbBounds = thumbBounds(container);
|
||||
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
final int relativeX = mx - bounds.x - this.thumbPadding;
|
||||
|
||||
if (mx < thumbBounds.x || mx > thumbBounds.x + thumbBounds.w) {
|
||||
this.handleClickOffset = mainAxisSize / 2;
|
||||
} else {
|
||||
this.handleClickOffset = mx - thumbBounds.x;
|
||||
}
|
||||
|
||||
final float newOffset = ((relativeX - this.handleClickOffset)
|
||||
* (container.getActualWidth() - container.getVisibleWidth())) / thumbSize;
|
||||
container.setHorizontalScrollOffset((int) newOffset);
|
||||
} else {
|
||||
final int relativeY = my - bounds.y - this.thumbPadding;
|
||||
|
||||
if (my < thumbBounds.y || my > thumbBounds.y + thumbBounds.h) {
|
||||
this.handleClickOffset = mainAxisSize / 2f;
|
||||
} else {
|
||||
this.handleClickOffset = my - thumbBounds.y;
|
||||
}
|
||||
|
||||
final float newOffset = ((relativeY - this.handleClickOffset)
|
||||
* (container.getActualHeight() - container.getVisibleHeight())) / thumbSize;
|
||||
|
||||
container.setVerticalScrollOffset((int) newOffset);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onMouseDragged(ScrollContainer container, int mx, int my, int buttonId, long heldTime) {
|
||||
|
||||
if (this.handleClickOffset >= 0) {
|
||||
final Rectangle4i bounds = trackBounds(container);
|
||||
final float mainAxisSize = calculateMainAxisSize(container);
|
||||
final float thumbSize = getMaxThumbSize(container) - mainAxisSize;
|
||||
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
final int relativeX = mx - bounds.x - this.thumbPadding;
|
||||
final float newOffset = ((relativeX - this.handleClickOffset)
|
||||
* (container.getActualWidth() - container.getVisibleWidth())) / thumbSize;
|
||||
container.setHorizontalScrollOffset((int) newOffset);
|
||||
} else {
|
||||
int relativeY = my - bounds.y - this.thumbPadding;
|
||||
final float newOffset = ((relativeY - this.handleClickOffset)
|
||||
* (container.getActualHeight() - container.getVisibleHeight())) / thumbSize;
|
||||
|
||||
container.setVerticalScrollOffset((int) newOffset);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onClickReleased(ScrollContainer container, int mx, int my, int buttonId) {
|
||||
|
||||
if (this.handleClickOffset >= 0) {
|
||||
this.handleClickOffset = -1;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isMouseDragged() {
|
||||
return this.handleClickOffset >= 0;
|
||||
}
|
||||
|
||||
public void draw(ScrollContainer container, int mx, int my, float opacity) {
|
||||
|
||||
if (opacity <= 0 && this.overflowType == OverflowType.OVERLAY) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.overflowType != OverflowType.OVERLAY) {
|
||||
opacity = 1;
|
||||
}
|
||||
|
||||
final Rectangle4i trackBounds = trackBounds(container);
|
||||
final Rectangle4i thumbBounds = thumbBounds(container);
|
||||
final boolean selected = isMouseDragged() || thumbBounds.contains(mx, my);
|
||||
final boolean disabled = getVisibleSize(container) >= getActualSize(container);
|
||||
|
||||
if (this.trackTexture != null) {
|
||||
drawTexture(this.trackTexture, this.trackSliceStart, this.trackSliceEnd, trackBounds, false, opacity);
|
||||
} else {
|
||||
drawTrackRect(container, trackBounds, opacity);
|
||||
}
|
||||
|
||||
if (this.thumbTexture != null) {
|
||||
drawTexture(this.thumbTexture, this.thumbSliceStart, this.thumbSliceEnd, thumbBounds, disabled, opacity);
|
||||
} else {
|
||||
drawThumbRect(container, thumbBounds, selected, opacity);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawTrackRect(ScrollContainer container, Rectangle4i bounds, float opacity) {
|
||||
final float trackSize = scrollbarSize / 3f;
|
||||
final Color trackColor = new Color(
|
||||
BACKGROUND_COLOR.getRed(),
|
||||
BACKGROUND_COLOR.getGreen(),
|
||||
BACKGROUND_COLOR.getBlue(),
|
||||
(int) (BACKGROUND_COLOR.getAlpha() * opacity));
|
||||
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
NEIClientUtils.drawRect(bounds.x, bounds.y + trackSize, bounds.w, trackSize, trackColor);
|
||||
} else {
|
||||
NEIClientUtils.drawRect(bounds.x + trackSize, bounds.y, trackSize, bounds.h, trackColor);
|
||||
}
|
||||
}
|
||||
|
||||
private void drawThumbRect(ScrollContainer container, Rectangle4i bounds, boolean selected, float opacity) {
|
||||
final Color color = selected ? THUMB_SELECTED_COLOR : THUMB_COLOR;
|
||||
final Color thumbColor = new Color(
|
||||
color.getRed(),
|
||||
color.getGreen(),
|
||||
color.getBlue(),
|
||||
(int) (color.getAlpha() * opacity));
|
||||
|
||||
NEIClientUtils.drawRect(bounds.x, bounds.y, bounds.w, bounds.h, thumbColor);
|
||||
}
|
||||
|
||||
private void drawTexture(DrawableResource texture, int sliceStart, int sliceEnd, Rectangle4i bounds,
|
||||
boolean disabled, float opacity) {
|
||||
final float c = disabled ? 0.65f : 1f;
|
||||
|
||||
GL11.glColor4f(c, c, c, opacity);
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
if (this.scrollType == ScrollType.HORIZONTAL) {
|
||||
texture.draw(bounds.x, bounds.y, bounds.w, texture.getHeight(), sliceStart, sliceEnd, 0, 0);
|
||||
} else {
|
||||
texture.draw(bounds.x, bounds.y, texture.getWidth(), bounds.h, 0, 0, sliceStart, sliceEnd);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,418 @@
|
||||
package codechicken.nei.scroll;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import codechicken.lib.gui.GuiDraw;
|
||||
import codechicken.lib.vec.Rectangle4i;
|
||||
import codechicken.nei.NEIClientUtils;
|
||||
import codechicken.nei.Widget;
|
||||
import codechicken.nei.WidgetContainer;
|
||||
import codechicken.nei.scroll.ScrollBar.OverflowType;
|
||||
import codechicken.nei.scroll.ScrollBar.ScrollPlace;
|
||||
import codechicken.nei.scroll.ScrollBar.ScrollType;
|
||||
|
||||
public class ScrollContainer extends WidgetContainer {
|
||||
|
||||
/** The mouse scroll wheel scroll speed */
|
||||
protected static final int MOUSE_SCROLL_SPEED = 16;
|
||||
|
||||
/** The keyboard arrow keys scroll speed */
|
||||
protected static final int KEYBOARD_SCROLL_SPEED = 36;
|
||||
|
||||
/** The scrollbar will fade away over this many ticks */
|
||||
protected static final int FADE_TICKS = 24;
|
||||
|
||||
protected int xScroll = 0;
|
||||
protected int yScroll = 0;
|
||||
|
||||
protected int actualWidth = 0;
|
||||
protected int actualHeight = 0;
|
||||
|
||||
protected int marginInline = 0;
|
||||
protected int marginBlock = 0;
|
||||
|
||||
protected final Map<Widget, Point> relativePostion = new HashMap<>();
|
||||
protected ScrollBar horizontalScrollBar;
|
||||
protected ScrollBar verticalScrollBar;
|
||||
protected int fade = 0;
|
||||
|
||||
public Rectangle4i boundsOutside() {
|
||||
int width = this.w;
|
||||
int height = this.h;
|
||||
|
||||
if (canScrollVertical() && (this.verticalScrollBar.getOverflowType() == OverflowType.SCROLL
|
||||
|| this.verticalScrollBar.getOverflowType() == OverflowType.AUTO)) {
|
||||
width += this.verticalScrollBar.getMargin();
|
||||
|
||||
if (this.verticalScrollBar.getScrollPlace() == ScrollPlace.OUTSIDE) {
|
||||
width += this.verticalScrollBar.getScrollbarSize();
|
||||
}
|
||||
}
|
||||
|
||||
if (canScrollHorizontal() && (this.horizontalScrollBar.getOverflowType() == OverflowType.SCROLL
|
||||
|| this.horizontalScrollBar.getOverflowType() == OverflowType.AUTO)) {
|
||||
height += this.horizontalScrollBar.getMargin();
|
||||
|
||||
if (this.horizontalScrollBar.getScrollPlace() == ScrollPlace.OUTSIDE) {
|
||||
height += this.horizontalScrollBar.getScrollbarSize();
|
||||
}
|
||||
}
|
||||
|
||||
return new Rectangle4i(x, y, width, height);
|
||||
}
|
||||
|
||||
public Rectangle4i boundsInside() {
|
||||
return new Rectangle4i(x, y, getVisibleWidth(), getVisibleHeight());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
super.clear();
|
||||
this.relativePostion.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeIf(Predicate<Widget> filter) {
|
||||
this.relativePostion.keySet().removeIf(filter);
|
||||
return super.removeIf(filter);
|
||||
}
|
||||
|
||||
public void addWidget(Widget widget) {
|
||||
super.addWidget(widget);
|
||||
this.relativePostion.put(widget, new Point(widget.x, widget.y));
|
||||
}
|
||||
|
||||
public Point getWidgetPosition(Widget widget) {
|
||||
return this.relativePostion.get(widget);
|
||||
}
|
||||
|
||||
public void setMarginInline(int marginInline) {
|
||||
this.marginInline = marginInline;
|
||||
}
|
||||
|
||||
public void setMarginBlock(int marginBlock) {
|
||||
this.marginBlock = marginBlock;
|
||||
}
|
||||
|
||||
public int getMarginInline() {
|
||||
return this.marginInline;
|
||||
}
|
||||
|
||||
public int getMarginBlock() {
|
||||
return this.marginBlock;
|
||||
}
|
||||
|
||||
public int getVisibleWidth() {
|
||||
|
||||
if (canScrollVertical() && this.verticalScrollBar.getScrollPlace() == ScrollPlace.INSIDE
|
||||
&& (this.verticalScrollBar.getOverflowType() == OverflowType.SCROLL
|
||||
|| this.verticalScrollBar.getOverflowType() == OverflowType.AUTO)) {
|
||||
return this.w - this.verticalScrollBar.getScrollbarSize();
|
||||
}
|
||||
|
||||
return this.w;
|
||||
}
|
||||
|
||||
public int getActualWidth() {
|
||||
return this.actualWidth;
|
||||
}
|
||||
|
||||
public boolean setHorizontalScrollOffset(int offset) {
|
||||
|
||||
if (!canScrollHorizontal() && this.w < getActualWidth()) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
final int scroll = this.xScroll;
|
||||
this.xScroll = Math.max(0, Math.min(getActualWidth() - getVisibleWidth(), offset));
|
||||
|
||||
if (scroll != this.xScroll) {
|
||||
|
||||
for (Widget widget : this.widgets) {
|
||||
widget.x += scroll - this.xScroll;
|
||||
}
|
||||
|
||||
this.fade = FADE_TICKS;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getHorizontalScrollOffset() {
|
||||
return this.xScroll;
|
||||
}
|
||||
|
||||
public boolean canScrollHorizontal() {
|
||||
return this.horizontalScrollBar != null
|
||||
&& (this.horizontalScrollBar.getOverflowType() != OverflowType.NONE && this.w < getActualWidth()
|
||||
|| this.horizontalScrollBar.getOverflowType() == OverflowType.SCROLL);
|
||||
}
|
||||
|
||||
public ScrollBar getHorizontalScroll() {
|
||||
return this.horizontalScrollBar;
|
||||
}
|
||||
|
||||
public ScrollContainer setHorizontalScroll() {
|
||||
return setHorizontalScroll(ScrollBar.defaultScrollBar());
|
||||
}
|
||||
|
||||
public ScrollContainer setHorizontalScroll(ScrollBar scrollBar) {
|
||||
this.horizontalScrollBar = scrollBar;
|
||||
|
||||
if (this.horizontalScrollBar != null) {
|
||||
this.horizontalScrollBar.setScrollType(ScrollType.HORIZONTAL);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getVisibleHeight() {
|
||||
|
||||
if (canScrollHorizontal() && this.horizontalScrollBar.getScrollPlace() == ScrollPlace.INSIDE
|
||||
&& (this.horizontalScrollBar.getOverflowType() == OverflowType.SCROLL
|
||||
|| this.horizontalScrollBar.getOverflowType() == OverflowType.AUTO)) {
|
||||
return this.h - this.horizontalScrollBar.getScrollbarSize();
|
||||
}
|
||||
|
||||
return this.h;
|
||||
}
|
||||
|
||||
public int getActualHeight() {
|
||||
return this.actualHeight;
|
||||
}
|
||||
|
||||
public boolean setVerticalScrollOffset(int offset) {
|
||||
|
||||
if (!canScrollVertical() && this.h < getActualHeight()) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
final int scroll = this.yScroll;
|
||||
this.yScroll = Math.max(0, Math.min(getActualHeight() - getVisibleHeight(), offset));
|
||||
|
||||
if (scroll != this.yScroll) {
|
||||
|
||||
for (Widget widget : this.widgets) {
|
||||
widget.y += scroll - this.yScroll;
|
||||
}
|
||||
|
||||
this.fade = FADE_TICKS;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getVerticalScrollOffset() {
|
||||
return this.yScroll;
|
||||
}
|
||||
|
||||
public boolean canScrollVertical() {
|
||||
return this.verticalScrollBar != null
|
||||
&& (this.verticalScrollBar.getOverflowType() != OverflowType.NONE && this.h < getActualHeight()
|
||||
|| this.verticalScrollBar.getOverflowType() == OverflowType.SCROLL);
|
||||
}
|
||||
|
||||
public ScrollBar getVerticalScroll() {
|
||||
return this.verticalScrollBar;
|
||||
}
|
||||
|
||||
public ScrollContainer setVerticalScroll() {
|
||||
return setVerticalScroll(ScrollBar.defaultScrollBar());
|
||||
}
|
||||
|
||||
public ScrollContainer setVerticalScroll(ScrollBar scrollBar) {
|
||||
this.verticalScrollBar = scrollBar;
|
||||
|
||||
if (this.verticalScrollBar != null) {
|
||||
this.verticalScrollBar.setScrollType(ScrollType.VERTICAL);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int mousex, int mousey) {
|
||||
final boolean canScrollVertical = canScrollVertical();
|
||||
final boolean canScrollHorizontal = canScrollHorizontal();
|
||||
|
||||
if (canScrollVertical || canScrollHorizontal) {
|
||||
GuiHelper.useScissor(
|
||||
this.x,
|
||||
this.y,
|
||||
getVisibleWidth(),
|
||||
getVisibleHeight(),
|
||||
() -> drawContent(mousex, mousey));
|
||||
|
||||
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
|
||||
|
||||
if (canScrollVertical) {
|
||||
this.verticalScrollBar.draw(this, mousex, mousey, getScrollBarOpacity());
|
||||
}
|
||||
|
||||
if (canScrollHorizontal) {
|
||||
this.horizontalScrollBar.draw(this, mousex, mousey, getScrollBarOpacity());
|
||||
}
|
||||
|
||||
GL11.glPopAttrib();
|
||||
} else {
|
||||
drawContent(mousex, mousey);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void drawContent(int mousex, int mousey) {
|
||||
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
|
||||
GL11.glPushMatrix();
|
||||
GL11.glColor4f(1, 1, 1, 1);
|
||||
|
||||
super.draw(mousex, mousey);
|
||||
|
||||
GL11.glPopMatrix();
|
||||
GL11.glPopAttrib();
|
||||
}
|
||||
|
||||
private float getScrollBarOpacity() {
|
||||
return Math.min(2f * this.fade, FADE_TICKS) / FADE_TICKS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
this.actualHeight = 0;
|
||||
this.actualWidth = 0;
|
||||
|
||||
for (Map.Entry<Widget, Point> entry : this.relativePostion.entrySet()) {
|
||||
entry.getKey().x = entry.getValue().x;
|
||||
entry.getKey().y = entry.getValue().y;
|
||||
}
|
||||
|
||||
super.update();
|
||||
|
||||
for (Widget widget : this.widgets) {
|
||||
this.actualWidth = Math.max(this.actualWidth, widget.x + widget.w + this.marginInline * 2);
|
||||
this.actualHeight = Math.max(this.actualHeight, widget.y + widget.h + this.marginBlock * 2);
|
||||
|
||||
widget.x += this.marginInline + this.x - this.xScroll;
|
||||
widget.y += this.marginBlock + this.y - this.yScroll;
|
||||
}
|
||||
|
||||
if (this.fade > 0) {
|
||||
final Point mouse = GuiDraw.getMousePosition();
|
||||
|
||||
if (canScrollVertical() && (this.verticalScrollBar.isMouseDragged()
|
||||
|| this.verticalScrollBar.trackBounds(this).contains(mouse.x, mouse.y))) {
|
||||
this.fade = FADE_TICKS;
|
||||
} else if (canScrollHorizontal() && (this.horizontalScrollBar.isMouseDragged()
|
||||
|| this.horizontalScrollBar.trackBounds(this).contains(mouse.x, mouse.y))) {
|
||||
this.fade = FADE_TICKS;
|
||||
} else {
|
||||
this.fade--;
|
||||
}
|
||||
}
|
||||
|
||||
setHorizontalScrollOffset(this.xScroll);
|
||||
setVerticalScrollOffset(this.yScroll);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMouseWheel(int direction, int mousex, int mousey) {
|
||||
|
||||
if (super.onMouseWheel(direction, mousex, mousey)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (boundsOutside().contains(mousex, mousey)) {
|
||||
|
||||
if (canScrollHorizontal() && (NEIClientUtils.shiftKey()
|
||||
|| this.horizontalScrollBar.trackBounds(this).contains(mousex, mousey))) {
|
||||
this.horizontalScrollBar.setScrollOffset(this, this.xScroll - direction * MOUSE_SCROLL_SPEED);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (canScrollVertical()) {
|
||||
this.verticalScrollBar.setScrollOffset(this, this.yScroll - direction * MOUSE_SCROLL_SPEED);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleClick(int mousex, int mousey, int button) {
|
||||
|
||||
if (canScrollVertical() && this.verticalScrollBar.onClick(this, mousex, mousey, button)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (canScrollHorizontal() && this.horizontalScrollBar.onClick(this, mousex, mousey, button)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (super.handleClick(mousex, mousey, button)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(int mousex, int mousey, int button, long heldTime) {
|
||||
|
||||
if (canScrollVertical() && this.verticalScrollBar.onMouseDragged(this, mousex, mousey, button, heldTime)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (canScrollHorizontal() && this.horizontalScrollBar.onMouseDragged(this, mousex, mousey, button, heldTime)) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.mouseDragged(mousex, mousey, button, heldTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseUp(int mousex, int mousey, int button) {
|
||||
|
||||
if (canScrollVertical()) {
|
||||
this.verticalScrollBar.onClickReleased(this, mousex, mousey, button);
|
||||
}
|
||||
|
||||
if (canScrollHorizontal()) {
|
||||
this.horizontalScrollBar.onClickReleased(this, mousex, mousey, button);
|
||||
}
|
||||
|
||||
super.mouseUp(mousex, mousey, button);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handleKeyPress(int keyCode, char keyChar) {
|
||||
|
||||
if (this.isFocused()) {
|
||||
switch (keyCode) {
|
||||
case Keyboard.KEY_UP:
|
||||
return canScrollVertical()
|
||||
&& this.verticalScrollBar.setScrollOffset(this, this.yScroll - KEYBOARD_SCROLL_SPEED);
|
||||
case Keyboard.KEY_DOWN:
|
||||
return canScrollVertical()
|
||||
&& this.verticalScrollBar.setScrollOffset(this, this.yScroll + KEYBOARD_SCROLL_SPEED);
|
||||
case Keyboard.KEY_LEFT:
|
||||
return canScrollHorizontal()
|
||||
&& this.horizontalScrollBar.setScrollOffset(this, this.xScroll - KEYBOARD_SCROLL_SPEED);
|
||||
case Keyboard.KEY_RIGHT:
|
||||
return canScrollHorizontal()
|
||||
&& this.horizontalScrollBar.setScrollOffset(this, this.xScroll + KEYBOARD_SCROLL_SPEED);
|
||||
}
|
||||
}
|
||||
|
||||
return super.handleKeyPress(keyCode, keyChar);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user