Luaエンジン:キーボードAPI

守矢諏訪子 <suwako@076.moe>、2026-06-28 02:30
コミット da3b0b636a84e6c2d22aabc506bbb43d6eb2e485 ・親 8cb968d ・ファイルを見る

5個のファイル、+147 -15

@@ -108,6 +108,7 @@
108108 void BindMath(sol::state &lua);
109109 void BindGraphics(sol::state &lua);
110110 void Execute(sol::state &lua);
111+ i32 StringToKey(const string &key);
111112
112113 private:
113114 vector<MeshObject> m_Objects;
@@ -64,6 +64,7 @@
6464 bool isNewShader = false;
6565
6666 bool showNewShaderPopup = false;
67+ bool viewportFocused = false;
6768 bool pendingCompile = false;
6869
6970 std::string version = version::full;
@@ -203,18 +203,12 @@
203203 },
204204
205205 indices = {
206- -- mae
207- 0,1,2, 2,3,0,
208- -- migi
209- 1,5,6, 6,2,1,
210- -- usiro
211- 5,4,7, 7,6,5,
212- -- hidari
213- 4,0,3, 3,7,4,
214- -- ue
215- 3,2,6, 6,7,3,
216- -- sita
217- 4,5,1, 1,0,4
206+ 0,1,2, 2,3,0, -- mae
207+ 1,5,6, 6,2,1, -- migi
208+ 5,4,7, 7,6,5, -- usiro
209+ 4,0,3, 3,7,4, -- hidari
210+ 3,2,6, 6,7,3, -- ue
211+ 4,5,1, 1,0,4 -- sita
218212 },
219213
220214 attributes = {
@@ -238,7 +232,7 @@
238232
239233 le.gfx.set_rotation(0, t*0.7, t*1.0, t*0.4)
240234 le.gfx.set_rotation(1, t*0.2, t*0.8, t*1.4)
241- le.gfx.set_rotation(2, t*0.2, t*0.8, t*1.4)
235+ le.gfx.set_rotation(2, t*0.4, t*0.3, t*0.8)
242236
243237 local reso = le.sys.get_resolution()
244238 local aspect = reso.x / reso.y
@@ -117,6 +117,7 @@
117117 void ViewPort::Draw(Info &info) {
118118 string tit = info.i18n->GetWord("editorviewport") + "###View";
119119 ImGui::Begin(tit.c_str());
120+ info.viewportFocused = ImGui::IsWindowFocused();
120121
121122 ImVec2 avail = ImGui::GetContentRegionAvail();
122123 if ((i32)avail.x != m_Width || (i32)avail.y != m_Height) {
@@ -155,6 +155,18 @@
155155 sol::lib::string
156156 );
157157
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+
158170 out.new_usertype<Vector2i>(
159171 "Vector2i",
160172 "x", sol::property(
@@ -203,6 +215,22 @@
203215 )
204216 );
205217
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+
206234 out.new_usertype<Vector3i>(
207235 "Vector3i",
208236 "x", sol::property(
@@ -267,6 +295,26 @@
267295 )
268296 );
269297
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+
270318 out.new_usertype<Vector4i>(
271319 "Vector4i",
272320 "x", sol::property(
@@ -497,6 +545,7 @@
497545
498546 void LuaEngine::BindIO(sol::state &lua) {
499547 lua["le"]["io"] = lua.create_table();
548+ lua["le"]["io"]["key"] = lua.create_table();
500549
501550 lua["le"]["io"]["get_mouse"] = [&]() -> Vector2d {
502551 Vector2d cursor{};
@@ -504,9 +553,20 @@
504553 return cursor;
505554 };
506555
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;
509559 };
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+ };
510570 }
511571
512572 void LuaEngine::BindMath(sol::state &lua) {
@@ -651,16 +711,34 @@
651711 m_Objects.push_back(obj);
652712 };
653713
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+
654720 lua["le"]["gfx"]["set_position"] = [&](i32 index, f32 x, f32 y, f32 z) {
655721 if (index < 0 || index >= m_Objects.size()) return;
656722 m_Objects[index].position = { x, y, z };
657723 };
658724
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+
659731 lua["le"]["gfx"]["set_rotation"] = [&](i32 index, f32 x, f32 y, f32 z) {
660732 if (index < 0 || index >= m_Objects.size()) return;
661733 m_Objects[index].rotation = { x, y, z };
662734 };
663735
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+
664742 lua["le"]["gfx"]["set_scale"] = [&](i32 index, f32 x, f32 y, f32 z) {
665743 if (index < 0 || index >= m_Objects.size()) return;
666744 m_Objects[index].scale = { x, y, z };
@@ -897,5 +975,62 @@
897975 sol::error err = res;
898976 LogError(err);
899977 }
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;
9001035 }
9011036 } // namespace lua