Added ClickCounter

Added GuiScrollPane
Generalised update checking and added general notification checking
Fixed some inconsistent exception logging
Updated to 1.7.10
This commit is contained in:
Chicken-Bomes committed 2014-06-29 20:38:07 +10:00
1 parent 4751d252ea
commit e373ce7860
11 files changed
+479 -403

No files matched your search

@@ -0,0 +1,52 @@
package codechicken.core.gui;
import java.util.Map;
import java.util.TreeMap;
public class ClickCounter<T>
{
public class ClickCount
{
public T clicked;
public long time;
public int count;
public boolean update(T clicked) {
if(this.clicked != clicked) {
this.clicked = clicked;
count = 0;
time = Long.MIN_VALUE;
return false;
}
return true;
}
}
public Map<Integer, ClickCount> buttons = new TreeMap<Integer, ClickCount>();
public ClickCount getCount(int button) {
ClickCount c = buttons.get(button);
if(c == null)
buttons.put(button, c = new ClickCount());
return c;
}
public void mouseDown(T clicked, int button) {
ClickCount c = getCount(button);
c.update(clicked);
}
public int mouseUp(T clicked, int button) {
ClickCount c = getCount(button);
if(!c.update(clicked))
return 0;
long time = System.currentTimeMillis();
if(time-c.time < 500)
c.count++;
else
c.count = 1;
c.time = time;
return c.count;
}
}
+215
View File
@@ -0,0 +1,215 @@
package codechicken.core.gui;
import codechicken.lib.math.MathHelper;
import codechicken.lib.vec.Rectangle4i;
import java.awt.*;
public abstract class GuiScrollPane extends GuiWidget
{
protected int scrollclicky = -1;
protected float scrollpercent;
protected int scrollmousey;
protected float percentscrolled;
public int marginleft;
public int margintop;
public int marginright;
public int marginbottom;
public GuiScrollPane(int x, int y, int width, int height) {
super(x, y, width, height);
}
public void setMargins(int left, int top, int right, int bottom) {
marginleft = left;
margintop = top;
marginright = right;
marginbottom = bottom;
}
public Rectangle windowBounds() {
return new Rectangle(x+marginleft, y+margintop, width-marginleft-marginright, height-margintop-marginbottom);
}
public abstract int contentHeight();
public Dimension scrollbarDim() {
int h = (int) ((windowBounds().getHeight() / contentHeight()) * height);
return new Dimension(5, h > height ? height : h < height / 15 ? height / 15 : h);
}
public Rectangle scrollbarBounds() {
Dimension dim = scrollbarDim();
return new Rectangle(
x + width - dim.width, y + (int) ((height - dim.height) * percentscrolled + 0.4999),
dim.width, dim.height);
}
public void scrollUp() {
scroll(-5);
}
public void scrollDown() {
scroll(5);
}
public void scroll(int i) {
percentscrolled += i * height / (float)(2*contentHeight());
calculatePercentScrolled();
}
public void calculatePercentScrolled() {
int barempty = height - scrollbarDim().height;
if (isScrolling()) {
int scrolldiff = scrollmousey - scrollclicky;
percentscrolled = scrolldiff / (float) barempty + scrollpercent;
}
percentscrolled = (float) MathHelper.clip(percentscrolled, 0, 1);
}
public boolean isScrolling() {
return scrollclicky >= 0;
}
public boolean hasScrollbar() {
return contentHeight() > height;
}
/**
* @return -1 for left, 0 for none, 1 for right
*/
public int scrollbarGuideAlignment() {
return -1;
}
public void showSlot(int sloty, int slotHeight) {
Rectangle w = windowBounds();
if (sloty + slotHeight > w.y + w.height) {
int diff = sloty - (w.y + w.height - slotHeight);
percentscrolled += diff / (double) (contentHeight() - w.height);
calculatePercentScrolled();
} else if (sloty < w.y) {
int diff = w.y - sloty;
percentscrolled -= diff / (double) (contentHeight() - w.height);
calculatePercentScrolled();
}
}
@Override
public void mouseClicked(int mx, int my, int button) {
Rectangle sbar = scrollbarBounds();
Rectangle w = windowBounds();
int barempty = height - sbar.height;
if (button == 0 &&
sbar.height < height && //the scroll bar can move (not full length)
mx >= sbar.x && mx <= sbar.x + sbar.width &&
my >= y && my <= y + height)//in the scroll pane
{
if (my < sbar.y) {
percentscrolled = (my - y) / (float) barempty;
calculatePercentScrolled();
} else if (my > sbar.y+sbar.height) {
percentscrolled = (my - y - sbar.height + 1) / (float) barempty;
calculatePercentScrolled();
} else {
scrollclicky = my;
scrollpercent = percentscrolled;
scrollmousey = my;
}
} else if (w.contains(mx, my))
slotDown(mx - w.x, my - w.y + scrolledPixels(), button);
}
/**
* Mouse down on slot area
* Coordinates relative to slot content area
*/
public void slotDown(int mx, int my, int button) {}
/**
* Mouse up on slot area
* Coordinates relative to slot content area
*/
public void slotUp(int mx, int my, int button) {}
@Override
public void mouseMovedOrUp(int mx, int my, int button) {
Rectangle w = windowBounds();
if (isScrolling() && button == 0)//we were scrolling and we released mouse
scrollclicky = -1;
else if (w.contains(mx, my))
slotUp(mx - w.x, my - w.y + scrolledPixels(), button);
}
@Override
public void mouseDragged(int mousex, int mousey, int button, long time) {
if (isScrolling()) {
int scrolldiff = mousey - scrollclicky;
int sbarh = scrollbarDim().height;
int barupallowed = (int) ((height - sbarh) * scrollpercent + 0.5);
int bardownallowed = (height - sbarh) - barupallowed;
if (-scrolldiff > barupallowed)
scrollmousey = scrollclicky - barupallowed;
else if (scrolldiff > bardownallowed)
scrollmousey = scrollclicky + bardownallowed;
else
scrollmousey = mousey;
calculatePercentScrolled();
}
}
public int scrolledPixels() {
int scrolled = (int) ((contentHeight() - windowBounds().height) * percentscrolled + 0.5);
if (scrolled < 0) scrolled = 0;
return scrolled;
}
public Rectangle4i bounds() {
return new Rectangle4i(x, y, width, height);
}
public boolean contains(int mx, int my) {
return bounds().contains(mx, my);
}
@Override
public void draw(int mx, int my, float frame) {
Rectangle w = windowBounds();
drawBackground(frame);
drawContent(mx-w.x, my+scrolledPixels()-w.y, frame);
drawOverlay(frame);
drawScrollbar(frame);
}
public void drawBackground(float frame) {
drawRect(x, y, x + width, y + height, 0xff000000);
}
public abstract void drawContent(int mx, int my, 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) {
Rectangle r = scrollbarBounds();
drawRect(r.x, r.y, r.x + r.width, r.y + r.height, 0xFF8B8B8B);//corners
drawRect(r.x, r.y, r.x + r.width - 1, r.y + r.height - 1, 0xFFF0F0F0);//topleft up
drawRect(r.x + 1, r.y + 1, r.x + r.width, r.y + r.height, 0xFF555555);//bottom right down
drawRect(r.x + 1, r.y + 1, r.x + r.width - 1, r.y + r.height - 1, 0xFFC6C6C6);//scrollbar
int algn = scrollbarGuideAlignment();
if (algn != 0)
drawRect(algn > 0 ? r.x + r.width : r.x - 1, y, r.x, y + height, 0xFF808080);//lineguide
}
}
+111 -315
View File
@@ -1,361 +1,157 @@
package codechicken.core.gui;
import net.minecraft.client.gui.GuiButton;
import org.lwjgl.input.Keyboard;
import codechicken.lib.math.MathHelper;
import java.awt.*;
public abstract class GuiScrollSlot extends GuiWidget
{
protected GuiButton scrollupbutton;
protected GuiButton scrolldownbutton;
public abstract class GuiScrollSlot extends GuiScrollPane
{
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;
protected ClickCounter<Integer> click = new ClickCounter<Integer>();
public boolean smoothScroll = true;
public GuiScrollSlot(int x, int y, int width, int height)
{
public GuiScrollSlot(int x, int y, int width, int height) {
super(x, y, width, height);
setContentSize(x+3, y+2, height-2);
setMargins(3, 2, 3, 2);
}
public GuiScrollSlot setActionCommand(String s)
{
public GuiScrollSlot setActionCommand(String s) {
actionCommand = s;
return this;
}
public void setSmoothScroll(boolean b)
{
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();
public abstract int getSlotHeight(int slot);
protected abstract int getNumSlots();
public abstract void selectNext();
public abstract void selectPrev();
public void selectNext() {}
protected abstract void slotClicked(int slot, int button, int mousex, int mousey, boolean doubleclick);
public void selectPrev() {}
protected abstract boolean isSlotSelected(int slot);
/**
* Coordinates are relative to slot area
*/
protected abstract void slotClicked(int slot, int button, int mx, int my, int count);
protected abstract void drawSlot(int slot, int x, int y, int mousex, int mousey, boolean selected, float frame);
protected abstract void drawSlot(int slot, int x, int y, int mx, int my, float frame);
protected void unfocus()
{
protected void unfocus() {
}
public void setFocused(boolean focus)
{
public void setFocused(boolean focus) {
focused = focus;
if(!focused)
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();
@Override
public int contentHeight() {
return getSlotY(getNumSlots());
}
public int getClickedSlot(int mousey)
{
if(mousey < contenty || mousey >= contenty+contentheight)
public int getSlotY(int slot) {
int h = 0;
for(int i = 0; i < slot; i++)
h+=getSlotHeight(i);
return h;
}
public int getSlot(int my) {
if (my < 0)
return -1;
for(int slot = 0; slot < getNumSlots(); slot++)
{
int sloty = getSlotY(slot);
if(mousey >= sloty && mousey < sloty + getSlotHeight())
return slot;
int y = 0;
for(int i = 0; i < getNumSlots(); i++) {
int h = getSlotHeight(i);
if(my >= y && my < y+h)
return i;
y+=h;
}
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 int getClickedSlot(int my) {
return getSlot(my-windowBounds().y+scrolledPixels());
}
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();
@Override
public int scrolledPixels() {
int scrolled = super.scrolledPixels();
if(!smoothScroll) {
int slot = getSlot(scrolled);
int sloty = getSlotY(slot);
int sloth = getSlotHeight(slot);
scrolled = sloty+(int)((scrolled-sloty)/(double)sloth+0.5)*sloth;
}
return scrolled;
}
public void actionPerformed(GuiButton guibutton)
{
if(!guibutton.enabled)
{
/**
* @return -1 for left, 1 for right
*/
public int scrollbarAlignment() {
return 1;
}
@Override
public int scrollbarGuideAlignment() {
return scrollbarAlignment();
}
@Override
public Rectangle scrollbarBounds() {
Rectangle r = super.scrollbarBounds();
if(scrollbarAlignment() == -1)
r.x = x;
return r;
}
public void showSlot(int slot) {
showSlot(getSlotY(slot), getSlotHeight(slot));
}
@Override
public void slotDown(int mx, int my, int button) {
int slot = getSlot(my);
click.mouseDown(slot >= 0 ? slot : null, button);
}
@Override
public void slotUp(int mx, int my, int button) {
int slot = getSlot(my);
int c = click.mouseUp(slot >= 0 ? slot : null, button);
if(c > 0 && slot >= 0)
slotClicked(slot, button, mx, my-getSlotY(slot), c);
}
@Override
public void keyTyped(char c, int keycode) {
if (!focused)
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)
if (keycode == Keyboard.KEY_UP)
selectPrev();
if(keycode == Keyboard.KEY_DOWN)
if (keycode == Keyboard.KEY_DOWN)
selectNext();
if(keycode == Keyboard.KEY_RETURN && actionCommand != null)
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 drawContent(int mx, int my, float frame) {
int scrolled = scrolledPixels();
Rectangle w = windowBounds();
int y = 0;
for (int slot = 0; slot < getNumSlots(); slot++) {
int h = getSlotHeight(slot);
if (y+h > scrolled && y < scrolled+w.height)
drawSlot(slot, w.x, w.y+y-scrolledPixels(), mx, my-y, frame);
y+=h;
}
}
@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);
}
}