baseslider.lzx
<library>
<include href="base/basevaluecomponent.lzx"/>
<include href="lz/button.lzx"/>
<include href="utils/states/dragstate.lzx"/>
<class name="baseslider" extends="basevaluecomponent" width="200">
<attribute name="trackheight" value="8
" type="number"/>
<attribute name="thumbwidth" value="10
" type="number"/>
<attribute name="thumbheight" value="18
" type="number"/>
<attribute name="minvalue" value="0
" type="number"/>
<attribute name="maxvalue" value="100
" type="number"/>
<attribute name="precision" value="0
" type="number"/>
<attribute name="showfill" value="true
"/>
<attribute name="showvalue" value="true
" type="boolean"/>
<attribute name="showrange" value="true
" type="boolean"/>
<attribute name="keystep" value="2
" type="number"/>
<attribute name="bordersize" value="1
" type="number"/>
<handler name="oninit">
//have to adjust thumb once at start because
//neither setValue nor onvalue is getting triggered
//when value is set.
_adjustThumb();
</handler>
<method name="getMinValue">
return minvalue;
</method>
<method name="getMaxValue">
return maxvalue;
</method>
<method name="setMinValue" args="v">
if (this.minvalue == v) return;
this.minvalue = v;
if (onminvalue) onminvalue.sendEvent();
//adjust value
if (this.minvalue > this.value) this.setAttribute("value",v);
_adjustThumb();
</method>
<method name="setMaxValue" args="v">
if (this.maxvalue == v) return;
this.maxvalue = v;
if (onmaxvalue) onmaxvalue.sendEvent();
//adjust value
if (this.value > v) this.setAttribute("value",v);
_adjustThumb();
</method>
<method name="setValue" args="v">
if (this.value == v) return;
// ensure in bounds
v = Math.max(v,this.minvalue);
v = Math.min(v,this.maxvalue);
this.value = v;
if (onvalue) onvalue.sendEvent();
if (!isinited) return;
_adjustThumb();
</method>
<method name="setPercentage" args="p">
var diff = this.minvalue-this.maxvalue;
setValue( diff*p + this.minvalue );
</method>
<method name="getPercentage">
return (this.value-this.minvalue) / (this.maxvalue-this.minvalue);
</method>
<method name="_adjustThumb">
var perc = getPercentage();
var x = Math.round( (this.width-track.thumb.width)*perc );
track.thumb.setAttribute('x', x);
</method>
<handler name="onkeydown" args="k">
//left arrow
if (k==37) this.track.thumb.setAttribute('x', this.track.thumb.x-keystep);
else
//right arrow
if (k==39) this.track.thumb.setAttribute('x', this.track.thumb.x+keystep);
</handler>
<method name="getFocusRect">
var fx = this.getAttributeRelative('x',canvas);
var fy = this.getAttributeRelative('y',canvas) - 4;
var fw = this.getAttributeRelative('width',canvas) + 2;
var fh = this.getAttributeRelative('height',canvas) + 6;
return [fx,fy,fw,fh];
</method>
</class>
<class name="baseslidertrack" width="100%" height="${classroot.trackheight}" bgcolor="0x333333">
<handler name="oninit">
classroot.track = this;
</handler>
</class>
<class name="basesliderthumb" extends="button" width="${parent.classroot.thumbwidth}" height="${parent.classroot.thumbheight}" onmousedown="thedragstate.setAttribute('applied', true);" onmouseup="thedragstate.setAttribute('applied', false);" focusable="false" y="${(parent.height-height)/2}">
<attribute name="showvalue" value="${parent.parent.showvalue}"/>
<dragstate name="thedragstate" drag_axis="x">
<text name="t" resize="true" x="${classroot.width/2-width/2}" y="-14" text="${parent.parent.parent.value}" visible="${classroot.showvalue}"/>
</dragstate>
<setter name="x" args="x">
var boundedx = x;
if ( x > parent.width-width ) {
var constrainX = parent.width-width;
boundedx = constrainX;
} else
if (parent.x > x) {
boundedx = parent.x;
}
super.setAttribute('x', boundedx);
//update slider value
var perc = ( (x) / (parent.parent.width-width) );
var val = Math.round( (((perc)*(parent.parent.maxvalue-parent.parent.minvalue))+parseFloat(parent.parent.minvalue))
* Math.pow(10,parent.parent.precision) ) / Math.pow(10,parent.parent.precision) ;
//only adjust value if user is dragging or left or right key is pressed
if (thedragstate.applied || lz.Keys.isKeyDown("leftarrow") || lz.Keys.isKeyDown("rightarrow") ) {
parent.parent.setValue(val);
}
</setter>
</class>
</library>
Cross References
Includes
Classes