| | @@ -155,6 +155,18 @@ |
| 155 | 155 | sol::lib::string |
| 156 | 156 | ); |
| 157 | 157 | |
| 158 | + out.new_usertype<Vector2>( |
| 159 | + "Vector2", |
| 160 | + "x", sol::property( |
| 161 | + [](Vector2 &v) { return v.pos.x; }, |
| 162 | + [](Vector2 &v, f32 val) { v.pos.x = val; } |
| 163 | + ), |
| 164 | + "y", sol::property( |
| 165 | + [](Vector2 &v) { return v.pos.y; }, |
| 166 | + [](Vector2 &v, f32 val) { v.pos.y = val; } |
| 167 | + ) |
| 168 | + ); |
| 169 | + |
| 158 | 170 | out.new_usertype<Vector2i>( |
| 159 | 171 | "Vector2i", |
| 160 | 172 | "x", sol::property( |
| | @@ -203,6 +215,22 @@ |
| 203 | 215 | ) |
| 204 | 216 | ); |
| 205 | 217 | |
| 218 | + out.new_usertype<Vector3>( |
| 219 | + "Vector3", |
| 220 | + "x", sol::property( |
| 221 | + [](Vector3 &v) { return v.pos.x; }, |
| 222 | + [](Vector3 &v, f32 val) { v.pos.x = val; } |
| 223 | + ), |
| 224 | + "y", sol::property( |
| 225 | + [](Vector3 &v) { return v.pos.y; }, |
| 226 | + [](Vector3 &v, f32 val) { v.pos.y = val; } |
| 227 | + ), |
| 228 | + "z", sol::property( |
| 229 | + [](Vector3 &v) { return v.pos.z; }, |
| 230 | + [](Vector3 &v, f32 val) { v.pos.z = val; } |
| 231 | + ) |
| 232 | + ); |
| 233 | + |
| 206 | 234 | out.new_usertype<Vector3i>( |
| 207 | 235 | "Vector3i", |
| 208 | 236 | "x", sol::property( |
| | @@ -267,6 +295,26 @@ |
| 267 | 295 | ) |
| 268 | 296 | ); |
| 269 | 297 | |
| 298 | + out.new_usertype<Vector4>( |
| 299 | + "Vector4", |
| 300 | + "x", sol::property( |
| 301 | + [](Vector4 &v) { return v.pos.x; }, |
| 302 | + [](Vector4 &v, f32 val) { v.pos.x = val; } |
| 303 | + ), |
| 304 | + "y", sol::property( |
| 305 | + [](Vector4 &v) { return v.pos.y; }, |
| 306 | + [](Vector4 &v, f32 val) { v.pos.y = val; } |
| 307 | + ), |
| 308 | + "z", sol::property( |
| 309 | + [](Vector4 &v) { return v.pos.z; }, |
| 310 | + [](Vector4 &v, f32 val) { v.pos.z = val; } |
| 311 | + ), |
| 312 | + "w", sol::property( |
| 313 | + [](Vector4 &v) { return v.pos.w; }, |
| 314 | + [](Vector4 &v, f32 val) { v.pos.w = val; } |
| 315 | + ) |
| 316 | + ); |
| 317 | + |
| 270 | 318 | out.new_usertype<Vector4i>( |
| 271 | 319 | "Vector4i", |
| 272 | 320 | "x", sol::property( |
| | @@ -497,6 +545,7 @@ |
| 497 | 545 | |
| 498 | 546 | void LuaEngine::BindIO(sol::state &lua) { |
| 499 | 547 | lua["le"]["io"] = lua.create_table(); |
| 548 | + lua["le"]["io"]["key"] = lua.create_table(); |
| 500 | 549 | |
| 501 | 550 | lua["le"]["io"]["get_mouse"] = [&]() -> Vector2d { |
| 502 | 551 | Vector2d cursor{}; |
| | @@ -504,9 +553,20 @@ |
| 504 | 553 | return cursor; |
| 505 | 554 | }; |
| 506 | 555 | |
| 507 | | - lua["le"]["io"]["is_key_down"] = [&](int key) -> bool { |
| 508 | | - return m_Info->window->GetKey(key) == GLFW_PRESS; |
| 556 | + lua["le"]["io"]["key"]["is_press"] = [this](const string &key) -> bool { |
| 557 | + if (!m_Info->viewportFocused) return false; |
| 558 | + return m_Info->window->GetKey(StringToKey(key)) == GLFW_PRESS; |
| 509 | 559 | }; |
| 560 | + |
| 561 | + lua["le"]["io"]["key"]["is_release"] = [this](const string &key) -> bool { |
| 562 | + if (!m_Info->viewportFocused) return false; |
| 563 | + return m_Info->window->GetKey(StringToKey(key)) == GLFW_RELEASE; |
| 564 | + }; |
| 565 | + |
| 566 | + lua["le"]["io"]["key"]["is_hold"] = [this](const string &key) -> bool { |
| 567 | + if (!m_Info->viewportFocused) return false; |
| 568 | + return m_Info->window->GetKey(StringToKey(key)) == GLFW_REPEAT; |
| 569 | + }; |
| 510 | 570 | } |
| 511 | 571 | |
| 512 | 572 | void LuaEngine::BindMath(sol::state &lua) { |
| | @@ -651,16 +711,34 @@ |
| 651 | 711 | m_Objects.push_back(obj); |
| 652 | 712 | }; |
| 653 | 713 | |
| 714 | + lua["le"]["gfx"]["get_position"] = [&](i32 index) -> Vector3 { |
| 715 | + if (index < 0 || index >= m_Objects.size()) return { 0, 0, 0 }; |
| 716 | + auto pos = m_Objects[index].position.pos; |
| 717 | + return { pos.x, pos.y, pos.z }; |
| 718 | + }; |
| 719 | + |
| 654 | 720 | lua["le"]["gfx"]["set_position"] = [&](i32 index, f32 x, f32 y, f32 z) { |
| 655 | 721 | if (index < 0 || index >= m_Objects.size()) return; |
| 656 | 722 | m_Objects[index].position = { x, y, z }; |
| 657 | 723 | }; |
| 658 | 724 | |
| 725 | + lua["le"]["gfx"]["get_rotation"] = [&](i32 index) -> Vector3 { |
| 726 | + if (index < 0 || index >= m_Objects.size()) return { 0, 0, 0 }; |
| 727 | + auto pos = m_Objects[index].rotation.pos; |
| 728 | + return { pos.x, pos.y, pos.z }; |
| 729 | + }; |
| 730 | + |
| 659 | 731 | lua["le"]["gfx"]["set_rotation"] = [&](i32 index, f32 x, f32 y, f32 z) { |
| 660 | 732 | if (index < 0 || index >= m_Objects.size()) return; |
| 661 | 733 | m_Objects[index].rotation = { x, y, z }; |
| 662 | 734 | }; |
| 663 | 735 | |
| 736 | + lua["le"]["gfx"]["get_scale"] = [&](i32 index) -> Vector3 { |
| 737 | + if (index < 0 || index >= m_Objects.size()) return { 0, 0, 0 }; |
| 738 | + auto pos = m_Objects[index].scale.pos; |
| 739 | + return { pos.x, pos.y, pos.z }; |
| 740 | + }; |
| 741 | + |
| 664 | 742 | lua["le"]["gfx"]["set_scale"] = [&](i32 index, f32 x, f32 y, f32 z) { |
| 665 | 743 | if (index < 0 || index >= m_Objects.size()) return; |
| 666 | 744 | m_Objects[index].scale = { x, y, z }; |
| | @@ -897,5 +975,62 @@ |
| 897 | 975 | sol::error err = res; |
| 898 | 976 | LogError(err); |
| 899 | 977 | } |
| 978 | + } |
| 979 | + |
| 980 | + i32 LuaEngine::StringToKey(const string &key) { |
| 981 | + static std::unordered_map<string, i32> map = { |
| 982 | + { "q", GLFW_KEY_Q }, |
| 983 | + { "w", GLFW_KEY_W }, |
| 984 | + { "e", GLFW_KEY_E }, |
| 985 | + { "r", GLFW_KEY_R }, |
| 986 | + { "t", GLFW_KEY_T }, |
| 987 | + { "y", GLFW_KEY_Y }, |
| 988 | + { "u", GLFW_KEY_U }, |
| 989 | + { "i", GLFW_KEY_I }, |
| 990 | + { "o", GLFW_KEY_O }, |
| 991 | + { "p", GLFW_KEY_P }, |
| 992 | + |
| 993 | + { "a", GLFW_KEY_A }, |
| 994 | + { "s", GLFW_KEY_S }, |
| 995 | + { "d", GLFW_KEY_D }, |
| 996 | + { "f", GLFW_KEY_F }, |
| 997 | + { "g", GLFW_KEY_G }, |
| 998 | + { "h", GLFW_KEY_H }, |
| 999 | + { "j", GLFW_KEY_J }, |
| 1000 | + { "k", GLFW_KEY_K }, |
| 1001 | + { "l", GLFW_KEY_L }, |
| 1002 | + |
| 1003 | + { "z", GLFW_KEY_Z }, |
| 1004 | + { "x", GLFW_KEY_X }, |
| 1005 | + { "c", GLFW_KEY_C }, |
| 1006 | + { "v", GLFW_KEY_V }, |
| 1007 | + { "b", GLFW_KEY_B }, |
| 1008 | + { "n", GLFW_KEY_N }, |
| 1009 | + { "m", GLFW_KEY_M }, |
| 1010 | + |
| 1011 | + { "1", GLFW_KEY_1 }, |
| 1012 | + { "2", GLFW_KEY_2 }, |
| 1013 | + { "3", GLFW_KEY_3 }, |
| 1014 | + { "4", GLFW_KEY_4 }, |
| 1015 | + { "5", GLFW_KEY_5 }, |
| 1016 | + { "6", GLFW_KEY_6 }, |
| 1017 | + { "7", GLFW_KEY_7 }, |
| 1018 | + { "8", GLFW_KEY_8 }, |
| 1019 | + { "9", GLFW_KEY_9 }, |
| 1020 | + { "0", GLFW_KEY_0 }, |
| 1021 | + |
| 1022 | + { "enter", GLFW_KEY_ENTER }, |
| 1023 | + { "space", GLFW_KEY_SPACE }, |
| 1024 | + { "shift", GLFW_KEY_LEFT_SHIFT | GLFW_KEY_RIGHT_SHIFT }, |
| 1025 | + { "backspace", GLFW_KEY_BACKSPACE }, |
| 1026 | + { "up", GLFW_KEY_UP }, |
| 1027 | + { "down", GLFW_KEY_DOWN }, |
| 1028 | + { "left", GLFW_KEY_LEFT }, |
| 1029 | + { "right", GLFW_KEY_RIGHT }, |
| 1030 | + }; |
| 1031 | + |
| 1032 | + auto it = map.find(key); |
| 1033 | + if (it != map.end()) return it->second; |
| 1034 | + return GLFW_KEY_UNKNOWN; |
| 900 | 1035 | } |
| 901 | 1036 | } // namespace lua |