2020-03-13 22:05:12 +00:00
|
|
|
package com.thebrokenrail.sorcerycraft.client.gui;
|
2020-03-01 18:19:59 +00:00
|
|
|
|
|
|
|
import com.mojang.blaze3d.systems.RenderSystem;
|
2020-03-03 23:08:22 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.SorceryCraft;
|
2020-03-13 22:05:12 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.gui.CastingTableScreenHandler;
|
2020-03-01 18:19:59 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.packet.SelectSpellC2SPacket;
|
2020-03-08 17:45:15 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.spell.api.Spell;
|
2020-03-06 23:35:29 +00:00
|
|
|
import com.thebrokenrail.sorcerycraft.spell.util.SpellTag;
|
2020-03-01 18:19:59 +00:00
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
|
|
|
import net.minecraft.client.font.TextRenderer;
|
2020-03-13 22:05:12 +00:00
|
|
|
import net.minecraft.client.gui.screen.ingame.HandledScreen;
|
2020-03-01 18:19:59 +00:00
|
|
|
import net.minecraft.client.gui.widget.ButtonWidget;
|
|
|
|
import net.minecraft.entity.player.PlayerInventory;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.text.Text;
|
|
|
|
import net.minecraft.text.TranslatableText;
|
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import net.minecraft.util.math.MathHelper;
|
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
2020-03-13 22:05:12 +00:00
|
|
|
public class CastingTableScreen extends HandledScreen<CastingTableScreenHandler> {
|
2020-03-01 18:19:59 +00:00
|
|
|
private static final Identifier TEXTURE = new Identifier("textures/gui/container/villager2.png");
|
|
|
|
private int selectedIndex;
|
|
|
|
private int indexStartOffset;
|
|
|
|
private boolean scrolling;
|
|
|
|
|
2020-03-09 22:47:31 +00:00
|
|
|
public CastingTableScreen(CastingTableScreenHandler container, PlayerInventory inventory, Text title) {
|
2020-03-01 18:19:59 +00:00
|
|
|
super(container, inventory, title);
|
2020-03-09 22:47:31 +00:00
|
|
|
backgroundWidth = 276;
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void drawForeground(int mouseX, int mouseY) {
|
2020-03-09 22:47:31 +00:00
|
|
|
int titleY = backgroundHeight - 94;
|
|
|
|
textRenderer.draw(title.asFormattedString(), (float) (49 + this.backgroundWidth / 2 - textRenderer.getStringWidth(title.asFormattedString()) / 2), 6.0F, 4210752);
|
|
|
|
textRenderer.draw(playerInventory.getDisplayName().asFormattedString(), 107.0F, (float) titleY, 4210752);
|
2020-03-03 23:08:22 +00:00
|
|
|
String spells = new TranslatableText("container." + SorceryCraft.NAMESPACE + ".spells").getString();
|
2020-03-06 23:35:29 +00:00
|
|
|
textRenderer.draw(spells, (float) (5 - textRenderer.getStringWidth(spells) / 2 + 48), 6.0F, 4210752);
|
2020-03-01 18:19:59 +00:00
|
|
|
renderXPCost();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void drawBackground(float delta, int mouseX, int mouseY) {
|
|
|
|
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
|
2020-03-06 23:35:29 +00:00
|
|
|
assert client != null;
|
|
|
|
client.getTextureManager().bindTexture(TEXTURE);
|
2020-03-09 22:47:31 +00:00
|
|
|
int i = (width - backgroundWidth) / 2;
|
|
|
|
int j = (height - backgroundHeight) / 2;
|
2020-03-17 17:22:49 +00:00
|
|
|
drawTexture(i, j, getZOffset(), 0.0F, 0.0F, backgroundWidth, backgroundHeight, 256, 512);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void render(int mouseX, int mouseY, float delta) {
|
|
|
|
renderBackground();
|
|
|
|
super.render(mouseX, mouseY, delta);
|
|
|
|
drawMouseoverTooltip(mouseX, mouseY);
|
|
|
|
|
|
|
|
renderScrollbar();
|
|
|
|
renderItems();
|
|
|
|
|
|
|
|
for (WidgetButtonPage button : buttons) {
|
|
|
|
if (button.isHovered()) {
|
|
|
|
button.renderToolTip(mouseX, mouseY);
|
|
|
|
}
|
2020-03-09 22:47:31 +00:00
|
|
|
button.visible = button.index < handler.getRecipes().length;
|
2020-03-01 18:19:59 +00:00
|
|
|
if (button.visible) {
|
2020-03-09 22:47:31 +00:00
|
|
|
Spell spell = handler.getRecipes()[button.getIndex() + indexStartOffset];
|
2020-03-18 15:56:25 +00:00
|
|
|
button.setMessage(SpellTag.getTranslatedSpell(spell.getID(), spell.getLevel()).getString());
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void renderScrollbar() {
|
2020-03-09 22:47:31 +00:00
|
|
|
Spell[] spells = handler.getRecipes();
|
2020-03-06 23:35:29 +00:00
|
|
|
assert client != null;
|
|
|
|
client.getTextureManager().bindTexture(TEXTURE);
|
2020-03-09 22:47:31 +00:00
|
|
|
int i = (width - backgroundWidth) / 2;
|
|
|
|
int j = (height - backgroundHeight) / 2;
|
2020-03-11 00:47:27 +00:00
|
|
|
int k = spells.length - 7;
|
2020-03-01 18:19:59 +00:00
|
|
|
|
2020-03-11 13:06:10 +00:00
|
|
|
if (k > 0) {
|
2020-03-11 00:47:27 +00:00
|
|
|
int modifier = (int) (((float) indexStartOffset / k) * (1 + 139 - 27));
|
2020-03-17 17:22:49 +00:00
|
|
|
drawTexture(i + 94, j + 18 + modifier, getZOffset(), 0.0F, 199.0F, 6, 27, 256, 512);
|
2020-03-01 18:19:59 +00:00
|
|
|
} else {
|
2020-03-17 17:22:49 +00:00
|
|
|
drawTexture(i + 94, j + 18, getZOffset(), 6.0F, 199.0F, 6, 27, 256, 512);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void renderItems() {
|
2020-03-09 22:47:31 +00:00
|
|
|
int i = (width - backgroundWidth) / 2;
|
|
|
|
int j = (height - backgroundHeight) / 2;
|
2020-03-01 18:19:59 +00:00
|
|
|
int k = j + 16 + 1;
|
|
|
|
|
2020-03-09 22:47:31 +00:00
|
|
|
Spell[] spells = handler.getRecipes();
|
2020-03-01 18:19:59 +00:00
|
|
|
for (int x = 0; x < spells.length; x++) {
|
|
|
|
if (!canScroll(spells.length) || (x >= indexStartOffset && x < 7 + indexStartOffset)) {
|
|
|
|
ItemStack itemStack = spells[x].getItemCost();
|
|
|
|
itemRenderer.zOffset = 100.0F;
|
2020-03-11 00:47:27 +00:00
|
|
|
int y = k + 2;
|
2020-03-01 18:19:59 +00:00
|
|
|
|
2020-03-11 00:47:27 +00:00
|
|
|
itemRenderer.renderGuiItem(itemStack, i + 5 + 68, y);
|
|
|
|
itemRenderer.renderGuiItemOverlay(textRenderer, itemStack, i + 5 + 68, y);
|
2020-03-01 18:19:59 +00:00
|
|
|
itemRenderer.zOffset = 0.0F;
|
|
|
|
k += 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void renderXPCost() {
|
2020-03-09 22:47:31 +00:00
|
|
|
if (handler.getRecipes().length > 0) {
|
|
|
|
int cost = handler.getRecipes()[selectedIndex].getXPCost();
|
|
|
|
int color = 8453920;
|
2020-03-06 23:35:29 +00:00
|
|
|
assert client != null;
|
|
|
|
assert client.player != null;
|
2020-03-09 22:47:31 +00:00
|
|
|
String string = new TranslatableText("container.repair.cost", cost).getString();
|
|
|
|
if (!handler.canTakeResult(playerInventory.player)) {
|
|
|
|
color = 16736352;
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 22:47:31 +00:00
|
|
|
int x2 = backgroundWidth - 8;
|
2020-03-06 23:35:29 +00:00
|
|
|
int x1 = x2 - textRenderer.getStringWidth(string);
|
2020-03-01 18:19:59 +00:00
|
|
|
fill(x1, 65, x2, 77, 1325400064);
|
2020-03-09 22:47:31 +00:00
|
|
|
textRenderer.drawWithShadow(string, (float) x1, 67.0F, color);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean canScroll(int listSize) {
|
|
|
|
return listSize > 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean mouseScrolled(double d, double e, double amount) {
|
2020-03-09 22:47:31 +00:00
|
|
|
int i = handler.getRecipes().length;
|
2020-03-11 21:34:52 +00:00
|
|
|
if (canScroll(i)) {
|
2020-03-01 18:19:59 +00:00
|
|
|
int j = i - 7;
|
|
|
|
indexStartOffset = (int) ((double) indexStartOffset - amount);
|
|
|
|
indexStartOffset = MathHelper.clamp(indexStartOffset, 0, j);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
|
2020-03-09 22:47:31 +00:00
|
|
|
int i = handler.getRecipes().length;
|
2020-03-01 18:19:59 +00:00
|
|
|
if (scrolling) {
|
|
|
|
int j = y + 18;
|
|
|
|
int k = j + 139;
|
|
|
|
int l = i - 7;
|
|
|
|
float f = ((float) mouseY - (float) j - 13.5F) / ((float) (k - j) - 27.0F);
|
2020-03-02 22:11:37 +00:00
|
|
|
f = f * (float) l + 0.5F;
|
2020-03-01 18:19:59 +00:00
|
|
|
indexStartOffset = MathHelper.clamp((int) f, 0, l);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean mouseClicked(double mouseX, double mouseY, int button) {
|
|
|
|
scrolling = false;
|
2020-03-09 22:47:31 +00:00
|
|
|
int i = (width - backgroundWidth) / 2;
|
|
|
|
int j = (height - backgroundHeight) / 2;
|
|
|
|
if (canScroll(handler.getRecipes().length) && mouseX > (double) (i + 94) && mouseX < (double) (i + 94 + 6) && mouseY > (double) (j + 18) && mouseY <= (double) (j + 18 + 139 + 1)) {
|
2020-03-01 18:19:59 +00:00
|
|
|
scrolling = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.mouseClicked(mouseX, mouseY, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void syncRecipeIndex() {
|
2020-03-09 22:47:31 +00:00
|
|
|
handler.setIndex(selectedIndex);
|
2020-03-06 23:35:29 +00:00
|
|
|
assert client != null;
|
|
|
|
SelectSpellC2SPacket.send(client, selectedIndex);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private final WidgetButtonPage[] buttons = new WidgetButtonPage[7];
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void init() {
|
|
|
|
super.init();
|
2020-03-09 22:47:31 +00:00
|
|
|
int i = (width - backgroundWidth) / 2;
|
|
|
|
int j = (height - backgroundHeight) / 2;
|
2020-03-01 18:19:59 +00:00
|
|
|
int k = j + 16 + 2;
|
|
|
|
|
|
|
|
for (int l = 0; l < 7; ++l) {
|
|
|
|
buttons[l] = addButton(new WidgetButtonPage(i + 5, k, l, (buttonWidget) -> {
|
|
|
|
selectedIndex = ((WidgetButtonPage) buttonWidget).getIndex() + indexStartOffset;
|
|
|
|
syncRecipeIndex();
|
|
|
|
}));
|
|
|
|
k += 20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
|
|
private class WidgetButtonPage extends ButtonWidget {
|
|
|
|
final int index;
|
|
|
|
|
|
|
|
public WidgetButtonPage(int i, int j, int k, PressAction pressAction) {
|
|
|
|
super(i, j, 89, 20, "", pressAction);
|
|
|
|
index = k;
|
|
|
|
visible = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-11 00:47:27 +00:00
|
|
|
public void render(int mouseX, int mouseY, float delta) {
|
|
|
|
active = (index + CastingTableScreen.this.indexStartOffset) != CastingTableScreen.this.selectedIndex;
|
|
|
|
super.render(mouseX, mouseY, delta);
|
2020-03-01 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getIndex() {
|
|
|
|
return this.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void renderToolTip(int mouseX, int mouseY) {
|
2020-03-09 22:47:31 +00:00
|
|
|
if (hovered && handler.getRecipes().length > index + indexStartOffset && mouseX > this.x + 65) {
|
|
|
|
ItemStack itemStack = handler.getRecipes()[index + indexStartOffset].getItemCost();
|
2020-03-01 18:19:59 +00:00
|
|
|
if (!itemStack.isEmpty()) {
|
|
|
|
renderTooltip(itemStack, mouseX, mouseY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-06 23:35:29 +00:00
|
|
|
public void drawCenteredString(TextRenderer textRenderer, String str, int centerX, int y, int color) {
|
2020-03-01 18:19:59 +00:00
|
|
|
drawString(textRenderer, str, x + 5, y, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|