Skip to content

Commit

Permalink
Starting to refactor the GUI into different classes
Browse files Browse the repository at this point in the history
  • Loading branch information
frodare committed Feb 9, 2017
1 parent 1d22431 commit c8a4921
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 310 deletions.
6 changes: 0 additions & 6 deletions java/net/torocraft/torohealthmod/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.util.EntitySelectors;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
Expand Down Expand Up @@ -114,7 +113,6 @@ public RayTraceResult getMouseOver(float partialTicks) {
Vec3d lookVectorFromEyePosition = observerPositionEyes.addVector(lookVector.xCoord * reachDistance, lookVector.yCoord * reachDistance, lookVector.zCoord * reachDistance);
this.pointedEntity = null;
Vec3d hitVector = null;
float f = 1.0F;
List<Entity> list = this.mc.world.getEntitiesInAABBexcluding(observer,
observer.getEntityBoundingBox().addCoord(lookVector.xCoord * reachDistance, lookVector.yCoord * reachDistance, lookVector.zCoord * reachDistance).expand(1.0D, 1.0D, 1.0D), EntitySelectors.NOT_SPECTATING);
double d2 = distance;
Expand Down Expand Up @@ -157,8 +155,4 @@ public RayTraceResult getMouseOver(float partialTicks) {
return objectMouseOver;
}

private boolean isSolidBlock(BlockPos pos, BlockPos prevPos) {
return prevPos.compareTo(pos) == 0;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package net.torocraft.torohealthmod.gui;
package net.torocraft.torohealthmod.config;

import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.torocraft.torohealthmod.ToroHealthMod;
import net.torocraft.torohealthmod.config.ConfigurationHandler;

public class GuiConfigToroHealth extends GuiConfig {

Expand Down
122 changes: 122 additions & 0 deletions java/net/torocraft/torohealthmod/display/EntityDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package net.torocraft.torohealthmod.display;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.math.MathHelper;

public class EntityDisplay implements ToroHealthDisplay {

private static final int RENDER_HEIGHT = 20;

private int x = 50;
private int y = 50;

private EntityLivingBase entity;
private Entity leashedToEntity;
private float prevYawOffset;
private float prevYaw;
private float prevPitch;
private float prevYawHead;
private float prevPrevYahHead;
private int scale = 1;

@Override
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public void setEntity(EntityLivingBase entity) {
this.entity = entity;
updateScale();
}

@Override
public void draw() {
try {
pushEntityLeasedTo();
pushEntityRotations();
glDraw();
popEntityRotations();
popEntityLeasedTo();
} catch (Throwable ignore) {

}
}

private void updateScale() {
scale = MathHelper.ceil(RENDER_HEIGHT / entity.height);
}

private void glDraw() {
GlStateManager.enableColorMaterial();
GlStateManager.pushMatrix();

GlStateManager.translate((float) x, (float) y, 50.0F);
GlStateManager.scale((float) (-scale), (float) scale, (float) scale);
GlStateManager.rotate(180.0F, 0.0F, 0.0F, 1.0F);
GlStateManager.rotate(135.0F, 0.0F, 1.0F, 0.0F);
GlStateManager.rotate(-100.0F, 0.0F, 1.0F, 0.0F);
GlStateManager.rotate(0.0f, 1.0F, 0.0F, 0.0F);

RenderHelper.enableStandardItemLighting();

GlStateManager.translate(0.0F, 0.0F, 0.0F);
RenderManager rendermanager = Minecraft.getMinecraft().getRenderManager();
rendermanager.setPlayerViewY(180.0F);
rendermanager.setRenderShadow(false);
rendermanager.doRenderEntity(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F, false);
rendermanager.setRenderShadow(true);

GlStateManager.popMatrix();
RenderHelper.disableStandardItemLighting();
GlStateManager.disableRescaleNormal();
GlStateManager.setActiveTexture(OpenGlHelper.lightmapTexUnit);
GlStateManager.disableTexture2D();
GlStateManager.setActiveTexture(OpenGlHelper.defaultTexUnit);
}

private void popEntityLeasedTo() {
if (entity instanceof EntityLiving && leashedToEntity != null) {
((EntityLiving) entity).setLeashedToEntity(leashedToEntity, false);
leashedToEntity = null;
}
}

private void pushEntityLeasedTo() {
if (entity instanceof EntityLiving) {
if (((EntityLiving) entity).getLeashed()) {
leashedToEntity = ((EntityLiving) entity).getLeashedToEntity();
((EntityLiving) entity).setLeashedToEntity(null, false);
}
}
}

private void popEntityRotations() {
entity.renderYawOffset = prevYawOffset;
entity.rotationYaw = prevYaw;
entity.rotationPitch = prevPitch;
entity.rotationYawHead = prevYawHead;
entity.prevRotationYawHead = prevPrevYahHead;
}

private void pushEntityRotations() {
prevYawOffset = entity.renderYawOffset;
prevYaw = entity.rotationYaw;
prevPitch = entity.rotationPitch;
prevYawHead = entity.rotationYawHead;
prevPrevYahHead = entity.prevRotationYawHead;
entity.renderYawOffset = 0.0f;
entity.rotationYaw = 0.0f;
entity.rotationPitch = 0.0f;
entity.rotationYawHead = 0.0f;
entity.prevRotationYawHead = 0.0f;
}
}
148 changes: 148 additions & 0 deletions java/net/torocraft/torohealthmod/display/HeartsDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package net.torocraft.torohealthmod.display;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.torocraft.torohealthmod.ToroHealthMod;
import net.torocraft.torohealthmod.gui.GuiEntityStatus;

public class HeartsDisplay implements ToroHealthDisplay {

private static final ResourceLocation TEXTURE = new ResourceLocation(ToroHealthMod.MODID, "textures/gui/entityStatus.png");
private static final int WIDTH = 84;
private static final int HEIGHT = 34;

private final Minecraft mc;
private final Gui gui;
private int x, originX = 100;
private int y, originY = 100;
private EntityLivingBase entity;

public HeartsDisplay(Minecraft mc, Gui gui) {
this.mc = mc;
this.gui = gui;
}

@Override
public void setPosition(int x, int y) {
originX = x;
originY = y;
resetToOrigin();
}

@Override
public void draw() {
if (entity == null) {
return;
}


resetToOrigin();
mc.renderEngine.bindTexture(TEXTURE);
Gui.drawModalRectWithCustomSizedTexture(x, y , 0.0f, 0.0f, WIDTH, HEIGHT, 200.0f, 200.0f);

drawName();
drawHearts();
drawArmor();
}

@Override
public void setEntity(EntityLivingBase entity) {
this.entity = entity;
}

private void resetToOrigin() {
x = originX;
y = originY;
}

private void drawName() {
gui.drawString(mc.fontRendererObj, entity.getDisplayName().getFormattedText(), x, y, 0xFFFFFF);
y += 10;
}

private int drawHearts() {
mc.renderEngine.bindTexture(GuiEntityStatus.ICONS);
int currentHealth = MathHelper.ceil(entity.getHealth());

int absorptionAmount = MathHelper.ceil(entity.getAbsorptionAmount());
int remainingAbsorption = absorptionAmount;

float maxHealth = entity.getMaxHealth();

int numRowsOfHearts = MathHelper.ceil((maxHealth + (float) absorptionAmount) / 2.0F / 10.0F);
int j2 = Math.max(10 - (numRowsOfHearts - 2), 3);

for (int currentHeartBeingDrawn = MathHelper.ceil((maxHealth + (float) absorptionAmount) / 2.0F)
- 1; currentHeartBeingDrawn >= 0; --currentHeartBeingDrawn) {
int texturePosX = 16;
int flashingHeartOffset = 0;

int rowsOfHearts = MathHelper.ceil((float) (currentHeartBeingDrawn + 1) / 10.0F) - 1;
int heartToDrawX = x + currentHeartBeingDrawn % 10 * 8;
int heartToDrawY = y + rowsOfHearts * j2;

int hardcoreModeOffset = 0;

if (entity.world.getWorldInfo().isHardcoreModeEnabled()) {
hardcoreModeOffset = 5;
}

gui.drawTexturedModalRect(heartToDrawX, heartToDrawY, 16 + flashingHeartOffset * 9, 9 * hardcoreModeOffset, 9, 9);

if (remainingAbsorption > 0) {
if (remainingAbsorption == absorptionAmount && absorptionAmount % 2 == 1) {
gui.drawTexturedModalRect(heartToDrawX, heartToDrawY, texturePosX + 153, 9 * hardcoreModeOffset, 9, 9);
--remainingAbsorption;
} else {
gui.drawTexturedModalRect(heartToDrawX, heartToDrawY, texturePosX + 144, 9 * hardcoreModeOffset, 9, 9);
remainingAbsorption -= 2;
}
} else {
if (currentHeartBeingDrawn * 2 + 1 < currentHealth) {
gui.drawTexturedModalRect(heartToDrawX, heartToDrawY, texturePosX + 36, 9 * hardcoreModeOffset, 9, 9);
}

if (currentHeartBeingDrawn * 2 + 1 == currentHealth) {
gui.drawTexturedModalRect(heartToDrawX, heartToDrawY, texturePosX + 45, 9 * hardcoreModeOffset, 9, 9);
}
}
}

y += (numRowsOfHearts - 1) * j2 + 10;

return remainingAbsorption;
}

private void drawArmor() {
mc.renderEngine.bindTexture(GuiEntityStatus.ICONS);

int armor = entity.getTotalArmorValue();

for (int i = 0; i < 10; ++i) {
if (armor > 0) {
int armorIconX = x + i * 8;

/*
* determines whether armor is full, half, or empty icon
*/
if (i * 2 + 1 < armor) {
gui.drawTexturedModalRect(armorIconX, y, 34, 9, 9, 9);
}

if (i * 2 + 1 == armor) {
gui.drawTexturedModalRect(armorIconX, y, 25, 9, 9, 9);
}

if (i * 2 + 1 > armor) {
gui.drawTexturedModalRect(armorIconX, y, 16, 9, 9, 9);
}
}
}

y += 10;
}

}
66 changes: 66 additions & 0 deletions java/net/torocraft/torohealthmod/display/NumericDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package net.torocraft.torohealthmod.display;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.ResourceLocation;
import net.torocraft.torohealthmod.ToroHealthMod;

public class NumericDisplay implements ToroHealthDisplay {

private static final ResourceLocation TEXTURE = new ResourceLocation(ToroHealthMod.MODID, "textures/gui/entityStatus.png");
private static final int WIDTH = 100;
private static final int HEIGHT = 34;

private final Minecraft mc;
private final Gui gui;
private int x = 100;
private int y = 140;
private EntityLivingBase entity;

public NumericDisplay(Minecraft mc, Gui gui) {
this.mc = mc;
this.gui = gui;
}

@Override
public void setPosition(int x, int y) {
this.x = x;
this.y = y;

}

@Override
public void draw() {
if (entity == null) {
return;
}

mc.renderEngine.bindTexture(TEXTURE);

/*
* defines positions of each element from the top left position of
* status display
*/
int bgX = 0, bgY = 0, healthBarX = 2, healthBarY = 16, nameX = 50, nameY = 4, healthX = 50, healthY = 20;

Gui.drawModalRectWithCustomSizedTexture(x + bgX, y + bgY, 0.0f, 0.0f, WIDTH, HEIGHT, 200.0f, 200.0f);

Gui.drawModalRectWithCustomSizedTexture(x + healthBarX, y + healthBarY, 0.0f, 150.0f, 96, 16, 200.0f, 200.0f);

int currentHealthWidth = (int) Math.ceil(96 * (entity.getHealth() / entity.getMaxHealth()));
Gui.drawModalRectWithCustomSizedTexture(x + healthBarX, y + healthBarY, 0.0f, 100.0f, currentHealthWidth, 16, 200.0f, 200.0f);

String name = entity.getDisplayName().getFormattedText();

gui.drawCenteredString(mc.fontRendererObj, name, x + nameX, y + nameY, 0xFFFFFF);
gui.drawCenteredString(mc.fontRendererObj, (int) Math.ceil(entity.getHealth()) + "/" + (int) entity.getMaxHealth(), x + healthX, y + healthY,
0xFFFFFF);
}

@Override
public void setEntity(EntityLivingBase entity) {
this.entity = entity;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.torocraft.torohealthmod.display;

import net.minecraft.entity.EntityLivingBase;

public interface ToroHealthDisplay {
void setEntity(EntityLivingBase entity);
void setPosition(int x, int y);
void draw();
}
Loading

0 comments on commit c8a4921

Please sign in to comment.