Skip to content

Commit

Permalink
Make it possible to use a custom texture for the small buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwane Kalam-Alami committed Aug 8, 2012
1 parent 30f860e commit 79459c6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 35 deletions.
Binary file added docs/_static/img/button10px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions docs/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,7 @@ Don't worry, you'll find a backup in your config folder. It's just that InvTweak
* **The config files don't appear!**

You're probably running a platform that is not 100% supported by Java. In any case, what you can do is extract manually these files from the Zip: *DefaultConfig.dat* should go to **{MinecraftDir}/config/InvTweaksConfig.txt**, and *DefaultTree.dat* to **{MinecraftDir}/config/InvTweaksTree.txt**.

* **The "..." icon looks bad with my texture pack!**

By default, the mod uses *gui/gui.png* to draw the small buttons for the inventory and chests. It looks good most of the time, but you can override this by adding a **gui/button10px.png** file to your pack. `Here is a template <_static/img/button10px.png>`_.
18 changes: 13 additions & 5 deletions src/InvTweaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -559,14 +559,18 @@ private void handleGUILayout(apm guiScreen) {

if (!customButtonsAdded) {

// Check for custom button texture
boolean customTextureAvailable = hasTexture("/gui/button10px.png");

// Inventory button
if (!isValidChest) {
controlList.add(new InvTweaksGuiSettingsButton(
cfgManager, InvTweaksConst.JIMEOWAN_ID,
getGuiX(guiContainer) + getGuiWidth(guiContainer) - 15,
getGuiY(guiContainer) + 5,
w, h, "...",
InvTweaksLocalization.get("invtweaks.button.settings.tooltip")));
InvTweaksLocalization.get("invtweaks.button.settings.tooltip"),
customTextureAvailable));
}

// Chest buttons
Expand All @@ -593,7 +597,8 @@ private void handleGUILayout(apm guiScreen) {
(isChestWayTooBig) ? x + 22 : x - 1,
(isChestWayTooBig) ? y - 3 : y,
w, h, "...",
InvTweaksLocalization.get("invtweaks.button.settings.tooltip")));
InvTweaksLocalization.get("invtweaks.button.settings.tooltip"),
customTextureAvailable));

// Sorting buttons
if (!config.getProperty(InvTweaksConfig.PROP_SHOW_CHEST_BUTTONS).equals("false")) {
Expand All @@ -606,7 +611,8 @@ private void handleGUILayout(apm guiScreen) {
(isChestWayTooBig) ? y + 12 : y,
w, h, "h", InvTweaksLocalization.get("invtweaks.button.chest3.tooltip"),
InvTweaksHandlerSorting.ALGORITHM_HORIZONTAL,
rowSize);
rowSize,
customTextureAvailable);
controlList.add(button);

button = new InvTweaksGuiSortingButton(
Expand All @@ -615,7 +621,8 @@ private void handleGUILayout(apm guiScreen) {
(isChestWayTooBig) ? y + 25 : y,
w, h, "v", InvTweaksLocalization.get("invtweaks.button.chest2.tooltip"),
InvTweaksHandlerSorting.ALGORITHM_VERTICAL,
rowSize);
rowSize,
customTextureAvailable);
controlList.add(button);

