Skip to content

Commit

Permalink
1. 现在可以为tip设置关闭后自动显示的下一个tip
Browse files Browse the repository at this point in the history
2. 现在可以在tip内添加一个纹理资源路径以显示图片
  • Loading branch information
goumo committed Dec 25, 2024
1 parent a0565cb commit 6a176a3
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 196 deletions.
273 changes: 192 additions & 81 deletions src/main/java/com/teammoeg/frostedheart/content/tips/Tip.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -75,14 +74,14 @@ public Tip getTip(String id) {
if (tip != null) {
return tip;
}
return Tip.builder(id).error(Tip.ErrorType.NOT_EXISTS, Lang.str(TIP_PATH.toString() + "\\" + id + ".json")).build();
return Tip.builder(id).error(Tip.ErrorType.OTHER, Lang.str(id), Lang.tips("error.tip_not_exists").component()).build();
}

/**
* 返回对应的 tip 实例是否存在
* 对应的 tip 是否存在
*/
public boolean hasTip(String id) {
return loadedTips.containsKey(id);
return id != null && loadedTips.containsKey(id);
}

/**
Expand All @@ -98,17 +97,25 @@ public void loadFromFile() {
File[] general = TIP_PATH.listFiles();
if (general != null) files.addAll(List.of(general));
if (!files.isEmpty()) {
int sum = 0;
for (File tipFile : files) {
Tip tip = Tip.fromJsonFile(tipFile);
loadedTips.put(tip.getId(), tip);
if (loadedTips.containsKey(tip.getId())) {
// 重复id
Tip d = Tip.builder("duplicate").error(Tip.ErrorType.LOAD, Lang.str(tip.getId()), Lang.tips("error.load.duplicate_id").component()).build();
display.force(d);
} else {
loadedTips.put(tip.getId(), tip);
sum++;
}
}
state.loadFromFile();
LOGGER.debug("{} tips loaded", sum);
}
LOGGER.debug("Loaded {} tips", files.size());
state.loadFromFile();
}

private void displayException(Tip.ErrorType type, Exception e) {
Tip exception = Tip.builder("exception").error(type, e).build();
private void displayException(Tip.ErrorType type, String id, Exception e) {
Tip exception = Tip.builder("exception").error(type, e, Lang.str(id)).build();
display.force(exception);
}

Expand Down Expand Up @@ -140,18 +147,22 @@ public void general(Tip tip) {

// 更改非临时 tip 的状态
if (!tip.isTemporary()) {
// if (tip.id.startsWith("*custom*")) {
// TipStateManager.manager.unlockCustom(tip);
// }
manager.state.setLockState(tip, true);
}

if (tip.isPin()) {
if (tip.isPin() && !TipRenderer.TIP_QUEUE.isEmpty()) {
Tip last = TipRenderer.TIP_QUEUE.get(0);
TipRenderer.removeCurrent();
TipRenderer.TIP_QUEUE.add(0, last);
TipRenderer.TIP_QUEUE.add(0, tip);
} else {
TipRenderer.TIP_QUEUE.add(tip);
}

// 添加下一个tip
if (!tip.getNextTip().isEmpty()) {
general(tip.getNextTip());
}
}

/**
Expand All @@ -165,8 +176,14 @@ public void force(String id) {
* 无视 tip 的状态,在渲染队列中强制添加此 tip
*/
public void force(Tip tip) {
if (!tip.isTemporary()) {
manager.state.setLockState(tip, true);
}

if (tip.isPin()) {
Tip last = TipRenderer.TIP_QUEUE.get(0);
TipRenderer.removeCurrent();
TipRenderer.TIP_QUEUE.add(0, last);
TipRenderer.TIP_QUEUE.add(0, tip);
} else {
TipRenderer.TIP_QUEUE.add(tip);
Expand All @@ -177,12 +194,14 @@ public void force(Tip tip) {
* 使 tip 永久显示,即 {@code alwaysVisible = true}
*/
public void alwaysVisible(Tip tip) {
if (TipRenderer.TIP_QUEUE.isEmpty()) return;

var list = TipRenderer.TIP_QUEUE;
if (list.size() <= 1 || list.get(0) == tip) return;
for (int i = 0; i < list.size(); i++) {
Tip t = list.get(i);
if (t == tip) {
Tip clone = Tip.builder("").copy(t).alwaysVisible(true).build();
Tip clone = Tip.builder("copy").copy(t).alwaysVisible(true).build();
TipRenderer.TIP_QUEUE.set(i, clone);
return;
}
Expand All @@ -192,15 +211,14 @@ public void alwaysVisible(Tip tip) {
/**
* 置顶 tip
*/
public void pin(String id) {
public void pin(Tip tip) {
var list = TipRenderer.TIP_QUEUE;
if (list.size() <= 1 || list.get(0).getId().equals(id)) return;
Iterator<Tip> iterator = list.iterator();
while (iterator.hasNext()) {
Tip tip = iterator.next();
if (tip.getId().equals(id)) {
iterator.remove();
list.add(0, tip);
if (list.size() <= 1 || list.get(0) == tip) return;

for (Tip t : list) {
if (t == tip) {
TipRenderer.TIP_QUEUE.remove(t);
force(tip);
return;
}
}
Expand Down Expand Up @@ -244,7 +262,7 @@ protected void loadFromFile() {
// 文件存在但是无法正确读取
if (TIP_STATE_FILE.exists()) {
String message = "The file '" + TIP_STATE_FILE + "' already exists but cannot be read correctly, it may be corrupted";
manager.displayException(Tip.ErrorType.LOAD, new Exception(message));
manager.displayException(Tip.ErrorType.LOAD, "tip_states.json", new Exception(message));
LOGGER.warn(message);
}
return;
Expand All @@ -254,7 +272,7 @@ protected void loadFromFile() {
.collect(Collectors.toMap(state -> manager.getTip(state.id), s -> s)));
} catch (IOException e) {
LOGGER.error("Unable to load file: '{}'", TIP_STATE_FILE, e);
manager.displayException(Tip.ErrorType.LOAD, e);
manager.displayException(Tip.ErrorType.LOAD, "tip_states.json", e);
}
}

Expand All @@ -267,7 +285,7 @@ public void saveToFile() {
writer.write(json);
} catch (IOException e) {
LOGGER.error("Unable to save file: '{}'", TIP_STATE_FILE, e);
manager.displayException(Tip.ErrorType.SAVE, e);
manager.displayException(Tip.ErrorType.SAVE, "tip_states.json", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.teammoeg.frostedheart.content.tips;

import com.teammoeg.frostedheart.FHMain;
import com.teammoeg.frostedheart.content.tips.client.gui.widget.TipWidget;
import com.teammoeg.frostedheart.infrastructure.config.FHConfig;
import com.teammoeg.frostedheart.util.client.ClientUtils;
Expand Down Expand Up @@ -46,7 +45,7 @@ public static boolean isTipRendering() {
*/
public static void removeCurrent() {
if (!TIP_QUEUE.isEmpty()) TIP_QUEUE.remove(0);
TipWidget.INSTANCE.setState(TipWidget.State.FADING_OUT);
TipWidget.INSTANCE.close();
}

@SubscribeEvent
Expand Down Expand Up @@ -120,12 +119,12 @@ public static void onGuiRender(ScreenEvent.Render.Post event) {
}

private static void update() {
if (TipWidget.INSTANCE.getState() == TipWidget.State.IDLE) {
if (!isTipRendering()) {
TIP_QUEUE.remove(TipWidget.INSTANCE.lastTip);
TipWidget.INSTANCE.lastTip = null;
// 切换下一个
if (!TIP_QUEUE.isEmpty()) {
TipWidget.INSTANCE.setTip(TIP_QUEUE.get(0));
TipWidget.INSTANCE.tip = TIP_QUEUE.get(0);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.teammoeg.frostedheart.content.tips.client.gui;

import com.teammoeg.frostedheart.FrostedHud;
import com.teammoeg.frostedheart.content.tips.Tip;
import com.teammoeg.frostedheart.content.tips.TipManager;
import com.teammoeg.frostedheart.content.tips.client.gui.widget.IconButton;
import com.teammoeg.frostedheart.content.waypoint.ClientWaypointManager;
Expand All @@ -9,6 +10,7 @@
import com.teammoeg.frostedheart.content.waypoint.waypoints.Waypoint;
import com.teammoeg.frostedheart.util.client.ClientUtils;
import com.teammoeg.frostedheart.util.client.FHColorHelper;
import com.teammoeg.frostedheart.util.lang.Lang;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Renderable;
Expand All @@ -32,6 +34,8 @@ public DebugScreen() {

@Override
public void init() {
buttons.clear();

addButton(IconButton.Icon.CROSS, FHColorHelper.CYAN, "Clear Tip Render Queue", (b) ->
TipManager.INSTANCE.display().clearRenderQueue()
);
Expand Down Expand Up @@ -72,10 +76,21 @@ public void init() {
addButton(IconButton.Icon.TRADE, FHColorHelper.CYAN, "Toggle Debug Overlay", (b) ->
FrostedHud.renderDebugOverlay = !FrostedHud.renderDebugOverlay
);
addButton(IconButton.Icon.LEAVE, FHColorHelper.CYAN, "Do Something", (b) -> {
String message = debug();
ClientUtils.getPlayer().sendSystemMessage(Lang.str(message));
});
super.init();
// addRenderableWidget(new TextLabelWidget(10, 10, 50, 50, Component.literal("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), ClientUtils.font()));
}

// 方便热重载debug
private String debug() {
Tip tip = Tip.builder("test").line(Lang.str("test")).line(Lang.str("aaaaaaaaaaaaa")).nextTip("default").build();
TipManager.INSTANCE.display().general(tip);
return tip.getNextTip();
}

public void addButton(IconButton.Icon icon, int color, String message, Button.OnPress onPress) {
IconButton button = new IconButton(0, 0, icon, color, Component.literal(message), onPress);
buttons.add(button);
Expand Down
Loading

0 comments on commit 6a176a3

Please sign in to comment.