summaryrefslogtreecommitdiff
path: root/NET/worlds/console/WorldButton.java
blob: 8aba03194245538540f744cfb73449737cfeee6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package NET.worlds.console;

import NET.worlds.network.NetUpdate;
import NET.worlds.scape.TeleportAction;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.MenuItem;
import java.awt.PopupMenu;

class WorldButton extends TextImageButtons {
   private static final long serialVersionUID = 8349532719484982330L;
   int buttonX;
   int buttonY;
   String pkg;
   int privacyLevel;
   boolean isLoaded;
   String readableName;
   DefaultConsole console;
   static String currentPackageName = "";
   private static Font wfont = new Font(Console.message("UniverseFont"), 0, 10);
   private static final int[] xText = new int[1];
   PopupMenu lastPackageMenu;

   public WorldButton(
      boolean isLoaded,
      int buttonX,
      int buttonY,
      int buttonWidth,
      int buttonHeight,
      String[] texts,
      String pkg,
      int privacy,
      DefaultConsole console,
      ImageButtonsCallback handler
   ) {
      super(null, buttonWidth, intToInts(buttonHeight), xText, texts, handler, wfont);
      this.readableName = texts[0];
      this.buttonX = buttonX;
      this.buttonY = buttonY;
      this.pkg = pkg;
      this.isLoaded = isLoaded;
      this.privacyLevel = privacy;
      this.console = console;
      this.setSize(buttonWidth, buttonHeight);
      this.setWidth(buttonWidth);
      this.setHeight(buttonHeight);
   }

   public static Dimension measure(String text) {
      return TextImageButtons.measure(text, wfont);
   }

   public void doAction() {
      if (this.console == null || !this.showPackageMenu()) {
         NetUpdate.loadWorld(this.pkg, true);
      }
   }

   public boolean showPackageMenu() {
      if (this.lastPackageMenu != null) {
         this.remove(this.lastPackageMenu);
         this.lastPackageMenu = null;
      }

      this.lastPackageMenu = WorldsMarkPart.getPackageMenu(this.pkg);
      if (this.lastPackageMenu != null && this.lastPackageMenu.getItemCount() != 0) {
         MenuItem first = this.lastPackageMenu.getItem(0);
         if (first instanceof BookmarkMenuItem) {
            BookmarkMenuItem title = new BookmarkMenuItem(this.readableName + " World:", ((BookmarkMenuItem)first).getTarget());
            this.lastPackageMenu.insert(title, 0);
            this.lastPackageMenu.insertSeparator(1);
         }

         this.add(this.lastPackageMenu);

         try {
            this.lastPackageMenu.show(this, -15, -7);
         } catch (RuntimeException var3) {
            System.out.println("Warning - could not show teleport location menu.");
         }

         return true;
      } else {
         return false;
      }
   }

   @Override
   public void update(Graphics g) {
      this.paint(g);
   }

   @Override
   public boolean action(Event event, Object what) {
      Object target = event.target;
      if (target instanceof BookmarkMenuItem) {
         TeleportAction.teleport(((BookmarkMenuItem)target).getTarget(), null);
         this.console.toggleUniverseMode();
         return true;
      } else {
         return super.action(event, what);
      }
   }

   @Override
   protected Graphics drawButton(Graphics g, int button, int state) {
      Color c;
      if (state == 1) {
         if (this.isLoaded) {
            c = currentPackageName.equalsIgnoreCase(this.pkg) ? new Color(192, 255, 192) : Color.white;
         } else {
            c = Color.red;
         }
      } else if (state == 2) {
         c = new Color(255, 192, 192);
      } else {
         c = Color.green;
      }

      return super.drawButton(g, button, state, c);
   }
}