calculator_display.lzx
<library>
<resource name="calc_display" src="./resources/display.png"/>
<class name="calcDisplay" resource="calc_display" x="20" y="31" oninit="this.start()">
<text name="screen" height="30" width="165" font="displayText" fontsize="25" y="5" x="5">
0
</text>
<method name="start">
this.valueX = 0;
this.lastInput = 'none';
this.oldValue = false;
this.allOperators = new Array( '+', '-', '/', '*' );
this.operator = '+';
this.screen.setAttribute('text', this.valueX.toString() );
</method>
<method name="clear">
this.start();
</method>
<method name="inputDigit" args="buttonObj">
var val = buttonObj.buttonText.text;
// weed out any non-numbers (e.g. operators, clear button)
if ( isInArray( val, this.allOperators ) ) {
var theOperator = val;
this.inputOperator( theOperator );
return;
} else if ( val == 'C' ) {
this.start();
return;
} else if ( val == '.' ) {
this.addDecimalPoint();
return;
} else if ( val == '+/-' ) {
this.negative();
return;
} else if ( val == '=' ) {
this.equals();
return;
}
var displ = this.screen.text;
if ( ( displ == '0' ) && ( val == '0' ) ) {
return;
}
if ( ( this.lastInput == 'none' )
|| ( this.lastInput == 'operator' ) ) {
// clear display and rewrite
this.screen.setAttribute('text', val );
} else if ( this.lastInput == 'digit' ) {
this.screen.setAttribute('text', displ + val );
} else if ( this.lastInput == 'equals' ) {
this.clear();
this.screen.setAttribute('text', val );
}
this.lastInput = 'digit';
</method>
<method name="inputOperator" args="operator">
if ( this.lastInput == 'digit' ) {
this.execute( this.screen.text );
}
this.valueX = this.screen.text;
this.operator = operator;
this.lastInput = 'operator';
</method>
<method name="equals">
if (this.lastInput != 'equals' ) {
this.oldValue = this.screen.text;
this.lastInput = 'equals';
this.execute( this.oldValue );
} else {
this.lastInput = 'equals';
this.execute( this.oldValue );
}
</method>
<method name="execute" args="val">
this.valueX -= 0;
var valFromDisp = val - 0;
if ( this.valueX == 0 ) return;
if ( this.operator == '+' ) {
val = this.valueX + valFromDisp;
} else if ( this.operator == '-' ) {
val = this.valueX - valFromDisp;
} else if ( this.operator == '*' ) {
val = this.valueX * valFromDisp;
} else if ( this.operator == '/' ) {
val = this.valueX / valFromDisp;
}
valFromDisp = val;
this.screen.setAttribute('text', valFromDisp.toString() );
this.valueX = this.screen.text;
</method>
<method name="isThereDecimal">
var disp = this.screen.text;
var isDecimal = false;
for ( var i=0; i<disp.length; i++ ) {
if ( disp.charAt( i ) == '.' ) {
return true;
}
}
return false;
</method>
<method name="addDecimalPoint">
if ( ( this.lastInput == 'none' )
|| ( this.lastInput == 'operator' ) ) {
if ( !this.isThereDecimal() ) {
this.screen.setAttribute('text', "0." );
}
} else if ( this.lastInput == 'digit' ) {
if ( !this.isThereDecimal() ) {
var newText = this.screen.text;
newText += ".";
this.screen.setAttribute('text', newText );
}
} else if ( this.lastInput == 'equals' ) {
this.clear();
this.screen.setAttribute('text', '0.' );
}
this.lastInput = 'digit';
</method>
<method name="negative">
if ( ( this.lastInput == 'digit' ) ||
( this.lastInput == 'equals' ) ) {
var newDisp = ( this.screen.text - 0 ) * -1;
this.screen.setAttribute('text', newDisp.toString() );
} else {
clear();
}
</method>
</class>
</library>
Cross References
Resources
Classes