Skip to content
This repository has been archived by the owner on Jun 10, 2019. It is now read-only.

Commit

Permalink
Implement a super simple libGDX-like TextureAtlas (based on gdx-textu…
Browse files Browse the repository at this point in the history
…repacker) to make handling GUI graphics easier.
  • Loading branch information
BlayTheNinth committed Feb 20, 2015
1 parent 5844222 commit ead0b66
Show file tree
Hide file tree
Showing 47 changed files with 506 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,3 +15,4 @@
/*.iml
/out
/gradle.properties
/gdx-texturepacker.jar
Binary file added assets-raw/button_add.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_clipboard.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_delete.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_favorite.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_first.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_join.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_last.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_next.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_part.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_prev.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_remove.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_unfavorite.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_upload.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/button_zoom.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/icon_active.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/menu_cat.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/menu_friends.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/menu_screenshots.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/menu_servers.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/menu_settings.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/menu_twitch.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/tab_bg_bottomleft.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/tab_bg_bottomright.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/tab_bg_content.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/tab_bg_topright.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets-raw/tab_header_left.png
Binary file added assets-raw/tab_header_middle.png
Binary file added assets-raw/tab_header_right.png
29 changes: 29 additions & 0 deletions eiragui.gdxtp
@@ -0,0 +1,29 @@
name=eiragui
filename=
input=assets-raw
output=src\main\resources\assets\eirairc\gfx

alias=true
alphaThreshold=0
debug=false
duplicatePadding=true
edgePadding=true
fast=false
filterMag=Nearest
filterMin=Nearest
format=RGBA8888
ignoreBlankImages=true
jpegQuality=1.0
maxHeight=256
maxWidth=256
minHeight=256
minWidth=256
outputFormat=png
paddingX=0
paddingY=0
pot=true
rotation=false
stripWhitespaceX=false
stripWhitespaceY=false
wrapX=ClampToEdge
wrapY=ClampToEdge
3 changes: 3 additions & 0 deletions src/main/java/net/blay09/mods/eirairc/client/ClientProxy.java
Expand Up @@ -7,6 +7,7 @@
import net.blay09.mods.eirairc.CommonProxy;
import net.blay09.mods.eirairc.EiraIRC;
import net.blay09.mods.eirairc.api.event.IRCChannelChatEvent;
import net.blay09.mods.eirairc.client.gui.EiraGui;
import net.blay09.mods.eirairc.client.gui.GuiEiraIRCRedirect;
import net.blay09.mods.eirairc.client.gui.chat.GuiEiraChat;
import net.blay09.mods.eirairc.client.gui.overlay.OverlayNotification;
Expand Down Expand Up @@ -64,6 +65,8 @@ public void init() {
if(ClientGlobalConfig.registerShortCommands) {
IRCCommandHandler.registerQuickCommands(ClientCommandHandler.instance);
}

EiraGui.init(Minecraft.getMinecraft().getResourceManager());
}

@Override
Expand Down
@@ -0,0 +1,79 @@
package net.blay09.mods.eirairc.client.graphics;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.ResourceLocation;

/**
* Created by Blay09 on 20.02.2015.
*/
public class AtlasRegion {

public final ResourceLocation texture;
public final String name;
private int regionX;
private int regionY;
private int regionWidth;
private int regionHeight;
private final int textureWidth = 256;
private final int textureHeight = 256;

public AtlasRegion(ResourceLocation texture, String name) {
this.texture = texture;
this.name = name;
}

public void setRegionX(int regionX) {
this.regionX = regionX;
}

public int getRegionX() {
return regionX;
}

public void setRegionY(int regionY) {
this.regionY = regionY;
}

public int getRegionY() {
return regionY;
}

public void setRegionWidth(int regionWidth) {
this.regionWidth = regionWidth;
}

public int getRegionWidth() {
return regionWidth;
}

public void setRegionHeight(int regionHeight) {
this.regionHeight = regionHeight;
}

public int getRegionHeight() {
return regionHeight;
}

public void draw(int x, int y) {
draw(x, y, regionWidth, regionHeight);
}

public void draw(int x, int y, int width, int height) {
Minecraft.getMinecraft().renderEngine.bindTexture(texture);

float u = (float) regionX / (float) textureWidth;
float v = (float) regionY / (float) textureHeight;
float u2 = (float) (regionX + regionWidth) / (float) textureWidth;
float v2 = (float) (regionY + regionHeight) / (float) textureHeight;

Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(x, y + height, 0, u, v2);
tessellator.addVertexWithUV(x + width, y + height, 0, u2, v2);
tessellator.addVertexWithUV(x + width, y, 0, u2, v);
tessellator.addVertexWithUV(x, y, 0, u, v);
tessellator.draw();
}

}
@@ -0,0 +1,70 @@
package net.blay09.mods.eirairc.client.graphics;

