Skip to content

Commit

Permalink
将警告逻辑转进抽象类
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyRespawnanchor committed Nov 10, 2024
1 parent 59bbb7e commit 4f94981
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,52 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.ayamemc.ayame.client.gui.widget.BlurWidget;
import org.ayamemc.ayame.util.ConfigUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.ayamemc.ayame.util.ResourceLocationHelper.withAyameNamespace;

@Environment(EnvType.CLIENT)
public abstract class AyameMainScreen extends Screen {
public abstract class AyameScreen extends Screen {
public static final ResourceLocation MENU_BACKGROUND_TEXTURE = withAyameNamespace("textures/gui/background.png");
public static final ResourceLocation MENU_BACKGROUND_OUTLINE_TEXTURE = withAyameNamespace("textures/gui/background_outline.png");
public static final ResourceLocation MENU_TOP_LAYER_TEXTURE = withAyameNamespace("textures/gui/top_layer.png");
protected static final int BACKGROUND_TEXTURE_WIDTH = 410;
protected static final int BACKGROUND_TEXTURE_HEIGHT = 220;
protected static final int MINI_BUTTON_SIZE = 16;
private static final int BUTTON_SIZE = 32;
protected final boolean skipWarningOnce;
protected Screen lastScreen;

public AyameMainScreen(@Nullable Screen lastScreen) {
/**
* 构造方法,允许设置是否单次跳过警告界面。
*
* @param lastScreen 上一个屏幕
* @param skipWarningOnce 是否跳过一次警告界面
*/
public AyameScreen(@Nullable Screen lastScreen, boolean skipWarningOnce) {
super(Component.empty());
this.lastScreen = lastScreen;
this.skipWarningOnce = skipWarningOnce;
}

public AyameScreen(@Nullable Screen lastScreen) {
super(Component.empty());
this.lastScreen = lastScreen;
this.skipWarningOnce = false;
}


@Override
protected void init() {
// 检查是否需要显示警告界面
if (!ConfigUtil.SKIP_AYAME_WARNING && !skipWarningOnce) {
this.minecraft.setScreen(new StatementScreen(null, lastScreen));
return;
}

// 显示模糊背景
BlurWidget blurredBackgroundWidget = new BlurWidget(getCenteredX(BACKGROUND_TEXTURE_WIDTH), getCenteredY(BACKGROUND_TEXTURE_HEIGHT), BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
this.addRenderableOnly(blurredBackgroundWidget);
}
Expand Down Expand Up @@ -94,7 +117,7 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta)
BUTTON_SIZE,
settingSprites,
button -> {
minecraft.setScreen(new SettingsScreen(this));
minecraft.setScreen(new SettingsScreen(this,true));
},
Component.translatable("ayame.screen.warningscreen.settingsscreen.title")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
/**
* {@code ModelSelectMenuScreen} 负责处理 Ayame 模型的选择界面。
* <p>
* 该类继承自 {@link AyameMainScreen},主要功能是:
* 该类继承自 {@link AyameScreen},主要功能是:
* <ul>
* <li>显示可供选择的模型列表</li>
* <li>处理模型选择的逻辑</li>
Expand All @@ -61,14 +61,13 @@
* 在模型切换时,可通过 {@link SwitchModelCallback} 进行切换时的自定义处理。
* </p>
*
* @see AyameMainScreen
* @see AyameScreen
*/
@Environment(EnvType.CLIENT)
public class ModelSelectMenuScreen extends AyameMainScreen {
public final static int searchBarWidth = 112;
public final static int searchBarHeight = 23;
public class ModelSelectMenuScreen extends AyameScreen {
public static final int searchBarWidth = 112;
public static final int searchBarHeight = 23;
protected static final Path MODEL_DIR = Path.of("config/ayame/models/");
public final boolean skipWarningOnce;
public final List<IModelResource> modelResources;
public @Nullable ModelType selectedModel = AyameModelCache.getPlayerModel(Minecraft.getInstance().player);
public @Nullable CloseCallback closeCallback;
Expand All @@ -81,64 +80,20 @@ public class ModelSelectMenuScreen extends AyameMainScreen {
* @param skipWarningOnce 是否跳过一次警告界面
*/
public ModelSelectMenuScreen(@Nullable Screen lastScreen, boolean skipWarningOnce) {
super(lastScreen);
this.skipWarningOnce = skipWarningOnce;
super(lastScreen, skipWarningOnce);
this.modelResources = ModelResourceAPI.listModels(true);
}

/**
* 带有回调函数的构造方法。
* <p>
* 该方法允许为屏幕关闭和模型切换设置回调函数。
*
* @param lastScreen 上一个屏幕
* @param skipWarningOnce 是否跳过一次警告界面
* @param closeCallback 屏幕关闭时执行的回调
* @param switchModelCallback 模型切换时执行的回调
*/
public ModelSelectMenuScreen(@Nullable Screen lastScreen, boolean skipWarningOnce, @Nullable CloseCallback closeCallback, @Nullable SwitchModelCallback switchModelCallback) {
this(lastScreen, skipWarningOnce);
this.closeCallback = closeCallback;
this.switchModelCallback = switchModelCallback;
}

/**
* 默认构造方法,不跳过警告界面。
*
* @param lastScreen 上一个屏幕
*/
public ModelSelectMenuScreen(@Nullable Screen lastScreen) {
this(lastScreen, false);
super(lastScreen, false);
this.modelResources = ModelResourceAPI.listModels(true);
}

/**
* 打开模型选择菜单并在选择后切换模型
*
* @param lastScreen 上一个屏幕
*/
public static void openDefaultModelSelectMenu(Screen lastScreen) {
ModelSelectMenuScreen screen = new ModelSelectMenuScreen(lastScreen, false, (modelResources, selectedModel) -> {
// close的callback,也许以后用的上
}, (modelResources, selectedModel) -> {
if (selectedModel != null) {
AyameModelCache.setPlayerModel(Minecraft.getInstance().player, selectedModel);
}
});
Minecraft.getInstance().setScreen(screen);
}

/**
* 屏幕初始化方法,负责初始化模型选择界面。
* <p>
* 如果未设置跳过警告,则会先显示警告屏幕。
*/
@Override
protected void init() {
if (!ConfigUtil.SKIP_AYAME_WARNING && !skipWarningOnce) {
this.minecraft.setScreen(new StatementScreen(this, lastScreen));
return;
}
super.init(); // 调用父类的初始化方法,加载通用的背景和组件
super.init(); // 继承父类的初始化方法,包括警告检查逻辑

// 搜索框组件初始化
EditBox searchBox = new EditBox(
this.font,
getAlignedX(BACKGROUND_TEXTURE_WIDTH, searchBarWidth, 0, Alignment.CENTER) + 27,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@
import org.jetbrains.annotations.Nullable;

@Environment(EnvType.CLIENT)
public class SettingsScreen extends AyameMainScreen {
public class SettingsScreen extends AyameScreen {
/**
* 构造方法,允许设置是否单次跳过警告界面。
*
* @param lastScreen 上一个屏幕
* @param skipWarningOnce 是否跳过一次警告界面
*/
public SettingsScreen(@Nullable Screen lastScreen, boolean skipWarningOnce) {
super(lastScreen, skipWarningOnce);
}
public SettingsScreen(@Nullable Screen lastScreen) {
super(lastScreen);
super(lastScreen, false);
}



@Override
protected @NotNull ResourceLocation renderTopLayerResourceLocation() {
return MENU_TOP_LAYER_TEXTURE;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ public static void renderCustomHandEventHandler(
}

public static void openSelectMenuKeyPressed() {
ModelSelectMenuScreen.openDefaultModelSelectMenu(minecraft.screen);
minecraft.setScreen(new ModelSelectMenuScreen(null));
}
}

0 comments on commit 4f94981

Please sign in to comment.