package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.ui.Keyboard;
import flash.utils.getTimer;
import MathParser;
import MathFormula;
public class TestMathParser extends Sprite
{
var mParser :MathParser = new MathParser(0);
var mFormula :MathFormula = new MathFormula();
var tfLabel1 :TextField = new TextField();
var tfLabel2 :TextField = new TextField();
var tfLabel3 :TextField = new TextField();
var tfExpression:TextField = new TextField();
var tfFormula :TextField = new TextField();
var tfParam :TextField = new TextField();
var tfResult1 :TextField = new TextField();
var tfResult2 :TextField = new TextField();
var tfMessage :TextField = new TextField();
var tfCont :Sprite = new Sprite();
var aTransforms :Array = [ new ColorTransform(1.4, 1.0, 1.5), new ColorTransform(0.6, 0.6, 0.8), new ColorTransform()];
public function TestMathParser()
{
tfLabel1.mouseEnabled = tfLabel2.mouseEnabled = tfLabel3.mouseEnabled = false;
tfExpression.type = tfFormula.type = tfParam.type = "input";
tfExpression.wordWrap = tfFormula.wordWrap = tfParam.wordWrap = true;
tfExpression.width = tfFormula.width = tfParam.width = 320;
tfExpression.height = tfFormula.height = tfParam.height = 50;
tfResult1.width = tfResult2.width = 320;
tfResult1.height = tfResult2.height = 20;
tfResult1.border = tfResult2.border = true;
tfExpression.background = tfFormula.background = tfParam.background = true;
tfResult1.background = tfResult2.background = true;
tfExpression.backgroundColor = tfFormula.backgroundColor =0xEFDEFF;
tfParam.backgroundColor = 0xECCECC;
tfResult1.backgroundColor = tfResult2.backgroundColor = 0x000000;
tfResult1.borderColor = tfResult2.borderColor = 0x555555;
tfResult1.textColor = tfResult2.textColor = tfMessage.textColor = 0xFFFFFF;
tfLabel1.textColor = tfLabel2.textColor = tfLabel3.textColor = 0xCCCCCC;
tfLabel1.x = tfLabel2.x = tfLabel3.x = 15;
tfExpression.x = tfFormula.x = tfParam.x = 15;
tfResult1.x = tfResult2.x = 15;
tfLabel1.y = 5;
tfExpression.y = 25;
tfResult1.y = 77;
tfLabel2.y = 102;
tfFormula.y = 124;
tfLabel3.y = 176;
tfParam.y = 196;
tfResult2.y = 248;
tfCont.x = 15;
tfCont.y = 275;
tfMessage.x = 10;
tfMessage.y = 2;
tfMessage.autoSize = "left";
tfMessage.mouseEnabled = tfMessage.selectable = false;
var tff1:TextFormat = new TextFormat("Verdana", 12, 0xFFFFFF, true);
var tff2:TextFormat = new TextFormat("Verdana", 11, 0x000000, true);
tfLabel1.defaultTextFormat = tfLabel2.defaultTextFormat = tfLabel3.defaultTextFormat = tff1;
tfResult1.defaultTextFormat = tfResult2.defaultTextFormat = tfMessage.defaultTextFormat = tff1;
tfLabel1.autoSize = tfLabel2.autoSize = tfLabel3.autoSize = "left";
tfExpression.defaultTextFormat = tfFormula.defaultTextFormat = tfParam.defaultTextFormat = tff2;
tfLabel1.text = "Expression:";
tfLabel2.text = "Formula :";
tfLabel3.text = "Parameters:";
tfExpression.text = "6*3+2*(-2-6)";
tfFormula.text = "2*x+3*y^2+log(5)+sin(10)";
tfParam.text = "x=2, y=4";
tfMessage.text = "Enter or click here to calculate";
addChild(tfLabel1);
addChild(tfLabel2);
addChild(tfLabel3);
addChild(tfExpression);
addChild(tfFormula);
addChild(tfParam);
addChild(tfResult1);
addChild(tfResult2);
addChild(tfCont);
tfCont.addChild(tfMessage);
btnUp(null);
graphics.lineStyle(5, 0xC9C5C9, 0.7);
graphics.beginFill(0x252525);
graphics.drawRoundRect(0,0,350,320,5,5);
graphics.endFill();
graphics.lineStyle(1, 0xDEDEDF, 1);
graphics.drawRoundRect(2,2,345,315,5,5);
var matrix = new Matrix();
matrix.createGradientBox(320,24,Math.PI/2);
tfCont.graphics.lineStyle(1, 0x555555, 0.7);
tfCont.graphics.beginGradientFill("linear",
[0x9A9A9A, 0x2A272A, 0x9A999B],
[1,1,1],
[0x25, 0xA9, 0xFF],
matrix);
tfCont.graphics.drawRoundRect(0,0,320,24,5,5);
tfCont.graphics.endFill();
tfCont.mouseEnabled = true;
tfCont.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
tfCont.addEventListener(MouseEvent.MOUSE_DOWN, btnDown);
tfCont.addEventListener(MouseEvent.MOUSE_OUT, btnOut);
tfCont.addEventListener(MouseEvent.MOUSE_UP, btnUp);
stage.addEventListener(KeyboardEvent.KEY_UP, kbUp);
}
function btnOver(e:MouseEvent) :void { tfCont.transform.colorTransform = aTransforms[0]; }
function btnDown(e:MouseEvent) :void { tfCont.transform.colorTransform = aTransforms[1]; }
function btnOut(e:MouseEvent) :void { tfCont.transform.colorTransform = aTransforms[2]; }
function btnUp(e:MouseEvent) :void
{
tfCont.transform.colorTransform = aTransforms[0];
calcExpression();
mFormula.formula = tfFormula.text;
mFormula.setParameters(tfParam.text);
tfResult2.text = mFormula.result.toString();
}
function kbUp(e:KeyboardEvent):void
{
if (e.keyCode != Keyboard.ENTER) return;
if (e.target == tfExpression)
{
calcExpression();
}
else if (e.target == tfFormula)
{
mFormula.formula = tfFormula.text;
tfResult2.text = mFormula.result.toString();
}
else if (e.target == tfParam.text)
{
mFormula.setParameters(tfParam.text);
tfResult2.text = mFormula.result.toString();
}
}
function calcExpression():void
{
var n:Number = mParser.parseFormula(tfExpression.text);
if (isNaN(n))
{
tfResult1.text = "ERROR";
}
else
{
tfResult1.text = n.toString();
}
}
}
}
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar