Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/gui/MouseOverArea.java
1457 views
1
package org.newdawn.slick.gui;
2
3
import org.newdawn.slick.Color;
4
import org.newdawn.slick.Graphics;
5
import org.newdawn.slick.Image;
6
import org.newdawn.slick.Input;
7
import org.newdawn.slick.Sound;
8
import org.newdawn.slick.geom.Rectangle;
9
import org.newdawn.slick.geom.Shape;
10
11
/**
12
* A mouse over area that can be used for menus or buttons
13
*
14
* @author kevin
15
*/
16
public class MouseOverArea extends AbstractComponent {
17
/** The default state */
18
private static final int NORMAL = 1;
19
20
/** The mouse down state */
21
private static final int MOUSE_DOWN = 2;
22
23
/** The mouse over state */
24
private static final int MOUSE_OVER = 3;
25
26
/** The normalImage being displayed in normal state */
27
private Image normalImage;
28
29
/** The normalImage being displayed in mouseOver state */
30
private Image mouseOverImage;
31
32
/** The normalImage being displayed in mouseDown state */
33
private Image mouseDownImage;
34
35
/** The colour used in normal state */
36
private Color normalColor = Color.white;
37
38
/** The colour used in mouseOver state */
39
private Color mouseOverColor = Color.white;
40
41
/** The colour used in mouseDown state */
42
private Color mouseDownColor = Color.white;
43
44
/** The sound for mouse over */
45
private Sound mouseOverSound;
46
47
/** The sound for mouse down */
48
private Sound mouseDownSound;
49
50
/** The shape defining the area */
51
private Shape area;
52
53
/** The current normalImage being displayed */
54
private Image currentImage;
55
56
/** The current color being used */
57
private Color currentColor;
58
59
/** True if the mouse is over the area */
60
private boolean over;
61
62
/** True if the mouse button is pressed */
63
private boolean mouseDown;
64
65
/** The state of the area */
66
private int state = NORMAL;
67
68
/** True if the mouse has been up since last press */
69
private boolean mouseUp;
70
71
/**
72
* Create a new mouse over area
73
*
74
* @param container
75
* The container displaying the mouse over area
76
* @param image
77
* The normalImage to display
78
* @param x
79
* The position of the area
80
* @param y
81
* the position of the area
82
* @param listener
83
* A listener to add to the area
84
*/
85
public MouseOverArea(GUIContext container, Image image, int x, int y, ComponentListener listener) {
86
this(container, image, x, y, image.getWidth(), image.getHeight());
87
addListener(listener);
88
}
89
90
/**
91
* Create a new mouse over area
92
*
93
* @param container
94
* The container displaying the mouse over area
95
* @param image
96
* The normalImage to display
97
* @param x
98
* The position of the area
99
* @param y
100
* the position of the area
101
*/
102
public MouseOverArea(GUIContext container, Image image, int x, int y) {
103
this(container, image, x, y, image.getWidth(), image.getHeight());
104
}
105
106
/**
107
* Create a new mouse over area
108
*
109
* @param container
110
* The container displaying the mouse over area
111
* @param image
112
* The normalImage to display
113
* @param x
114
* The position of the area
115
* @param y
116
* the position of the area
117
* @param width
118
* The width of the area
119
* @param height
120
* The height of the area
121
* @param listener
122
* A listener to add to the area
123
*/
124
public MouseOverArea(GUIContext container, Image image, int x, int y,
125
int width, int height, ComponentListener listener) {
126
this(container,image,x,y,width,height);
127
addListener(listener);
128
}
129
130
/**
131
* Create a new mouse over area
132
*
133
* @param container
134
* The container displaying the mouse over area
135
* @param image
136
* The normalImage to display
137
* @param x
138
* The position of the area
139
* @param y
140
* the position of the area
141
* @param width
142
* The width of the area
143
* @param height
144
* The height of the area
145
*/
146
public MouseOverArea(GUIContext container, Image image, int x, int y,
147
int width, int height) {
148
this(container,image,new Rectangle(x,y,width,height));
149
}
150
151
/**
152
* Create a new mouse over area
153
*
154
* @param container
155
* The container displaying the mouse over area
156
* @param image
157
* The normalImage to display
158
* @param shape
159
* The shape defining the area of the mouse sensitive zone
160
*/
161
public MouseOverArea(GUIContext container, Image image, Shape shape) {
162
super(container);
163
164
area = shape;
165
normalImage = image;
166
currentImage = image;
167
mouseOverImage = image;
168
mouseDownImage = image;
169
170
currentColor = normalColor;
171
172
state = NORMAL;
173
Input input = container.getInput();
174
over = area.contains(input.getMouseX(), input.getMouseY());
175
mouseDown = input.isMouseButtonDown(0);
176
updateImage();
177
}
178
179
/**
180
* Moves the component.
181
*
182
* @param x X coordinate
183
* @param y Y coordinate
184
*/
185
public void setLocation(float x, float y) {
186
if (area != null) {
187
area.setX(x);
188
area.setY(y);
189
}
190
}
191
192
/**
193
* Set the x coordinate of this area
194
*
195
* @param x The new x coordinate of this area
196
*/
197
public void setX(float x) {
198
area.setX(x);
199
}
200
201
/**
202
* Set the y coordinate of this area
203
*
204
* @param y The new y coordinate of this area
205
*/
206
public void setY(float y) {
207
area.setY(y);
208
}
209
210
/**
211
* Returns the position in the X coordinate
212
*
213
* @return x
214
*/
215
public int getX() {
216
return (int) area.getX();
217
}
218
219
/**
220
* Returns the position in the Y coordinate
221
*
222
* @return y
223
*/
224
public int getY() {
225
return (int) area.getY();
226
}
227
228
/**
229
* Set the normal color used on the image in the default state
230
*
231
* @param color
232
* The color to be used
233
*/
234
public void setNormalColor(Color color) {
235
normalColor = color;
236
}
237
238
/**
239
* Set the color to be used when the mouse is over the area
240
*
241
* @param color
242
* The color to be used when the mouse is over the area
243
*/
244
public void setMouseOverColor(Color color) {
245
mouseOverColor = color;
246
}
247
248
/**
249
* Set the color to be used when the mouse is down the area
250
*
251
* @param color
252
* The color to be used when the mouse is down the area
253
*/
254
public void setMouseDownColor(Color color) {
255
mouseDownColor = color;
256
}
257
258
/**
259
* Set the normal image used on the image in the default state
260
*
261
* @param image
262
* The image to be used
263
*/
264
public void setNormalImage(Image image) {
265
normalImage = image;
266
}
267
268
/**
269
* Set the image to be used when the mouse is over the area
270
*
271
* @param image
272
* The image to be used when the mouse is over the area
273
*/
274
public void setMouseOverImage(Image image) {
275
mouseOverImage = image;
276
}
277
278
/**
279
* Set the image to be used when the mouse is down the area
280
*
281
* @param image
282
* The image to be used when the mouse is down the area
283
*/
284
public void setMouseDownImage(Image image) {
285
mouseDownImage = image;
286
}
287
288
/**
289
* @see org.newdawn.slick.gui.AbstractComponent#render(org.newdawn.slick.gui.GUIContext,
290
* org.newdawn.slick.Graphics)
291
*/
292
public void render(GUIContext container, Graphics g) {
293
if (currentImage != null) {
294
295
int xp = (int) (area.getX() + ((getWidth() - currentImage.getWidth()) / 2));
296
int yp = (int) (area.getY() + ((getHeight() - currentImage.getHeight()) / 2));
297
298
currentImage.draw(xp, yp, currentColor);
299
} else {
300
g.setColor(currentColor);
301
g.fill(area);
302
}
303
updateImage();
304
}
305
306
/**
307
* Update the current normalImage based on the mouse state
308
*/
309
private void updateImage() {
310
if (!over) {
311
currentImage = normalImage;
312
currentColor = normalColor;
313
state = NORMAL;
314
mouseUp = false;
315
} else {
316
if (mouseDown) {
317
if ((state != MOUSE_DOWN) && (mouseUp)) {
318
if (mouseDownSound != null) {
319
mouseDownSound.play();
320
}
321
currentImage = mouseDownImage;
322
currentColor = mouseDownColor;
323
state = MOUSE_DOWN;
324
325
notifyListeners();
326
mouseUp = false;
327
}
328
329
return;
330
} else {
331
mouseUp = true;
332
if (state != MOUSE_OVER) {
333
if (mouseOverSound != null) {
334
mouseOverSound.play();
335
}
336
currentImage = mouseOverImage;
337
currentColor = mouseOverColor;
338
state = MOUSE_OVER;
339
}
340
}
341
}
342
343
mouseDown = false;
344
state = NORMAL;
345
}
346
347
/**
348
* Set the mouse over sound effect
349
*
350
* @param sound
351
* The mouse over sound effect
352
*/
353
public void setMouseOverSound(Sound sound) {
354
mouseOverSound = sound;
355
}
356
357
/**
358
* Set the mouse down sound effect
359
*
360
* @param sound
361
* The mouse down sound effect
362
*/
363
public void setMouseDownSound(Sound sound) {
364
mouseDownSound = sound;
365
}
366
367
/**
368
* @see org.newdawn.slick.util.InputAdapter#mouseMoved(int, int, int, int)
369
*/
370
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
371
over = area.contains(newx, newy);
372
}
373
374
/**
375
* @see org.newdawn.slick.util.InputAdapter#mouseDragged(int, int, int, int)
376
*/
377
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
378
mouseMoved(oldx, oldy, newx, newy);
379
}
380
381
/**
382
* @see org.newdawn.slick.util.InputAdapter#mousePressed(int, int, int)
383
*/
384
public void mousePressed(int button, int mx, int my) {
385
over = area.contains(mx, my);
386
if (button == 0) {
387
mouseDown = true;
388
}
389
}
390
391
/**
392
* @see org.newdawn.slick.util.InputAdapter#mouseReleased(int, int, int)
393
*/
394
public void mouseReleased(int button, int mx, int my) {
395
over = area.contains(mx, my);
396
if (button == 0) {
397
mouseDown = false;
398
}
399
}
400
401
/**
402
* @see org.newdawn.slick.gui.AbstractComponent#getHeight()
403
*/
404
public int getHeight() {
405
return (int) (area.getMaxY() - area.getY());
406
}
407
408
/**
409
* @see org.newdawn.slick.gui.AbstractComponent#getWidth()
410
*/
411
public int getWidth() {
412
return (int) (area.getMaxX() - area.getX());
413
}
414
415
/**
416
* Check if the mouse is over this area
417
*
418
* @return True if the mouse is over this area
419
*/
420
public boolean isMouseOver() {
421
return over;
422
}
423
424
/**
425
* Set the location of this area
426
*
427
* @param x The x coordinate of this area
428
* @param y The y coordiante of this area
429
*/
430
public void setLocation(int x, int y) {
431
setLocation((float) x,(float) y);
432
}
433
}
434
435