Changeset 8edd04e in lost-haven for gamegui/ScrollList.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (4 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gamegui/ScrollList.java
r155577b r8edd04e 2 2 3 3 import java.awt.*; 4 import java.awt.event. *;4 import java.awt.event.MouseEvent; 5 5 import java.awt.image.BufferedImage; 6 import java.util. *;6 import java.util.ArrayList; 7 7 8 8 public class ScrollList extends Member { 9 private ArrayList<Listable> lstObjects; 10 private Listable selectedItem; 11 private Font font; 12 private FontMetrics metrics; 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 this.metrics = metrics; 23 textStart = 0; 24 change = false; 25 } 26 27 public ArrayList<Listable> getList() { 28 return lstObjects; 29 } 30 31 public Listable getSelected() { 32 return selectedItem; 33 } 34 35 public boolean isChanged() { 36 return change; 37 } 38 39 public void changeHandled() { 40 change = false; 41 } 42 43 public void deselect() { 44 selectedItem = null; 45 } 46 47 public void clear() { 48 lstObjects = new ArrayList<Listable>(); 49 selectedItem = null; 50 textStart = 0; 51 changeHandled(); 52 } 53 54 public boolean handleEvent(MouseEvent e) { 55 if(getX() < e.getX() && e.getX() < getX()+getWidth()) { 56 if(getY() < e.getY() && getY()-textStart+2 < e.getY() && e.getY() < getY()+getHeight() && e.getY() < getY()+lstObjects.size()*15-textStart+2) { 57 selectedItem = lstObjects.get((e.getY()-getY()+textStart-2)/15); 58 change = true; 59 return true; 60 } 61 } 62 63 if(!getScrollBar().handleEvent(e)) 64 return false; 65 66 if(e.getY() < getY()+getScrollBar().getWidth()) { 67 changeTextStart(-30); 68 }else if(getY()+getHeight()-getScrollBar().getWidth() < e.getY()) { 69 changeTextStart(30); 70 } 71 72 return true; 73 } 74 75 private void changeTextStart(int increment) { 76 textStart += increment; 77 78 if(lstObjects.size()*15+6>getHeight() && textStart >= lstObjects.size()*15+6-getHeight()) { 79 textStart = lstObjects.size()*15+6-getHeight(); 80 getScrollBar().setPosition(getScrollBar().getMaxSize()-getScrollBar().getSize()); 81 }else if(textStart < 0 || lstObjects.size()*15+6<=getHeight()) { 82 textStart = 0; 83 getScrollBar().setPosition(0); 84 }else 85 getScrollBar().setPosition(textStart*getScrollBar().getMaxSize()/(lstObjects.size()*15+6)); 86 } 87 88 public void draw(Graphics g) { 89 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 90 GraphicsDevice device = env.getDefaultScreenDevice(); 91 GraphicsConfiguration gc = device.getDefaultConfiguration(); 92 93 BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight()); 94 Graphics2D srcGraphics = source.createGraphics(); 95 96 srcGraphics.setColor(Color.green); 97 srcGraphics.setFont(font); 98 99 for(int x=0; x<lstObjects.size(); x++) { 100 if(selectedItem != null && selectedItem.equals(lstObjects.get(x))) { 101 srcGraphics.setColor(Color.blue); 102 srcGraphics.fillRect(0, x*15-textStart+3, getWidth(), 15); 103 srcGraphics.setColor(Color.green); 104 } 105 106 lstObjects.get(x).drawListString(5, metrics.getHeight()+x*15-textStart, srcGraphics); 107 } 108 109 g.drawImage(source, getX(), getY(), null); 110 111 g.setColor(Color.red); 112 g.drawRect(getX(), getY(), getWidth(), getHeight()); 113 114 if(lstObjects.size()*15+6 > getHeight()) 115 getScrollBar().setSize(getScrollBar().getMaxSize()*getHeight()/(lstObjects.size()*15+6)); 116 else 117 getScrollBar().setSize(getScrollBar().getMaxSize()); 118 119 getScrollBar().draw(g); 120 } 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 } 121 124 }
Note:
See TracChangeset
for help on using the changeset viewer.