Line data Source code
1 : /* Copyright (C) 2026 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/Object.h"
24 : #include "scriptinterface/Conversions.h"
25 : #include "scriptinterface/Interface.h"
26 : #include "scriptinterface/Request.h"
27 :
28 : #include <js/CallArgs.h>
29 : #include <js/Class.h>
30 : #include <js/PropertyAndElement.h>
31 : #include <js/RootingAPI.h>
32 : #include <js/TypeDecls.h>
33 : #include <js/Value.h>
34 : #include <string>
35 :
36 : struct JSContext;
37 :
38 : JSClass JSI_GUISize::JSI_class = {
39 : "GUISize", 0, &JSI_GUISize::JSI_classops
40 : };
41 :
42 : JSClassOps JSI_GUISize::JSI_classops = {
43 : nullptr, nullptr,
44 : nullptr, nullptr,
45 : nullptr, nullptr, nullptr,
46 : nullptr, JSI_GUISize::construct, nullptr
47 : };
48 :
49 : JSFunctionSpec JSI_GUISize::JSI_methods[] =
50 : {
51 : JS_FN("toString", JSI_GUISize::toString, 0, 0),
52 : JS_FS_END
53 : };
54 :
55 26 : void JSI_GUISize::RegisterScriptClass(Script::Interface& scriptInterface)
56 : {
57 26 : scriptInterface.DefineCustomObjectType(&JSI_GUISize::JSI_class, JSI_GUISize::construct, 0, nullptr, JSI_GUISize::JSI_methods, nullptr, nullptr);
58 26 : }
59 :
60 0 : bool JSI_GUISize::construct(JSContext* cx, unsigned int argc, JS::Value* vp)
61 : {
62 0 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
63 0 : Script::Request rq(cx);
64 0 : const Script::Interface& scriptInterface = rq.GetScriptInterface();
65 :
66 0 : JS::RootedObject obj(rq.cx, scriptInterface.CreateCustomObject("GUISize"));
67 :
68 0 : if (args.length() == 8)
69 : {
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", args[4]);
75 0 : JS_SetProperty(rq.cx, obj, "rtop", args[5]);
76 0 : JS_SetProperty(rq.cx, obj, "rright", args[6]);
77 0 : JS_SetProperty(rq.cx, obj, "rbottom", args[7]);
78 : }
79 0 : else if (args.length() == 4)
80 : {
81 0 : JS::RootedValue zero(rq.cx, JS::NumberValue(0));
82 0 : JS_SetProperty(rq.cx, obj, "left", args[0]);
83 0 : JS_SetProperty(rq.cx, obj, "top", args[1]);
84 0 : JS_SetProperty(rq.cx, obj, "right", args[2]);
85 0 : JS_SetProperty(rq.cx, obj, "bottom", args[3]);
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 0 : }
91 : else
92 : {
93 0 : JS::RootedValue zero(rq.cx, JS::NumberValue(0));
94 0 : JS_SetProperty(rq.cx, obj, "left", zero);
95 0 : JS_SetProperty(rq.cx, obj, "top", zero);
96 0 : JS_SetProperty(rq.cx, obj, "right", zero);
97 0 : JS_SetProperty(rq.cx, obj, "bottom", zero);
98 0 : JS_SetProperty(rq.cx, obj, "rleft", zero);
99 0 : JS_SetProperty(rq.cx, obj, "rtop", zero);
100 0 : JS_SetProperty(rq.cx, obj, "rright", zero);
101 0 : JS_SetProperty(rq.cx, obj, "rbottom", zero);
102 0 : }
103 :
104 0 : args.rval().setObject(*obj);
105 0 : return true;
106 0 : }
107 :
108 : // Produces "10", "-10", "50%", "50%-10", "50%+10", etc
109 0 : std::string JSI_GUISize::ToPercentString(double pix, double per)
110 : {
111 0 : if (per == 0)
112 0 : return fmt::format("{}", pix);
113 :
114 0 : if (pix == 0.0)
115 0 : return fmt::format("{}%", per);
116 :
117 0 : return fmt::format("{}%{:+}", per, pix);
118 : }
119 :
120 0 : bool JSI_GUISize::toString(JSContext* cx, unsigned int argc, JS::Value* vp)
121 : {
122 0 : JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
123 0 : std::string buffer;
124 :
125 0 : Script::Request rq(cx);
126 0 : double val, valr;
127 :
128 : #define SIDE(side) \
129 : Script::GetProperty(rq, args.thisv(), #side, val); \
130 : Script::GetProperty(rq, args.thisv(), "r"#side, valr); \
131 : buffer += ToPercentString(val, valr);
132 :
133 0 : SIDE(left);
134 0 : buffer += " ";
135 0 : SIDE(top);
136 0 : buffer += " ";
137 0 : SIDE(right);
138 0 : buffer += " ";
139 0 : SIDE(bottom);
140 : #undef SIDE
141 :
142 0 : Script::ToJSVal(rq, args.rval(), buffer);
143 0 : return true;
144 0 : }
|