public class RadialMenu {
public int x, y, u, v, select = -1;
public float radius, radiusDouble, radiusUV, radiusDoubleUV, initialDegree;
List<Segment> segments = new ArrayList<Segment>();
public RadialMenu(int x, int y, int u, int v, float radius, float radiusDouble, float radiusUV, float initialDegree) {
this.x = x;
this.y = y;
this.u = u;
this.v = v;
this.radius = radius;
this.radiusDouble = radiusDouble;
this.radiusUV = radiusUV;
radiusDoubleUV = radiusUV / (radius / radiusDouble);
this.initialDegree = initialDegree;
}
public void action(int select, Segment segment) {
//to-do
}
public void mouseClicked(int mouseX, int mouseY, int button) {
if (button == 0) {
float a = 360F / (float)segments.size();
float offsetMX = mouseX - x;
float offsetMY = mouseY - y;
float dist = MathHelper.sqrt(offsetMX * offsetMX + offsetMY * offsetMY);
if (dist < radius * (select != -1 ? 1.125 : 1) && dist >= radiusDouble) {
double mouseR = Math.toDegrees(Math.atan2(offsetMX, offsetMY));
mouseR += initialDegree;
if (mouseR < 0) mouseR += 360D;
if (mouseR > 360D) mouseR -= 360D;
select = (int)(mouseR / a);
if ((select >= segments.size() || select < 0) && select != -1)
select = -1;
else
action(select, segments.get(select));
//System.out.println("mouseClicked " + select);
}
}
}
public void draw(int mouseX, int mouseY, float parTick) {
float a = 360F / (float)segments.size();
float b;
float degree, sin, cos;
float osin = 0;
float ocos = 0;
float offsetU = 1F / 256;
float offsetV = 1F / 256;
float offsetMX = mouseX - x;
float offsetMY = mouseY - y;
float dist = MathHelper.sqrt(offsetMX * offsetMX + offsetMY * offsetMY);
if (dist < radius * (select != -1 ? 1.125 : 1) && dist >= radiusDouble) {
double mouseR = Math.toDegrees(Math.atan2(offsetMX, offsetMY));
mouseR += initialDegree;
if (mouseR < 0) mouseR += 360D;
if (mouseR >= 360D) mouseR -= 360D;
select = (int)(mouseR / a);
} else
select = -1;
if ((select >= segments.size() || select < 0) && select != -1)
select = -1;
GlStateManager.disableTexture2D();
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferbuilder = tessellator.getBuffer();
bufferbuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_COLOR);
//bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX);
int segmet;
if (segments.size() >= 36)
segmet = segments.size();
else
segmet = (int)((36 / segments.size()) * segments.size());
a = 360F / segmet;
int test = segmet / segments.size();
int c;
for (int i = 0; i <= segmet; ++i) {
b = a * i;
degree = (b + initialDegree) * (float)Math.PI / 180F;
sin = MathHelper.sin(degree);
cos = MathHelper.cos(degree);
if (select != -1) {
c = select * test;
if (i > c && i <= c + test) {
//bufferbuilder.setColorRGBA_F(0.75F, 0.5F, 0F, 0.5F);
//bufferbuilder.color(1F, 1F, 1F, 0.75F);
bufferbuilder.pos(x + (sin * radius * 1.125), y + (cos * radius * 1.125), 0D)
.tex((u + (sin * radiusUV)) * offsetU, (v + (cos * radiusUV)) * offsetV)
.color(255, 255, 255, 127)
.endVertex();
bufferbuilder.pos(x + (sin * radiusDouble), y + (cos * radiusDouble), 0D)
.tex((u + (sin * radiusDoubleUV)) * offsetU, (v + (cos * radiusDoubleUV)) * offsetV)
.color(255, 255, 255, 127)
.endVertex();
bufferbuilder.pos(x + (osin * radiusDouble), y + (ocos * radiusDouble), 0D)
.tex((u + (osin * radiusDoubleUV)) * offsetU, (v + (ocos * radiusDoubleUV)) * offsetV)
.color(255, 255, 255, 127)
.endVertex();
bufferbuilder.pos(x + (osin * radius * 1.125), y + (ocos * radius * 1.125), 0D)
.tex((u + (osin * radiusUV)) * offsetU, (v + (ocos * radiusUV)) * offsetV)
.color(255, 255, 255, 127)
.endVertex();
osin = sin;
ocos = cos;
continue;
}
}
//bufferbuilder.setColorRGBA_F(0.125F, 0.5F, 1F, 0.5F);
//bufferbuilder.color(0.375F, 0.375F, 0.375F, 0.25F);
bufferbuilder.pos(x + (sin * radius), y + (cos * radius), 0D)
.tex((u + (sin * radiusUV)) * offsetU, (v + (cos * radiusUV)) * offsetV)
.color(64, 64, 64, 96)
.endVertex();
bufferbuilder.pos(x + (sin * radiusDouble), y + (cos * radiusDouble), 0D)
.tex((u + (sin * radiusDoubleUV)) * offsetU, (v + (cos * radiusDoubleUV)) * offsetV)
.color(64, 64, 64, 96)
.endVertex();
bufferbuilder.pos(x + (osin * radiusDouble), y + (ocos * radiusDouble), 0D)
.tex((u + (osin * radiusDoubleUV)) * offsetU, (v + (ocos * radiusDoubleUV)) * offsetV)
.color(64, 64, 64, 96)
.endVertex();
bufferbuilder.pos(x + (osin * radius), y + (ocos * radius), 0D)
.tex((u + (osin * radiusUV)) * offsetU, (v + (ocos * radiusUV)) * offsetV)
.color(64, 64, 64, 96)
.endVertex();
osin = sin;
ocos = cos;
}
tessellator.draw();
GlStateManager.color(1F, 1F, 1F, 1F);
GlStateManager.disableBlend();
GlStateManager.enableTexture2D();
GlStateManager.enableRescaleNormal();
RenderHelper.enableGUIStandardItemLighting();
RenderItem ri = Minecraft.getMinecraft().getRenderItem();
a = 360F / segments.size();
for (int i = 0; i < segments.size(); ++i) {
b = a * i;
c = (int)((b + (a * (i + 1))) / 2);
degree = (c + initialDegree) * (float)Math.PI / 180F;
sin = MathHelper.sin(degree);
cos = MathHelper.cos(degree);
ri.renderItemIntoGUI(segments.get(i).itemStack,
x + (int)(sin * ((radius + radiusDouble) / 2)) - 8,
y + (int)(cos * ((radius + radiusDouble) / 2)) - 8);
}
RenderHelper.disableStandardItemLighting();
GlStateManager.disableRescaleNormal();
if (select != -1)
Minecraft.getMinecraft().fontRenderer.drawStringWithShadow(segments.get(select).text,
x - Minecraft.getMinecraft().fontRenderer.getStringWidth(segments.get(select).text) / 2, y - 10, 0xFFFFFF);
}
public static class Segment {
public final String text;
public final ItemStack itemStack;
public Segment(String text, ItemStack itemStack) {
this.text = text;
this.itemStack = itemStack;
}
}
}