Skip to content

Commit

Permalink
Adapt to any GUI Scale
Browse files Browse the repository at this point in the history
Add example dir
  • Loading branch information
Karashok-Leo committed Jul 24, 2024
1 parent 248b7b1 commit e0ed5cd
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 31 deletions.
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions example/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 15,
"description": "Loot Bag Example"
}
}
48 changes: 34 additions & 14 deletions src/main/java/karashokleo/lootbag/client/screen/LootBagScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@

public abstract class LootBagScreen<B extends Bag> 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;
Expand All @@ -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()
{
Expand All @@ -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)
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,36 @@

public class OptionalLootBagScreen extends ScrollableLootBagScreen<OptionalBag>
{
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

public class RandomLootBagScreen extends ScrollableLootBagScreen<RandomBag>
{
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

public class SingleLootBagScreen extends LootBagScreen<SingleBag>
{
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
Expand Down

0 comments on commit e0ed5cd

Please sign in to comment.