From e0ed5cd5cdfa1b7d587d6f1026efcb3fdb3852ef Mon Sep 17 00:00:00 2001 From: Karashok-Leo <1010177289@qq.com> Date: Thu, 25 Jul 2024 00:25:59 +0800 Subject: [PATCH] Adapt to any GUI Scale Add example dir --- build.gradle | 12 ++++- .../528c12981bcc19b42fa4e0892ced347b8285ecbc | 12 ++--- .../data/loot-bag/loot-bag/bag/optional.json | 0 .../data/loot-bag/loot-bag/bag/random.json | 0 .../data/loot-bag/loot-bag/bag/single.json | 0 .../data/loot-bag/loot-bag/content/beef.json | 0 .../loot-bag/loot-bag/content/creeper.json | 0 .../loot-bag/content/diamond_sword.json | 0 .../loot-bag/loot-bag/content/skeleton.json | 0 .../data/loot-bag/loot-bag/content/stone.json | 0 .../loot-bag/loot-bag/content/zombie.json | 0 example/pack.mcmeta | 6 +++ .../lootbag/client/screen/LootBagScreen.java | 48 +++++++++++++------ .../client/screen/OptionalLootBagScreen.java | 16 ++++--- .../client/screen/RandomLootBagScreen.java | 4 +- .../client/screen/SingleLootBagScreen.java | 4 +- 16 files changed, 71 insertions(+), 31 deletions(-) rename {src/main/generated => example}/.cache/528c12981bcc19b42fa4e0892ced347b8285ecbc (93%) rename {src/main/generated => example}/data/loot-bag/loot-bag/bag/optional.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/bag/random.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/bag/single.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/content/beef.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/content/creeper.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/content/diamond_sword.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/content/skeleton.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/content/stone.json (100%) rename {src/main/generated => example}/data/loot-bag/loot-bag/content/zombie.json (100%) create mode 100644 example/pack.mcmeta diff --git a/build.gradle b/build.gradle index 2a1e22e..18ce8e4 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,17 @@ sourceSets { } fabricApi { - configureDataGeneration(setting -> setting.setAddToResources(false)) + configureDataGeneration(setting -> { + setting.setOutputDirectory(project.file("example")) + setting.setAddToResources(false) + }) +} + +runDatagen.doLast { + copy { + from("example") + into("run/global_packs/required_data/loot_bag") + } } repositories { diff --git a/src/main/generated/.cache/528c12981bcc19b42fa4e0892ced347b8285ecbc b/example/.cache/528c12981bcc19b42fa4e0892ced347b8285ecbc similarity index 93% rename from src/main/generated/.cache/528c12981bcc19b42fa4e0892ced347b8285ecbc rename to example/.cache/528c12981bcc19b42fa4e0892ced347b8285ecbc index 72d70d6..03a6f41 100644 --- a/src/main/generated/.cache/528c12981bcc19b42fa4e0892ced347b8285ecbc +++ b/example/.cache/528c12981bcc19b42fa4e0892ced347b8285ecbc @@ -1,10 +1,10 @@ -// 1.20.1 2024-07-24T20:03:33.1058387 Loot Bag/loot-bag -12a23588d255b90acb935f301064c8d84c733d7b data\loot-bag\loot-bag\content\beef.json +// 1.20.1 2024-07-24T23:43:04.917187 Loot Bag/loot-bag 6a9f6e5e8a45784071dd283cf395423e48f8784d data\loot-bag\loot-bag\bag\optional.json -f54b6c66159d4046b810721a3cff5f20e8485454 data\loot-bag\loot-bag\content\stone.json -75f6a64930ab5fd893c7e6fe5ab1b263b5aa2c00 data\loot-bag\loot-bag\content\diamond_sword.json f0d082404dd7abeb49ec459520530a42a62653b6 data\loot-bag\loot-bag\bag\random.json -d30b0171c46313065c576b20b9efd79857e0d660 data\loot-bag\loot-bag\content\creeper.json -9e9c7d432b9aee015f45667b952ac623e1191ddc data\loot-bag\loot-bag\bag\single.json e3dea042295d513a01f0c7648265dc8a4be4510a data\loot-bag\loot-bag\content\zombie.json +f54b6c66159d4046b810721a3cff5f20e8485454 data\loot-bag\loot-bag\content\stone.json +9e9c7d432b9aee015f45667b952ac623e1191ddc data\loot-bag\loot-bag\bag\single.json +d30b0171c46313065c576b20b9efd79857e0d660 data\loot-bag\loot-bag\content\creeper.json +12a23588d255b90acb935f301064c8d84c733d7b data\loot-bag\loot-bag\content\beef.json ae169ee46def1a4c68dbd40ae8d08b8dd11a7ba8 data\loot-bag\loot-bag\content\skeleton.json +75f6a64930ab5fd893c7e6fe5ab1b263b5aa2c00 data\loot-bag\loot-bag\content\diamond_sword.json diff --git a/src/main/generated/data/loot-bag/loot-bag/bag/optional.json b/example/data/loot-bag/loot-bag/bag/optional.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/bag/optional.json rename to example/data/loot-bag/loot-bag/bag/optional.json diff --git a/src/main/generated/data/loot-bag/loot-bag/bag/random.json b/example/data/loot-bag/loot-bag/bag/random.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/bag/random.json rename to example/data/loot-bag/loot-bag/bag/random.json diff --git a/src/main/generated/data/loot-bag/loot-bag/bag/single.json b/example/data/loot-bag/loot-bag/bag/single.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/bag/single.json rename to example/data/loot-bag/loot-bag/bag/single.json diff --git a/src/main/generated/data/loot-bag/loot-bag/content/beef.json b/example/data/loot-bag/loot-bag/content/beef.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/content/beef.json rename to example/data/loot-bag/loot-bag/content/beef.json diff --git a/src/main/generated/data/loot-bag/loot-bag/content/creeper.json b/example/data/loot-bag/loot-bag/content/creeper.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/content/creeper.json rename to example/data/loot-bag/loot-bag/content/creeper.json diff --git a/src/main/generated/data/loot-bag/loot-bag/content/diamond_sword.json b/example/data/loot-bag/loot-bag/content/diamond_sword.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/content/diamond_sword.json rename to example/data/loot-bag/loot-bag/content/diamond_sword.json diff --git a/src/main/generated/data/loot-bag/loot-bag/content/skeleton.json b/example/data/loot-bag/loot-bag/content/skeleton.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/content/skeleton.json rename to example/data/loot-bag/loot-bag/content/skeleton.json diff --git a/src/main/generated/data/loot-bag/loot-bag/content/stone.json b/example/data/loot-bag/loot-bag/content/stone.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/content/stone.json rename to example/data/loot-bag/loot-bag/content/stone.json diff --git a/src/main/generated/data/loot-bag/loot-bag/content/zombie.json b/example/data/loot-bag/loot-bag/content/zombie.json similarity index 100% rename from src/main/generated/data/loot-bag/loot-bag/content/zombie.json rename to example/data/loot-bag/loot-bag/content/zombie.json diff --git a/example/pack.mcmeta b/example/pack.mcmeta new file mode 100644 index 0000000..4f00ebd --- /dev/null +++ b/example/pack.mcmeta @@ -0,0 +1,6 @@ +{ + "pack": { + "pack_format": 15, + "description": "Loot Bag Example" + } +} diff --git a/src/main/java/karashokleo/lootbag/client/screen/LootBagScreen.java b/src/main/java/karashokleo/lootbag/client/screen/LootBagScreen.java index 6580bdf..3335c0b 100644 --- a/src/main/java/karashokleo/lootbag/client/screen/LootBagScreen.java +++ b/src/main/java/karashokleo/lootbag/client/screen/LootBagScreen.java @@ -15,18 +15,13 @@ public abstract class LootBagScreen extends Screen { - private static final String TEXT_OPEN = "text.loot-bag.open"; - private static final int OPEN_WIDTH = 72; - private static final int OPEN_HEIGHT = 24; - private static final int OPEN_Y = 210; - private static final int TITLE_Y = 20; + private static final Text TEXT_OPEN = Text.translatable("text.loot-bag.open"); private static final int TITLE_COLOR = 0xffffff; - private static final int NAME_Y = 120; private static final int NAME_COLOR = 0xffffff; - private static final int DESC_Y = 140; private static final int DESC_COLOR = 0xffffff; private static final float ICON_SIZE = 64; - private static final float ICON_Y = 40; + private static final int OPEN_WIDTH = 72; + private static final int OPEN_HEIGHT = 24; protected final B bag; protected final int slot; protected ButtonWidget openButton; @@ -42,12 +37,37 @@ protected LootBagScreen(Text title, B bag, int slot) protected void init() { openButton = ButtonWidget - .builder(Text.translatable(TEXT_OPEN), button -> open()) - .dimensions((width - OPEN_WIDTH) / 2, OPEN_Y - OPEN_HEIGHT / 2, OPEN_WIDTH, OPEN_HEIGHT) + .builder(TEXT_OPEN, button -> open()) + .dimensions((width - OPEN_WIDTH) / 2, this.getOpenY() - OPEN_HEIGHT / 2, OPEN_WIDTH, OPEN_HEIGHT) .build(); addDrawableChild(openButton); } + protected int getTitleY() + { + return (int) (0.08F * height); + } + + protected float getIconY() + { + return 0.16F * height; + } + + protected int getNameY() + { + return (int) (0.24F * height + ICON_SIZE); + } + + protected int getDescY() + { + return this.getNameY() + 20; + } + + protected int getOpenY() + { + return (int) (0.84F * height); + } + @Override public boolean shouldPause() { @@ -67,18 +87,18 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) protected void drawTitle(DrawContext context) { - context.drawCenteredTextWithShadow(textRenderer, title, width / 2, TITLE_Y, TITLE_COLOR); + context.drawCenteredTextWithShadow(textRenderer, title, width / 2, this.getTitleY(), TITLE_COLOR); } protected void drawName(DrawContext context) { - context.drawCenteredTextWithShadow(textRenderer, getCurrentContent().getName().formatted(Formatting.BOLD), width / 2, NAME_Y, NAME_COLOR); + context.drawCenteredTextWithShadow(textRenderer, getCurrentContent().getName().formatted(Formatting.BOLD), width / 2, this.getNameY(), NAME_COLOR); } protected void drawDescription(DrawContext context) { for (int i = 0; i < getCurrentContent().getDescriptionLines(); i++) - context.drawCenteredTextWithShadow(textRenderer, getCurrentContent().getDesc(i), width / 2, DESC_Y + i * 15, DESC_COLOR); + context.drawCenteredTextWithShadow(textRenderer, getCurrentContent().getDesc(i), width / 2, this.getDescY() + i * 15, DESC_COLOR); } protected void drawIcon(DrawContext context, Content.Icon icon, float offsetX, float scale, float alpha) @@ -88,7 +108,7 @@ protected void drawIcon(DrawContext context, Content.Icon icon, float offsetX, f MatrixStack matrixStack = context.getMatrices(); matrixStack.push(); - matrixStack.translate((width - ICON_SIZE) / 2F, ICON_Y, 0); + matrixStack.translate((width - ICON_SIZE) / 2F, this.getIconY(), 0); matrixStack.translate(offsetX > 0 ? offsetX * 2 : offsetX, (1F - scale) / 2F * ICON_SIZE, 0); matrixStack.scale(scaleW, scaleW, scaleW); diff --git a/src/main/java/karashokleo/lootbag/client/screen/OptionalLootBagScreen.java b/src/main/java/karashokleo/lootbag/client/screen/OptionalLootBagScreen.java index afda2c7..2e58dce 100644 --- a/src/main/java/karashokleo/lootbag/client/screen/OptionalLootBagScreen.java +++ b/src/main/java/karashokleo/lootbag/client/screen/OptionalLootBagScreen.java @@ -11,32 +11,36 @@ public class OptionalLootBagScreen extends ScrollableLootBagScreen { - protected static final String TEXT_OPTIONAL = "text.loot-bag.optional_screen"; + protected static final Text TEXT_OPTIONAL = Text.translatable("text.loot-bag.optional_screen"); protected static final Identifier ARROW = LootBag.id("textures/gui/arrow.png"); protected static final int ARROW_WIDTH = 14; protected static final int ARROW_HEIGHT = 22; - protected static final int ARROW_X = 100; - protected static final int ARROW_Y = 210; + protected static final int ARROW_X_OFFSET = 100; protected ToggleButtonWidget prevArrow; protected ToggleButtonWidget nextArrow; public OptionalLootBagScreen(OptionalBag bag, int slot) { - super(Text.translatable(TEXT_OPTIONAL), bag, slot); + super(TEXT_OPTIONAL, bag, slot); } @Override protected void init() { super.init(); - prevArrow = new ToggleButtonWidget((width - ARROW_X - ARROW_WIDTH) / 2, ARROW_Y - ARROW_HEIGHT / 2, ARROW_WIDTH, ARROW_HEIGHT, true); + prevArrow = new ToggleButtonWidget((width - ARROW_X_OFFSET - ARROW_WIDTH) / 2, this.getArrowY() - ARROW_HEIGHT / 2, ARROW_WIDTH, ARROW_HEIGHT, true); prevArrow.setTextureUV(1, 1, ARROW_WIDTH + 2, ARROW_HEIGHT + 2, ARROW); - nextArrow = new ToggleButtonWidget((width + ARROW_X - ARROW_WIDTH) / 2, ARROW_Y - ARROW_HEIGHT / 2, ARROW_WIDTH, ARROW_HEIGHT, false); + nextArrow = new ToggleButtonWidget((width + ARROW_X_OFFSET - ARROW_WIDTH) / 2, this.getArrowY() - ARROW_HEIGHT / 2, ARROW_WIDTH, ARROW_HEIGHT, false); nextArrow.setTextureUV(1, 1, ARROW_WIDTH + 2, ARROW_HEIGHT + 2, ARROW); addDrawableChild(prevArrow); addDrawableChild(nextArrow); } + protected int getArrowY() + { + return this.getOpenY(); + } + @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { diff --git a/src/main/java/karashokleo/lootbag/client/screen/RandomLootBagScreen.java b/src/main/java/karashokleo/lootbag/client/screen/RandomLootBagScreen.java index 83fa883..81231a7 100644 --- a/src/main/java/karashokleo/lootbag/client/screen/RandomLootBagScreen.java +++ b/src/main/java/karashokleo/lootbag/client/screen/RandomLootBagScreen.java @@ -9,12 +9,12 @@ public class RandomLootBagScreen extends ScrollableLootBagScreen { - protected static final String TEXT_RANDOM = "text.loot-bag.random_screen"; + protected static final Text TEXT_RANDOM = Text.translatable("text.loot-bag.random_screen"); protected int tick; public RandomLootBagScreen(RandomBag bag, int slot) { - super(Text.translatable(TEXT_RANDOM), bag, slot); + super(TEXT_RANDOM, bag, slot); } @Override diff --git a/src/main/java/karashokleo/lootbag/client/screen/SingleLootBagScreen.java b/src/main/java/karashokleo/lootbag/client/screen/SingleLootBagScreen.java index 303e0ee..277b6cd 100644 --- a/src/main/java/karashokleo/lootbag/client/screen/SingleLootBagScreen.java +++ b/src/main/java/karashokleo/lootbag/client/screen/SingleLootBagScreen.java @@ -7,11 +7,11 @@ public class SingleLootBagScreen extends LootBagScreen { - private static final String TEXT_SINGLE = "text.loot-bag.single_screen"; + private static final Text TEXT_SINGLE = Text.translatable("text.loot-bag.single_screen"); public SingleLootBagScreen(SingleBag bag, int slot) { - super(Text.translatable(TEXT_SINGLE), bag, slot); + super(TEXT_SINGLE, bag, slot); } @Override