Polish的绘图机制一 -- item的 paint 方法


老祖宗:paint
public void paint( int x, int y, int leftBorder, int rightBorder, Graphics g ) {
  //#if tmp.invisible
   if (this.isInvisible) {
    return;
   }
  //#endif
   
  //#debug ovidiu
  Benchmark.startSmartTimer("0");
   
  // initialise this item if necessary: //绘制之前进行必要的初始化动作
  if (!this.isInitialized) {
   if (this.availableWidth == 0) {
    setAvailableDimensions(leftBorder, rightBorder);
   }
   
   int previousItemWidth = this.itemWidth;
   int previousItemHeight = this.itemHeight;
   
   init(this.availableWidth, this.availableWidth, this.availableHeight);
   
   if ((previousItemWidth != this.itemWidth || previousItemHeight != this.itemHeight) && this.parent != null)
   {
    this.parent.requestInit();
   }
  }
  //横纵坐标调整 不知道为什么在css语法里面还找不到?
  //#if polish.css.x-adjust 
   if (this.xAdjustment != null) {
    int value = this.xAdjustment.getValue(this.itemWidth);
    x += value;
    leftBorder += value;
    rightBorder += value;
   }
  //#endif
  //#if polish.css.y-adjust
   if (this.yAdjustment != null) {
    y += this.yAdjustment.getValue(this.itemHeight);
   }
  //#endif
  int origX = x;
  int origY = y;
   
                            //css滤镜效果
  //#if polish.css.filter && polish.midp2
   if (this.isFiltersActive && this.filters != null && !this.filterPaintNormally) {
    RgbImage rgbImage = this.filterRgbImage;
    if ( rgbImage == null) {
     this.filterPaintNormally = true;
     int[] rgbData = UiAccess.getRgbData( this );
     rgbImage = new RgbImage( rgbData, this.itemWidth);
     this.filterRgbImage = rgbImage;
     this.filterPaintNormally = false;
    }
    //System.out.println("painting RGB data for " + this  + ", pixel=" + Integer.toHexString( rgbData[ rgbData.length / 2 ]));  

    for (int i=0; i<this.filters.length; i++) {
     RgbFilter filter = this.filters[i];
     rgbImage = filter.process(rgbImage);
    }    
    
    int width = rgbImage.getWidth();
    int height = rgbImage.getHeight();
    int[] rgbData = rgbImage.getRgbData();
    if (this.isLayoutRight) {
     x = rightBorder - width;
    } else if (this.isLayoutCenter) {
     x =  leftBorder + ((rightBorder - leftBorder)/2) - (width/2);
    }
    DrawUtil.drawRgb(rgbData, x, y, width, height, true, g );
    
    //#mdebug ovidiu
    Benchmark.pauseSmartTimer("0");
    Benchmark.incrementSmartTimer("1");
    Benchmark.check();
    //#enddebug
    //#if polish.Item.ShowCommandsOnHold
     if (this.isShowCommands) {
      paintCommands( origX, origY, g );
     }
    //#endif
    return;
   }
  //#endif
                            //Css的透明度属性
  //#if polish.css.opacity && polish.midp2
   if (this.opacity != 255 && !this.opacityPaintNormally) {
    if (this.opacity == 0) {
     return;
    }
    int[] rgbData = this.opacityRgbData;
    if ( rgbData == null || this.opacity != this.opacityAtGeneration ) {
     this.opacityPaintNormally = true;
     rgbData = UiAccess.getRgbData( this, this.opacity );
     this.opacityRgbData = rgbData;
     this.opacityPaintNormally = false;
     this.opacityAtGeneration = this.opacity;
    }
    //System.out.println("painting RGB data for " + this  + ", pixel=" + Integer.toHexString( rgbData[ rgbData.length / 2 ]));
    DrawUtil.drawRgb(rgbData, x, y, this.itemWidth, this.itemHeight, true, g );
    //#if polish.Item.ShowCommandsOnHold
     if (this.isShowCommands) {
      paintCommands( origX, origY, g );
     }
    //#endif
    return;
   }
  //#endif
 
  
  //boolean isLayoutShrink = (this.layout & LAYOUT_SHRINK) == LAYOUT_SHRINK;

  // paint background and border when the label should be included in this:
  //#if polish.css.include-label
   if (this.includeLabel) {
    int width = this.itemWidth - this.marginLeft - this.marginRight;
    int height = this.itemHeight - this.marginTop - this.marginBottom;
    int bX = x + this.marginLeft;
    int bY = y + this.marginTop + this.backgroundYOffset;
    paintBackgroundAndBorder( bX, bY, width, height, g );
   }
  //#endif
  //#if polish.css.complete-background || polish.css.complete-border
   int width = this.itemWidth - this.marginLeft - this.marginRight + (this.completeBackgroundPadding << 1);
   int height = this.itemHeight - this.marginTop - this.marginBottom + (this.completeBackgroundPadding << 1);
   int bX = x + this.marginLeft - this.completeBackgroundPadding;
   int bY = y + this.marginTop + this.backgroundYOffset - this.completeBackgroundPadding;
   //#if polish.css.complete-background
    if (this.completeBackground != null) {
     this.completeBackground.paint(bX, bY, width, height, g);
    }
   //#endif
   //#if polish.css.complete-border
    if (this.completeBorder!= null) {
     this.completeBorder.paint(bX, bY, width, height, g);
    }
   //#endif
  //#endif
  //绘制label
                            // paint label:
  if (this.label != null) {
   if (this.useSingleRow) {
    this.label.paint( x, y, leftBorder, rightBorder - (this.contentWidth + this.paddingHorizontal), g );
    leftBorder += this.label.itemWidth;
   } else {
    this.label.paint( x, y, leftBorder, rightBorder, g );
    y += this.label.itemHeight;
   }
  }
  
  leftBorder += (this.marginLeft + getBorderWidthLeft() + this.paddingLeft);
  //#ifdef polish.css.before
   leftBorder += this.beforeWidth;
  //#endif
  //System.out.println( this.style.name + ":  increasing leftBorder by " + (this.marginLeft + this.borderWidth + this.paddingLeft));
  rightBorder -= (this.marginRight + getBorderWidthRight() + this.paddingRight);
  //#ifdef polish.css.after
   rightBorder -= this.afterWidth;
  //#endif
//有些代码虽然被注释掉了 但是我觉得有必要研究一下
  //System.out.println( this.style.name + ":  decreasing rightBorder by " + (this.marginRight + this.borderWidth + this.paddingRight));
//  if ( this.isLayoutCenter  && availWidth > this.itemWidth) {
//   int difference = (availWidth - this.itemWidth) >> 1;
//   System.out.println("increasing x from " + x + " to " + (x + difference) + ", availableWidth=" + this.availableWidth + ", availWidth=" + availWidth + ", itemWidth=" + this.itemWidth);
//   x += difference;  //移动到居中位置
//   if (!this.isLayoutExpand) { //控件应该占据剩下的所有空间
//    leftBorder += difference;
//    rightBorder -= difference;
//    //System.out.println("item " + this + ": (center) shrinking left border to " + leftBorder + ", right border to " + rightBorder);
//   }
//  } else if ( this.isLayoutRight && availWidth > this.itemWidth) {
//   // adjust the x-position so that the item is painted up to
//   // the right border (when it starts at x):
//   x += availWidth - this.itemWidth;
//   if (!this.isLayoutExpand) {
//    leftBorder += availWidth - this.itemWidth;
//    //System.out.println("item " + this + ": (right) shrinking left border to " + leftBorder);
//   }
//  } else if (isLayoutShrink && availWidth > this.itemWidth) {
//   rightBorder -= availWidth - this.itemWidth; //使用最小的空间
//   //System.out.println("item " + this + ": (left) shrinking right border to " + rightBorder);
//  }
  
  
  x += this.marginLeft;
  y += this.marginTop;
  // paint background and border: //为什么这里又要画一次背景和边框呢?
  //#if polish.css.include-label
   if (!this.includeLabel) {
  //#endif
    int backgroundX = x;
    if (this.label != null) {
     backgroundX += this.contentX - this.paddingLeft - this.marginLeft - getBorderWidthLeft();
     if (this.isLayoutCenter) {
      //TODO: what happens when having an animated background width?
                        rightBorder = backgroundX + this.backgroundWidth - this.paddingRight - this.marginRight - getBorderWidthRight();
                        leftBorder = x + this.contentX;
     }
    }
    paintBackgroundAndBorder(backgroundX, y, this.backgroundWidth, this.backgroundHeight, g);
  //#if polish.css.include-label
   }
  //#endif

  //#if polish.css.content-x-adjust
   if (this.contentXAdjustment != null) {
    x += this.contentXAdjustment.getValue(this.contentWidth);
   }
  //#endif
  //#if polish.css.content-y-adjust
   if (this.contentYAdjustment != null) {
    y += this.contentYAdjustment.getValue(this.contentHeight);
   }
  //#endif
  x += this.contentX - this.marginLeft; //getBorderWidthLeft() + this.paddingLeft;
  y += this.contentY - this.marginTop; //getBorderWidthTop() + this.paddingTop;
  if (this.label != null && !this.useSingleRow) {
   y -= this.label.itemHeight;
  }
  int originalContentY = y;
  //绘画前置函数
  // paint before element:
  //#if polish.css.before || polish.css.after || polish.css.min-height  || polish.css.max-height
   boolean isVerticalCenter = (this.layout & LAYOUT_VCENTER) == LAYOUT_VCENTER;
   boolean isTop = !isVerticalCenter && (this.layout & LAYOUT_TOP) == LAYOUT_TOP;
   boolean isBottom = !isVerticalCenter && (this.layout & LAYOUT_BOTTOM) == LAYOUT_BOTTOM;
  //#endif
  //#ifdef polish.css.before
   if (this.beforeImage != null) {
    int beforeX = x;
    int beforeY = y;
    int yAdjust = this.beforeHeight - this.contentHeight;
    if ( this.beforeHeight < this.contentHeight) {
     if (isTop) {
      //beforeY -= yAdjust;
     } else if (isBottom) {
      beforeY += yAdjust;
     } else {
      beforeY -= (yAdjust >> 1);
     }
    } else {
     if (isTop) {
      // keep contY
     } else if (isBottom) {
      y += yAdjust;
     } else {
      y += (yAdjust >> 1);
     }
     //contY += (this.beforeHeight - this.contentHeight) / 2;
    }
    
    if(this.isLayoutRight)
    {
     beforeX = rightBorder - (this.contentWidth + this.beforeWidth);
    }
    
    g.drawImage(this.beforeImage, beforeX, beforeY, Graphics.TOP | Graphics.LEFT );
    x += this.beforeWidth;
   }
  //#endif
  //绘画后置函数
  // paint after element:
  //#ifdef polish.css.after
   if (this.afterImage != null) {
    int afterY = originalContentY;
    int yAdjust = this.afterHeight - this.contentHeight;
    if ( this.afterHeight < this.contentHeight) {
     if (isTop) {
      afterY -= yAdjust;
     } else if (isBottom) {
      afterY += yAdjust;
     } else {
      afterY -= (yAdjust >> 1);
     }
     //afterY += (this.contentHeight - this.afterHeight) / 2;
    } else {
     //#ifdef polish.css.before
     if (this.afterHeight > this.beforeHeight) {
     //#endif
      if (isTop) {
       // keep contY
      } else if (isBottom) {
       y = originalContentY + yAdjust;
      } else {
       y = originalContentY + (yAdjust >> 1);
      }
      //contY = originalContentY + (this.afterHeight - this.contentHeight) / 2;
     //#ifdef polish.css.before
     }
     //#endif
    }
    g.drawImage(this.afterImage, rightBorder, afterY, Graphics.TOP | Graphics.LEFT );
   }
  //#endif
  //绘画内容
  //#if polish.css.content-visible
   if (!this.isContentVisible) {
    this.contentWidth = 0;
    this.contentHeight = 0;
   } else {
  //#endif
   // paint content:
   //#ifdef polish.css.view-type
    if (this.view != null) {
     this.view.paintContent( this, x, y, leftBorder, rightBorder, g);
    } else {
   //#endif
     paintContent( x, y, leftBorder, rightBorder, g );
   //#ifdef polish.css.view-type
    }
   //#endif
  //#if polish.css.content-visible
   }
  //#endif
   
//  g.setColor(0xffFF00);
//  g.drawRect( getAbsoluteX() + 1, getAbsoluteY() + 1, this.itemWidth - 2, this.itemHeight - 2);
//  if (this.parent != null) {
//   g.setColor(0x00ff00);
//   g.drawRect( this.parent.getAbsoluteX()  + this.parent.contentX + this.relativeX, this.parent.getAbsoluteY() + this.parent.contentY + this.relativeY, this.itemWidth, this.itemHeight);
//  }
  //#if polish.Item.ShowCommandsOnHold
   if (this.isShowCommands) {
    paintCommands( origX, origY, g );
   }
  //#endif
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值