button = new InvTweaksGuiSortingButton(
Expand All @@ -624,7 +631,8 @@ private void handleGUILayout(apm guiScreen) {
(isChestWayTooBig) ? y + 38 : y,
w, h, "s", InvTweaksLocalization.get("invtweaks.button.chest1.tooltip"),
InvTweaksHandlerSorting.ALGORITHM_DEFAULT,
rowSize);
rowSize,
customTextureAvailable);
controlList.add(button);

}
Expand Down
26 changes: 18 additions & 8 deletions src/InvTweaksGuiIconButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ public class InvTweaksGuiIconButton extends InvTweaksGuiTooltipButton {

protected InvTweaksConfigManager cfgManager;

private boolean useCustomTexture;

public InvTweaksGuiIconButton(InvTweaksConfigManager cfgManager,
int id, int x, int y, int w, int h,
String displayString, String tooltip) {
String displayString, String tooltip, boolean useCustomTexture) {
super(id, x, y, w, h, displayString, tooltip);
this.cfgManager = cfgManager;
this.useCustomTexture = useCustomTexture;
}

public void a(Minecraft minecraft, int i, int j) { /* drawButton */
Expand All @@ -28,14 +31,21 @@ public void a(Minecraft minecraft, int i, int j) { /* drawButton */
}

// Draw background (use the 4 corners of the texture to fit best its small size)
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glBindTexture(3553, getTexture(minecraft, "/gui/gui.png"));
int k = getHoverState(isMouseOverButton(i, j));
drawTexturedModalRect(getXPosition(), getYPosition(), 1, 46 + k * 20 + 1, getWidth() / 2, getHeight() / 2);
drawTexturedModalRect(getXPosition(), getYPosition() + getHeight() / 2, 1, 46 + k * 20 + 20 - getHeight() / 2 - 1, getWidth() / 2, getHeight() / 2);
drawTexturedModalRect(getXPosition() + getWidth() / 2, getYPosition(), 200 - getWidth() / 2 - 1, 46 + k * 20 + 1, getWidth() / 2, getHeight() / 2);
drawTexturedModalRect(getXPosition() + getWidth() / 2, getYPosition() + getHeight() / 2, 200 - getWidth() / 2 - 1, 46 + k * 20 + 19 - getHeight() / 2, getWidth() / 2,
getHeight() / 2);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

if (useCustomTexture) {
GL11.glBindTexture(3553, getTexture(minecraft, "/gui/button10px.png"));
drawTexturedModalRect(getXPosition(), getYPosition(), (k - 1) * 10, 0, getWidth(), getHeight());
}
else {
GL11.glBindTexture(3553, getTexture(minecraft, "/gui/gui.png"));
drawTexturedModalRect(getXPosition(), getYPosition(), 1, 46 + k * 20 + 1, getWidth() / 2, getHeight() / 2);
drawTexturedModalRect(getXPosition(), getYPosition() + getHeight() / 2, 1, 46 + k * 20 + 20 - getHeight() / 2 - 1, getWidth() / 2, getHeight() / 2);
drawTexturedModalRect(getXPosition() + getWidth() / 2, getYPosition(), 200 - getWidth() / 2 - 1, 46 + k * 20 + 1, getWidth() / 2, getHeight() / 2);
drawTexturedModalRect(getXPosition() + getWidth() / 2, getYPosition() + getHeight() / 2, 200 - getWidth() / 2 - 1, 46 + k * 20 + 19 - getHeight() / 2, getWidth() / 2,
getHeight() / 2);
}

}

Expand Down
6 changes: 3 additions & 3 deletions src/InvTweaksGuiSettingsButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
public class InvTweaksGuiSettingsButton extends InvTweaksGuiIconButton {

private static final Logger log = Logger.getLogger("InvTweaks");

public InvTweaksGuiSettingsButton(InvTweaksConfigManager cfgManager,
int id, int x, int y, int w, int h,
String displayString, String tooltip) {
super(cfgManager, id, x, y, w, h, displayString, tooltip);
String displayString, String tooltip, boolean useCustomTexture) {
super(cfgManager, id, x, y, w, h, displayString, tooltip, useCustomTexture);
}

public void a(Minecraft minecraft, int i, int j) {
Expand Down
4 changes: 2 additions & 2 deletions src/InvTweaksGuiSortingButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class InvTweaksGuiSortingButton extends InvTweaksGuiIconButton {
public InvTweaksGuiSortingButton(InvTweaksConfigManager cfgManager,
int id, int x, int y, int w, int h,
String displayString, String tooltip,
int algorithm, int rowSize) {
super(cfgManager, id, x, y, w, h, displayString, tooltip);
int algorithm, int rowSize, boolean useCustomTexture) {
super(cfgManager, id, x, y, w, h, displayString, tooltip, useCustomTexture);
this.algorithm = algorithm;
this.rowSize = rowSize;
}
Expand Down
62 changes: 45 additions & 17 deletions src/InvTweaksObfuscation.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.minecraft.client.Minecraft;
Expand All @@ -19,24 +21,18 @@ public class InvTweaksObfuscation {

protected InvTweaksModCompatibility mods;

private static Field creativeSlotField = null;
private static Map<String, Field> fieldsMap = new HashMap<String, Field>();

static {
// CreativeSlot.underlyingSlot
makeFieldPublic(aqo.class, "b");
// RenderEngine.texturePack
makeFieldPublic(ave.class, "k");
}

public InvTweaksObfuscation(Minecraft mc) {
this.mc = mc;
this.mods = new InvTweaksModCompatibility(this);

if (creativeSlotField == null) {
try {
creativeSlotField = aqo.class.getDeclaredField("b");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(creativeSlotField, Modifier.PUBLIC);
}
catch (Exception e) {
creativeSlotField = null;
log.severe("Failed to make creative slot accessible: " + e.getMessage());
}
}
}

// Minecraft members
Expand Down Expand Up @@ -264,7 +260,7 @@ protected int getSlotNumber(pq slot) {
try {
// Creative slots don't set the "d" property, serve as a proxy for true slots
if (slot instanceof aqo) {
pq underlyingSlot = (pq) creativeSlotField.get(slot);
pq underlyingSlot = (pq) getThroughReflection(aqo.class, "b", slot);
if (underlyingSlot != null) {
return underlyingSlot.d;
}
Expand Down Expand Up @@ -316,16 +312,19 @@ protected String getDisplayString(aog guiButton) {
protected void playSound(String string, float f, float g) {
mc.A.a(string, f, g);
}
public long getCurrentTime() {
protected long getCurrentTime() {
return getTheWorld().D();
}

protected int getKeyCode(ane w) { // KeyBinding
return w.d;
}
protected int getSpecialChestRowSize(aqg guiContainer, int defaultValue) {
return mods.getSpecialChestRowSize(guiContainer, defaultValue);
}
protected boolean hasTexture(String texture) {
ayi texturePack = (ayi) getThroughReflection(ave.class, "k", mc.o);
return texturePack.e().a(texture) != null;
}

// Static access

Expand Down Expand Up @@ -439,5 +438,34 @@ protected boolean isItemArmor(Object o) { // ItemArmor
protected boolean isBasicSlot(Object o) { // Slot
return o != null && o.getClass().equals(pq.class);
}

// Reflection utils

protected static void makeFieldPublic(Class<?> c, String field) {
try {
Field f = c.getDeclaredField(field);
f.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(f, Modifier.PUBLIC);
fieldsMap.put(c.getName() + field, f);
}
catch (Exception e) {
log.severe("Failed to make " + c.getName() + "." + field + " accessible: " + e.getMessage());
}
}

/**
* Access value from any field, even private.
* Field must be made public through the makeFieldPublic() function first.
* @return
*/
protected static Object getThroughReflection(Class<?> c, String field, Object instance) {
try {
return fieldsMap .get(c.getName() + field).get(instance);
} catch (Exception e) {
return null;
}
}

}

0 comments on commit 79459c6

Please sign in to comment.