Skip to content

Commit e2f2549

Browse files
committed
Add more Lua bindings of classes
1 parent 11a28c0 commit e2f2549

7 files changed

Lines changed: 236 additions & 2 deletions

File tree

PopLib/scripting/lua/lcolor.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "lpoplib.hpp"
2+
#include "graphics/color.hpp"
3+
4+
using namespace PopLib;
5+
6+
void open_color(sol::state_view lua)
7+
{
8+
sol::table poplib;
9+
if (lua["PopLib"].valid() && lua["PopLib"].get_type() == sol::type::table)
10+
poplib = lua["PopLib"];
11+
else
12+
{
13+
poplib = lua.create_table();
14+
lua["PopLib"] = poplib;
15+
}
16+
17+
poplib.new_usertype<PopLib::Color>(
18+
"Color",
19+
sol::constructors<PopLib::Color(), PopLib::Color(int), PopLib::Color(int, int), PopLib::Color(int, int, int),
20+
PopLib::Color(int, int, int, int)>(),
21+
22+
"GetRed", &PopLib::Color::GetRed, "GetGreen", &PopLib::Color::GetGreen, "GetBlue", &PopLib::Color::GetBlue,
23+
"GetAlpha", &PopLib::Color::GetAlpha, "ToInt", &PopLib::Color::ToInt,
24+
25+
"mRed", &PopLib::Color::mRed, "mGreen", &PopLib::Color::mGreen, "mBlue", &PopLib::Color::mBlue, "mAlpha",
26+
&PopLib::Color::mAlpha);
27+
28+
poplib["Color"]["Black"] = PopLib::Color::Black;
29+
poplib["Color"]["White"] = PopLib::Color::White;
30+
}

PopLib/scripting/lua/lkeycodes.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "lpoplib.hpp"
2+
#include "misc/keycodes.hpp"
3+
4+
using namespace PopLib;
5+
6+
void open_keycodes(sol::state_view lua)
7+
{
8+
sol::table poplib;
9+
if (lua["PopLib"].valid() && lua["PopLib"].get_type() == sol::type::table)
10+
poplib = lua["PopLib"];
11+
else
12+
{
13+
poplib = lua.create_table();
14+
lua["PopLib"] = poplib;
15+
}
16+
17+
sol::table keycode = lua.create_table();
18+
keycode["UNKNOWN"] = KEYCODE_UNKNOWN;
19+
keycode["RETURN"] = KEYCODE_RETURN;
20+
keycode["BACK"] = KEYCODE_BACK;
21+
keycode["TAB"] = KEYCODE_TAB;
22+
keycode["SPACE"] = KEYCODE_SPACE;
23+
keycode["ESCAPE"] = KEYCODE_ESCAPE;
24+
keycode["LEFT"] = KEYCODE_LEFT;
25+
keycode["RIGHT"] = KEYCODE_RIGHT;
26+
keycode["UP"] = KEYCODE_UP;
27+
keycode["DOWN"] = KEYCODE_DOWN;
28+
keycode["SHIFT"] = KEYCODE_SHIFT;
29+
keycode["CONTROL"] = KEYCODE_CONTROL;
30+
keycode["ALT"] = KEYCODE_MENU;
31+
keycode["DELETE"] = KEYCODE_DELETE;
32+
keycode["INSERT"] = KEYCODE_INSERT;
33+
keycode["HOME"] = KEYCODE_HOME;
34+
keycode["END"] = KEYCODE_END;
35+
36+
// F1–F12
37+
for (int i = 1; i <= 12; ++i)
38+
{
39+
keycode["F" + std::to_string(i)] = KEYCODE_F1 + (i - 1);
40+
}
41+
42+
poplib["KeyCode"] = keycode;
43+
44+
poplib.set_function("GetKeyCodeFromName", &GetKeyCodeFromName);
45+
poplib.set_function("GetKeyNameFromCode", &GetKeyNameFromCode);
46+
}

PopLib/scripting/lua/lmatrix.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "lpoplib.hpp"
2+
#include "math/matrix.hpp"
3+
4+
using namespace PopLib;
5+
6+
void open_matrix(sol::state_view lua)
7+
{
8+
sol::table poplib;
9+
if (lua["PopLib"].valid() && lua["PopLib"].get_type() == sol::type::table)
10+
poplib = lua["PopLib"];
11+
else
12+
{
13+
poplib = lua.create_table();
14+
lua["PopLib"] = poplib;
15+
}
16+
17+
poplib.new_usertype<PopLib::Matrix3>(
18+
"Matrix3", sol::constructors<PopLib::Matrix3()>(), "ZeroMatrix", &PopLib::Matrix3::ZeroMatrix,
19+
"LoadIdentity", &PopLib::Matrix3::LoadIdentity, sol::meta_function::multiplication,
20+
sol::resolve<PopLib::Matrix3(const PopLib::Matrix3 &) const>(&PopLib::Matrix3::operator*),
21+
sol::meta_function::to_string, [](const PopLib::Matrix3 &m) { return "Matrix3(...)"; });
22+
23+
poplib.new_usertype<PopLib::Transform2D>(
24+
"Transform2D",
25+
sol::constructors<PopLib::Transform2D(), PopLib::Transform2D(bool),
26+
PopLib::Transform2D(const PopLib::Matrix3 &)>(),
27+
sol::base_classes, sol::bases<PopLib::Matrix3>(),
28+
29+
"Translate", &PopLib::Transform2D::Translate, "RotateRad", &PopLib::Transform2D::RotateRad,
30+
"RotateDeg", &PopLib::Transform2D::RotateDeg, "Scale", &PopLib::Transform2D::Scale);
31+
32+
poplib.new_usertype<PopLib::Transform>(
33+
"Transform", sol::constructors<PopLib::Transform()>(), "Reset", &PopLib::Transform::Reset, "Translate",
34+
&PopLib::Transform::Translate, "RotateRad", &PopLib::Transform::RotateRad, "RotateDeg",
35+
&PopLib::Transform::RotateDeg, "Scale", &PopLib::Transform::Scale, "GetMatrix", &PopLib::Transform::GetMatrix,
36+
37+
"mTransX1", &PopLib::Transform::mTransX1, "mTransY1", &PopLib::Transform::mTransY1, "mTransX2",
38+
&PopLib::Transform::mTransX2, "mTransY2", &PopLib::Transform::mTransY2, "mScaleX", &PopLib::Transform::mScaleX,
39+
"mScaleY", &PopLib::Transform::mScaleY, "mRot", &PopLib::Transform::mRot, "mHaveRot",
40+
&PopLib::Transform::mHaveRot, "mHaveScale", &PopLib::Transform::mHaveScale, "mComplex",
41+
&PopLib::Transform::mComplex);
42+
}

