Calculator

Moderator: Impudent1

Post Reply
Impudent1
Posts: 57
Joined: June 12th, 2004, 6:40 pm

As per the thread in the scripts discussion requesting a built in calculator.

Given that the text input fields only deal with very simple expressions it can be nice to have a simple calculator handy.

Image

This script goes in your Scripts or Scripts\Startup folder.

http://www.leapfrog-productions.com/Scripts/Calc.jsx
Impudent1
Posts: 57
Joined: June 12th, 2004, 6:40 pm

I have updated the Calculator to hopefully accomodate the mac/windows button width differences.

http://www.leapfrog-productions.com/Scripts/Calc_v2.jsx

This has not been tested on macOS, and running an osx skin in xp doesn't reflect the width changes properly, so if the layout is still skewed please let me know/post a screenshot so I can adjust the layout.

For the record this is probably not the best way to go regarding the button fix, I would probably recommend looking at the DemoPalette.jsx that is included with 6.5 and study the automagic layout.
cavolfiore89
Posts: 1
Joined: June 6th, 2013, 3:48 am

Thanks so much!!! Very handy!!! :D :D :D
Ryunin
Posts: 2
Joined: May 21st, 2017, 7:02 am

Since the website is down, i could get the code from the website no idea if it is still working:

Date: JANUARY 17, 2006

Code: Select all

/* Palettized Calculator */
// v2 modified to make the UI button widths fixed on macOS

