Skip to content

Commit

Permalink
Added 'priority' to binding targets
Browse files Browse the repository at this point in the history
  • Loading branch information
xTracr committed Mar 4, 2024
1 parent 7b478de commit ad7e049
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 85 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Snapshots are [here](https://github.com/xTracr/RealCamera/actions/workflows/buil
* Use F6 to toggle the feature on or off and other hotkeys to adjust the camera.
* Configure these features in the config screen (Cloth Config required).

> Learn more about [Configuration](https://github.com/xTracr/RealCamera/wiki/Configuration)
### Dependencies ###

* Fabric:
Expand All @@ -42,7 +40,8 @@ Snapshots are [here](https://github.com/xTracr/RealCamera/actions/workflows/buil
* Compatible:
* most camera mods
* most player model mods
* Not Enough Animations[^1]

## [More Information](https://github.com/xTracr/RealCamera/wiki) ##
[^1]: It is suggested to disable the `Animation Smoothing` option in the config of *Not Enough Animations*.

## [Change Log](changelog.md#English) ##
5 changes: 2 additions & 3 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* 按下F6来开关,另外一些键来调整摄像头
* 在Config界面配置以上特性(需Cloth Config)

> 详细了解[如何配置](https://github.com/xTracr/RealCamera/wiki/Configuration)
### 依赖项目 ###

* Fabric:
Expand All @@ -39,7 +37,8 @@
* 兼容:
* 多数摄像头模组
* 多数模型模组
* Not Enough Animations[^1]

## [更多信息](https://github.com/xTracr/RealCamera/wiki) ##
[^1]:建议在 *更多动画* 的配置内禁用`Animation Smoothing`选项。

## [更新日志](changelog.md#中文) ##
14 changes: 3 additions & 11 deletions common/src/main/java/com/xtracr/realcamera/RealCameraCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import org.joml.Matrix3f;
import org.joml.Matrix4f;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

public class RealCameraCore {
Expand Down Expand Up @@ -66,8 +64,7 @@ public static void setCameraPos(Vec3d vec3d) {
}

public static void init(MinecraftClient client) {
active = config().isEnabled() && client.options.getPerspective().isFirstPerson()
&& client.gameRenderer.getCamera() != null && client.player != null;
active = config().isEnabled() && client.options.getPerspective().isFirstPerson() && client.gameRenderer.getCamera() != null && client.player != null;
}

public static boolean isActive() {
Expand Down Expand Up @@ -120,14 +117,9 @@ public static void computeCamera(MinecraftClient client, float tickDelta) {
recorder.buildLastRecord();

Matrix3f normal = new Matrix3f();
List<BindingTarget> targets = new ArrayList<>(List.of(config().getTarget()));
List<BindingTarget> targetList = config().binding.targetList;
recorder.setCurrent(renderLayer -> targetList.stream().anyMatch(t -> renderLayer.toString().contains(t.textureId())));
String textureId = recorder.currentTextureId();
if (textureId != null) targets.addAll(targetList.stream().filter(t -> textureId.contains(t.textureId())).toList());
for (BindingTarget target : targets) {
for (BindingTarget target : config().getTargetList()) {
try {
recorder.setCurrent(renderLayer -> renderLayer.toString().contains(target.textureId()));
if (!recorder.setCurrent(renderLayer -> renderLayer.toString().contains(target.textureId()))) continue;
pos = recorder.getTargetPosAndRot(target, normal);
currentTarget = target;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
import java.util.List;

public class BindingTarget {
protected static final List<BindingTarget> defaultTargets = List.of(createDefaultTarget("minecraft_head", "minecraft:textures/entity/player/", false),
createDefaultTarget("minecraft_head_2", "minecraft:textures/entity/player/", true),
createDefaultTarget("skin_head", "minecraft:skins/", false),
createDefaultTarget("skin_head_2", "minecraft:skins/", true));
protected static final List<BindingTarget> defaultTargets = List.of(createDefaultTarget("minecraft_head", "minecraft:textures/entity/player/", 5, false),
createDefaultTarget("skin_head", "minecraft:skins/", 5, false),
createDefaultTarget("minecraft_head_2", "minecraft:textures/entity/player/", 0, true),
createDefaultTarget("skin_head_2", "minecraft:skins/", 0, true));
private final String name, textureId;
private final int priority;
private final float forwardU, forwardV, upwardU, upwardV, posU, posV, disablingDepth;
private final boolean bindX, bindY, bindZ, bindRotation;
private final double scale;
protected double offsetX, offsetY, offsetZ;
protected double scale, offsetX, offsetY, offsetZ;
protected float pitch, yaw, roll;

public BindingTarget() {
this(null, null, 0, 0, 0, 0, 0, 0, 0.2f, false, true, false, false, 1, 0, 0, 0, 0, 0, 0);
this(null, null, 0, 0, 0, 0, 0, 0, 0, 0.2f, false, true, false, false, 1, 0, 0, 0, 0, 0, 0);
}

public BindingTarget(String name, String textureId, float forwardU, float forwardV, float upwardU, float upwardV, float posU, float posV, float disablingDepth, boolean bindX, boolean bindY, boolean bindZ, boolean bindRotation, double scale, double offsetX, double offsetY, double offsetZ, float pitch, float yaw, float roll) {
public BindingTarget(String name, String textureId, int priority, float forwardU, float forwardV, float upwardU, float upwardV, float posU, float posV, float disablingDepth, boolean bindX, boolean bindY, boolean bindZ, boolean bindRotation, double scale, double offsetX, double offsetY, double offsetZ, float pitch, float yaw, float roll) {
this.name = name;
this.textureId = textureId;
this.priority = priority;
this.forwardU = forwardU;
this.forwardV = forwardV;
this.upwardU = upwardU;
Expand All @@ -43,8 +44,8 @@ public BindingTarget(String name, String textureId, float forwardU, float forwar
this.roll = roll;
}

private static BindingTarget createDefaultTarget(String name, String textureId, boolean shouldBind) {
return new BindingTarget(name, textureId, 0.1875f, 0.2f, 0.1875f, 0.075f, 0.1875f, 0.2f, 0.2f, shouldBind, true, shouldBind, shouldBind, 1, -0.12, 0, 0, 0, 0, 0);
private static BindingTarget createDefaultTarget(String name, String textureId, int priority, boolean shouldBind) {
return new BindingTarget(name, textureId, priority, 0.1875f, 0.2f, 0.1875f, 0.075f, 0.1875f, 0.2f, 0.2f, shouldBind, true, shouldBind, shouldBind, 1, -0.12, 0, 0, 0, 0, 0);
}

public boolean isEmpty() {
Expand All @@ -59,6 +60,10 @@ public String textureId() {
return textureId;
}

public int priority() {
return priority;
}

public float forwardU() {
return forwardU;
}
Expand Down Expand Up @@ -107,6 +112,10 @@ public double scale() {
return scale;
}

public void setScale(double scale) {
this.scale = scale;
}

public double offsetX() {
return offsetX * scale;
}
Expand Down
16 changes: 5 additions & 11 deletions common/src/main/java/com/xtracr/realcamera/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.util.math.MathHelper;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -149,16 +150,9 @@ public void adjustOffsetZ(int count) {
}

// binding
public String getTargetName() {
return binding.targetName;
}

public void setTargetName(String targetName) {
binding.targetName = targetName;
}

public BindingTarget getTarget() {
return binding.targetList.stream().filter(t -> t.name().equals(binding.targetName)).findAny().orElse(null);
public List<BindingTarget> getTargetList() {
binding.clamp();
return binding.targetList;
}

public void putTarget(BindingTarget target) {
Expand All @@ -167,6 +161,7 @@ public void putTarget(BindingTarget target) {
.filter(i -> binding.targetList.get(i).name().equals(target.name()))
.findAny()
.ifPresentOrElse(i -> binding.targetList.set(i, target), () -> binding.targetList.add(target));
binding.targetList.sort(Comparator.comparingInt(t -> -t.priority()));
}

public static class Classic {
Expand Down Expand Up @@ -209,7 +204,6 @@ public AdjustMode cycle() {

public static class Binding {
public boolean adjustOffset = true;
public String targetName = "minecraft_head";
public List<BindingTarget> targetList = new ArrayList<>(BindingTarget.defaultTargets);

private void clamp() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public int getFocusedIndex(int mouseX, int mouseY, int layers) {
public void preview(DrawContext context, BindingTarget target, int entitySize, int forwardArgb, int upwardArgb, int leftArgb) {
drawByAnother(context.getVertexConsumers());
context.draw();
target.setOffsetX(entitySize * target.offsetX());
target.setOffsetY(entitySize * target.offsetY());
target.setOffsetZ(entitySize * target.offsetZ());
target.setScale(entitySize * target.scale());
Matrix3f normal = new Matrix3f();
Vec3d pos;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ModelViewScreen extends Screen {
private Vec2f focusedUV;
private TextFieldWidget textureIdField, nameField;
private NumberFieldWidget<Float> forwardUField, forwardVField, upwardUField, upwardVField, posUField, posVField, scaleField, depthField;
private NumberFieldWidget<Integer> priorityField;
private final CyclingButtonWidget<Integer> selectingButton = createCyclingButton(Map.of(
0, Text.translatable(KEY_WIDGET + "forwardMode").styled(s -> s.withColor(Formatting.GREEN)),
1, Text.translatable(KEY_WIDGET + "upwardMode").styled(s -> s.withColor(Formatting.RED)),
Expand Down Expand Up @@ -147,11 +148,8 @@ private void initLeftWidgets(final int category) {
ConfigFile.save();
initWidgets(category, page);
}));
adder.add(createButton(Text.translatable(KEY_WIDGET + "bind"), widgetWidth, button -> {
ConfigFile.modConfig.setTargetName(nameField.getText());
ConfigFile.save();
initWidgets(category, page);
})).setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "bind")));
adder.add(priorityField = NumberFieldWidget.ofInt(textRenderer, widgetWidth - 2, widgetHeight - 2, 0, priorityField))
.setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "priority")));
adder.add(nameField = createTextField(widgetWidth * 2 + 4, nameField), 2, smallPositioner)
.setTooltip(Tooltip.of(Text.translatable(KEY_TOOLTIP + "targetName")));
nameField.setMaxLength(20);
Expand All @@ -161,7 +159,7 @@ private void initLeftWidgets(final int category) {
}

private void initRightWidgets(final int page) {
List<BindingTarget> targetList = ConfigFile.modConfig.binding.targetList;
List<BindingTarget> targetList = ConfigFile.modConfig.getTargetList();
final int widgetsPerPage = 6, pages = (targetList.size() - 1) / widgetsPerPage + 1;
GridWidget gridWidget = new GridWidget();
gridWidget.getMainPositioner().margin(4, 2, 0, 0);
Expand All @@ -173,8 +171,7 @@ private void initRightWidgets(final int page) {
for (int i = page * widgetsPerPage; i < Math.min((page + 1) * widgetsPerPage, targetList.size()); i++) {
BindingTarget target = targetList.get(i);
String name = target.name();
adder.add(createButton(Text.literal(name).styled(s -> name.equals(ConfigFile.modConfig.getTargetName()) ? s.withColor(Formatting.GREEN) : s),
widgetWidth * 2 - 18, button -> loadBindingTarget(target)));
adder.add(createButton(Text.literal(name), widgetWidth * 2 - 18, button -> loadBindingTarget(target)));
adder.add(new TexturedButton(32, 32, button -> {
targetList.remove(target);
ConfigFile.save();
Expand Down Expand Up @@ -256,8 +253,8 @@ protected void drawEntity(DrawContext context, float x, float y, int mouseX, int
}

protected BindingTarget generateBindingTarget() {
return new BindingTarget(nameField.getText(), textureIdField.getText(), forwardUField.getValue(), forwardVField.getValue(), upwardUField.getValue(),
upwardVField.getValue(), posUField.getValue(), posVField.getValue(), depthField.getValue(),
return new BindingTarget(nameField.getText(), textureIdField.getText(), priorityField.getValue(), forwardUField.getValue(), forwardVField.getValue(),
upwardUField.getValue(), upwardVField.getValue(), posUField.getValue(), posVField.getValue(), depthField.getValue(),
bindXButton.getValue() == 0, bindYButton.getValue() == 0, bindZButton.getValue() == 0, bindRotButton.getValue() == 0,
scaleField.getValue(), offsetXSlider.getValue(), offsetYSlider.getValue(), offsetZSlider.getValue(),
(float) pitchSlider.getValue(), (float) yawSlider.getValue(), (float) rollSlider.getValue());
Expand All @@ -267,6 +264,7 @@ protected void loadBindingTarget(BindingTarget target) {
if (target.isEmpty()) return;
nameField.setText(target.name());
textureIdField.setText(target.textureId());
priorityField.setValue(target.priority());
forwardUField.setValue(target.forwardU());
forwardVField.setValue(target.forwardV());
upwardUField.setValue(target.upwardU());
Expand Down Expand Up @@ -300,7 +298,7 @@ private DoubleSliderWidget createSlider(String key, int width, double min, doubl
}

private NumberFieldWidget<Float> createFloatField(int width, float defaultValue, @Nullable NumberFieldWidget<Float> copyFrom) {
return new FloatFieldWidget(textRenderer, width - 2, widgetHeight - 2, defaultValue, copyFrom, Text.empty()).setMax(1.0f).setMin(0f);
return NumberFieldWidget.ofFloat(textRenderer, width - 2, widgetHeight - 2, defaultValue, copyFrom).setMax(1.0f).setMin(0f);
}

private TextFieldWidget createTextField(int width, @Nullable TextFieldWidget copyFrom) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ public abstract class NumberFieldWidget<T extends Comparable<T>> extends TextFie
protected T maximum, minimum;
private Tooltip tooltip;

NumberFieldWidget(TextRenderer textRenderer, int x, int y, int width, int height, T defaultValue, T maximum, T minimum, @Nullable NumberFieldWidget<T> copyFrom, Text text) {
super(textRenderer, x, y, width, height, text);
NumberFieldWidget(TextRenderer textRenderer, int x, int y, int width, int height, T defaultValue, T maximum, T minimum, @Nullable NumberFieldWidget<T> copyFrom) {
super(textRenderer, x, y, width, height, Text.empty());
this.defaultValue = defaultValue;
this.maximum = maximum;
this.minimum = minimum;
setValue(defaultValue);
if (copyFrom != null) setValue(copyFrom.getValue());
}

public static NumberFieldWidget<Float> ofFloat(TextRenderer textRenderer, int width, int height, float defaultValue, @Nullable NumberFieldWidget<Float> copyFrom) {
return new FloatFieldWidget(textRenderer, 0, 0, width, height, defaultValue, copyFrom);
}

public static NumberFieldWidget<Integer> ofInt(TextRenderer textRenderer, int width, int height, int defaultValue, @Nullable NumberFieldWidget<Integer> copyFrom) {
return new IntFieldWidget(textRenderer, 0, 0, width, height, defaultValue, copyFrom);
}

public T getValue() {
try {
return getValueInternal();
Expand Down Expand Up @@ -56,6 +64,7 @@ public NumberFieldWidget<T> setMin(T minimum) {
protected void checkText() {
super.setTooltip(tooltip);
setRenderTextProvider((string, firstCharacterIndex) -> OrderedText.styledForwardsVisitedString(string, Style.EMPTY));
if (getText().isEmpty()) return;
try {
T value = getValueInternal();
if (value.compareTo(minimum) < 0) throw new Exception("< " + minimum);
Expand Down Expand Up @@ -83,4 +92,28 @@ public void renderButton(DrawContext context, int mouseX, int mouseY, float delt
checkText();
super.renderButton(context, mouseX, mouseY, delta);
}

private static class FloatFieldWidget extends NumberFieldWidget<Float> {
FloatFieldWidget(TextRenderer textRenderer, int x, int y, int width, int height, float defaultValue, @Nullable NumberFieldWidget<Float> copyFrom) {
super(textRenderer, x, y, width, height, defaultValue, Float.MAX_VALUE, -Float.MAX_VALUE, copyFrom);
setMaxLength(8);
}

@Override
protected Float getValueInternal() {
return Float.parseFloat(getText());
}
}

private static class IntFieldWidget extends NumberFieldWidget<Integer> {
IntFieldWidget(TextRenderer textRenderer, int x, int y, int width, int height, int defaultValue, @Nullable NumberFieldWidget<Integer> copyFrom) {
super(textRenderer, x, y, width, height, defaultValue, Integer.MAX_VALUE, Integer.MIN_VALUE, copyFrom);
setMaxLength(8);
}

@Override
protected Integer getValueInternal() {
return Integer.parseInt(getText());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Mixin(HeldItemRenderer.class)
public abstract class MixinHeldItemRenderer {
@Inject(method = "renderItem(FLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider$Immediate;Lnet/minecraft/client/network/ClientPlayerEntity;I)V", at = @At("HEAD"), cancellable = true)
private void realcamera$cancelRendering(float tickDelta, MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers, ClientPlayerEntity player, int light, CallbackInfo cInfo) {
private void realcamera$cancelRender(float tickDelta, MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers, ClientPlayerEntity player, int light, CallbackInfo cInfo) {
if (player instanceof ClientPlayerEntity && RealCameraCore.isActive() && ConfigFile.modConfig.isRendering()) {
cInfo.cancel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class MixinWorldRenderer {
private void realcamera$renderClientPlayer(MatrixStack matrices, float tickDelta, long limitTime, boolean renderBlockOutline,
Camera camera, GameRenderer gameRenderer, LightmapTextureManager lightmapTextureManager, Matrix4f projectionMatrix, CallbackInfo cInfo) {
final ModConfig config = ConfigFile.modConfig;
if (camera.isThirdPerson() || !RealCameraCore.isActive() || !config.isRendering() || (camera.getFocusedEntity() instanceof PlayerEntity player && player.isUsingSpyglass())) return;
if (!RealCameraCore.isActive() || !config.isRendering() || (camera.getFocusedEntity() instanceof PlayerEntity player && player.isUsingSpyglass())) return;
VertexConsumerProvider.Immediate immediate = this.bufferBuilders.getEntityVertexConsumers();
Vec3d cameraPos = camera.getPos();
if (!config.isClassic()) RealCameraCore.renderPlayer(immediate);
Expand Down
Loading

0 comments on commit ad7e049

Please sign in to comment.