Files
CodeChickenCore/src/codechicken/core/gui/GuiScrollSlot.java
T

157 lines
3.9 KiB
Java
Raw Normal View History

2014-04-29 00:35:55 +10:00
package codechicken.core.gui;
import org.lwjgl.input.Keyboard;
2014-06-29 20:38:07 +10:00
import java.awt.*;
2014-04-29 00:35:55 +10:00
2014-06-29 20:38:07 +10:00
public abstract class GuiScrollSlot extends GuiScrollPane
{
2014-04-29 00:35:55 +10:00
protected String actionCommand;
public boolean focused;
2014-06-29 20:38:07 +10:00
protected ClickCounter<Integer> click = new ClickCounter<Integer>();
2014-04-29 00:35:55 +10:00
public boolean smoothScroll = true;
2014-06-29 20:38:07 +10:00
public GuiScrollSlot(int x, int y, int width, int height) {
2014-04-29 00:35:55 +10:00
super(x, y, width, height);
2014-06-29 20:38:07 +10:00
setMargins(3, 2, 3, 2);
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
public GuiScrollSlot setActionCommand(String s) {
2014-04-29 00:35:55 +10:00
actionCommand = s;
return this;
}
2014-06-29 20:38:07 +10:00
public void setSmoothScroll(boolean b) {
2014-04-29 00:35:55 +10:00
smoothScroll = b;
}
2014-06-29 20:38:07 +10:00
public abstract int getSlotHeight(int slot);
2014-04-29 00:35:55 +10:00
protected abstract int getNumSlots();
2014-06-29 20:38:07 +10:00
public void selectNext() {}
2014-04-29 00:35:55 +10:00
2014-06-29 20:38:07 +10:00
public void selectPrev() {}
2014-04-29 00:35:55 +10:00
2014-06-29 20:38:07 +10:00
/**
* Coordinates are relative to slot area
*/
protected abstract void slotClicked(int slot, int button, int mx, int my, int count);
2014-04-29 00:35:55 +10:00
2014-06-29 20:38:07 +10:00
protected abstract void drawSlot(int slot, int x, int y, int mx, int my, float frame);
2014-04-29 00:35:55 +10:00
2014-06-29 20:38:07 +10:00
protected void unfocus() {
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
public void setFocused(boolean focus) {
2014-04-29 00:35:55 +10:00
focused = focus;
2014-06-29 20:38:07 +10:00
if (!focused)
2014-04-29 00:35:55 +10:00
unfocus();
}
2014-06-29 20:38:07 +10:00
@Override
public int contentHeight() {
return getSlotY(getNumSlots());
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
public int getSlotY(int slot) {
int h = 0;
for(int i = 0; i < slot; i++)
h+=getSlotHeight(i);
return h;
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
public int getSlot(int my) {
if (my < 0)
2014-04-29 00:35:55 +10:00
return -1;
2014-06-29 20:38:07 +10:00
int y = 0;
for(int i = 0; i < getNumSlots(); i++) {
int h = getSlotHeight(i);
if(my >= y && my < y+h)
return i;
y+=h;
2014-04-29 00:35:55 +10:00
}
return -1;
}
2014-06-29 20:38:07 +10:00
public int getClickedSlot(int my) {
return getSlot(my-windowBounds().y+scrolledPixels());
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
@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;
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
return scrolled;
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
/**
* @return -1 for left, 1 for right
*/
public int scrollbarAlignment() {
return 1;
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
@Override
public int scrollbarGuideAlignment() {
return scrollbarAlignment();
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
@Override
public Rectangle scrollbarBounds() {
Rectangle r = super.scrollbarBounds();
if(scrollbarAlignment() == -1)
r.x = x;
return r;
2014-04-29 00:35:55 +10:00
}
2014-06-29 20:38:07 +10:00
public void showSlot(int slot) {
showSlot(getSlotY(slot), getSlotHeight(slot));
2014-04-29 00:35:55 +10:00
}
@Override
2014-06-29 20:38:07 +10:00
public void slotDown(int mx, int my, int button) {
int slot = getSlot(my);
click.mouseDown(slot >= 0 ? slot : null, button);
2014-04-29 00:35:55 +10:00
}
@Override
2014-06-29 20:38:07 +10:00
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);
2014-04-29 00:35:55 +10:00
}
@Override
2014-06-29 20:38:07 +10:00
public void keyTyped(char c, int keycode) {
if (!focused)
2014-04-29 00:35:55 +10:00
return;
2014-06-29 20:38:07 +10:00
if (keycode == Keyboard.KEY_UP)
2014-04-29 00:35:55 +10:00
selectPrev();
2014-06-29 20:38:07 +10:00
if (keycode == Keyboard.KEY_DOWN)
2014-04-29 00:35:55 +10:00
selectNext();
2014-06-29 20:38:07 +10:00
if (keycode == Keyboard.KEY_RETURN && actionCommand != null)
2014-04-29 00:35:55 +10:00
sendAction(actionCommand);
}
2014-06-29 20:38:07 +10:00
2014-04-29 00:35:55 +10:00
@Override
2014-06-29 20:38:07 +10:00
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;
}
2014-04-29 00:35:55 +10:00
}
}