📄 panel.java
字号:
protected void keyReleased(int keyCode) { FireScreen screen = FireScreen.getScreen(); int gameCode = screen.getGameAction(keyCode); if (container != null && (gameCode==Canvas.LEFT || gameCode==Canvas.RIGHT || gameCode == Canvas.UP || gameCode == Canvas.DOWN )) { if(container.focusableComponents==null) container.focusableComponents = container.generateListOfFocusableComponents(true); Component lastSelected=screen.getSelectedComponent(); // previously selected component (may be outside this panel int index=-1; int focusableComponentsCount = container.focusableComponents.size(); if(lastSelected!=null) index = container.focusableComponents.indexOf(lastSelected); // find the index of the lastSelected if it is inside this panel int vpx = getViewPortPositionX(); int vpy = getViewPortPositionY(); int vpmx = vpx+viewPortWidth; int vpmy = vpy+viewPortHeight; // there are no focusable components or focus is already on the first or last. Check if we must move out of this container. if(((gameCode==Canvas.UP || gameCode==Canvas.LEFT) && (index==0 || focusableComponentsCount==0) && vpy==0) || ((gameCode==Canvas.DOWN || gameCode==Canvas.RIGHT) && (index==focusableComponentsCount-1 || focusableComponentsCount==0) && vpmy==container.height)) { // user wants to move out of this Panel. screen.setSelectedComponent(this); // if parent is null i am a top level component, no need to send the event above me. if(parent!=null) parent.keyReleased(keyCode); else // top level component. Cycle the event only if there are selectable components { if(focusableComponentsCount>0) screen.keyReleased(keyCode); } return; } if((gameCode==Canvas.LEFT || gameCode==Canvas.RIGHT) && (container.layoutManager instanceof GridLayout)==false) // not grid layout {// special case for fast scrolling. if((container.width<=viewPortWidth || (scrollBarPolicy&HORIZONTAL_SCROLLBAR)!=HORIZONTAL_SCROLLBAR)) // no horizontal scrolling) { if(gameCode==Canvas.LEFT) { scrollVertically(-fastVScrollLength); } else if(gameCode==Canvas.RIGHT) { scrollVertically(fastVScrollLength); } } else // horizontal scrolling { if(gameCode==Canvas.LEFT) { scrollHorizontally(-normalHScrollLength); } else if(gameCode==Canvas.RIGHT) { scrollHorizontally(normalHScrollLength); } } } else if(focusableComponentsCount>0) { // first locate the previously selected component (if any) if(index==-1) // selected component does not belong to this container. Deselect it. { screen.setSelectedComponent(null); lastSelected=null; } if(lastSelected!=null) { // check if the component is inside the viewport. int []coords = getCoordsOfComponentInContainer(lastSelected,container); coords[0] += lastSelected.width/2; // translate coords to center of component coords[1] += lastSelected.height/2; if((coords[0]>= vpx && coords[0]<vpmx) && (coords[1]>= vpy && coords[1]<vpmy)) {// component is inside the viewport, send the event to the container to select the next component container.keyReleased(keyCode); // check if newly selected component is outside the viewport Component newSelected = screen.getSelectedComponent(); if(newSelected!=null && newSelected!=lastSelected) { coords = getCoordsOfComponentInContainer(newSelected,container); if(coords[0]<vpx || coords[0]+newSelected.width>vpmx || coords[1]<vpy || coords[1]+newSelected.height>vpmy) {// component is partially or completelly outside the viewport, scroll is good. scrollToSelectedComponent(fastHScrollLength,fastVScrollLength); } } else scroll(gameCode, (gameCode==Canvas.LEFT|| gameCode==Canvas.RIGHT)?normalHScrollLength:normalVScrollLength); } else {// component is not inside the viewport // if component is not in the direction of the key press (i.e. it is bellow the bottom of the viewport and the // key press is Canvas.UP) We should deselect it. if((gameCode==Canvas.UP && coords[1]>vpmy) || (gameCode==Canvas.DOWN && coords[1]<vpy) || (gameCode==Canvas.LEFT && coords[0]>vpmx) || (gameCode==Canvas.RIGHT && coords[0]<vpx) ) { // deselect the component screen.setSelectedComponent(null); // send the event again. screen.keyReleased(keyCode); return; } // alternatevly, the movement is towards the component so scroll to the component. // bring the component inside the viewport at once scrollToSelectedComponent(fastHScrollLength,fastVScrollLength); } } if(lastSelected==null) // find a component inside the viewport and send the event to it. { int newIndex; if(gameCode==Canvas.DOWN || gameCode==Canvas.RIGHT) newIndex = getFirstFocusableComponentInsideViewport(true); else // UP or LEFT newIndex = getFirstFocusableComponentInsideViewport(false); if(newIndex==-1) // nothing found just scroll scroll(gameCode, (gameCode==Canvas.LEFT|| gameCode==Canvas.RIGHT)?normalHScrollLength:normalVScrollLength); else { Component newSelected = (Component)container.focusableComponents.elementAt(newIndex); screen.setSelectedComponent(newSelected); newSelected.keyReleased(keyCode); } } } else scroll(gameCode, (gameCode==Canvas.LEFT|| gameCode==Canvas.RIGHT)?normalHScrollLength:normalVScrollLength); // nothing to select. only scroll } if(keyListener!=null) keyListener.keyReleased(keyCode,this); } /** * Brings the top left corner and as much of the selected component as possible inside the viewport of this panel. * @param maxhscroll * @param maxvscroll */ public void scrollToSelectedComponent(int maxhscroll,int maxvscroll) { Component cmp = FireScreen.getScreen().getSelectedComponent(); if(cmp==null) return; if(cmp.selected==false) cmp.setSelected(true); int coords[] = getCoordsOfComponentInContainer(cmp,container); int vpx = getViewPortPositionX(); int vpy = getViewPortPositionY(); int diffX = coords[0]-vpx; int diffY = coords[1]-vpy; if(vpx+coords[0]+cmp.width<=viewPortWidth) // no need for horizontal scrolling diffX=0; if(diffY<0 && cmp.height<((25*viewPortHeight)/100)) diffY-= ((20*viewPortHeight)/100); if(maxvscroll>-1) { if(diffY>0 && diffY>maxvscroll) diffY = maxvscroll; else if(diffY<0 && -diffY>maxvscroll) diffY = -maxvscroll; } if(maxhscroll>-1) { if(diffX>0 && diffX>maxhscroll) diffX = maxhscroll; else if(diffX<0 && -diffX>maxhscroll) diffX = -maxhscroll; } setViewPortPosition(vpx+diffX,vpy+diffY); FireScreen screen = FireScreen.getScreen(); if(this.animation==null && screen.isAnimationsEnabled()) { ScrollAnimation anim = new ScrollAnimation(this,vpx,vpy,vpx+diffX,vpy+diffY); screen.registerAnimation(anim); } } private void scroll(int gameCode, int pixels) { int vpx = getViewPortPositionX(); if(gameCode==Canvas.LEFT) { if(container.width>viewPortWidth && vpx>0) scrollHorizontally(-pixels); else scrollVertically(-pixels); } else if(gameCode==Canvas.RIGHT) { if(container.width>viewPortWidth && (vpx+viewPortWidth)<container.width) scrollHorizontally(pixels); else scrollVertically(pixels); } else if(gameCode==Canvas.UP) scrollVertically(-pixels); else if(gameCode==Canvas.DOWN) scrollVertically(pixels); FireScreen screen = FireScreen.getScreen(); Component lastSelected = screen.getSelectedComponent(); if(lastSelected!=null && lastSelected.selected==false) lastSelected.setSelected(true);// set the component as selected again. } /** * Finds a selectable component inside the container that is visible throuht the viewport. * It will return either the first component (top most) or the last (bottom most) inside the * viewport, depending on the parameter. * @param startFromTop * @return */ private int getFirstFocusableComponentInsideViewport(boolean startFromTop) { int step,index; if(startFromTop) { index=0; step=+1; } else { index = container.focusableComponents.size()-1; step=-1; } int vpx = getViewPortPositionX(); int vpy = getViewPortPositionY(); int vpmx = vpx+viewPortWidth; int vpmy = vpy + viewPortHeight; int coords[]; for(;index>-1 && index<container.focusableComponents.size();index+=step) { Component cmp = (Component)container.focusableComponents.elementAt(index); coords = getCoordsOfComponentInContainer(cmp,container); coords[0] += cmp.width/2; // translate coords to center of component coords[1] += cmp.height/2; if((coords[0]>= vpx && coords[0]<vpmx) && (coords[1]>= vpy && coords[1]<vpmy)) { return index; } } // no selectable component inside viewport. return -1; } protected void keyRepeated(int keyCode) { keyReleased(keyCode); } public Vector generateListOfFocusableComponents(boolean recursive) { if(container!=null) return container.generateListOfFocusableComponents(recursive); else return new Vector(); } public int[] getMinSize() { if (parent == null) { FireScreen screen = FireScreen.getScreen(); return new int[]{screen.getWidth(), screen.getHeight()}; } return super.getMinSize(); } public int getScrollBarPolicy() { return scrollBarPolicy; } /** * A panel can be set to remove itself from the FireScreen when it receives a pointer event that is outside it. * The firescreen will only send pointer and key events to the top most (relatively to ZINDEX) focusable component or container. * If a for example a popup window (a Panel) is open which is smaller than the screen size, * then it is common behaivior to close the popup when the user taps outside the window.<br/> * * This is the default behaivior of the Panel. * * @return true if this Panel is set to close when receiving pointer events outside its dimensions (default is true) */ public boolean isCloseOnOutofBoundsPointerEvents() { return closeOnOutofBoundsPointerEvents; } /** * @see #isCloseOnOutofBoundsPointerEvents() * @param closeOnOutofBoundsPointerEvents */ public void setCloseOnOutofBoundsPointerEvents(boolean closeOnOutofBoundsPointerEvents) { this.closeOnOutofBoundsPointerEvents = closeOnOutofBoundsPointerEvents; } /** * If true, the user can also scroll by draging the container inside the panel. Default is false. * @return true is dragscroll is enabled (default is false) */ public boolean isDragScroll() { return dragScroll; } /** * @see #setDragScroll(boolean) * @param dragScroll */ public void setDragScroll(boolean dragScroll) { this.dragScroll = dragScroll; } /** * If showBackground is enabled the Panel will draw a theme specific background behind the Container. * The Container or parts of it must be ofcource transparent for the background to be visible. * * @return true if this Panel is set to show a background. (default is false) */ public boolean isShowBackground() { return showBackground; } public void setShowBackground(boolean showBackground) { this.showBackground = showBackground; } /** * If this flag is set then the Panel will display decorations. * @return true is the decorations are enabled. (default is true) */ public boolean isShowDecorations() { return showDecorations; } /** * @see #isShowDecorations() * @param showDecorations */ public void setShowDecorations(boolean showDecorations) { this.showDecorations = showDecorations; } public Image getBackgroundTexture() { return backgroundTexture; } public void setBackgroundTexture(Image backgroundTexture) { this.backgroundTexture = backgroundTexture; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -