📄 simplemenu2ddrawable.java
字号:
/*
* Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books.
* Copyright (C) 2001-2002 WOTLAS Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package wotlas.libs.graphics2D.menu;
import wotlas.libs.graphics2D.*;
import java.awt.*;
import java.util.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.net.URL;
/** A drawable that displays a menu representing the content of the SimpleMenu2D.
*
* @author Aldiss
*/
public class SimpleMenu2DDrawable extends Drawable {
/*------------------------------------------------------------------------------------*/
/** Default Font Name used.
*/
// static private String defaultFontName = "dialog";
// diego : i cant see menu 'cause it seems dialog isnt the right name, at least on my pc with win2k
static private String defaultFontName = "Dialog.plain";
/*------------------------------------------------------------------------------------*/
/** Some geometric definitions.
*/
static final private int DIST_BETWEEN_TWO_LINES = 5;
static final private int MINIMUM_MENU_WIDTH = 40;
static final private int HORIZONTAL_BORDER = 5;
static final private int BAR_IMAGE_WIDTH = 100;
/** Animation speed : time for 100 pixels moved
*/
static final private int ANIM_SPEED = 300; // 300ms to display 100 pixels
/** Maximum time them menu is displayed if none of its item is selected
*/
static final private int DISPLAY_TIMEOUT = 3000; // 3s
/** DRAWABLE PRIORITIES
*/
static final public short MENU_PRIORITY = 1000; // menu drawable
/** Static Menu images
*/
static public Image middleBarImage;
static public Image arrowRightImage;
static public Image arrowRightSelectImage;
static public Image arrowDownImage;
static public Image arrowDownSelectImage;
static public Image arrowUpImage;
static public Image arrowUpSelectImage;
/** We load the images...
*/
static {
// we load our small menu images
String basePath = "images/";
MediaTracker tracker = new MediaTracker(new Label());
URL url = SimpleMenu2DDrawable.class.getResource(basePath+"middle-bar.gif");
/*
if( url==null ) {
// We try inside a JAR...
basePath = "/wotlas/libs/graphics2D/menu/images/";
url = SimpleMenu2DDrawable.class.getResource(basePath+"middle-bar.gif");
}
*/
middleBarImage = Toolkit.getDefaultToolkit().getImage( url );
tracker.addImage(middleBarImage,0);
url = SimpleMenu2DDrawable.class.getResource(basePath+"arrow-right.gif");
arrowRightImage = Toolkit.getDefaultToolkit().getImage( url );
tracker.addImage(arrowRightImage,1);
url = SimpleMenu2DDrawable.class.getResource(basePath+"arrow-right-select.gif");
arrowRightSelectImage = Toolkit.getDefaultToolkit().getImage( url );
tracker.addImage(arrowRightSelectImage,2);
url = SimpleMenu2DDrawable.class.getResource(basePath+"arrow-down.gif");
arrowDownImage = Toolkit.getDefaultToolkit().getImage( url );
tracker.addImage(arrowDownImage,3);
url = SimpleMenu2DDrawable.class.getResource(basePath+"arrow-down-select.gif");
arrowDownSelectImage = Toolkit.getDefaultToolkit().getImage( url );
tracker.addImage(arrowDownSelectImage,4);
url = SimpleMenu2DDrawable.class.getResource(basePath+"arrow-up.gif");
arrowUpImage = Toolkit.getDefaultToolkit().getImage( url );
tracker.addImage(arrowUpImage,5);
url = SimpleMenu2DDrawable.class.getResource(basePath+"arrow-up-select.gif");
arrowUpSelectImage = Toolkit.getDefaultToolkit().getImage( url );
tracker.addImage(arrowUpSelectImage,6);
try{
tracker.waitForAll();
}catch(InterruptedException e) { e.printStackTrace(); }
};
/** Selected text color
*/
public static final Color blueColor = new Color( 20, 80, 160 );
/*------------------------------------------------------------------------------------*/
/** Our Simple Menu2D from which we'll take our data from.
*/
private SimpleMenu2D menu2D;
/** Font to use.
*/
private Font font;
/** The name of the font used
*/
private String fontName;
/** Text Font Size.
*/
private float size;
/** True if we must recompute text width and height
*/
private boolean recompute;
/** Our parent menu rectangle (just used when the menu is displayed)
*/
private Rectangle parentRectangle;
/** TimeStamp for menu animation
*/
private long timeStamp;
/** Next left-upper Point of the menu location
*/
private Point pNext;
/*------------------------------------------------------------------------------------*/
/** To get the default font name.
*/
static public String getDefaultFontName() {
return defaultFontName;
}
/*------------------------------------------------------------------------------------*/
/** To set the default font name.
*/
static public void setDefaultFontName( String fontName ) {
defaultFontName = fontName;
}
/*------------------------------------------------------------------------------------*/
/** Constructor with our menu reference.
* @param menu2D menu data
* @param p position where the menu should appear.
* @param priority drawable priority
*/
public SimpleMenu2DDrawable( SimpleMenu2D menu2D ) {
super();
this.menu2D = menu2D;
priority = MENU_PRIORITY;
fontName=defaultFontName;
r.x = 0;
r.y = 0;
recompute = true;
size = 10;
pNext = new Point(-1,-1);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To initialize this drawable with the ImageLibrary. Don't call it yourself ! it's
* done automatically when you call addDrawable on the GraphicsDirector.
*
* IF you need the ImageLib for some special inits just extend this method and don't
* forget to call a super.init(imageLib) !
*
* @param imagelib ImageLibrary where you can take the images to display.
*/
protected void init( ImageLibrary imageLib ) {
super.init(imageLib);
setFont(fontName); // init the font & size
setSize(size);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To refresh the menu's state
*/
public void refreshState() {
recompute = true;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To animate the menu.
*/
public void animateMenu() {
timeStamp = System.currentTimeMillis(); // timestamp == now
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To set the parent menu rectangle.
*/
public void setParentRectangle( Rectangle r ) {
parentRectangle = r;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** We return the index of the item which is at the y range.
*/
public int getItemAt(int y){
return menu2D.getFirstItemIndex()
+ ( y-r.y-DIST_BETWEEN_TWO_LINES/2-1 )/( (int)size+DIST_BETWEEN_TWO_LINES );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -