Skip to content

Commit

Permalink
拆子类
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyRespawnanchor committed Oct 6, 2024
1 parent 8100b23 commit 49b2acf
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 99 deletions.
29 changes: 0 additions & 29 deletions common/src/main/java/org/ayamemc/ayame/client/gui/Alignment.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.ayamemc.ayame.Ayame;
import org.ayamemc.ayame.client.gui.Alignment;
import org.ayamemc.ayame.client.gui.widget.BlurWidget;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -28,13 +27,12 @@ public abstract class AyameMainScreen 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");
public static final ResourceLocation SETTINGS_TEXTURE = withAyameNamespace("textures/gui/settings.png");
private static final Path MODEL_DIR = Path.of("config/ayame/models/");
private static final int BACKGROUND_TEXTURE_WIDTH = 410;
private static final int BACKGROUND_TEXTURE_HEIGHT = 220;
protected static final int BACKGROUND_TEXTURE_WIDTH = 410;
protected static final int BACKGROUND_TEXTURE_HEIGHT = 220;
private static final int BUTTON_SIZE = 32;
protected static final int MINI_BUTTON_SIZE = 16;

protected final Screen lastScreen;
protected Screen lastScreen;

public AyameMainScreen(@Nullable Screen lastScreen) {
super(Component.empty());
Expand All @@ -45,20 +43,6 @@ public AyameMainScreen(@Nullable Screen lastScreen) {
protected void init() {
BlurWidget blurredBackgroundWidget = new BlurWidget(getCenteredX(BACKGROUND_TEXTURE_WIDTH), getCenteredY(BACKGROUND_TEXTURE_HEIGHT), BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
this.addRenderableOnly(blurredBackgroundWidget);

final int searchBarWidth = 112;
final int searchBarHeight = 23;
EditBox searchBarEditBox = new EditBox(
this.font,
getAlignedX(BACKGROUND_TEXTURE_WIDTH, searchBarWidth, 0, Alignment.CENTER) + 27,
getAlignedY(BACKGROUND_TEXTURE_HEIGHT, searchBarHeight, 0, Alignment.TOP) + 1,
searchBarWidth,
searchBarHeight,
Component.translatable("ayame.widget.searchBarEditBox")
);
searchBarEditBox.setHint(Component.translatable("ayame.widget.searchBarEditBox").withStyle(ChatFormatting.DARK_GRAY));
searchBarEditBox.setBordered(true);
addRenderableWidget(searchBarEditBox);
}

@Override
Expand Down Expand Up @@ -90,39 +74,21 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta)
withAyameNamespace("settings_disabled"),
withAyameNamespace("settings_enabled_focused")
);
WidgetSprites opendirSprites = new WidgetSprites(
withAyameNamespace("opendir"),
withAyameNamespace("opendir"),
withAyameNamespace("opendir_enabled_focused")
);

ImageButton settingsButton = new ImageButton(
getAlignedX(BACKGROUND_TEXTURE_WIDTH, BUTTON_SIZE, 0, Alignment.LEFT),
getAlignedY(BACKGROUND_TEXTURE_HEIGHT, BUTTON_SIZE, 0, Alignment.BOTTOM),
BUTTON_SIZE,
BUTTON_SIZE,
settingSprites,
button -> {
Ayame.LOGGER.info("Setting button clicked.");
minecraft.setScreen(new SettingsScreen(this));
},
Component.empty()
Component.translatable("ayame.screen.warningscreen.settingsscreen.title")
);
ImageButton opendirButton = new ImageButton(
getAlignedX(BACKGROUND_TEXTURE_WIDTH, BUTTON_SIZE, 0, Alignment.LEFT),
getAlignedY(BACKGROUND_TEXTURE_HEIGHT, BUTTON_SIZE, 90, Alignment.BOTTOM),
BUTTON_SIZE,
BUTTON_SIZE,
opendirSprites,
button -> {
Ayame.LOGGER.info("Opendir button clicked.");
Util.getPlatform().openPath(MODEL_DIR);
},
Component.empty()
);
opendirButton.setTooltip(Tooltip.create(Component.translatable("ayame.button.opendir.tooltip")));


addRenderableWidget(settingsButton);
addRenderableWidget(opendirButton);

Component titleText = Component.translatable(setTranslatableTitle());
int centerX = getCenteredStringX(titleText);
Expand All @@ -143,15 +109,11 @@ protected int getCenteredY(int elementHeight) {

protected int getAlignedX(int containerWidth, int elementWidth, int margin, Alignment alignment) {
int baseX = getCenteredX(containerWidth);
switch (alignment) {
case LEFT:
return baseX + margin;
case RIGHT:
return baseX + containerWidth - elementWidth - margin;
case CENTER:
default:
return baseX + (containerWidth - elementWidth) / 2;
}
return switch (alignment) {
case LEFT -> baseX + margin;
case RIGHT -> baseX + containerWidth - elementWidth - margin;
default -> baseX + (containerWidth - elementWidth) / 2;
};
}

protected int getCenteredStringX(Component text) {
Expand All @@ -162,19 +124,23 @@ protected int getCenteredStringX(Component text) {

protected int getAlignedY(int containerHeight, int elementHeight, int margin, Alignment alignment) {
int baseY = getCenteredY(containerHeight);
switch (alignment) {
case TOP:
return baseY + margin;
case BOTTOM:
return baseY + containerHeight - elementHeight - margin;
case CENTER:
default:
return baseY + (containerHeight - elementHeight) / 2;
}
return switch (alignment) {
case TOP -> baseY + margin;
case BOTTOM -> baseY + containerHeight - elementHeight - margin;
default -> baseY + (containerHeight - elementHeight) / 2;
};
}

@Override
public void onClose() {
minecraft.setScreen(lastScreen);
}

protected enum Alignment {
CENTER,
LEFT,
RIGHT,
TOP,
BOTTOM,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@

package org.ayamemc.ayame.client.gui.screen;

import com.mojang.blaze3d.vertex.PoseStack;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.ayamemc.ayame.client.api.ModelResourceAPI;
import org.ayamemc.ayame.client.resource.IModelResource;
Expand All @@ -33,8 +42,11 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.util.List;

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

/**
* {@code ModelSelectMenuScreen} 负责处理 Ayame 模型的选择界面。
* <p>
Expand All @@ -54,6 +66,11 @@
*/
@Environment(EnvType.CLIENT)
public class ModelSelectMenuScreen extends AyameMainScreen {
protected static final Path MODEL_DIR = Path.of("config/ayame/models/");

public final static int searchBarWidth = 112;
public final static int searchBarHeight = 23;

public final boolean skipWarningOnce;
public final List<IModelResource> modelResources;
public @Nullable AyameModelType selectedModel = AyameModelCache.getPlayerModel(Minecraft.getInstance().player);
Expand Down Expand Up @@ -125,6 +142,41 @@ protected void init() {
return;
}
super.init(); // 调用父类的初始化方法,加载通用的背景和组件
EditBox searchBox = new EditBox(
this.font,
getAlignedX(BACKGROUND_TEXTURE_WIDTH, searchBarWidth, 0, Alignment.CENTER) + 27,
getAlignedY(BACKGROUND_TEXTURE_HEIGHT, searchBarHeight, 0, Alignment.TOP) + 1,
searchBarWidth,
searchBarHeight,
Component.translatable("ayame.widget.searchBox")
);
searchBox.setHint(Component.translatable("ayame.widget.searchBox").withStyle(ChatFormatting.DARK_GRAY));
searchBox.setBordered(true);
addRenderableWidget(searchBox);
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
super.render(guiGraphics, mouseX, mouseY, delta);

WidgetSprites opendirSprites = new WidgetSprites(
withAyameNamespace("opendir"),
withAyameNamespace("opendir"),
withAyameNamespace("opendir_enabled_focused")
);
ImageButton opendirButton = new ImageButton(
getAlignedX(BACKGROUND_TEXTURE_WIDTH, MINI_BUTTON_SIZE, 0, Alignment.RIGHT) - 125,
getAlignedY(BACKGROUND_TEXTURE_HEIGHT, MINI_BUTTON_SIZE, 0, Alignment.BOTTOM) - 3,
MINI_BUTTON_SIZE,
MINI_BUTTON_SIZE,
opendirSprites,
button -> {
Util.getPlatform().openPath(MODEL_DIR);
},
Component.translatable("ayame.button.opendir.tooltip")
);
opendirButton.setTooltip(Tooltip.create(Component.translatable("ayame.button.opendir.tooltip")));
addRenderableWidget(opendirButton);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,4 @@ public SettingsScreen(@Nullable Screen lastScreen) {
protected @NotNull String setTranslatableTitle() {
return "ayame.screen.warningscreen.settingsscreen.title";
}

@Override
public void onClose() {
if (minecraft.level == null) { // 玩家不是在世界内打开的话那肯定就是通过模组菜单打开的
minecraft.setScreen(lastScreen);
} else {
minecraft.setScreen(null);
}
}
}
2 changes: 1 addition & 1 deletion common/src/main/resources/assets/ayame/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"ayame.screen.warningscreen.modelselectscreen.title": "Select Model",
"ayame.screen.warningscreen.settingsscreen.title": "Ayame Settings",
"ayame.button.opendir.tooltip": "Open Model Folder\n(Place model files here)",
"ayame.widget.searchBarEditBox": "Search..."
"ayame.widget.searchBox": "Search..."
}
2 changes: 1 addition & 1 deletion common/src/main/resources/assets/ayame/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"ayame.screen.warningscreen.modelselectscreen.title": "选择模型",
"ayame.screen.warningscreen.settingsscreen.title": "Ayame 设置",
"ayame.button.opendir.tooltip": "打开模型文件夹\n(请将模型文件放在这里)",
"ayame.widget.searchBarEditBox": "搜索..."
"ayame.widget.searchBox": "搜索..."
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 49b2acf

Please sign in to comment.