Files
CustomNPC-Plus/src/main/java/noppes/npcs/client/gui/player/GuiDialogInteract.java
T

494 lines
16 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.client.gui.player;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
2022-05-30 04:51:30 -05:00
import net.minecraft.client.gui.ScaledResolution;
2018-09-13 12:03:28 -04:00
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.ResourceLocation;
import noppes.npcs.NoppesStringUtils;
import noppes.npcs.NoppesUtilPlayer;
import noppes.npcs.client.ClientProxy;
import noppes.npcs.client.NoppesUtil;
import noppes.npcs.client.TextBlockClient;
import noppes.npcs.client.controllers.MusicController;
import noppes.npcs.client.gui.util.GuiNPCInterface;
import noppes.npcs.client.gui.util.IGuiClose;
import noppes.npcs.constants.EnumOptionType;
import noppes.npcs.constants.EnumPlayerPacket;
import noppes.npcs.controllers.data.Dialog;
import noppes.npcs.controllers.data.DialogOption;
2018-09-13 12:03:28 -04:00
import noppes.npcs.entity.EntityNPCInterface;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
public class GuiDialogInteract extends GuiNPCInterface implements IGuiClose
{
private Dialog dialog;
private int selected = 0;
2022-05-30 04:51:30 -05:00
private List<TextBlockClient> lineBlocks = new ArrayList<TextBlockClient>();
2018-09-13 12:03:28 -04:00
private List<Integer> options = new ArrayList<Integer>();
private int rowStart = 0;
private int rowTotal = 0;
2022-05-30 04:51:30 -05:00
private ScaledResolution scaledResolution;
private int renderDialogType = 1; //0 - Instant (classic), 1 - Gradual
private String gradualText = "";
private int currentBlock = 0;
private int currentLine = 0;
private int gradualTextTime = 0;
private static int gradualTextSpeed = 10;
private boolean showPreviousBlocks = true;
2022-05-30 18:31:58 -05:00
private int textOffsetX, textOffsetY;
2022-05-30 04:51:30 -05:00
private int titleOffsetX, titleOffsetY;
private int dialogWidth = 500;
private int dialogHeight = 100;
private int dialogTextBottom = 0;
private int optionSpaceX, optionSpaceY;
private int optionOffsetX, optionOffsetY;
private int optionDefaultY = 175;
private int scrollY;
2018-09-13 12:03:28 -04:00
private ResourceLocation wheel;
private ResourceLocation[] wheelparts;
private ResourceLocation indicator;
private boolean isGrabbed = false;
public GuiDialogInteract(EntityNPCInterface npc, Dialog dialog){
super(npc);
this.dialog = dialog;
appendDialog(dialog);
ySize = 238;
wheel = this.getResource("wheel.png");
indicator = this.getResource("indicator.png");
wheelparts = new ResourceLocation[]{getResource("wheel1.png"),getResource("wheel2.png"),getResource("wheel3.png"),
getResource("wheel4.png"),getResource("wheel5.png"),getResource("wheel6.png")};
}
public void initGui(){
super.initGui();
isGrabbed = false;
grabMouse(dialog.showWheel);
guiTop = (height - ySize);
calculateRowHeight();
2022-05-30 04:51:30 -05:00
this.scaledResolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
this.drawDefaultBackground = dialog.darkenScreen;
2018-09-13 12:03:28 -04:00
}
public void grabMouse(boolean grab){
if(grab && !isGrabbed){
Minecraft.getMinecraft().mouseHelper.grabMouseCursor();
isGrabbed = true;
}
else if(!grab && isGrabbed){
Minecraft.getMinecraft().mouseHelper.ungrabMouseCursor();
isGrabbed = false;
}
}
public void drawScreen(int i, int j, float f){
GL11.glColor4f(1, 1, 1, 1);
if(!dialog.hideNPC){
float l = (guiLeft - 70);
float i1 = (guiTop + ySize);
GL11.glEnable(GL11.GL_COLOR_MATERIAL);
GL11.glPushMatrix();
GL11.glTranslatef(l, i1, 50F);
float zoomed = npc.height;
if(npc.width * 2 > zoomed)
zoomed = npc.width * 2;
zoomed = 2 / zoomed * 40;
GL11.glScalef(-zoomed, zoomed, zoomed);
GL11.glRotatef(180F, 0.0F, 0.0F, 1.0F);
float f2 = npc.renderYawOffset;
float f3 = npc.rotationYaw;
float f4 = npc.rotationPitch;
float f7 = npc.rotationYawHead;
float f5 = (float)(l) - i;
float f6 = (float)(i1 - 50) - j;
int rotation = npc.ai.orientation;
npc.ai.orientation = 0;
GL11.glRotatef(135F, 0.0F, 1.0F, 0.0F);
RenderHelper.enableStandardItemLighting();
GL11.glRotatef(-135F, 0.0F, 1.0F, 0.0F);
GL11.glRotatef(-(float)Math.atan(f6 / 80F) * 20F, 1.0F, 0.0F, 0.0F);
npc.renderYawOffset = 0;
npc.rotationYaw = (float)Math.atan(f5 / 80F) * 40F;
npc.rotationPitch = -(float)Math.atan(f6 / 80F) * 20F;
npc.prevRotationYawHead = npc.rotationYawHead = npc.rotationYaw;
GL11.glTranslatef(0.0F, npc.yOffset, 0.0F);
RenderManager.instance.playerViewY = 180F;
try{
RenderManager.instance.renderEntityWithPosYaw(npc, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F);
}
catch(Exception e){
}
npc.ai.orientation = rotation;
npc.renderYawOffset = f2;
npc.rotationYaw = f3;
npc.rotationPitch = f4;
npc.prevRotationYawHead = npc.rotationYawHead = f7;
GL11.glPopMatrix();
RenderHelper.disableStandardItemLighting();
GL11.glDisable(GL12.GL_RESCALE_NORMAL);
OpenGlHelper.setActiveTexture(OpenGlHelper.lightmapTexUnit);
GL11.glDisable(GL11.GL_TEXTURE_2D);
OpenGlHelper.setActiveTexture(OpenGlHelper.defaultTexUnit);
}
super.drawScreen(i, j, f);
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(770, 771, 1, 0);
GL11.glEnable(GL11.GL_ALPHA_TEST);
2022-05-30 04:51:30 -05:00
2018-09-13 12:03:28 -04:00
GL11.glPushMatrix();
2022-05-30 04:51:30 -05:00
GL11.glTranslatef(0.0F, 0.5f, 100.065F);
if (renderDialogType == 1) {
drawString(fontRendererObj, "Text Speed: " + gradualTextSpeed, 10, 10, 0xFFFFFF);
}
GL11.glPushMatrix();
GL11.glTranslatef(textOffsetX, 0.0F, 0.0F);
if (renderDialogType == 0) {
int count = 0;
for (TextBlockClient block : lineBlocks) {
int size = ClientProxy.Font.width(block.getName() + " ");
2022-05-30 18:31:58 -05:00
drawDialogString(block.getName() + " ", -4 - size, block.color, count, false);
2022-05-30 04:51:30 -05:00
for (IChatComponent line : block.lines) {
2022-05-30 18:31:58 -05:00
drawDialogString(line.getFormattedText(), 0, block.color, count, true);
2022-05-30 04:51:30 -05:00
count++;
}
count++;
}
} else {
int count = 0;
for (int pastBlock = 0; pastBlock < currentBlock; pastBlock++) {
TextBlockClient block = lineBlocks.get(pastBlock);
int size = ClientProxy.Font.width(block.getName() + " ");
2022-05-30 18:31:58 -05:00
drawDialogString(block.getName() + " ", -4 - size, block.color, count, false);
2022-05-30 04:51:30 -05:00
for (IChatComponent line : block.lines) {
2022-05-30 18:31:58 -05:00
drawDialogString(line.getFormattedText(), 0, block.color, count, true);
2022-05-30 04:51:30 -05:00
count++;
}
count++;
}
if (currentBlock < lineBlocks.size()) {
TextBlockClient block = lineBlocks.get(currentBlock);
int size = ClientProxy.Font.width(block.getName() + " ");
2022-05-30 18:31:58 -05:00
drawDialogString(block.getName() + " ", -4 - size, block.color, count, false);
2022-05-30 04:51:30 -05:00
for (int pastLine = 0; pastLine < currentLine; pastLine++) {
IChatComponent line = block.lines.get(pastLine);
2022-05-30 18:31:58 -05:00
drawDialogString(line.getFormattedText(), 0, block.color, count, true);
2022-05-30 04:51:30 -05:00
count++;
}
if (currentLine < block.lines.size()) {
IChatComponent line = block.lines.get(currentLine);
try {
if (gradualTextSpeed > 10 || gradualTextTime%(11 - gradualTextSpeed) == 0) {
int addChar = gradualTextSpeed > 10 ? gradualTextSpeed - 9 : 1;
gradualText += line.getFormattedText().substring(gradualText.length(), gradualText.length() + addChar);
if (gradualTextTime%3 == 0) {
NoppesUtil.clickSound();
}
}
} catch (IndexOutOfBoundsException exception) {
gradualText = line.getFormattedText();
}
2022-05-30 18:31:58 -05:00
drawDialogString(gradualText, 0, block.color, count, true);
2022-05-30 04:51:30 -05:00
if (gradualText.length() == line.getFormattedText().length()) {
gradualText = "";
currentLine++;
if (currentLine >= lineBlocks.get(currentBlock).lines.size()) {
currentBlock++;
}
}
gradualTextTime++;
}
}
}
GL11.glPopMatrix();
GL11.glPushMatrix();
if(!options.isEmpty()){
if(!dialog.showWheel)
drawLinedOptions(j);
else
drawWheel();
}
2018-09-13 12:03:28 -04:00
GL11.glPopMatrix();
2022-05-30 04:51:30 -05:00
GL11.glPopMatrix();
2018-09-13 12:03:28 -04:00
}
private int selectedX = 0;
private int selectedY = 0;
private void drawWheel(){
2022-05-30 18:31:58 -05:00
int yoffset = scaledResolution.getScaledHeight() - 50 + optionOffsetY;
dialogTextBottom = yoffset - 100;
2018-09-13 12:03:28 -04:00
GL11.glColor4f(1, 1, 1, 1);
mc.renderEngine.bindTexture(wheel);
2022-05-30 18:31:58 -05:00
drawTexturedModalRect((width/2) - 31 + optionOffsetX, yoffset, 0, 0, 63, 40);
2018-09-13 12:03:28 -04:00
selectedX += Mouse.getDX();
selectedY += Mouse.getDY();
int limit = 80;
2022-05-30 18:31:58 -05:00
if(selectedX > limit + optionOffsetX)
selectedX = limit + optionOffsetX;
if(selectedX < -limit + optionOffsetX)
selectedX = -limit + optionOffsetX;
2018-09-13 12:03:28 -04:00
if(selectedY > limit)
selectedY = limit;
if(selectedY < -limit)
selectedY = -limit;
selected = 1;
if(selectedY < -20)
selected++;
if(selectedY > 54)
selected--;
if(selectedX < 0)
selected += 3;
mc.renderEngine.bindTexture(wheelparts[selected]);
2022-05-30 18:31:58 -05:00
drawTexturedModalRect((width/2) - 31 + optionOffsetX,yoffset, 0, 0, 85, 55);
2018-09-13 12:03:28 -04:00
for(int slot:dialog.options.keySet()){
DialogOption option = dialog.options.get(slot);
if(option == null || option.optionType == EnumOptionType.Disabled)
continue;
int color = option.optionColor;
if(slot == (selected))
color = 0x838FD8;
//drawString(fontRenderer, option.title, width/2 -50 ,yoffset+ 162 + slot * 13 , color);
if(slot == 0)
2022-05-30 18:31:58 -05:00
drawString(fontRendererObj, option.title, width/2 + 13 + optionOffsetX,yoffset - 6 , color);
2018-09-13 12:03:28 -04:00
if(slot == 1)
2022-05-30 18:31:58 -05:00
drawString(fontRendererObj, option.title, width/2 + 33 + optionOffsetX,yoffset + 12 , color);
2018-09-13 12:03:28 -04:00
if(slot == 2)
2022-05-30 18:31:58 -05:00
drawString(fontRendererObj, option.title, width/2 + 27 + optionOffsetX,yoffset + 32 , color);
2018-09-13 12:03:28 -04:00
if(slot == 3)
2022-05-30 18:31:58 -05:00
drawString(fontRendererObj, option.title, width/2 - 13 + optionOffsetX - ClientProxy.Font.width(option.title),yoffset - 6 , color);
2018-09-13 12:03:28 -04:00
if(slot == 4)
2022-05-30 18:31:58 -05:00
drawString(fontRendererObj, option.title, width/2 - 33 + optionOffsetX - ClientProxy.Font.width(option.title),yoffset + 12 , color);
2018-09-13 12:03:28 -04:00
if(slot == 5)
2022-05-30 18:31:58 -05:00
drawString(fontRendererObj, option.title, width/2 - 27 + optionOffsetX - ClientProxy.Font.width(option.title),yoffset + 32 , color);
2018-09-13 12:03:28 -04:00
}
mc.renderEngine.bindTexture(indicator);
drawTexturedModalRect(width/2 + selectedX/4 - 2,yoffset + 16 - selectedY/6, 0, 0, 8, 8);
}
private void drawLinedOptions(int j){
2022-05-30 18:31:58 -05:00
int offset = scaledResolution.getScaledHeight() - (options.size() + 2) * ClientProxy.Font.height() + optionOffsetY - options.size() * optionSpaceY;
2022-05-30 04:51:30 -05:00
dialogTextBottom = offset - 100;
if(j >= offset){
2022-05-30 18:31:58 -05:00
int selected = options.size() - (j - offset + optionSpaceY) / (ClientProxy.Font.height() + optionSpaceY);
2018-09-13 12:03:28 -04:00
if(selected < options.size())
this.selected = selected;
}
if(selected >= options.size())
selected = 0;
if(selected < 0)
selected = 0;
2022-05-30 04:51:30 -05:00
drawHorizontalLine(guiLeft - 60, guiLeft + xSize + 120, offset, 0xFFFFFFFF);
2018-09-13 12:03:28 -04:00
for(int k = 0; k < options.size(); k++){
2022-05-30 04:51:30 -05:00
int id = options.get(options.size() - 1 - k);
2018-09-13 12:03:28 -04:00
DialogOption option = dialog.options.get(id);
2022-05-30 18:31:58 -05:00
int y = scaledResolution.getScaledHeight() - (k + 2) * ClientProxy.Font.height() + optionOffsetY + (-1 - k) * optionSpaceY;
2018-09-13 12:03:28 -04:00
if(selected == k){
drawString(fontRendererObj, ">", guiLeft - 60, y, 0xe0e0e0);
}
2022-05-30 04:51:30 -05:00
drawString(fontRendererObj, NoppesStringUtils.formatText(option.title, player, npc), guiLeft - 30 + optionSpaceX * k, y, option.optionColor);
2018-09-13 12:03:28 -04:00
}
}
2022-05-30 04:51:30 -05:00
2022-05-30 18:31:58 -05:00
private void drawDialogString(String text, int left, int color, int count, boolean mainDialogText){
2022-05-30 04:51:30 -05:00
int height = count - rowStart;
int y = (height * ClientProxy.Font.height()) + dialogTextBottom + scrollY;
if (y < dialogTextBottom - dialogHeight || y > dialogTextBottom + ClientProxy.Font.height() * 8) {
return;
}
2022-05-30 18:31:58 -05:00
int offsetX, offsetY;
if (mainDialogText) {
offsetX = textOffsetX;
offsetY = textOffsetY;
} else {
offsetX = titleOffsetX;
offsetY = titleOffsetY;
}
drawString(fontRendererObj, text, guiLeft + left + offsetX, y + offsetY, color);
2022-05-30 04:51:30 -05:00
}
2018-09-13 12:03:28 -04:00
public void drawString(FontRenderer fontRendererIn, String text, int x, int y, int color){
ClientProxy.Font.drawString(text, x, y, color);
}
@Override
public void keyTyped(char c, int i){
if(i == mc.gameSettings.keyBindForward.getKeyCode() || i == Keyboard.KEY_UP){
2022-05-30 04:51:30 -05:00
if (dialog.showWheel) {
selected--;
} else {
selected++;
}
2018-09-13 12:03:28 -04:00
}
if(i == mc.gameSettings.keyBindBack.getKeyCode() || i == Keyboard.KEY_DOWN){
2022-05-30 04:51:30 -05:00
if (dialog.showWheel) {
selected++;
} else {
selected--;
}
2018-09-13 12:03:28 -04:00
}
2022-05-30 04:51:30 -05:00
if(i == mc.gameSettings.keyBindForward.getKeyCode() || i == Keyboard.KEY_LEFT){
gradualTextSpeed--;
if (gradualTextSpeed < 1) {
gradualTextSpeed = 1;
}
}
if(i == mc.gameSettings.keyBindBack.getKeyCode() || i == Keyboard.KEY_RIGHT){
gradualTextSpeed++;
}
2022-05-30 04:53:17 -05:00
if (i == mc.gameSettings.keyBindBack.getKeyCode() || i == 201 && scrollY < rowStart * ClientProxy.Font.height()) {//Page up
2022-05-30 04:51:30 -05:00
scrollY += ClientProxy.Font.height() * 2;
}
2022-05-30 04:53:17 -05:00
if (i == mc.gameSettings.keyBindBack.getKeyCode() || i == 209 && scrollY > 0) {//Page down
2022-05-30 04:51:30 -05:00
scrollY -= ClientProxy.Font.height() * 2;
}
2018-09-13 12:03:28 -04:00
if(i == 28){
handleDialogSelection();
}
if (closeOnEsc && (i == 1 || isInventoryKey(i))){
NoppesUtilPlayer.sendData(EnumPlayerPacket.Dialog, dialog.id, -1);
closed();
close();
}
super.keyTyped(c, i);
}
@Override
public void mouseClicked(int i,int j,int k){
2022-05-30 04:51:30 -05:00
if(selected == -1 && options.isEmpty() || selected >= 0) {
scrollY = 0;
handleDialogSelection();
}
2018-09-13 12:03:28 -04:00
}
private void handleDialogSelection(){
int optionId = -1;
if(dialog.showWheel)
optionId = selected;
else if(!options.isEmpty())
2022-05-30 04:51:30 -05:00
optionId = options.get(options.size() - 1 - selected);
2018-09-13 12:03:28 -04:00
NoppesUtilPlayer.sendData(EnumPlayerPacket.Dialog, dialog.id, optionId);
if(dialog == null || !dialog.hasOtherOptions() || options.isEmpty()){
closed();
close();
return;
}
DialogOption option = dialog.options.get(optionId);
if(option == null || option.optionType != EnumOptionType.DialogOption){
closed();
close();
return;
}
2022-05-30 04:51:30 -05:00
if (!showPreviousBlocks) {
lineBlocks.clear();
}
lineBlocks.add(new TextBlockClient(player.getDisplayName(), option.title, dialogWidth, option.optionColor, player, npc));
gradualText = "";
currentBlock = lineBlocks.size()-1;
currentLine = 0;
gradualTextTime = 0;
2018-09-13 12:03:28 -04:00
calculateRowHeight();
NoppesUtil.clickSound();
}
private void closed(){
grabMouse(false);
NoppesUtilPlayer.sendData(EnumPlayerPacket.CheckQuestCompletion);
}
public void save() {
}
public void appendDialog(Dialog dialog) {
closeOnEsc = !dialog.disableEsc;
this.dialog = dialog;
this.options = new ArrayList<Integer>();
if(dialog.sound != null && !dialog.sound.isEmpty()){
MusicController.Instance.stopMusic();
MusicController.Instance.playSound(dialog.sound, (float)npc.posX, (float)npc.posY, (float)npc.posZ);
}
2022-05-30 04:51:30 -05:00
if (!showPreviousBlocks) {
lineBlocks.clear();
}
lineBlocks.add(new TextBlockClient(npc, dialog.text, dialogWidth, 0xe0e0e0, player, npc));
gradualText = "";
currentBlock = lineBlocks.size()-1;
currentLine = 0;
gradualTextTime = 0;
2018-09-13 12:03:28 -04:00
for(int slot:dialog.options.keySet()){
DialogOption option = dialog.options.get(slot);
if(option == null || option.optionType == EnumOptionType.Disabled)
continue;
options.add(slot);
}
calculateRowHeight();
grabMouse(dialog.showWheel);
}
private void calculateRowHeight(){
rowTotal = 0;
2022-05-30 04:51:30 -05:00
for(TextBlockClient block : lineBlocks){
2018-09-13 12:03:28 -04:00
rowTotal += block.lines.size() + 1;
}
int max = dialogHeight / ClientProxy.Font.height();
2022-05-30 04:51:30 -05:00
2018-09-13 12:03:28 -04:00
rowStart = rowTotal - max;
if(rowStart < 0)
rowStart = 0;
}
@Override
public void setClose(int i, NBTTagCompound data) {
grabMouse(false);
}
}