1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#pragma once
#include "MathHelper.h"
#include "Runtime/Utilities/Assert.h"
#include "Runtime/Lua/LuaHelper.h"
#include "ITransData.h"
namespace Internal
{
template<typename T>
struct Vector2T
{
Vector2T(T x = 0, T y = 0)
{
this->x = x;
this->y = y;
}
inline void Set(T x, T y)
{
this->x = x;
this->y = y;
}
Vector2T Clamp(T xmin, T xmax, T ymin, T ymax)
{
Vector2T v;
v.x = clamp(x, xmin, xmax);
v.y = clamp(y, ymin, ymax);
return v;
}
T operator[](int i)
{
if (i == 0) return x;
else if (i == 1) return y;
Assert(false);
}
bool operator == (const Vector2T& v) const
{
return v.x == x && v.y == y;
}
bool operator != (const Vector2T& v) const
{
return v.x != x || v.y != y;
}
Vector2T<T> operator - (const Vector2T& v) const
{
Vector2T<T> res = Vector2T<T>(x - v.x, y - v.y);
return res;
}
void CastToLuaObject(LuaBind::State& state) const
{
int top = state.GetTop();
if (LuaHelper::GetLuaClass(state, "GameLab.Engine.Math.Vector2"))
{
lua_getfield(state, -1, "New");
lua_pushnumber(state, (float)x);
lua_pushnumber(state, (float)y);
state.Call(2, 1);
lua_replace(state, top + 1);
lua_settop(state, top + 1);
}
else
{
lua_pushnil(state);
}
}
void RestoreFromLuaObject(LuaBind::State& state, int index)
{
if (lua_isnil(state, index))
return;
if (LuaHelper::IsType(state, "GameLab.Engine.Math.Vector2", index))
{
x = state.GetField<float>(index, "x", 0);
y = state.GetField<float>(index, "y", 0);
}
else if (LuaHelper::IsType(state, "table", index))
{
x = state.GetField<float>(index, 0, 0);
y = state.GetField<float>(index, 1, 0);
}
}
static int GetDataSize()
{
return 2 * sizeof(T);
}
T x, y;
static Vector2T<T> zero;
static Vector2T<T> one;
};
template<typename T>
Vector2T<T> Vector2T<T>::zero = Vector2T(0, 0);
template<typename T>
Vector2T<T> Vector2T<T>::one = Vector2T(1, 1);
}
|