Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public static void load() {
}

public static void save() {
if (config == null) load();
try (BufferedWriter writer = Files.newBufferedWriter(path.get())) {
config.clamp();
GSON.toJson(config, writer);
} catch (Exception exception) {
RealCamera.LOGGER.warn("Failed to save " + FILE_NAME, exception);
Expand Down
13 changes: 13 additions & 0 deletions common/src/main/java/com/xtracr/realcamera/gui/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.xtracr.realcamera.gui;

public enum Category {
CONFIGS,
PREVIEW,
DISABLE;

public final String id = name().toLowerCase();

Category next() {
return values()[(ordinal() + 1) % values().length];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.xtracr.realcamera.gui;

import com.mojang.blaze3d.platform.InputConstants;
import com.xtracr.realcamera.config.ConfigFile;

public record LayoutConstants(int xSize, int ySize, int x, int y, int middleWidth, int widgetWidth, int widgetHeight, int wideWidgetWidth, int compactWidgetWidth, InputConstants.Key modifierKey) {
public static LayoutConstants create(int width, int height) {
int xSize = Math.clamp(width - 10, 10, 450);
int ySize = Math.clamp(height - 10, 10, 206);
int middleWidth = Math.max(10, xSize - 200);
int x = (width - xSize) / 2;
int y = (height - ySize) / 2;
int widgetWidth = 42;
int widgetHeight = 18;
int wideWidgetWidth = widgetWidth * 2 + 4;
int compactWidgetWidth = widgetWidth * 2 - 18;
InputConstants.Key modifierKey = InputConstants.getKey(ConfigFile.config().binding.screenModifierKey);
return new LayoutConstants(xSize, ySize, x, y, middleWidth, widgetWidth, widgetHeight, wideWidgetWidth, compactWidgetWidth, modifierKey);
}
}
194 changes: 49 additions & 145 deletions common/src/main/java/com/xtracr/realcamera/gui/ModelViewScreen.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DoubleSlider extends AbstractSliderButton {
private final double min, max;

public DoubleSlider(int width, int height, double value, double min, double max, Function<Double, Component> textFactory) {
super(0, 0, width, height, textFactory.apply(value), Mth.clamp(0, (value - min) / (max - min), 1));
super(0, 0, width, height, textFactory.apply(value), Mth.clamp((value - min) / (max - min), 0, 1));
this.textFactory = textFactory;
this.min = min;
this.max = max;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.xtracr.realcamera.gui.components;

import com.mojang.blaze3d.platform.InputConstants;
import com.xtracr.realcamera.config.UVRectangle;
import com.xtracr.realcamera.gui.util.GUIHelper;
import com.xtracr.realcamera.gui.util.TextureViewport;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.world.phys.Vec2;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

public class UVRectangleWidget extends AbstractWidget {
private static final int SELECTION_COLOR = 0x4F3333CC, SELECTION_HOVER_COLOR = 0x2F3333CC, OUTLINE_COLOR = 0xAAFFFFFF;

public float uMin, vMin, uMax, vMax;
@Nullable
private TextureViewport viewport;
private Runnable onDelete = () -> {};
private Runnable onFocusUpdate = () -> {};

public UVRectangleWidget(float uMin, float vMin, float uMax, float vMax) {
super(0, 0, 16, 16, CommonComponents.EMPTY);
this.uMin = uMin;
this.vMin = vMin;
this.uMax = uMax;
this.vMax = vMax;
}

public void setOnDelete(@NonNull Runnable onDelete) {
this.onDelete = onDelete;
}

public void setOnFocusUpdate(@NonNull Runnable onFocusUpdate) {
this.onFocusUpdate = onFocusUpdate;
}

public UVRectangleWidget setViewport(@NonNull TextureViewport viewport) {
this.viewport = viewport;
return this;
}

public UVRectangle toUVRectangle() {
return new UVRectangle(uMin, vMin, uMax, vMax);
}

public boolean contains(UVRectangleWidget other) {
return uMin <= other.uMin && vMin <= other.vMin && uMax >= other.uMax && vMax >= other.vMax;
}

public boolean mergeWith(UVRectangleWidget other) {
if (uMin == other.uMin && uMax == other.uMax && vMin <= other.vMax && vMax >= other.vMin) {
vMin = Math.min(vMin, other.vMin);
vMax = Math.max(vMax, other.vMax);
return true;
}
if (vMin == other.vMin && vMax == other.vMax && uMin <= other.uMax && uMax >= other.uMin) {
uMin = Math.min(uMin, other.uMin);
uMax = Math.max(uMax, other.uMax);
return true;
}
return false;
}

@Override
public void extractWidgetRenderState(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, float partialTicks) {
if (viewport == null) return;
Vec2 minXY = viewport.uvToXY(uMin, vMin), maxXY = viewport.uvToXY(uMax, vMax);
float x1 = minXY.x, y1 = minXY.y, x2 = maxXY.x, y2 = maxXY.y, width = x2 - x1, height = y2 - y1;
setX((int) x1);
setY((int) y1);
setWidth((int) width);
setHeight((int) height);
GUIHelper.enableScissor(graphics, viewport.area());
GUIHelper.fill(graphics, x1, y1, x2, y2, SELECTION_COLOR);
if (isHoveredOrFocused()) GUIHelper.fill(graphics, x1, y1, x2, y2, SELECTION_HOVER_COLOR);
if (isFocused()) GUIHelper.outline(graphics, x1, y1, width, height, OUTLINE_COLOR);
graphics.disableScissor();
}

@Override
public boolean keyPressed(@NonNull KeyEvent event) {
if (event.input() == InputConstants.KEY_DELETE) {
onDelete.run();
return true;
}
if (event.isSelection() && uMin < uMax && vMin < vMax) {
return true;
}
return super.keyPressed(event);
}

@Override
public void setFocused(boolean focused) {
super.setFocused(focused);
if (focused) onFocusUpdate.run();
}

@Override
protected void updateWidgetNarration(@NonNull NarrationElementOutput narrationElementOutput) {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xtracr.realcamera.gui;
package com.xtracr.realcamera.gui.util;

import com.xtracr.realcamera.mixin.accessor.GuiGraphicsExtractorAccessor;
import com.xtracr.realcamera.renderer.state.BuiltModelRecord;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.xtracr.realcamera.gui;
package com.xtracr.realcamera.gui.util;

import com.google.common.collect.ImmutableSet;
import com.mojang.blaze3d.platform.Lighting;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.xtracr.realcamera.gui.util;

import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.world.phys.Vec2;

public record TextureViewport(ScreenRectangle area, int textureScale, float textureX, float textureY) {
private static final int DEFAULT_SCALE = 80;

public Vec2 uvToXY(float u, float v) {
int left = area.left(), width = area.width(), top = area.top(), height = area.height();
float scale = (textureScale * width) / (float) DEFAULT_SCALE;
float x = (u + textureX - 0.5f) * scale + left + width / 2.0f;
float y = (v + textureY - 0.5f) * scale + top + height / 2.0f;
return new Vec2(x, y);
}

public Vec2 xyToUV(float x, float y) {
int left = area.left(), width = area.width(), top = area.top(), height = area.height();
float invScale = (float) DEFAULT_SCALE / (textureScale * width);
float u = (x - left - width / 2.0f) * invScale - textureX + 0.5f;
float v = (y - top - height / 2.0f) * invScale - textureY + 0.5f;
return new Vec2(u, v);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.xtracr.realcamera.gui.util;

import com.xtracr.realcamera.config.UVRectangle;
import com.xtracr.realcamera.gui.components.DoubleSlider;
import com.xtracr.realcamera.gui.components.NumberField;
import com.xtracr.realcamera.gui.components.UVRectangleWidget;
import com.xtracr.realcamera.util.LocUtil;
import com.xtracr.realcamera.util.MathUtil;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.CycleButton;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;

import java.util.Map;

public final class WidgetFactory {

public static Tooltip tooltip(String key, Object... args) {
return Tooltip.create(LocUtil.MODEL_VIEW_TOOLTIP(key, args));
}

public static Button button(Component message, int width, int height, Button.OnPress onPress) {
return Button.builder(message, onPress).size(width, height).build();
}

public static UVRectangleWidget rectWidget(UVRectangle rect) {
return new UVRectangleWidget(rect.uMin(), rect.vMin(), rect.uMax(), rect.vMax());
}

public static <T> CycleButton.Builder<T> cyclingButton(Map<T, Component> messages, T defaultValue) {
return new CycleButton.Builder<>(messages::get, () -> defaultValue).withValues(messages.keySet());
}

public static DoubleSlider slider(String key, int width, int height, double min, double max) {
return new DoubleSlider(width, height, 0, min, max, d -> LocUtil.MODEL_VIEW_WIDGET(key, MathUtil.round(d, 2)));
}

public static NumberField<Float> floatField(Font font, int width, int height, float defaultValue) {
return NumberField.ofFloat(font, width - 2, height - 2, defaultValue, null).setMax(1.0f).setMin(0f);
}

public static EditBox textField(Font font, int width, int height, int maxLength) {
EditBox editBox = new EditBox(font, 0, 0, width - 2, height - 2, CommonComponents.EMPTY);
editBox.setMaxLength(maxLength);
return editBox;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.xtracr.realcamera.gui.GUIHelper;
import com.xtracr.realcamera.gui.util.GUIHelper;
import com.xtracr.realcamera.util.MathUtil;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.gui.render.TextureSetup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.xtracr.realcamera.gui.GUIHelper;
import com.xtracr.realcamera.gui.util.GUIHelper;
import com.xtracr.realcamera.util.MathUtil;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.gui.render.TextureSetup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.xtracr.realcamera.gui.GUIHelper;
import com.xtracr.realcamera.gui.util.GUIHelper;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.gui.render.TextureSetup;
import net.minecraft.client.renderer.state.gui.GuiElementRenderState;
Expand Down