source: lost-perception/gamegui/ScrollList.java@ e5d2936

Last change on this file since e5d2936 was ebd3538, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

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