input.d 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. module scripts.lua.input;
  2. import bindbc.lua;
  3. import raylib;
  4. import variables;
  5. import graphics.effects;
  6. import std.conv;
  7. import system.abstraction;
  8. import system.config;
  9. import std.string;
  10. import std.algorithm;
  11. import system.init;
  12. import graphics.playback;
  13. import std.file;
  14. import graphics.collision;
  15. import audio.sfx;
  16. float lastKeyPressTime = 0.0;
  17. immutable float keyPressCooldown = 0.001;
  18. extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
  19. {
  20. try
  21. {
  22. int keyCode = cast(int)luaL_checkinteger(L, 1);
  23. bool isPressed = IsKeyPressed(keyCode).to!bool;
  24. double currentTime = GetTime();
  25. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  26. {
  27. lua_pushboolean(L, false);
  28. }
  29. else if (isPressed)
  30. {
  31. lastKeyPressTime = currentTime;
  32. lua_pushboolean(L, true);
  33. }
  34. else
  35. {
  36. lua_pushboolean(L, false);
  37. }
  38. return 1;
  39. }
  40. catch (Exception e)
  41. {
  42. debugWriteln(e.msg);
  43. lua_pushboolean(L, false);
  44. return 1;
  45. }
  46. }
  47. extern (C) nothrow int luaL_isKeyDown(lua_State *L) {
  48. return IsKeyDown(cast(int)luaL_checkinteger(L, 1));
  49. }
  50. extern (C) nothrow int luaL_isMouseButtonPressed(lua_State* L)
  51. {
  52. try
  53. {
  54. int keyCode = cast(int)luaL_checkinteger(L, 1);
  55. bool isPressed = IsMouseButtonPressed(keyCode);
  56. double currentTime = GetTime();
  57. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  58. {
  59. lua_pushboolean(L, false);
  60. }
  61. else if (isPressed)
  62. {
  63. lastKeyPressTime = currentTime;
  64. lua_pushboolean(L, true);
  65. }
  66. else
  67. {
  68. lua_pushboolean(L, false);
  69. }
  70. return 1;
  71. }
  72. catch (Exception e)
  73. {
  74. debugWriteln(e.msg);
  75. lua_pushboolean(L, false);
  76. return 1;
  77. }
  78. }
  79. extern (C) nothrow int luaL_isMouseButtonDown(lua_State* L)
  80. {
  81. try
  82. {
  83. int keyCode = cast(int)luaL_checkinteger(L, 1);
  84. bool isPressed = IsMouseButtonDown(keyCode);
  85. double currentTime = GetTime();
  86. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  87. {
  88. lua_pushboolean(L, false);
  89. }
  90. else if (isPressed)
  91. {
  92. lastKeyPressTime = currentTime;
  93. lua_pushboolean(L, true);
  94. }
  95. else
  96. {
  97. lua_pushboolean(L, false);
  98. }
  99. return 1;
  100. }
  101. catch (Exception e)
  102. {
  103. debugWriteln(e.msg);
  104. lua_pushboolean(L, false);
  105. return 1;
  106. }
  107. }
  108. extern (C) nothrow int luaL_getMouseX(lua_State *L) {
  109. lua_pushnumber(L, GetMousePosition.x);
  110. return 1;
  111. }
  112. extern (C) nothrow int luaL_getMouseY(lua_State *L) {
  113. lua_pushnumber(L, GetMousePosition.y);
  114. return 1;
  115. }