Added Label widget.

Reworked WidgetGeometry to allow for separate pos/size computation method.
This commit is contained in:
Thomas Guimbretiere committed 2013-10-11 21:43:58 +02:00
1 parent a87d3b9de7
commit 5601fcdf7b
5 files changed
+114 -35

No files matched your search

+5 -1
View File
@@ -1,5 +1,6 @@
package mcp.mobius.waila.gui;
import mcp.mobius.waila.gui.widgets.Label;
import mcp.mobius.waila.gui.widgets.PictureDisplay;
import mcp.mobius.waila.gui.widgets.WidgetGeometry;
import net.minecraft.client.gui.GuiScreen;
@@ -9,6 +10,9 @@ public class ScreenTest extends ScreenBase {
public ScreenTest(GuiScreen parent) {
super(parent);
this.getRoot().addWidget("Picture", new PictureDisplay(this.getRoot(), "waila:textures/test.png"));
this.getRoot().getWidget("Picture").setGeometry(new WidgetGeometry(50.0, 50.0, 50.0, 50.0, WidgetGeometry.Align.CENTER, WidgetGeometry.Align.CENTER));
this.getRoot().getWidget("Picture").setGeometry(new WidgetGeometry(50.0, 50.0, 50.0, 50.0, true, true, WidgetGeometry.Align.CENTER, WidgetGeometry.Align.CENTER));
this.getRoot().addWidget("Text", new Label(this.getRoot(), "waila:textures/test.png"));
this.getRoot().getWidget("Text").setGeometry(new WidgetGeometry(50.0D, 0.0D, 50, 50, true, false, WidgetGeometry.Align.CENTER, WidgetGeometry.Align.TOP));
}
}
+49
View File
@@ -0,0 +1,49 @@
package mcp.mobius.waila.gui.widgets;
import mcp.mobius.waila.gui.interfaces.IWidget;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.util.Point;
public class Label extends WidgetBase {
private String text = "";
private int color;
public Label(IWidget parent, String text){
super(parent);
this.setText(text);
this.color = 0xFFFFFF;
}
public Label(IWidget parent, String text, int color){
super(parent);
this.setText(text);
this.color = color;
}
@Override
public void setGeometry(WidgetGeometry geom){
this.geom = geom;
this.updateGeometry();
}
private void setText(String text){
this.text = text;
this.updateGeometry();
}
private void updateGeometry(){
if (this.geom == null)
this.geom = new WidgetGeometry(0,0,50,50, false, false);
this.geom = new WidgetGeometry(this.geom.x, this.geom.y, this.mc.fontRenderer.getStringWidth(this.text), 8, this.geom.fracPos, false, this.geom.alignX, this.geom.alignY);
}
@Override
public void draw(Point pos) {
this.mc.fontRenderer.drawString(this.text, pos.getX(), pos.getY(), this.color);
}
}
@@ -15,7 +15,7 @@ public class LayoutCanvas extends WidgetBase{
public LayoutCanvas(){
super();
this.setGeometry(0, 0, this.rez.getScaledWidth(), this.rez.getScaledHeight());
this.setGeometry(0, 0, this.rez.getScaledWidth(), this.rez.getScaledHeight(), false, false);
Mouse.getDWheel(); // This is to "calibrate" the DWheel
this.lastMouseEvent = new MouseEvent(this);
}
@@ -23,7 +23,7 @@ public class LayoutCanvas extends WidgetBase{
@Override
public void draw(){
this.rez = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight);
this.setGeometry(0, 0, this.rez.getScaledWidth(), this.rez.getScaledHeight());
this.setGeometry(0, 0, this.rez.getScaledWidth(), this.rez.getScaledHeight(), false, false);
this.draw(this.getPos());
+4 -4
View File
@@ -29,13 +29,15 @@ public abstract class WidgetBase implements IWidget {
this.mc = Minecraft.getMinecraft();
this.rez = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight);
this.texManager = this.mc.renderEngine;
this.setGeometry(new WidgetGeometry(0,0,50,50,false,false));
}
public WidgetBase(IWidget parent){
this.setParent(parent);
this.mc = Minecraft.getMinecraft();
this.rez = new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight);
this.texManager = this.mc.renderEngine;
this.texManager = this.mc.renderEngine;
this.setGeometry(new WidgetGeometry(0,0,50,50,false,false));
}
/////////////////////
@@ -101,8 +103,7 @@ public abstract class WidgetBase implements IWidget {
@Override
public void setGeometry(WidgetGeometry geom) { this.geom = geom; }
public void setGeometry(double x, double y, double sx, double sy){ this.setGeometry(new WidgetGeometry (x, y, sx, sy)) ;}
public void setGeometry(int x, int y, int sx, int sy) { this.setGeometry(new WidgetGeometry (x, y, sx, sy)) ;}
public void setGeometry(double x, double y, double sx, double sy, boolean fp, boolean fs){ this.setGeometry(new WidgetGeometry (x, y, sx, sy, fp, fs)) ;}
@Override
public WidgetGeometry getGeometry() { return this.geom; }
@@ -188,5 +189,4 @@ public abstract class WidgetBase implements IWidget {
public void onMouseLeave(MouseEvent event) {
//System.out.printf("%70s %s\n", this, event);
}
}
@@ -11,17 +11,25 @@ public class WidgetGeometry {
public enum Align {LEFT, CENTER, RIGHT, TOP, BOTTOM};
double relX = -1;
double relY = -1;
int absX = -1;
int absY = -1;
//double relX = -1;
//double relY = -1;
//int absX = -1;
//int absY = -1;
double relSX = -1;
double relSY = -1;
int absSX = -1;
int absSY = -1;
//double relSX = -1;
//double relSY = -1;
//int absSX = -1;
//int absSY = -1;
boolean isFractional;
//boolean isAbsolute;
double x = -1;
double y = -1;
double sx = -1;
double sy = -1;
boolean fracPos;
boolean fracSize;
Align alignX;
Align alignY;
@@ -31,14 +39,24 @@ public class WidgetGeometry {
public PointDouble(double x, double y){ this.x = x; this.y = y;};
}
public WidgetGeometry(double x, double y, double sx, double sy, boolean fracPos, boolean fracSize){
this(x, y, sx, sy, fracPos, fracSize, Align.LEFT, Align.TOP);
}
public WidgetGeometry(double x, double y, double sx, double sy, boolean fracPos, boolean fracSize, Align alignX, Align alignY){
this.x = x;
this.y = y;
this.sx = sx;
this.sy = sy;
this.fracPos = fracPos;
this.fracSize = fracSize;
this.alignX = alignX;
this.alignY = alignY;
}
/*
public WidgetGeometry(double x, double y, double sx, double sy){
this.relX = x;
this.relY = y;
this.relSX = sx;
this.relSY = sy;
this.isFractional = false;
this.alignX = Align.LEFT;
this.alignY = Align.TOP;
this(x, y, sx, sy, Align.LEFT, Align.TOP);
}
public WidgetGeometry(double x, double y, double sx, double sy, Align alignX, Align alignY){
@@ -46,20 +64,25 @@ public class WidgetGeometry {
this.relY = y;
this.relSX = sx;
this.relSY = sy;
this.isFractional = false;
this.isAbsolute = false;
this.alignX = alignX;
this.alignY = alignY;
}
public WidgetGeometry(int x, int y, int sx, int sy){
this(x, y, sx, sy, Align.LEFT, Align.TOP);
}
public WidgetGeometry(int x, int y, int sx, int sy, Align alignX, Align alignY){
this.absX = x;
this.absY = y;
this.absSX = sx;
this.absSY = sy;
this.isFractional = true;
this.alignX = Align.LEFT;
this.alignY = Align.TOP;
this.isAbsolute = true;
this.alignX = alignX;
this.alignY = alignY;
}
*/
//public PointDouble getRelativePos() { return new PointDouble(this.relX, this.relY);}
//public Point getAbsolutePos() { return new Point(this.absX, this.absY);}
@@ -69,15 +92,18 @@ public class WidgetGeometry {
public Point getPos(IWidget parent){
Point thisPos;
if (this.isFractional){
if (!this.fracPos && parent != null){
Point parentPos = parent.getPos();
thisPos = new Point(parentPos.getX() + this.absX, parentPos.getY() +this.absY);
thisPos = new Point(parentPos.getX() + (int)this.x, parentPos.getY() + (int)this.y);
}
else if (!this.fracPos && parent == null){
thisPos = new Point((int)this.x, (int)this.y);
}
else {
Point parentPos = parent.getPos();
Point parentSize = parent.getSize();
thisPos = new Point ( MathHelper.ceiling_double_int(parentPos.getX() + parentSize.getX() * this.relX / 100D),
MathHelper.ceiling_double_int(parentPos.getY() + parentSize.getY() * this.relY / 100D));
thisPos = new Point ( MathHelper.ceiling_double_int(parentPos.getX() + parentSize.getX() * this.x / 100D),
MathHelper.ceiling_double_int(parentPos.getY() + parentSize.getY() * this.y / 100D));
}
int x = thisPos.getX();
@@ -99,12 +125,12 @@ public class WidgetGeometry {
}
public Point getSize(IWidget parent){
if (this.isFractional)
return new Point(this.absSX, this.absSY);
if (!this.fracSize)
return new Point((int)this.sx, (int)this.sy);
else {
Point parentSize = parent.getSize();
Point thisSize = new Point ( MathHelper.ceiling_double_int(parentSize.getX() * this.relSX / 100D),
MathHelper.ceiling_double_int(parentSize.getY() * this.relSY / 100D));
Point thisSize = new Point ( MathHelper.ceiling_double_int(parentSize.getX() * this.sx / 100D),
MathHelper.ceiling_double_int(parentSize.getY() * this.sy / 100D));
return thisSize;
}
}