PopLib/scripting/lua/lpoint.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "lpoplib.hpp"
2+
#include "math/point.hpp"
3+
4+
using namespace PopLib;
5+
6+
template <typename T> struct TPointWrapper : public TPoint<T>
7+
{
8+
using TPoint<T>::TPoint; // inherit constructors
9+
10+
TPointWrapper() : TPoint<T>()
11+
{
12+
}
13+
TPointWrapper(T x, T y) : TPoint<T>(x, y)
14+
{
15+
}
16+
TPointWrapper(const TPoint<T> &p) : TPoint<T>(p)
17+
{
18+
}
19+
20+
TPointWrapper operator+(const TPointWrapper &other) const
21+
{
22+
return TPointWrapper(this->mX + other.mX, this->mY + other.mY);
23+
}
24+
25+
TPointWrapper operator-(const TPointWrapper &other) const
26+
{
27+
return TPointWrapper(this->mX - other.mX, this->mY - other.mY);
28+
}
29+
30+
TPointWrapper operator*(const TPointWrapper &other) const
31+
{
32+
return TPointWrapper(this->mX * other.mX, this->mY * other.mY);
33+
}
34+
35+
TPointWrapper operator/(const TPointWrapper &other) const
36+
{
37+
return TPointWrapper(this->mX / other.mX, this->mY / other.mY);
38+
}
39+
40+
TPointWrapper operator*(T s) const
41+
{
42+
return TPointWrapper(this->mX * s, this->mY * s);
43+
}
44+
45+
TPointWrapper operator/(T s) const
46+
{
47+
return TPointWrapper(this->mX / s, this->mY / s);
48+
}
49+
50+
bool operator==(const TPointWrapper &other) const
51+
{
52+
return this->mX == other.mX && this->mY == other.mY;
53+
}
54+
};
55+
56+
void open_point(sol::state_view lua)
57+
{
58+
sol::table poplib;
59+
if (lua["PopLib"].valid() && lua["PopLib"].get_type() == sol::type::table)
60+
{
61+
poplib = lua["PopLib"];
62+
}
63+
else
64+
{
65+
poplib = lua.create_table();
66+
lua["PopLib"] = poplib;
67+
}
68+
69+
poplib.new_usertype<TPointWrapper<int>>(
70+
"Point",
71+
sol::constructors<TPointWrapper<int>(), TPointWrapper<int>(int, int),
72+
TPointWrapper<int>(const TPoint<int> &)>(),
73+
"mX", &TPointWrapper<int>::mX, "mY", &TPointWrapper<int>::mY, sol::meta_function::equal_to,
74+
&TPointWrapper<int>::operator==, sol::meta_function::addition, &TPointWrapper<int>::operator+,
75+
sol::meta_function::subtraction, &TPointWrapper<int>::operator-, sol::meta_function::multiplication,
76+
sol::overload(
77+
static_cast<TPointWrapper<int> (TPointWrapper<int>::*)(const TPointWrapper<int> &) const>(
78+
&TPointWrapper<int>::operator*),
79+
static_cast<TPointWrapper<int> (TPointWrapper<int>::*)(int) const>(&TPointWrapper<int>::operator*)),
80+
sol::meta_function::division,
81+
sol::overload(
82+
static_cast<TPointWrapper<int> (TPointWrapper<int>::*)(const TPointWrapper<int> &) const>(
83+
&TPointWrapper<int>::operator/),
84+
static_cast<TPointWrapper<int> (TPointWrapper<int>::*)(int) const>(&TPointWrapper<int>::operator/)),
85+
sol::meta_function::to_string, [](const TPointWrapper<int> &p) {
86+
return "Point(" + std::to_string(p.mX) + ", " + std::to_string(p.mY) + ")";
87+
});
88+
89+
poplib.new_usertype<TPointWrapper<double>>(
90+
"FPoint",
91+
sol::constructors<TPointWrapper<double>(), TPointWrapper<double>(double, double),
92+
TPointWrapper<double>(const TPoint<double> &)>(),
93+
"mX", &TPointWrapper<double>::mX, "mY", &TPointWrapper<double>::mY, sol::meta_function::equal_to,
94+
&TPointWrapper<double>::operator==, sol::meta_function::addition, &TPointWrapper<double>::operator+,
95+
sol::meta_function::subtraction, &TPointWrapper<double>::operator-, sol::meta_function::multiplication,
96+
sol::overload(
97+
static_cast<TPointWrapper<double> (TPointWrapper<double>::*)(const TPointWrapper<double> &) const>(
98+
&TPointWrapper<double>::operator*),
99+
static_cast<TPointWrapper<double> (TPointWrapper<double>::*)(double) const>(
100+
&TPointWrapper<double>::operator*)),
101+
sol::meta_function::division,
102+
sol::overload(
103+
static_cast<TPointWrapper<double> (TPointWrapper<double>::*)(const TPointWrapper<double> &) const>(
104+
&TPointWrapper<double>::operator/),
105+
static_cast<TPointWrapper<double> (TPointWrapper<double>::*)(double) const>(
106+
&TPointWrapper<double>::operator/)),
107+
sol::meta_function::to_string, [](const TPointWrapper<double> &p) {
108+
return "FPoint(" + std::to_string(p.mX) + ", " + std::to_string(p.mY) + ")";
109+
});
110+
}

PopLib/scripting/lua/lpoplib.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
void pop_openlibs(sol::state &lua)
44
{
55
open_rect(lua);
6+
open_color(lua);
7+
open_keycodes(lua);
8+
open_point(lua);
9+
open_matrix(lua);
610
}

PopLib/scripting/lua/lpoplib.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "../scripting_base.hpp"
88

99
void(open_rect)(sol::state_view lua);
10+
void(open_color)(sol::state_view lua);
11+
void(open_keycodes)(sol::state_view lua);
12+
void(open_point)(sol::state_view lua);
13+
void(open_matrix)(sol::state_view lua);
1014

1115
void(pop_openlibs)(sol::state &lua);
1216

PopLib/scripting/lua/lrect.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ void open_rect(sol::state_view lua)
77
{
88
sol::table poplib;
99
if (lua["PopLib"].valid() && lua["PopLib"].get_type() == sol::type::table)
10-
{
1110
poplib = lua["PopLib"];
12-
}
1311
else
1412
{
1513
poplib = lua.create_table();

0 commit comments

Comments
 (0)