calculator-$11.lzx
<canvas width="100%" height="400">
<resource name="calc_body" src="resources/body.png"/>
<resource name="calc_display" src="resources/display.png"/>
<resource name="button_grn">
<frame src="resources/new_button_green_off.png"/>
<frame src="resources/new_button_green_over.png"/>
<frame src="resources/new_button_green_down.png"/>
</resource>
<resource name="button_blu">
<frame src="resources/new_button_blue_off.png"/>
<frame src="resources/new_button_blue_over.png"/>
<frame src="resources/new_button_blue_down.png"/>
</resource>
<resource name="button_red">
<frame src="resources/new_button_red_off.png"/>
<frame src="resources/new_button_red_over.png"/>
<frame src="resources/new_button_red_down.png"/>
</resource>
<class name="calcButton" resource="button_blu" clickable="true" onclick="display.inputDigit(this);" oninit="this.initButton()" extends="basebutton">
<attribute name="buttLabel" type="string"/>
<attribute name="labelX" type="number" value="11
"/>
<method name="initButton">
this.buttonText.setAttribute('x',
this.labelX);
this.buttonText.setAttribute('text', this.buttLabel);
</method>
<text name="buttonText" font="obliqueText" fontsize="25" fgcolor="#ffffff" x="13" valign="middle" width="0">
<handler name="ontext">
this.setAttribute('width', this.getTextWidth() + 5);
</handler>
</text>
</class>
<script>
// utility function that finds an item in an array
//
function isInArray(x, theArray) {
var isThere = false;
for (var i=0; i != theArray.length; i++) {
if (theArray[i] == x) {
isThere = true;
}
}
return isThere;
}
</script>
<view name="calculator" resource="calc_body" x="20" y="20">
<view id="display" 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') { if (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>
</view>
<view name="buttons" x="19" y="88">
<simplelayout axis="y" spacing="7"/>
<view name="row1" width="100%">
<calcButton buttLabel="C" resource="button_red"/>
<calcButton buttLabel="/" align="right" labelX="13"/>
</view>
<view name="row2">
<simplelayout axis="x" spacing="7"/>
<calcButton buttLabel="7"/>
<calcButton buttLabel="8"/>
<calcButton buttLabel="9"/>
<calcButton buttLabel="*" labelX="13"/>
</view>
<view name="row3">
<simplelayout axis="x" spacing="7"/>
<calcButton buttLabel="4"/>
<calcButton buttLabel="5"/>
<calcButton buttLabel="6"/>
<calcButton buttLabel="-" labelX="15"/>
</view>
<view name="row4">
<simplelayout axis="x" spacing="7"/>
<calcButton buttLabel="1"/>
<calcButton buttLabel="2"/>
<calcButton buttLabel="3"/>
<calcButton buttLabel="+"/>
</view>
<view name="row5">
<simplelayout axis="x" spacing="7"/>
<calcButton buttLabel="0"/>
<calcButton buttLabel="+/-" labelX="1"/>
<calcButton buttLabel="." labelX="16"/>
<calcButton buttLabel="=" resource="button_grn" labelX="12"/>
</view>
</view>
</view>
</canvas>
Cross References
Resources
Classes
Named Instances