summaryrefslogtreecommitdiff
path: root/NET/worlds/console/FourTilePanel.java
blob: f010994874fcc1934328ae9fad5680cc75e7db82 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package NET.worlds.console;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Rectangle;

public class FourTilePanel extends Panel {
   private static final long serialVersionUID = -7002173409842219262L;
   private static final int tw = 5;
   private static final int th = 5;
   private static final int aw = 7;
   private static final int ah = 4;
   private static final int bw = 3;
   private static Color widgetColor = new Color(255, 238, 177);
   private static Color lineColor = new Color(162, 162, 162);
   private Component[] comps = new Component[4];
   private int[] order = new int[]{0, 1, 2, 3};
   private float[] divider = new float[]{0.22F, 0.63F};
   private int tx;
   private int ty;
   private int prevWidth;
   private int prevHeight;
   private Rectangle moveWidget = new Rectangle();
   private FourTileSwapper swap01 = new FourTileSwapper(this, 0, 1);
   private FourTileSwapper swap02 = new FourTileSwapper(this, 0, 2);
   private FourTileSwapper swap23 = new FourTileSwapper(this, 2, 3);
   private FourTileSwapper swap13 = new FourTileSwapper(this, 1, 3);
   private FourTileSwapper[] swaps = new FourTileSwapper[]{this.swap01, this.swap02, this.swap23, this.swap13};
   private boolean dragging;
   private int offsetx;
   private int offsety;
   private int minx;
   private int miny;
   private int maxx;
   private int maxy;
   private int specialTileIndex;
   private Component oneTile;

   public FourTilePanel(Component ul, Component ur, Component ll, Component lr, int special) {
      this.setLayout(null);
      this.specialTileIndex = special;
      this.add(this.comps[0] = ul);
      this.add(this.comps[1] = ur);
      this.add(this.comps[2] = ll);
      this.add(this.comps[3] = lr);
      this.setBackground(Color.black);
      if (GammaFrameState.restoreLayout(this.order, this.divider)) {
         this.useOneTileMode();
      } else {
         this.useFourTileMode();
      }
   }

   public boolean isOneTileMode() {
      return this.oneTile != null;
   }

   public void useOneTileMode() {
      if (this.oneTile == null) {
         this.oneTile = this.comps[this.specialTileIndex];

         for (int i = 0; i < 4; i++) {
            if (i != this.specialTileIndex) {
               this.remove(this.comps[i]);
            }
         }

         this.moveComponents();
         this.saveLayout();
         this.validate();
      }
   }

   public void useFourTileMode() {
      if (this.oneTile != null) {
         this.oneTile = null;

         for (int i = 0; i < 4; i++) {
            if (i != this.specialTileIndex) {
               this.add(this.comps[i]);
            }
         }

         this.moveComponents();
         this.saveLayout();
         this.validate();
      }
   }

   @Override
   public void reshape(int x, int y, int width, int height) {
      super.reshape(x, y, width, height);
      if (!GammaFrameState.isIconic()) {
         if (this.prevWidth != width) {
            this.tx = Math.max((int)(this.divider[0] * width), 12);
            this.prevWidth = width;
         }

         if (this.prevHeight != height) {
            this.ty = Math.max((int)(this.divider[1] * height), 9);
            this.prevHeight = height;
         }

         this.moveComponents(width, height);
      }
   }

   private void saveLayout() {
      GammaFrameState.saveLayout(this.order, this.divider, this.oneTile != null);
   }

   private void moveComponents() {
      Dimension size = this.getSize();
      this.moveComponents(size.width, size.height);
   }

   private void moveComponents(int width, int height) {
      if (this.oneTile == null) {
         this.comps[this.order[0]].reshape(3, 3, this.tx - 3, this.ty - 3);
         this.comps[this.order[1]].reshape(this.tx + 5, 3, width - this.tx - 5 - 3, this.ty - 3);
         this.comps[this.order[2]].reshape(3, this.ty + 5, this.tx - 3, height - this.ty - 5 - 3);
         this.comps[this.order[3]].reshape(this.tx + 5, this.ty + 5, width - this.tx - 5 - 3, height - this.ty - 5 - 3);
      } else {
         this.oneTile.reshape(0, 0, width, height);
      }

      this.repaint();
   }

