Skip to content

Commit

Permalink
#1 Property System instead of Floats
Browse files Browse the repository at this point in the history
... while I'm at it, right? :P
Properties are stored in an CSBFloat object, which handles min, max, slider values and shit like that. Very clean!
looong overdue.

+ layouting done with calculation, not trial and error.
  • Loading branch information
TeNNoX committed Apr 8, 2015
1 parent dc74f58 commit 5c55732
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 202 deletions.
186 changes: 50 additions & 136 deletions src/main/java/tennox/customselectionbox/CSB.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public class CSB {
public static CSBCommonProxy proxy;
private static Configuration config;
public static Logger logger;
public static float red;
public static float green;
public static float blue;
public static float alpha;
public static float thickness;
public static float blinkalpha;
public static float blinkspeed;
public static CSBFloat red = new CSBFloat("Red", 255, 0f, 0f);
public static CSBFloat green = new CSBFloat("Green", 255, 0f, 0f);
public static CSBFloat blue = new CSBFloat("Blue", 255, 0f, 0f);
public static CSBFloat alpha = new CSBFloat("Alpha", 255, 0.4f, 1.0f);
public static CSBFloat thickness = new CSBFloat("Thickness", 7, 2f, 4f).setMinMax(0.1f, 7f);
public static CSBFloat blinkalpha = new CSBFloat("Blink Alpha", 255, 0f, 0.4f);
public static CSBFloat blinkspeed = new CSBFloat("Blink Speed", 100, 0f, 0.2f);
public static boolean diffButtonLoc;
public static boolean disableDepthBuffer;
public static int breakAnimation;
Expand Down Expand Up @@ -91,44 +91,53 @@ public void preInit(FMLPreInitializationEvent event) {
int bra = config.get("general", "break_animation", 0).getInt();
config.save();

setRed(r / 255.0F);
setGreen(g / 255.0F);
setBlue(b / 255.0F);
setAlpha(a / 255.0F);
setThickness(t);
setBlinkAlpha(ba / 255.0F);
setBlinkSpeed(bs / 100.0F);
red.setFromInt(r);
green.setFromInt(g);
blue.setFromInt(b);
alpha.setFromInt(a);
thickness.setFromInt(t);
blinkalpha.setFromInt(ba);
blinkspeed.setFromInt(bs);
setBreakAnimation(bra);

logger.info("red=" + getRed() + " green=" + getGreen() + " blue=" + getBlue() + " alpha=" + getAlpha());
logger.info("thickness=" + getThickness() + " diffbuttonloc=" + diffButtonLoc);
logger.info("blinkalpha=" + getBlinkAlpha() + " blinkspeed=" + getBlinkSpeed());
logger.info("SAVED: red=" + red.get() + " green=" + green.get() + " blue=" + blue.get() + " alpha=" + alpha.get());
logger.info("SAVED: thickness=" + thickness.get() + " diffbuttonloc=" + diffButtonLoc + " blinkalpha=" + blinkalpha.get() + " blinkspeed=" + blinkspeed.get());
}

public static void save() {
config.load();
config.get("general", "red", 0).set(getRedInt());
config.get("general", "green", 0).set(getGreenInt());
config.get("general", "blue", 0).set(getBlueInt());
config.get("general", "alpha", 255).set(getAlphaInt());
config.get("general", "thickness", 4).set(getThicknessInt());
config.get("general", "blink_alpha", 100).set(getBlinkAlphaInt());
config.get("general", "blink_speed", 30).set(getBlinkSpeedInt());
config.get("general", "red", 0).set(red.getAsInt());
config.get("general", "green", 0).set(green.getAsInt());
config.get("general", "blue", 0).set(blue.getAsInt());
config.get("general", "alpha", 255).set(alpha.getAsInt());
config.get("general", "thickness", 4).set(thickness.getAsInt());
config.get("general", "blink_alpha", 100).set(blinkalpha.getAsInt());
config.get("general", "blink_speed", 30).set(blinkspeed.getAsInt());
config.get("general", "disable_depth", false).set(disableDepthBuffer);
config.get("general", "break_animation", 0).set(breakAnimation);
config.save();
logger.info("SAVED: red=" + getRed() + " green=" + getGreen() + " blue=" + getBlue() + " alpha=" + getAlpha());
logger.info("SAVED: thickness=" + getThickness() + " blinkalpha=" + getBlinkAlpha() + " blinkspeed=" + getBlinkSpeed());
logger.info("SAVED: red=" + red.get() + " green=" + green.get() + " blue=" + blue.get() + " alpha=" + alpha.get());
logger.info("SAVED: thickness=" + thickness.get() + " blinkalpha=" + blinkalpha.get() + " blinkspeed=" + blinkspeed.get());
}

public static void reset(boolean mc) {
setRed(0.0F);
setGreen(0.0F);
setBlue(0.0F);
setAlpha(mc ? 0.4F : 1.0F);
setThickness(mc ? 2.0F : 4.0F);
setBlinkAlpha(mc ? 0.0F : 0.390625F);
setBlinkSpeed(0.2F);
if (mc) {
red.mcDefault();
green.mcDefault();
blue.mcDefault();
alpha.mcDefault();
thickness.mcDefault();
blinkalpha.mcDefault();
blinkspeed.mcDefault();
} else {
red.csbDefault();
green.csbDefault();
blue.csbDefault();
alpha.csbDefault();
thickness.csbDefault();
blinkalpha.csbDefault();
blinkspeed.csbDefault();
}
disableDepthBuffer = false;
setBreakAnimation(0);
save();
Expand Down Expand Up @@ -162,7 +171,7 @@ public static void drawSelectionBox(EntityPlayer player, MovingObjectPosition mo
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
GlStateManager.color(0.0F, 0.0F, 0.0F, 0.4F);
GL11.glLineWidth(getThickness());
GL11.glLineWidth(thickness.get());
GlStateManager.disableTexture2D();
GlStateManager.depthMask(false);
float f1 = 0.002F;
Expand All @@ -185,10 +194,10 @@ public static void drawSelectionBox(EntityPlayer player, MovingObjectPosition mo
bb = bb.expand(-breakProgress / 2, -breakProgress / 2, -breakProgress / 2);

// draw blinking block
drawBlinkingBlock(bb, (breakAnimation == ALPHA) ? breakProgress : getBlinkAlpha());
drawBlinkingBlock(bb, (breakAnimation == ALPHA) ? breakProgress : blinkalpha.get());

// set the color back to original and draw outline
GL11.glColor4f(getRed(), getGreen(), getBlue(), getAlpha());
GL11.glColor4f(red.get(), green.get(), blue.get(), alpha.get());
drawOutlinedBoundingBox(bb, -1);

// draw the blockplace indicator //
Expand All @@ -199,7 +208,7 @@ public static void drawSelectionBox(EntityPlayer player, MovingObjectPosition mo

if (bb != null) {
bb = bb.expand(0.0020000000949949026D, 0.0020000000949949026D, 0.0020000000949949026D).offset(-d0, -d1, -d2);
GL11.glColor4f(getRed(), getGreen(), getBlue(), getAlpha() / 3);
GL11.glColor4f(red.get(), green.get(), blue.get(), alpha.get() / 3);
drawOutlinedBoundingBox(bb, -1);
}
}
Expand Down Expand Up @@ -241,7 +250,7 @@ private static AxisAlignedBB getBlockSpecificBB(World world, EntityPlayer player
} catch (Exception e) {
// if (!(block instanceof BlockSnow) && !(block instanceof BlockAnvil) && !(block instanceof BlockVine) && !(block instanceof BlockButton)
// && !(block instanceof BlockLever))
// logger.catching(e);
// logger.catching(e);
}

if (block instanceof BlockLadder) { // Ladder
Expand Down Expand Up @@ -489,10 +498,10 @@ private static void drawBlinkingBlock(AxisAlignedBB par1AxisAlignedBB, float alp
Tessellator tessellator = Tessellator.getInstance();

if (alpha > 0.0F) {
if (getBlinkSpeed() > 0 && CSB.breakAnimation != ALPHA)
alpha *= (float) Math.abs(Math.sin(Minecraft.getSystemTime() / 100.0D * getBlinkSpeed()));
if (blinkspeed.get() > 0 && CSB.breakAnimation != ALPHA)
alpha *= (float) Math.abs(Math.sin(Minecraft.getSystemTime() / 100.0D * blinkspeed.get()));

GL11.glColor4f(getRed(), getGreen(), getBlue(), alpha);
GL11.glColor4f(red.get(), green.get(), blue.get(), alpha);
renderDown(par1AxisAlignedBB);
renderUp(par1AxisAlignedBB);
renderNorth(par1AxisAlignedBB);
Expand Down Expand Up @@ -581,105 +590,10 @@ public static void renderEast(AxisAlignedBB par1AxisAlignedBB) {
tessellator.draw();
}

public static void renderBlock() {
}

public static float getRed() {
return between(red, 0.0F, 1.0F);
}

public static float getGreen() {
return between(green, 0.0F, 1.0F);
}

public static float getBlue() {
return between(blue, 0.0F, 1.0F);
}

public static float getAlpha() {
return between(alpha, 0.0F, 1.0F);
}

public static float getThickness() {
return between(thickness, 0.1F, 7.0F);
}

public static float getBlinkAlpha() {
return between(blinkalpha, 0.0F, 1.0F);
}

public static float getBlinkSpeed() {
return between(blinkspeed, 0.0F, 1.0F);
}

public static void setRed(float r) {
red = between(r, 0.0F, 1.0F);
}

public static void setGreen(float g) {
green = between(g, 0.0F, 1.0F);
}

public static void setBlue(float b) {
blue = between(b, 0.0F, 1.0F);
}

public static void setAlpha(float a) {
alpha = between(a, 0.0F, 1.0F);
}

public static void setThickness(float t) {
thickness = between(t, 0.1F, 7.0F);
}

public static void setBlinkAlpha(float ba) {
blinkalpha = between(ba, 0.0F, 1.0F);
}

public static void setBlinkSpeed(float s) {
blinkspeed = between(s, 0.0F, 1.0F);
}

public static void setBreakAnimation(int index) {
breakAnimation = between(index, 0, LASTANIMATION_INDEX);
}

public static int getRedInt() {
return Math.round(getRed() * 256.0F);
}

public static int getGreenInt() {
return Math.round(getGreen() * 256.0F);
}

public static int getBlueInt() {
return Math.round(getBlue() * 256.0F);
}

public static int getAlphaInt() {
return Math.round(getAlpha() * 256.0F);
}

public static int getThicknessInt() {
return Math.round(getThickness());
}

public static int getBlinkAlphaInt() {
return Math.round(getBlinkAlpha() * 256.0F);
}

public static int getBlinkSpeedInt() {
return Math.round(getBlinkSpeed() * 100.0F);
}

private static float between(float i, float x, float y) {
if (i < x)
i = x;
if (i > y)
i = y;
return i;
}

private static int between(int i, int x, int y) {
if (i < x)
i = x;
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/tennox/customselectionbox/CSBFloat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package tennox.customselectionbox;

public class CSBFloat {

private float value;
private float min, max;
private float mcDefault, csbDefault;

private String name;
private float maxInt;

public CSBFloat(String name, float maxInt, float mcDefault, float csbDefault) {
this.name = name;
this.mcDefault = mcDefault;
this.csbDefault = csbDefault;
this.maxInt = maxInt;
this.min = 0f;
this.max = 1f;
validate();
}

public String displayString() {
return name + ": " + getAsInt();
}

public float sliderVal() {
validate();
return (get() - min) / (max - min);
}

public void setFromSlider(float sliderValue) {
set(sliderValue * (max - min) + min);
}

private void validate() {
if (value < min)
value = min;
if (value > max)
value = max;
}

public CSBFloat setMinMax(float min, float max) {
this.min = min;
this.max = max;
validate();
return this;
}

public int getAsInt() {
return Math.round(get() * (maxInt / max));
}

public void setFromInt(int val) {
set((float) val / ((float) maxInt / max));
}

public float get() {
validate();
return value;
}

public void set(float val) {
value = val;
validate();
}

public void mcDefault() {
this.set(mcDefault);
}

public void csbDefault() {
this.set(csbDefault);
}

public String toString() {
return name + "[" + get() + ", min=" + min + ", max=" + max + "|" + maxInt + " mc=" + mcDefault + " csb=" + csbDefault + "]";
}
}
27 changes: 18 additions & 9 deletions src/main/java/tennox/customselectionbox/CSBSettingsGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.util.ResourceLocation;

public class CSBSettingsGUI extends GuiScreen {
GuiScreen parent;
Expand All @@ -17,19 +18,22 @@ public CSBSettingsGUI(GuiScreen p) {
@SuppressWarnings("unchecked")
public void initGui() {
this.buttonList.clear();


int between = 22;

// left
this.buttonList.add(new CSBSlider(1, 4, this.height / 2 + -62, CSB.getRed()));
this.buttonList.add(new CSBSlider(2, 4, this.height / 2 + -38, CSB.getGreen()));
this.buttonList.add(new CSBSlider(3, 4, this.height / 2 + -14, CSB.getBlue()));
this.buttonList.add(new CSBSlider(4, 4, this.height / 2 + 10, CSB.getAlpha()));
this.buttonList.add(new CSBSlider(5, 4, this.height / 2 + 34, CSB.getThickness() / 7.0F));
int top = (int) (this.height / 2 - 4 - (6 * between / 2f));
this.buttonList.add(new CSBSlider(1, 2, top + between * 1, CSB.red));
this.buttonList.add(new CSBSlider(2, 2, top + between * 2, CSB.green));
this.buttonList.add(new CSBSlider(3, 2, top + between * 3, CSB.blue));
this.buttonList.add(new CSBSlider(4, 2, top + between * 4, CSB.alpha));
this.buttonList.add(new CSBSlider(5, 2, top + between * 5, CSB.thickness));

// right
this.buttonList.add(new CSBButton(9, this.width - 154, this.height / 2 - 52, 150, 20, "Disable Depth: " + (CSB.disableDepthBuffer ? "ON" : "OFF")));
this.buttonList.add(new CSBButton(10, this.width - 154, this.height / 2 - 30, 150, 20, "Break Animation: " + getBreakAnimationName()));
this.buttonList.add(new CSBSlider(7, this.width - 154, this.height / 2 + 2, CSB.getBlinkAlpha()));
this.buttonList.add(new CSBSlider(8, this.width - 154, this.height / 2 + 26, CSB.getBlinkSpeed()));
this.buttonList.add(new CSBSlider(7, this.width - 154, this.height / 2 + 2, CSB.blinkalpha));
this.buttonList.add(new CSBSlider(8, this.width - 154, this.height / 2 + 26, CSB.blinkspeed));

this.buttonList.add(new CSBButton(20, this.width / 2 - 100, this.height - 48, "Done"));
this.buttonList.add(new CSBButton(21, this.width / 2 - 100, this.height - 24, 95, 20, "CSB defaults"));
Expand Down Expand Up @@ -77,7 +81,12 @@ protected void keyTyped(char par1, int par2) {

public void drawScreen(int par1, int par2, float par3) {
drawGradientRect(0, 0, this.width, 48 - 4, -1072689136, -804253680); // top
drawGradientRect(0, this.height / 2 - 67, 158, this.height / 2 + 59, -1072689136, -804253680); // left

// left
int top = (int) (this.height / 2 - 4 - ((6f * 22f) / 2f));
int bottom = top + 22 * 6;
drawGradientRect(0, top - 2, 154, bottom + 2, -1072689136, -804253680);

drawGradientRect(this.width - 158, this.height / 2 - 56, this.width, this.height / 2 + 50, -1072689136, -804253680); // right
drawGradientRect(0, this.height - 48 - 4, this.width, this.height, -1072689136, -804253680); // bottom

Expand Down
Loading

0 comments on commit 5c55732

Please sign in to comment.