Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.20' into 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
duckegg128 committed Jan 14, 2025
2 parents bae9532 + 1a1d665 commit 0b829f9
Show file tree
Hide file tree
Showing 36 changed files with 666 additions and 280 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.teammoeg.frostedheart.base.client.gui.widget;

import com.mojang.blaze3d.vertex.PoseStack;
import com.teammoeg.frostedheart.util.client.FHColorHelper;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;

public class ColorEditbox extends EditBox {
private static final String PREFIX = "0x";
private static final String PREFIX_WITH_ALPHA = "0xFF";
protected final Font font;
protected final boolean withAlpha;

public ColorEditbox(Font font, int x, int y, int width, int height, Component message) {
this(font, x, y, width, height, message, true, 0);
}

public ColorEditbox(Font font, int x, int y, int width, int height, Component message, boolean withAlpha, int colorValue) {
super(font, x, y, width, height, message);
this.font = font;
this.withAlpha = withAlpha;
setValue(colorValue);
setMaxLength(withAlpha ? 6 : 8);
setResponder(s -> {
try {
setTextColor(FHColorHelper.WHITE);
Integer.parseUnsignedInt(s, 16);
} catch (NumberFormatException e) {
setTextColor(FHColorHelper.RED);
}
});
}

@Override
public void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
super.renderWidget(graphics, mouseX, mouseY, partialTick);
graphics.drawString(font, getPrefix(), getX()-2-font.width(getPrefix()), getY()+(getHeight()/2)-4, 0xFFFFFFFF);

PoseStack pose = graphics.pose();
pose.pushPose();
pose.translate(1, 1, 0);
graphics.fill(getX()+getWidth()+2, getY(), getX()+getWidth()+getHeight()+2, getY()+getHeight(), FHColorHelper.makeDark(getColorValue(), 0.25F));
pose.translate(-1, -1, 0);
graphics.fill(getX()+getWidth()+2, getY(), getX()+getWidth()+getHeight()+2, getY()+getHeight(), getColorValue());
pose.popPose();
}

public int getColorValue() {
try {
return withAlpha ? FHColorHelper.setAlpha(Integer.parseUnsignedInt(getValue(), 16), 1F) : Integer.parseUnsignedInt(getValue(), 16);
} catch (NumberFormatException e) {
return FHColorHelper.RED;
}
}

public void setValue(int value) {
if (withAlpha) {
setValue(FHColorHelper.toHexString(value).substring(2).toUpperCase());
} else {
setValue(FHColorHelper.toHexString(value).toUpperCase());
}
}

private String getPrefix() {
return withAlpha ? PREFIX_WITH_ALPHA : PREFIX;
}

