mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 03:24:06 +09:00
added sources for Slick
Former-commit-id: 1647fa32ef6894bd7db44f741f07c2f4dcdf9054 Former-commit-id: 0e5810dcfbe1fd59b13e7cabe9f1e93c5542da2d
This commit is contained in:
182
lib/slick-source/org/newdawn/slick/gui/AbstractComponent.java
Normal file
182
lib/slick-source/org/newdawn/slick/gui/AbstractComponent.java
Normal file
@@ -0,0 +1,182 @@
|
||||
package org.newdawn.slick.gui;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import org.newdawn.slick.Graphics;
|
||||
import org.newdawn.slick.Input;
|
||||
import org.newdawn.slick.SlickException;
|
||||
import org.newdawn.slick.geom.Rectangle;
|
||||
import org.newdawn.slick.util.InputAdapter;
|
||||
|
||||
/**
|
||||
* The utility class to handle all the input related gubbins for basic GUI
|
||||
* components
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public abstract class AbstractComponent extends InputAdapter {
|
||||
/** The component that currently has focus */
|
||||
private static AbstractComponent currentFocus = null;
|
||||
|
||||
/** The game container */
|
||||
protected GUIContext container;
|
||||
|
||||
/** Listeners for the component to notify */
|
||||
protected Set listeners;
|
||||
|
||||
/** True if this component currently has focus */
|
||||
private boolean focus = false;
|
||||
|
||||
/** The input we're responding to */
|
||||
protected Input input;
|
||||
|
||||
/**
|
||||
* Create a new component
|
||||
*
|
||||
* @param container
|
||||
* The container displaying this component
|
||||
*/
|
||||
public AbstractComponent(GUIContext container) {
|
||||
this.container = container;
|
||||
|
||||
listeners = new HashSet();
|
||||
|
||||
input = container.getInput();
|
||||
input.addPrimaryListener(this);
|
||||
|
||||
setLocation(0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a component listener to be informed when the component sees fit.
|
||||
*
|
||||
* It will ignore listeners already added.
|
||||
*
|
||||
* @param listener
|
||||
* listener
|
||||
*/
|
||||
public void addListener(ComponentListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a component listener.
|
||||
*
|
||||
* It will ignore if the listener wasn't added.
|
||||
*
|
||||
* @param listener
|
||||
* listener
|
||||
*/
|
||||
public void removeListener(ComponentListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify all the listeners.
|
||||
*/
|
||||
protected void notifyListeners() {
|
||||
Iterator it = listeners.iterator();
|
||||
while (it.hasNext()) {
|
||||
((ComponentListener) it.next()).componentActivated(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render this component to the screen
|
||||
*
|
||||
* @param container
|
||||
* The container displaying this component
|
||||
* @param g
|
||||
* The graphics context used to render to the display
|
||||
* @throws SlickException
|
||||
* If there has been an error rendering the component
|
||||
*/
|
||||
public abstract void render(GUIContext container, Graphics g)
|
||||
throws SlickException;
|
||||
|
||||
/**
|
||||
* Moves the component.
|
||||
*
|
||||
* @param x
|
||||
* X coordinate
|
||||
* @param y
|
||||
* Y coordinate
|
||||
*/
|
||||
public abstract void setLocation(int x, int y);
|
||||
|
||||
/**
|
||||
* Returns the position in the X coordinate
|
||||
*
|
||||
* @return x
|
||||
*/
|
||||
public abstract int getX();
|
||||
|
||||
/**
|
||||
* Returns the position in the Y coordinate
|
||||
*
|
||||
* @return y
|
||||
*/
|
||||
public abstract int getY();
|
||||
|
||||
/**
|
||||
* Get the width of the component
|
||||
*
|
||||
* @return The width of the component
|
||||
*/
|
||||
public abstract int getWidth();
|
||||
|
||||
/**
|
||||
* Get the height of the component
|
||||
*
|
||||
* @return The height of the component
|
||||
*/
|
||||
public abstract int getHeight();
|
||||
|
||||
/**
|
||||
* Indicate whether this component should be focused or not
|
||||
*
|
||||
* @param focus
|
||||
* if the component should be focused
|
||||
*/
|
||||
public void setFocus(boolean focus) {
|
||||
if (focus) {
|
||||
if (currentFocus != null) {
|
||||
currentFocus.setFocus(false);
|
||||
}
|
||||
currentFocus = this;
|
||||
} else {
|
||||
if (currentFocus == this) {
|
||||
currentFocus = null;
|
||||
}
|
||||
}
|
||||
this.focus = focus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this component currently has focus
|
||||
*
|
||||
* @return if this field currently has focus
|
||||
*/
|
||||
public boolean hasFocus() {
|
||||
return focus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Consume the event currently being processed
|
||||
*/
|
||||
protected void consumeEvent() {
|
||||
input.consumeEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives the focus to this component with a click of the mouse.
|
||||
*
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#mouseReleased(int, int, int)
|
||||
*/
|
||||
public void mouseReleased(int button, int x, int y) {
|
||||
setFocus(Rectangle.contains(x, y, getX(), getY(), getWidth(),
|
||||
getHeight()));
|
||||
}
|
||||
}
|
||||
83
lib/slick-source/org/newdawn/slick/gui/BasicComponent.java
Normal file
83
lib/slick-source/org/newdawn/slick/gui/BasicComponent.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package org.newdawn.slick.gui;
|
||||
|
||||
import org.newdawn.slick.Graphics;
|
||||
import org.newdawn.slick.SlickException;
|
||||
|
||||
/**
|
||||
* Renamed to provide backwards compatibility
|
||||
*
|
||||
* @author kevin
|
||||
* @deprecated
|
||||
*/
|
||||
public abstract class BasicComponent extends AbstractComponent {
|
||||
/** The x position of the component */
|
||||
protected int x;
|
||||
/** The y position of the component */
|
||||
protected int y;
|
||||
/** The width of the component */
|
||||
protected int width;
|
||||
/** The height of the component */
|
||||
protected int height;
|
||||
|
||||
/**
|
||||
* Create a new component
|
||||
*
|
||||
* @param container
|
||||
* The container displaying this component
|
||||
*/
|
||||
public BasicComponent(GUIContext container) {
|
||||
super(container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#getHeight()
|
||||
*/
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#getWidth()
|
||||
*/
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#getX()
|
||||
*/
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#getY()
|
||||
*/
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow the sub-component to render
|
||||
*
|
||||
* @param container The container holding the GUI
|
||||
* @param g The graphics context into which we should render
|
||||
*/
|
||||
public abstract void renderImpl(GUIContext container, Graphics g);
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#render(org.newdawn.slick.gui.GUIContext, org.newdawn.slick.Graphics)
|
||||
*/
|
||||
public void render(GUIContext container, Graphics g) throws SlickException {
|
||||
renderImpl(container,g);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#setLocation(int, int)
|
||||
*/
|
||||
public void setLocation(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.newdawn.slick.gui;
|
||||
|
||||
/**
|
||||
* A descritpion of a class responding to events occuring on a GUI component
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public interface ComponentListener {
|
||||
|
||||
/**
|
||||
* Notification that a component has been activated (button clicked,
|
||||
* text field entered, etc)
|
||||
*
|
||||
* @param source The source of the event
|
||||
*/
|
||||
public void componentActivated(AbstractComponent source);
|
||||
}
|
||||
103
lib/slick-source/org/newdawn/slick/gui/GUIContext.java
Normal file
103
lib/slick-source/org/newdawn/slick/gui/GUIContext.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package org.newdawn.slick.gui;
|
||||
|
||||
import org.lwjgl.input.Cursor;
|
||||
import org.newdawn.slick.Font;
|
||||
import org.newdawn.slick.Input;
|
||||
import org.newdawn.slick.SlickException;
|
||||
import org.newdawn.slick.opengl.ImageData;
|
||||
|
||||
/**
|
||||
* The context in which GUI components are created and rendered
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public interface GUIContext {
|
||||
|
||||
/**
|
||||
* Get the input system
|
||||
*
|
||||
* @return The input system available to this game container
|
||||
*/
|
||||
public Input getInput();
|
||||
|
||||
/**
|
||||
* Get the accurate system time
|
||||
*
|
||||
* @return The system time in milliseconds
|
||||
*/
|
||||
public long getTime();
|
||||
|
||||
/**
|
||||
* Get the width of the standard screen resolution
|
||||
*
|
||||
* @return The screen width
|
||||
*/
|
||||
public abstract int getScreenWidth();
|
||||
|
||||
/**
|
||||
* Get the height of the standard screen resolution
|
||||
*
|
||||
* @return The screen height
|
||||
*/
|
||||
public abstract int getScreenHeight();
|
||||
|
||||
/**
|
||||
* Get the width of the game canvas
|
||||
*
|
||||
* @return The width of the game canvas
|
||||
*/
|
||||
public int getWidth();
|
||||
|
||||
/**
|
||||
* Get the height of the game canvas
|
||||
*
|
||||
* @return The height of the game canvas
|
||||
*/
|
||||
public int getHeight();
|
||||
|
||||
/**
|
||||
* Get the default system font
|
||||
*
|
||||
* @return The default system font
|
||||
*/
|
||||
public Font getDefaultFont();
|
||||
|
||||
/**
|
||||
* Set the mouse cursor to be displayed - this is a hardware cursor and hence
|
||||
* shouldn't have any impact on FPS.
|
||||
*
|
||||
* @param ref The location of the image to be loaded for the cursor
|
||||
* @param hotSpotX The x coordinate of the hotspot within the cursor image
|
||||
* @param hotSpotY The y coordinate of the hotspot within the cursor image
|
||||
* @throws SlickException Indicates a failure to load the cursor image or create the hardware cursor
|
||||
*/
|
||||
public abstract void setMouseCursor(String ref, int hotSpotX, int hotSpotY) throws SlickException;
|
||||
|
||||
/**
|
||||
* Set the mouse cursor to be displayed - this is a hardware cursor and hence
|
||||
* shouldn't have any impact on FPS.
|
||||
*
|
||||
* @param data The image data from which the cursor can be construted
|
||||
* @param hotSpotX The x coordinate of the hotspot within the cursor image
|
||||
* @param hotSpotY The y coordinate of the hotspot within the cursor image
|
||||
* @throws SlickException Indicates a failure to load the cursor image or create the hardware cursor
|
||||
*/
|
||||
public abstract void setMouseCursor(ImageData data, int hotSpotX, int hotSpotY) throws SlickException;
|
||||
|
||||
/**
|
||||
* Set the mouse cursor to be displayed - this is a hardware cursor and hence
|
||||
* shouldn't have any impact on FPS.
|
||||
*
|
||||
* @param cursor The cursor to use
|
||||
* @param hotSpotX The x coordinate of the hotspot within the cursor image
|
||||
* @param hotSpotY The y coordinate of the hotspot within the cursor image
|
||||
* @throws SlickException Indicates a failure to load the cursor image or create the hardware cursor
|
||||
*/
|
||||
public abstract void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException;
|
||||
|
||||
/**
|
||||
* Set the default mouse cursor - i.e. the original cursor before any native
|
||||
* cursor was set
|
||||
*/
|
||||
public abstract void setDefaultMouseCursor();
|
||||
}
|
||||
433
lib/slick-source/org/newdawn/slick/gui/MouseOverArea.java
Normal file
433
lib/slick-source/org/newdawn/slick/gui/MouseOverArea.java
Normal file
@@ -0,0 +1,433 @@
|
||||
package org.newdawn.slick.gui;
|
||||
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.Graphics;
|
||||
import org.newdawn.slick.Image;
|
||||
import org.newdawn.slick.Input;
|
||||
import org.newdawn.slick.Sound;
|
||||
import org.newdawn.slick.geom.Rectangle;
|
||||
import org.newdawn.slick.geom.Shape;
|
||||
|
||||
/**
|
||||
* A mouse over area that can be used for menus or buttons
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class MouseOverArea extends AbstractComponent {
|
||||
/** The default state */
|
||||
private static final int NORMAL = 1;
|
||||
|
||||
/** The mouse down state */
|
||||
private static final int MOUSE_DOWN = 2;
|
||||
|
||||
/** The mouse over state */
|
||||
private static final int MOUSE_OVER = 3;
|
||||
|
||||
/** The normalImage being displayed in normal state */
|
||||
private Image normalImage;
|
||||
|
||||
/** The normalImage being displayed in mouseOver state */
|
||||
private Image mouseOverImage;
|
||||
|
||||
/** The normalImage being displayed in mouseDown state */
|
||||
private Image mouseDownImage;
|
||||
|
||||
/** The colour used in normal state */
|
||||
private Color normalColor = Color.white;
|
||||
|
||||
/** The colour used in mouseOver state */
|
||||
private Color mouseOverColor = Color.white;
|
||||
|
||||
/** The colour used in mouseDown state */
|
||||
private Color mouseDownColor = Color.white;
|
||||
|
||||
/** The sound for mouse over */
|
||||
private Sound mouseOverSound;
|
||||
|
||||
/** The sound for mouse down */
|
||||
private Sound mouseDownSound;
|
||||
|
||||
/** The shape defining the area */
|
||||
private Shape area;
|
||||
|
||||
/** The current normalImage being displayed */
|
||||
private Image currentImage;
|
||||
|
||||
/** The current color being used */
|
||||
private Color currentColor;
|
||||
|
||||
/** True if the mouse is over the area */
|
||||
private boolean over;
|
||||
|
||||
/** True if the mouse button is pressed */
|
||||
private boolean mouseDown;
|
||||
|
||||
/** The state of the area */
|
||||
private int state = NORMAL;
|
||||
|
||||
/** True if the mouse has been up since last press */
|
||||
private boolean mouseUp;
|
||||
|
||||
/**
|
||||
* Create a new mouse over area
|
||||
*
|
||||
* @param container
|
||||
* The container displaying the mouse over area
|
||||
* @param image
|
||||
* The normalImage to display
|
||||
* @param x
|
||||
* The position of the area
|
||||
* @param y
|
||||
* the position of the area
|
||||
* @param listener
|
||||
* A listener to add to the area
|
||||
*/
|
||||
public MouseOverArea(GUIContext container, Image image, int x, int y, ComponentListener listener) {
|
||||
this(container, image, x, y, image.getWidth(), image.getHeight());
|
||||
addListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new mouse over area
|
||||
*
|
||||
* @param container
|
||||
* The container displaying the mouse over area
|
||||
* @param image
|
||||
* The normalImage to display
|
||||
* @param x
|
||||
* The position of the area
|
||||
* @param y
|
||||
* the position of the area
|
||||
*/
|
||||
public MouseOverArea(GUIContext container, Image image, int x, int y) {
|
||||
this(container, image, x, y, image.getWidth(), image.getHeight());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new mouse over area
|
||||
*
|
||||
* @param container
|
||||
* The container displaying the mouse over area
|
||||
* @param image
|
||||
* The normalImage to display
|
||||
* @param x
|
||||
* The position of the area
|
||||
* @param y
|
||||
* the position of the area
|
||||
* @param width
|
||||
* The width of the area
|
||||
* @param height
|
||||
* The height of the area
|
||||
* @param listener
|
||||
* A listener to add to the area
|
||||
*/
|
||||
public MouseOverArea(GUIContext container, Image image, int x, int y,
|
||||
int width, int height, ComponentListener listener) {
|
||||
this(container,image,x,y,width,height);
|
||||
addListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new mouse over area
|
||||
*
|
||||
* @param container
|
||||
* The container displaying the mouse over area
|
||||
* @param image
|
||||
* The normalImage to display
|
||||
* @param x
|
||||
* The position of the area
|
||||
* @param y
|
||||
* the position of the area
|
||||
* @param width
|
||||
* The width of the area
|
||||
* @param height
|
||||
* The height of the area
|
||||
*/
|
||||
public MouseOverArea(GUIContext container, Image image, int x, int y,
|
||||
int width, int height) {
|
||||
this(container,image,new Rectangle(x,y,width,height));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new mouse over area
|
||||
*
|
||||
* @param container
|
||||
* The container displaying the mouse over area
|
||||
* @param image
|
||||
* The normalImage to display
|
||||
* @param shape
|
||||
* The shape defining the area of the mouse sensitive zone
|
||||
*/
|
||||
public MouseOverArea(GUIContext container, Image image, Shape shape) {
|
||||
super(container);
|
||||
|
||||
area = shape;
|
||||
normalImage = image;
|
||||
currentImage = image;
|
||||
mouseOverImage = image;
|
||||
mouseDownImage = image;
|
||||
|
||||
currentColor = normalColor;
|
||||
|
||||
state = NORMAL;
|
||||
Input input = container.getInput();
|
||||
over = area.contains(input.getMouseX(), input.getMouseY());
|
||||
mouseDown = input.isMouseButtonDown(0);
|
||||
updateImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the component.
|
||||
*
|
||||
* @param x X coordinate
|
||||
* @param y Y coordinate
|
||||
*/
|
||||
public void setLocation(float x, float y) {
|
||||
if (area != null) {
|
||||
area.setX(x);
|
||||
area.setY(y);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the x coordinate of this area
|
||||
*
|
||||
* @param x The new x coordinate of this area
|
||||
*/
|
||||
public void setX(float x) {
|
||||
area.setX(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the y coordinate of this area
|
||||
*
|
||||
* @param y The new y coordinate of this area
|
||||
*/
|
||||
public void setY(float y) {
|
||||
area.setY(y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the position in the X coordinate
|
||||
*
|
||||
* @return x
|
||||
*/
|
||||
public int getX() {
|
||||
return (int) area.getX();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the position in the Y coordinate
|
||||
*
|
||||
* @return y
|
||||
*/
|
||||
public int getY() {
|
||||
return (int) area.getY();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the normal color used on the image in the default state
|
||||
*
|
||||
* @param color
|
||||
* The color to be used
|
||||
*/
|
||||
public void setNormalColor(Color color) {
|
||||
normalColor = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color to be used when the mouse is over the area
|
||||
*
|
||||
* @param color
|
||||
* The color to be used when the mouse is over the area
|
||||
*/
|
||||
public void setMouseOverColor(Color color) {
|
||||
mouseOverColor = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the color to be used when the mouse is down the area
|
||||
*
|
||||
* @param color
|
||||
* The color to be used when the mouse is down the area
|
||||
*/
|
||||
public void setMouseDownColor(Color color) {
|
||||
mouseDownColor = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the normal image used on the image in the default state
|
||||
*
|
||||
* @param image
|
||||
* The image to be used
|
||||
*/
|
||||
public void setNormalImage(Image image) {
|
||||
normalImage = image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the image to be used when the mouse is over the area
|
||||
*
|
||||
* @param image
|
||||
* The image to be used when the mouse is over the area
|
||||
*/
|
||||
public void setMouseOverImage(Image image) {
|
||||
mouseOverImage = image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the image to be used when the mouse is down the area
|
||||
*
|
||||
* @param image
|
||||
* The image to be used when the mouse is down the area
|
||||
*/
|
||||
public void setMouseDownImage(Image image) {
|
||||
mouseDownImage = image;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#render(org.newdawn.slick.gui.GUIContext,
|
||||
* org.newdawn.slick.Graphics)
|
||||
*/
|
||||
public void render(GUIContext container, Graphics g) {
|
||||
if (currentImage != null) {
|
||||
|
||||
int xp = (int) (area.getX() + ((getWidth() - currentImage.getWidth()) / 2));
|
||||
int yp = (int) (area.getY() + ((getHeight() - currentImage.getHeight()) / 2));
|
||||
|
||||
currentImage.draw(xp, yp, currentColor);
|
||||
} else {
|
||||
g.setColor(currentColor);
|
||||
g.fill(area);
|
||||
}
|
||||
updateImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the current normalImage based on the mouse state
|
||||
*/
|
||||
private void updateImage() {
|
||||
if (!over) {
|
||||
currentImage = normalImage;
|
||||
currentColor = normalColor;
|
||||
state = NORMAL;
|
||||
mouseUp = false;
|
||||
} else {
|
||||
if (mouseDown) {
|
||||
if ((state != MOUSE_DOWN) && (mouseUp)) {
|
||||
if (mouseDownSound != null) {
|
||||
mouseDownSound.play();
|
||||
}
|
||||
currentImage = mouseDownImage;
|
||||
currentColor = mouseDownColor;
|
||||
state = MOUSE_DOWN;
|
||||
|
||||
notifyListeners();
|
||||
mouseUp = false;
|
||||
}
|
||||
|
||||
return;
|
||||
} else {
|
||||
mouseUp = true;
|
||||
if (state != MOUSE_OVER) {
|
||||
if (mouseOverSound != null) {
|
||||
mouseOverSound.play();
|
||||
}
|
||||
currentImage = mouseOverImage;
|
||||
currentColor = mouseOverColor;
|
||||
state = MOUSE_OVER;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mouseDown = false;
|
||||
state = NORMAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the mouse over sound effect
|
||||
*
|
||||
* @param sound
|
||||
* The mouse over sound effect
|
||||
*/
|
||||
public void setMouseOverSound(Sound sound) {
|
||||
mouseOverSound = sound;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the mouse down sound effect
|
||||
*
|
||||
* @param sound
|
||||
* The mouse down sound effect
|
||||
*/
|
||||
public void setMouseDownSound(Sound sound) {
|
||||
mouseDownSound = sound;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.util.InputAdapter#mouseMoved(int, int, int, int)
|
||||
*/
|
||||
public void mouseMoved(int oldx, int oldy, int newx, int newy) {
|
||||
over = area.contains(newx, newy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.util.InputAdapter#mouseDragged(int, int, int, int)
|
||||
*/
|
||||
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
|
||||
mouseMoved(oldx, oldy, newx, newy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.util.InputAdapter#mousePressed(int, int, int)
|
||||
*/
|
||||
public void mousePressed(int button, int mx, int my) {
|
||||
over = area.contains(mx, my);
|
||||
if (button == 0) {
|
||||
mouseDown = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.util.InputAdapter#mouseReleased(int, int, int)
|
||||
*/
|
||||
public void mouseReleased(int button, int mx, int my) {
|
||||
over = area.contains(mx, my);
|
||||
if (button == 0) {
|
||||
mouseDown = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#getHeight()
|
||||
*/
|
||||
public int getHeight() {
|
||||
return (int) (area.getMaxY() - area.getY());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#getWidth()
|
||||
*/
|
||||
public int getWidth() {
|
||||
return (int) (area.getMaxX() - area.getX());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the mouse is over this area
|
||||
*
|
||||
* @return True if the mouse is over this area
|
||||
*/
|
||||
public boolean isMouseOver() {
|
||||
return over;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the location of this area
|
||||
*
|
||||
* @param x The x coordinate of this area
|
||||
* @param y The y coordiante of this area
|
||||
*/
|
||||
public void setLocation(int x, int y) {
|
||||
setLocation((float) x,(float) y);
|
||||
}
|
||||
}
|
||||
476
lib/slick-source/org/newdawn/slick/gui/TextField.java
Normal file
476
lib/slick-source/org/newdawn/slick/gui/TextField.java
Normal file
@@ -0,0 +1,476 @@
|
||||
package org.newdawn.slick.gui;
|
||||
|
||||
import org.lwjgl.Sys;
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.Font;
|
||||
import org.newdawn.slick.Graphics;
|
||||
import org.newdawn.slick.Input;
|
||||
import org.newdawn.slick.geom.Rectangle;
|
||||
|
||||
/**
|
||||
* A single text field supporting text entry
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class TextField extends AbstractComponent {
|
||||
/** The key repeat interval */
|
||||
private static final int INITIAL_KEY_REPEAT_INTERVAL = 400;
|
||||
/** The key repeat interval */
|
||||
private static final int KEY_REPEAT_INTERVAL = 50;
|
||||
|
||||
/** The width of the field */
|
||||
private int width;
|
||||
|
||||
/** The height of the field */
|
||||
private int height;
|
||||
|
||||
/** The location in the X coordinate */
|
||||
protected int x;
|
||||
|
||||
/** The location in the Y coordinate */
|
||||
protected int y;
|
||||
|
||||
/** The maximum number of characters allowed to be input */
|
||||
private int maxCharacter = 10000;
|
||||
|
||||
/** The value stored in the text field */
|
||||
private String value = "";
|
||||
|
||||
/** The font used to render text in the field */
|
||||
private Font font;
|
||||
|
||||
/** The border color - null if no border */
|
||||
private Color border = Color.white;
|
||||
|
||||
/** The text color */
|
||||
private Color text = Color.white;
|
||||
|
||||
/** The background color - null if no background */
|
||||
private Color background = new Color(0, 0, 0, 0.5f);
|
||||
|
||||
/** The current cursor position */
|
||||
private int cursorPos;
|
||||
|
||||
/** True if the cursor should be visible */
|
||||
private boolean visibleCursor = true;
|
||||
|
||||
/** The last key pressed */
|
||||
private int lastKey = -1;
|
||||
|
||||
/** The last character pressed */
|
||||
private char lastChar = 0;
|
||||
|
||||
/** The time since last key repeat */
|
||||
private long repeatTimer;
|
||||
|
||||
/** The text before the paste in */
|
||||
private String oldText;
|
||||
|
||||
/** The cursor position before the paste */
|
||||
private int oldCursorPos;
|
||||
|
||||
/** True if events should be consumed by the field */
|
||||
private boolean consume = true;
|
||||
|
||||
/**
|
||||
* Create a new text field
|
||||
*
|
||||
* @param container
|
||||
* The container rendering this field
|
||||
* @param font
|
||||
* The font to use in the text field
|
||||
* @param x
|
||||
* The x coordinate of the top left corner of the text field
|
||||
* @param y
|
||||
* The y coordinate of the top left corner of the text field
|
||||
* @param width
|
||||
* The width of the text field
|
||||
* @param height
|
||||
* The height of the text field
|
||||
* @param listener
|
||||
* The listener to add to the text field
|
||||
*/
|
||||
public TextField(GUIContext container, Font font, int x, int y, int width,
|
||||
int height, ComponentListener listener) {
|
||||
this(container,font,x,y,width,height);
|
||||
addListener(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new text field
|
||||
*
|
||||
* @param container
|
||||
* The container rendering this field
|
||||
* @param font
|
||||
* The font to use in the text field
|
||||
* @param x
|
||||
* The x coordinate of the top left corner of the text field
|
||||
* @param y
|
||||
* The y coordinate of the top left corner of the text field
|
||||
* @param width
|
||||
* The width of the text field
|
||||
* @param height
|
||||
* The height of the text field
|
||||
*/
|
||||
public TextField(GUIContext container, Font font, int x, int y, int width,
|
||||
int height) {
|
||||
super(container);
|
||||
|
||||
this.font = font;
|
||||
|
||||
setLocation(x, y);
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate if the input events should be consumed by this field
|
||||
*
|
||||
* @param consume True if events should be consumed by this field
|
||||
*/
|
||||
public void setConsumeEvents(boolean consume) {
|
||||
this.consume = consume;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate the key input handling for this field
|
||||
*/
|
||||
public void deactivate() {
|
||||
setFocus(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the component.
|
||||
*
|
||||
* @param x
|
||||
* X coordinate
|
||||
* @param y
|
||||
* Y coordinate
|
||||
*/
|
||||
public void setLocation(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the position in the X coordinate
|
||||
*
|
||||
* @return x
|
||||
*/
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the position in the Y coordinate
|
||||
*
|
||||
* @return y
|
||||
*/
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the width of the component
|
||||
*
|
||||
* @return The width of the component
|
||||
*/
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the height of the component
|
||||
*
|
||||
* @return The height of the component
|
||||
*/
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the background color. Set to null to disable the background
|
||||
*
|
||||
* @param color
|
||||
* The color to use for the background
|
||||
*/
|
||||
public void setBackgroundColor(Color color) {
|
||||
background = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the border color. Set to null to disable the border
|
||||
*
|
||||
* @param color
|
||||
* The color to use for the border
|
||||
*/
|
||||
public void setBorderColor(Color color) {
|
||||
border = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the text color.
|
||||
*
|
||||
* @param color
|
||||
* The color to use for the text
|
||||
*/
|
||||
public void setTextColor(Color color) {
|
||||
text = color;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#render(org.newdawn.slick.gui.GUIContext,
|
||||
* org.newdawn.slick.Graphics)
|
||||
*/
|
||||
public void render(GUIContext container, Graphics g) {
|
||||
if (lastKey != -1) {
|
||||
if (input.isKeyDown(lastKey)) {
|
||||
if (repeatTimer < System.currentTimeMillis()) {
|
||||
repeatTimer = System.currentTimeMillis() + KEY_REPEAT_INTERVAL;
|
||||
keyPressed(lastKey, lastChar);
|
||||
}
|
||||
} else {
|
||||
lastKey = -1;
|
||||
}
|
||||
}
|
||||
Rectangle oldClip = g.getClip();
|
||||
g.setWorldClip(x,y,width, height);
|
||||
|
||||
// Someone could have set a color for me to blend...
|
||||
Color clr = g.getColor();
|
||||
|
||||
if (background != null) {
|
||||
g.setColor(background.multiply(clr));
|
||||
g.fillRect(x, y, width, height);
|
||||
}
|
||||
g.setColor(text.multiply(clr));
|
||||
Font temp = g.getFont();
|
||||
|
||||
int cpos = font.getWidth(value.substring(0, cursorPos));
|
||||
int tx = 0;
|
||||
if (cpos > width) {
|
||||
tx = width - cpos - font.getWidth("_");
|
||||
}
|
||||
|
||||
g.translate(tx + 2, 0);
|
||||
g.setFont(font);
|
||||
g.drawString(value, x + 1, y + 1);
|
||||
|
||||
if (hasFocus() && visibleCursor) {
|
||||
g.drawString("_", x + 1 + cpos + 2, y + 1);
|
||||
}
|
||||
|
||||
g.translate(-tx - 2, 0);
|
||||
|
||||
if (border != null) {
|
||||
g.setColor(border.multiply(clr));
|
||||
g.drawRect(x, y, width, height);
|
||||
}
|
||||
g.setColor(clr);
|
||||
g.setFont(temp);
|
||||
g.clearWorldClip();
|
||||
g.setClip(oldClip);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value in the text field
|
||||
*
|
||||
* @return The value in the text field
|
||||
*/
|
||||
public String getText() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value to be displayed in the text field
|
||||
*
|
||||
* @param value
|
||||
* The value to be displayed in the text field
|
||||
*/
|
||||
public void setText(String value) {
|
||||
this.value = value;
|
||||
if (cursorPos > value.length()) {
|
||||
cursorPos = value.length();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position of the cursor
|
||||
*
|
||||
* @param pos
|
||||
* The new position of the cursor
|
||||
*/
|
||||
public void setCursorPos(int pos) {
|
||||
cursorPos = pos;
|
||||
if (cursorPos > value.length()) {
|
||||
cursorPos = value.length();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate whether the mouse cursor should be visible or not
|
||||
*
|
||||
* @param visibleCursor
|
||||
* True if the mouse cursor should be visible
|
||||
*/
|
||||
public void setCursorVisible(boolean visibleCursor) {
|
||||
this.visibleCursor = visibleCursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the length of the allowed input
|
||||
*
|
||||
* @param length
|
||||
* The length of the allowed input
|
||||
*/
|
||||
public void setMaxLength(int length) {
|
||||
maxCharacter = length;
|
||||
if (value.length() > maxCharacter) {
|
||||
value = value.substring(0, maxCharacter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the paste into the field, overrideable for custom behaviour
|
||||
*
|
||||
* @param text The text to be pasted in
|
||||
*/
|
||||
protected void doPaste(String text) {
|
||||
recordOldPosition();
|
||||
|
||||
for (int i=0;i<text.length();i++) {
|
||||
keyPressed(-1, text.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Record the old position and content
|
||||
*/
|
||||
protected void recordOldPosition() {
|
||||
oldText = getText();
|
||||
oldCursorPos = cursorPos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the undo of the paste, overrideable for custom behaviour
|
||||
*
|
||||
* @param oldCursorPos before the paste
|
||||
* @param oldText The text before the last paste
|
||||
*/
|
||||
protected void doUndo(int oldCursorPos, String oldText) {
|
||||
if (oldText != null) {
|
||||
setText(oldText);
|
||||
setCursorPos(oldCursorPos);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#keyPressed(int, char)
|
||||
*/
|
||||
public void keyPressed(int key, char c) {
|
||||
if (hasFocus()) {
|
||||
if (key != -1)
|
||||
{
|
||||
if ((key == Input.KEY_V) &&
|
||||
((input.isKeyDown(Input.KEY_LCONTROL)) || (input.isKeyDown(Input.KEY_RCONTROL)))) {
|
||||
String text = Sys.getClipboard();
|
||||
if (text != null) {
|
||||
doPaste(text);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if ((key == Input.KEY_Z) &&
|
||||
((input.isKeyDown(Input.KEY_LCONTROL)) || (input.isKeyDown(Input.KEY_RCONTROL)))) {
|
||||
if (oldText != null) {
|
||||
doUndo(oldCursorPos, oldText);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// alt and control keys don't come through here
|
||||
if (input.isKeyDown(Input.KEY_LCONTROL) || input.isKeyDown(Input.KEY_RCONTROL)) {
|
||||
return;
|
||||
}
|
||||
if (input.isKeyDown(Input.KEY_LALT) || input.isKeyDown(Input.KEY_RALT)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (lastKey != key) {
|
||||
lastKey = key;
|
||||
repeatTimer = System.currentTimeMillis() + INITIAL_KEY_REPEAT_INTERVAL;
|
||||
} else {
|
||||
repeatTimer = System.currentTimeMillis() + KEY_REPEAT_INTERVAL;
|
||||
}
|
||||
lastChar = c;
|
||||
|
||||
if (key == Input.KEY_LEFT) {
|
||||
if (cursorPos > 0) {
|
||||
cursorPos--;
|
||||
}
|
||||
// Nobody more will be notified
|
||||
if (consume) {
|
||||
container.getInput().consumeEvent();
|
||||
}
|
||||
} else if (key == Input.KEY_RIGHT) {
|
||||
if (cursorPos < value.length()) {
|
||||
cursorPos++;
|
||||
}
|
||||
// Nobody more will be notified
|
||||
if (consume) {
|
||||
container.getInput().consumeEvent();
|
||||
}
|
||||
} else if (key == Input.KEY_BACK) {
|
||||
if ((cursorPos > 0) && (value.length() > 0)) {
|
||||
if (cursorPos < value.length()) {
|
||||
value = value.substring(0, cursorPos - 1)
|
||||
+ value.substring(cursorPos);
|
||||
} else {
|
||||
value = value.substring(0, cursorPos - 1);
|
||||
}
|
||||
cursorPos--;
|
||||
}
|
||||
// Nobody more will be notified
|
||||
if (consume) {
|
||||
container.getInput().consumeEvent();
|
||||
}
|
||||
} else if (key == Input.KEY_DELETE) {
|
||||
if (value.length() > cursorPos) {
|
||||
value = value.substring(0,cursorPos) + value.substring(cursorPos+1);
|
||||
}
|
||||
// Nobody more will be notified
|
||||
if (consume) {
|
||||
container.getInput().consumeEvent();
|
||||
}
|
||||
} else if ((c < 127) && (c > 31) && (value.length() < maxCharacter)) {
|
||||
if (cursorPos < value.length()) {
|
||||
value = value.substring(0, cursorPos) + c
|
||||
+ value.substring(cursorPos);
|
||||
} else {
|
||||
value = value.substring(0, cursorPos) + c;
|
||||
}
|
||||
cursorPos++;
|
||||
// Nobody more will be notified
|
||||
if (consume) {
|
||||
container.getInput().consumeEvent();
|
||||
}
|
||||
} else if (key == Input.KEY_RETURN) {
|
||||
notifyListeners();
|
||||
// Nobody more will be notified
|
||||
if (consume) {
|
||||
container.getInput().consumeEvent();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.gui.AbstractComponent#setFocus(boolean)
|
||||
*/
|
||||
public void setFocus(boolean focus) {
|
||||
lastKey = -1;
|
||||
|
||||
super.setFocus(focus);
|
||||
}
|
||||
}
|
||||
3
lib/slick-source/org/newdawn/slick/gui/package.html
Normal file
3
lib/slick-source/org/newdawn/slick/gui/package.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<BODY>
|
||||
Some extremely simple GUI elements which should be used where a game does not require a full GUI
|
||||
</BODY>
|
||||
Reference in New Issue
Block a user