{ // Top brace

	var Memory = 0;
	var Number1 = "";
	var Number2 = "";
	var NewNumber = "blank";
	var opvalue = "";

// fix for mac ui button widths
	var isMac = "true";

		if (system.osName.indexOf("Windows") != -1) {
			isMac = "false";
							    }	

// functions section

               function Display(displaynumber) {
               calculator.answer.text = displaynumber;
               }
               
               function MemoryClear() {
               Memory = 0;
               calculator.mem.text = "";
               }
               
               function MemoryRecall(answer) {
               if(NewNumber != "blank") {
               Number2 += answer;
               } else {
               Number1 = answer;
               }
               NewNumber = "blank";
               Display(answer);
               }
               
               function MemorySubtract(answer) {
               Memory = Memory - eval(answer);
               }
               
               function MemoryAdd(answer) {
               Memory = Memory + eval(answer);
               calculator.mem.text = " M ";
               NewNumber = "blank";
               }
               
               function ClearCalc() {
               Number1 = "";
               Number2 = "";
               NewNumber = "blank";
               Display("");
               }
               
               function Backspace(answer) {
               answerlength = answer.length;
               answer = answer.substring(0, answerlength - 1);
               if (Number2 != "") {
               Number2 = answer.toString();
               Display(Number2);
               } else {
               Number1 = answer.toString();
               Display(Number1);
                  }
               }
               
               function CECalc() {
               Number2 = "";
               NewNumber = "yes";
               Display("");
               }
               
               function CheckNumber(answer) {
               if(answer.text == ".") {
               Number = calculator.answer.text;
               if(Number.indexOf(".") != -1) {
               answer.text = "";
                  }
               }
               if(NewNumber == "yes") {
               Number2 += answer;
               Display(Number2);
               }
               else {
               if(NewNumber == "blank") {
               Number1 = answer;
               Number2 = "";
               NewNumber = "no";
               }
               else {
               Number1 += answer;
               }
               Display(Number1);
                  }
               }
               function AddButton(x) {
               if(x == 1) EqualButton();
               if(Number2 != "") {
               Number1 = parseFloat(Number1) + parseFloat(Number2);
               }
               NewNumber = "yes";
               opvalue = '+';
               Display(Number1);
               }
               function SubButton(x) {
               if(x == 1) EqualButton();
               if(Number2 != "") {
               Number1 = parseFloat(Number1) - parseFloat(Number2);
               }
               NewNumber = "yes";
               opvalue = '-';
               Display(Number1);
               }
               function MultButton(x) {
               if(x == 1) EqualButton();
               if(Number2 != "") {
               Number1 = parseFloat(Number1) * parseFloat(Number2);
               }
               NewNumber = "yes";
               opvalue = '*';
               Display(Number1);
               }
               function DivButton(x) {
               if(x == 1) EqualButton();
               if(Number2 != "") {
               Number1 = parseFloat(Number1) / parseFloat(Number2);
               }
               NewNumber = "yes";
               opvalue = '/';
               Display(Number1);
               }
               function SqrtButton() {
               Number1 = Math.sqrt(Number1);
               NewNumber = "blank";
               Display(Number1);
               }
               function PercentButton() {
               if(NewNumber != "blank") {
               Number1 *= .01;
               NewNumber = "blank";
               Display(Number1);
                  }
               }
               function RecipButton() {
               Number1 = 1/Number1;
               NewNumber = "blank";
               Display(Number1);
               }
               function NegateButton() {
               Number1 = parseFloat(-Number1);
               NewNumber = "no";
               Display(Number1);
               }
               function EqualButton() {
               if(opvalue == '+') AddButton(0);
               if(opvalue == '-') SubButton(0);
               if(opvalue == '*') MultButton(0);
               if(opvalue == '/') DivButton(0);
               Number2 = "";
               opvalue = "";
               }

// main section
					
// create an empty dialog window near the upper left corner of the window
if (isMac == "true"){ // set temp to != to see mac size
	var calculator = new Window('palette', 'calculator', [10,10,255,175]);
		 }else{	
	var calculator = new Window('palette', 'calculator', [10,10,185,165]);
		    }
		
// add our static text
if (isMac == "true"){
	calculator.add('statictext', [80,5,220,20], 'AE Floating Calculator' );
		}else{
	calculator.add('statictext', [40,5,160,20], 'AE Floating Calculator' );
		    }		
// answerfield
if (isMac == "true"){
	calculator.answer = calculator.add('edittext', [10, 20, 235, 40], "");
		}else{	
	calculator.answer = calculator.add('edittext', [10, 20, 165, 40], "");
		     }
	calculator.answer.onChange = function(){ CheckNumber(this.text); }

// memfield
if (isMac == "true"){
	calculator.mem = calculator.add('edittext',[10, 40, 70, 60], "" );
	}else{
	calculator.mem = calculator.add('edittext',[10, 40, 40, 60], "" );
		    }	

// backspace button
if (isMac == "true"){
	calculator.backspace = calculator.add('button', [75, 40, 155, 60], "Backspace");
}else{
	calculator.backspace = calculator.add('button', [42, 40, 120, 60], "Backspace");
		    }
	calculator.backspace.onClick = function(){ Backspace(calculator.answer.text); return false; }

// CE button
if (isMac == "true"){
	calculator.CE = calculator.add('button', [160, 40, 200, 60], "CE");
}else{
	calculator.CE = calculator.add('button', [122, 40, 142, 60], "CE");
		    }
	calculator.CE.onClick = function(){ CECalc(); return false; }

// C button
if (isMac == "true"){
	calculator.C = calculator.add('button', [205, 40, 235, 60], "C");
}else{
	calculator.C = calculator.add('button', [144, 40, 164, 60], "C");
		    }
	calculator.C.onClick = function(){ ClearCalc(); return false; }

// MC button
if (isMac == "true"){
	calculator.MC = calculator.add('button', [10, 65, 50, 85], "MC");
}else{
	calculator.MC = calculator.add('button', [10, 62, 40, 82], "MC");
		    }	
	calculator.MC.onClick = function(){ MemoryClear(); return false; }

// MR button
if (isMac == "true"){
	calculator.MR = calculator.add('button', [10, 90, 50, 110], "MR");
}else{
	calculator.MR = calculator.add('button', [10, 84, 40, 104], "MR");
		    }
	calculator.MR.onClick = function(){ MemoryRecall(Memory); return false; }

// MS button
if (isMac == "true"){
	calculator.MS = calculator.add('button', [10, 115, 50, 135], "MS");
}else{
	calculator.MS = calculator.add('button', [10, 106, 40, 126], "MS");
		    }
	calculator.MS.onClick = function(){ MemorySubtract(calculator.answer.text); return false; }

// M+ button
if (isMac == "true"){
	calculator.Mplus = calculator.add('button', [10, 140, 50, 160], "M+");
}else{	
	calculator.Mplus = calculator.add('button', [10, 128, 40, 148], "M+");
		    }
	calculator.Mplus.onClick = function(){ MemoryAdd(calculator.answer.text); return false; }

// 7 button
if (isMac == "true"){
	calculator.calc7 = calculator.add('button', [55, 65, 85, 85], "7");
}else{
	calculator.calc7 = calculator.add('button', [42, 62, 62, 82], "7");
		    }
	calculator.calc7.onClick = function(){ CheckNumber('7'); return false; }

// 8 button
if (isMac == "true"){
	calculator.calc8 = calculator.add('button', [90, 65, 120, 85], "8");
}else{
	calculator.calc8 = calculator.add('button', [64, 62, 84, 82], "8");
		    }
	calculator.calc8.onClick = function(){ CheckNumber('8'); return false; }

// 9 button
if (isMac == "true"){
	calculator.calc9 = calculator.add('button', [125, 65, 155, 85], "9");
}else{
	calculator.calc9 = calculator.add('button', [86, 62, 106, 82], "9");
		    }
	calculator.calc9.onClick = function(){ CheckNumber('9'); return false; }

// divide button
if (isMac == "true"){
	calculator.divide = calculator.add('button', [160, 65, 190, 85], "/");
}else{
	calculator.divide = calculator.add('button', [108, 62, 128, 82], "/");
		    }
	calculator.divide.onClick = function(){ DivButton(1); return false; }

// sqrt button
if (isMac == "true"){
	calculator.sqrt = calculator.add('button', [195, 65, 235, 85], "sqrt");
}else{
	calculator.sqrt = calculator.add('button', [130, 62, 165, 82], "sqrt");
		}
	calculator.sqrt.onClick = function(){ SqrtButton(); return false; }

// 4 button
if (isMac == "true"){
	calculator.calc4 = calculator.add('button', [55, 90, 85, 110], "4");
}else{
	calculator.calc4 = calculator.add('button', [42, 84, 62, 104], "4");
		    }
	calculator.calc4.onClick = function(){ CheckNumber('4'); return false; }

// 5 button
if (isMac == "true"){
	calculator.calc5 = calculator.add('button', [90, 90, 120, 110], "5");
}else{
	calculator.calc5 = calculator.add('button', [64, 84, 84, 104], "5");
		}
	calculator.calc5.onClick = function(){ CheckNumber('5'); return false; }

// 6 button
if (isMac == "true"){
	calculator.calc6 = calculator.add('button', [125, 90, 155, 110], "6");
}else{
	calculator.calc6 = calculator.add('button', [86, 84, 106, 104], "6");
		    }
	calculator.calc6.onClick = function(){ CheckNumber('6'); return false; }

// multiply button
if (isMac == "true"){
	calculator.multiply = calculator.add('button', [160, 90, 190, 110], "*");
}else{
	calculator.multiply = calculator.add('button', [108, 84, 128, 104], "*");
		    }
	calculator.multiply.onClick = function(){ MultButton(1); return false; }

// percentage button
if (isMac == "true"){
	calculator.percent = calculator.add('button', [195, 90, 235, 110], "%");
}else{
	calculator.percent = calculator.add('button', [130, 84, 165, 104], "%");
		    }
	calculator.percent.onClick = function(){ PercentButton(); return false; }

// 1 button
if (isMac == "true"){
	calculator.calc1 = calculator.add('button', [55, 115, 85, 135], "1");
}else{
	calculator.calc1 = calculator.add('button', [42, 106, 62, 126], "1");
		    }
	calculator.calc1.onClick = function(){ CheckNumber('1'); return false; }

// 2 button
if (isMac == "true"){
	calculator.calc2 = calculator.add('button', [90, 115, 120, 135], "2");
}else{
	calculator.calc2 = calculator.add('button', [64, 106, 84, 126], "2");
		    }
	calculator.calc2.onClick = function(){ CheckNumber('2'); return false; }

// 3 button
if (isMac == "true"){
	calculator.calc3 = calculator.add('button', [125, 115, 155, 135], "3");
}else{
	calculator.calc3 = calculator.add('button', [86, 106, 106, 126], "3");
		    }
	calculator.calc3.onClick = function(){ CheckNumber('3'); return false; }

// minus button
if (isMac == "true"){
	calculator.minus = calculator.add('button', [160, 115, 190, 135], "-");
}else{
	calculator.minus = calculator.add('button', [108, 106, 128, 126], "-");
		    }
	calculator.minus.onClick = function(){ SubButton(1); return false; }

// reciprocal button
if (isMac == "true"){
	calculator.recip = calculator.add('button', [195, 115, 235, 135], "1/x");
}else{
	calculator.recip = calculator.add('button', [130, 106, 165, 126], "1/x");
		    }
	calculator.recip.onClick = function(){ RecipButton(); return false; }

// 0 button
if (isMac == "true"){
	calculator.calc0 = calculator.add('button', [55, 140, 85, 160], "0");
}else{
	calculator.calc0 = calculator.add('button', [42, 128, 62, 148], "0");
		    }
	calculator.calc0.onClick = function(){ CheckNumber('0'); return false; }

// plusminus button
if (isMac == "true"){
	calculator.negate = calculator.add('button', [90, 140, 120, 160], "+/-");
}else{
	calculator.negate = calculator.add('button', [64, 128, 84, 148], "+/-");
		    }
	calculator.negate.onClick = function(){ NegateButton(); return false; }

// decimal button
if (isMac == "true"){
	calculator.dot = calculator.add('button', [125, 140, 155, 160], ".");
}else{
	calculator.dot = calculator.add('button', [86, 128, 106, 148], ".");
		    }
	calculator.dot.onClick = function(){ CheckNumber('.'); return false; }

// plus button
if (isMac == "true"){
	calculator.plus = calculator.add('button', [160, 140, 190, 160], "+");
}else{
	calculator.plus = calculator.add('button', [108, 128, 128, 148], "+");
		}
	calculator.plus.onClick = function(){ AddButton(1); return false; }

// equals button
if (isMac == "true"){
	calculator.equal = calculator.add('button', [195, 140, 235, 160], "=");
}else{
	calculator.equal = calculator.add('button', [130, 128, 165, 148], "=");
		    }
	calculator.equal.onClick = function(){ EqualButton(); return false; }

// Show it
     calculator.center();
     calculator.show();


} // End brace
Post Reply