@Override
public @NotNull String getValue() {
return (withAlpha ? "FF" : "") + super.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -1,82 +1,78 @@
package com.teammoeg.frostedheart.content.tips.client.gui.widget;
package com.teammoeg.frostedheart.base.client.gui.widget;

import com.mojang.blaze3d.vertex.PoseStack;
import com.teammoeg.frostedheart.FHMain;
import com.teammoeg.frostedheart.util.client.ClientUtils;
import com.teammoeg.frostedheart.util.client.FHColorHelper;
import com.teammoeg.frostedheart.util.client.FHGuiHelper;
import com.teammoeg.frostedheart.util.client.RawMouseHelper;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraftforge.common.util.Size2i;
import org.jetbrains.annotations.NotNull;

public class IconButton extends Button {
public static final ResourceLocation ICON_LOCATION = new ResourceLocation(FHMain.MODID, "textures/gui/hud_icon.png");
public static final ResourceLocation ICON_LOCATION = FHMain.rl("textures/gui/hud/flat_icon.png");
public static final int TEXTURE_HEIGHT = 80;
public static final int TEXTURE_WIDTH = 80;
public static final int ICON_SIZE = 10;

public Icon currentIcon;
public final Icon icon;
public int color;
// 为什么是int? 混素达咩(
private int scale;

/**
* @param icon 按钮的图标 {@link Icon}
*/
public IconButton(int x, int y, Icon icon, int color, Component title, OnPress pressedAction) {
super(x, y, 10, 10, title, pressedAction, Button.DEFAULT_NARRATION);
this(x, y, icon, color, 1, title, pressedAction);
}

public IconButton(int x, int y, Icon icon, int color, int scale, Component title, OnPress pressedAction) {
super(x, y, icon.size.width, icon.size.height, title, pressedAction, Button.DEFAULT_NARRATION);
this.scale = Mth.clamp(scale, 1, Integer.MAX_VALUE);
width *= scale;
height *= scale;
this.color = color;
this.currentIcon = icon;
this.icon = icon;
}

@Override
public void renderWidget(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) {
if (isHoveredOrFocused()) {
graphics.fill(getX(), getY(), getX()+width, getY()+height, FHColorHelper.setAlpha(color, 50));
if (!getMessage().getString().isEmpty() && isHovered()) {
graphics.fill(getX(), getY(), getX()+getWidth(), getY()+getHeight(), FHColorHelper.setAlpha(color, 50));
if (!getMessage().getString().isBlank() && isHovered()) {
int textWidth = ClientUtils.font().width(getMessage());
int renderX = getX()-textWidth+8;
if (renderX < 0) {
graphics.fill(getX(), getY()-12, getX()+2 + textWidth, getY(), FHColorHelper.setAlpha(color, 50));
graphics.fill(getX(),
getY()-12,
getX()+2 + textWidth,
getY(),
FHColorHelper.setAlpha(color, 50));
graphics.drawString(ClientUtils.font(), getMessage(), getX()+2, getY()-10, color);
} else {
graphics.fill(getX()+8 - textWidth, getY()-12, getX()+10, getY(), FHColorHelper.setAlpha(color, 50));
graphics.drawString(ClientUtils.font(), getMessage(), getX()-textWidth+width, getY()-10, color);
graphics.fill(getX()-textWidth+getWidth()-1,
getY()-12,
getX()+getWidth(),
getY(),
FHColorHelper.setAlpha(color, 50));
graphics.drawString(ClientUtils.font(), getMessage(), getX()-textWidth+getWidth(), getY()-10, color);
}
}
}

FHGuiHelper.bindTexture(ICON_LOCATION);
FHGuiHelper.blitColored(graphics.pose(), getX(), getY(), ICON_SIZE, ICON_SIZE, currentIcon.x, currentIcon.y, ICON_SIZE, ICON_SIZE, TEXTURE_WIDTH, TEXTURE_HEIGHT, color, alpha);
}

/**
* 渲染一个图标
* @param icon {@link IconButton.Icon}
* @param color 图标的颜色
*/
public static void renderIcon(PoseStack pose, Icon icon, int x, int y, int color) {
FHGuiHelper.bindTexture(ICON_LOCATION);
FHGuiHelper.blitColored(pose, x, y, ICON_SIZE, ICON_SIZE, icon.x, icon.y, ICON_SIZE, ICON_SIZE, TEXTURE_WIDTH, TEXTURE_HEIGHT, color);
FHGuiHelper.blitColored(graphics.pose(), getX(), getY(), getWidth(), getHeight(), icon.x*scale, icon.y*scale, getWidth(), getHeight(), TEXTURE_WIDTH*scale, TEXTURE_HEIGHT*scale, color, alpha);
}

/**
* 直接在屏幕中渲染一个图标按钮
* @param icon {@link IconButton.Icon}
* @param color 图标的颜色
* @param BGColor 未被选中时的背景颜色,为 0 时不显示
* @return 是否被按下
*/
public static boolean renderIconButton(GuiGraphics graphics, Icon icon, double mouseX, double mouseY, int x, int y, int color, int BGColor) {
boolean mouseIn = RawMouseHelper.isMouseIn(mouseX, mouseY, x, y, ICON_SIZE, ICON_SIZE);
if (color != 0 && mouseIn) {
graphics.fill(x, y, x+ICON_SIZE, y+ICON_SIZE, 50 << 24 | color & 0x00FFFFFF);
} else if (BGColor != 0) {
graphics.fill(x, y, x+ICON_SIZE, y+ICON_SIZE, BGColor);
}
renderIcon(graphics.pose(), icon, x, y, color);
return mouseIn && RawMouseHelper.isLeftClicked();
public void setScale(int scale) {
int w = this.width / this.scale;
int h = this.height / this.scale;
this.scale = scale;
this.width = w * scale;
this.height = h * scale;
}

public enum Icon {
Expand Down Expand Up @@ -108,12 +104,20 @@ public enum Icon {
RIGHT_SLIDE (0 ,50),
WRENCH (0 ,70);

final int x;
final int y;
public final int x;
public final int y;
public final Size2i size;

Icon(int x, int y, Size2i size) {
this.x = x;
this.y = y;
this.size = size;
}

Icon(int x, int y) {
this.x = x;
this.y = y;
this.size = new Size2i(10, 10);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.teammoeg.frostedheart.base.client.gui.widget;

import com.mojang.blaze3d.systems.RenderSystem;
import com.teammoeg.frostedheart.util.client.FHColorHelper;
import com.teammoeg.frostedheart.util.client.FHGuiHelper;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Checkbox;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;

public class IconCheckbox extends Checkbox {
private static final IconButton.Icon SELECTED_ICON = IconButton.Icon.CHECK;
private int scale;

public IconCheckbox(int pX, int pY, Component pMessage, boolean pSelected) {
this(pX, pY, 1, pMessage, pSelected);
}

public IconCheckbox(int pX, int pY, int scale, Component pMessage, boolean pSelected) {
super(pX, pY, SELECTED_ICON.size.width, SELECTED_ICON.size.height, pMessage, pSelected, false);
this.scale = Mth.clamp(scale, 1, Integer.MAX_VALUE);
width *= scale;
height *= scale;
}

public void setScale(int scale) {
int w = this.width / this.scale;
int h = this.height / this.scale;
this.scale = scale;
this.width = w * scale;
this.height = h * scale;
}

@Override
public void renderWidget(@NotNull GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
IconButton.Icon icon = selected() ? SELECTED_ICON : IconButton.Icon.CROSS;
RenderSystem.enableDepthTest();
pGuiGraphics.setColor(1.0F, 1.0F, 1.0F, this.alpha);
RenderSystem.enableBlend();
FHGuiHelper.bindTexture(IconButton.ICON_LOCATION);
FHGuiHelper.blitColored(pGuiGraphics.pose(), getX(), getY(), getWidth(), getHeight(), icon.x*scale, icon.y*scale, getWidth(), getHeight(), IconButton.TEXTURE_WIDTH*scale, IconButton.TEXTURE_HEIGHT*scale, FHColorHelper.CYAN, alpha);
pGuiGraphics.setColor(1.0F, 1.0F, 1.0F, 1.0F);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

public class FHDynamicModels {
public static void setup() {
T1GeneratorRenderer.FUEL = DynamicBlockModelReference.getModelCached(FHMain.MODID, "block/multiblocks/generator_fuel.obj");
T2GeneratorRenderer.FUEL = DynamicBlockModelReference.getModelCached(FHMain.MODID, "block/multiblocks/generator_t2_fuel.obj" );
MechCalcRenderer.MODEL = DynamicBlockModelReference.getModelCached(FHMain.MODID, "block/mechanical_calculator_movable.obj");
T1GeneratorRenderer.FUEL = DynamicBlockModelReference.getModelCached(FHMain.MODID, "block/multiblocks/generator_fuel").register();
T2GeneratorRenderer.FUEL = DynamicBlockModelReference.getModelCached(FHMain.MODID, "block/multiblocks/generator_t2_fuel").register();
MechCalcRenderer.MODEL = DynamicBlockModelReference.getModelCached(FHMain.MODID, "block/mechanical_calculator_movable").register();
}
}
20 changes: 5 additions & 15 deletions src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.common.util.Size2i;
import org.slf4j.Logger;
Expand Down Expand Up @@ -152,7 +150,7 @@ public CompoundTag toNBT() {
nbt.putInt("fontColor", fontColor);
nbt.putInt("backgroundColor", backgroundColor);
var toAddContents = new ListTag();
this.contents.stream().map(content -> StringTag.valueOf(getKeyOrElseStr(content))).forEach(toAddContents::add);
this.contents.stream().map(content -> StringTag.valueOf(Lang.getKeyOrElseStr(content))).forEach(toAddContents::add);
nbt.put("contents", toAddContents);
return nbt;
}
Expand All @@ -171,19 +169,11 @@ public JsonObject toJson() {
json.addProperty("fontColor", Integer.toHexString(fontColor).toUpperCase());
json.addProperty("backgroundColor", Integer.toHexString(backgroundColor).toUpperCase());
var toAddContents = new JsonArray();
this.contents.stream().map(Tip::getKeyOrElseStr).forEach(toAddContents::add);
this.contents.stream().map(Lang::getKeyOrElseStr).forEach(toAddContents::add);
json.add("contents", toAddContents);
return json;
}

private static String getKeyOrElseStr(Component component) {
if (component instanceof MutableComponent c && (c.getContents() instanceof TranslatableContents t)) {
return t.getKey();
} else {
return component.getString();
}
}

public static Tip.Builder builder(String id) {
return new Builder(id);
}
Expand Down Expand Up @@ -387,7 +377,7 @@ public Builder fromNBT(CompoundTag nbt) {
}

public Builder fromJson(JsonObject json) {
if (!editable) return this;
if (!editable || json == null) return this;

if (json.has("id")) {
String s = json.get("id").getAsString();
Expand Down Expand Up @@ -431,12 +421,12 @@ public Builder fromJson(JsonObject json) {
}

if (json.has("category" )) category (json.get("category").getAsString());
if (json.has("next" )) nextTip (json.get("next").getAsString());
if (json.has("nextTip" )) nextTip (json.get("nextTip").getAsString());
if (json.has("alwaysVisible" )) alwaysVisible(json.get("alwaysVisible").getAsBoolean());
if (json.has("onceOnly" )) onceOnly (json.get("onceOnly").getAsBoolean());
if (json.has("hide" )) hide (json.get("hide").getAsBoolean());
if (json.has("pin" )) pin (json.get("pin").getAsBoolean());
if (json.has("visibleTime" )) displayTime (Math.max(json.get("visibleTime").getAsInt(), 0));
if (json.has("displayTime" )) displayTime (Math.max(json.get("displayTime").getAsInt(), 0));
if (json.has("fontColor" )) fontColor (getColorOrElse(json, "fontColor", FHColorHelper.CYAN));
if (json.has("backgroundColor")) BGColor (getColorOrElse(json, "backgroundColor", FHColorHelper.BLACK));

Expand Down
Loading

0 comments on commit 0b829f9

Please sign in to comment.