Skip to content

Commit

Permalink
搜索栏完善坐标
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyRespawnanchor committed Oct 5, 2024
1 parent 1665e0f commit dc26d0e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 124 deletions.
29 changes: 29 additions & 0 deletions common/src/main/java/org/ayamemc/ayame/client/gui/Alignment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Custom player model mod. Powered by GeckoLib.
* Copyright (C) 2024 CrystalNeko, HappyRespawnanchor, pertaz(Icon Designer)
*
* This file is part of Ayame.
*
* Ayame is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ayame is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ayame. If not, see <https://www.gnu.org/licenses/>.
*/

package org.ayamemc.ayame.client.gui;

public enum Alignment {
CENTER,
LEFT,
RIGHT,
TOP,
BOTTOM,
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
/*
* Custom player model mod. Powered by GeckoLib.
* Copyright (C) 2024 CrystalNeko, HappyRespawnanchor, pertaz(Icon Designer)
*
* This file is part of Ayame.
*
* Ayame is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ayame is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ayame. If not, see <https://www.gnu.org/licenses/>.
*/

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

import com.mojang.blaze3d.systems.RenderSystem;
Expand All @@ -34,6 +14,7 @@
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 @@ -60,37 +41,29 @@ public AyameMainScreen(@Nullable Screen lastScreen) {
this.lastScreen = lastScreen;
}

/**
* 初始化屏幕方法,可以由子类覆盖
*/
@Override
protected void init() {
BlurWidget blurredBackgroundWidget = new BlurWidget(getCenterX(BACKGROUND_TEXTURE_WIDTH), getCenterY(BACKGROUND_TEXTURE_HEIGHT), BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
BlurWidget blurredBackgroundWidget = new BlurWidget(getCenteredX(BACKGROUND_TEXTURE_WIDTH), getCenteredY(BACKGROUND_TEXTURE_HEIGHT), BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
this.addRenderableOnly(blurredBackgroundWidget);
final int searchBarWidth = 208;
final int searchBarHeight = 20;

final int searchBarWidth = 112;
final int searchBarHeight = 23;
EditBox searchBarEditBox = new EditBox(
this.font,
getComponentCenterX(searchBarWidth),
getComponentTopY(searchBarHeight),
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
public void renderBackground(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
if (minecraft.level == null) {
//this.renderPanorama(guiGraphics, partialTick);
super.renderBackground(guiGraphics, mouseX, mouseY, partialTick);
renderBackgroundTexture(guiGraphics, mouseX, mouseY, partialTick);
} else {
Expand All @@ -100,35 +73,31 @@ public void renderBackground(GuiGraphics guiGraphics, int mouseX, int mouseY, fl

protected void renderBackgroundTexture(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
RenderSystem.enableBlend();
guiGraphics.blit(MENU_BACKGROUND_TEXTURE, getCenterX(BACKGROUND_TEXTURE_WIDTH), getCenterY(BACKGROUND_TEXTURE_HEIGHT), 0, 0, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
guiGraphics.blit(MENU_BACKGROUND_TEXTURE, getCenteredX(BACKGROUND_TEXTURE_WIDTH), getCenteredY(BACKGROUND_TEXTURE_HEIGHT), 0, 0, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
RenderSystem.disableBlend();
}


/**
* 渲染屏幕内容,包括背景边框
*/
@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
super.render(guiGraphics, mouseX, mouseY, delta);
RenderSystem.enableBlend();
guiGraphics.blit(MENU_BACKGROUND_OUTLINE_TEXTURE, getCenterX(BACKGROUND_TEXTURE_WIDTH), getCenterY(BACKGROUND_TEXTURE_HEIGHT), 0, 0, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
guiGraphics.blit(renderTopLayerResourceLocation(), getCenterX(BACKGROUND_TEXTURE_WIDTH), getCenterY(BACKGROUND_TEXTURE_HEIGHT), 0, 0, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
guiGraphics.blit(MENU_BACKGROUND_OUTLINE_TEXTURE, getCenteredX(BACKGROUND_TEXTURE_WIDTH), getCenteredY(BACKGROUND_TEXTURE_HEIGHT), 0, 0, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
guiGraphics.blit(renderTopLayerResourceLocation(), getCenteredX(BACKGROUND_TEXTURE_WIDTH), getCenteredY(BACKGROUND_TEXTURE_HEIGHT), 0, 0, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT, BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT);
RenderSystem.disableBlend();

WidgetSprites settingSprites = new WidgetSprites(
withAyameNamespace("settings"),
withAyameNamespace("settings_disabled"), // 似乎不会被使用
withAyameNamespace("settings_disabled"),
withAyameNamespace("settings_enabled_focused")
);
WidgetSprites opendirSprites = new WidgetSprites(
withAyameNamespace("opendir"),
withAyameNamespace("opendir"), // 似乎不会被使用
withAyameNamespace("opendir"),
withAyameNamespace("opendir_enabled_focused")
);
ImageButton settingsButton = new ImageButton(
getLeftAlignedX(BACKGROUND_TEXTURE_WIDTH, 0),
getBottomAlignedY(BACKGROUND_TEXTURE_HEIGHT, BUTTON_SIZE, 0),
getAlignedX(BACKGROUND_TEXTURE_WIDTH, BUTTON_SIZE, 0, Alignment.LEFT),
getAlignedY(BACKGROUND_TEXTURE_HEIGHT, BUTTON_SIZE, 0, Alignment.BOTTOM),
BUTTON_SIZE,
BUTTON_SIZE,
settingSprites,
Expand All @@ -139,8 +108,8 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta)
Component.empty()
);
ImageButton opendirButton = new ImageButton(
getLeftAlignedX(BACKGROUND_TEXTURE_WIDTH, 0),
getBottomAlignedY(BACKGROUND_TEXTURE_HEIGHT, BUTTON_SIZE, 90),
getAlignedX(BACKGROUND_TEXTURE_WIDTH, BUTTON_SIZE, 0, Alignment.LEFT),
getAlignedY(BACKGROUND_TEXTURE_HEIGHT, BUTTON_SIZE, 90, Alignment.BOTTOM),
BUTTON_SIZE,
BUTTON_SIZE,
opendirSprites,
Expand All @@ -154,108 +123,58 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta)

addRenderableWidget(settingsButton);
addRenderableWidget(opendirButton);
// if (this instanceof SettingsScreen) {
// addRenderableOnly(settingsButton);
// } else {
// addRenderableWidget(settingsButton);
// }

Component titleText = Component.translatable(setTranslatableTitle());

// 计算居中显示的 X 坐标
int centerX = getCenteredStringX(titleText);

// 渲染文本
guiGraphics.drawString(this.font, titleText, centerX, font.lineHeight, 0xFFFFFFFF, true);
}


/**
* 实现一个自定义的指定图层
*
* @return 你要传入的图片路径
*/
protected abstract @NotNull ResourceLocation renderTopLayerResourceLocation();

protected abstract @NotNull String setTranslatableTitle();

/**
* 获取指定宽度在屏幕中心的X坐标
*/
protected int getCenterX(int elementWidth) {
protected int getCenteredX(int elementWidth) {
return (this.width - elementWidth) / 2;
}

/**
* 获取指定高度在屏幕中心的Y坐标
*/
protected int getCenterY(int elementHeight) {
protected int getCenteredY(int elementHeight) {
return (this.height - elementHeight) / 2;
}

/**
* 获取元素在屏幕左边对齐的X坐标
*/
protected int getLeftAlignedX(int containerWidth, int margin) {
return getCenterX(containerWidth) + margin;
}

/**
* 获取元素在屏幕底部对齐的Y坐标
*/
protected int getBottomAlignedY(int containerHeight, int elementHeight, int margin) {
return getCenterY(containerHeight) + containerHeight - elementHeight - margin;
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;
}
}

protected int getCenteredStringX(Component text) {
int textWidth = this.font.width(text);
return (this.width - textWidth) / 2;
}

/**
* 获取与贴图无关的组件在屏幕中心的 X 坐标
*/
protected int getComponentCenterX(int componentWidth) {
return (this.width - componentWidth) / 2;
}

/**
* 获取与贴图无关的组件在屏幕中心的 Y 坐标
*/
protected int getComponentCenterY(int componentHeight) {
return (this.height - componentHeight) / 2;
}
/**
* 获取组件在屏幕顶部对齐的 Y 坐标
*/
protected int getComponentTopY(int margin) {
return margin;
}

/**
* 获取组件在屏幕底部对齐的 Y 坐标
*/
protected int getComponentBottomY(int componentHeight, int margin) {
return this.height - componentHeight - margin;
}

/**
* 获取组件在屏幕左边对齐的 X 坐标
*/
protected int getComponentLeftX(int margin) {
return margin;
}

/**
* 获取组件在屏幕右边对齐的 X 坐标
*/
protected int getComponentRightX(int componentWidth, int margin) {
return this.width - componentWidth - margin;
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;
}
}


@Override
public void onClose() {
minecraft.setScreen(lastScreen);
}
}
}
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 Model"
"ayame.widget.searchBarEditBox": "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.searchBarEditBox": "搜索..."
}

0 comments on commit dc26d0e

Please sign in to comment.