Skip to content

Commit

Permalink
Fix resize behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Apr 3, 2023
1 parent dddd002 commit 830d097
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/teacon/slides/ModClientRegistries.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import org.teacon.slides.projector.ProjectorScreen;
import org.teacon.slides.screen.ProjectorScreen;
import org.teacon.slides.renderer.ProjectorRenderer;

import javax.annotation.ParametersAreNonnullByDefault;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public final class ProjectorContainerMenu extends AbstractContainerMenu {
final ProjectorUpdatePacket updatePacket;
public final ProjectorUpdatePacket updatePacket;

public ProjectorContainerMenu(int containerId, ProjectorBlockEntity entity) {
super(ModRegistries.MENU.get(), containerId);
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/teacon/slides/screen/LazyWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.teacon.slides.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraftforge.common.util.NonNullFunction;
import net.minecraftforge.common.util.NonNullSupplier;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.function.Supplier;

@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public final class LazyWidget<T extends GuiEventListener & NarratableEntry> implements Supplier<T> {
private @Nullable T cached;
private final NonNullSupplier<T> initializer;
private final NonNullFunction<T, T> refresher;

private <U> LazyWidget(U init, NonNullFunction<? super T, U> refresher, NonNullFunction<U, ? extends T> supplier) {
this.refresher = old -> supplier.apply(refresher.apply(old));
this.initializer = () -> supplier.apply(init);
this.cached = null;
}

public static <T extends GuiEventListener & NarratableEntry, U> LazyWidget<T> of(
U init, NonNullFunction<? super T, U> refresher, NonNullFunction<U, ? extends T> supplier) {
return new LazyWidget<>(init, refresher, supplier);
}

public T refresh() {
RenderSystem.assertOnRenderThread();
var obj = this.cached;
if (obj == null) {
obj = this.initializer.get();
} else {
obj = this.refresher.apply(obj);
}
this.cached = obj;
return obj;
}

@Override
public T get() {
RenderSystem.assertOnRenderThread();
var obj = this.cached;
if (obj == null) {
obj = this.initializer.get();
this.cached = obj;
}
return obj;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.teacon.slides.projector;
package org.teacon.slides.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
Expand All @@ -19,7 +19,6 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Rotation;
import net.minecraftforge.common.util.Lazy;
import net.objecthunter.exp4j.ExpressionBuilder;
import org.apache.commons.lang3.StringUtils;
import org.joml.Matrix4f;
Expand All @@ -29,6 +28,9 @@
import org.lwjgl.glfw.GLFW;
import org.teacon.slides.SlideShow;
import org.teacon.slides.network.ProjectorUpdatePacket;
import org.teacon.slides.projector.ProjectorBlock;
import org.teacon.slides.projector.ProjectorBlockEntity;
import org.teacon.slides.projector.ProjectorContainerMenu;
import org.teacon.slides.url.ProjectorURL;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -66,18 +68,18 @@ public final class ProjectorScreen extends AbstractContainerScreen<ProjectorCont
URL_MAX_LENGTH = 1 << 9,
COLOR_MAX_LENGTH = 1 << 3;

private final Lazy<EditBox> mURLInput;
private final Lazy<EditBox> mColorInput;
private final Lazy<EditBox> mWidthInput;
private final Lazy<EditBox> mHeightInput;
private final Lazy<EditBox> mOffsetXInput;
private final Lazy<EditBox> mOffsetYInput;
private final Lazy<EditBox> mOffsetZInput;
private final LazyWidget<EditBox> mURLInput;
private final LazyWidget<EditBox> mColorInput;
private final LazyWidget<EditBox> mWidthInput;
private final LazyWidget<EditBox> mHeightInput;
private final LazyWidget<EditBox> mOffsetXInput;
private final LazyWidget<EditBox> mOffsetYInput;
private final LazyWidget<EditBox> mOffsetZInput;

private final Lazy<Button> mFlipRotation;
private final Lazy<Button> mCycleRotation;
private final Lazy<Button> mSwitchSingleSided;
private final Lazy<Button> mSwitchDoubleSided;
private final LazyWidget<Button> mFlipRotation;
private final LazyWidget<Button> mCycleRotation;
private final LazyWidget<Button> mSwitchSingleSided;
private final LazyWidget<Button> mSwitchDoubleSided;

private final ProjectorUpdatePacket mUpdatePacket;

Expand Down Expand Up @@ -105,26 +107,26 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
mDoubleSided = menu.updatePacket.doubleSided;
mImgBlocked = menu.updatePacket.imgUrlBlockedNow;
// url input
mURLInput = Lazy.of(() -> {
mURLInput = LazyWidget.of(toImageUrl(mUpdatePacket.imgUrl), EditBox::getValue, value -> {
var input = new EditBox(font, leftPos + 30, topPos + 29, 137, 16, URL_TEXT);
input.setMaxLength(URL_MAX_LENGTH);
input.setResponder(text -> {
try {
mURL = new ProjectorURL(text);
mInvalidURL = false;
mImgBlocked = menu.updatePacket.imgUrlBlockedNow && mURL.equals(mUpdatePacket.imgUrl);
mImgBlocked = mUpdatePacket.imgUrlBlockedNow && mURL.equals(mUpdatePacket.imgUrl);
} catch (IllegalArgumentException e) {
mURL = null;
mInvalidURL = StringUtils.isNotBlank(text);
mImgBlocked = false;
}
input.setTextColor(mInvalidURL ? 0xE04B4B : mImgBlocked ? 0xE0E04B : 0xE0E0E0);
});
input.setValue(mUpdatePacket.imgUrl == null ? "" : mUpdatePacket.imgUrl.toUrl().toString());
input.setValue(value);
return input;
});
// color input
mColorInput = Lazy.of(() -> {
mColorInput = LazyWidget.of(String.format("%08X", mUpdatePacket.color), EditBox::getValue, value -> {
var input = new EditBox(font, leftPos + 55, topPos + 155, 56, 16, COLOR_TEXT);
input.setMaxLength(COLOR_MAX_LENGTH);
input.setResponder(text -> {
Expand All @@ -136,11 +138,11 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
}
input.setTextColor(mInvalidColor ? 0xE04B4B : 0xE0E0E0);
});
input.setValue(String.format("%08X", mUpdatePacket.color));
input.setValue(value);
return input;
});
// width input
mWidthInput = Lazy.of(() -> {
mWidthInput = LazyWidget.of(toOptionalSignedString(mUpdatePacket.dimensionX), EditBox::getValue, value -> {
var input = new EditBox(font, leftPos + 30, topPos + 51, 56, 16, WIDTH_TEXT);
input.setResponder(text -> {
try {
Expand All @@ -152,11 +154,11 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
}
input.setTextColor(mInvalidWidth ? 0xE04B4B : 0xE0E0E0);
});
input.setValue(toOptionalSignedString(mUpdatePacket.dimensionX));
input.setValue(value);
return input;
});
// height input
mHeightInput = Lazy.of(() -> {
mHeightInput = LazyWidget.of(toOptionalSignedString(mUpdatePacket.dimensionY), EditBox::getValue, value -> {
var input = new EditBox(font, leftPos + 111, topPos + 51, 56, 16, HEIGHT_TEXT);
input.setResponder(text -> {
try {
Expand All @@ -168,11 +170,11 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
}
input.setTextColor(mInvalidHeight ? 0xE04B4B : 0xE0E0E0);
});
input.setValue(toOptionalSignedString(mUpdatePacket.dimensionY));
input.setValue(value);
return input;
});
// offset x input
mOffsetXInput = Lazy.of(() -> {
mOffsetXInput = LazyWidget.of(toSignedString(mUpdatePacket.slideOffsetX), EditBox::getValue, value -> {
var input = new EditBox(font, leftPos + 30, topPos + 103, 29, 16, OFFSET_X_TEXT);
input.setResponder(text -> {
try {
Expand All @@ -183,11 +185,11 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
}
input.setTextColor(mInvalidOffsetX ? 0xE04B4B : 0xE0E0E0);
});
input.setValue(toSignedString(mUpdatePacket.slideOffsetX));
input.setValue(value);
return input;
});
// offset y input
mOffsetYInput = Lazy.of(() -> {
mOffsetYInput = LazyWidget.of(toSignedString(mUpdatePacket.slideOffsetY), EditBox::getValue, value -> {
var input = new EditBox(font, leftPos + 84, topPos + 103, 29, 16, OFFSET_Y_TEXT);
input.setResponder(text -> {
try {
Expand All @@ -198,11 +200,11 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
}
input.setTextColor(mInvalidOffsetY ? 0xE04B4B : 0xE0E0E0);
});
input.setValue(toSignedString(mUpdatePacket.slideOffsetY));
input.setValue(value);
return input;
});
// offset z input
mOffsetZInput = Lazy.of(() -> {
mOffsetZInput = LazyWidget.of(toSignedString(mUpdatePacket.slideOffsetZ), EditBox::getValue, value -> {
var input = new EditBox(font, leftPos + 138, topPos + 103, 29, 16, OFFSET_Z_TEXT);
input.setResponder(text -> {
try {
Expand All @@ -213,43 +215,43 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
}
input.setTextColor(mInvalidOffsetZ ? 0xE04B4B : 0xE0E0E0);
});
input.setValue(toSignedString(mUpdatePacket.slideOffsetZ));
input.setValue(value);
return input;
});
// internal rotation buttons
mFlipRotation = Lazy.of(() -> {
mFlipRotation = LazyWidget.of(true, b -> b.visible, value -> {
var button = new Button(leftPos + 117, topPos + 153, 179, 153, 18, 19, FLIP_TEXT, () -> {
var newRotation = mRotation.flip();
updateRotation(newRotation);
});
button.visible = true;
button.visible = value;
return button;
});
mCycleRotation = Lazy.of(() -> {
mCycleRotation = LazyWidget.of(true, b -> b.visible, value -> {
var button = new Button(leftPos + 142, topPos + 153, 179, 173, 18, 19, ROTATE_TEXT, () -> {
var newRotation = mRotation.compose(Rotation.CLOCKWISE_90);
updateRotation(newRotation);
});
button.visible = true;
button.visible = value;
return button;
});
// single sided / double sided
mSwitchSingleSided = Lazy.of(() -> {
mSwitchSingleSided = LazyWidget.of(mDoubleSided, b -> b.visible, value -> {
var button = new Button(leftPos + 9, topPos + 153, 179, 113, 18, 19, SINGLE_DOUBLE_SIDED_TEXT, () -> {
if (mDoubleSided) {
updateDoubleSided(false);
}
});
button.visible = mDoubleSided;
button.visible = value;
return button;
});
mSwitchDoubleSided = Lazy.of(() -> {
mSwitchDoubleSided = LazyWidget.of(!mDoubleSided, b -> b.visible, value -> {
var button = new Button(leftPos + 9, topPos + 153, 179, 133, 18, 19, SINGLE_DOUBLE_SIDED_TEXT, () -> {
if (!mDoubleSided) {
updateDoubleSided(true);
}
});
button.visible = !mDoubleSided;
button.visible = value;
return button;
});
}
Expand All @@ -258,18 +260,18 @@ public ProjectorScreen(ProjectorContainerMenu menu, Inventory inventory, Compone
protected void init() {
super.init();

addRenderableWidget(mURLInput.get());
addRenderableWidget(mColorInput.get());
addRenderableWidget(mWidthInput.get());
addRenderableWidget(mHeightInput.get());
addRenderableWidget(mOffsetXInput.get());
addRenderableWidget(mOffsetYInput.get());
addRenderableWidget(mOffsetZInput.get());
addRenderableWidget(mURLInput.refresh());
addRenderableWidget(mColorInput.refresh());
addRenderableWidget(mWidthInput.refresh());
addRenderableWidget(mHeightInput.refresh());
addRenderableWidget(mOffsetXInput.refresh());
addRenderableWidget(mOffsetYInput.refresh());
addRenderableWidget(mOffsetZInput.refresh());

addRenderableWidget(mFlipRotation.get());
addRenderableWidget(mCycleRotation.get());
addRenderableWidget(mSwitchSingleSided.get());
addRenderableWidget(mSwitchDoubleSided.get());
addRenderableWidget(mFlipRotation.refresh());
addRenderableWidget(mCycleRotation.refresh());
addRenderableWidget(mSwitchSingleSided.refresh());
addRenderableWidget(mSwitchDoubleSided.refresh());

setInitialFocus(mURLInput.get());
}
Expand Down Expand Up @@ -461,6 +463,10 @@ private static float parseFloat(String text) {
return (float) new ExpressionBuilder(text).implicitMultiplication(false).build().evaluate();
}

private static String toImageUrl(@Nullable ProjectorURL imgUrl) {
return imgUrl == null ? "" : imgUrl.toUrl().toString();
}

private static String toOptionalSignedString(float f) {
return Float.toString(Math.round(f * 1.0E5F) / 1.0E5F);
}
Expand Down

0 comments on commit 830d097

Please sign in to comment.