2018-01-04 23:03:50 +01:00
/// <reference path="./moduleView.ts" />
namespace pxsim . visuals {
export abstract class MotorView extends ModuleView implements LayoutElement {
2018-04-10 20:50:58 +02:00
protected motorLabelGroup : SVGGElement ;
protected motorLabel : SVGTextElement ;
private currentLabel : string ;
2018-01-04 23:03:50 +01:00
constructor ( xml : string , prefix : string , id : NodeType , port : NodeType ,
protected rotating_hole_id : string ) {
super ( xml , prefix , id , port ) ;
}
updateState() {
super . updateState ( ) ;
const motorState = ev3board ( ) . getMotors ( ) [ this . port ] ;
if ( ! motorState ) return ;
const speed = motorState . getSpeed ( ) ;
2018-04-10 20:50:58 +02:00
this . setMotorAngle ( motorState . getAngle ( ) % 360 ) ;
this . setMotorLabel ( speed ) ;
2018-01-04 23:03:50 +01:00
}
private setMotorAngle ( angle : number ) {
const holeEl = this . content . getElementById ( this . normalizeId ( this . rotating_hole_id ) )
this . renderMotorAngle ( holeEl , angle ) ;
}
protected abstract renderMotorAngle ( holeEl : Element , angle : number ) : void ;
getWiringRatio() {
return 0.37 ;
}
2018-04-10 20:50:58 +02:00
setMotorLabel ( speed : number , force? : boolean ) {
if ( ! force && this . currentLabel === ` ${ speed } ` ) return ;
this . currentLabel = ` ${ speed } ` ;
if ( ! this . motorLabel ) {
this . motorLabelGroup = pxsim . svg . child ( this . content , "g" ) as SVGGElement ;
this . motorLabel = pxsim . svg . child ( this . motorLabelGroup , "text" , { 'text-anchor' : 'middle' , 'x' : '0' , 'y' : '0' , 'class' : 'sim-text number inverted' } ) as SVGTextElement ;
}
2018-04-13 23:01:25 +02:00
this . motorLabel . textContent = ` ${ this . currentLabel } % ` ;
this . positionMotorLabel ( ) ;
2018-04-10 20:50:58 +02:00
}
protected abstract positionMotorLabel ( ) : void ;
2018-01-04 23:03:50 +01:00
}
}