import net.minecraft.client.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Blay09 on 20.02.2015.
*/
public class TextureAtlas {

private final List<TextureAtlasPage> pages = new ArrayList<TextureAtlasPage>();

public TextureAtlas(IResourceManager resourceManager, ResourceLocation resourceLocation) throws IOException {
String resourcePath = resourceLocation.getResourcePath();
resourcePath = resourcePath.substring(0, resourcePath.lastIndexOf('/') + 1);
InputStream in = resourceManager.getResource(resourceLocation).getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
TextureAtlasPage currentPage = null;
AtlasRegion currentRegion = null;
while((line = reader.readLine()) != null) {
line = line.trim();
if(line.isEmpty()) {
currentPage = null;
continue;
}
if(currentPage == null) {
currentPage = new TextureAtlasPage(line, new ResourceLocation(resourceLocation.getResourceDomain(), resourcePath + line));
pages.add(currentPage);
} else {
int sepIdx = line.indexOf(':');
if(sepIdx == -1) {
currentRegion = new AtlasRegion(currentPage.texture, line);
currentPage.addRegion(currentRegion);
} else {
String key = line.substring(0, sepIdx);
String value = line.substring(sepIdx + 1);
if(currentRegion != null) {
if(key.equals("xy")) {
String[] xy = value.split(",");
currentRegion.setRegionX(Integer.parseInt(xy[0].trim()));
currentRegion.setRegionY(Integer.parseInt(xy[1].trim()));
} else if(key.equals("size")) {
String[] size = value.split(",");
currentRegion.setRegionWidth(Integer.parseInt(size[0].trim()));
currentRegion.setRegionHeight(Integer.parseInt(size[1].trim()));
}
}
}
}
}
reader.close();
in.close();
}

public AtlasRegion findRegion(String name) {
for(TextureAtlasPage page : pages) {
AtlasRegion region = page.getRegion(name);
if(region != null) {
return region;
}
}
throw new RuntimeException("Could not find EiraIRC atlas region '" + name + "'");
}

}
@@ -0,0 +1,30 @@
package net.blay09.mods.eirairc.client.graphics;

import net.minecraft.util.ResourceLocation;

import java.util.HashMap;
import java.util.Map;

/**
* Created by Blay09 on 20.02.2015.
*/
public class TextureAtlasPage {

private final Map<String, AtlasRegion> regionMap = new HashMap<String, AtlasRegion>();
public final String fileName;
public final ResourceLocation texture;

public TextureAtlasPage(String fileName, ResourceLocation texture) {
this.fileName = fileName;
this.texture = texture;
}

public void addRegion(AtlasRegion region) {
regionMap.put(region.name, region);
}

public AtlasRegion getRegion(String name) {
return regionMap.get(name);
}

}
16 changes: 15 additions & 1 deletion src/main/java/net/blay09/mods/eirairc/client/gui/EiraGui.java
@@ -1,14 +1,28 @@
package net.blay09.mods.eirairc.client.gui;

import net.blay09.mods.eirairc.client.graphics.TextureAtlas;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.crash.CrashReport;
import net.minecraft.util.ResourceLocation;

import java.io.IOException;

/**
* Created by Blay09 on 05.10.2014.
*/
public class EiraGui {

public static final ResourceLocation texMenu = new net.minecraft.util.ResourceLocation("eirairc", "gfx/menu.png");
public static TextureAtlas atlas;

public static void init(IResourceManager resourceManager) {
try {
atlas = new TextureAtlas(resourceManager, new ResourceLocation("eirairc", "gfx/eiragui.pack"));
} catch (IOException e) {
Minecraft.getMinecraft().displayCrashReport(new CrashReport("Could not load EiraIRC texture atlas", e));
}
}

public static void drawTexturedRect256(int x, int y, int width, int height, int texCoordX, int texCoordY, int regionWidth, int regionHeight, float zLevel) {
drawTexturedRect(x, y, width, height, texCoordX, texCoordY, regionWidth, regionHeight, zLevel, 256, 256);
Expand Down
Expand Up @@ -34,23 +34,23 @@ public void initGui() {
final int buttonCenterX = width / 2;
final int buttonCenterY = height / 2;

btnServers = new GuiMenuButton(0, "Servers", buttonCenterX - 132, buttonCenterY - 95, BUTTON_SIZE, BUTTON_SIZE, 0, BUTTON_SIZE);
btnServers = new GuiMenuButton(0, "Servers", buttonCenterX - 132, buttonCenterY - 95, BUTTON_SIZE, BUTTON_SIZE, EiraGui.atlas.findRegion("menu_servers"));
buttonList.add(btnServers);

btnTwitch = new GuiMenuButton(1, "Twitch", buttonCenterX - 32, buttonCenterY - 95, BUTTON_SIZE, BUTTON_SIZE, 0, 0);
btnTwitch = new GuiMenuButton(1, "Twitch", buttonCenterX - 32, buttonCenterY - 95, BUTTON_SIZE, BUTTON_SIZE, EiraGui.atlas.findRegion("menu_twitch"));
buttonList.add(btnTwitch);

btnPlaceholder = new GuiMenuButton(2, "???", buttonCenterX + 64, buttonCenterY - 95, BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE);
btnPlaceholder = new GuiMenuButton(2, "???", buttonCenterX + 64, buttonCenterY - 95, BUTTON_SIZE, BUTTON_SIZE, EiraGui.atlas.findRegion("menu_cat"));
btnPlaceholder.setPlayButtonSound(false);
buttonList.add(btnPlaceholder);

btnFriends = new GuiMenuButton(3, "Friends", buttonCenterX - 132, buttonCenterY, BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE * 3, 0);
btnFriends = new GuiMenuButton(3, "???", buttonCenterX - 132, buttonCenterY, BUTTON_SIZE, BUTTON_SIZE, EiraGui.atlas.findRegion("menu_friends"));
buttonList.add(btnFriends);

btnScreenshots = new GuiMenuButton(4, "Screenshots", buttonCenterX - 32, buttonCenterY, BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE, 0);
btnScreenshots = new GuiMenuButton(4, "Screenshots", buttonCenterX - 32, buttonCenterY, BUTTON_SIZE, BUTTON_SIZE, EiraGui.atlas.findRegion("menu_screenshots"));
buttonList.add(btnScreenshots);

btnSettings = new GuiMenuButton(5, "Settings", buttonCenterX + 64, buttonCenterY, BUTTON_SIZE, BUTTON_SIZE, BUTTON_SIZE * 2, 0);
btnSettings = new GuiMenuButton(5, "Settings", buttonCenterX + 64, buttonCenterY, BUTTON_SIZE, BUTTON_SIZE, EiraGui.atlas.findRegion("menu_settings"));
buttonList.add(btnSettings);
}

Expand Down
@@ -1,11 +1,8 @@
package net.blay09.mods.eirairc.client.gui.base;

import cpw.mods.fml.client.config.GuiUtils;
import net.blay09.mods.eirairc.client.gui.EiraGui;
import net.blay09.mods.eirairc.client.graphics.AtlasRegion;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

/**
Expand All @@ -15,20 +12,14 @@ public class GuiImageButton extends GuiButton {

private static final float FADE_PER_FRAME = 0.05f;

private final ResourceLocation res;
private int texCoordX;
private int texCoordY;
private AtlasRegion region;
private float alphaFade = 1f;
private int fadeMode;
private String tooltipText;

public GuiImageButton(int id, int xPos, int yPos, ResourceLocation res, int texCoordX, int texCoordY, int texWidth, int texHeight) {
public GuiImageButton(int id, int xPos, int yPos, AtlasRegion region) {
super(id, xPos, yPos, "");
this.res = res;
this.texCoordX = texCoordX;
this.texCoordY = texCoordY;
width = texWidth;
height = texHeight;
setTextureRegion(region);
}

public String getTooltipText() {
Expand All @@ -43,11 +34,10 @@ public void setFadeMode(int fadeMode) {
this.fadeMode = fadeMode;
}

public void setTextureRegion(int texCoordX, int texCoordY, int texWidth, int texHeight) {
this.texCoordX = texCoordX;
this.texCoordY = texCoordY;
width = texWidth;
height = texHeight;
public void setTextureRegion(AtlasRegion region) {
this.region = region;
width = region.getRegionWidth();
height = region.getRegionHeight();
}

public boolean isAlphaVisible() {
Expand All @@ -70,7 +60,6 @@ public void drawButton(Minecraft mc, int mouseX, int mouseY) {
}
}
// Render the button with fade alpha and hover effect
mc.renderEngine.bindTexture(res);
boolean hovered = false;
if(enabled) {
if (mouseX >= xPosition && mouseY >= yPosition && mouseX < xPosition + width && mouseY < yPosition + height) {
Expand All @@ -86,7 +75,7 @@ public void drawButton(Minecraft mc, int mouseX, int mouseY) {
GL11.glPushMatrix();
GL11.glTranslatef(0.5f, 0.5f, 0.5f);
}
GuiUtils.drawTexturedModalRect(xPosition, yPosition, texCoordX, texCoordY, width, height, this.zLevel);
region.draw(xPosition, yPosition);
if(hovered) {
GL11.glPopMatrix();
}
Expand Down

0 comments on commit ead0b66

Please sign in to comment.