Path: blob/master/SLICK_HOME/src/org/newdawn/slick/gui/MouseOverArea.java
1457 views
package org.newdawn.slick.gui;12import org.newdawn.slick.Color;3import org.newdawn.slick.Graphics;4import org.newdawn.slick.Image;5import org.newdawn.slick.Input;6import org.newdawn.slick.Sound;7import org.newdawn.slick.geom.Rectangle;8import org.newdawn.slick.geom.Shape;910/**11* A mouse over area that can be used for menus or buttons12*13* @author kevin14*/15public class MouseOverArea extends AbstractComponent {16/** The default state */17private static final int NORMAL = 1;1819/** The mouse down state */20private static final int MOUSE_DOWN = 2;2122/** The mouse over state */23private static final int MOUSE_OVER = 3;2425/** The normalImage being displayed in normal state */26private Image normalImage;2728/** The normalImage being displayed in mouseOver state */29private Image mouseOverImage;3031/** The normalImage being displayed in mouseDown state */32private Image mouseDownImage;3334/** The colour used in normal state */35private Color normalColor = Color.white;3637/** The colour used in mouseOver state */38private Color mouseOverColor = Color.white;3940/** The colour used in mouseDown state */41private Color mouseDownColor = Color.white;4243/** The sound for mouse over */44private Sound mouseOverSound;4546/** The sound for mouse down */47private Sound mouseDownSound;4849/** The shape defining the area */50private Shape area;5152/** The current normalImage being displayed */53private Image currentImage;5455/** The current color being used */56private Color currentColor;5758/** True if the mouse is over the area */59private boolean over;6061/** True if the mouse button is pressed */62private boolean mouseDown;6364/** The state of the area */65private int state = NORMAL;6667/** True if the mouse has been up since last press */68private boolean mouseUp;6970/**71* Create a new mouse over area72*73* @param container74* The container displaying the mouse over area75* @param image76* The normalImage to display77* @param x78* The position of the area79* @param y80* the position of the area81* @param listener82* A listener to add to the area83*/84public MouseOverArea(GUIContext container, Image image, int x, int y, ComponentListener listener) {85this(container, image, x, y, image.getWidth(), image.getHeight());86addListener(listener);87}8889/**90* Create a new mouse over area91*92* @param container93* The container displaying the mouse over area94* @param image95* The normalImage to display96* @param x97* The position of the area98* @param y99* the position of the area100*/101public MouseOverArea(GUIContext container, Image image, int x, int y) {102this(container, image, x, y, image.getWidth(), image.getHeight());103}104105/**106* Create a new mouse over area107*108* @param container109* The container displaying the mouse over area110* @param image111* The normalImage to display112* @param x113* The position of the area114* @param y115* the position of the area116* @param width117* The width of the area118* @param height119* The height of the area120* @param listener121* A listener to add to the area122*/123public MouseOverArea(GUIContext container, Image image, int x, int y,124int width, int height, ComponentListener listener) {125this(container,image,x,y,width,height);126addListener(listener);127}128129/**130* Create a new mouse over area131*132* @param container133* The container displaying the mouse over area134* @param image135* The normalImage to display136* @param x137* The position of the area138* @param y139* the position of the area140* @param width141* The width of the area142* @param height143* The height of the area144*/145public MouseOverArea(GUIContext container, Image image, int x, int y,146int width, int height) {147this(container,image,new Rectangle(x,y,width,height));148}149150/**151* Create a new mouse over area152*153* @param container154* The container displaying the mouse over area155* @param image156* The normalImage to display157* @param shape158* The shape defining the area of the mouse sensitive zone159*/160public MouseOverArea(GUIContext container, Image image, Shape shape) {161super(container);162163area = shape;164normalImage = image;165currentImage = image;166mouseOverImage = image;167mouseDownImage = image;168169currentColor = normalColor;170171state = NORMAL;172Input input = container.getInput();173over = area.contains(input.getMouseX(), input.getMouseY());174mouseDown = input.isMouseButtonDown(0);175updateImage();176}177178/**179* Moves the component.180*181* @param x X coordinate182* @param y Y coordinate183*/184public void setLocation(float x, float y) {185if (area != null) {186area.setX(x);187area.setY(y);188}189}190191/**192* Set the x coordinate of this area193*194* @param x The new x coordinate of this area195*/196public void setX(float x) {197area.setX(x);198}199200/**201* Set the y coordinate of this area202*203* @param y The new y coordinate of this area204*/205public void setY(float y) {206area.setY(y);207}208209/**210* Returns the position in the X coordinate211*212* @return x213*/214public int getX() {215return (int) area.getX();216}217218/**219* Returns the position in the Y coordinate220*221* @return y222*/223public int getY() {224return (int) area.getY();225}226227/**228* Set the normal color used on the image in the default state229*230* @param color231* The color to be used232*/233public void setNormalColor(Color color) {234normalColor = color;235}236237/**238* Set the color to be used when the mouse is over the area239*240* @param color241* The color to be used when the mouse is over the area242*/243public void setMouseOverColor(Color color) {244mouseOverColor = color;245}246247/**248* Set the color to be used when the mouse is down the area249*250* @param color251* The color to be used when the mouse is down the area252*/253public void setMouseDownColor(Color color) {254mouseDownColor = color;255}256257/**258* Set the normal image used on the image in the default state259*260* @param image261* The image to be used262*/263public void setNormalImage(Image image) {264normalImage = image;265}266267/**268* Set the image to be used when the mouse is over the area269*270* @param image271* The image to be used when the mouse is over the area272*/273public void setMouseOverImage(Image image) {274mouseOverImage = image;275}276277/**278* Set the image to be used when the mouse is down the area279*280* @param image281* The image to be used when the mouse is down the area282*/283public void setMouseDownImage(Image image) {284mouseDownImage = image;285}286287/**288* @see org.newdawn.slick.gui.AbstractComponent#render(org.newdawn.slick.gui.GUIContext,289* org.newdawn.slick.Graphics)290*/291public void render(GUIContext container, Graphics g) {292if (currentImage != null) {293294int xp = (int) (area.getX() + ((getWidth() - currentImage.getWidth()) / 2));295int yp = (int) (area.getY() + ((getHeight() - currentImage.getHeight()) / 2));296297currentImage.draw(xp, yp, currentColor);298} else {299g.setColor(currentColor);300g.fill(area);301}302updateImage();303}304305/**306* Update the current normalImage based on the mouse state307*/308private void updateImage() {309if (!over) {310currentImage = normalImage;311currentColor = normalColor;312state = NORMAL;313mouseUp = false;314} else {315if (mouseDown) {316if ((state != MOUSE_DOWN) && (mouseUp)) {317if (mouseDownSound != null) {318mouseDownSound.play();319}320currentImage = mouseDownImage;321currentColor = mouseDownColor;322state = MOUSE_DOWN;323324notifyListeners();325mouseUp = false;326}327328return;329} else {330mouseUp = true;331if (state != MOUSE_OVER) {332if (mouseOverSound != null) {333mouseOverSound.play();334}335currentImage = mouseOverImage;336currentColor = mouseOverColor;337state = MOUSE_OVER;338}339}340}341342mouseDown = false;343state = NORMAL;344}345346/**347* Set the mouse over sound effect348*349* @param sound350* The mouse over sound effect351*/352public void setMouseOverSound(Sound sound) {353mouseOverSound = sound;354}355356/**357* Set the mouse down sound effect358*359* @param sound360* The mouse down sound effect361*/362public void setMouseDownSound(Sound sound) {363mouseDownSound = sound;364}365366/**367* @see org.newdawn.slick.util.InputAdapter#mouseMoved(int, int, int, int)368*/369public void mouseMoved(int oldx, int oldy, int newx, int newy) {370over = area.contains(newx, newy);371}372373/**374* @see org.newdawn.slick.util.InputAdapter#mouseDragged(int, int, int, int)375*/376public void mouseDragged(int oldx, int oldy, int newx, int newy) {377mouseMoved(oldx, oldy, newx, newy);378}379380/**381* @see org.newdawn.slick.util.InputAdapter#mousePressed(int, int, int)382*/383public void mousePressed(int button, int mx, int my) {384over = area.contains(mx, my);385if (button == 0) {386mouseDown = true;387}388}389390/**391* @see org.newdawn.slick.util.InputAdapter#mouseReleased(int, int, int)392*/393public void mouseReleased(int button, int mx, int my) {394over = area.contains(mx, my);395if (button == 0) {396mouseDown = false;397}398}399400/**401* @see org.newdawn.slick.gui.AbstractComponent#getHeight()402*/403public int getHeight() {404return (int) (area.getMaxY() - area.getY());405}406407/**408* @see org.newdawn.slick.gui.AbstractComponent#getWidth()409*/410public int getWidth() {411return (int) (area.getMaxX() - area.getX());412}413414/**415* Check if the mouse is over this area416*417* @return True if the mouse is over this area418*/419public boolean isMouseOver() {420return over;421}422423/**424* Set the location of this area425*426* @param x The x coordinate of this area427* @param y The y coordiante of this area428*/429public void setLocation(int x, int y) {430setLocation((float) x,(float) y);431}432}433434435