Path: blob/master/SLICK_HOME/src/org/newdawn/slick/gui/AbstractComponent.java
1457 views
package org.newdawn.slick.gui;12import java.util.HashSet;3import java.util.Iterator;4import java.util.Set;56import org.newdawn.slick.Graphics;7import org.newdawn.slick.Input;8import org.newdawn.slick.SlickException;9import org.newdawn.slick.geom.Rectangle;10import org.newdawn.slick.util.InputAdapter;1112/**13* The utility class to handle all the input related gubbins for basic GUI14* components15*16* @author kevin17*/18public abstract class AbstractComponent extends InputAdapter {19/** The component that currently has focus */20private static AbstractComponent currentFocus = null;2122/** The game container */23protected GUIContext container;2425/** Listeners for the component to notify */26protected Set listeners;2728/** True if this component currently has focus */29private boolean focus = false;3031/** The input we're responding to */32protected Input input;3334/**35* Create a new component36*37* @param container38* The container displaying this component39*/40public AbstractComponent(GUIContext container) {41this.container = container;4243listeners = new HashSet();4445input = container.getInput();46input.addPrimaryListener(this);4748setLocation(0, 0);49}5051/**52* Add a component listener to be informed when the component sees fit.53*54* It will ignore listeners already added.55*56* @param listener57* listener58*/59public void addListener(ComponentListener listener) {60listeners.add(listener);61}6263/**64* Remove a component listener.65*66* It will ignore if the listener wasn't added.67*68* @param listener69* listener70*/71public void removeListener(ComponentListener listener) {72listeners.remove(listener);73}7475/**76* Notify all the listeners.77*/78protected void notifyListeners() {79Iterator it = listeners.iterator();80while (it.hasNext()) {81((ComponentListener) it.next()).componentActivated(this);82}83}8485/**86* Render this component to the screen87*88* @param container89* The container displaying this component90* @param g91* The graphics context used to render to the display92* @throws SlickException93* If there has been an error rendering the component94*/95public abstract void render(GUIContext container, Graphics g)96throws SlickException;9798/**99* Moves the component.100*101* @param x102* X coordinate103* @param y104* Y coordinate105*/106public abstract void setLocation(int x, int y);107108/**109* Returns the position in the X coordinate110*111* @return x112*/113public abstract int getX();114115/**116* Returns the position in the Y coordinate117*118* @return y119*/120public abstract int getY();121122/**123* Get the width of the component124*125* @return The width of the component126*/127public abstract int getWidth();128129/**130* Get the height of the component131*132* @return The height of the component133*/134public abstract int getHeight();135136/**137* Indicate whether this component should be focused or not138*139* @param focus140* if the component should be focused141*/142public void setFocus(boolean focus) {143if (focus) {144if (currentFocus != null) {145currentFocus.setFocus(false);146}147currentFocus = this;148} else {149if (currentFocus == this) {150currentFocus = null;151}152}153this.focus = focus;154}155156/**157* Check if this component currently has focus158*159* @return if this field currently has focus160*/161public boolean hasFocus() {162return focus;163}164165/**166* Consume the event currently being processed167*/168protected void consumeEvent() {169input.consumeEvent();170}171172/**173* Gives the focus to this component with a click of the mouse.174*175* @see org.newdawn.slick.gui.AbstractComponent#mouseReleased(int, int, int)176*/177public void mouseReleased(int button, int x, int y) {178setFocus(Rectangle.contains(x, y, getX(), getY(), getWidth(),179getHeight()));180}181}182183