Initial commit

This commit is contained in:
Chicken-Bomes committed 2014-04-29 00:35:55 +10:00
commit 0d94e9ac7f
78 files changed
+7045

No files matched your search

+75
View File
@@ -0,0 +1,75 @@
package codechicken.core.gui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
public class GuiCCButton extends GuiWidget
{
public String text;
public String actionCommand;
private boolean isEnabled = true;
public boolean visible = true;
public GuiCCButton(int x, int y, int width, int height, String text)
{
super(x, y, width, height);
this.text = text;
}
public void setText(String s)
{
text = s;
}
public boolean isEnabled()
{
return isEnabled;
}
public void setEnabled(boolean b)
{
isEnabled = b;
}
@Override
public void mouseClicked(int x, int y, int button)
{
if(isEnabled && pointInside(x, y) && actionCommand != null)
{
sendAction(actionCommand, button);
Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F));
}
}
@Override
public void draw(int mousex, int mousey, float frame)
{
if(!visible)
return;
renderEngine.bindTexture(guiTex);
GL11.glColor4f(1, 1, 1, 1);
boolean mouseover = pointInside(mousex, mousey);
int state = !isEnabled ? 0 : mouseover ? 2 : 1;
drawTexturedModalRect(x, y, 0, 46 + state * 20, width / 2, height / 2);//top left
drawTexturedModalRect(x + width / 2, y, 200 - width / 2, 46 + state * 20, width / 2, height / 2);//top right
drawTexturedModalRect(x, y + height / 2, 0, 46 + state * 20 + 20 - height / 2, width / 2, height / 2);//bottom left
drawTexturedModalRect(x + width / 2, y + height / 2, 200 - width / 2, 46 + state * 20 + 20 - height / 2, width / 2, height / 2);//bottom right
drawCenteredString(fontRenderer, text, x + width / 2, y + (height - 8) / 2, getTextColour(mousex, mousey));
}
public int getTextColour(int mousex, int mousey)
{
return !isEnabled ? 0xFFA0A0A0 : pointInside(mousex, mousey) ? 0xFFFFFFA0 : 0xFFE0E0E0;
}
public GuiCCButton setActionCommand(String string)
{
actionCommand = string;
return this;
}
}
@@ -0,0 +1,189 @@
package codechicken.core.gui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ChatAllowedCharacters;
import org.lwjgl.input.Keyboard;
public class GuiCCTextField extends GuiWidget
{
private String text;
private boolean isFocused;
private boolean isEnabled;
public int maxStringLength;
public int cursorCounter;
public String actionCommand;
private String allowedCharacters;
public GuiCCTextField(int x, int y, int width, int height, String text)
{
super(x, y, width, height);
isFocused = false;
isEnabled = true;
this.text = text;
}
public GuiCCTextField setActionCommand(String s)
{
actionCommand = s;
return this;
}
public void setText(String s)
{
if(s.equals(text))
return;
String oldText = text;
text = s;
onTextChanged(oldText);
}
public void onTextChanged(String oldText)
{
}
public final String getText()
{
return text;
}
public final boolean isEnabled()
{
return isEnabled;
}
public void setEnabled(boolean b)
{
isEnabled = b;
if(!isEnabled && isFocused)
setFocused(false);
}
public final boolean isFocused()
{
return isFocused;
}
@Override
public void update()
{
cursorCounter++;
}
@Override
public void keyTyped(char c, int keycode)
{
if(!isEnabled || !isFocused)
return;
/*if(c == '\t')//tab
{
parentGuiScreen.selectNextField();
}*/
if(c == '\026')//paste
{
String s = GuiScreen.getClipboardString();
if(s == null || s.equals(""))
return;
for(int i = 0; i < s.length(); i++)
{
if(text.length() == maxStringLength)
return;
char tc = s.charAt(i);
if(canAddChar(tc))
setText(text + tc);
}
}
if(keycode == Keyboard.KEY_RETURN)
{
setFocused(false);
sendAction(actionCommand, getText());
}
if(keycode == Keyboard.KEY_BACK && text.length() > 0)
setText(text.substring(0, text.length() - 1));
if((text.length() < maxStringLength || maxStringLength == 0) && canAddChar(c))
setText(text + c);
}
public boolean canAddChar(char c)
{
return allowedCharacters == null ? ChatAllowedCharacters.isAllowedCharacter(c) : allowedCharacters.indexOf(c) >= 0;
}
@Override
public void mouseClicked(int x, int y, int button)
{
if(isEnabled && pointInside(x, y))
{
setFocused(true);
if(button == 1)
setText("");
}
else
setFocused(false);
}
public void setFocused(boolean focus)
{
if(focus == isFocused)
return;
isFocused = focus;
onFocusChanged();
}
public void onFocusChanged()
{
if(isFocused)
cursorCounter = 0;
}
@Override
public void draw(int i, int j, float f)
{
drawBackground();
drawText();
}
public void drawBackground()
{
drawRect(x - 1, y - 1, x + width + 1, y + height + 1, 0xffa0a0a0);
drawRect(x, y, x + width, y + height, 0xff000000);
}
public String getDrawText()
{
String s = getText();
if(isEnabled && isFocused && (cursorCounter / 6) % 2 == 0)
s+="_";
return s;
}
public void drawText()
{
drawString(fontRenderer, getDrawText(), x + 4, y + height/2 - 4, getTextColour());
}
public int getTextColour()
{
return isEnabled ? 0xe0e0e0 : 0x707070;
}
public GuiCCTextField setMaxStringLength(int i)
{
maxStringLength = i;
return this;
}
public GuiCCTextField setAllowedCharacters(String s)
{
allowedCharacters = s;
return this;
}
}
@@ -0,0 +1,141 @@
package codechicken.core.gui;
import java.awt.Point;
import java.util.ArrayList;
import codechicken.lib.gui.GuiDraw;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
public class GuiScreenWidget extends GuiScreen implements IGuiActionListener
{
public ArrayList<GuiWidget> widgets = new ArrayList<GuiWidget>();
public int xSize, ySize, guiTop, guiLeft;
public GuiScreenWidget()
{
this(176, 166);
}
public GuiScreenWidget(int xSize, int ySize)
{
super();
this.xSize = xSize;
this.ySize = ySize;
}
@Override
public void initGui()
{
guiTop = (height - ySize) / 2;
guiLeft = (width - xSize) / 2;
}
public void reset()
{
initGui();
widgets.clear();
addWidgets();
}
@Override
public void setWorldAndResolution(Minecraft mc, int i, int j)
{
boolean init = this.mc == null;
super.setWorldAndResolution(mc, i, j);
if(init)
addWidgets();
}
public void add(GuiWidget widget)
{
widgets.add(widget);
widget.onAdded(this);
}
@Override
public void drawScreen(int mousex, int mousey, float f)
{
GL11.glTranslated(guiLeft, guiTop, 0);
drawBackground();
for(GuiWidget widget : widgets)
widget.draw(mousex-guiLeft, mousey-guiTop, f);
drawForeground();
GL11.glTranslated(-guiLeft, -guiTop, 0);
}
public void drawBackground()
{
}
public void drawForeground()
{
}
@Override
protected void mouseClicked(int x, int y, int button)
{
super.mouseClicked(x, y, button);
for(GuiWidget widget : widgets)
widget.mouseClicked(x-guiLeft, y-guiTop, button);
}
@Override
protected void mouseMovedOrUp(int x, int y, int button)
{
super.mouseMovedOrUp(x, y, button);
for(GuiWidget widget : widgets)
widget.mouseMovedOrUp(x-guiLeft, y-guiTop, button);
}
@Override
protected void mouseClickMove(int x, int y, int button, long time)
{
super.mouseClickMove(x, y, button, time);
for(GuiWidget widget : widgets)
widget.mouseDragged(x-guiLeft, y-guiTop, button, time);
}
@Override
public void updateScreen()
{
super.updateScreen();
if(mc.currentScreen == this)
for(GuiWidget widget : widgets)
widget.update();
}
@Override
public void keyTyped(char c, int keycode)
{
super.keyTyped(c, keycode);
for(GuiWidget widget : widgets)
widget.keyTyped(c, keycode);
}
@Override
public void handleMouseInput()
{
super.handleMouseInput();
int i = Mouse.getEventDWheel();
if(i != 0)
{
Point p = GuiDraw.getMousePosition();
int scroll = i > 0 ? 1 : -1;
for(GuiWidget widget : widgets)
widget.mouseScrolled(p.x, p.y, scroll);
}
}
@Override
public void actionPerformed(String ident, Object... params)
{
}
public void addWidgets()
{
}
}
+361
View File
@@ -0,0 +1,361 @@
package codechicken.core.gui;
import net.minecraft.client.gui.GuiButton;
import org.lwjgl.input.Keyboard;
import codechicken.lib.math.MathHelper;
public abstract class GuiScrollSlot extends GuiWidget
{
protected GuiButton scrollupbutton;
protected GuiButton scrolldownbutton;
protected String actionCommand;
protected int scrollclicky = -1;
protected float scrollpercent;
protected int scrollmousey;
protected float percentscrolled;
protected int lastslotclicked = -1;
protected int lastbuttonclicked = -1;
protected long lastslotclicktime;
public boolean focused;
public int contentx;
public int contenty;
public int contentheight;
public boolean smoothScroll = true;
public GuiScrollSlot(int x, int y, int width, int height)
{
super(x, y, width, height);
setContentSize(x+3, y+2, height-2);
}
public GuiScrollSlot setActionCommand(String s)
{
actionCommand = s;
return this;
}
public void setSmoothScroll(boolean b)
{
smoothScroll = b;
}
public void setSize(int x, int y, int width, int height)
{
int cx = contentx-this.x;
int cy = contenty-this.y;
int ch = height-contentheight;
super.setSize(x, y, width, height);
setContentSize(x+cx, y+cy, height-ch);
}
public void setContentSize(int x, int y, int h)
{
this.contentx = x;
this.contenty = y;
this.contentheight = h;
}
public void registerButtons(GuiButton up, GuiButton down, String selectionCommand)
{
scrollupbutton = up;
scrolldownbutton = down;
actionCommand = selectionCommand;
}
public abstract int getSlotHeight();
protected abstract int getNumSlots();
public abstract void selectNext();
public abstract void selectPrev();
protected abstract void slotClicked(int slot, int button, int mousex, int mousey, boolean doubleclick);
protected abstract boolean isSlotSelected(int slot);
protected abstract void drawSlot(int slot, int x, int y, int mousex, int mousey, boolean selected, float frame);
protected void unfocus()
{
}
public void setFocused(boolean focus)
{
focused = focus;
if(!focused)
unfocus();
}
public void scrollUp()
{
scroll(-5);
}
public void scrollDown()
{
scroll(5);
}
public void scroll(int i)
{
percentscrolled += i/(float)contentheight * 100;
calculatePercentScrolled();
}
public int totalContentHeight()
{
return getNumSlots() * getSlotHeight();
}
public int getSlotY(int slot)
{
int scrolledPixels = (int)((totalContentHeight() - contentheight) * percentscrolled + 0.5);
if(!smoothScroll)
scrolledPixels = (int)(scrolledPixels/(double)getSlotHeight()+0.5)*getSlotHeight();
return contenty-scrolledPixels+slot*getSlotHeight();
}
public int getClickedSlot(int mousey)
{
if(mousey < contenty || mousey >= contenty+contentheight)
return -1;
for(int slot = 0; slot < getNumSlots(); slot++)
{
int sloty = getSlotY(slot);
if(mousey >= sloty && mousey < sloty + getSlotHeight())
return slot;
}
return -1;
}
public int getScrollBarWidth()
{
return 5;
}
public int getScrollBarHeight()
{
int sbarh = (int)((contentheight / (float)totalContentHeight()) * height);
if(sbarh > height)
{
return height;
}
else if(sbarh < height / 15)
{
return height / 15;
}
else
{
return sbarh;
}
}
public void calculatePercentScrolled()
{
int barempty = height - getScrollBarHeight();
if(scrollclicky >= 0)
{
int scrolldiff = scrollmousey - scrollclicky;
percentscrolled = scrolldiff / (float)barempty + scrollpercent;
}
percentscrolled = (float) MathHelper.clip(percentscrolled, 0, 1);
}
public void showSlot(int slot)
{
int sloty = getSlotY(slot);
if(sloty+getSlotHeight() > contenty+contentheight)
{
int diff = sloty-(contenty+contentheight-getSlotHeight());
percentscrolled += diff/(double)(totalContentHeight() - contentheight);
calculatePercentScrolled();
}
else if(sloty < contenty)
{
int diff = contenty-sloty;
percentscrolled -= diff/(double)(totalContentHeight() - contentheight);
calculatePercentScrolled();
}
}
public void processMouse(int mousex, int mousey)
{
if(scrollclicky >= 0)
{
int scrolldiff = mousey - scrollclicky;
int barupallowed = (int)((height - getScrollBarHeight()) * scrollpercent + 0.5);
int bardownallowed = (height - getScrollBarHeight()) - barupallowed;
if(-scrolldiff > barupallowed)
scrollmousey = scrollclicky - barupallowed;
else if(scrolldiff > bardownallowed)
scrollmousey = scrollclicky + bardownallowed;
else
scrollmousey = mousey;
calculatePercentScrolled();
}
}
public void actionPerformed(GuiButton guibutton)
{
if(!guibutton.enabled)
{
return;
}
if(scrollupbutton != null && guibutton.id == scrollupbutton.id)
{
scrollUp();
}
else if(scrolldownbutton != null && guibutton.id == scrolldownbutton.id)
{
scrollDown();
}
}
@Override
public void mouseClicked(int mousex, int mousey, int button)
{
boolean needsfocus = (mousex >= x && mousex < x + width &&
mousey >= y && mousey <= y + height);
if(needsfocus != focused)
{
setFocused(needsfocus);
}
int barempty = height - getScrollBarHeight();
int sbarx = x + width - getScrollBarWidth();
int sbary = y + (int)(barempty * percentscrolled + 0.5);
if(button == 0 &&
getScrollBarHeight() < height && //the scroll bar can move (not full length)
mousex >= sbarx && mousex <= x + width &&
mousey >= y && mousey <= y + height)//in the scroll pane
{
if(mousey < sbary)
{
percentscrolled = (mousey - y) / (float)barempty;
calculatePercentScrolled();
}
else if(mousey > sbary + getScrollBarHeight())
{
percentscrolled = (mousey - y - getScrollBarHeight() + 1) / (float)barempty;
calculatePercentScrolled();
}
else
{
scrollclicky = mousey;
scrollpercent = percentscrolled;
scrollmousey = mousey;
}
}
else if(mousex >= contentx && mousex < sbarx &&
mousey >= contenty && mousey <= contenty + contentheight)//in the box
{
int slot = getClickedSlot(mousey);
if(slot >= 0)
slotClicked(slot, button, mousex-contentx, mousey-getSlotY(slot),
slot == lastslotclicked && button == lastbuttonclicked && System.currentTimeMillis() - lastslotclicktime < 500);
lastslotclicked = slot;
lastbuttonclicked = button;
lastslotclicktime = System.currentTimeMillis();
}
}
@Override
public void mouseMovedOrUp(int mousex, int mousey, int button)
{
if(scrollclicky >= 0 && button == 0)//we were scrolling and we released mouse
scrollclicky = -1;
}
@Override
public void keyTyped(char c, int keycode)
{
if(!focused)
return;
if(keycode == Keyboard.KEY_UP)
selectPrev();
if(keycode == Keyboard.KEY_DOWN)
selectNext();
if(keycode == Keyboard.KEY_RETURN && actionCommand != null)
sendAction(actionCommand);
}
public void drawSlotBox(float frame)
{
drawRect(x, y, x + width, y + height, 0xff000000);
}
public void drawBackground(float frame)
{
}
public void drawOverlay(float frame)
{
//outlines
drawRect(x, y - 1, x + width, y, 0xffa0a0a0);//top
drawRect(x, y + height, x + width, y + height + 1, 0xffa0a0a0);//bottom
drawRect(x - 1, y - 1, x, y + height + 1, 0xffa0a0a0);//left
drawRect(x + width, y - 1, x + width + 1, y + height + 1, 0xffa0a0a0);//right
}
public void drawScrollBar(float frame)
{
int sbarw = getScrollBarWidth();
int sbarh = getScrollBarHeight();
int sbarx = x + width - sbarw;
int sbary = y + (int)((height - sbarh) * percentscrolled + 0.4999);
drawRect(sbarx, sbary, sbarx+sbarw, sbary+sbarh, 0xFF8B8B8B);//corners
drawRect(sbarx, sbary, sbarx+sbarw-1, sbary+sbarh - 1, 0xFFF0F0F0);//topleft up
drawRect(sbarx + 1, sbary + 1, sbarx+sbarw, sbary+sbarh, 0xFF555555);//bottom right down
drawRect(sbarx + 1, sbary + 1, sbarx+sbarw-1, sbary+sbarh - 1, 0xFFC6C6C6);//scrollbar
if(drawLineGuide())
drawRect(sbarx - 1, y, sbarx, y + height, 0xFF808080);//lineguide
}
public boolean drawLineGuide()
{
return true;
}
public void drawSlots(int mousex, int mousey, float frame)
{
for(int slot = 0; slot < getNumSlots(); slot++)
{
int sloty = getSlotY(slot);
if(sloty > contenty - getSlotHeight() && sloty < contenty + contentheight)
drawSlot(slot, contentx, sloty, mousex-contentx, mousey-sloty, isSlotSelected(slot), frame);
}
}
@Override
public void mouseDragged(int x, int y, int button, long time)
{
processMouse(x, y);
}
@Override
public void draw(int mousex, int mousey, float frame)
{
drawBackground(frame);
drawSlotBox(frame);
drawSlots(mousex, mousey, frame);
drawOverlay(frame);
drawScrollBar(frame);
}
}
+87
View File
@@ -0,0 +1,87 @@
package codechicken.core.gui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;
public class GuiWidget extends Gui
{
protected static final ResourceLocation guiTex = new ResourceLocation("textures/gui/widgets.png");
public GuiScreen parentScreen;
public TextureManager renderEngine;
public FontRenderer fontRenderer;
public int x;
public int y;
public int width;
public int height;
public GuiWidget(int x, int y, int width, int height)
{
setSize(x, y, width, height);
}
public void setSize(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public boolean pointInside(int px, int py)
{
return px >= x && px < x + width && py >= y && py < y + height;
}
public void sendAction(String actionCommand, Object... params)
{
sendAction(parentScreen, actionCommand, params);
}
public static void sendAction(GuiScreen screen, String actionCommand, Object... params)
{
if(actionCommand != null && screen instanceof IGuiActionListener)
((IGuiActionListener) screen).actionPerformed(actionCommand, params);
}
public void mouseClicked(int x, int y, int button)
{
}
public void mouseMovedOrUp(int x, int y, int button)
{
}
public void mouseDragged(int x, int y, int button, long time)
{
}
public void update()
{
}
public void draw(int mousex, int mousey, float frame)
{
}
public void keyTyped(char c, int keycode)
{
}
public void mouseScrolled(int x, int y, int scroll)
{
}
public void onAdded(GuiScreen s)
{
Minecraft mc = Minecraft.getMinecraft();
parentScreen = s;
renderEngine = mc.renderEngine;
fontRenderer = mc.fontRenderer;
}
}
@@ -0,0 +1,6 @@
package codechicken.core.gui;
public interface IGuiActionListener
{
public void actionPerformed(String actionCommand, Object... params);
}