Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / gui / interface_expr_user_fct.cpp
blobe2bb589e1dcc188ca5393c2d1aeb85a791cb14f1
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2019-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "stdpch.h"
23 #include "nel/gui/interface_expr.h"
24 #include "nel/gui/interface_link.h"
25 #include "nel/gui/interface_element.h"
26 #include "nel/gui/db_manager.h"
27 #include "nel/misc/i18n.h"
29 using namespace std;
30 using namespace NLMISC;
32 #ifdef DEBUG_NEW
33 #define new DEBUG_NEW
34 #endif
36 namespace NLGUI
39 void ifexprufct_forcelink()
44 // alias to CInterfaceExprValue
45 typedef CInterfaceExprValue CIEV;
47 /** takes arguments of a binary operator, and promote them to the best type.
48 * string are not supported
49 * \return true
51 bool promoteToNumericalBestType(CInterfaceExpr::TArgList &_list)
53 uint i;
54 bool bIsNumerical = true;
56 for (i = 0 ; i < _list.size(); ++i)
57 if (!_list[i].isNumerical())
58 bIsNumerical = false;
60 if (bIsNumerical)
62 bool bDouble = false;
64 for (i = 0 ; i < _list.size(); ++i)
65 if (_list[i].getType() == CIEV::Double)
66 bDouble = true;
68 if (bDouble)
70 for (i = 0 ; i < _list.size(); ++i)
71 _list[i].toDouble();
73 else
75 for (i = 0 ; i < _list.size(); ++i)
76 _list[i].toInteger();
78 return true;
81 return false;
85 /////////////////////
86 // ADD 2 numbers //
87 /////////////////////
89 static DECLARE_INTERFACE_USER_FCT(userFctAdd)
91 if (!promoteToNumericalBestType(args))
93 nlwarning("add : invalid entry");
94 return false;
96 switch(args[0].getType())
98 case CIEV::Integer: result.setInteger(args[0].getInteger() + args[1].getInteger()); return true;
99 case CIEV::Double: result.setDouble(args[0].getDouble() + args[1].getDouble()); return true;
100 default: break;
102 return false;
104 REGISTER_INTERFACE_USER_FCT("add", userFctAdd)
106 /////////////////////
107 // SUB 2 numbers //
108 /////////////////////
110 static DECLARE_INTERFACE_USER_FCT(userFctSub)
112 if (!promoteToNumericalBestType(args))
114 nlwarning("sub : invalid entry");
115 return false;
117 switch(args[0].getType())
119 case CIEV::Integer: result.setInteger(args[0].getInteger() - args[1].getInteger()); return true;
120 case CIEV::Double: result.setDouble(args[0].getDouble() - args[1].getDouble()); return true;
121 default: break;
123 return false;
125 REGISTER_INTERFACE_USER_FCT("sub", userFctSub)
127 ///////////////////
128 // MUL 2 numbers //
129 ///////////////////
131 static DECLARE_INTERFACE_USER_FCT(userFctMul)
133 if (!promoteToNumericalBestType(args))
135 nlwarning("mul : invalid entry");
136 return false;
138 switch(args[0].getType())
140 case CIEV::Integer: result.setInteger(args[0].getInteger() * args[1].getInteger()); return true;
141 case CIEV::Double: result.setDouble(args[0].getDouble() * args[1].getDouble()); return true;
142 default: break;
144 return false;
146 REGISTER_INTERFACE_USER_FCT("mul", userFctMul)
148 ///////////////////
149 // DIV 2 numbers //
150 ///////////////////
152 static DECLARE_INTERFACE_USER_FCT(userFctDiv)
154 if (args.size() != 2 || !args[0].isNumerical() || !args[1].isNumerical())
156 nlwarning("div: bad arguments");
157 return false;
159 args[0].toDouble();
160 args[1].toDouble();
161 if (args[1].getDouble() == 0)
163 nlwarning("div: zero divide");
164 return false;
166 result.setDouble(args[0].getDouble() / args[1].getDouble());
167 return true;
169 REGISTER_INTERFACE_USER_FCT("div", userFctDiv)
171 ///////////////////
172 // MOD 2 numbers //
173 ///////////////////
175 static DECLARE_INTERFACE_USER_FCT(userFctMod)
177 if (args.size() != 2 || !args[0].isNumerical() || !args[1].isNumerical())
179 nlwarning("mod: bad arguments");
180 return false;
182 args[0].toDouble();
183 args[1].toDouble();
184 if (args[1].getDouble() == 0)
186 nlwarning("mod: zero divide");
187 return false;
189 result.setDouble(fmod(args[0].getDouble(), args[1].getDouble()));
190 return true;
192 REGISTER_INTERFACE_USER_FCT("mod", userFctMod)
194 ///////////////////
195 // abs 1 number //
196 ///////////////////
198 static DECLARE_INTERFACE_USER_FCT(userFctAbs)
200 if (args.size() != 1 || !args[0].isNumerical())
202 nlwarning("abs: bad arguments");
203 return false;
205 args[0].toDouble();
206 result.setDouble(fabs(args[0].getDouble()));
207 return true;
209 REGISTER_INTERFACE_USER_FCT("abs", userFctAbs)
211 ////////////////////////////////////
212 // Identity (copy its first arg) //
213 ////////////////////////////////////
215 static DECLARE_INTERFACE_USER_FCT(userFctIdentity)
217 if (!args.empty())
219 result = args[0];
220 return true;
222 else
224 return false;
227 REGISTER_INTERFACE_USER_FCT("identity", userFctIdentity)
230 //////////////////////////////////////////////////////////////////////////////////////////////////////
231 // Evaluate all of the args, and return 0, so may be used to create dependances on database entries //
232 //////////////////////////////////////////////////////////////////////////////////////////////////////
233 static DECLARE_INTERFACE_USER_FCT(userFctDepends)
235 result.setInteger(0);
236 return true;
238 REGISTER_INTERFACE_USER_FCT("depends", userFctDepends)
241 //////////////////////////
242 // comparison operators //
243 //////////////////////////
245 /** ugly macro to declare comparison operator in a 'compact' way
246 * this can compare number vs number & string vs string
248 #define CREATE_CMP_OPERATOR(op, name) \
249 static DECLARE_INTERFACE_USER_FCT(userFct##name) \
252 if (args.size() != 2) \
254 nlwarning("comparison : bad number of arguments"); \
255 return false; \
257 if (args[0].getType() == CIEV::String && args[1].getType() == CIEV::String) \
259 result.setBool(strcmp(args[0].getString().c_str(), \
260 args[1].getString().c_str()) op 0); \
261 return true; \
263 if (args.size() != 2 || !args[0].isNumerical() || !args[1].isNumerical()) \
265 nlwarning("comparison : arguments are not numerical"); \
266 return false; \
268 args[0].toDouble(); \
269 args[1].toDouble(); \
270 result.setBool(args[0].getDouble() op args[1].getDouble()); \
271 return true; \
273 REGISTER_INTERFACE_USER_FCT(#name, userFct##name)
277 // declare all comparison operators
278 //CREATE_CMP_OPERATOR(==, eq)
279 CREATE_CMP_OPERATOR(!=, ne)
280 CREATE_CMP_OPERATOR(==, eq)
281 CREATE_CMP_OPERATOR(< , lt)
282 CREATE_CMP_OPERATOR(<=, le)
283 CREATE_CMP_OPERATOR(> , gt)
284 CREATE_CMP_OPERATOR(>=, ge)
288 //////////////////
289 // Logical OR //
290 //////////////////
291 static DECLARE_INTERFACE_USER_FCT(userFctOr)
293 for(uint k = 0; k < args.size(); ++k)
295 if (!args[k].toBool())
297 nlwarning("Argument is not boolean");
298 return false;
300 if (args[k].getBool())
302 result.setBool(true);
303 return true;
306 result.setBool(false);
307 return true;
309 REGISTER_INTERFACE_USER_FCT("or", userFctOr)
311 //////////////////
312 // Logical AND //
313 //////////////////
314 static DECLARE_INTERFACE_USER_FCT(userFctAnd)
316 for(uint k = 0; k < args.size(); ++k)
318 if (!args[k].toBool())
320 nlwarning("Argument is not boolean");
321 return false;
323 if (!args[k].getBool())
325 result.setBool(false);
326 return true;
329 result.setBool(true);
330 return true;
332 REGISTER_INTERFACE_USER_FCT("and", userFctAnd)
334 //////////////////
335 // Logical NOT //
336 //////////////////
337 static DECLARE_INTERFACE_USER_FCT(userFctNot)
339 if (args.size() != 1)
341 nlwarning("not : bad number of arguments");
342 return false;
344 if (!args[0].toBool())
346 nlwarning("Argument is not boolean");
347 return false;
349 result.setBool(!args[0].getBool());
350 return true;
352 REGISTER_INTERFACE_USER_FCT("not", userFctNot)
355 ////////////////////////////////////
356 // String Add Operation //
357 ////////////////////////////////////
359 static DECLARE_INTERFACE_USER_FCT(userFctStr)
361 if (!args.empty())
363 string res;
364 for (uint32 i = 0; i < args.size(); ++i)
366 args[i].toString();
367 res += args[i].getString();
369 result.setString (res);
371 return true;
373 else
375 return false;
378 REGISTER_INTERFACE_USER_FCT("str", userFctStr)
380 ////////////////////////////////////
381 // Integer Operation //
382 ////////////////////////////////////
384 static DECLARE_INTERFACE_USER_FCT(userFctInt)
386 if (args.size() != 1)
388 return false;
391 args[0].toInteger();
392 result.setInteger(args[0].getInteger());
393 return true;
395 REGISTER_INTERFACE_USER_FCT("int", userFctInt)
397 ////////////////////////////////////
398 // Branching Operation ifthenelse //
399 ////////////////////////////////////
401 static DECLARE_INTERFACE_USER_FCT(userFctIfThenElse)
403 if ((args.size() < 1) || (args.size() > 3))
404 return false;
406 if (!args[0].toBool())
407 return false;
409 if (args[0].getBool())
411 result = args[1];
413 else
415 if (args.size() == 3)
416 result = args[2];
417 else
418 result.setBool (false);
421 return true;
423 REGISTER_INTERFACE_USER_FCT("ifthenelse", userFctIfThenElse)
425 ////////////////////////////////
426 // Branching Operation switch //
427 ////////////////////////////////
429 static DECLARE_INTERFACE_USER_FCT(userFctSwitch)
431 if (args.size() < 2)
432 return false;
434 if (!args[0].toInteger())
435 return false;
437 sint64 n = args[0].getInteger();
438 if ((n > ((sint64)args.size()-2)) || (n < 0))
439 return false;
441 result = args[(uint)n+1];
443 return true;
445 REGISTER_INTERFACE_USER_FCT("switch", userFctSwitch)
447 /////////////////////////////////
448 // Takes maximum of any numbers //
449 /////////////////////////////////
450 static DECLARE_INTERFACE_USER_FCT(userFctMax)
452 // compute type of output
453 if (!promoteToNumericalBestType(args))
455 nlwarning("max : invalid entry");
456 return false;
458 uint i;
459 if (args[0].getType() == CIEV::Integer)
461 sint64 m;
462 m = args[0].getInteger();
463 for (i = 1; i < args.size(); ++i)
464 m = std::max(m,args[i].getInteger());
465 result.setInteger(m);
467 else
469 double m;
470 m = args[0].getDouble();
471 for (i = 1; i < args.size(); ++i)
472 m = std::max(m,args[i].getDouble());
473 result.setDouble(m);
475 return true;
477 REGISTER_INTERFACE_USER_FCT("max", userFctMax)
479 /////////////////////////////////
480 // Takes minimum of 2 numbers //
481 /////////////////////////////////
482 static DECLARE_INTERFACE_USER_FCT(userFctMin)
484 // compute type of output
485 if (!promoteToNumericalBestType(args))
487 nlwarning("max : invalid entry");
488 return false;
490 uint i;
491 if (args[0].getType() == CIEV::Integer)
493 sint64 m;
494 m = args[0].getInteger();
495 for (i = 1; i < args.size(); ++i)
496 m = std::min(m,args[i].getInteger());
497 result.setInteger(m);
499 else
501 double m;
502 m = args[0].getDouble();
503 for (i = 1; i < args.size(); ++i)
504 m = std::min(m,args[i].getDouble());
505 result.setDouble(m);
507 return true;
509 REGISTER_INTERFACE_USER_FCT("min", userFctMin)
511 //////////////////////////////
512 // Get Reflected Property //
513 //////////////////////////////
514 static DECLARE_INTERFACE_USER_FCT(userFctGetProp)
516 if (args.size() != 1)
517 return false;
519 if (args[0].getType() != CIEV::String)
520 return false;
522 string sTmp = args[0].getString();
523 std::vector<CInterfaceLink::CTargetInfo> targetsVector;
524 CInterfaceLink::splitLinkTargets(sTmp, NULL, targetsVector);
526 if (targetsVector.empty())
528 nlwarning("no target found");
529 return false;
531 CInterfaceLink::CTargetInfo &rTI = targetsVector[0];
533 CInterfaceElement *elem = rTI.Elem;
534 if (!elem)
536 nlwarning("<CInterfaceExpr::getprop> : Element is NULL");
537 return false;
539 const CReflectedProperty *pRP = elem->getReflectedProperty(rTI.PropertyName);
541 if (!pRP) return false;
542 switch(pRP->Type)
544 case CReflectedProperty::Boolean:
545 result.setBool ((elem->*(pRP->GetMethod.GetBool))());
546 break;
547 case CReflectedProperty::SInt32:
548 result.setInteger ((elem->*(pRP->GetMethod.GetSInt32))());
549 break;
550 case CReflectedProperty::Float:
551 result.setDouble ((elem->*(pRP->GetMethod.GetFloat))());
552 break;
553 case CReflectedProperty::String:
554 result.setString ((elem->*(pRP->GetMethod.GetString))());
555 break;
556 #ifdef RYZOM_LUA_UCSTRING
557 case CReflectedProperty::UCString:
558 result.setString ((elem->*(pRP->GetMethod.GetUCString))().toUtf8());
559 break;
560 #endif
561 case CReflectedProperty::StringRef:
562 result.setString ((elem->*(pRP->GetMethod.GetStringRef))());
563 break;
564 #ifdef RYZOM_LUA_UCSTRING
565 case CReflectedProperty::UCStringRef:
566 result.setString ((elem->*(pRP->GetMethod.GetUCStringRef))().toUtf8());
567 break;
568 #endif
569 case CReflectedProperty::RGBA:
570 result.setRGBA ((elem->*(pRP->GetMethod.GetRGBA))());
571 break;
572 default:
573 nlstop;
574 return false;
576 return true;
578 REGISTER_INTERFACE_USER_FCT("getprop", userFctGetProp)
581 ///////////////////////////////
582 // Convert an int to a color //
583 ///////////////////////////////
584 static DECLARE_INTERFACE_USER_FCT(userIntToColor)
586 if (args.size() != 1)
588 nlwarning("Bad number of args");
589 return false;
591 if (!args[0].toInteger())
593 nlwarning("Bad type for arg 0 : should be an integer");
594 return false;
596 CRGBA col;
597 uint32 intCol = (uint32) args[0].getInteger();
598 col.R = uint8(intCol & 0xff);
599 col.G = uint8((intCol >> 8) & 0xff);
600 col.B = uint8((intCol >> 16) & 0xff);
601 col.A = uint8((intCol >> 24) & 0xff);
602 result.setRGBA(col);
603 return true;
605 REGISTER_INTERFACE_USER_FCT("intToColor", userIntToColor)
607 ///////////////////////////////
608 // Get components of a color //
609 ///////////////////////////////
610 static DECLARE_INTERFACE_USER_FCT(userGetRed)
612 if (args.size() != 1)
614 nlwarning("Bad number of args");
615 return false;
617 if (!args[0].toRGBA())
619 nlwarning("Bad type for arg 0 : should be a color");
620 return false;
622 result.setInteger((sint64) args[0].getRGBA().R);
623 return true;
625 REGISTER_INTERFACE_USER_FCT("getRed", userGetRed)
627 static DECLARE_INTERFACE_USER_FCT(userGetGreen)
629 if (args.size() != 1)
631 nlwarning("Bad number of args");
632 return false;
634 if (!args[0].toRGBA())
636 nlwarning("Bad type for arg 0 : should be a color");
637 return false;
639 result.setInteger((sint64) args[0].getRGBA().G);
640 return true;
642 REGISTER_INTERFACE_USER_FCT("getGreen", userGetGreen)
644 static DECLARE_INTERFACE_USER_FCT(userGetBlue)
646 if (args.size() != 1)
648 nlwarning("Bad number of args");
649 return false;
651 if (!args[0].toRGBA())
653 nlwarning("Bad type for arg 0 : should be a color");
654 return false;
656 result.setInteger((sint64) args[0].getRGBA().B);
657 return true;
659 REGISTER_INTERFACE_USER_FCT("getBlue", userGetBlue)
661 static DECLARE_INTERFACE_USER_FCT(userGetAlpha)
663 if (args.size() != 1)
665 nlwarning("Bad number of args");
666 return false;
668 if (!args[0].toRGBA())
670 nlwarning("Bad type for arg 0 : should be a color");
671 return false;
673 result.setInteger((sint64) args[0].getRGBA().A);
674 return true;
676 REGISTER_INTERFACE_USER_FCT("getAlpha", userGetAlpha)
678 ////////////////////////////////////////////////
679 // make a rgb color from 3 components R, G, B //
680 ////////////////////////////////////////////////
681 static DECLARE_INTERFACE_USER_FCT(userMakeRGB)
683 if ((args.size() != 3) && (args.size() != 4))
685 nlwarning("Bad number of args : 3 or 4 args required : R, G, B, [A]");
686 return false;
688 if (!args[0].toInteger() || !args[1].toInteger() || !args[2].toInteger())
690 nlwarning("Not all args converting to integer");
691 return false;
693 uint8 nAlpha = 255;
694 if (args.size() == 4 )
695 nAlpha = (uint8)args[3].getInteger();
697 NLMISC::CRGBA col((uint8) args[0].getInteger(), (uint8) args[1].getInteger(), (uint8) args[2].getInteger(),nAlpha);
698 result.setRGBA(col);
700 return true;
702 REGISTER_INTERFACE_USER_FCT("makeRGB", userMakeRGB)
705 //////////////////////////////
706 // Get number of DB entries //
707 //////////////////////////////
708 static DECLARE_INTERFACE_USER_FCT(userDBCount)
710 if (args.size() != 1)
711 return false;
713 if (args[0].getType() != CIEV::String)
714 return false;
716 string sTmp = args[0].getString();
718 if (sTmp.find('$') == string::npos)
719 return false;
721 string sFirstPart = sTmp.substr(0,sTmp.find('$'));
722 string sSecondPart = sTmp.substr(sTmp.find('$')+1,sTmp.size());
724 sint i = 0;
725 bool bExit = false;
727 while (!bExit)
729 sTmp = sFirstPart + NLMISC::toString(i) + sSecondPart;
730 CCDBNodeLeaf *pNL = NLGUI::CDBManager::getInstance()->getDbProp(sTmp,false);
731 CCDBNodeBranch *pNB = NLGUI::CDBManager::getInstance()->getDbBranch(sTmp);
732 if (pNL != NULL)
734 if (pNL->getValue64() == 0)
735 bExit = true;
736 else
737 ++i;
739 else if (pNB != NULL)
741 ++i;
743 else
745 bExit = true;
749 result.setInteger(i);
751 return true;
753 REGISTER_INTERFACE_USER_FCT("dbcount", userDBCount)
755 ////////////////////////
756 // Get a random value //
757 ////////////////////////
758 static DECLARE_INTERFACE_USER_FCT(userFctRand)
760 if ((args.size() != 2) && (args.size() != 0))
761 return false;
763 if (args.size() == 2)
765 if (!args[0].toDouble()) return false;
766 if (!args[1].toDouble()) return false;
768 double s = args[0].getDouble();
769 double e = 0.999+args[1].getDouble();
771 result.setDouble ( s + (e-s)*NLMISC::frand(1.0));
773 else
775 result.setDouble (NLMISC::frand(1.0));
778 return true;
780 REGISTER_INTERFACE_USER_FCT("rand", userFctRand)
783 static DECLARE_INTERFACE_USER_FCT(userFctGetBit)
785 if (args.size() != 2)
786 return false;
788 if (!args[0].toInteger()) return false;
789 if (!args[1].toInteger()) return false;
791 sint64 i = args[0].getInteger();
792 sint64 s = args[1].getInteger();
794 result.setInteger (0);
796 if ((i & (SINT64_CONSTANT(1)<<s)) != 0)
797 result.setInteger (1);
799 return true;
801 REGISTER_INTERFACE_USER_FCT("getbit", userFctGetBit)
806 // ***********************
807 // INTERPOLATION FUNCTIONS
808 // ***********************
810 //////////////
811 // i_linear //
812 //////////////
814 static DECLARE_INTERFACE_USER_FCT(userFctILinear)
816 if (args.size() != 3)
817 return false;
819 if (!args[0].toDouble()) return false;
820 if (!args[1].toDouble()) return false;
821 if (!args[2].toDouble()) return false;
823 double i = args[0].getDouble(); // Interpolant
824 double s = args[1].getDouble(); // Start
825 double e = args[2].getDouble(); // End
827 result.setDouble ( s + i * (e - s) );
829 return true;
831 REGISTER_INTERFACE_USER_FCT("ilinear", userFctILinear)
835 //////////////////
836 // Bitwise OR //
837 //////////////////
838 static DECLARE_INTERFACE_USER_FCT(userFctBitOr)
840 uint64 res= 0;
841 for(uint k = 0; k < args.size(); ++k)
843 if (!args[k].toInteger())
845 nlwarning("Argument is not integer");
846 return false;
848 res|= args[k].getInteger();
850 result.setInteger(res);
851 return true;
853 REGISTER_INTERFACE_USER_FCT("bor", userFctBitOr)
855 //////////////////
856 // Bitwise AND //
857 //////////////////
858 static DECLARE_INTERFACE_USER_FCT(userFctBitAnd)
860 if(args.empty())
862 result.setInteger(0);
863 return true;
866 uint64 res= UINT64_CONSTANT(0xFFFFFFFFFFFFFFFF);
867 for(uint k = 0; k < args.size(); ++k)
869 if (!args[k].toInteger())
871 nlwarning("Argument is not integer");
872 return false;
874 res&= args[k].getInteger();
876 result.setInteger(res);
877 return true;
879 REGISTER_INTERFACE_USER_FCT("band", userFctBitAnd)
881 //////////////////
882 // Bitwise NOT //
883 //////////////////
884 static DECLARE_INTERFACE_USER_FCT(userFctBitNot)
886 if (args.size() != 1)
888 nlwarning("bnot : bad number of arguments");
889 return false;
891 if (!args[0].toInteger())
893 nlwarning("Argument is not integer");
894 return false;
896 result.setInteger(~args[0].getInteger());
897 return true;
899 REGISTER_INTERFACE_USER_FCT("bnot", userFctBitNot)
901 //////////////////
902 // Bitwise XOR //
903 //////////////////
904 static DECLARE_INTERFACE_USER_FCT(userFctBitXor)
906 if (args.size() != 2)
908 nlwarning("bxor : bad number of arguments");
909 return false;
911 if (!args[0].toInteger() || !args[1].toInteger())
913 nlwarning("Argument is not integer");
914 return false;
916 result.setInteger(args[0].getInteger() ^ args[1].getInteger());
917 return true;
919 REGISTER_INTERFACE_USER_FCT("bxor", userFctBitXor)
921 ///////////////////////////
922 // Unsigned right shift //
923 ///////////////////////////
924 static DECLARE_INTERFACE_USER_FCT(userFctSHR)
926 if (args.size() != 2)
928 nlwarning("shr : bad number of arguments");
929 return false;
931 if (!args[0].toInteger() || !args[1].toInteger())
933 nlwarning("Argument is not integer");
934 return false;
936 result.setInteger(((uint64)args[0].getInteger()) >> args[1].getInteger());
937 return true;
939 REGISTER_INTERFACE_USER_FCT("shr", userFctSHR)
941 //////////////////////////
942 // Unsigned left shift //
943 //////////////////////////
944 static DECLARE_INTERFACE_USER_FCT(userFctSHL)
946 if (args.size() != 2)
948 nlwarning("shl : bad number of arguments");
949 return false;
951 if (!args[0].toInteger() || !args[1].toInteger())
953 nlwarning("Argument is not integer");
954 return false;
956 result.setInteger(((uint64)args[0].getInteger()) << args[1].getInteger());
957 return true;
959 REGISTER_INTERFACE_USER_FCT("shl", userFctSHL)
961 /////////////////////////
962 // Signed right shift //
963 /////////////////////////
964 static DECLARE_INTERFACE_USER_FCT(userFctSAR)
966 if (args.size() != 2)
968 nlwarning("sar : bad number of arguments");
969 return false;
971 if (!args[0].toInteger() || !args[1].toInteger())
973 nlwarning("Argument is not integer");
974 return false;
976 result.setInteger(args[0].getInteger() >> args[1].getInteger());
977 return true;
979 REGISTER_INTERFACE_USER_FCT("sar", userFctSAR)
981 ////////////////////////
982 // Signed left shift //
983 ////////////////////////
984 static DECLARE_INTERFACE_USER_FCT(userFctSAL)
986 if (args.size() != 2)
988 nlwarning("sal : bad number of arguments");
989 return false;
991 if (!args[0].toInteger() || !args[1].toInteger())
993 nlwarning("Argument is not integer");
994 return false;
996 result.setInteger(args[0].getInteger() << args[1].getInteger());
997 return true;
999 REGISTER_INTERFACE_USER_FCT("sal", userFctSAL)
1001 /////////////////////////////////////////////////////////////////////////////
1002 // Extend the sign of the argument considered over 8 bits to a 64 bits int //
1003 /////////////////////////////////////////////////////////////////////////////
1004 static DECLARE_INTERFACE_USER_FCT(extSign8To64)
1006 if (args.size() != 1)
1008 nlwarning("extSign8To64 : bad number of arguments");
1009 return false;
1011 if (!args[0].toInteger())
1013 nlwarning("Argument is not integer");
1014 return false;
1016 sint8 i = (sint8)args[0].getInteger();
1017 result.setInteger((sint64)i);
1018 return true;
1020 REGISTER_INTERFACE_USER_FCT("extSign8To64", extSign8To64)
1022 //////////////////////////////////////////////////////////////////////////////
1023 // Extend the sign of the argument considered over 11 bits to a 64 bits int //
1024 //////////////////////////////////////////////////////////////////////////////
1025 static DECLARE_INTERFACE_USER_FCT(extSign11To64)
1027 if (args.size() != 1)
1029 nlwarning("extSign12To64 : bad number of arguments");
1030 return false;
1032 if (!args[0].toInteger())
1034 nlwarning("Argument is not integer");
1035 return false;
1037 sint32 i = (sint16)args[0].getInteger() & 0x7ff;
1038 if( i > 1023 )
1039 i |= 0xfffff800;
1040 result.setInteger((sint64)i);
1041 return true;
1043 REGISTER_INTERFACE_USER_FCT("extSign11To64", extSign11To64)
1045 ////////////////////////
1046 // Ryzom version info //
1047 ////////////////////////
1048 static DECLARE_INTERFACE_USER_FCT(isFinalVersion)
1050 if (!args.empty())
1052 nlwarning("isFinalVersion : no args required");
1053 return false;
1055 #if FINAL_VERSION
1056 result.setBool(true);
1057 #else
1058 result.setBool(false);
1059 #endif
1060 return true;
1062 REGISTER_INTERFACE_USER_FCT("isFinalVersion", isFinalVersion)
1064 ////////////////////////
1065 ////////////////////////
1066 static DECLARE_INTERFACE_USER_FCT(secondsToTimeString)
1068 if (args.size() != 1)
1070 nlwarning("intToTimeString : 1 args required");
1071 return false;
1073 if (!args[0].toInteger())
1075 nlwarning("intToTimeString : args 0 required to be an int");
1076 return false;
1079 sint64 nVal = args[0].getInteger();
1080 string sTmp;
1082 if (nVal < 0) nVal = 0;
1084 sTmp = toString(nVal % 60) + " " + CI18N::get("uiMissionTimerSecond");
1086 nVal = nVal / 60;
1088 if( nVal > 0 )
1090 sTmp = toString(nVal % 60) + " " + CI18N::get("uiMissionTimerMinute") + " " + sTmp;
1092 nVal = nVal / 60;
1094 if( nVal > 0 )
1096 sTmp = toString(nVal % 24) + " " + CI18N::get("uiMissionTimerHour") + " " + sTmp;
1098 nVal = nVal / 24;
1100 if( nVal > 0 )
1102 sTmp = toString(nVal) + " " + CI18N::get("uiMissionTimerDay") + " " + sTmp;
1107 result.setString(sTmp);
1109 return true;
1111 REGISTER_INTERFACE_USER_FCT("secondsToTimeString", secondsToTimeString)
1113 ////////////////////////
1114 ////////////////////////
1115 static DECLARE_INTERFACE_USER_FCT(secondsToTimeStringShort)
1117 if (args.size() != 1)
1119 nlwarning("intToTimeString : 1 args required");
1120 return false;
1122 if (!args[0].toInteger())
1124 nlwarning("intToTimeString : args 0 required to be an int");
1125 return false;
1128 sint64 nVal = args[0].getInteger();
1129 string sTmp;
1131 if (nVal < 0) nVal = 0;
1133 sTmp = toString("%02d", nVal % 60);
1135 nVal = nVal / 60;
1137 if( nVal > 0 )
1139 sTmp = toString(nVal % 60) + "'" + sTmp;
1141 nVal = nVal / 60;
1143 // if at least one hour, just display number of hour
1144 if( nVal > 0 )
1146 sTmp = toString(nVal % 24) + "h";
1148 nVal = nVal / 24;
1150 // if at least one hour, just display number of days
1151 if( nVal > 0 )
1153 sTmp = toString(nVal) + "d";
1158 result.setString(sTmp);
1160 return true;
1162 REGISTER_INTERFACE_USER_FCT("secondsToTimeStringShort", secondsToTimeStringShort)
1164 ////////////////////////
1165 ////////////////////////
1166 static DECLARE_INTERFACE_USER_FCT(oldvalue)
1168 if (args.size() != 1)
1170 nlwarning("oldvalue : 1 arg required");
1171 return false;
1173 CCDBNodeLeaf *nl = NLGUI::CDBManager::getInstance()->getDbProp(args[0].getString());
1174 if (!nl)
1176 nlwarning("oldvalue : arg 0 required to be an interface leaf");
1177 return false;
1179 result.setInteger (nl->getOldValue64());
1181 return true;
1183 REGISTER_INTERFACE_USER_FCT("oldvalue", oldvalue)
1185 ////////////////////////
1186 ////////////////////////
1187 static DECLARE_INTERFACE_USER_FCT(localize)
1189 if (args.size() != 1)
1191 nlwarning("localize : 1 arg required");
1192 return false;
1194 result.setString(CI18N::get(args[0].getString()));
1195 return true;
1197 REGISTER_INTERFACE_USER_FCT("localize", localize);