streamscrubber.lzx
<library>
<class name="_scrubstate" extends="state">
<attribute name="__scrubstate_xdoffset" value="$once{this.xoffset - this.getMouse( 'x' )}"/>
<attribute name="x" value="this.__scrubstate_getnewpos( this.immediateparent.getMouse( 'x' ) + this.__scrubstate_xdoffset)
" when="always"/>
<method name="__scrubstate_getnewpos" args="newpos">
if (newpos < min) newpos = min;
if (newpos > max) newpos = max;
if (this.x != newpos) { // new scrubber value
//Debug.write('new newpos=%w',newpos);
var totalLength = this.max - this.min;
var pos = newpos - this.min;
var newtime = pos/totalLength * this.stream.totaltime;
/**
Debug.write('pos=%w totalLength=%w perc=%w',
pos, totalLength, pos/totalLength);
Debug.write('totaltime=%w newtime=%w',
this.stream.totaltime, newtime);
**/
this.stream.seek(newtime);
}
return newpos;
</method>
</class>
<class name="streamscrubber">
<attribute name="stream" value="null
"/>
<attribute name="scrubbing" value="false
" type="boolean"/>
<attribute name="min" value="0
" type="number"/>
<attribute name="max" value="$once{parent.width-this.width}" type="number"/>
<attribute name="time" value="0
"/>
<attribute name="totaltime" value="0
"/>
<attribute name="progress" value="0
"/>
<attribute name="_waspaused" value="true
" type="boolean"/>
<attribute name="_trackdel" value="null
"/>
<event name="onstream"/>
<event name="ontime"/>
<event name="ontotaltime"/>
<event name="onprogress"/>
<setter name="stream" args="newStream">
this.stream = newStream;
if (!_trackdel) {
this._trackdel = new LzDelegate(this, "_trackprogress");
}
this._trackdel.unregisterAll();
this._trackdel.register(this.stream, 'ontime');
this._trackdel.register(this.stream, 'ontotaltime');
this._trackdel.register(this, 'onmin');
if (this.onstream) this.onstream.sendEvent(this);
</setter>
<handler name="onmousedown">
if (!this.stream) {
Debug.warn('streamscrubber %w: null stream');
}
this._waspaused = this.stream.paused;
this.stream.pause(true);
this.setAttribute('scrubbing', true);
this._trackdel.unregisterAll();
this.scrubaction.apply();
</handler>
<handler name="onmouseup">
this.scrubaction.remove();
this.setAttribute('scrubbing', false);
this._trackdel.register(this.stream, 'ontime');
this._trackdel.register(this.stream, 'ontotaltime');
if (!this._waspaused) this.stream.pause(false);
</handler>
<method name="_trackprogress" args="ignore=null">
var newTime = false, newTotal = false;
if (this.time != this.stream.time) {
newTime = true;
this.time = this.stream.time;
}
if (this.totaltime != this.stream.totaltime) {
newTotal = true;
this.totaltime = this.stream.totaltime;
}
this.progress = this.totaltime > 0 ?
this.time / this.totaltime : 0;
if (this.inited) {
var newx = this.min + (this.max-this.min) * this.progress;
this.setAttribute('x', newx );
}
if (this.ontime && newTime) this.ontime.sendEvent(this);
if (this.ontotaltime && newTotal) this.ontotaltime.sendEvent(this);
if (this.onprogress) this.onprogress.sendEvent(this);
</method>
<_scrubstate name="scrubaction"/>
</class>
</library>