Index: ps/trunk/source/gui/GUIbase.cpp =================================================================== --- ps/trunk/source/gui/GUIbase.cpp +++ ps/trunk/source/gui/GUIbase.cpp @@ -19,7 +19,7 @@ #include "GUIbase.h" -#include "gui/scripting/JSInterface_GUITypes.h" +#include "gui/scripting/JSInterface_GUISize.h" #include "ps/CLogger.h" CClientArea::CClientArea() : pixel(0.f,0.f,0.f,0.f), percent(0.f,0.f,0.f,0.f) Index: ps/trunk/source/gui/IGUIObject.cpp =================================================================== --- ps/trunk/source/gui/IGUIObject.cpp +++ ps/trunk/source/gui/IGUIObject.cpp @@ -17,12 +17,10 @@ #include "precompiled.h" -#include "gui/IGUIObject.h" +#include "IGUIObject.h" #include "gui/CGUI.h" #include "gui/CGUISetting.h" -#include "gui/scripting/JSInterface_GUITypes.h" -#include "gui/scripting/JSInterface_IGUIObject.h" #include "ps/CLogger.h" #include "ps/GameSetup/Config.h" #include "ps/Profile.h" Index: ps/trunk/source/gui/scripting/JSInterface_GUISize.h =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_GUISize.h +++ ps/trunk/source/gui/scripting/JSInterface_GUISize.h @@ -0,0 +1,38 @@ +/* Copyright (C) 2019 Wildfire Games. + * 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 . + */ + +#ifndef INCLUDED_JSI_GUISIZE +#define INCLUDED_JSI_GUISIZE + +#include "scriptinterface/ScriptInterface.h" +#include "ps/CStr.h" + +namespace JSI_GUISize +{ + extern JSClass JSI_class; + extern JSPropertySpec JSI_props[]; + extern JSFunctionSpec JSI_methods[]; + + void RegisterScriptClass(ScriptInterface& scriptInterface); + + bool construct(JSContext* cx, uint argc, JS::Value* vp); + bool toString(JSContext* cx, uint argc, JS::Value* vp); + + CStr ToPercentString(double pix, double per); +} + +#endif // INCLUDED_JSI_GUISIZE Index: ps/trunk/source/gui/scripting/JSInterface_GUISize.cpp =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_GUISize.cpp +++ ps/trunk/source/gui/scripting/JSInterface_GUISize.cpp @@ -0,0 +1,125 @@ +/* Copyright (C) 2019 Wildfire Games. + * 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 . + */ + +#include "precompiled.h" + +#include "JSInterface_GUISize.h" + +#include "ps/CStr.h" +#include "scriptinterface/ScriptInterface.h" + +JSClass JSI_GUISize::JSI_class = { + "GUISize", 0, + nullptr, nullptr, + nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, JSI_GUISize::construct, nullptr +}; + +JSFunctionSpec JSI_GUISize::JSI_methods[] = +{ + JS_FN("toString", JSI_GUISize::toString, 0, 0), + JS_FS_END +}; + +void JSI_GUISize::RegisterScriptClass(ScriptInterface& scriptInterface) +{ + scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 0, nullptr, JSI_GUISize::JSI_methods, nullptr, nullptr); +} + +bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp) +{ + JSAutoRequest rq(cx); + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; + JS::RootedObject obj(cx, pScriptInterface->CreateCustomObject("GUISize")); + + if (args.length() == 8) + { + JS_SetProperty(cx, obj, "left", args[0]); + JS_SetProperty(cx, obj, "top", args[1]); + JS_SetProperty(cx, obj, "right", args[2]); + JS_SetProperty(cx, obj, "bottom", args[3]); + JS_SetProperty(cx, obj, "rleft", args[4]); + JS_SetProperty(cx, obj, "rtop", args[5]); + JS_SetProperty(cx, obj, "rright", args[6]); + JS_SetProperty(cx, obj, "rbottom", args[7]); + } + else if (args.length() == 4) + { + JS::RootedValue zero(cx, JS::NumberValue(0)); + JS_SetProperty(cx, obj, "left", args[0]); + JS_SetProperty(cx, obj, "top", args[1]); + JS_SetProperty(cx, obj, "right", args[2]); + JS_SetProperty(cx, obj, "bottom", args[3]); + JS_SetProperty(cx, obj, "rleft", zero); + JS_SetProperty(cx, obj, "rtop", zero); + JS_SetProperty(cx, obj, "rright", zero); + JS_SetProperty(cx, obj, "rbottom", zero); + } + else + { + JS::RootedValue zero(cx, JS::NumberValue(0)); + JS_SetProperty(cx, obj, "left", zero); + JS_SetProperty(cx, obj, "top", zero); + JS_SetProperty(cx, obj, "right", zero); + JS_SetProperty(cx, obj, "bottom", zero); + JS_SetProperty(cx, obj, "rleft", zero); + JS_SetProperty(cx, obj, "rtop", zero); + JS_SetProperty(cx, obj, "rright", zero); + JS_SetProperty(cx, obj, "rbottom", zero); + } + + args.rval().setObject(*obj); + return true; +} + +// Produces "10", "-10", "50%", "50%-10", "50%+10", etc +CStr JSI_GUISize::ToPercentString(double pix, double per) +{ + if (per == 0) + return CStr::FromDouble(pix); + + return CStr::FromDouble(per)+"%"+(pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr::FromDouble(pix) : CStr::FromDouble(pix)); +} + +bool JSI_GUISize::toString(JSContext* cx, uint argc, JS::Value* vp) +{ + // JSAutoRequest not needed for the calls below + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + CStr buffer; + + ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; + double val, valr; + +#define SIDE(side) \ + pScriptInterface->GetProperty(args.thisv(), #side, val); \ + pScriptInterface->GetProperty(args.thisv(), "r"#side, valr); \ + buffer += ToPercentString(val, valr); + + SIDE(left); + buffer += " "; + SIDE(top); + buffer += " "; + SIDE(right); + buffer += " "; + SIDE(bottom); +#undef SIDE + + ScriptInterface::ToJSVal(cx, args.rval(), buffer); + return true; +} Index: ps/trunk/source/gui/scripting/JSInterface_GUITypes.h =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_GUITypes.h +++ ps/trunk/source/gui/scripting/JSInterface_GUITypes.h @@ -1,42 +0,0 @@ -/* Copyright (C) 2019 Wildfire Games. - * 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 . - */ - -#ifndef INCLUDED_JSI_GUITYPES -#define INCLUDED_JSI_GUITYPES - -#include "scriptinterface/ScriptInterface.h" - -#define GUISTDTYPE(x) \ - namespace JSI_GUI##x \ - { \ - extern JSClass JSI_class; \ - extern JSPropertySpec JSI_props[]; \ - extern JSFunctionSpec JSI_methods[]; \ - bool construct(JSContext* cx, uint argc, JS::Value* vp); \ - bool toString(JSContext* cx, uint argc, JS::Value* vp); \ - } - -GUISTDTYPE(Size) - -#undef GUISTDTYPE // avoid unnecessary pollution - -namespace JSI_GUITypes -{ - void init(ScriptInterface& scriptInterface); -} - -#endif // INCLUDED_JSI_GUITYPES Index: ps/trunk/source/gui/scripting/JSInterface_GUITypes.cpp =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_GUITypes.cpp +++ ps/trunk/source/gui/scripting/JSInterface_GUITypes.cpp @@ -1,125 +0,0 @@ -/* Copyright (C) 2019 Wildfire Games. - * 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 . - */ - -#include "precompiled.h" - -#include "JSInterface_GUITypes.h" - -#include "ps/CStr.h" -#include "scriptinterface/ScriptInterface.h" - -JSClass JSI_GUISize::JSI_class = { - "GUISize", 0, - nullptr, nullptr, - nullptr, nullptr, - nullptr, nullptr, nullptr, nullptr, - nullptr, nullptr, JSI_GUISize::construct, nullptr -}; - -JSFunctionSpec JSI_GUISize::JSI_methods[] = -{ - JS_FN("toString", JSI_GUISize::toString, 0, 0), - JS_FS_END -}; - -bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp) -{ - JSAutoRequest rq(cx); - JS::CallArgs args = JS::CallArgsFromVp(argc, vp); - ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; - JS::RootedObject obj(cx, pScriptInterface->CreateCustomObject("GUISize")); - - if (args.length() == 8) - { - JS_SetProperty(cx, obj, "left", args[0]); - JS_SetProperty(cx, obj, "top", args[1]); - JS_SetProperty(cx, obj, "right", args[2]); - JS_SetProperty(cx, obj, "bottom", args[3]); - JS_SetProperty(cx, obj, "rleft", args[4]); - JS_SetProperty(cx, obj, "rtop", args[5]); - JS_SetProperty(cx, obj, "rright", args[6]); - JS_SetProperty(cx, obj, "rbottom", args[7]); - } - else if (args.length() == 4) - { - JS::RootedValue zero(cx, JS::NumberValue(0)); - JS_SetProperty(cx, obj, "left", args[0]); - JS_SetProperty(cx, obj, "top", args[1]); - JS_SetProperty(cx, obj, "right", args[2]); - JS_SetProperty(cx, obj, "bottom", args[3]); - JS_SetProperty(cx, obj, "rleft", zero); - JS_SetProperty(cx, obj, "rtop", zero); - JS_SetProperty(cx, obj, "rright", zero); - JS_SetProperty(cx, obj, "rbottom", zero); - } - else - { - JS::RootedValue zero(cx, JS::NumberValue(0)); - JS_SetProperty(cx, obj, "left", zero); - JS_SetProperty(cx, obj, "top", zero); - JS_SetProperty(cx, obj, "right", zero); - JS_SetProperty(cx, obj, "bottom", zero); - JS_SetProperty(cx, obj, "rleft", zero); - JS_SetProperty(cx, obj, "rtop", zero); - JS_SetProperty(cx, obj, "rright", zero); - JS_SetProperty(cx, obj, "rbottom", zero); - } - - args.rval().setObject(*obj); - return true; -} - -// Produces "10", "-10", "50%", "50%-10", "50%+10", etc -CStr ToPercentString(double pix, double per) -{ - if (per == 0) - return CStr::FromDouble(pix); - - return CStr::FromDouble(per)+"%"+(pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr::FromDouble(pix) : CStr::FromDouble(pix)); -} - -bool JSI_GUISize::toString(JSContext* cx, uint argc, JS::Value* vp) -{ - // JSAutoRequest not needed for the calls below - JS::CallArgs args = JS::CallArgsFromVp(argc, vp); - CStr buffer; - - ScriptInterface* pScriptInterface = ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface; - double val, valr; - -#define SIDE(side) \ - pScriptInterface->GetProperty(args.thisv(), #side, val); \ - pScriptInterface->GetProperty(args.thisv(), "r"#side, valr); \ - buffer += ToPercentString(val, valr); - - SIDE(left); - buffer += " "; - SIDE(top); - buffer += " "; - SIDE(right); - buffer += " "; - SIDE(bottom); -#undef SIDE - - ScriptInterface::ToJSVal(cx, args.rval(), buffer); - return true; -} - -void JSI_GUITypes::init(ScriptInterface& scriptInterface) -{ - scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 1, nullptr, JSI_GUISize::JSI_methods, nullptr, nullptr); -} Index: ps/trunk/source/gui/scripting/JSInterface_IGUIObject.h =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_IGUIObject.h +++ ps/trunk/source/gui/scripting/JSInterface_IGUIObject.h @@ -24,6 +24,9 @@ { extern JSClass JSI_class; extern JSFunctionSpec JSI_methods[]; + + void RegisterScriptClass(ScriptInterface& scriptInterface); + bool getProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp); bool setProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp, JS::ObjectOpResult& result); bool toString(JSContext* cx, uint argc, JS::Value* vp); @@ -31,7 +34,6 @@ bool blur(JSContext* cx, uint argc, JS::Value* vp); bool getComputedSize(JSContext* cx, uint argc, JS::Value* vp); bool getTextSize(JSContext* cx, uint argc, JS::Value* vp); - void init(ScriptInterface& scriptInterface); } #endif // INCLUDED_JSI_IGUIOBJECT Index: ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp =================================================================== --- ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp +++ ps/trunk/source/gui/scripting/JSInterface_IGUIObject.cpp @@ -20,13 +20,8 @@ #include "JSInterface_IGUIObject.h" #include "gui/CGUI.h" -#include "gui/CGUIColor.h" #include "gui/CGUISetting.h" -#include "gui/CList.h" -#include "gui/GUIManager.h" #include "gui/IGUIObject.h" -#include "gui/IGUIScrollBar.h" -#include "gui/scripting/JSInterface_GUITypes.h" #include "scriptinterface/ScriptExtraHeaders.h" #include "scriptinterface/ScriptInterface.h" @@ -47,6 +42,11 @@ JS_FS_END }; +void JSI_IGUIObject::RegisterScriptClass(ScriptInterface& scriptInterface) +{ + scriptInterface.DefineCustomObjectType(&JSI_class, nullptr, 0, nullptr, JSI_methods, nullptr, nullptr); +} + bool JSI_IGUIObject::getProperty(JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp) { JSAutoRequest rq(cx); @@ -174,11 +174,6 @@ return result.fail(JSMSG_UNDEFINED_PROP); } -void JSI_IGUIObject::init(ScriptInterface& scriptInterface) -{ - scriptInterface.DefineCustomObjectType(&JSI_class, nullptr, 1, nullptr, JSI_methods, nullptr, nullptr); -} - bool JSI_IGUIObject::toString(JSContext* cx, uint argc, JS::Value* vp) { // No JSAutoRequest needed for these calls Index: ps/trunk/source/gui/scripting/ScriptFunctions.cpp =================================================================== --- ps/trunk/source/gui/scripting/ScriptFunctions.cpp +++ ps/trunk/source/gui/scripting/ScriptFunctions.cpp @@ -20,9 +20,9 @@ #include "ScriptFunctions.h" #include "graphics/scripting/JSInterface_GameView.h" -#include "gui/IGUIObject.h" #include "gui/scripting/JSInterface_GUIManager.h" -#include "gui/scripting/JSInterface_GUITypes.h" +#include "gui/scripting/JSInterface_GUISize.h" +#include "gui/scripting/JSInterface_IGUIObject.h" #include "i18n/scripting/JSInterface_L10n.h" #include "lobby/scripting/JSInterface_Lobby.h" #include "network/scripting/JSInterface_Network.h" @@ -50,8 +50,8 @@ */ void GuiScriptingInit(ScriptInterface& scriptInterface) { - JSI_IGUIObject::init(scriptInterface); - JSI_GUITypes::init(scriptInterface); + JSI_GUISize::RegisterScriptClass(scriptInterface); + JSI_IGUIObject::RegisterScriptClass(scriptInterface); JSI_ConfigDB::RegisterScriptFunctions(scriptInterface); JSI_Console::RegisterScriptFunctions(scriptInterface);