summaryrefslogtreecommitdiff
path: root/NET/worlds/console/ImageButtons.java
blob: b27f9e5a1f53fbc5b5fa11036c484c2e96763a30 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package NET.worlds.console;

import NET.worlds.scape.FrameEvent;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.PopupMenu;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

public class ImageButtons extends ImageCanvas implements FramePart, DialogDisabled {
   private static final long serialVersionUID = -1495360044843592740L;
   protected static final int BLANK = 0;
   protected static final int NORMAL = 1;
   protected static final int CURSED = 2;
   protected static final int DOWN = 3;
   private static final int TITLE = 0;
   private static final int BUTTON = 1;
   private int width;
   private int height;
   private Dimension ghostImageDimension;
   protected Rectangle[] buttons;
   private int[] types;
   private boolean background = false;
   private int cursedButton = -1;
   private int clickedButton = -1;
   private boolean clickedButtonDown;
   private ImageButtonsCallback handler;
   private ImageButtonsCallback downUpHandler;
   private PopupMenu menu;
   private boolean isDialogDisabled;

   private void initEvents() {
      this.enableEvents(16L);
      this.enableEvents(32L);
   }

   protected ImageButtons() {
      this.buttons = new Rectangle[0];
      this.initEvents();
   }

   public ImageButtons(String imageName, int buttonWidth, int buttonHeight, ImageButtonsCallback handler) {
      this(imageName, buttonWidth, intToInts(buttonHeight), handler);
   }

   public ImageButtons(String imageName, Rectangle[] buttonRects, ImageButtonsCallback handler) {
      super(imageName);
      this.handler = handler;
      this.background = true;
      this.setHotspots(buttonRects);
      this.initEvents();
   }

   public ImageButtons(String imageName, int buttonWidth, int[] buttonHeights, ImageButtonsCallback handler) {
      super(imageName);
      this.handler = handler;
      this.buttons = new Rectangle[buttonHeights.length];
      this.types = new int[buttonHeights.length];
      int y = 0;

      for (int i = 0; i < buttonHeights.length; i++) {
         int h = buttonHeights[i];
         int rh;
         if (h > 0) {
            rh = h;
            this.types[i] = 1;
         } else {
            rh = -h;
            this.types[i] = 0;
         }

         this.buttons[i] = new Rectangle(0, y, buttonWidth, rh);
         y += rh;
      }

      this.ghostImageDimension = new Dimension(buttonWidth * 4, buttonHeights[0]);
      this.initEvents();
   }

   public void setHandler(ImageButtonsCallback handler) {
      this.handler = handler;
   }

   public void setDownUpHandler(ImageButtonsCallback handler) {
      this.downUpHandler = handler;
   }

   protected void setBackground(boolean background) {
      this.background = background;
   }

   protected void setWidth(int width) {
      this.width = width;
   }

   protected void setHeight(int height) {
      this.height = height;
   }

   protected synchronized void setHotspots(Rectangle[] buttonRects) {
      this.buttons = new Rectangle[buttonRects.length];
      this.types = new int[buttonRects.length];

      for (int i = 0; i < buttonRects.length; i++) {
         this.types[i] = 1;
         this.buttons[i] = new Rectangle(buttonRects[i]);
      }

      this.cursedButton = -1;
      this.clickedButton = -1;
      this.clickedButtonDown = false;
   }

   @Override
   public synchronized void paint(Graphics g) {
      g.clipRect(0, 0, this.width, this.height);
      if (this.background && this.image_ != null) {
         g.drawImage(this.image_, -1 * this.width, 0, null);
      }

      for (int i = 0; i < this.buttons.length; i++) {
         int state = 1;
         if (i == this.cursedButton) {
            state = 2;
         }

         if (i == this.clickedButton) {
            state = this.clickedButtonDown ? 3 : 1;
         }

         this.drawButton(g, i, state);
      }
   }

   @Override
   public Dimension preferredSize() {
      Dimension d = super.preferredSize();
      if (d.width == 0 && d.height == 0 && this.ghostImageDimension != null) {
         d = this.ghostImageDimension;
      }

      return new Dimension(this.width = d.width / 4, this.height = d.height);
   }

   @Override
   public Dimension minimumSize() {
      return this.preferredSize();
   }

   @Override
   public void processMouseMotionEvent(MouseEvent e) {
      if (!this.isDialogDisabled) {
         switch (e.getID()) {
            case 503:
            case 506:
               this.buttonAction(e.getX(), e.getY(), e.getID());
            case 504:
            case 505:
         }
      }

      super.processMouseEvent(e);
   }

   @Override
   public void processMouseEvent(MouseEvent e) {
      if (!this.isDialogDisabled) {
         switch (e.getID()) {
            case 501:
            case 502:
            case 504:
            case 505:
               this.buttonAction(e.getX(), e.getY(), e.getID());
            case 503:
         }
      }

      super.processMouseEvent(e);
   }

   @Override
   public boolean mouseMove(Event e, int x, int y) {
      return this.buttonAction(x, y, 503);
   }

   @Override
   public boolean mouseDown(Event e, int x, int y) {
      return (e.modifiers & 12) != 0 ? false : this.buttonAction(x, y, 501);
   }

   @Override
   public boolean mouseUp(Event e, int x, int y) {
      return (e.modifiers & 12) != 0 ? false : this.buttonAction(x, y, 502);
   }

