1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#include "wrap_Fixture.h"
22#include "common/StringMap.h"
23
24namespace love
25{
26namespace physics
27{
28namespace box2d
29{
30
31Fixture *luax_checkfixture(lua_State *L, int idx)
32{
33 Fixture *f = luax_checktype<Fixture>(L, idx);
34 if (!f->isValid())
35 luaL_error(L, "Attempt to use destroyed fixture.");
36 return f;
37}
38
39int w_Fixture_getType(lua_State *L)
40{
41 Fixture *t = luax_checkfixture(L, 1);
42 const char *type = "";
43 Shape::getConstant(t->getType(), type);
44 lua_pushstring(L, type);
45 return 1;
46}
47
48int w_Fixture_setFriction(lua_State *L)
49{
50 Fixture *t = luax_checkfixture(L, 1);
51 float arg1 = (float)luaL_checknumber(L, 2);
52 t->setFriction(arg1);
53 return 0;
54}
55
56int w_Fixture_setRestitution(lua_State *L)
57{
58 Fixture *t = luax_checkfixture(L, 1);
59 float arg1 = (float)luaL_checknumber(L, 2);
60 t->setRestitution(arg1);
61 return 0;
62}
63
64int w_Fixture_setDensity(lua_State *L)
65{
66 Fixture *t = luax_checkfixture(L, 1);
67 float arg1 = (float)luaL_checknumber(L, 2);
68 luax_catchexcept(L, [&](){ t->setDensity(arg1); });
69 return 0;
70}
71
72int w_Fixture_setSensor(lua_State *L)
73{
74 Fixture *t = luax_checkfixture(L, 1);
75 bool arg1 = luax_checkboolean(L, 2);
76 t->setSensor(arg1);
77 return 0;
78}
79
80int w_Fixture_getFriction(lua_State *L)
81{
82 Fixture *t = luax_checkfixture(L, 1);
83 lua_pushnumber(L, t->getFriction());
84 return 1;
85}
86
87int w_Fixture_getRestitution(lua_State *L)
88{
89 Fixture *t = luax_checkfixture(L, 1);
90 lua_pushnumber(L, t->getRestitution());
91 return 1;
92}
93
94int w_Fixture_getDensity(lua_State *L)
95{
96 Fixture *t = luax_checkfixture(L, 1);
97 lua_pushnumber(L, t->getDensity());
98 return 1;
99}
100
101int w_Fixture_isSensor(lua_State *L)
102{
103 Fixture *t = luax_checkfixture(L, 1);
104 luax_pushboolean(L, t->isSensor());
105 return 1;
106}
107
108int w_Fixture_getBody(lua_State *L)
109{
110 Fixture *t = luax_checkfixture(L, 1);
111 Body *body = t->getBody();
112 if (body == 0)
113 return 0;
114 luax_pushtype(L, body);
115 return 1;
116}
117
118int w_Fixture_getShape(lua_State *L)
119{
120 Fixture *t = luax_checkfixture(L, 1);
121 Shape * shape = t->getShape();
122 if (shape == nullptr)
123 return 0;
124 switch (shape->getType())
125 {
126 case Shape::SHAPE_EDGE:
127 luax_pushtype(L, dynamic_cast<EdgeShape *>(shape));
128 break;
129 case Shape::SHAPE_CHAIN:
130 luax_pushtype(L, dynamic_cast<ChainShape *>(shape));
131 break;
132 case Shape::SHAPE_CIRCLE:
133 luax_pushtype(L, dynamic_cast<CircleShape *>(shape));
134 break;
135 case Shape::SHAPE_POLYGON:
136 luax_pushtype(L, dynamic_cast<PolygonShape *>(shape));
137 break;
138 default:
139 luax_pushtype(L, shape);
140 break;
141 }
142 return 1;
143}
144
145int w_Fixture_testPoint(lua_State *L)
146{
147 Fixture *t = luax_checkfixture(L, 1);
148 float x = (float)luaL_checknumber(L, 2);
149 float y = (float)luaL_checknumber(L, 3);
150 luax_pushboolean(L, t->testPoint(x, y));
151 return 1;
152}
153
154int w_Fixture_rayCast(lua_State *L)
155{
156 Fixture *t = luax_checkfixture(L, 1);
157 lua_remove(L, 1);
158 int ret = 0;
159 luax_catchexcept(L, [&](){ ret = t->rayCast(L); });
160 return ret;
161}
162
163int w_Fixture_setFilterData(lua_State *L)
164{
165 Fixture *t = luax_checkfixture(L, 1);
166 int v[3];
167 v[0] = (int) luaL_checkinteger(L, 2);
168 v[1] = (int) luaL_checkinteger(L, 3);
169 v[2] = (int) luaL_checkinteger(L, 4);
170 t->setFilterData(v);
171 return 0;
172}
173
174int w_Fixture_getFilterData(lua_State *L)
175{
176 Fixture *t = luax_checkfixture(L, 1);
177 int v[3];
178 t->getFilterData(v);
179 lua_pushinteger(L, v[0]);
180 lua_pushinteger(L, v[1]);
181 lua_pushinteger(L, v[2]);
182 return 3;
183}
184
185int w_Fixture_setCategory(lua_State *L)
186{
187 Fixture *t = luax_checkfixture(L, 1);
188 lua_remove(L, 1);
189 return t->setCategory(L);
190}
191
192int w_Fixture_getCategory(lua_State *L)
193{
194 Fixture *t = luax_checkfixture(L, 1);
195 lua_remove(L, 1);
196 return t->getCategory(L);
197}
198
199int w_Fixture_setMask(lua_State *L)
200{
201 Fixture *t = luax_checkfixture(L, 1);
202 lua_remove(L, 1);
203 return t->setMask(L);
204}
205
206int w_Fixture_getMask(lua_State *L)
207{
208 Fixture *t = luax_checkfixture(L, 1);
209 lua_remove(L, 1);
210 return t->getMask(L);
211}
212
213int w_Fixture_setUserData(lua_State *L)
214{
215 Fixture *t = luax_checkfixture(L, 1);
216 lua_remove(L, 1);
217 return t->setUserData(L);
218}
219
220int w_Fixture_getUserData(lua_State *L)
221{
222 Fixture *t = luax_checkfixture(L, 1);
223 lua_remove(L, 1);
224 return t->getUserData(L);
225}
226
227int w_Fixture_getBoundingBox(lua_State *L)
228{
229 Fixture *t = luax_checkfixture(L, 1);
230 lua_remove(L, 1);
231 return t->getBoundingBox(L);
232}
233
234int w_Fixture_getMassData(lua_State *L)
235{
236 Fixture *t = luax_checkfixture(L, 1);
237 lua_remove(L, 1);
238 return t->getMassData(L);
239}
240
241int w_Fixture_getGroupIndex(lua_State *L)
242{
243 Fixture *t = luax_checkfixture(L, 1);
244 int i = t->getGroupIndex();
245 lua_pushinteger(L, i);
246 return 1;
247}
248
249int w_Fixture_setGroupIndex(lua_State *L)
250{
251 Fixture *t = luax_checkfixture(L, 1);
252 int i = (int) luaL_checkinteger(L, 2);
253 t->setGroupIndex(i);
254 return 0;
255}
256
257int w_Fixture_destroy(lua_State *L)
258{
259 Fixture *t = luax_checkfixture(L, 1);
260 luax_catchexcept(L, [&](){ t->destroy(); });
261 return 0;
262}
263
264int w_Fixture_isDestroyed(lua_State *L)
265{
266 Fixture *f = luax_checktype<Fixture>(L, 1);
267 luax_pushboolean(L, !f->isValid());
268 return 1;
269}
270
271static const luaL_Reg w_Fixture_functions[] =
272{
273 { "getType", w_Fixture_getType },
274 { "setFriction", w_Fixture_setFriction },
275 { "setRestitution", w_Fixture_setRestitution },
276 { "setDensity", w_Fixture_setDensity },
277 { "setSensor", w_Fixture_setSensor },
278 { "getFriction", w_Fixture_getFriction },
279 { "getRestitution", w_Fixture_getRestitution },
280 { "getDensity", w_Fixture_getDensity },
281 { "getBody", w_Fixture_getBody },
282 { "getShape", w_Fixture_getShape },
283 { "isSensor", w_Fixture_isSensor },
284 { "testPoint", w_Fixture_testPoint },
285 { "rayCast", w_Fixture_rayCast },
286 { "setFilterData", w_Fixture_setFilterData },
287 { "getFilterData", w_Fixture_getFilterData },
288 { "setCategory", w_Fixture_setCategory },
289 { "getCategory", w_Fixture_getCategory },
290 { "setMask", w_Fixture_setMask },
291 { "getMask", w_Fixture_getMask },
292 { "setUserData", w_Fixture_setUserData },
293 { "getUserData", w_Fixture_getUserData },
294 { "getBoundingBox", w_Fixture_getBoundingBox },
295 { "getMassData", w_Fixture_getMassData },
296 { "getGroupIndex", w_Fixture_getGroupIndex },
297 { "setGroupIndex", w_Fixture_setGroupIndex },
298 { "destroy", w_Fixture_destroy },
299 { "isDestroyed", w_Fixture_isDestroyed },
300 { 0, 0 }
301};
302
303extern "C" int luaopen_fixture(lua_State *L)
304{
305 return luax_register_type(L, &Fixture::type, w_Fixture_functions, nullptr);
306}
307
308} // box2d
309} // physics
310} // love
311
312