📄 container.java
字号:
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#initItem( int, int )
*/
protected void initContent(int firstLineWidth, int lineWidth) {
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 712, "Container: intialising content for " + this + ": autofocus=", this.autoFocusEnabled);
int myContentWidth = 0;
int myContentHeight = 0;
try {
Item[] myItems = (Item[]) this.itemsList.toArray( new Item[ this.itemsList.size() ]);
this.items = myItems;
if (this.autoFocusEnabled && this.autoFocusIndex >= myItems.length ) {
this.autoFocusIndex = 0;
}
//#ifdef tmp.supportViewType
//# if (this.view != null) {
//# boolean requireScrolling = this.isScrollRequired;
//# if (this.autoFocusEnabled) {
//#debug
//# System.out.println("Container/View: autofocusing element " + this.autoFocusIndex);
//# this.autoFocusEnabled = false;
//# requireScrolling = true;
//# if (this.autoFocusIndex < myItems.length && this.autoFocusIndex >= 0 ) {
//# Item item = myItems [ this.autoFocusIndex ];
//# if (item.appearanceMode != Item.PLAIN) {
//# // make sure that the item has applied it's own style first:
//# item.getItemHeight( firstLineWidth, lineWidth );
//# // now focus the item:
//# focus( this.autoFocusIndex, item, 0 );
//# this.view.focusedIndex = this.autoFocusIndex;
//# this.view.focusedItem = this.focusedItem;
//# }
//# }
//# }
//# this.view.initContent(this, firstLineWidth, lineWidth);
//# this.contentWidth = this.view.contentWidth;
//# this.contentHeight = this.view.contentHeight;
//# this.appearanceMode = this.view.appearanceMode;
//# if (requireScrolling && this.enableScrolling && this.focusedItem != null) {
//#debug
//# System.out.println("initContent(): scrolling autofocused or scroll-required item for view");
//# Item item = this.focusedItem;
//# scroll( true, item.xLeftPos, item.yTopPos, item.itemWidth, item.itemHeight );
//# }
//#
//# return;
//# }
//#endif
boolean isLayoutShrink = (this.layout & LAYOUT_SHRINK) == LAYOUT_SHRINK;
boolean hasFocusableItem = false;
for (int i = 0; i < myItems.length; i++) {
Item item = myItems[i];
//System.out.println("initalising " + item.getClass().getName() + ":" + i);
int width = item.getItemWidth( lineWidth, lineWidth );
int height = item.itemHeight; // no need to call getItemHeight() since the item is now initialised...
// now the item should have a style, so it can be safely focused
// without loosing the style information:
if (item.appearanceMode != PLAIN) {
hasFocusableItem = true;
}
if (this.autoFocusEnabled && (i >= this.autoFocusIndex ) && (item.appearanceMode != Item.PLAIN)) {
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 770, "Container: autofocusing element ", i);
this.autoFocusEnabled = false;
focus( i, item, 0 );
height = item.getItemHeight(lineWidth, lineWidth);
if (!isLayoutShrink) {
width = item.itemWidth; // no need to call getItemWidth() since the item is now initialised...
} else {
width = 0;
}
if (this.enableScrolling) {
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 781, "initContent(): scrolling autofocused item");
scroll( true, 0, myContentHeight, width, height );
}
} else if (i == this.focusedIndex) {
if (isLayoutShrink) {
width = 0;
}
if (this.isScrollRequired) {
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 790, "initContent(): scroll is required.");
scroll( true, 0, myContentHeight, width, height );
this.isScrollRequired = false;
}
}
if (width > myContentWidth) {
myContentWidth = width;
}
item.yTopPos = myContentHeight;
item.yBottomPos = myContentHeight + height;
myContentHeight += height + this.paddingVertical;
//System.out.println("item.yTopPos=" + item.yTopPos);
}
if (!hasFocusableItem) {
this.appearanceMode = PLAIN;
} else {
this.appearanceMode = INTERACTIVE;
if (isLayoutShrink && this.focusedItem != null) {
Item item = this.focusedItem;
//System.out.println("container has shrinking layout and contains focuse item " + item);
item.isInitialised = false;
boolean doExpand = item.isLayoutExpand;
int width;
if (doExpand) {
item.isLayoutExpand = false;
width = item.getItemWidth( lineWidth, lineWidth );
item.isInitialised = false;
item.isLayoutExpand = true;
} else {
width = item.itemWidth;
}
if (width > myContentWidth) {
myContentWidth = width;
}
if ( this.minimumWidth != 0 && myContentWidth < this.minimumWidth ) {
myContentWidth = this.minimumWidth;
}
//myContentHeight += item.getItemHeight( lineWidth, lineWidth );
}
}
} catch (ArrayIndexOutOfBoundsException e) {
//#debug error
de.enough.polish.util.Debug.debug("error", "de.enough.polish.ui.Container", 832, "Unable to init container ", e );
}
this.contentHeight = myContentHeight;
this.contentWidth = myContentWidth;
}
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#paintItem(int, int, javax.microedition.lcdui.Graphics)
*/
protected void paintContent(int x, int y, int leftBorder, int rightBorder, Graphics g) {
// paints all items,
// the layout will be done according to this containers'
// layout or according to the items layout, when specified.
// adjust vertical start for scrolling:
//#if polish.debug.debug
if (this.enableScrolling) {
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 850, "Container: drawing " + getClass().getName() + " with yOffset=", this.yOffset );
}
//#endif
boolean setClipping = (this.yOffset != 0 && this.marginTop != 0);
int clipX = 0;
int clipY = 0;
int clipWidth = 0;
int clipHeight = 0;
if (setClipping) {
clipX = g.getClipX();
clipY = g.getClipY();
clipWidth = g.getClipWidth();
clipHeight = g.getClipHeight();
g.clipRect(clipX, y - this.paddingTop, clipWidth, clipHeight - ((y - this.paddingTop) - clipY) );
}
x = leftBorder;
y += this.yOffset;
//#ifdef tmp.supportViewType
//# if (this.view != null) {
//# this.view.paintContent(x, y, leftBorder, rightBorder, g);
//# } else {
//#endif
Item[] myItems;
//synchronized (this.itemsList) {
myItems = this.items;
//}
int focusedX = x;
int focusedY = 0;
int focusedRightBorder = rightBorder;
if (!(this.isLayoutCenter || this.isLayoutRight)) {
// adjust the right border:
rightBorder = leftBorder + this.contentWidth;
}
Item focItem = this.focusedItem;
int focIndex = this.focusedIndex;
for (int i = 0; i < myItems.length; i++) {
Item item = myItems[i];
// currently the NEWLINE_AFTER and NEWLINE_BEFORE layouts will be ignored,
// since after every item a line break will be done.
if (i == focIndex) {
focusedY = y;
item.getItemHeight( rightBorder - x, rightBorder - leftBorder );
} else {
// the currently focused item is painted last
item.paint(x, y, leftBorder, rightBorder, g);
}
y += item.itemHeight + this.paddingVertical;
}
// paint the currently focused item:
if (focItem != null) {
//System.out.println("Painting focusedItem " + this.focusedItem + " with width=" + this.focusedItem.itemWidth + " and with increased colwidth of " + (focusedRightBorder - focusedX) );
focItem.paint(focusedX, focusedY, focusedX, focusedRightBorder, g);
}
//#ifdef tmp.supportViewType
//# }
//#endif
Item item = this.focusedItem;
if (item != null) {
if (item.internalX != -9999) {
this.internalX = item.contentX - this.contentX + item.internalX;
this.internalWidth = item.internalWidth;
this.internalY = item.contentY - this.contentY + item.internalY;
this.internalHeight = item.internalHeight;
} else {
this.internalX = item.xLeftPos - this.contentX;
this.internalWidth = item.itemWidth;
this.internalY = item.yTopPos + this.yOffset; //(item.yTopPos - this.yOffset) - this.contentY;
this.internalHeight = item.itemHeight;
}
// outcommented by rob - 2006-07-13 - now positions are determined in the initContent() method already
// if (this.isFirstPaint) {
// this.isFirstPaint = false;
// if (this.enableScrolling) {
// if ( this.contentY + this.internalY + this.internalHeight > this.yBottom ) {
// //#debug
// System.out.println("first paint, now scrolling...");
// scroll( true, this.contentX, this.contentY + this.internalY, this.contentWidth, this.internalHeight + 3 ); // + 3 gives room for a focused border etc
// }
// }
// }
}
if (setClipping) {
g.setClip(clipX, clipY, clipWidth, clipHeight);
}
}
//#ifdef polish.useDynamicStyles
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#getCssSelector()
*/
protected String createCssSelector() {
return "container";
}
//#endif
/* (non-Javadoc)
* @see de.enough.polish.ui.Item#handleKeyPressed(int, int)
*/
protected boolean handleKeyPressed(int keyCode, int gameAction) {
if (this.itemsList.size() == 0) {
return false;
}
if (this.focusedItem != null) {
Item item = this.focusedItem;
if ( item.handleKeyPressed(keyCode, gameAction) ) {
if (this.enableScrolling && item.internalX != -9999) {
adjustScrolling(item);
}
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 963, "Container(" + this + "): handleKeyPressed consumed by item " + item.getClass().getName() + "/", item );
return true;
}
}
// now allow a navigation within the container:
//#ifdef tmp.supportViewType
//# if (this.view != null) {
//# boolean handled = this.view.handleKeyPressed(keyCode, gameAction);
//# if (handled) {
//# return true;
//# }
//# // Item next = this.view.getNextItem(keyCode, gameAction);
//# // if (next != null) {
//# // focus( this.view.focusedIndex, next, gameAction );
//# // return true;
//# // } else
//# if (this.enableScrolling) {
//# if (gameAction == Canvas.UP && this.targetYOffset < 0 ) {
//# // scroll the container view upwards without changing the focused item:
//#if polish.Container.ScrollDelta:defined
//#= this.targetYOffset += ${polish.Container.ScrollDelta};
//#else
//# this.targetYOffset += 30;
//#endif
//# if (this.targetYOffset > 0 ) {
//# this.targetYOffset = 0;
//# }
//#if polish.scroll-mode
//# if (!this.scrollSmooth) {
//# this.yOffset = this.targetYOffset;
//# }
//#endif
//# return true;
//# }
//# if (gameAction == Canvas.DOWN
//# && (this.itemHeight + this.targetYOffset > (this.yBottom - this.yTop)) )
//# {
//# // scroll the container view downwards without changing the focused item:
//#if polish.Container.ScrollDelta:defined
//#= this.targetYOffset -= ${polish.Container.ScrollDelta};
//#else
//# this.targetYOffset -= 30;
//#endif
//#if polish.scroll-mode
//# if (!this.scrollSmooth) {
//# this.yOffset = this.targetYOffset;
//# }
//#endif
//# return true;
//# }
//# }
//# return false;
//# }
//#endif
boolean processed = false;
int availableHeight = this.yBottom - this.yTop;
int offset;
//#if polish.scroll-mode
//# if (this.scrollSmooth) {
//#endif
offset = this.targetYOffset;
//#if polish.scroll-mode
//# } else {
//# offset = this.yOffset;
//# }
//#endif
if ( (gameAction == Canvas.RIGHT && keyCode != Canvas.KEY_NUM6)
|| (gameAction == Canvas.DOWN && keyCode != Canvas.KEY_NUM8)) {
if (this.focusedItem != null
&& this.enableScrolling
&& offset + this.focusedItem.yBottomPos > availableHeight)
{
if (gameAction == Canvas.RIGHT) {
return false;
}
// keep the focus do scroll downwards:
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 1041, "Container(" + this + "): scrolling down: keeping focus, focusedIndex=", this.focusedIndex );
} else {
processed = shiftFocus( true, 0 );
}
//#debug
de.enough.polish.util.Debug.debug("debug", "de.enough.polish.ui.Container", 1046, "Container(" + this + "): forward shift by one item succeded: " + processed + ", focusedIndex=", this.focusedIndex );
if ((!processed) && this.enableScrolling
&& ( (this.focusedItem != null && offset + this.focusedItem.yBottomPos > availableHeight)
|| offset + this.itemHeight > availableHeight)) {
// scroll downwards:
//#if polish.Container.ScrollDelta:defined
//#= offset -= ${polish.Container.ScrollDelta};
//#else
offset -= 30;
//#endif
//#debug
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -