Skip to content

Commit

Permalink
Allow for GUIs to be run at 60 FPS (#316)
Browse files Browse the repository at this point in the history
Co-authored-by: Yefancy <[email protected]>
  • Loading branch information
bruberu and Yefancy authored Dec 10, 2021
1 parent b03ee58 commit 73ec83d
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 29 deletions.
6 changes: 6 additions & 0 deletions src/main/java/gregtech/api/gui/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ public void detectAndSendChanges() {
public void updateScreen() {
}

/**
* Called clientside approximately every 1/60th of a second with this modular UI open
*/
public void updateScreenOnFrame() {
}

/**
* Called each draw tick to draw this widget in GUI
*/
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/gregtech/api/gui/impl/ModularUIGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import gregtech.api.gui.Widget;
import gregtech.api.gui.widgets.SlotWidget;
import gregtech.api.net.PacketUIWidgetUpdate;
import gregtech.common.ConfigHolder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
Expand All @@ -28,6 +27,7 @@ public class ModularUIGui extends GuiContainer implements IRenderContext {
public static final float rColorForOverlay = 1;
public static final float gColorForOverlay = 1;
public static final float bColorForOverlay = 1;
private float lastUpdate;

public ModularUI getModularUI() {
return modularUI;
Expand Down Expand Up @@ -72,6 +72,12 @@ public void handleWidgetUpdate(PacketUIWidgetUpdate packet) {

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
float now = getModularUI().entityPlayer.ticksExisted + partialTicks;
int times = (int) ((now - lastUpdate) / 0.333f);
for (int i = 0; i < times; i++) {
modularUI.guiWidgets.values().forEach(Widget::updateScreenOnFrame);
lastUpdate += 0.333f;
}
this.hoveredSlot = null;
drawDefaultBackground();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ public void updateScreen() {
}
}

@Override
public void updateScreenOnFrame() {
for (Widget widget : widgets) {
if (widget.isActive()) {
widget.updateScreenOnFrame();
}
}
}

@Override
public void drawInForeground(int mouseX, int mouseY) {
for (Widget widget : widgets) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/gregtech/api/gui/widgets/ProgressWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import gregtech.api.gui.IRenderContext;
import gregtech.api.gui.Widget;
import gregtech.api.gui.resources.TextureArea;
import gregtech.api.util.GTLog;
import gregtech.api.util.Position;
import gregtech.api.util.Size;
import net.minecraft.network.PacketBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public AnimaWidgetGroup(int x, int y, int width, int height) {
}

@Override
public void updateScreen() {
public void updateScreenOnFrame() {
if (interpolator != null) {
interpolator.update();
}
super.updateScreen();
super.updateScreenOnFrame();
}

@SideOnly(Side.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void handleClientAction(int id, PacketBuffer buffer) {
@Override
public void updateScreen() {
super.updateScreen();
if (mouseClickTime > 5) { // click
if (mouseClickTime > 3) { // click
Pair<SystemCall, String> pair = actions[actionMap(false, isCtrlDown(), isShiftDown())];
sendToServer(pair);
playButtonClickSound();
Expand All @@ -113,7 +113,7 @@ public boolean mouseClicked(int mouseX, int mouseY, int button) {
if (isMouseOverElement(mouseX, mouseY)) {
if (mouseClickTime == -1) {
mouseClickTime = 0;
} else if (mouseClickTime <= 5) { // double click
} else if (mouseClickTime <= 3) { // double click
Pair<SystemCall, String> pair = actions[actionMap(true, isCtrlDown(), isShiftDown())];
sendToServer(pair);
playButtonClickSound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ public void showMenu() {
}

@Override
public void updateScreen() {
public void updateScreenOnFrame() {
if(interpolator != null) interpolator.update();
super.updateScreen();
super.updateScreenOnFrame();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AbstractApplication initApp() {
this.setOs(os);
this.addWidget(new ImageWidget(5, 5, 333 - 10, 232 - 10, TerminalTheme.COLOR_B_2));
// enemy 0: Title
this.addWidget(new LabelWidget(333 / 2, 222 / 2 - 50, "Theseus's Escape", 0xFFFFFFFF), 0);
this.addWidget(new LabelWidget(333 / 2, 222 / 2 - 50, "Theseus's Escape", 0xFFFFFFFF).setXCentered(true), 0);
this.addWidget(new ClickButtonWidget(323 / 2 - 10, 222 / 2 - 10, 30, 30, "Play",
(clickData -> {
this.setGameState(1);
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/gregtech/common/terminal/app/game/pong/PongApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,7 @@ public void score(boolean side) {
}

@Override
public void updateScreen() {
super.updateScreen();
timer++;
if (Keyboard.isKeyDown(Keyboard.KEY_UP) ^ Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
if (Keyboard.isKeyDown(Keyboard.KEY_UP))
userInput = 1;
else
userInput = 0;
} else {
userInput = -1;
}
public void updateScreenOnFrame() {
if (ball.getSelfPosition().getX() < 10) {
this.score(false); // Right side gains a point
} else if (ball.getSelfPosition().getX() > 323) {
Expand All @@ -101,7 +91,7 @@ public void updateScreen() {

TwoDimensionalRayTracer.TwoDimensionalRayTraceResult result = TwoDimensionalRayTracer.nearestBoxSegmentCollision(
new Vector2f(ball.getSelfPosition().x, ball.getSelfPosition().y),
new Vector2f((float) (Math.cos(ball.theta) * 6), (float) (Math.sin(ball.theta) * 6)),
new Vector2f((float) (Math.cos(ball.theta) * 2), (float) (Math.sin(ball.theta) * 2)),
solidObjects,
new Vector2f(4, 4));
while (result.time != 1 && timeLeft != 0) {
Expand All @@ -128,20 +118,30 @@ public void updateScreen() {
timeLeft -= result.time * timeLeft;
result = TwoDimensionalRayTracer.nearestBoxSegmentCollision(
new Vector2f(ball.getSelfPosition().x, ball.getSelfPosition().y),
new Vector2f((float) (Math.cos(ball.theta) * 8 * timeLeft), (float) (Math.sin(ball.theta) * 8 * timeLeft)),
new Vector2f((float) (Math.cos(ball.theta) * 3 * timeLeft), (float) (Math.sin(ball.theta) * 3 * timeLeft)),
solidObjects,
new Vector2f(4, 4));
// To prevent it getting permanently lodged into something.
ball.addSelfPosition((Math.cos(ball.theta) * 6 * (result.time + 0.1) * timeLeft), (Math.sin(ball.theta) * 6 * (result.time + 0.1) * timeLeft));
ball.addSelfPosition((Math.cos(ball.theta) * 2 * (result.time + 0.1) * (timeLeft + 0.1)), (Math.sin(ball.theta) * 2 * (result.time + 0.1) * (timeLeft + 0.1)));
}
ball.addSelfPosition((Math.cos(ball.theta) * 6 * timeLeft), (Math.sin(ball.theta) * 6 * timeLeft));
ball.addSelfPosition((Math.cos(ball.theta) * 2 * timeLeft), (Math.sin(ball.theta) * 2 * timeLeft));
solidObjects.remove(2);
solidObjects.remove(2);
}
if (ball.getSelfPosition().getY() > 222) {
ball.setSelfPosition(new Position(ball.getSelfPosition().getX(), 211));
} else if (ball.getSelfPosition().getY() < 10)
ball.setSelfPosition(new Position(ball.getSelfPosition().getX(), 21));
timer++;
if (Keyboard.isKeyDown(Keyboard.KEY_UP) ^ Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
if (Keyboard.isKeyDown(Keyboard.KEY_UP))
userInput = 1;
else
userInput = 0;
} else {
userInput = -1;
}
super.updateScreenOnFrame();
}

public int simplePaddleAI(PaddleWidget paddle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public void drawInBackground(int mouseX, int mouseY, float partialTicks, IRender
}

@Override
public void updateScreen() {
super.updateScreen();
public void updateScreenOnFrame() {
super.updateScreenOnFrame();
if (this.getSelfPosition().getY() < 30) {
this.setSelfPosition(new Position(this.getSelfPosition().getX(), 30));
}
Expand All @@ -31,10 +31,10 @@ public void updateScreen() {
int speed;
switch (controlSupplier.apply(this)) {
case 0:
speed = 6;
speed = 2;
break;
case 1:
speed = -6;
speed = -2;
break;
default:
speed = 0;
Expand All @@ -44,7 +44,7 @@ public void updateScreen() {
}

public Rectangle toSelfRectangleBox() {
return new Rectangle(this.getSelfPosition().x - this.toRectangleBox().width / 2, this.getSelfPosition().y - this.toRectangleBox().height / 2,
return new Rectangle(this.getSelfPosition().x - this.toRectangleBox().width / 2 - 2, this.getSelfPosition().y - this.toRectangleBox().height / 2,
this.toRectangleBox().width, this.toRectangleBox().height);
}

Expand Down

0 comments on commit 73ec83d

Please sign in to comment.