   @Override
   public boolean mouseDrag(Event e, int x, int y) {
      return this.buttonAction(x, y, 506);
   }

   @Override
   public boolean mouseEnter(Event e, int x, int y) {
      return this.buttonAction(x, y, 504);
   }

   @Override
   public boolean mouseExit(Event e, int x, int y) {
      return this.buttonAction(x, y, 505);
   }

   @Override
   public boolean handleEvent(Event event) {
      return this.isDialogDisabled ? false : super.handleEvent(event);
   }

   protected Graphics drawButton(Graphics g, int button, int state) {
      if (button != -1) {
         Rectangle r = this.buttons[button];
         if (this.image_ != null && (g != null || (g = this.getGraphics()) != null)) {
            Graphics g1 = g.create(r.x, r.y, r.width, r.height);
            g1.drawImage(this.image_, -state * this.width - r.x, -r.y, null);
            g1.dispose();
         }
      }

      return g;
   }

   private synchronized boolean buttonAction(int x, int y, int action) {
      return this.buttonAction(this.locateButton(x, y), action);
   }

   private int locateButton(int x, int y) {
      for (int i = 0; i < this.buttons.length; i++) {
         if (this.types[i] == 1 && this.buttons[i].inside(x, y)) {
            return i;
         }
      }

      return -1;
   }

   public void drawFirstButton(int state) {
      if (this.buttons.length > 0) {
         Graphics g = this.drawButton(null, 0, state);
         if (g != null) {
            g.dispose();
         }
      }
   }

   public void drawNormal() {
      this.drawFirstButton(1);
   }

   public void drawCursed() {
      this.drawFirstButton(2);
   }

   public void drawDown() {
      this.drawFirstButton(3);
   }

   boolean buttonAction(int button, int action) {
      Graphics g = null;
      if (action != 503 && action != 504) {
         if (action == 505) {
            if (this.cursedButton != -1) {
               g = this.drawButton(g, this.cursedButton, 1);
               this.cursedButton = -1;
            }

            if (this.downUpHandler == null && this.clickedButton != -1 && this.clickedButtonDown) {
               g = this.drawButton(g, this.clickedButton, 1);
               this.clickedButtonDown = false;
            }
         } else if (action == 501) {
            if (this.clickedButton != -1) {
               g = this.drawButton(g, this.clickedButton, 1);
               if (this.downUpHandler != null) {
                  this.downUpHandler.imageButtonsCallback(this, -1);
               }

               this.clickedButtonDown = false;
            }

            if (this.cursedButton != -1) {
               g = this.drawButton(g, this.cursedButton, 1);
               this.cursedButton = -1;
            }

            if ((this.clickedButton = button) != -1) {
               g = this.drawButton(g, this.clickedButton, 3);
               this.clickedButtonDown = true;
               if (this.downUpHandler != null) {
                  this.downUpHandler.imageButtonsCallback(this, button);
               }
            }
         } else if (action == 506) {
            if (this.downUpHandler == null && this.clickedButton != -1) {
               if (this.clickedButtonDown) {
                  if (button != this.clickedButton) {
                     g = this.drawButton(g, this.clickedButton, 1);
                     this.clickedButtonDown = false;
                  }
               } else if (button == this.clickedButton) {
                  g = this.drawButton(g, this.clickedButton, 3);
                  this.clickedButtonDown = true;
               }
            }
         } else if (action == 502) {
            this.cursedButton = button;
            if (this.cursedButton != -1) {
               g = this.drawButton(g, this.cursedButton, 2);
            }

            if (this.clickedButtonDown) {
               if (this.cursedButton != this.clickedButton) {
                  g = this.drawButton(g, this.clickedButton, 1);
               }

               Object ret = this.handler.imageButtonsCallback(this, this.clickedButton);
               if (ret instanceof PopupMenu && this.clickedButton != -1) {
                  if (this.menu != null) {
                     this.remove(this.menu);
                  }

                  this.add(this.menu = (PopupMenu)ret);
                  this.menu.show(this, this.buttons[this.clickedButton].x, this.buttons[this.clickedButton].y + this.buttons[this.clickedButton].height);
               }

               if (this.downUpHandler != null && this.downUpHandler.imageButtonsCallback(this, -1) != null) {
                  this.cursedButton = -1;
               }

               this.clickedButtonDown = false;
               this.clickedButton = -1;
            }
         }
      } else {
         if (button != this.cursedButton) {
            g = this.drawButton(g, this.cursedButton, 1);
            g = this.drawButton(g, this.cursedButton = button, 2);
         }

         if (this.clickedButton != -1 && this.clickedButton != button && this.downUpHandler == null) {
            g = this.drawButton(g, this.clickedButton, 1);
            this.clickedButtonDown = false;
         }
      }

      if (g != null) {
         g.dispose();
      }

      return true;
   }

   public static int[] intToInts(int value) {
      return new int[]{value};
   }

   @Override
   public void activate(Console c, Container f, Console prev) {
   }

   @Override
   public void deactivate() {
   }

   @Override
   public boolean action(Event event, Object what) {
      return false;
   }

   @Override
   public boolean handle(FrameEvent f) {
      return false;
   }

   @Override
   public void dialogDisable(boolean disable) {
      if (this.isDialogDisabled = disable) {
         this.cursedButton = -1;
         this.clickedButton = -1;
         this.clickedButtonDown = false;
         this.repaint();
      }
   }
}