123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package me.km.networking;
- import java.util.ArrayList;
- import java.util.TreeMap;
- import me.km.KajetansMod;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.gui.Gui;
- import net.minecraft.client.renderer.GlStateManager;
- import net.minecraft.util.ResourceLocation;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- @SideOnly(Side.CLIENT)
- public class PlayerDisplayGui extends Gui
- {
- private static final ResourceLocation CUSTOM_ICONS = new ResourceLocation(KajetansMod.MODID, "textures/gui/icons.png");
-
- public final static PlayerDisplayGui INSTANCE = new PlayerDisplayGui(Minecraft.getMinecraft());
-
- private class IconStorage
- {
- private Icon icon;
- private int count;
-
- public IconStorage(int id, int count)
- {
- this.icon = Icon.getIcon(id);
- this.count = count;
- }
-
- public int getWidth()
- {
- return count * icon.getWidth() - count + 1;
- }
-
- public int getHeight()
- {
- return icon.getHeight();
- }
- }
-
- private class LineData
- {
- private ArrayList<String> list;
- private ArrayList<IconStorage> icons;
-
- public LineData(String s)
- {
- this.list = new ArrayList<>();
- this.icons = new ArrayList<>();
-
- int pos;
- int old = 0;
- while(true)
- {
- // icon format #X-Y- where X and Y are numbers
- pos = s.indexOf("#", old);
- if(pos == -1)
- {
- list.add(s.substring(old));
- break;
- }
- list.add(s.substring(old, pos));
- try
- {
- int first = s.indexOf("-", pos);
- int second = s.indexOf("-", first + 1);
- icons.add(new IconStorage(Integer.parseInt(s.substring(pos + 1, first)), Integer.parseInt(s.substring(first + 1, second))));
- old = second + 1;
- }
- catch(Exception ex)
- {
- list.add("FEHLER");
- break;
- }
- }
- }
-
- public int getWidth()
- {
- return list.stream().mapToInt(s -> mc.fontRenderer.getStringWidth(s)).sum() + icons.stream().mapToInt(i -> i.getWidth()).sum();
- }
-
- public int getHeight()
- {
- return Math.max(mc.fontRenderer.FONT_HEIGHT, icons.stream().mapToInt(i -> i.getHeight()).max().orElse(0) + 1);
- }
- }
-
- private TreeMap<Integer, LineData> strings;
- private Minecraft mc;
-
- public PlayerDisplayGui(Minecraft mc)
- {
- this.mc = mc;
- this.strings = new TreeMap<>();
- }
-
- public void add(int i, String s)
- {
- strings.put(i, new LineData(s));
- }
-
- public void remove(int i)
- {
- strings.remove(i);
- }
-
- public void clear()
- {
- strings.clear();
- }
-
- public void paint()
- {
- if(strings.isEmpty())
- {
- return;
- }
- int x;
- int y = 2;
-
- int width = 0;
- int height = 4;
-
- for(LineData data : strings.values())
- {
- height += data.getHeight();
- width = Math.max(data.getWidth(), width);
- }
-
- drawRect(0, 0, width + 4, height, 1342177280);
-
- String s;
- for(LineData data : strings.values())
- {
- x = 2;
- for(int i = 0; i < data.list.size(); i ++)
- {
- s = data.list.get(i);
- mc.fontRenderer.drawString(s, x, y + 1, 0xFFFFFF);
- if(!s.isEmpty())
- {
- x += mc.fontRenderer.getStringWidth(s);
- }
- else
- {
- x--;
- }
- if(i < data.icons.size())
- {
- x += paintIcons(data.icons.get(i), x, y);
- }
- }
- y += data.getHeight();
- }
- }
-
- private void bind(ResourceLocation res)
- {
- mc.getTextureManager().bindTexture(res);
- }
-
- private int paintIcons(IconStorage is, int x, int y)
- {
- bind(CUSTOM_ICONS);
- GlStateManager.enableBlend();
- Icon icon = is.icon;
- int width = icon.getWidth();
- int height = icon.getHeight();
- int offX = icon.getOffsetX();
- int offY = icon.getOffsetY();
- for(int i = 0; i < is.count; i++)
- {
- drawTexturedModalRect(x + width * i - i, y, offX, offY, width, height);
- }
- GlStateManager.disableBlend();
- return is.getWidth();
- }
- }
|