   @Override
   public void paint(Graphics g) {
      Dimension size = this.getSize();
      g.setColor(this.getBackground());
      if (this.oneTile == null) {
         g.fillRect(0, 0, 3, size.height);
         g.fillRect(0, 0, size.width, 3);
         g.fillRect(0, size.height - 3, size.width, 3);
         g.fillRect(size.width - 3, 0, 3, size.height);
         g.fillRect(0, this.ty, size.width, 5);
         g.fillRect(this.tx, 0, 5, size.height);
         g.setColor(lineColor);
         g.drawLine(0, this.ty + 2, size.width, this.ty + 2);
         g.drawLine(this.tx + 2, 0, this.tx + 2, size.height);
         g.setColor(widgetColor);
         g.fillRect(this.tx, this.ty, 5, 5);
         this.moveWidget.reshape(this.tx, this.ty, 5, 5);
         int tmp = (this.tx + 2 - 7) / 2;
         g.fillArc(tmp, this.ty - 2, 7, 4, 0, -180);
         g.fillArc(tmp, this.ty + 5 - 2, 7, 4, 0, 180);
         this.swap02.reshape(tmp, this.ty, 7, 5);
         tmp = (this.tx + 2 + size.width - 7) / 2;
         g.fillArc(tmp, this.ty - 2, 7, 4, 0, -180);
         g.fillArc(tmp, this.ty + 5 - 2, 7, 4, 0, 180);
         this.swap13.reshape(tmp, this.ty, 7, 5);
         tmp = (this.ty + 2 - 7) / 2;
         g.fillArc(this.tx - 2, tmp, 4, 7, 90, -180);
         g.fillArc(this.tx + 5 - 2, tmp, 4, 7, 90, 180);
         this.swap01.reshape(this.tx, tmp, 5, 7);
         tmp = (this.ty + 2 + size.height - 7) / 2;
         g.fillArc(this.tx - 2, tmp, 4, 7, 90, -180);
         g.fillArc(this.tx + 5 - 2, tmp, 4, 7, 90, 180);
         this.swap23.reshape(this.tx, tmp, 5, 7);
      }
   }

   @Override
   public boolean mouseDrag(Event evt, int x, int y) {
      if (this.dragging) {
         this.tx = x + this.offsetx;
         this.ty = y + this.offsety;
         this.divider[0] = (float)this.tx / this.getSize().width;
         this.divider[1] = (float)this.ty / this.getSize().height;
         this.tx = Math.max(this.tx, this.minx);
         this.ty = Math.max(this.ty, this.miny);
         this.tx = Math.min(this.tx, this.maxx);
         this.ty = Math.min(this.ty, this.maxy);
         this.moveComponents();
      }

      return true;
   }

   @Override
   public boolean mouseUp(Event evt, int x, int y) {
      if (this.dragging) {
         this.validate();
         this.saveLayout();
      }

      this.dragging = false;
      return true;
   }

   @Override
   public boolean mouseDown(Event evt, int x, int y) {
      for (int i = 0; i < this.swaps.length; i++) {
         if (this.swaps[i].maybeSwap(x, y)) {
            return true;
         }
      }

      if (this.moveWidget.inside(x, y)) {
         this.offsetx = this.tx - x;
         this.offsety = this.ty - y;
         this.minx = 12;
         this.maxx = this.getSize().width - this.minx;
         this.miny = 12;
         this.maxy = this.getSize().height - this.miny;
         if (this.minx < this.maxx && this.miny < this.maxy) {
            this.dragging = true;
         }
      }

      return true;
   }

   void swap(int c1, int c2) {
      int tmp = this.order[c1];
      this.order[c1] = this.order[c2];
      this.order[c2] = tmp;
      this.moveComponents();
      this.validate();
      this.saveLayout();
   }
}