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_CGUISize.h"
21 :
22 : #include "gui/CGUI.h"
23 : #include "gui/CGUISetting.h"
24 : #include "gui/ObjectBases/IGUIObject.h"
25 : #include "gui/Scripting/JSInterface_GUISize.h"
26 : #include "gui/SettingTypes/CGUISize.h"
27 : #include "lib/code_generation.h"
28 : #include "maths/Rect.h"
29 : #include "ps/CLogger.h"
30 : #include "ps/CStr.h"
31 : #include "scriptinterface/Object.h"
32 : #include "scriptinterface/Conversions.h"
33 : #include "scriptinterface/Interface.h"
34 : #include "scriptinterface/Request.h"
35 :
36 : #include <fmt/format.h>
37 : #include <js/CallArgs.h>
38 : #include <js/Class.h>
39 : #include <js/Conversions.h>
40 : #include <js/Object.h>
41 : #include <js/PropertyDescriptor.h>
42 : #include <js/PropertySpec.h>
43 : #include <js/RootingAPI.h>
44 : #include <js/TypeDecls.h>
45 : #include <js/Value.h>
46 : #include <jsapi.h>
47 : #include <string>
48 : struct JSContext;
49 :
50 : namespace
51 : {
52 : template <float CRect::*Member, CRect CGUISize::*RectMember>
53 1 : bool GetCRectField(JSContext* cx, unsigned argc, JS::Value* vp)
54 : {
55 1 : JS::CallArgs args{JS::CallArgsFromVp(argc, vp)};
56 1 : JS::RootedObject obj{cx, &args.thisv().toObject()};
57 1 : CGUISimpleSetting<CGUISize>* wrapper{JS::GetMaybePtrFromReservedSlot<CGUISimpleSetting<CGUISize>>(obj, Script::Interface::JSObjectReservedSlots::PRIVATE)};
58 :
59 1 : args.rval().setDouble(wrapper->GetMutable().*RectMember.*Member);
60 1 : return true;
61 1 : }
62 :
63 : template <float CRect::*Member, CRect CGUISize::*RectMember>
64 4 : bool SetCRectField(JSContext* cx, unsigned argc, JS::Value* vp)
65 : {
66 4 : JS::CallArgs args{JS::CallArgsFromVp(argc, vp)};
67 4 : JS::RootedObject obj{cx, &args.thisv().toObject()};
68 8 : CGUISimpleSetting<CGUISize>* wrapper{JS::GetMaybePtrFromReservedSlot<CGUISimpleSetting<CGUISize>>(obj, Script::Interface::JSObjectReservedSlots::PRIVATE)};
69 :
70 : double val;
71 8 : if (!JS::ToNumber(cx, args.get(0), &val))
72 : return false;
73 :
74 4 : wrapper->GetMutable().*RectMember.*Member = static_cast<float>(val);
75 8 : wrapper->Set(wrapper->GetMutable(), true); // causes CGUISimpleSetting to actually process the change
76 :
77 4 : args.rval().setUndefined();
78 :
79 4 : return true;
80 4 : }
81 :
82 : // Produces "10", "-10", "50%", "50%-10", "50%+10", etc
83 0 : std::string ToPercentString(double pix, double per)
84 : {
85 0 : if (per == 0)
86 0 : return fmt::format("{}", pix);
87 :
88 0 : if (pix == 0)
89 0 : return fmt::format("{}%", per);
90 :
91 0 : return fmt::format("{}%{:+}", per, pix);
92 : }
93 :
94 0 : bool toString(JSContext* cx, unsigned int argc, JS::Value* vp)
95 : {
96 0 : JS::CallArgs args{JS::CallArgsFromVp(argc, vp)};
97 0 : JS::RootedObject obj{cx, &args.thisv().toObject()};
98 0 : CGUISimpleSetting<CGUISize>* wrapper{JS::GetMaybePtrFromReservedSlot<CGUISimpleSetting<CGUISize>>(obj, Script::Interface::JSObjectReservedSlots::PRIVATE)};
99 0 : std::string buffer;
100 :
101 0 : buffer += ToPercentString(wrapper->GetMutable().pixel.left, wrapper->GetMutable().percent.left) + " ";
102 0 : buffer += ToPercentString(wrapper->GetMutable().pixel.top, wrapper->GetMutable().percent.top) + " ";
103 0 : buffer += ToPercentString(wrapper->GetMutable().pixel.right, wrapper->GetMutable().percent.right) + " ";
104 0 : buffer += ToPercentString(wrapper->GetMutable().pixel.bottom, wrapper->GetMutable().percent.bottom);
105 :
106 0 : Script::Request rq{cx};
107 0 : Script::ToJSVal(rq, args.rval(), buffer);
108 0 : return true;
109 0 : }
110 :
111 : JSClass JSI_class = {
112 : "CGUISize", JSCLASS_HAS_RESERVED_SLOTS(1)
113 : };
114 :
115 : JSFunctionSpec JSI_methods[] =
116 : {
117 : JS_FN("toString", toString, 0, 0),
118 : JS_FN("toSource", toString, 0, 0),
119 : JS_FS_END
120 : };
121 :
122 : JSPropertySpec JSI_props[] =
123 : {
124 : JS_PSGS("top", (GetCRectField<&CRect::top, &CGUISize::pixel>), (SetCRectField<&CRect::top, &CGUISize::pixel>), JSPROP_ENUMERATE),
125 : JS_PSGS("left", (GetCRectField<&CRect::left, &CGUISize::pixel>), (SetCRectField<&CRect::left, &CGUISize::pixel>), JSPROP_ENUMERATE),
126 : JS_PSGS("bottom", (GetCRectField<&CRect::bottom, &CGUISize::pixel>), (SetCRectField<&CRect::bottom, &CGUISize::pixel>), JSPROP_ENUMERATE),
127 : JS_PSGS("right", (GetCRectField<&CRect::right, &CGUISize::pixel>), (SetCRectField<&CRect::right, &CGUISize::pixel>), JSPROP_ENUMERATE),
128 :
129 : JS_PSGS("rtop", (GetCRectField<&CRect::top, &CGUISize::percent>), (SetCRectField<&CRect::top, &CGUISize::percent>), JSPROP_ENUMERATE),
130 : JS_PSGS("rleft", (GetCRectField<&CRect::left, &CGUISize::percent>), (SetCRectField<&CRect::left, &CGUISize::percent>), JSPROP_ENUMERATE),
131 : JS_PSGS("rbottom", (GetCRectField<&CRect::bottom, &CGUISize::percent>), (SetCRectField<&CRect::bottom, &CGUISize::percent>), JSPROP_ENUMERATE),
132 : JS_PSGS("rright", (GetCRectField<&CRect::right, &CGUISize::percent>), (SetCRectField<&CRect::right, &CGUISize::percent>), JSPROP_ENUMERATE),
133 : JS_PS_END
134 : };
135 : } // end anonymous namespace
136 :
137 :
138 :
139 26 : void JSI_CGUISize::RegisterScriptClass(Script::Interface& scriptInterface)
140 : {
141 26 : scriptInterface.DefineCustomObjectType(&JSI_class, nullptr, 0, JSI_props, JSI_methods, nullptr, nullptr);
142 26 : }
143 :
144 : template class CGUISimpleSetting<CGUISize>;
145 : template<>
146 3 : void CGUISimpleSetting<CGUISize>::ToJSVal(const Script::Request& rq, JS::MutableHandleValue ret)
147 : {
148 3 : const Script::Interface& scriptInterface = rq.GetScriptInterface();
149 3 : JS::RootedObject obj{rq.cx, scriptInterface.CreateCustomObject("CGUISize")};
150 :
151 3 : JS::SetReservedSlot(obj, Script::Interface::JSObjectReservedSlots::PRIVATE, JS::PrivateValue(this));
152 3 : ret.setObject(*obj);
153 3 : };
154 :
155 : template<>
156 1 : bool CGUISimpleSetting<CGUISize>::DoFromString(const CStrW& value)
157 : {
158 1 : return CGUI::ParseString<CGUISize>(&m_Object.GetGUI(), value, m_Setting);
159 : };
160 :
161 : template<>
162 1 : bool CGUISimpleSetting<CGUISize>::DoFromJSVal(const Script::Request& rq, JS::HandleValue value)
163 : {
164 1 : if (value.isString())
165 : {
166 0 : CStrW str;
167 0 : if (!Script::FromJSVal(rq, value, str))
168 : {
169 0 : LOGERROR("CGUISize could not read JS string");
170 0 : return false;
171 : }
172 :
173 0 : if (!m_Setting.FromString(str.ToUTF8()))
174 : {
175 0 : LOGERROR("CGUISize could not parse JS string");
176 0 : return false;
177 : }
178 :
179 : return true;
180 0 : }
181 :
182 1 : if (!value.isObject())
183 : {
184 0 : LOGERROR("CGUISize value is not an String, nor Object");
185 0 : return false;
186 : }
187 :
188 1 : JS::RootedObject obj{rq.cx, &value.toObject()};
189 1 : if (JS_InstanceOf(rq.cx, obj, &JSI_class, nullptr))
190 : {
191 0 : CGUISimpleSetting<CGUISize>* wrapper = JS::GetMaybePtrFromReservedSlot<CGUISimpleSetting<CGUISize>>(obj, Script::Interface::JSObjectReservedSlots::PRIVATE);
192 0 : if (this != wrapper)
193 0 : this->Set(wrapper->m_Setting, false);
194 0 : return true;
195 : }
196 :
197 1 : if (JS_InstanceOf(rq.cx, obj, &JSI_GUISize::JSI_class, nullptr))
198 : {
199 0 : ONCE(LOGWARNING("Assigning an GUISize to CGUISize is deprecated. Please use the object.size = "
200 : "{{left:number, top:number, right:number, bottom:number}} format instead. This support "
201 : "will be removed in a future version."));
202 : }
203 :
204 1 : bool atLeastOnePropertySet{false};
205 1 : CRect pixel{};
206 1 : CRect percent{};
207 9 : auto tryGet = [&](const char* name, float& field)
208 : {
209 8 : if (!Script::HasProperty(rq, value, name) || !Script::GetProperty(rq, value, name, field))
210 6 : return;
211 :
212 2 : atLeastOnePropertySet = true;
213 1 : };
214 :
215 1 : tryGet("left", pixel.left);
216 1 : tryGet("top", pixel.top);
217 1 : tryGet("right", pixel.right);
218 1 : tryGet("bottom", pixel.bottom);
219 :
220 1 : tryGet("rtop", percent.top);
221 1 : tryGet("rleft", percent.left);
222 1 : tryGet("rbottom", percent.bottom);
223 1 : tryGet("rright", percent.right);
224 :
225 : // If no properties were set, return false to indicate failure.
226 1 : if (!atLeastOnePropertySet)
227 : {
228 0 : LOGERROR("CGUISize could not read JS object");
229 0 : return false;
230 : }
231 :
232 1 : m_Setting.pixel = pixel;
233 1 : m_Setting.percent = percent;
234 :
235 : return true;
236 1 : };
|