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