Skip to content

Commit

Permalink
Fix Scrollcontainer and add clipping to it to prevent overlaps.
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegratedQuantum committed Dec 12, 2021
1 parent d922766 commit 67cd3ad
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 59 deletions.
27 changes: 14 additions & 13 deletions src/cubyz/gui/UISystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ public boolean doesGUIPauseGame() {
return gui == null ? false : gui.doesPauseGame();
}

public void updateGUIScale() {
int guiScale = Math.min(Window.getWidth()/480, Window.getHeight()/270);
guiScale = Math.max(1, guiScale);
ClientSettings.GUI_SCALE = guiScale;
if(gui != null)
gui.updateGUIScale();
for(MenuGUI gui : overlays) {
gui.updateGUIScale();
}
for(MenuGUI gui : menuQueue) {
gui.updateGUIScale();
}
}

public void render() {
if(Keyboard.isKeyPressed(GLFW.GLFW_KEY_F1)) {
Keyboard.setKeyPressed(GLFW.GLFW_KEY_F1, false);
Expand All @@ -133,19 +147,6 @@ public void render() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
int guiScale = Math.min(Window.getWidth()/480, Window.getHeight()/270);
guiScale = Math.max(1, guiScale);
if(guiScale != ClientSettings.GUI_SCALE) {
ClientSettings.GUI_SCALE = guiScale;
if(gui != null)
gui.updateGUIScale();
for(MenuGUI gui : overlays) {
gui.updateGUIScale();
}
for(MenuGUI gui : menuQueue) {
gui.updateGUIScale();
}
}
transitionTime += System.currentTimeMillis() - lastAnimTime;
lastAnimTime = System.currentTimeMillis();
Graphics.setGlobalAlphaMultiplier(1);
Expand Down
20 changes: 12 additions & 8 deletions src/cubyz/gui/components/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,25 @@ public boolean isInside(Vector2d vec) {
return isInside((int) vec.x, (int) vec.y);
}

public void render() {
// Calculate coordinates in the window:
public final void render() {
renderInContainer(0, 0, Window.getWidth(), Window.getHeight());
}

public final void renderInContainer(int containerX, int containerY, int width, int height) {
// Calculate coordinates in the container:
if((align & ALIGN_LEFT) != 0) {
lastRenderX = x;
lastRenderX = x + containerX;
} else if((align & ALIGN_RIGHT) != 0) {
lastRenderX = Window.getWidth() - x;
lastRenderX = width - x + containerX;
} else {
lastRenderX = Window.getWidth()/2 + x;
lastRenderX = width/2 + x + containerX;
}
if((align & ALIGN_TOP) != 0) {
lastRenderY = y;
lastRenderY = y + containerY;
} else if((align & ALIGN_BOTTOM) != 0) {
lastRenderY = Window.getHeight() - y;
lastRenderY = height - y + containerY;
} else {
lastRenderY = Window.getHeight()/2 + y;
lastRenderY = height/2 + y + containerY;
}
// Call the subclass render function:
render(lastRenderX, lastRenderY);
Expand Down
11 changes: 9 additions & 2 deletions src/cubyz/gui/components/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.util.ArrayList;

import org.joml.Vector4i;

import cubyz.rendering.Graphics;
import cubyz.rendering.Window;

/**
* A Component that contains other Components.
*/
Expand All @@ -28,10 +33,12 @@ public void remove(int index) {
}

@Override
public void render() {
public void render(int x, int y) {
Vector4i oldClip = Graphics.setClip(new Vector4i(x, Window.getHeight() - y - height, width, height));
for (Component child : childrens) {
child.render();
child.renderInContainer(x, y, width, height);
}
Graphics.restoreClip(oldClip);
}

}
12 changes: 8 additions & 4 deletions src/cubyz/gui/components/ScrollingContainer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cubyz.gui.components;

import org.joml.Vector4i;

import cubyz.gui.input.Mouse;
import cubyz.rendering.Graphics;
import cubyz.rendering.Window;

public class ScrollingContainer extends Container {

Expand All @@ -13,14 +16,14 @@ public class ScrollingContainer extends Container {

@Override
public void render(int x, int y) {
Vector4i oldClip = Graphics.setClip(new Vector4i(x, Window.getHeight() - y - height, width, height));
maxY = 0;
for (Component child : childrens) {
maxY = Math.max(maxY, child.getY()+child.getHeight());
child.setY(child.getY() - scrollY);
child.render();
child.setY(child.getY() + scrollY);
child.renderInContainer(x, y - scrollY, width, height);
}
if (maxY > height) {
maxY -= height;
if (maxY > 0) {
Graphics.setColor(0x000000);
Graphics.fillRect(x + width - scrollBarWidth, y, scrollBarWidth, height);
Graphics.setColor(0xffffff);
Expand All @@ -46,6 +49,7 @@ public void render(int x, int y) {
scrollY = 0;
}
scrollY = Math.min(maxY, scrollY);
Graphics.restoreClip(oldClip);
}

}
24 changes: 13 additions & 11 deletions src/cubyz/gui/menu/SaveSelectorGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import cubyz.gui.MenuGUI;
import cubyz.gui.components.Button;
import cubyz.gui.components.Component;
import cubyz.gui.components.ScrollingContainer;
import cubyz.rendering.VisibleChunk;
import cubyz.rendering.Window;
import cubyz.utils.translate.ContextualTextKey;
import cubyz.utils.translate.TextKey;
import cubyz.world.ServerWorld;
Expand All @@ -32,9 +34,13 @@ public class SaveSelectorGUI extends MenuGUI {
private Button[] deleteButtons;
private Button createNew;
private Button back;

private ScrollingContainer container;

@Override
public void init() {
container = new ScrollingContainer();

// Find all save folders that currently exist:
File folder = new File("saves");
if (!folder.exists()) {
Expand All @@ -54,6 +60,7 @@ public void init() {
GameLauncher.logic.loadWorld(world);
});
saveButtons[i] = b;
container.add(b);
b = new Button(TextKey.createTextKey("gui.cubyz.saves.delete"));
int index = i;
Path path = listOfFiles[i].toPath();
Expand Down Expand Up @@ -97,6 +104,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce
}
});
deleteButtons[i] = b;
container.add(b);
}
createNew = new Button(TextKey.createTextKey("gui.cubyz.saves.create"));
createNew.setOnAction(() -> {
Expand All @@ -112,11 +120,12 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce

@Override
public void updateGUIScale() {
int y = 10;
container.setBounds(10 * GUI_SCALE, 10 * GUI_SCALE, Window.getWidth() - 20, Window.getHeight() - 70 * GUI_SCALE, Component.ALIGN_TOP_LEFT);
int y = 0;
for (int i = 0; i < saveButtons.length; i++) {
saveButtons[i].setBounds(10 * GUI_SCALE, y * GUI_SCALE, 200 * GUI_SCALE, 20 * GUI_SCALE, Component.ALIGN_TOP_LEFT);
saveButtons[i].setBounds(0, y * GUI_SCALE, 200 * GUI_SCALE, 20 * GUI_SCALE, Component.ALIGN_TOP_LEFT);
saveButtons[i].setFontSize(16 * GUI_SCALE);
deleteButtons[i].setBounds(220 * GUI_SCALE, y * GUI_SCALE, 50 * GUI_SCALE, 20 * GUI_SCALE, Component.ALIGN_TOP_LEFT);
deleteButtons[i].setBounds(210 * GUI_SCALE, y * GUI_SCALE, 50 * GUI_SCALE, 20 * GUI_SCALE, Component.ALIGN_TOP_LEFT);
deleteButtons[i].setFontSize(16 * GUI_SCALE);
y += 30;
}
Expand All @@ -129,14 +138,7 @@ public void updateGUIScale() {

@Override
public void render() {
for (Button b : saveButtons) {
if(b != null)
b.render();
}
for (Button b : deleteButtons) {
if(b != null)
b.render();
}
container.render();
if(createNew == null) init();
createNew.render();
back.render();
Expand Down
4 changes: 2 additions & 2 deletions src/cubyz/gui/menu/settings/KeybindingsGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public void updateGUIScale() {

y += 30;
}

container.setBounds(0, 0, Window.getWidth(), Window.getHeight() - 70 * GUI_SCALE, Component.ALIGN_TOP_LEFT);
}

public void endListen(int keyCode) {
Expand Down Expand Up @@ -188,8 +190,6 @@ public void render() {
}
}

container.setBounds(0, 0, Window.getWidth(), Window.getHeight() - 70 * GUI_SCALE, Component.ALIGN_TOP_LEFT);

container.render();
done.render();
}
Expand Down
66 changes: 47 additions & 19 deletions src/cubyz/rendering/Graphics.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
package cubyz.rendering;

import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_LINE_LOOP;
import static org.lwjgl.opengl.GL11.GL_LINE_STRIP;
import static org.lwjgl.opengl.GL11.GL_TRIANGLE_STRIP;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.glActiveTexture;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glGenBuffers;
import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
import static org.lwjgl.opengl.GL20.glUniform1f;
import static org.lwjgl.opengl.GL20.glUniform1i;
import static org.lwjgl.opengl.GL20.glUniform2f;
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;
import static org.lwjgl.opengl.GL32.*;

import java.io.IOException;

import org.joml.Vector4i;

import cubyz.rendering.text.CubyzFont;
import cubyz.rendering.text.TextLine;
import cubyz.utils.Utils;
Expand All @@ -36,6 +20,8 @@ public class Graphics {
private static int color;

private static float globalAlphaMultiplier = 1;

private static Vector4i clip = null;

/**
* Sets a new color using the hexcode. The alpha channel is given seperately.
Expand All @@ -61,6 +47,48 @@ public static void setGlobalAlphaMultiplier(float multiplier) {
globalAlphaMultiplier = multiplier;
TextLine.setGlobalAlphaMultiplier(multiplier);
}

/**
* @param newClip not null
* @return the previous clipping rect to restore it later.
*/
public static Vector4i setClip(Vector4i newClip) {
if(clip != null) {
if(newClip.x < clip.x) {
newClip.z -= (clip.x - newClip.x);
newClip.x += (clip.x - newClip.x);
}
if(newClip.y < clip.y) {
newClip.w -= (clip.y - newClip.y);
newClip.y += (clip.y - newClip.y);
}
if(newClip.x + newClip.z > clip.x + clip.z) {
newClip.z -= (newClip.x + newClip.z) - (clip.x + clip.z);
}
if(newClip.y + newClip.w > clip.y + clip.w) {
newClip.w -= (newClip.y + newClip.w) - (clip.y + clip.w);
}
} else {
glEnable(GL_SCISSOR_TEST);
}
Vector4i oldClip = clip;
clip = newClip;
glScissor(clip.x, clip.y, clip.z, clip.w);
return oldClip;
}

/**
* Should be used to restore the old clip when leaving the render function.
* @param previousClip
*/
public static void restoreClip(Vector4i previousClip) {
clip = previousClip;
if(clip == null) {
glDisable(GL_SCISSOR_TEST);
} else {
glScissor(clip.x, clip.y, clip.z, clip.w);
}
}


// ----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/cubyz/rendering/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.lwjgl.system.MemoryStack;

import cubyz.utils.Logger;
import cubyz.client.Cubyz;
import cubyz.gui.input.Keyboard;

public abstract class Window {
Expand Down Expand Up @@ -202,6 +203,7 @@ private static void init(long monitorID) {
Window.width = width;
Window.height = height;
Window.setResized(true);
Cubyz.gameUI.updateGUIScale();
});

glfwSetWindowFocusCallback(handle, (window, focused) -> {
Expand Down

0 comments on commit 67cd3ad

Please sign in to comment.