1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.FontMetrics;
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.event.MouseEvent;
|
---|
7 | import java.awt.Font;
|
---|
8 | import java.util.ArrayList;
|
---|
9 |
|
---|
10 | public class TabbedWindow extends Member {
|
---|
11 | private ArrayList<Window> windows;
|
---|
12 | private ArrayList<String> windowLabels;
|
---|
13 | private Window activeWindow;
|
---|
14 | private int tabHeight;
|
---|
15 | private Font tabFont;
|
---|
16 |
|
---|
17 | public TabbedWindow(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final int newTabHeight, final Font newFont) {
|
---|
18 | super(newName, newX, newY, newWidth, newHeight);
|
---|
19 | this.windows = new ArrayList<Window>();
|
---|
20 | this.windowLabels = new ArrayList<String>();
|
---|
21 | this.tabHeight = newTabHeight;
|
---|
22 | this.activeWindow = null;
|
---|
23 | this.tabFont = newFont;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public void add(final Window newWindow, final String name) {
|
---|
27 | newWindow.offset(this.getX(), this.getY() + this.tabHeight);
|
---|
28 | if (this.activeWindow == null) {
|
---|
29 | this.activeWindow = newWindow;
|
---|
30 | }
|
---|
31 | this.windows.add(newWindow);
|
---|
32 | this.windowLabels.add(name);
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void clear() {
|
---|
37 | this.activeWindow = this.windows.get(0);
|
---|
38 | for (int x = 0; x < this.windows.size(); ++x) {
|
---|
39 | this.windows.get(x).clear();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void offset(final int xOffset, final int yOffset) {
|
---|
44 | super.offset(xOffset, yOffset);
|
---|
45 | for (int x = 0; x < this.windows.size(); ++x) {
|
---|
46 | this.windows.get(x).offset(xOffset, yOffset);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public Window getWindow(final String aName) {
|
---|
51 | for (int x = 0; x < this.windows.size(); ++x) {
|
---|
52 | if (this.windows.get(x).getName().equals(aName)) {
|
---|
53 | return this.windows.get(x);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | return null;
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public boolean handleEvent(final MouseEvent e) {
|
---|
61 | for (int x = 0; x < this.windows.size(); ++x) {
|
---|
62 | if (this.isClicked(this.getX() + x * this.getWidth() / this.windows.size(), this.getY(), this.getWidth() / this.windows.size(), this.tabHeight, e.getX(), e.getY())) {
|
---|
63 | this.activeWindow = this.windows.get(x);
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | return this.activeWindow.handleEvent(e);
|
---|
68 | }
|
---|
69 |
|
---|
70 | private boolean isClicked(final int x, final int y, final int width, final int height, final int mouseX, final int mouseY) {
|
---|
71 | return x <= mouseX && mouseX <= x + width && y <= mouseY && mouseY <= y + height;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void draw(final Graphics g) {
|
---|
76 | final FontMetrics metrics = g.getFontMetrics(this.tabFont);
|
---|
77 | g.setColor(Color.black);
|
---|
78 | g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
---|
79 | g.setFont(this.tabFont);
|
---|
80 | for (int x = 0; x < this.windows.size(); ++x) {
|
---|
81 | g.setColor(Color.green);
|
---|
82 | g.drawString(this.windowLabels.get(x), this.getX() + x * this.getWidth() / this.windows.size() + (this.getWidth() / this.windows.size() - metrics.stringWidth(this.windowLabels.get(x))) / 2, this.getY() + (this.tabHeight + metrics.getHeight()) / 2 - 2);
|
---|
83 | g.setColor(Color.red);
|
---|
84 | g.drawLine(this.getX() + x * this.getWidth() / this.windows.size(), this.getY(), this.getX() + x * this.getWidth() / this.windows.size(), this.getY() + this.tabHeight);
|
---|
85 | if (this.windows.get(x).equals(this.activeWindow)) {
|
---|
86 | this.windows.get(x).draw(g);
|
---|
87 | g.setColor(Color.red);
|
---|
88 | g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
---|
89 | g.drawLine(this.getX(), this.getY() + this.tabHeight, this.getX() + x * this.getWidth() / this.windows.size(), this.getY() + this.tabHeight);
|
---|
90 | g.drawLine(this.getX() + (x + 1) * this.getWidth() / this.windows.size(), this.getY() + this.tabHeight, this.getX() + this.getWidth(), this.getY() + this.tabHeight);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public String getActive() {
|
---|
96 | if (this.activeWindow != null) {
|
---|
97 | return this.activeWindow.getName();
|
---|
98 | }
|
---|
99 | return "";
|
---|
100 | }
|
---|
101 | }
|
---|