Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.kerlann.gui;
- import com.flansmod.client.FlansModResourceHandler;
- import com.flansmod.common.driveables.DriveableData;
- import com.flansmod.common.driveables.DriveableType;
- import com.flansmod.common.driveables.EntityVehicle;
- import com.flansmod.common.driveables.VehicleType;
- import com.flansmod.common.parts.PartType;
- import com.flansmod.common.types.EnumType;
- import fr.kerlann.main.DrawLife;
- import fr.kerlann.network.PacketHandler;
- import fr.kerlann.npc.EntityNPC;
- import fr.kerlann.network.message.PacketSpawn;
- import fr.kerlann.utils.GuiDrawUtils;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.gui.Gui;
- import net.minecraft.client.gui.GuiButton;
- import net.minecraft.client.gui.GuiScreen;
- import net.minecraft.client.gui.ScaledResolution;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.util.ResourceLocation;
- import net.minecraft.util.math.MathHelper;
- import net.minecraft.util.text.TextFormatting;
- import net.minecraftforge.client.event.GuiScreenEvent;
- import net.minecraftforge.common.MinecraftForge;
- import org.apache.commons.lang3.StringUtils;
- import org.lwjgl.input.Mouse;
- import org.lwjgl.opengl.GL11;
- import java.awt.*;
- import java.io.IOException;
- import java.text.NumberFormat;
- import java.util.*;
- import java.util.List;
- public class GuiConcessionnaire extends GuiScreen {
- EntityNPC npc;
- private EntityVehicle vehicleToRender = null;
- public static Map<String, Integer> vehicleList = new HashMap();
- private int rotation;
- private GuiButton buttonAcheter;
- private GuiBouton bouttonVehicle;
- private String vehiculename;
- private int scrollY = 0;
- private int maxScroll;
- private static ResourceLocation fond = new ResourceLocation("drawlife:textures/gui/concessionnaire/fond.png");
- private static ResourceLocation liste = new ResourceLocation("drawlife:textures/gui/concessionnaire/liste.png");
- private static ResourceLocation boutonachat = new ResourceLocation("drawlife:textures/gui/concessionnaire/bouton_acheter.png");
- private static ResourceLocation information = new ResourceLocation("drawlife:textures/gui/concessionnaire/infos.png");
- private String message;
- public GuiConcessionnaire(EntityNPC npc) {
- super();
- this.npc = npc;
- }
- public void updateScreen() {
- super.updateScreen();
- }
- public void drawScreen(int x, int y, float f) {
- GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
- GuiDrawUtils.drawImage(0,0, fond, this.width, this.height);
- this.rotation++;
- double scaleX = this.width / 1920.0;
- double scaleY = this.height / 1080.0;
- x /= scaleX;
- y /= scaleY;
- GL11.glScaled(scaleX, scaleY, 1.0);
- GL11.glPushMatrix();
- for (int k = 0; k < this.buttonList.size(); ++k) {
- final GuiButton bu = this.buttonList.get(k);
- if (bu.id == 100) {
- final int e = new ScaledResolution(this.mc).getScaleFactor();
- GL11.glEnable(3089);
- GL11.glScissor((int)(45 * e * scaleX), (int)((bu.y - 57) * e * scaleY), (int)(324 * e * scaleX), (int)(e * 845 * scaleY));
- GL11.glTranslated(0.0, -this.scrollY, 0.0);
- }
- bu.drawButton(this.mc, x, y, f);
- }
- GL11.glDisable(3089);
- GL11.glPopMatrix();
- if (this.vehicleList.size() != 0 && this.maxScroll != 0) {
- int relativeHeight = this.maxScroll % 846;
- if (this.maxScroll <= 0) {
- relativeHeight = 846;
- }
- int relativeY = 146 + this.scrollY * (846 - relativeHeight) / this.maxScroll;
- drawRect(369, relativeY, 386, relativeY + relativeHeight, -1);
- }
- GL11.glScaled(1.0 / scaleX, 1.0 / scaleY, 1.0);
- int middle = (int)(1050.0 * scaleX);
- if(this.vehiculename != null) {
- VehicleType type = VehicleType.getVehicle(this.vehiculename);
- if(type != null) {
- String s = StringUtils.abbreviate(type.name, 34);
- this.drawString(this.mc.fontRenderer, s, middle - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (892.0 * scaleY), 16777215);
- if (this.vehicleList.get(this.vehiculename) != null && this.vehicleList != null) {
- NumberFormat numberFormatter = NumberFormat.getNumberInstance(Locale.ROOT);
- s = "Prix : " + numberFormatter.format(Integer.valueOf(this.vehicleList.get(this.vehiculename))) + " \u20ac";
- this.drawString(this.mc.fontRenderer, s, middle - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (940.0 * scaleY), 16777215);
- }
- if (this.vehicleToRender != null) {
- GuiDrawUtils.drawImage(0,0, information, this.width, this.height);
- String s2 = StringUtils.abbreviate(""+type.numCargoSlots, 34);
- this.drawString(this.mc.fontRenderer, s2, (int)(1570.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s2) / 2, (int) (228.0 * scaleY), 0xffffff);
- String s3 = StringUtils.abbreviate(""+(int)(type.maxThrottle * 1.0F * 100F) + " Km/h", 34);
- this.drawString(this.mc.fontRenderer, s3, (int)(1540.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (275 * scaleY), 0xffffff);
- String s4 = StringUtils.abbreviate(""+(int) (10 * type.fuelTankSize / 82) + " L", 34);
- this.drawString(this.mc.fontRenderer, s4, (int)(1680.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (320 * scaleY), 0xffffff);
- String s5 = StringUtils.abbreviate(""+type.seats.length, 34);
- this.drawString(this.mc.fontRenderer, s5, (int)(1620.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (368 * scaleY), 0xffffff);
- }
- }
- }
- this.drawModel(middle - 10);
- if (this.message != null) {
- if (this.message.contains("Achat")) {
- this.drawDefaultBackground();
- }
- GuiDrawUtils.renderCenteredTextScaled(this.message, 2, this.height - 2 - this.mc.fontRenderer.FONT_HEIGHT, 1D, 0xffffff);
- }
- }
- public void initGui() {
- super.initGui();
- this.buttonList.add(buttonAcheter = new GuiButton(50, 1578, 928, 248, 76, "Acheter") {
- public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks){
- GL11.glColor3f(1.0f, 1.0f, 1.0f);
- if(visible) {
- Minecraft.getMinecraft().getTextureManager().bindTexture(boutonachat);
- Gui.drawScaledCustomSizeModalRect(this.x, this.y, 4.0f, 0.0f, 248, 76, this.width, this.height, 258.0f, 86.0f);
- }
- }
- });
- this.buttonList.add(new GuiButton(51, 1850, 27, 38, 42, "Fermer") {
- public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks){
- }
- });
- double scaleX = this.width / 1920.0;
- double scaleY = this.height / 1080.0;
- int i = 0, totalHeight=0;
- int buttonsHeight = 0;
- for(String vehic : vehicleList.keySet())
- {
- VehicleType type = VehicleType.getVehicle(vehic);
- if(type != null) {
- List<String> lines = trimTextToWidth(type.name, (int) (324.0 * scaleX));
- int height = (int) ((this.mc.fontRenderer.FONT_HEIGHT * lines.size() + 12) / scaleY);
- this.buttonList.add(new GuiBouton(i + 100, 45, 146 + totalHeight, 324, height, vehic) {
- @Override
- public void drawButton(Minecraft mc, int mx, int my, float partialTicks) {
- GL11.glColor3f(1.0f, 1.0f, 1.0f);
- Gui.drawRect(this.x, this.y, this.x + this.width, this.y + 3, Color.WHITE.getRGB());
- int y = this.y + 6;
- GL11.glScaled(1.0 / scaleX, 1.0 / scaleY, 1.0);
- if (lines.size() == 1) {
- this.drawString(GuiConcessionnaire.this.fontRenderer, (this.select ? "§l" : "§f") + lines.get(0), (int) (scaleX * (this.x + this.width / 2) - GuiConcessionnaire.this.fontRenderer.getStringWidth((String) lines.get(0)) / 2), (int) (scaleY * (y + this.height / 2)) - GuiConcessionnaire.this.fontRenderer.FONT_HEIGHT / 2 - 3, 0xffffff);
- y += (int) (mc.fontRenderer.FONT_HEIGHT / scaleY);
- } else {
- int in = -1;
- for (final String line : lines) {
- this.drawString(GuiConcessionnaire.this.fontRenderer, (this.select ? "§l" : "§f") + line, (int) (scaleX * (this.x + this.width / 2) - GuiConcessionnaire.this.fontRenderer.getStringWidth(line) / 2), (int) (scaleY * (y + this.height / 2)) - GuiConcessionnaire.this.fontRenderer.FONT_HEIGHT * (lines.size() - in) / 2, 0xffffff);
- y += (int) (mc.fontRenderer.FONT_HEIGHT / scaleY);
- ++in;
- }
- }
- GL11.glScaled(scaleX, scaleY, 1.0);
- }
- });
- totalHeight += height;
- i++;
- }
- }
- scrollY = 0;
- maxScroll = totalHeight-220;
- }
- protected void keyTyped(final char typedChar, final int keyCode) throws IOException {
- super.keyTyped(typedChar, keyCode);
- }
- public void moneyCheckedCallback(double currentPlayerMoney) {
- if (currentPlayerMoney >= this.vehicleList.get(this.vehiculename)) {
- PacketHandler.INSTANCE.sendToServer(new PacketSpawn(vehiculename, this.npc.getPosX(), this.npc.getPosY(), this.npc.getPosZ(), this.npc.getRotation(), this.vehicleList.get(this.vehiculename)));
- this.bouttonVehicle.select = false;
- this.vehiculename = null;
- this.mc.displayGuiScreen((GuiScreen)null);
- }
- else {
- this.message = TextFormatting.RED +"Vous n'avez pas assez d'argent !";
- this.buttonList.forEach(bu -> bu.enabled = true);
- }
- }
- protected void actionPerformed(GuiButton button) throws IOException {
- if (button.id == 50) {
- if (this.vehicleToRender != null) {
- this.buttonList.forEach(bu -> bu.enabled = false);
- VaultIconomySupportChannel.sendCurrentMoneyRq();
- this.message = TextFormatting.GOLD +"Achat en cours...";
- }
- } else if (button.id >= 100) {
- for (VehicleType v : VehicleType.types) {
- if (v.shortName == null) {
- continue;
- }
- if (v.shortName.equals(button.displayString)) {
- NBTTagCompound tag = new NBTTagCompound();
- tag.setString("Type", v.shortName);
- tag.setString("Engine", PartType.defaultEngines.get(EnumType.vehicle).shortName);
- this.vehicleToRender = new EntityVehicle(this.mc.world, 0.0, 0.0, 0.0, (VehicleType) DriveableType.getDriveable(v.shortName), new DriveableData(tag));
- break;
- }
- }
- this.bouttonVehicle = (GuiBouton)button;
- boolean oneSelected = this.bouttonVehicle.select;
- this.vehiculename = (oneSelected ? button.displayString : "");
- int lastClicked = oneSelected ? button.id : -1;
- for (int k = 0; k < this.buttonList.size(); ++k) {
- final GuiButton b = this.buttonList.get(k);
- if (b.id != lastClicked && b.id >= 100) {
- ((GuiBouton)b).select = false;
- }
- }
- this.buttonAcheter.enabled = oneSelected;
- this.buttonAcheter.visible = oneSelected;
- }
- else if (button.id == 51) {
- this.mc.player.closeScreen();
- }
- super.actionPerformed(button);
- }
- public void onGuiClosed() {
- }
- public boolean doesGuiPauseGame() {
- return false;
- }
- public void drawModel(int par0) {
- if (this.vehicleToRender != null && this.vehicleToRender.getDriveableType() != null) {
- GL11.glPushMatrix();
- GL11.glTranslatef((float)par0, (float)(this.height / 2 + 35), 100.0f);
- GL11.glScalef(-50.0f * this.vehicleToRender.getDriveableType().modelScale / this.vehicleToRender.getDriveableType().cameraDistance, 50.0f * this.vehicleToRender.getDriveableType().modelScale / this.vehicleToRender.getDriveableType().cameraDistance, 50.0f * this.vehicleToRender.getDriveableType().modelScale / this.vehicleToRender.getDriveableType().cameraDistance);
- GL11.glScalef(5.0f, 5.0f, 5.0f);
- GL11.glRotatef(180.0f, 0.0f, 0.1f, 1.0f);
- GL11.glRotatef(0.0f, 1.0f, 0.0f, 0.0f);
- GL11.glRotatef(this.rotation / 1.0f, 0.0f, 20.0f, 0.0f);
- this.mc.renderEngine.bindTexture(FlansModResourceHandler.getTexture(this.vehicleToRender.getVehicleType()));
- if (this.vehicleToRender.getDriveableType().model != null) {
- this.vehicleToRender.getDriveableType().model.render(this.vehicleToRender.getDriveableType());
- }
- GL11.glDisable(2929);
- GL11.glDisable(3008);
- GL11.glPopMatrix();
- }
- }
- public void handleMouseInput() throws IOException {
- super.handleMouseInput();
- if (this.maxScroll > 0) {
- final int i = Mouse.getEventDWheel() / 10;
- if (i != 0) {
- final int slotH = 25;
- this.scrollY -= ((i > 0) ? slotH : (-slotH));
- this.scrollY = Math.max(Math.min(this.scrollY, this.maxScroll), 0);
- this.scrollY -= this.scrollY % slotH;
- }
- }
- }
- protected void mouseClicked(int mx, int my, final int button) throws IOException{
- if (button == 0) {
- double scaleX = this.width / 1920.0;
- double scaleY = this.height / 1080.0;
- mx /= scaleX;
- my /= scaleY;
- for (int l = 0; l < this.buttonList.size(); ++l) {
- final GuiButton guibutton = this.buttonList.get(l);
- if (guibutton.mousePressed(this.mc, mx, my + ((guibutton.id >= 100) ? this.scrollY : 0))) {
- GuiScreenEvent.ActionPerformedEvent.Pre event = new GuiScreenEvent.ActionPerformedEvent.Pre((GuiScreen)this, guibutton, this.buttonList);
- if (MinecraftForge.EVENT_BUS.post(event)) {
- break;
- }
- event.getButton().playPressSound(this.mc.getSoundHandler());
- this.actionPerformed(event.getButton());
- if (this.equals(this.mc.currentScreen)) {
- MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.ActionPerformedEvent.Post(this, event.getButton(), this.buttonList));
- }
- }
- }
- }
- }
- public static List<String> trimTextToWidth(String text, int maxWidth)
- {
- List<String> lines = new ArrayList<String>();
- while(!text.isEmpty())
- {
- String rawTrim = Minecraft.getMinecraft().fontRenderer.trimStringToWidth(text, maxWidth);
- String str = text.substring(MathHelper.clamp(rawTrim.length(), 0, text.length()), MathHelper.clamp(rawTrim.length() + 1, 0, text.length()));
- Character lastChar = rawTrim.isEmpty() ? null : rawTrim.charAt(rawTrim.length() - 1);
- Character nextChar = str.isEmpty() ? null : str.charAt(str.length() - 1);
- int lastSpace = rawTrim.lastIndexOf(' ');
- boolean flag = lastChar == null || lastChar == ' ' || nextChar == null || nextChar == ' ' || lastSpace == 0 || lastSpace == -1;
- String line;
- if(rawTrim.contains("\n") && (rawTrim.indexOf("\n") == 0 || rawTrim.charAt(rawTrim.indexOf("\n") - 1) != '\\'))
- {
- line = rawTrim.substring(0, rawTrim.indexOf("\n"));
- text = text.replaceFirst("\n", "");
- }
- else
- {
- if(flag)
- {
- line = rawTrim;
- }
- else
- {
- line = rawTrim.substring(0, MathHelper.clamp(lastSpace + 1, 0, rawTrim.length()));
- }
- }
- if(line.isEmpty())
- {
- break;
- }
- text = text.substring(line.length(), text.length());
- lines.add(line);
- }
- if(lines.isEmpty())
- lines.add("");
- return lines;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement