Line data Source code
1 : /* Copyright (C) 2021 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "JSInterface_GUISize.h"
21 :
22 : #include "ps/CStr.h"
23 : #include "scriptinterface/ScriptInterface.h"
24 : #include "scriptinterface/Object.h"
25 :
26 : JSClass JSI_GUISize::JSI_class = {
27 : "GUISize", 0, &JSI_GUISize::JSI_classops
28 : };
29 :
30 : JSClassOps JSI_GUISize::JSI_classops = {
31 : nullptr, nullptr,
32 : nullptr, nullptr,
33 : nullptr, nullptr, nullptr, nullptr,
34 : nullptr, JSI_GUISize::construct, nullptr
35 : };
36 :
37 : JSFunctionSpec JSI_GUISize::JSI_methods[] =
38 : {
39 : JS_FN("toString", JSI_GUISize::toString, 0, 0),
40 : JS_FS_END
41 : };
42 :
43 12 : void JSI_GUISize::RegisterScriptClass(ScriptInterface& scriptInterface)
44 : {
45 12 : scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 0, nullptr, JSI_GUISize::JSI_methods, nullptr, nullptr);
46 12 : }
47 :
48 0 : bool JSI_GUISize::construct(JSContext* cx, uint argc, JS::Value* vp)
49 : {
50 0 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
51 0 : ScriptRequest rq(cx);
52 0 : const ScriptInterface& scriptInterface = rq.GetScriptInterface();
53 :
54 0 : JS::RootedObject obj(rq.cx, scriptInterface.CreateCustomObject("GUISize"));
55 :
56 0 : if (args.length() == 8)
57 : {
58 0 : JS_SetProperty(rq.cx, obj, "left", args[0]);
59 0 : JS_SetProperty(rq.cx, obj, "top", args[1]);
60 0 : JS_SetProperty(rq.cx, obj, "right", args[2]);
61 0 : JS_SetProperty(rq.cx, obj, "bottom", args[3]);
62 0 : JS_SetProperty(rq.cx, obj, "rleft", args[4]);
63 0 : JS_SetProperty(rq.cx, obj, "rtop", args[5]);
64 0 : JS_SetProperty(rq.cx, obj, "rright", args[6]);
65 0 : JS_SetProperty(rq.cx, obj, "rbottom", args[7]);
66 : }
67 0 : else if (args.length() == 4)
68 : {
69 0 : JS::RootedValue zero(rq.cx, JS::NumberValue(0));
70 0 : JS_SetProperty(rq.cx, obj, "left", args[0]);
71 0 : JS_SetProperty(rq.cx, obj, "top", args[1]);
72 0 : JS_SetProperty(rq.cx, obj, "right", args[2]);
73 0 : JS_SetProperty(rq.cx, obj, "bottom", args[3]);
74 0 : JS_SetProperty(rq.cx, obj, "rleft", zero);
75 0 : JS_SetProperty(rq.cx, obj, "rtop", zero);
76 0 : JS_SetProperty(rq.cx, obj, "rright", zero);
77 0 : JS_SetProperty(rq.cx, obj, "rbottom", zero);
78 : }
79 : else
80 : {
81 0 : JS::RootedValue zero(rq.cx, JS::NumberValue(0));
82 0 : JS_SetProperty(rq.cx, obj, "left", zero);
83 0 : JS_SetProperty(rq.cx, obj, "top", zero);
84 0 : JS_SetProperty(rq.cx, obj, "right", zero);
85 0 : JS_SetProperty(rq.cx, obj, "bottom", zero);
86 0 : JS_SetProperty(rq.cx, obj, "rleft", zero);
87 0 : JS_SetProperty(rq.cx, obj, "rtop", zero);
88 0 : JS_SetProperty(rq.cx, obj, "rright", zero);
89 0 : JS_SetProperty(rq.cx, obj, "rbottom", zero);
90 : }
91 :
92 0 : args.rval().setObject(*obj);
93 0 : return true;
94 : }
95 :
96 : // Produces "10", "-10", "50%", "50%-10", "50%+10", etc
97 0 : CStr JSI_GUISize::ToPercentString(double pix, double per)
98 : {
99 0 : if (per == 0)
100 0 : return CStr::FromDouble(pix);
101 :
102 0 : return CStr::FromDouble(per)+"%"+(pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr::FromDouble(pix) : CStr::FromDouble(pix));
103 : }
104 :
105 0 : bool JSI_GUISize::toString(JSContext* cx, uint argc, JS::Value* vp)
106 : {
107 0 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
108 0 : CStr buffer;
109 :
110 0 : ScriptRequest rq(cx);
111 : double val, valr;
112 :
113 : #define SIDE(side) \
114 : Script::GetProperty(rq, args.thisv(), #side, val); \
115 : Script::GetProperty(rq, args.thisv(), "r"#side, valr); \
116 : buffer += ToPercentString(val, valr);
117 :
118 0 : SIDE(left);
119 0 : buffer += " ";
120 0 : SIDE(top);
121 0 : buffer += " ";
122 0 : SIDE(right);
123 0 : buffer += " ";
124 0 : SIDE(bottom);
125 : #undef SIDE
126 :
127 0 : Script::ToJSVal(rq, args.rval(), buffer);
128 0 : return true;
129 : }
|