summaryrefslogtreecommitdiff
path: root/NET/worlds/console/TextImageButtons.java
blob: 3f9b36bda57bd3a96cf45c78007181be9c5cf69c (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
package NET.worlds.console;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;

class TextImageButtons extends ImageButtons {
   private static final long serialVersionUID = -3744924750348174006L;
   private int buttonCount;
   private int[] xText;
   private String[] texts;
   private int[] buttonBottoms;
   private Font font;
   private static Font defFont = new Font(Console.message("ButtonFont"), 0, 9);
   private static final int yBaseline = 4;

   public static Dimension measure(String text, Font f) {
      Dimension sz = new Dimension();
      FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
      sz.width = fm.stringWidth(text);
      sz.height = fm.getHeight();
      return sz;
   }

   public TextImageButtons(String imageName, int buttonWidth, int[] buttonHeights, int[] textLefts, String[] texts, ImageButtonsCallback handler) {
      this(imageName, buttonWidth, buttonHeights, textLefts, texts, handler, defFont);
   }

   public TextImageButtons(String imageName, int buttonWidth, int[] buttonHeights, int[] textLefts, String[] texts, ImageButtonsCallback handler, Font f) {
      super(imageName, buttonWidth, buttonHeights, handler);
      this.xText = textLefts;
      this.texts = texts;
      this.buttonCount = buttonHeights.length;
      this.font = f;
      this.buttonBottoms = new int[this.buttonCount];
      int lastBottom = 0;

      for (int i = 0; i < this.buttonCount; i++) {
         lastBottom += buttonHeights[i];
         this.buttonBottoms[i] = lastBottom;
      }
   }

   public void setTexts(String[] newTexts) {
      assert newTexts.length == this.texts.length;

      this.texts = newTexts;
      this.repaint();
   }

   public String getText(int i) {
      return this.texts[i];
   }

   @Override
   protected Graphics drawButton(Graphics g, int button, int state) {
      return this.drawButton(g, button, state, Color.white);
   }

   protected Graphics drawButton(Graphics g, int button, int state, Color c) {
      if (button >= 0 && button < this.buttonCount && this.texts[button] != null) {
         g = super.drawButton(g, button, state);
         if (g != null || (g = this.getGraphics()) != null) {
            g.setColor(c);
            g.setFont(this.font);
            g.drawString(this.texts[button], this.xText[button], this.buttonBottoms[button] - 4);
         }
      } else {
         g = super.drawButton(g, button, 0);
      }

      return g;
   }
}