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