Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.82 KB | None | 0 0
  1. package fr.kerlann.gui;
  2.  
  3.  
  4.  
  5. import com.flansmod.client.FlansModResourceHandler;
  6. import com.flansmod.common.driveables.DriveableData;
  7. import com.flansmod.common.driveables.DriveableType;
  8. import com.flansmod.common.driveables.EntityVehicle;
  9. import com.flansmod.common.driveables.VehicleType;
  10. import com.flansmod.common.parts.PartType;
  11. import com.flansmod.common.types.EnumType;
  12. import fr.kerlann.main.DrawLife;
  13. import fr.kerlann.network.PacketHandler;
  14. import fr.kerlann.npc.EntityNPC;
  15. import fr.kerlann.network.message.PacketSpawn;
  16. import fr.kerlann.utils.GuiDrawUtils;
  17. import net.minecraft.client.Minecraft;
  18. import net.minecraft.client.gui.Gui;
  19. import net.minecraft.client.gui.GuiButton;
  20. import net.minecraft.client.gui.GuiScreen;
  21. import net.minecraft.client.gui.ScaledResolution;
  22. import net.minecraft.nbt.NBTTagCompound;
  23. import net.minecraft.util.ResourceLocation;
  24. import net.minecraft.util.math.MathHelper;
  25. import net.minecraft.util.text.TextFormatting;
  26. import net.minecraftforge.client.event.GuiScreenEvent;
  27. import net.minecraftforge.common.MinecraftForge;
  28. import org.apache.commons.lang3.StringUtils;
  29. import org.lwjgl.input.Mouse;
  30. import org.lwjgl.opengl.GL11;
  31.  
  32. import java.awt.*;
  33. import java.io.IOException;
  34. import java.text.NumberFormat;
  35. import java.util.*;
  36. import java.util.List;
  37.  
  38.  
  39.  
  40. public class GuiConcessionnaire extends GuiScreen {
  41. EntityNPC npc;
  42. private EntityVehicle vehicleToRender = null;
  43. public static Map<String, Integer> vehicleList = new HashMap();
  44. private int rotation;
  45.  
  46. private GuiButton buttonAcheter;
  47. private GuiBouton bouttonVehicle;
  48. private String vehiculename;
  49. private int scrollY = 0;
  50. private int maxScroll;
  51. private static ResourceLocation fond = new ResourceLocation("drawlife:textures/gui/concessionnaire/fond.png");
  52. private static ResourceLocation liste = new ResourceLocation("drawlife:textures/gui/concessionnaire/liste.png");
  53. private static ResourceLocation boutonachat = new ResourceLocation("drawlife:textures/gui/concessionnaire/bouton_acheter.png");
  54. private static ResourceLocation information = new ResourceLocation("drawlife:textures/gui/concessionnaire/infos.png");
  55. private String message;
  56.  
  57.  
  58. public GuiConcessionnaire(EntityNPC npc) {
  59. super();
  60. this.npc = npc;
  61. }
  62.  
  63. public void updateScreen() {
  64. super.updateScreen();
  65. }
  66.  
  67.  
  68.  
  69. public void drawScreen(int x, int y, float f) {
  70. GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  71. GuiDrawUtils.drawImage(0,0, fond, this.width, this.height);
  72. this.rotation++;
  73. double scaleX = this.width / 1920.0;
  74. double scaleY = this.height / 1080.0;
  75. x /= scaleX;
  76. y /= scaleY;
  77.  
  78. GL11.glScaled(scaleX, scaleY, 1.0);
  79. GL11.glPushMatrix();
  80. for (int k = 0; k < this.buttonList.size(); ++k) {
  81. final GuiButton bu = this.buttonList.get(k);
  82. if (bu.id == 100) {
  83. final int e = new ScaledResolution(this.mc).getScaleFactor();
  84. GL11.glEnable(3089);
  85. GL11.glScissor((int)(45 * e * scaleX), (int)((bu.y - 57) * e * scaleY), (int)(324 * e * scaleX), (int)(e * 845 * scaleY));
  86. GL11.glTranslated(0.0, -this.scrollY, 0.0);
  87. }
  88. bu.drawButton(this.mc, x, y, f);
  89. }
  90. GL11.glDisable(3089);
  91. GL11.glPopMatrix();
  92. if (this.vehicleList.size() != 0 && this.maxScroll != 0) {
  93. int relativeHeight = this.maxScroll % 846;
  94. if (this.maxScroll <= 0) {
  95. relativeHeight = 846;
  96. }
  97. int relativeY = 146 + this.scrollY * (846 - relativeHeight) / this.maxScroll;
  98. drawRect(369, relativeY, 386, relativeY + relativeHeight, -1);
  99. }
  100. GL11.glScaled(1.0 / scaleX, 1.0 / scaleY, 1.0);
  101. int middle = (int)(1050.0 * scaleX);
  102. if(this.vehiculename != null) {
  103. VehicleType type = VehicleType.getVehicle(this.vehiculename);
  104. if(type != null) {
  105. String s = StringUtils.abbreviate(type.name, 34);
  106. this.drawString(this.mc.fontRenderer, s, middle - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (892.0 * scaleY), 16777215);
  107. if (this.vehicleList.get(this.vehiculename) != null && this.vehicleList != null) {
  108. NumberFormat numberFormatter = NumberFormat.getNumberInstance(Locale.ROOT);
  109. s = "Prix : " + numberFormatter.format(Integer.valueOf(this.vehicleList.get(this.vehiculename))) + " \u20ac";
  110. this.drawString(this.mc.fontRenderer, s, middle - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (940.0 * scaleY), 16777215);
  111. }
  112. if (this.vehicleToRender != null) {
  113. GuiDrawUtils.drawImage(0,0, information, this.width, this.height);
  114.  
  115. String s2 = StringUtils.abbreviate(""+type.numCargoSlots, 34);
  116. this.drawString(this.mc.fontRenderer, s2, (int)(1570.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s2) / 2, (int) (228.0 * scaleY), 0xffffff);
  117. String s3 = StringUtils.abbreviate(""+(int)(type.maxThrottle * 1.0F * 100F) + " Km/h", 34);
  118. this.drawString(this.mc.fontRenderer, s3, (int)(1540.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (275 * scaleY), 0xffffff);
  119.  
  120. String s4 = StringUtils.abbreviate(""+(int) (10 * type.fuelTankSize / 82) + " L", 34);
  121. this.drawString(this.mc.fontRenderer, s4, (int)(1680.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (320 * scaleY), 0xffffff);
  122.  
  123. String s5 = StringUtils.abbreviate(""+type.seats.length, 34);
  124. this.drawString(this.mc.fontRenderer, s5, (int)(1620.0 * scaleX) - this.mc.fontRenderer.getStringWidth(s) / 2, (int) (368 * scaleY), 0xffffff);
  125.  
  126. }
  127. }
  128. }
  129.  
  130. this.drawModel(middle - 10);
  131. if (this.message != null) {
  132. if (this.message.contains("Achat")) {
  133. this.drawDefaultBackground();
  134. }
  135. GuiDrawUtils.renderCenteredTextScaled(this.message, 2, this.height - 2 - this.mc.fontRenderer.FONT_HEIGHT, 1D, 0xffffff);
  136. }
  137. }
  138.  
  139. public void initGui() {
  140. super.initGui();
  141. this.buttonList.add(buttonAcheter = new GuiButton(50, 1578, 928, 248, 76, "Acheter") {
  142. public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks){
  143. GL11.glColor3f(1.0f, 1.0f, 1.0f);
  144. if(visible) {
  145. Minecraft.getMinecraft().getTextureManager().bindTexture(boutonachat);
  146. Gui.drawScaledCustomSizeModalRect(this.x, this.y, 4.0f, 0.0f, 248, 76, this.width, this.height, 258.0f, 86.0f);
  147. }
  148. }
  149. });
  150. this.buttonList.add(new GuiButton(51, 1850, 27, 38, 42, "Fermer") {
  151. public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks){
  152. }
  153. });
  154. double scaleX = this.width / 1920.0;
  155. double scaleY = this.height / 1080.0;
  156. int i = 0, totalHeight=0;
  157. int buttonsHeight = 0;
  158. for(String vehic : vehicleList.keySet())
  159. {
  160. VehicleType type = VehicleType.getVehicle(vehic);
  161. if(type != null) {
  162. List<String> lines = trimTextToWidth(type.name, (int) (324.0 * scaleX));
  163. int height = (int) ((this.mc.fontRenderer.FONT_HEIGHT * lines.size() + 12) / scaleY);
  164. this.buttonList.add(new GuiBouton(i + 100, 45, 146 + totalHeight, 324, height, vehic) {
  165. @Override
  166. public void drawButton(Minecraft mc, int mx, int my, float partialTicks) {
  167. GL11.glColor3f(1.0f, 1.0f, 1.0f);
  168. Gui.drawRect(this.x, this.y, this.x + this.width, this.y + 3, Color.WHITE.getRGB());
  169. int y = this.y + 6;
  170. GL11.glScaled(1.0 / scaleX, 1.0 / scaleY, 1.0);
  171. if (lines.size() == 1) {
  172. 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);
  173. y += (int) (mc.fontRenderer.FONT_HEIGHT / scaleY);
  174. } else {
  175. int in = -1;
  176. for (final String line : lines) {
  177. 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);
  178. y += (int) (mc.fontRenderer.FONT_HEIGHT / scaleY);
  179. ++in;
  180. }
  181. }
  182. GL11.glScaled(scaleX, scaleY, 1.0);
  183. }
  184. });
  185. totalHeight += height;
  186. i++;
  187. }
  188. }
  189. scrollY = 0;
  190. maxScroll = totalHeight-220;
  191.  
  192. }
  193.  
  194. protected void keyTyped(final char typedChar, final int keyCode) throws IOException {
  195. super.keyTyped(typedChar, keyCode);
  196. }
  197.  
  198. public void moneyCheckedCallback(double currentPlayerMoney) {
  199. if (currentPlayerMoney >= this.vehicleList.get(this.vehiculename)) {
  200. PacketHandler.INSTANCE.sendToServer(new PacketSpawn(vehiculename, this.npc.getPosX(), this.npc.getPosY(), this.npc.getPosZ(), this.npc.getRotation(), this.vehicleList.get(this.vehiculename)));
  201. this.bouttonVehicle.select = false;
  202. this.vehiculename = null;
  203. this.mc.displayGuiScreen((GuiScreen)null);
  204. }
  205. else {
  206. this.message = TextFormatting.RED +"Vous n'avez pas assez d'argent !";
  207. this.buttonList.forEach(bu -> bu.enabled = true);
  208. }
  209. }
  210.  
  211. protected void actionPerformed(GuiButton button) throws IOException {
  212.  
  213. if (button.id == 50) {
  214. if (this.vehicleToRender != null) {
  215. this.buttonList.forEach(bu -> bu.enabled = false);
  216. VaultIconomySupportChannel.sendCurrentMoneyRq();
  217. this.message = TextFormatting.GOLD +"Achat en cours...";
  218. }
  219. } else if (button.id >= 100) {
  220. for (VehicleType v : VehicleType.types) {
  221. if (v.shortName == null) {
  222. continue;
  223. }
  224. if (v.shortName.equals(button.displayString)) {
  225. NBTTagCompound tag = new NBTTagCompound();
  226. tag.setString("Type", v.shortName);
  227. tag.setString("Engine", PartType.defaultEngines.get(EnumType.vehicle).shortName);
  228. this.vehicleToRender = new EntityVehicle(this.mc.world, 0.0, 0.0, 0.0, (VehicleType) DriveableType.getDriveable(v.shortName), new DriveableData(tag));
  229. break;
  230. }
  231. }
  232. this.bouttonVehicle = (GuiBouton)button;
  233. boolean oneSelected = this.bouttonVehicle.select;
  234. this.vehiculename = (oneSelected ? button.displayString : "");
  235. int lastClicked = oneSelected ? button.id : -1;
  236. for (int k = 0; k < this.buttonList.size(); ++k) {
  237. final GuiButton b = this.buttonList.get(k);
  238. if (b.id != lastClicked && b.id >= 100) {
  239. ((GuiBouton)b).select = false;
  240. }
  241. }
  242. this.buttonAcheter.enabled = oneSelected;
  243. this.buttonAcheter.visible = oneSelected;
  244. }
  245. else if (button.id == 51) {
  246. this.mc.player.closeScreen();
  247. }
  248. super.actionPerformed(button);
  249. }
  250.  
  251. public void onGuiClosed() {
  252. }
  253.  
  254. public boolean doesGuiPauseGame() {
  255. return false;
  256. }
  257.  
  258. public void drawModel(int par0) {
  259. if (this.vehicleToRender != null && this.vehicleToRender.getDriveableType() != null) {
  260. GL11.glPushMatrix();
  261. GL11.glTranslatef((float)par0, (float)(this.height / 2 + 35), 100.0f);
  262. 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);
  263. GL11.glScalef(5.0f, 5.0f, 5.0f);
  264. GL11.glRotatef(180.0f, 0.0f, 0.1f, 1.0f);
  265. GL11.glRotatef(0.0f, 1.0f, 0.0f, 0.0f);
  266. GL11.glRotatef(this.rotation / 1.0f, 0.0f, 20.0f, 0.0f);
  267. this.mc.renderEngine.bindTexture(FlansModResourceHandler.getTexture(this.vehicleToRender.getVehicleType()));
  268. if (this.vehicleToRender.getDriveableType().model != null) {
  269. this.vehicleToRender.getDriveableType().model.render(this.vehicleToRender.getDriveableType());
  270. }
  271. GL11.glDisable(2929);
  272. GL11.glDisable(3008);
  273. GL11.glPopMatrix();
  274. }
  275. }
  276.  
  277. public void handleMouseInput() throws IOException {
  278. super.handleMouseInput();
  279. if (this.maxScroll > 0) {
  280. final int i = Mouse.getEventDWheel() / 10;
  281. if (i != 0) {
  282. final int slotH = 25;
  283. this.scrollY -= ((i > 0) ? slotH : (-slotH));
  284. this.scrollY = Math.max(Math.min(this.scrollY, this.maxScroll), 0);
  285. this.scrollY -= this.scrollY % slotH;
  286. }
  287. }
  288. }
  289.  
  290. protected void mouseClicked(int mx, int my, final int button) throws IOException{
  291. if (button == 0) {
  292. double scaleX = this.width / 1920.0;
  293. double scaleY = this.height / 1080.0;
  294. mx /= scaleX;
  295. my /= scaleY;
  296. for (int l = 0; l < this.buttonList.size(); ++l) {
  297. final GuiButton guibutton = this.buttonList.get(l);
  298. if (guibutton.mousePressed(this.mc, mx, my + ((guibutton.id >= 100) ? this.scrollY : 0))) {
  299. GuiScreenEvent.ActionPerformedEvent.Pre event = new GuiScreenEvent.ActionPerformedEvent.Pre((GuiScreen)this, guibutton, this.buttonList);
  300. if (MinecraftForge.EVENT_BUS.post(event)) {
  301. break;
  302. }
  303. event.getButton().playPressSound(this.mc.getSoundHandler());
  304. this.actionPerformed(event.getButton());
  305. if (this.equals(this.mc.currentScreen)) {
  306. MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.ActionPerformedEvent.Post(this, event.getButton(), this.buttonList));
  307. }
  308. }
  309. }
  310. }
  311. }
  312.  
  313.  
  314. public static List<String> trimTextToWidth(String text, int maxWidth)
  315. {
  316.  
  317. List<String> lines = new ArrayList<String>();
  318.  
  319. while(!text.isEmpty())
  320. {
  321. String rawTrim = Minecraft.getMinecraft().fontRenderer.trimStringToWidth(text, maxWidth);
  322.  
  323. String str = text.substring(MathHelper.clamp(rawTrim.length(), 0, text.length()), MathHelper.clamp(rawTrim.length() + 1, 0, text.length()));
  324. Character lastChar = rawTrim.isEmpty() ? null : rawTrim.charAt(rawTrim.length() - 1);
  325. Character nextChar = str.isEmpty() ? null : str.charAt(str.length() - 1);
  326.  
  327. int lastSpace = rawTrim.lastIndexOf(' ');
  328.  
  329. boolean flag = lastChar == null || lastChar == ' ' || nextChar == null || nextChar == ' ' || lastSpace == 0 || lastSpace == -1;
  330.  
  331. String line;
  332.  
  333. if(rawTrim.contains("\n") && (rawTrim.indexOf("\n") == 0 || rawTrim.charAt(rawTrim.indexOf("\n") - 1) != '\\'))
  334. {
  335. line = rawTrim.substring(0, rawTrim.indexOf("\n"));
  336. text = text.replaceFirst("\n", "");
  337. }
  338. else
  339. {
  340. if(flag)
  341. {
  342. line = rawTrim;
  343. }
  344. else
  345. {
  346. line = rawTrim.substring(0, MathHelper.clamp(lastSpace + 1, 0, rawTrim.length()));
  347. }
  348. }
  349.  
  350. if(line.isEmpty())
  351. {
  352. break;
  353. }
  354.  
  355. text = text.substring(line.length(), text.length());
  356. lines.add(line);
  357. }
  358.  
  359. if(lines.isEmpty())
  360. lines.add("");
  361.  
  362. return lines;
  363. }
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement