Awesome! This is just what I need.
For those of you who want it, here's a little code that I made for this:
code:
var Angle = "Radian";
function OnEvent_ChatWndEditKeyDown(ChatWnd, KeyCode, CtrlKeyDown, ShiftKeyDown){
if(KeyCode == 116 && !CtrlKeyDown && !ShiftKeyDown) {
var x = ChatWnd.EditText.substring(ChatWnd.EditText_GetCurSelStart(), ChatWnd.EditText_GetCurSelEnd()).Calculate()
if(x != undefined) {
ChatWnd.EditText_ReplaceSel(x)
}
}
}
This is to add to Shondoit's calculator so that you can use it in your chats. All you have to do, is type your expression, highlight it and press F5. Also, you can change Angle to "Radian", "Degree", or "Grad" (as seen in Shondoit's script).
Also, Shondoit you might want to check:
code:
if (/^Math\.(a(?:sin|cos|tan2?))\((.+)\)$/.test(Token)) {
var Func = "Math." + RegExp.$1 + "(" + RegExp.$2 + ")/Math.PI"
if (/^degrees?$/i.test(Angle)){
Token = Func + "*180"
} else if (/^grads?$/i.test(Angle)) {
Token = Func + "*200"
}
[b]Token = Token.Calculate()[/b]
}
When I debug trace it, it seems that this loops because the angle setting is not changed. Perhaps change this to
Token = eval(Token)?
-----
EDIT: Thought I might just add this in. If you want to make your own custom calculator functions, add the following code inside the script (you will need to match where it goes):
code:
Token = eval(Token)
}
else if (typeof(Custom[Token.toLowerCase()])=="function" || eval("typeof("+Token+")=='function'")) {
if (eval("typeof("+Token+")!='function'")) {
Token = "Custom." + Token.toLowerCase()
}
Token += this.charAt(i++ + 1)
var ParamToken = ""
var Parentheses = 0
while ((/[^)]/.test(this.charAt(i + 1)) || Parentheses > 0) && i < this.length) {
if (this.charAt(i + 1) == "(") Parentheses++; else if (this.charAt(i + 1) == ")") Parentheses--
ParamToken += this.charAt(i++ + 1)
}
var Params = ParamToken.split(/\s*,\s*/)
for (Index in Params) {
Params[Index] = Params[Index].Calculate()
}
Token += Params.join(",")
Token += this.charAt(i++ + 1)
} else {
PopStack("*")
}
If you know how to shorten this into the code, that would be great
.
Next, you define your own functions like this (these are a few of mine):
code:
var Custom = {
"base" : function (Number, FromBase, ToBase) {
if (Number != Math.floor(Number) || FromBase != Math.floor(FromBase) || ToBase != Math.floor(ToBase)) return undefined
Number = String(Number)
if (FromBase < 2) FromBase = 2
if (FromBase > 10) FromBase = 10
if (ToBase < 2) ToBase = 2
if (ToBase > 36) ToBase = 36
var Internal = 0
var Character
for (var i = Number.length - 1; i >= 0; i--) {
Character = Number.charAt(i)
if (Character >= FromBase) return undefined
Internal += Character * Math.pow(FromBase, Number.length - 1 - i)
}
var Result = ""
while (Internal > 0) {
if (Internal % ToBase > 9) Result = String.fromCharCode(Internal % ToBase + 55) + Result
else Result = Internal % ToBase + Result
Internal = Math.floor(Internal / ToBase)
}
return Result
},
"choose" : function (Total, Choose) {
if (Total == Choose || Total == 0 || Choose == 0) return 1
if (Choose > Total) return 0
Result = eval(Factorial(Total) + "/(" + Factorial(Total - Choose) + "*" + Factorial(Choose) + ")")
return Result
function Factorial (Number) {
var Factorial = ""
for (var i = 1; i < Number; i++) {
Factorial += i + "*"
}
Factorial += Number
return Factorial
}
},
"root" : function (Base, Root) {
if (Root == undefined) Root = 2
return Math.pow(Base, 1 / Root)
}
}
These functions work normally, like "choose(5,4)" will return 5. (Just if you wanted to know, "base" converts bases, "choose" finds the number of ways to choose k objects from n (max: 170, otherwise it returns infinity), and "root" finds the nth root of a number.)
-----
EDIT #2: I also noticed that you can't type negative numbers in your expression...?