📄 menubar.java
字号:
//#endif
}
}
//#ifdef polish.useDynamicStyles
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#createCssSelector()
*/
protected String createCssSelector() {
return "menubar";
}
//#endif
/**
* Used to toggle the opened state of the menu bar
*
* @param open true when the menu should be opened
*/
protected void setOpen( boolean open ) {
this.isInitialised = (open == this.isOpened);
if (!open && this.isOpened) {
this.commandsContainer.hideNotify();
} else if (open && !this.isOpened) {
//#if !polish.MenuBar.focusFirstAfterClose
// focus the first item again, so when the user opens the menu again, it will be "fresh" again
this.commandsContainer.focus(0);
//#endif
this.commandsContainer.showNotify();
}
this.isOpened = open;
this.isInitialised = false;
}
protected boolean handleKeyPressed(int keyCode, int gameAction) {
//#debug
//# System.out.println("MenuBar: handleKeyPressed(" + keyCode + ", " + gameAction );
this.isSoftKeyPressed = false;
if (this.isOpened) {
if (keyCode == this.selectOptionsMenuKey) {
this.isSoftKeyPressed = true;
CommandItem commandItem = (CommandItem) this.commandsContainer.getFocusedItem();
//#if tmp.useInvisibleMenuBar
//# if (commandItem.command == this.hideCommand ) {
//# setOpen( false );
//# return true;
//# }
//# //TODO find a way how to handle the hide command on BlackBerry when it is invoked directly...
//#endif
commandItem.handleKeyPressed( 0, Canvas.FIRE );
// boolean handled = commandItem.handleKeyPressed( 0, Canvas.FIRE );
// if (!handled) { // CommandItem returns false when it invokes the command listener
// this is now done automatically by the Screen class
// setOpen( false );
// }
return true;
//#if polish.key.ReturnKey:defined
//#= } else if (keyCode == this.closeOptionsMenuKey || keyCode == ${polish.key.ReturnKey}) {
//#else
} else if (keyCode == this.closeOptionsMenuKey) {
//#endif
this.isSoftKeyPressed = true;
int selectedIndex = this.commandsContainer.getFocusedIndex();
if (!this.commandsContainer.handleKeyPressed(0, Canvas.LEFT)
|| selectedIndex != this.commandsContainer.getFocusedIndex() )
{
setOpen( false );
}
//System.out.println("MenuBar is closing due to key " + keyCode);
return true;
} else {
if (keyCode != 0) {
gameAction = this.screen.getGameAction(keyCode);
}
//#if tmp.useInvisibleMenuBar
//# // handle hide command specifically:
//# if ( gameAction == Canvas.FIRE && ((CommandItem)this.commandsContainer.focusedItem).command == this.hideCommand ) {
//# setOpen( false );
//# return true;
//# }
//#endif
// if (gameAction == Canvas.FIRE) {
// int focusedIndex = this.commandsContainer.focusedIndex;
// Command command = (Command) this.commandsList.get(focusedIndex);
// setOpen( false );
// this.screen.callCommandListener(command);
// return true;
// }
boolean handled = this.commandsContainer.handleKeyPressed(keyCode, gameAction);
if (handled) {
this.isInitialised = false;
} else {
if (gameAction == Canvas.DOWN || gameAction == Canvas.UP) {
//#debug error
//# System.out.println("Container DID NOT HANDLE DOWN OR UP, selectedIndex=" + this.commandsContainer.getFocusedIndex() + ", count="+ this.commandsContainer.size() );
}
setOpen( false );
}
return handled;
}
} else {
if (keyCode == LEFT_SOFT_KEY && this.singleLeftCommand != null) {
this.isSoftKeyPressed = true;
this.screen.callCommandListener(this.singleLeftCommand);
return true;
} else if (keyCode == RIGHT_SOFT_KEY && this.singleRightCommand != null) {
this.isSoftKeyPressed = true;
this.screen.callCommandListener(this.singleRightCommand);
return true;
} else if (keyCode == this.openOptionsMenuKey ) {
this.isSoftKeyPressed = true;
//#if tmp.useInvisibleMenuBar
//# if ( !this.isOpened && this.positiveCommand != null
//# && ((this.singleRightCommand != null && this.commandsContainer.size() == 3)
//# || (this.singleRightCommand == null && this.commandsContainer.size() == 2)) )
//# {
//# // invoke positive command:
//# this.screen.callCommandListener(this.positiveCommand);
//# return true;
//# } else
//#endif
if (this.commandsList.size() > 0) {
setOpen( true );
return true;
}
}
}
return false;
}
//#ifdef polish.hasPointerEvents
//# protected boolean handlePointerPressed(int x, int y) {
//# // check if one of the command buttons has been pressed:
//# int leftCommandEndX = this.marginLeft + this.paddingLeft + this.singleLeftCommandItem.itemWidth;
//# int rightCommandStartX = this.screen.screenWidth - (this.marginRight + this.paddingRight + this.singleRightCommandItem.itemWidth);
//# //System.out.println("MenuBar: handlePointerPressed( x=" + x + ", y=" + y + " )\nleftCommandEndX = " + leftCommandEndX + ", rightCommandStartXs = " + rightCommandStartX + " screenHeight=" + this.screen.screenHeight);
//# if (y > this.screen.screenHeight) {
//# //System.out.println("menubar clicked");
//# boolean isCloseKeySelected;
//# boolean isOpenKeySelected;
//#
//#if tmp.RightOptions
//# isCloseKeySelected = x > rightCommandStartX;
//# isOpenKeySelected = x < leftCommandEndX;
//#else
//# isCloseKeySelected = x < leftCommandEndX;
//# isOpenKeySelected = x > rightCommandStartX;
//#endif
//# if (this.isOpened) {
//# if ( isCloseKeySelected ) {
//# //System.out.println("selecting command from opened menu");
//# setOpen( false );
//# Command command = (Command) this.commandsList.get( this.commandsContainer.focusedIndex );
//# this.screen.callCommandListener(command);
//# } else if ( isOpenKeySelected ) {
//# //System.out.println("closing menu");
//# setOpen( false );
//# }
//# } else if (this.singleLeftCommand != null
//# && x < leftCommandEndX)
//# {
//# //System.out.println("calling single left command");
//# this.screen.callCommandListener(this.singleLeftCommand);
//# } else if (this.singleRightCommand != null
//# && x > rightCommandStartX)
//# {
//# //System.out.println("calling single right command");
//# this.screen.callCommandListener(this.singleRightCommand);
//# } else if (this.commandsList.size() > 0
//# && x < leftCommandEndX)
//# {
//# //System.out.println("opening menu");
//# setOpen( true );
//# }
//# //System.out.println("nothing was clicked...");
//# return true;
//# // okay, y is above the menu bar, so let the commandContainer process the event:
//# } else if (this.isOpened) {
//# y -= this.commandsContainerY;
//# boolean handled = this.commandsContainer.handlePointerPressed(x, y);
//# return handled;
//# // setOpen( false );
//# // // a menu-item could have been selected:
//# // if (x <= this.commandsContainer.xLeftPos + this.commandsContainerWidth
//# // && y >= this.commandsContainerY )
//# // {
//# //
//# // //boolean handled = this.commandsContainer.handlePointerPressed( x, y );
//# // //System.out.println("handling pointer pressed in open menu at " + x + ", " + y + ": " + handled);
//# // this.commandsContainer.handlePointerPressed( x, y );
//# // int focusedIndex = this.commandsContainer.getFocusedIndex();
//# // Command cmd = (Command) this.commandsList.get( focusedIndex );
//# // this.screen.callCommandListener( cmd );
//# // return true;
//# // }
//# }
//# return false;
//# }
//#endif
public void setStyle(Style style) {
//#if !polish.Bugs.noTranslucencyWithDrawRgb
if (this.overlayBackground == null) {
//#if polish.color.overlay:defined
//# int color = 0;
//#= color = ${polish.color.overlay};
//# if ((color & 0xFF000000) != 0) {
//# this.overlayBackground = new TranslucentSimpleBackground( color );
//# }
//#else
this.overlayBackground = new TranslucentSimpleBackground( 0xAA000000 );
//#endif
}
//#endif
//#if tmp.useInvisibleMenuBar
//# this.background = null;
//# this.border = null;
//#else
super.setStyle(style);
//#ifdef polish.css.menubar-show-image-and-text
//# Boolean showImageAndTextBool = style.getBooleanProperty( 51 );
//# if (showImageAndTextBool != null) {
//# this.showImageAndText = showImageAndTextBool.booleanValue();
//# }
//#endif
//#ifdef polish.css.menubar-options-image
//# String optionsUrl = style.getProperty(50);
//# if (optionsUrl != null) {
//# try {
//# this.optionsImage = StyleSheet.getImage(optionsUrl, this, false);
//# int imageHeight = this.optionsImage.getHeight();
//# if (imageHeight > this.contentHeight) {
//# this.contentHeight = imageHeight;
//# }
//# } catch (IOException e) {
//#debug error
//# System.out.println("Unable to load options-image " + optionsUrl + e );
//# }
//# }
//#endif
//#ifdef polish.css.menubar-select-image
//# String selectUrl = style.getProperty(48);
//# if (selectUrl != null) {
//# try {
//# this.selectImage = StyleSheet.getImage(selectUrl, this, false);
//# int imageHeight = this.selectImage.getHeight();
//# if (imageHeight > this.contentHeight) {
//# this.contentHeight = imageHeight;
//# }
//# } catch (IOException e) {
//#debug error
//# System.out.println("Unable to load select-image " + selectUrl + e );
//# }
//# }
//#endif
//#ifdef polish.css.menubar-cancel-image
//# String cancelUrl = style.getProperty(49);
//# if (cancelUrl != null) {
//# try {
//# this.cancelImage = StyleSheet.getImage(cancelUrl, this, false);
//# int imageHeight = this.cancelImage.getHeight();
//# if (imageHeight > this.contentHeight) {
//# this.contentHeight = imageHeight;
//# }
//# } catch (IOException e) {
//#debug error
//# System.out.println("Unable to load cancel-image " + cancelUrl + e );
//# }
//# }
//#endif
//#endif
}
public boolean animate() {
boolean animated = false;
if (this.background != null) {
animated = animated | this.background.animate();
}
if (this.isOpened) {
animated = animated | this.commandsContainer.animate();
}
if (this.singleLeftCommandItem != null) {
animated = animated | this.singleLeftCommandItem.animate();
}
if (this.singleRightCommandItem != null) {
animated = animated | this.singleRightCommandItem.animate();
}
return animated;
}
/**
* Adds the given command as a subcommand to the specified parent command.
*
* @param parentCommand the parent command
* @param childCommand the child command
* @param commandStyle the style for the command
* @throws IllegalStateException when the parent command has not be added before
*/
public void addSubCommand(Command childCommand, Command parentCommand, Style commandStyle) {
//#if tmp.useInvisibleMenuBar
//# if (parentCommand == this.positiveCommand) {
//# this.positiveCommand = null;
//# }
//#endif
// find parent CommandItem, could be tricky, especially when there are nested commands over several layers
int index = this.commandsList.indexOf( parentCommand );
CommandItem parentCommandItem = null;
if (index != -1) {
// found it:
parentCommandItem = (CommandItem) this.commandsContainer.get( index );
} else if (parentCommand == this.singleLeftCommand ){
parentCommandItem = new CommandItem( parentCommand, this );
this.commandsContainer.add( parentCommandItem );
this.commandsList.add( parentCommand );
this.singleLeftCommand = null;
} else if (parentCommand == this.singleRightCommand ){
parentCommandItem = new CommandItem( parentCommand, this );
this.commandsContainer.add( parentCommandItem );
this.commandsList.add( parentCommand );
this.singleRightCommand = null;
} else {
// search through all commands
for ( int i=0; i < this.commandsContainer.size(); i++ ) {
CommandItem item = (CommandItem) this.commandsContainer.get( i );
parentCommandItem = item.getChild( parentCommand );
if ( parentCommandItem != null ) {
break;
}
}
}
if ( parentCommandItem == null ) {
throw new IllegalStateException();
}
parentCommandItem.addChild( childCommand, commandStyle );
if (this.isOpened) {
this.isInitialised = false;
repaint();
}
}
/**
* Removes all commands from this MenuBar.
* This option is only available when the "menu" fullscreen mode is activated.
*/
public void removeAllCommands() {
this.commands.clear();
this.commandsContainer.clear();
}
//#ifdef polish.MenuBar.additionalMethods:defined
//#include ${polish.MenuBar.additionalMethods}
//#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -