Skip to content

Commit

Permalink
改善了编辑tip时的问题显示
Browse files Browse the repository at this point in the history
  • Loading branch information
goumo committed Jan 14, 2025
1 parent d7806d6 commit 72f1040
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
45 changes: 27 additions & 18 deletions src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Getter
public class Tip {
private static final Logger LOGGER = LogUtils.getLogger();
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static final Tip EMPTY = Tip.builder("empty").error(ErrorType.OTHER, Lang.tips("error.not_exists").component()).build();
public static final Component ERROR_DESC = Component.translatable("tips.frostedheart.error.desc");
public static final Tip EMPTY = Tip.builder("empty").error(ErrorType.OTHER, Lang.tips("error.not_exists").component(), ERROR_DESC).build();

/**
* 显示内容
Expand Down Expand Up @@ -134,7 +134,7 @@ public boolean saveAsFile() {
return true;
} catch (IOException e) {
LOGGER.error("Unable to save file: '{}'", file, e);
Tip.builder("exception").error(ErrorType.SAVE, e, Lang.str(file.getName())).build().forceDisplay();
Tip.builder("exception").error(ErrorType.SAVE, e).build().forceDisplay();
return false;
}
}
Expand Down Expand Up @@ -191,7 +191,7 @@ public static Tip fromJsonFile(File filePath) {
Tip.Builder builder = builder("exception");
if (!filePath.exists()) {
LOGGER.error("File does not exists '{}'", filePath);
builder.error(ErrorType.LOAD, Lang.str(filePath.toString()), Lang.tips("error.file_not_exists").component());
builder.error(ErrorType.LOAD, Lang.str(filePath.toString()), Lang.tips("error.file_not_exists", ERROR_DESC).component());
} else {
try {
String content = new String(Files.readAllBytes(Paths.get(String.valueOf(filePath))));
Expand All @@ -200,11 +200,11 @@ public static Tip fromJsonFile(File filePath) {

} catch (JsonSyntaxException e) {
LOGGER.error("Invalid JSON format '{}'", filePath, e);
builder.error(ErrorType.LOAD, e, Lang.str(builder.id), Lang.tips("error.invalid_json").component());
builder.error(ErrorType.LOAD, e, Lang.str(builder.id), Lang.tips("error.invalid_json", ERROR_DESC).component());

} catch (Exception e) {
LOGGER.error("Unable to load file '{}'", filePath, e);
builder.error(ErrorType.LOAD, e, Lang.str(builder.id), Lang.tips("error.other").component());
builder.error(ErrorType.LOAD, e, Lang.str(builder.id), Lang.tips("error.other").component(), ERROR_DESC);
}
}
return builder.build();
Expand Down Expand Up @@ -261,6 +261,10 @@ public Builder lines(Collection<Component> texts) {
return this;
}

public Builder lines(Component... texts) {
return lines(List.of(texts));
}

public Builder clearContents() {
if (!editable) return this;
this.contents.clear();
Expand Down Expand Up @@ -382,14 +386,15 @@ public Builder fromJson(JsonObject json) {
if (json.has("id")) {
String s = json.get("id").getAsString();
if (s.isBlank()) {
error(ErrorType.LOAD, Lang.str("ID: " + id), Lang.tips("error.load.no_id").component());
id = "exception";
error(ErrorType.LOAD, Lang.tips("error.load.no_id").component());
return this;
} else if (TipManager.INSTANCE.hasTip(s)) {
error(ErrorType.LOAD, Lang.str("ID: " + s), Lang.tips("error.load.duplicate_id").component());
return this;
}
id = s;
} else {
error(ErrorType.LOAD, Lang.str("ID: " + id), Lang.tips("error.load.no_id").component());
id = "exception";
error(ErrorType.LOAD, Lang.tips("error.load.no_id").component());
return this;
}

Expand Down Expand Up @@ -434,11 +439,10 @@ public Builder fromJson(JsonObject json) {
return this;
}

public Builder error(ErrorType type, Component... descriptions) {
public Builder error(ErrorType type, Collection<Component> descriptions) {
clearContents()
.line(Lang.tips("error." + type.key).component())
.lines(Arrays.asList(descriptions))
.line(Lang.tips("error.desc").component())
.lines(descriptions)
.color(FHColorHelper.RED, FHColorHelper.BLACK)
.alwaysVisible(true)
.setTemporary()
Expand All @@ -447,9 +451,14 @@ public Builder error(ErrorType type, Component... descriptions) {
return this;
}

public Builder error(ErrorType type, Component... descriptions) {
return error(type, List.of(descriptions));
}

public Builder error(ErrorType type, Exception exception, Component... descriptions) {
return error(type, descriptions)
.line(Lang.str(exception.getMessage()));
var desc = new ArrayList<>(List.of(descriptions));
desc.add(Lang.str(exception.getMessage()));
return error(type, desc);
}

private void imageSize(ResourceLocation location) {
Expand All @@ -464,18 +473,18 @@ private void imageSize(ResourceLocation location) {
}
} catch (IOException e) {
LOGGER.error("Invalid texture resource location {}", location, e);
error(ErrorType.LOAD, e, Lang.tips("error.load.invalid_image").component());
error(ErrorType.LOAD, e, Lang.tips("error.load.invalid_image", location).component());
}
}
this.image = null;
error(ErrorType.LOAD, Lang.tips("error.load.invalid_image").component());
error(ErrorType.LOAD, Lang.tips("error.load.invalid_image", location).component());
}

private int getColorOrElse(JsonObject json, String name, int defColor) {
try {
return Integer.parseUnsignedInt(json.get(name).getAsString(), 16);
} catch (NumberFormatException e) {
line(Lang.tips("error.load.invalid_digit", name).component());
error(ErrorType.LOAD, Lang.tips("error.load.invalid_digit", name).component());
return defColor;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Tip getTip(String id) {
if (tip != null) {
return tip;
}
return Tip.builder(id).error(Tip.ErrorType.OTHER, Lang.str(id), Lang.tips("error.load.tip_not_exists").component()).build();
return Tip.builder(id).error(Tip.ErrorType.DISPLAY, Lang.tips("error.load.tip_not_exists", id).component(), Tip.ERROR_DESC).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.teammoeg.frostedheart.base.client.gui.widget.IconButton;
import com.teammoeg.frostedheart.base.client.gui.widget.IconCheckbox;
import com.teammoeg.frostedheart.content.tips.Tip;
import com.teammoeg.frostedheart.content.tips.TipManager;
import com.teammoeg.frostedheart.content.tips.TipRenderer;
import com.teammoeg.frostedheart.util.client.ClientUtils;
import com.teammoeg.frostedheart.util.client.FHColorHelper;
Expand All @@ -36,7 +37,17 @@ public TipEditsList(Minecraft pMinecraft, int pWidth, int pHeight, int pY0, int
this.font = pMinecraft.font;
setRenderHeader(true, 10);

addEntry(new StringEntry("id", Component.translatable("gui.frostedheart.tip_editor.title")));
var idEntry = new StringEntry("id", Component.translatable("gui.frostedheart.tip_editor.title"));
idEntry.input.setResponder((s) -> {
if (TipManager.INSTANCE.hasTip(s)) {
idEntry.input.setTextColor(FHColorHelper.RED);
updatePreview();
} else {
idEntry.input.setTextColor(FHColorHelper.WHITE);
updatePreview();
}
});
addEntry(idEntry);
addEntry(new StringEntry("category", Component.translatable("gui.frostedheart.tip_editor.category")));
addEntry(new StringEntry("nextTip", Component.translatable("gui.frostedheart.tip_editor.next_tip")));
addEntry(new StringEntry("image", Component.translatable("gui.frostedheart.tip_editor.image")));
Expand All @@ -49,12 +60,12 @@ public TipEditsList(Minecraft pMinecraft, int pWidth, int pHeight, int pY0, int
addEntry(new BooleanEntry("pin", Component.translatable("gui.frostedheart.tip_editor.pin")));
}

public void updatePreview() {
public void updatePreview(Component... infos) {
TipRenderer.TIP_QUEUE.clear();
TipRenderer.forceClose();
Tip tip = Tip.builder("").fromJson(getJson()).alwaysVisible(true).build();
tip.forceDisplay();
Tip tip = Tip.builder("").fromJson(getJson()).lines(infos).alwaysVisible(true).build();
this.cachedId = tip.getId();
tip.forceDisplay();
}

public JsonObject getJson() {
Expand Down Expand Up @@ -192,6 +203,7 @@ public StringEntry(String property, Component message) {
super(property, message);
this.input = new EditBox(font, 0, 0, 64, 12, message);
this.input.setResponder(b -> updatePreview());
this.input.setMaxLength(1024);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ void update(Tip tip) {

// 文本换行
var contents = tip.getContents();
titleLines = ClientUtils.font().split(contents.get(0), width-24);
titleLines = ClientUtils.font().split(contents.get(0), width);
contentLines = new ArrayList<>();
if (contents.size() > 1)
for (int i = 1; i < contents.size(); i++)
contentLines.addAll(ClientUtils.font().split(contents.get(i), width-24));
contentLines.addAll(ClientUtils.font().split(contents.get(i), width));
totalLineSize = titleLines.size() + contentLines.size();
int height = (totalLineSize * RenderContext.LINE_SPACE);

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/assets/frostedheart/lang/en_us/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,13 @@
"tips.frostedheart.empty.title": "Oops, there's nothing here",
"tips.frostedheart.error.desc": "Please check if the Modpack is installed properly or report this issue",
"tips.frostedheart.error.other": "[Error] An unknown error occurred",
"tips.frostedheart.error.display": "[Error] Unable to load tip",
"tips.frostedheart.error.display": "[Error] Unable to display tip",
"tips.frostedheart.error.save": "[Error] Unable to save file",
"tips.frostedheart.error.load": "[Error] Unable to load file",
"tips.frostedheart.error.load.no_id": "Tip id is empty/blank",
"tips.frostedheart.error.load.duplicate_id": "Tip id is duplicate",
"tips.frostedheart.error.load.file_not_exists": "File does not exists",
"tips.frostedheart.error.load.tip_not_exists": "Tip does not exists",
"tips.frostedheart.error.load.tip_not_exists": "Tip '%s' does not exists",
"tips.frostedheart.error.load.empty": "No content to display",
"tips.frostedheart.error.load.invalid_json": "Invalid JSON file format",
"tips.frostedheart.error.load.invalid_image": "Invalid texture ResourceLocation '%s'",
Expand Down
7 changes: 4 additions & 3 deletions src/main/resources/assets/frostedheart/lang/zh_cn/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -919,16 +919,17 @@
"tooltip.frostedheart.pressure.average_intake": "平均消耗气压值",
"tooltip.frostedheart.pressure.average_output": "平均产生气压值",
"gui.frostedheart.creative_heater.heat": "产生的气压值",

"tips.frostedheart.empty.title": "哎呀,这里什么都没有",
"tips.frostedheart.error.desc": "请检查整合包是否正确安装,或向我们报告这个bug",
"tips.frostedheart.error.other": "错误",
"tips.frostedheart.error.other": "出现未知错误",
"tips.frostedheart.error.display": "错误:无法显示提示",
"tips.frostedheart.error.save": "错误:无法保存文件",
"tips.frostedheart.error.load": "错误:无法加载文件",
"tips.frostedheart.error.load.no_id": "提示ID为空或不存在",
"tips.frostedheart.error.load.no_id": "提示ID为空",
"tips.frostedheart.error.load.duplicate_id": "重复的提示ID",
"tips.frostedheart.error.load.file_not_exists": "文件不存在",
"tips.frostedheart.error.load.tip_not_exists": "提示不存在",
"tips.frostedheart.error.load.tip_not_exists": "提示 '%s' 不存在",
"tips.frostedheart.error.load.empty": "没有内容可供显示",
"tips.frostedheart.error.load.invalid_json": "无效的Json格式",
"tips.frostedheart.error.load.invalid_image": "无效的纹理资源路径 '%s'",
Expand Down

0 comments on commit 72f1040

Please sign in to comment.