Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. package com.minecolonies.blockout.controls;
  2.  
  3. import com.minecolonies.blockout.Pane;
  4. import com.minecolonies.blockout.PaneParams;
  5. import net.minecraft.client.Minecraft;
  6. import net.minecraft.client.renderer.GlStateManager;
  7. import net.minecraft.util.ResourceLocation;
  8. import net.minecraft.util.Tuple;
  9.  
  10. import javax.imageio.ImageIO;
  11. import javax.imageio.ImageReader;
  12. import javax.imageio.stream.ImageInputStream;
  13. import java.io.IOException;
  14. import java.util.Iterator;
  15.  
  16. import static com.minecolonies.blockout.Log.getLogger;
  17.  
  18. /**
  19. * Simple image element.
  20. */
  21. public class Image extends Pane
  22. {
  23. public static final int MINECRAFT_DEFAULT_TEXTURE_MAP_SIZE = 256;
  24.  
  25. protected ResourceLocation resourceLocation;
  26. protected int imageOffsetX = 0;
  27. protected int imageOffsetY = 0;
  28. protected int imageWidth = 0;
  29. protected int imageHeight = 0;
  30. protected int mapWidth = MINECRAFT_DEFAULT_TEXTURE_MAP_SIZE;
  31. protected int mapHeight = MINECRAFT_DEFAULT_TEXTURE_MAP_SIZE;
  32.  
  33. /**
  34. * Default Constructor.
  35. */
  36. public Image()
  37. {
  38. super();
  39. }
  40.  
  41. /**
  42. * Constructor used by the xml loader.
  43. *
  44. * @param params PaneParams loaded from the xml.
  45. */
  46. public Image(final PaneParams params)
  47. {
  48. super(params);
  49. final String source = params.getStringAttribute("source", null);
  50. if (source != null)
  51. {
  52. resourceLocation = new ResourceLocation(source);
  53. loadMapDimensions();
  54. }
  55.  
  56. PaneParams.SizePair size = params.getSizePairAttribute("imageoffset", null, null);
  57. if (size != null)
  58. {
  59. imageOffsetX = size.getX();
  60. imageOffsetY = size.getY();
  61. }
  62.  
  63. size = params.getSizePairAttribute("imagesize", null, null);
  64. if (size != null)
  65. {
  66. imageWidth = size.getX();
  67. imageHeight = size.getY();
  68. }
  69. }
  70.  
  71. private void loadMapDimensions()
  72. {
  73. final Tuple<Integer, Integer> dimensions = getImageDimensions(resourceLocation);
  74. mapWidth = 256;//dimensions.getFirst();
  75. mapHeight = 256;//dimensions.getSecond();
  76. }
  77.  
  78. /**
  79. * Load and image from a {@link ResourceLocation} and return a {@link Tuple} containing its width and height.
  80. *
  81. * @param resourceLocation The {@link ResourceLocation} pointing to the image.
  82. * @return Width and height.
  83. */
  84. public static Tuple<Integer, Integer> getImageDimensions(final ResourceLocation resourceLocation)
  85. {
  86. int width = 0;
  87. int height = 0;
  88.  
  89. final Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix("png");
  90. if (it.hasNext())
  91. {
  92. final ImageReader reader = it.next();
  93. try (ImageInputStream stream = ImageIO.createImageInputStream(Minecraft.getMinecraft().getResourceManager().getResource(resourceLocation).getInputStream()))
  94. {
  95. reader.setInput(stream);
  96. width = reader.getWidth(reader.getMinIndex());
  97. height = reader.getHeight(reader.getMinIndex());
  98. }
  99. catch (final IOException e)
  100. {
  101. getLogger().warn(e);
  102. }
  103. finally
  104. {
  105. reader.dispose();
  106. }
  107. }
  108.  
  109. return new Tuple<>(width, height);
  110. }
  111.  
  112. /**
  113. * Set the image.
  114. *
  115. * @param source String path.
  116. */
  117. public void setImage(final String source)
  118. {
  119. setImage(source, 0, 0, 0, 0);
  120. }
  121.  
  122. /**
  123. * Set the image.
  124. *
  125. * @param source String path.
  126. * @param offsetX image x offset.
  127. * @param offsetY image y offset.
  128. * @param w image width.
  129. * @param h image height.
  130. */
  131. public void setImage(final String source, final int offsetX, final int offsetY, final int w, final int h)
  132. {
  133. setImage((source != null) ? new ResourceLocation(source) : null, offsetX, offsetY, w, h);
  134. }
  135.  
  136. /**
  137. * Set the image.
  138. *
  139. * @param loc ResourceLocation for the image.
  140. * @param offsetX image x offset.
  141. * @param offsetY image y offset.
  142. * @param w image width.
  143. * @param h image height.
  144. */
  145. public void setImage(final ResourceLocation loc, final int offsetX, final int offsetY, final int w, final int h)
  146. {
  147. resourceLocation = loc;
  148. imageOffsetX = offsetX;
  149. imageOffsetY = offsetY;
  150. imageWidth = w;
  151. imageHeight = h;
  152.  
  153. loadMapDimensions();
  154. }
  155.  
  156. /**
  157. * Set the image.
  158. *
  159. * @param loc ResourceLocation for the image.
  160. */
  161. public void setImage(final ResourceLocation loc)
  162. {
  163. setImage(loc, 0, 0, 0, 0);
  164. }
  165.  
  166. /**
  167. * Draw this image on the GUI.
  168. *
  169. * @param mx Mouse x (relative to parent)
  170. * @param my Mouse y (relative to parent)
  171. */
  172. @Override
  173. protected void drawSelf(final int mx, final int my)
  174. {
  175. // Some other texture must need to be ticked, I tried ticking the current one.
  176. // This fixes the problem, even if you put it after the draw call. So I guess I'll keep it.
  177.  
  178. GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
  179.  
  180. this.mc.getTextureManager().bindTexture(resourceLocation);
  181.  
  182. //Draw
  183. drawModalRectWithCustomSizedTexture(x, y,
  184. imageOffsetX, imageOffsetY,
  185. imageWidth != 0 ? imageWidth : getWidth(),
  186. imageHeight != 0 ? imageHeight : getHeight(),
  187. mapWidth, mapHeight);
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement