Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/gui/AbstractComponent.java
1457 views
1
package org.newdawn.slick.gui;
2
3
import java.util.HashSet;
4
import java.util.Iterator;
5
import java.util.Set;
6
7
import org.newdawn.slick.Graphics;
8
import org.newdawn.slick.Input;
9
import org.newdawn.slick.SlickException;
10
import org.newdawn.slick.geom.Rectangle;
11
import org.newdawn.slick.util.InputAdapter;
12
13
/**
14
* The utility class to handle all the input related gubbins for basic GUI
15
* components
16
*
17
* @author kevin
18
*/
19
public abstract class AbstractComponent extends InputAdapter {
20
/** The component that currently has focus */
21
private static AbstractComponent currentFocus = null;
22
23
/** The game container */
24
protected GUIContext container;
25
26
/** Listeners for the component to notify */
27
protected Set listeners;
28
29
/** True if this component currently has focus */
30
private boolean focus = false;
31
32
/** The input we're responding to */
33
protected Input input;
34
35
/**
36
* Create a new component
37
*
38
* @param container
39
* The container displaying this component
40
*/
41
public AbstractComponent(GUIContext container) {
42
this.container = container;
43
44
listeners = new HashSet();
45
46
input = container.getInput();
47
input.addPrimaryListener(this);
48
49
setLocation(0, 0);
50
}
51
52
/**
53
* Add a component listener to be informed when the component sees fit.
54
*
55
* It will ignore listeners already added.
56
*
57
* @param listener
58
* listener
59
*/
60
public void addListener(ComponentListener listener) {
61
listeners.add(listener);
62
}
63
64
/**
65
* Remove a component listener.
66
*
67
* It will ignore if the listener wasn't added.
68
*
69
* @param listener
70
* listener
71
*/
72
public void removeListener(ComponentListener listener) {
73
listeners.remove(listener);
74
}
75
76
/**
77
* Notify all the listeners.
78
*/
79
protected void notifyListeners() {
80
Iterator it = listeners.iterator();
81
while (it.hasNext()) {
82
((ComponentListener) it.next()).componentActivated(this);
83
}
84
}
85
86
/**
87
* Render this component to the screen
88
*
89
* @param container
90
* The container displaying this component
91
* @param g
92
* The graphics context used to render to the display
93
* @throws SlickException
94
* If there has been an error rendering the component
95
*/
96
public abstract void render(GUIContext container, Graphics g)
97
throws SlickException;
98
99
/**
100
* Moves the component.
101
*
102
* @param x
103
* X coordinate
104
* @param y
105
* Y coordinate
106
*/
107
public abstract void setLocation(int x, int y);
108
109
/**
110
* Returns the position in the X coordinate
111
*
112
* @return x
113
*/
114
public abstract int getX();
115
116
/**
117
* Returns the position in the Y coordinate
118
*
119
* @return y
120
*/
121
public abstract int getY();
122
123
/**
124
* Get the width of the component
125
*
126
* @return The width of the component
127
*/
128
public abstract int getWidth();
129
130
/**
131
* Get the height of the component
132
*
133
* @return The height of the component
134
*/
135
public abstract int getHeight();
136
137
/**
138
* Indicate whether this component should be focused or not
139
*
140
* @param focus
141
* if the component should be focused
142
*/
143
public void setFocus(boolean focus) {
144
if (focus) {
145
if (currentFocus != null) {
146
currentFocus.setFocus(false);
147
}
148
currentFocus = this;
149
} else {
150
if (currentFocus == this) {
151
currentFocus = null;
152
}
153
}
154
this.focus = focus;
155
}
156
157
/**
158
* Check if this component currently has focus
159
*
160
* @return if this field currently has focus
161
*/
162
public boolean hasFocus() {
163
return focus;
164
}
165
166
/**
167
* Consume the event currently being processed
168
*/
169
protected void consumeEvent() {
170
input.consumeEvent();
171
}
172
173
/**
174
* Gives the focus to this component with a click of the mouse.
175
*
176
* @see org.newdawn.slick.gui.AbstractComponent#mouseReleased(int, int, int)
177
*/
178
public void mouseReleased(int button, int x, int y) {
179
setFocus(Rectangle.contains(x, y, getX(), getY(), getWidth(),
180
getHeight()));
181
}
182
}
183