roundrect.lzx
<library>
<class name="roundrect" extends="drawview" defaultplacement="content" width="100" height="100">
<attribute name="inset" value="5
" type="size" setter="setInset(inset);"/>
<attribute name="oninset" value="null
"/>
<attribute name="insetleft" value="${null}" type="size" setter="setInsetLeft(insetleft);"/>
<attribute name="oninsetleft" value="null
"/>
<attribute name="insetright" value="${null}" type="size" setter="setInsetRight(insetright);"/>
<attribute name="oninsetright" value="null
"/>
<attribute name="insettop" value="${null}" type="size" setter="setInsetTop(insettop);"/>
<attribute name="oninsettop" value="null
"/>
<attribute name="insetbottom" value="${null}" type="size" setter="setInsetBottom(insetbottom);"/>
<attribute name="oninsetbottom" value="null
"/>
<method name="setInset" args="v">
this.insetleft = v;
this.insetright = v;
this.insettop = v;
this.insetbottom = v;
if ( this.context ) this.drawStructure();
if ( oninset ) this.oninset.sendEvent();
</method>
<method name="setInsetLeft" args="v">
if ( v ) this.insetleft = v;
if ( this.context ) this.drawStructure();
if ( oninsetleft ) this.oninsetleft.sendEvent();
</method>
<method name="setInsetRight" args="v">
if ( v ) this.insetright = v;
if ( this.context ) this.drawStructure();
if ( oninsetright ) this.oninsetright.sendEvent();
</method>
<method name="setInsetTop" args="v">
if ( v ) this.insettop = v;
if ( this.context ) this.drawStructure();
if ( oninsettop ) this.oninsettop.sendEvent();
</method>
<method name="setInsetBottom" args="v">
if ( v ) this.insetbottom = v;
if ( this.context ) this.drawStructure();
if ( oninsetbottom ) this.oninsetbottom.sendEvent();
</method>
<handler name="onwidth">
if ( this.context ) this.drawStructure();
</handler>
<handler name="onheight">
if ( this.context ) this.drawStructure();
</handler>
<attribute name="borderWidth" value="1
"/>
<attribute name="borderRadius" value="5
"/>
<attribute name="borderColor" type="color" value="black
"/>
<attribute name="borderOpacity" value="1
"/>
<attribute name="backgroundStartColor" type="color" value="$once{null}"/>
<attribute name="backgroundStopColor" type="color" value="$once{null}"/>
<attribute name="backgroundStartOpacity" value="1
"/>
<attribute name="backgroundStopOpacity" value="1
"/>
<attribute name="backgroundGradientOrientation" type="string" value="vertical
"/>
<attribute name="boxShadowX" value="5
"/>
<attribute name="boxShadowY" value="5
"/>
<attribute name="boxShadowColor" type="color" value="$once{null}"/>
<attribute name="boxShadowOpacity" value="0.5
"/>
<handler name="oninit">
if ( this.context ) this.drawStructure();
</handler>
<handler name="oncontext">
this.drawStructure();
this._cache = null;
</handler>
<method name="drawStructure">
if (! this.context) return;
if (! this['_cache']) {
this._cache = {
borderWidth: this.borderWidth
,borderRadius: this.borderRadius
,borderColor: this.borderColor
,borderOpacity: this.borderOpacity
,backgroundStartColor: this.backgroundStartColor
,backgroundStopColor: this.backgroundStopColor
,backgroundGradientOrientation: this.backgroundGradientOrientation
,backgroundStartOpacity: this.backgroundStartOpacity
,backgroundStopOpacity: this.backgroundStopOpacity
,boxShadowColor: this.boxShadowColor
,boxShadowOpacity: this.boxShadowOpacity
,boxShadowX: this.boxShadowX
,boxShadowY: this.boxShadowY
,insetleft: this.insetleft
,insettop: this.insettop
,insetright: this.insetright
,insetbottom: this.insetbottom
,inset: this['inset']
,height: this.height
,width: this.width
};
} else {
var dirty = false;
var c = this._cache;
for (var i in c) {
if (c[i] != this[i]) {
c[i] = this[i];
dirty = true;
break;
}
}
if (dirty == false) return;
}
var w = this.borderWidth;
var r = this.borderRadius;
var x = w/2;
var y = w/2;
var c0 = this.backgroundStartColor;
var c1 = this.backgroundStopColor;
this.clear();
// Adjust the margins and offsets in the content area,
// in case insets changed
if(typeof this.content != 'undefined'){
this.content.reset();
}
// Draw the drop-shadow
if (w != 0 && this.boxShadowColor != null &&
this.boxShadowOpacity != 0) {
var sx = this.boxShadowX;
var sy = this.boxShadowY;
this.beginPath();
this.rect(sx+x, sy+y, this.width-w, this.height-w, r);
this.fillStyle = this.boxShadowColor;
this.globalAlpha = this.boxShadowOpacity;
this.lineWidth = this.borderWidth;
this.fill();
if (c0 == null && c1 == null)
c0 = c1 = 0xffffff;
}
// Create the frame for the border and the background
this.beginPath();
this.rect(x, y, this.width-w, this.height-w, r);
// Draw the background
if (c0 != null || c1 != null) {
var g = this.backgroundGradientOrientation == 'vertical'
? this.createLinearGradient(0, w/2, 0, this.height-w)
: this.createLinearGradient(w/2, 0, this.width-w, 0);
var a0 = this.backgroundStartOpacity;
var a1 = this.backgroundStopOpacity;
if (c0 == null) {
c0 = c1;
a0 = 0;
}
if (c1 == null) {
c1 = c0;
a1 = 0;
}
this.globalAlpha = a0;
g.addColorStop(0, c0);
this.globalAlpha = a1;
g.addColorStop(1, c1);
this.fillStyle = g;
this.fill();
}
this.strokeStyle = this.borderColor;
this.lineWidth = this.borderWidth;
this.globalAlpha = this.borderOpacity;
this.stroke();
</method>
<view name="content" x="0" y="0" width="$once{parent.width}" height="$once{parent.height}">
<method name="reset">
this.setAttribute( "x", parent.insetleft );
this.setAttribute( "y", parent.insettop );
this.setAttribute( "width", parent.width - parent.insetleft -
parent.insetright - 1 );
this.setAttribute( "height", parent.height - parent.insettop -
parent.insetbottom - 1 );
</method>
</view>
</class>
</library>