Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Calculator script (https://forums.graalonline.com/forums/showthread.php?t=134257578)

Liberated 01-07-2010 08:47 PM

Calculator script
 
1 Attachment(s)
**UPDATED**
I made a simple calculator script, it's my biggest script so far.
this is what it looks like:
http://i78.photobucket.com/albums/j1...g?t=1262889724
Updates planned for the future:

*I will make it more advanced so you will have a mem function, and a square root and ^2 function.


this is the code:
PHP Code:

//#CLIENTSIDE
function onWeaponFired()
{
  
this.number1 1;
  
this.number2 0;
  
this.divide 0;
  
this.add 0;
  
this.subtract 0;
  
this.multiply 0;
  new 
GuiWindowCtrl("Calculator_window")
  {
    
profile GuiBlueWindowProfile;
    
clientrelative true;
    
clientextent "165, 200";
    
canmove true;
    
canresize false;
    
closequery false;
    
destroyonhide true;
    
text "Calculator";
    
300;
    
200;
    
    new 
GuiTextEditCtrl("Calculator_input")
    {
      
profile GuiBlueTextEditProfile;
      
useownprofile true;
      
profile.align "right"
      
5;
      
5;
      
width 155;
      
height 30;
      
text="0";
      
text.align "right";
    }
    new 
GuiButtonCtrl("Calculator_clear")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "Clear";
      
width 35;
      
5;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_plusmin")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+/-";
      
width 35;
      
45;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_divide")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "/";
      
width 35;
      
85;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_multiply")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "X";
      
width 35;
      
125;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
        new 
GuiButtonCtrl("Calculator_seven")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "7";
      
width 35;
      
5;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_eight")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "8";
      
width 35;
      
45;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_nine")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "9";
      
width 35;
      
85;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_subtract")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "-";
      
width 35;
      
125;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
        new 
GuiButtonCtrl("Calculator_four")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "4";
      
width 35;
      
5;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_five")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "5";
      
width 35;
      
45;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_six")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "6";
      
width 35;
      
85;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_add")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+";
      
width 35;
      
125;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
        new 
GuiButtonCtrl("Calculator_one")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "1";
      
width 35;
      
5;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_two")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "2";
      
width 35;
      
45;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_three")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "3";
      
width 35;
      
85;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_equals")
    {
      
profile GuiBlueButtonProfile;
      
height 55;
      
text "=";
      
width 35;
      
125;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_zero")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "0";
      
width 75;
      
5;
      
165;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_dot")
    {
    
profile GuiBlueButtonProfile;
      
height 25;
      
text ".";
      
width 35;
      
85;
      
165;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }   
  }
}
function 
ButtonPressed(obj)
{
  if(
obj.text in |09|)
  {
    if(
Calculator_input.text == "0")
    {
      
Calculator_input.text obj.text;
    }
    else
    {
      
Calculator_input.text @= obj.text;
    }
  }
  else if(
obj.text == ".")
  {
    
    
Calculator_input.text @= obj.text;
  }
  else if(
obj.text == "+/-")
  {
    
Calculator_input.text -= Calculator_input.text;
  }
  else if(
obj.text == "X")
  { 
    if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
    }
    
this.divide 0;
    
this.add 0;
    
this.subtract 0;
    
this.multiply 1;
    
this.number1 *= Calculator_input.text;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "/")
  {
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.number1 1;
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
    }
    
this.multiply 0;
    
this.add 0;
    
this.subtract 0;
    
this.divide 1;
    
this.number1 Calculator_input.text this.number1;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "+")
  { 
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.number1 1;
    }
    else if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
    }
    
this.multiply 0;
    
this.divide 0;
    
this.subtract 0;
    
this.add 1;
    
this.number2 += Calculator_input.text;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "-")
  { 
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.multiply 0;
    }
    else if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
    }
    
this.subtract 1;
    
this.number2 Calculator_input.text this.number2;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "=")
  {
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.number1 1;
      
this.multiply 0;
      
this.number2 0;
    }
    else if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
      
this.number1 1;
      
this.number2 0;
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
      
this.number2 0;
      
this.number1 1;
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
      
this.number1 1;  
      
this.number2 0;
    }
  }
  else if(
obj.text == "Clear")
  {
    
Calculator_input.text 0;
    
this.number1 1;  
    
this.number2 0;   
  } 
}
//Created by Miniman 

or in a handy text file.
any tips or critics are appreciated, i know i could've done *= with the multiply and add functions. but decided to leave it this way so they match with the divide and subtract functions.

thanks to chris, switch and fowlplay4 for the help.

cbk1994 01-07-2010 09:09 PM

Looks good :)

Quote:

Originally Posted by Liberated (Post 1548872)
but decided to leave it this way so they match with the divide and subtract functions.

You can do "/=", "-=", etc.

Liberated 01-07-2010 09:19 PM

Quote:

Originally Posted by cbk1994 (Post 1548875)
Looks good :)



You can do "/=", "-=", etc.

but i have to change Calculator_input.text.
and if i would do
PHP Code:

Calculator_input.text -= this.number1;
i would get Calculator_input.text Calculator_input.text this.number1;
when i need Calculator_input.text this.number1 Calculator_input.text

the wrong way arround :s
same with /= but for multiplying and adding it doesn't matter which one comes first.

for example
PHP Code:

Calculator_input.text 5;
this.number1 2;
i need this.number1 Calculator_input.text = -3
but 
if i would do =- i would get Calculator_input.text -= this.number1which is
 3 but in the order which it is entered in the calculator i need the first way



Switch 01-07-2010 10:06 PM

Try using catchEvent() in the buttons so you don't need all those obj.onAction()'s

Liberated 01-07-2010 10:10 PM

Quote:

Originally Posted by Switch (Post 1548887)
Try using catchEvent() in the buttons so you don't need all those obj.onAction()'s

What doe catchEvent() do?
I was already wondering if there wasn't an easier way btw.

Switch 01-07-2010 10:22 PM

Quote:

Originally Posted by Liberated (Post 1548888)
What doe catchEvent() do?
I was already wondering if there wasn't an easier way btw.

catchevent(str, str, str) - adds an event handler for the specified object name and event, third parameter is the name of the function which receives the event (first parameter of the event will be the object for which the event occured)
http://wiki.graal.net/index.php/Crea...ient/TGraalVar

PHP Code:

//in button creation
catchEvent(this"onAction""buttonPressed");

//later
function buttonPressed(obj) { //obj is the object caught
  //stuff


Doing simple checks (probably easiest to use obj.text) inside buttonPressed() will determine what to do.

Example:
PHP Code:

if (obj.text in |09|) {
  
this.number1 @= obj.text;



fowlplay4 01-08-2010 01:05 AM

That's kind of vague, but here's a live example of catchevent.

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
catchEventTest();
}

function 
catchEventTest() {
  new 
GuiButtonCtrl("HelloWorldButton") {
    
0;
    
width screenwidth;
    
height screenheight;
    
text "Hello World!";
    
thiso.catchevent(this.name"onAction""onHelloWorldPressed");
  }
}

function 
onHelloWorldPressed(obj) {
  
player.chat format("Good bye %s!"obj.name);
  
obj.destroy();


Also right align the result, and you'll be gr8.

Liberated 01-08-2010 06:55 PM

V2.0 is done!
PHP Code:

//#CLIENTSIDE
function onWeaponFired()
{
  
this.number1 1;
  
this.number2 0;
  
this.divide 0;
  
this.add 0;
  
this.subtract 0;
  
this.multiply 0;
  new 
GuiWindowCtrl("Calculator_window")
  {
    
profile GuiBlueWindowProfile;
    
clientrelative true;
    
clientextent "165, 200";
    
canmove true;
    
canresize false;
    
closequery false;
    
destroyonhide true;
    
text "Calculator";
    
300;
    
200;
    
    new 
GuiTextEditCtrl("Calculator_input")
    {
      
profile GuiBlueTextEditProfile;
      
useownprofile true;
      
profile.align "right"
      
5;
      
5;
      
width 155;
      
height 30;
      
text="0";
    }
    new 
GuiButtonCtrl("Calculator_clear")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "Clear";
      
width 35;
      
5;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_plusmin")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+/-";
      
width 35;
      
45;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_divide")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "/";
      
width 35;
      
85;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_multiply")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "X";
      
width 35;
      
125;
      
45;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
        new 
GuiButtonCtrl("Calculator_seven")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "7";
      
width 35;
      
5;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_eight")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "8";
      
width 35;
      
45;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_nine")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "9";
      
width 35;
      
85;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_subtract")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "-";
      
width 35;
      
125;
      
75;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
        new 
GuiButtonCtrl("Calculator_four")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "4";
      
width 35;
      
5;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_five")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "5";
      
width 35;
      
45;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_six")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "6";
      
width 35;
      
85;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_add")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "+";
      
width 35;
      
125;
      
105;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
        new 
GuiButtonCtrl("Calculator_one")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "1";
      
width 35;
      
5;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_two")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "2";
      
width 35;
      
45;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_three")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "3";
      
width 35;
      
85;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_equals")
    {
      
profile GuiBlueButtonProfile;
      
height 55;
      
text "=";
      
width 35;
      
125;
      
135;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_zero")
    {
      
profile GuiBlueButtonProfile;
      
height 25;
      
text "0";
      
width 75;
      
5;
      
165;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }
    new 
GuiButtonCtrl("Calculator_dot")
    {
    
profile GuiBlueButtonProfile;
      
height 25;
      
text ".";
      
width 35;
      
85;
      
165;
      
thiso.catchEvent(this"onAction""buttonPressed");
    }   
  }
}
function 
ButtonPressed(obj)
{
  if(
obj.text in |09|)
  {
    if(
Calculator_input.text == "0")
    {
      
Calculator_input.text obj.text;
    }
    else
    {
      
Calculator_input.text @= obj.text;
    }
  }
  else if(
obj.text == ".")
  {
    
    
Calculator_input.text @= obj.text;
  }
  else if(
obj.text == "+/-")
  {
    
Calculator_input.text -= Calculator_input.text;
  }
  else if(
obj.text == "X")
  { 
    if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
    }
    
this.divide 0;
    
this.add 0;
    
this.subtract 0;
    
this.multiply 1;
    
this.number1 *= Calculator_input.text;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "/")
  {
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.number1 1;
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
    }
    
this.multiply 0;
    
this.add 0;
    
this.subtract 0;
    
this.divide 1;
    
this.number1 Calculator_input.text this.number1;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "+")
  { 
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.number1 1;
    }
    else if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
    }
    
this.multiply 0;
    
this.divide 0;
    
this.subtract 0;
    
this.add 1;
    
this.number2 += Calculator_input.text;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "-")
  { 
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.multiply 0;
    }
    else if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
    }
    
this.subtract 1;
    
this.number2 Calculator_input.text this.number2;
    
Calculator_input.text "0";
  }
  else if(
obj.text == "=")
  {
    if(
this.multiply == 1)
    {
      
Calculator_input.text *= this.number1;
      
this.number1 1;
      
this.multiply 0;
      
this.number2 0;
    }
    else if(
this.divide == 1)
    {
      
this.divide 0;
      
Calculator_input.text = (this.number1 Calculator_input.text);
      
this.number1 1;
      
this.number2 0;
    }
    else if(
this.add == 1)
    {
      
this.add 0;
      
Calculator_input.text += this.number2;
      
this.number2 0;
      
this.number1 1;
    }
    else if(
this.subtract == 1)
    {
      
Calculator_input.text this.number2 Calculator_input.text;
      
this.subtract 0;
      
this.number1 1;  
      
this.number2 0;
    }
  }
  else if(
obj.text == "Clear")
  {
    
Calculator_input.text 0;
    
this.number1 1;  
    
this.number2 0;   
  } 


it looks exactly the same but it's 2 times as good!
I used the catchEvent() thing, and now you don't have to do8*8=64 * 8= etc.
now you can just do 8*8*8=, or even 8*8+3/2
however, to make this possible, quite alot more code was used, and despite catchEvent(), it now has exactly as much lines as it used to have
it doesn't use bodmas tho. and it just does the calculations in the sequence you put it in.
right align also works now.
@fowlplay4,
i haven't been able to align it to the right. any tips? fixed, thanks

fowlplay4 01-08-2010 07:00 PM

Something like this should work, but I know align has been a pain before with me.

PHP Code:

    new GuiTextEditCtrl("Calculator_input")
    {
      
profile GuiBlueTextEditProfile;
      
useownprofile true;
      
profile.align "right";
      
5;
      
5;
      
width 155;
      
height 30;
      
text="0";
    } 


Liberated 01-08-2010 07:05 PM

Quote:

Originally Posted by fowlplay4 (Post 1549081)
Something like this should work, but I know align has been a pain before with me.

i tried text.align before, but that didn't work, maybe this does, i hope it will.

edit: works, lemme update earlier post.

jkldogg 02-22-2010 02:16 AM

how do u use the script? like do u say /calculator or something? i added the script to my server, vega united, but it's not working

12171217 02-22-2010 02:51 AM

Quote:

Originally Posted by jkldogg (Post 1557749)
how do u use the script? like do u say /calculator or something? i added the script to my server, vega united, but it's not working

Vega United is one of the most promising server's I've seen in a long time. Good job.

Soala 02-22-2010 03:42 AM

Good job there buddy, +rep coming your way!

Quote:

Originally Posted by jkldogg (Post 1557749)
how do u use the script? like do u say /calculator or something? i added the script to my server, vega united, but it's not working

Quote:

function onWeaponFired()
This should tell you enough about how it works. You actually just press D after selecting it in your inventory.
Just look at the script, if it was meant to be launched with a command you would actually see if the script is told to run by a command.

smirt362 02-22-2010 11:25 AM

Pretty cool.

jkldogg 02-25-2010 07:04 PM

thank you
 
Quote:

Originally Posted by 12171217 (Post 1557761)
Vega United is one of the most promising server's I've seen in a long time. Good job.

wow, thanks ^^


All times are GMT +2. The time now is 12:52 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.