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