📄 simplemenu2d.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 java.awt.event.MouseEvent;
import java.awt.Rectangle;
import java.awt.Point;
/** A simple 2D menu. Contains menu items and a grapical representation.
*
* @author Aldiss
*/
public class SimpleMenu2D implements Menu2D {
/*------------------------------------------------------------------------------------*/
/** Maximum number of items displayed at the same time. If the menu has more
* items than this limit, arrows are displayed to navigate in the list.
*/
public static final int MAX_ITEMS_DISPLAYED = 10;
/** Time to wait before automatic scrolling (ms)
*/
public static final int MIN_TIME_BEFORE_SCROLL = 2000;
/** Time to wait between two mouse move automatic navigation (ms)
*/
public static final int MIN_TIME_BETWEEN_SCROLL = 200;
/** Min Time before we display a menu
*/
public static final int MIN_TIME_BEFORE_MENU = 50;
/*------------------------------------------------------------------------------------*/
/** Menu name.
*/
protected String name;
/** Menu Items
*/
protected SimpleMenu2DItem items[];
/** Index of the currently selected item
*/
protected int selectedItemIndex;
/** Index of the first item displayed on top of the list ( used if the list
* contains too many items to be displayed )
*/
protected int firstItemIndex;
/** Is the first index an arrow ?
*/
protected boolean isFirstIndexArrow;
/** Is this menu visible ?
*/
protected boolean isVisible;
/** Our menu manager
*/
protected Menu2DManager menuManager;
/** Our graphical representation
*/
protected SimpleMenu2DDrawable menuDrawable;
/** TimeStamp for automatic actions
*/
protected long timeStamp;
/** Can we automatically scroll ?
*/
protected boolean scrollEnabled;
/** Initial position when this menu is being dragged.
*/
protected int dragFromX, dragFromY;
/*------------------------------------------------------------------------------------*/
/** Constructor with menu name.
*/
public SimpleMenu2D( String name ) {
this( name, null );
}
/*------------------------------------------------------------------------------------*/
/** Constructor with menu name and items to display.
*/
public SimpleMenu2D( String name, String items[] ) {
this.name = name;
setItems( items );
selectedItemIndex = -1;
firstItemIndex = 0;
isFirstIndexArrow = false;
isVisible = false;
scrollEnabled=false;
menuDrawable = new SimpleMenu2DDrawable(this);
}
/*------------------------------------------------------------------------------------*/
/** To initialize this menu.
*/
public void init( Menu2DManager menuManager ) {
this.menuManager = menuManager;
if(items!=null)
for( int i=0; i<items.length; i++ )
if( items[i].link !=null )
items[i].link.init( menuManager );
}
/*------------------------------------------------------------------------------------*/
/** To initialize the content of this menu. The list of items replace the previous
* one.
*/
public void setItems( String itemsName[] ) {
// previous items ?
if( items!=null )
for( int i=0; i<items.length; i++ )
if( items[i].link !=null )
items[i].link.hide();
// items check
if( itemsName==null ) {
items = new SimpleMenu2DItem[0];
return;
}
items = new SimpleMenu2DItem[itemsName.length];
for( int i=0; i<itemsName.length; i++ )
items[i] = new SimpleMenu2DItem( itemsName[i] );
// we refresh the state of our drawable...
if(menuDrawable!=null)
menuDrawable.refreshState();
}
/*------------------------------------------------------------------------------------*/
/** To get the item name at the specified index.
* @return null if not found, the itemname otherwise
*/
public String getItemName( int index ) {
if( index<0 || index>=items.length )
return null;
return items[index].itemName;
}
/*------------------------------------------------------------------------------------*/
/** To get an item's index given its name.
* @return -1 if not found, the item's index otherwise
*/
public int getItemIndex( String itemName ) {
// we search the item's index
for( int i=0; i<items.length; i++ )
if( items[i].itemName.equals(itemName) )
return i;
return -1; // not found
}
/*------------------------------------------------------------------------------------*/
/** To change the name of an item.
* @param oldItemName item to search & update
* @param newItemName new name to set
* @return true if the item was found, false if not found
*/
public boolean changeItemName( String oldItemName, String newItemName ) {
// we search the item index
for( int i=0; i<items.length; i++ )
if( items[i].itemName.equals(oldItemName) ) {
if( items[i].link!=null && items[i].link.isVisible() )
items[i].link.hide();
items[i].itemName = newItemName;
menuDrawable.refreshState();
return true;
}
return false; // not found
}
/*------------------------------------------------------------------------------------*/
/** To add an item to the list. This method is not synchronized.
* @param itemName name of the item to add
*/
public void addItem( String itemName ) {
// any sub-menus too hide ?
if( items!=null )
for( int i=0; i<items.length; i++ )
if( items[i].link !=null )
items[i].link.hide();
// items update
SimpleMenu2DItem tmp[];
if( items==null )
tmp = new SimpleMenu2DItem[1];
else
tmp = new SimpleMenu2DItem[items.length+1];
System.arraycopy( items, 0, tmp, 0, tmp.length-1 );
tmp[tmp.length-1] = new SimpleMenu2DItem(itemName);
// we refresh the state of our drawable...
items = tmp;
menuDrawable.refreshState();
}
/*------------------------------------------------------------------------------------*/
/** To remove an item from the list. This method is not synchronized.
* @param itemName name of the item to remove
* @return true if removed, false if not found
*/
public boolean removeItem( String itemName ) {
// Item index
int ind = getItemIndex(itemName);
if( ind<0 )
return false;
// any sub-menus to hide ?
if( items!=null )
for( int i=0; i<items.length; i++ )
if( items[i].link !=null )
items[i].link.hide();
// items update
SimpleMenu2DItem tmp[] = new SimpleMenu2DItem[items.length-1];
System.arraycopy( items, 0, tmp, 0, ind );
if(ind+1<items.length)
System.arraycopy( items, ind+1, tmp, ind, items.length-ind-1 );
// we refresh the state of our drawable...
items = tmp;
menuDrawable.refreshState();
return true;
}
/*------------------------------------------------------------------------------------*/
/** To enable/disable an item.
* @param itemName to search and to enable/disable
* @param enabled true to enable, false to disable.
* @return true if the item was found, false if not found
*/
public boolean setItemEnabled( String itemName, boolean enabled ) {
// we search the item index
for( int i=0; i<items.length; i++ )
if( items[i].itemName.equals(itemName) ) {
if( items[i].isEnabled && !enabled && items[i].link!=null
&& items[i].link.isVisible() )
items[i].link.hide();
items[i].isEnabled = enabled;
return true;
}
return false; // not found
}
/*------------------------------------------------------------------------------------*/
/** To add a menu link to an item. The link replaces any previous link.
* @param itemName to search and add the link on
* @param menu2D to add (beware ! MUST be a SimpleMenu2D here)
* @return true if the item was found & the menu added, false if not found
*/
public boolean addItemLink( String itemName, Menu2D menu2D ) {
// we search the item index
for( int i=0; i<items.length; i++ )
if( items[i].itemName.equals(itemName) ) {
if( items[i].link!=null && items[i].link.isVisible() )
items[i].link.hide();
menu2D.init(menuManager); // we init the sub-menu
items[i].link = (SimpleMenu2D) menu2D;
return true;
}
return false; // not found
}
/*------------------------------------------------------------------------------------*/
/** To remove a menu link on an item.
* @param itemName to search and remove the link on
* @return true if the item was found & the menu removed, false if not found
*/
public boolean removeItemLink( String itemName ) {
// we search the item index
for( int i=0; i<items.length; i++ )
if( items[i].itemName.equals(itemName) ) {
if( items[i].link!=null && items[i].link.isVisible() )
items[i].link.hide();
items[i].link = null;
return true;
}
return false; // not found
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -