2021-05-13 02:43:33 -07:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-07-08 08:23:47 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2019-09-30 01:19:56 -07:00
|
|
|
#include "JSInterface_GUISize.h"
|
2018-04-27 09:48:44 -07:00
|
|
|
|
2006-06-01 19:10:27 -07:00
|
|
|
#include "ps/CStr.h"
|
2018-04-27 09:48:44 -07:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2004-07-08 08:23:47 -07:00
|
|
|
|
|
|
|
|
JSClass JSI_GUISize::JSI_class = {
|
2020-11-18 06:39:04 -08:00
|
|
|
"GUISize", 0, &JSI_GUISize::JSI_classops
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
JSClassOps JSI_GUISize::JSI_classops = {
|
2016-09-02 09:26:54 -07:00
|
|
|
nullptr, nullptr,
|
|
|
|
|
nullptr, nullptr,
|
|
|
|
|
nullptr, nullptr, nullptr, nullptr,
|
2020-11-24 07:47:03 -08:00
|
|
|
nullptr, JSI_GUISize::construct, nullptr
|
2004-07-08 08:23:47 -07:00
|
|
|
};
|
|
|
|
|
|
2015-08-21 10:08:41 -07:00
|
|
|
JSFunctionSpec JSI_GUISize::JSI_methods[] =
|
2004-07-08 08:23:47 -07:00
|
|
|
{
|
2019-01-13 08:37:41 -08:00
|
|
|
JS_FN("toString", JSI_GUISize::toString, 0, 0),
|
2014-03-28 13:26:32 -07:00
|
|
|
JS_FS_END
|
2004-07-08 08:23:47 -07:00
|
|
|
};
|
|
|
|
|
|
2019-09-30 01:19:56 -07:00
|
|
|
void JSI_GUISize::RegisterScriptClass(ScriptInterface& scriptInterface)
|
|
|
|
|
{
|
|
|
|
|
scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 0, nullptr, JSI_GUISize::JSI_methods, nullptr, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 03:27:36 -07:00
|
|
|
bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp)
|
2004-07-08 08:23:47 -07:00
|
|
|
{
|
2014-07-12 09:55:09 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2014-07-13 08:31:48 -07:00
|
|
|
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(*pScriptInterface);
|
2020-11-13 05:18:22 -08:00
|
|
|
|
|
|
|
|
JS::RootedObject obj(rq.cx, pScriptInterface->CreateCustomObject("GUISize"));
|
2010-11-16 15:00:52 -08:00
|
|
|
|
2014-07-12 09:55:09 -07:00
|
|
|
if (args.length() == 8)
|
2004-07-08 08:23:47 -07:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS_SetProperty(rq.cx, obj, "left", args[0]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "top", args[1]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "right", args[2]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "bottom", args[3]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rleft", args[4]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rtop", args[5]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rright", args[6]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rbottom", args[7]);
|
2004-07-11 11:18:54 -07:00
|
|
|
}
|
2014-07-12 09:55:09 -07:00
|
|
|
else if (args.length() == 4)
|
2004-07-11 11:18:54 -07:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue zero(rq.cx, JS::NumberValue(0));
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "left", args[0]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "top", args[1]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "right", args[2]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "bottom", args[3]);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rleft", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rtop", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rright", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rbottom", zero);
|
2004-07-08 08:23:47 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue zero(rq.cx, JS::NumberValue(0));
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "left", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "top", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "right", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "bottom", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rleft", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rtop", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rright", zero);
|
|
|
|
|
JS_SetProperty(rq.cx, obj, "rbottom", zero);
|
2004-07-08 08:23:47 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-12 09:55:09 -07:00
|
|
|
args.rval().setObject(*obj);
|
2015-01-24 06:46:52 -08:00
|
|
|
return true;
|
2004-07-08 08:23:47 -07:00
|
|
|
}
|
|
|
|
|
|
2004-07-11 11:18:54 -07:00
|
|
|
// Produces "10", "-10", "50%", "50%-10", "50%+10", etc
|
2019-09-30 01:19:56 -07:00
|
|
|
CStr JSI_GUISize::ToPercentString(double pix, double per)
|
2004-07-11 11:18:54 -07:00
|
|
|
{
|
|
|
|
|
if (per == 0)
|
2011-02-17 12:08:20 -08:00
|
|
|
return CStr::FromDouble(pix);
|
2015-08-21 10:08:41 -07:00
|
|
|
|
|
|
|
|
return CStr::FromDouble(per)+"%"+(pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr::FromDouble(pix) : CStr::FromDouble(pix));
|
2004-07-11 11:18:54 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-28 03:27:36 -07:00
|
|
|
bool JSI_GUISize::toString(JSContext* cx, uint argc, JS::Value* vp)
|
2004-07-08 08:23:47 -07:00
|
|
|
{
|
2019-08-13 07:11:43 -07:00
|
|
|
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
|
2004-09-03 07:12:43 -07:00
|
|
|
CStr buffer;
|
|
|
|
|
|
2019-09-04 09:15:37 -07:00
|
|
|
ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface;
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(*pScriptInterface);
|
2019-09-04 09:15:37 -07:00
|
|
|
double val, valr;
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
#define SIDE(side) \
|
2019-08-13 07:11:43 -07:00
|
|
|
pScriptInterface->GetProperty(args.thisv(), #side, val); \
|
|
|
|
|
pScriptInterface->GetProperty(args.thisv(), "r"#side, valr); \
|
2015-08-21 10:08:41 -07:00
|
|
|
buffer += ToPercentString(val, valr);
|
|
|
|
|
|
2019-09-04 09:15:37 -07:00
|
|
|
SIDE(left);
|
|
|
|
|
buffer += " ";
|
|
|
|
|
SIDE(top);
|
|
|
|
|
buffer += " ";
|
|
|
|
|
SIDE(right);
|
|
|
|
|
buffer += " ";
|
|
|
|
|
SIDE(bottom);
|
2004-09-03 07:12:43 -07:00
|
|
|
#undef SIDE
|
2019-09-04 09:15:37 -07:00
|
|
|
|
2021-05-13 02:43:33 -07:00
|
|
|
Script::ToJSVal(rq, args.rval(), buffer);
|
2015-01-24 06:46:52 -08:00
|
|
|
return true;
|
2004-07-08 08:23:47 -07:00
|
|
|
}
|