[8edd04e] | 1 | package gamegui;
|
---|
| 2 |
|
---|
| 3 | import java.awt.*;
|
---|
| 4 | import java.awt.event.MouseEvent;
|
---|
| 5 | import java.awt.image.BufferedImage;
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 |
|
---|
| 8 | public class ScrollList extends Member {
|
---|
| 9 |
|
---|
| 10 | private ArrayList<Listable> lstObjects;
|
---|
| 11 | private Listable selectedItem;
|
---|
| 12 | private Font font;
|
---|
| 13 | private int fontHeight;
|
---|
| 14 | private int textStart;
|
---|
| 15 | private boolean change;
|
---|
| 16 |
|
---|
| 17 | public ScrollList(String newName, int newX, int newY, int newWidth, int newHeight, Font font, FontMetrics metrics) {
|
---|
| 18 | super(newName, newX, newY, newWidth, newHeight);
|
---|
| 19 | this.lstObjects = new ArrayList<Listable>();
|
---|
| 20 | this.selectedItem = null;
|
---|
| 21 | this.font = font;
|
---|
| 22 | if (metrics == null) {
|
---|
| 23 | this.fontHeight = 0;
|
---|
| 24 | } else {
|
---|
| 25 | this.fontHeight = metrics.getHeight();
|
---|
| 26 | }
|
---|
| 27 | this.textStart = 0;
|
---|
| 28 | this.change = false;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public ArrayList<Listable> getList() {
|
---|
| 32 | return this.lstObjects;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public Listable getSelected() {
|
---|
| 36 | return this.selectedItem;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public boolean isChanged() {
|
---|
| 40 | return this.change;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public void changeHandled() {
|
---|
| 44 | this.change = false;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public void deselect() {
|
---|
| 48 | this.selectedItem = null;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public void clear() {
|
---|
| 52 | this.lstObjects.clear();
|
---|
| 53 | this.selectedItem = null;
|
---|
| 54 | this.textStart = 0;
|
---|
| 55 | changeHandled();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public boolean handleEvent(MouseEvent e) {
|
---|
| 59 | if (!getScrollBar().handleEvent(e))
|
---|
| 60 | return false;
|
---|
| 61 | if (e.getY() < getY() + getScrollBar().getWidth()) {
|
---|
| 62 | changeTextStart(-30);
|
---|
| 63 | } else if (getY() + getHeight() - getScrollBar().getWidth() < e.getY()) {
|
---|
| 64 | changeTextStart(30);
|
---|
| 65 | }
|
---|
| 66 | return true;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | private void changeTextStart(int increment) {
|
---|
| 70 | this.textStart += increment;
|
---|
| 71 | int listHeight = 0;
|
---|
| 72 | if (this.lstObjects.size() > 0) {
|
---|
| 73 | Listable e = this.lstObjects.get(0);
|
---|
| 74 | listHeight = e.getHeight() * (int)Math.ceil(this.lstObjects.size() / (getWidth() / e.getWidth())) + e.getYOffset();
|
---|
| 75 | }
|
---|
| 76 | if (listHeight > getHeight() && this.textStart >= listHeight - getHeight()) {
|
---|
| 77 | this.textStart = listHeight - getHeight();
|
---|
| 78 | getScrollBar().setPosition(getScrollBar().getMaxSize() - getScrollBar().getSize());
|
---|
| 79 | } else if (this.textStart < 0 || listHeight <= getHeight()) {
|
---|
| 80 | this.textStart = 0;
|
---|
| 81 | getScrollBar().setPosition(0);
|
---|
| 82 | } else {
|
---|
| 83 | getScrollBar().setPosition(this.textStart * getScrollBar().getMaxSize() / listHeight);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public int getTextStart() {
|
---|
| 88 | return this.textStart;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public void draw(Graphics g) {
|
---|
| 92 | GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
---|
| 93 | GraphicsDevice device = env.getDefaultScreenDevice();
|
---|
| 94 | GraphicsConfiguration gc = device.getDefaultConfiguration();
|
---|
| 95 | BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight());
|
---|
| 96 | Graphics2D srcGraphics = source.createGraphics();
|
---|
| 97 | srcGraphics.setColor(Color.green);
|
---|
| 98 | if (this.font != null) {
|
---|
| 99 | srcGraphics.setFont(this.font);
|
---|
| 100 | }
|
---|
| 101 | int listHeight = 0;
|
---|
| 102 | Listable e = null;
|
---|
| 103 | if (this.lstObjects.size() > 0) {
|
---|
| 104 | e = this.lstObjects.get(0);
|
---|
| 105 | listHeight = e.getHeight() * (int)Math.ceil(this.lstObjects.size() / (getWidth() / e.getWidth())) + e.getYOffset();
|
---|
| 106 | }
|
---|
| 107 | int numPerRow = 0;
|
---|
| 108 | if (e != null) {
|
---|
| 109 | numPerRow = getWidth() / e.getWidth();
|
---|
| 110 | }
|
---|
| 111 | for (int x = 0; x < this.lstObjects.size(); x++) {
|
---|
| 112 | ((Listable)this.lstObjects.get(x)).draw(e.getHeight() * x % numPerRow + e.getXOffset(), this.fontHeight + x / numPerRow * e.getHeight() - this.textStart, srcGraphics);
|
---|
| 113 | }
|
---|
| 114 | g.drawImage(source, getX(), getY(), null);
|
---|
| 115 | g.setColor(Color.red);
|
---|
| 116 | g.drawRect(getX(), getY(), getWidth(), getHeight());
|
---|
| 117 | if (listHeight > getHeight()) {
|
---|
| 118 | getScrollBar().setSize(getScrollBar().getMaxSize() * getHeight() / listHeight);
|
---|
| 119 | } else {
|
---|
| 120 | getScrollBar().setSize(getScrollBar().getMaxSize());
|
---|
| 121 | }
|
---|
| 122 | getScrollBar().draw(g);
|
---|
| 123 | }
|
---|
| 124 | }
|
---|