From cf39a40f2962aafa7ff9ef0bfff10001c1b3ef7e Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 20 Jan 2021 21:42:14 -0600 Subject: [PATCH] Scripting quality of life adjustments --- core/Scripting/Lua.cpp | 36 +++-- core/Scripting/Lua.h | 1 + core/Scripting/ScriptingLanguage.h | 3 + core/constants.h | 250 +++++++++++++++++++++++++++++ demo/main.lua | 14 +- scripting/couch.i | 2 + 6 files changed, 289 insertions(+), 17 deletions(-) create mode 100644 core/constants.h diff --git a/core/Scripting/Lua.cpp b/core/Scripting/Lua.cpp index dcd58a7..ba22a53 100644 --- a/core/Scripting/Lua.cpp +++ b/core/Scripting/Lua.cpp @@ -19,10 +19,12 @@ void Lua::Initialize() { if (err != LUA_OK) { Error(); } - lua_getglobal(L, "init"); - err = lua_pcall(L, 0, 0, 0); - if (err != LUA_OK) { - Error(); + if (HasHook("init")) { + lua_getglobal(L, "init"); + err = lua_pcall(L, 0, 0, 0); + if (err != LUA_OK) { + Error(); + } } } else if (err == LUA_ERRFILE) { Util::Die("Could not find main.lua."); @@ -34,8 +36,12 @@ void Lua::Initialize() { // Bind input functions //glfwSetWindowUserPointer(window, (void*) L); Input *input = Input::GetInstance(); - input->keyHandlers.push_back(LuaKeyHandler); - input->mousePositionHandlers.push_back(LuaMousePositionHandler); + if (HasHook("onkey")) { + input->keyHandlers.push_back(LuaKeyHandler); + } + if (HasHook("onmousemotion")) { + input->mousePositionHandlers.push_back(LuaMousePositionHandler); + } #else // LUA_SCRIPTING Util::Die("Lua is selected as scripting language, but this binary was built without Lua support."); #endif // LUA_SCRIPTING @@ -43,9 +49,11 @@ void Lua::Initialize() { void Lua::Update(double delta) { #ifdef LUA_SCRIPTING - lua_getglobal(L, "update"); - lua_pushnumber(L, delta); - lua_call(L, 1, 0); + if (HasHook("update")) { + lua_getglobal(L, "update"); + lua_pushnumber(L, delta); + lua_call(L, 1, 0); + } #endif // LUA_SCRIPTING } @@ -63,6 +71,16 @@ void Lua::Error() { Util::Die("Whut?"); } +bool Lua::HasHook(const char *name) { + bool exists = false; +#ifdef LUA_SCRIPTING + int type = lua_getglobal(L, name); + lua_pop(L, -1); + exists = type != LUA_TNIL; +#endif // LUA_SCRIPTING + return exists; +} + void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods) { #ifdef LUA_SCRIPTING // lua_State *L = (lua_State*) glfwGetWindowUserPointer(window); diff --git a/core/Scripting/Lua.h b/core/Scripting/Lua.h index 60968ab..524e626 100644 --- a/core/Scripting/Lua.h +++ b/core/Scripting/Lua.h @@ -22,6 +22,7 @@ public: void Update(double delta); void Close(); void Error(); + bool HasHook(const char *name); private: #ifdef LUA_SCRIPTING static lua_State *L; diff --git a/core/Scripting/ScriptingLanguage.h b/core/Scripting/ScriptingLanguage.h index 995da64..d34d9b3 100644 --- a/core/Scripting/ScriptingLanguage.h +++ b/core/Scripting/ScriptingLanguage.h @@ -1,12 +1,15 @@ #ifndef SCRIPTINGLANGUAGE_H #define SCRIPTINGLANGUAGE_H +#include "types.h" + class ScriptingLanguage { public: virtual void Initialize() = 0; virtual void Update(double delta) = 0; virtual void Close() = 0; virtual void Error() = 0; + virtual bool HasHook(const char * name) = 0; static ScriptingLanguage *GetCurrentLanguage(); protected: static ScriptingLanguage *language; diff --git a/core/constants.h b/core/constants.h new file mode 100644 index 0000000..c6f946b --- /dev/null +++ b/core/constants.h @@ -0,0 +1,250 @@ +#ifndef CONSTANTS_H +#define CONSTANTS_H + +#define KEY_SPACE 32 +#define KEY_APOSTROPHE 39 /* ' */ +#define KEY_COMMA 44 /* , */ +#define KEY_MINUS 45 /* - */ +#define KEY_PERIOD 46 /* . */ +#define KEY_SLASH 47 /* / */ +#define KEY_0 48 +#define KEY_1 49 +#define KEY_2 50 +#define KEY_3 51 +#define KEY_4 52 +#define KEY_5 53 +#define KEY_6 54 +#define KEY_7 55 +#define KEY_8 56 +#define KEY_9 57 +#define KEY_SEMICOLON 59 /* ; */ +#define KEY_EQUAL 61 /* = */ +#define KEY_A 65 +#define KEY_B 66 +#define KEY_C 67 +#define KEY_D 68 +#define KEY_E 69 +#define KEY_F 70 +#define KEY_G 71 +#define KEY_H 72 +#define KEY_I 73 +#define KEY_J 74 +#define KEY_K 75 +#define KEY_L 76 +#define KEY_M 77 +#define KEY_N 78 +#define KEY_O 79 +#define KEY_P 80 +#define KEY_Q 81 +#define KEY_R 82 +#define KEY_S 83 +#define KEY_T 84 +#define KEY_U 85 +#define KEY_V 86 +#define KEY_W 87 +#define KEY_X 88 +#define KEY_Y 89 +#define KEY_Z 90 +#define KEY_LEFT_BRACKET 91 /* [ */ +#define KEY_BACKSLASH 92 /* \ */ +#define KEY_RIGHT_BRACKET 93 /* ] */ +#define KEY_GRAVE_ACCENT 96 /* ` */ +#define KEY_WORLD_1 161 /* non-US #1 */ +#define KEY_WORLD_2 162 /* non-US #2 */ + +/* Function keys */ +#define KEY_ESCAPE 256 +#define KEY_ENTER 257 +#define KEY_TAB 258 +#define KEY_BACKSPACE 259 +#define KEY_INSERT 260 +#define KEY_DELETE 261 +#define KEY_RIGHT 262 +#define KEY_LEFT 263 +#define KEY_DOWN 264 +#define KEY_UP 265 +#define KEY_PAGE_UP 266 +#define KEY_PAGE_DOWN 267 +#define KEY_HOME 268 +#define KEY_END 269 +#define KEY_CAPS_LOCK 280 +#define KEY_SCROLL_LOCK 281 +#define KEY_NUM_LOCK 282 +#define KEY_PRINT_SCREEN 283 +#define KEY_PAUSE 284 +#define KEY_F1 290 +#define KEY_F2 291 +#define KEY_F3 292 +#define KEY_F4 293 +#define KEY_F5 294 +#define KEY_F6 295 +#define KEY_F7 296 +#define KEY_F8 297 +#define KEY_F9 298 +#define KEY_F10 299 +#define KEY_F11 300 +#define KEY_F12 301 +#define KEY_F13 302 +#define KEY_F14 303 +#define KEY_F15 304 +#define KEY_F16 305 +#define KEY_F17 306 +#define KEY_F18 307 +#define KEY_F19 308 +#define KEY_F20 309 +#define KEY_F21 310 +#define KEY_F22 311 +#define KEY_F23 312 +#define KEY_F24 313 +#define KEY_F25 314 +#define KEY_KP_0 320 +#define KEY_KP_1 321 +#define KEY_KP_2 322 +#define KEY_KP_3 323 +#define KEY_KP_4 324 +#define KEY_KP_5 325 +#define KEY_KP_6 326 +#define KEY_KP_7 327 +#define KEY_KP_8 328 +#define KEY_KP_9 329 +#define KEY_KP_DECIMAL 330 +#define KEY_KP_DIVIDE 331 +#define KEY_KP_MULTIPLY 332 +#define KEY_KP_SUBTRACT 333 +#define KEY_KP_ADD 334 +#define KEY_KP_ENTER 335 +#define KEY_KP_EQUAL 336 +#define KEY_LEFT_SHIFT 340 +#define KEY_LEFT_CONTROL 341 +#define KEY_LEFT_ALT 342 +#define KEY_LEFT_SUPER 343 +#define KEY_RIGHT_SHIFT 344 +#define KEY_RIGHT_CONTROL 345 +#define KEY_RIGHT_ALT 346 +#define KEY_RIGHT_SUPER 347 +#define KEY_MENU 348 +#define KEY_SPACE 32 +#define KEY_APOSTROPHE 39 /* ' */ +#define KEY_COMMA 44 /* , */ +#define KEY_MINUS 45 /* - */ +#define KEY_PERIOD 46 /* . */ +#define KEY_SLASH 47 /* / */ +#define KEY_0 48 +#define KEY_1 49 +#define KEY_2 50 +#define KEY_3 51 +#define KEY_4 52 +#define KEY_5 53 +#define KEY_6 54 +#define KEY_7 55 +#define KEY_8 56 +#define KEY_9 57 +#define KEY_SEMICOLON 59 /* ; */ +#define KEY_EQUAL 61 /* = */ +#define KEY_A 65 +#define KEY_B 66 +#define KEY_C 67 +#define KEY_D 68 +#define KEY_E 69 +#define KEY_F 70 +#define KEY_G 71 +#define KEY_H 72 +#define KEY_I 73 +#define KEY_J 74 +#define KEY_K 75 +#define KEY_L 76 +#define KEY_M 77 +#define KEY_N 78 +#define KEY_O 79 +#define KEY_P 80 +#define KEY_Q 81 +#define KEY_R 82 +#define KEY_S 83 +#define KEY_T 84 +#define KEY_U 85 +#define KEY_V 86 +#define KEY_W 87 +#define KEY_X 88 +#define KEY_Y 89 +#define KEY_Z 90 +#define KEY_LEFT_BRACKET 91 /* [ */ +#define KEY_BACKSLASH 92 /* \ */ +#define KEY_RIGHT_BRACKET 93 /* ] */ +#define KEY_GRAVE_ACCENT 96 /* ` */ +#define KEY_WORLD_1 161 /* non-US #1 */ +#define KEY_WORLD_2 162 /* non-US #2 */ + +/* Function keys */ +#define KEY_ESCAPE 256 +#define KEY_ENTER 257 +#define KEY_TAB 258 +#define KEY_BACKSPACE 259 +#define KEY_INSERT 260 +#define KEY_DELETE 261 +#define KEY_RIGHT 262 +#define KEY_LEFT 263 +#define KEY_DOWN 264 +#define KEY_UP 265 +#define KEY_PAGE_UP 266 +#define KEY_PAGE_DOWN 267 +#define KEY_HOME 268 +#define KEY_END 269 +#define KEY_CAPS_LOCK 280 +#define KEY_SCROLL_LOCK 281 +#define KEY_NUM_LOCK 282 +#define KEY_PRINT_SCREEN 283 +#define KEY_PAUSE 284 +#define KEY_F1 290 +#define KEY_F2 291 +#define KEY_F3 292 +#define KEY_F4 293 +#define KEY_F5 294 +#define KEY_F6 295 +#define KEY_F7 296 +#define KEY_F8 297 +#define KEY_F9 298 +#define KEY_F10 299 +#define KEY_F11 300 +#define KEY_F12 301 +#define KEY_F13 302 +#define KEY_F14 303 +#define KEY_F15 304 +#define KEY_F16 305 +#define KEY_F17 306 +#define KEY_F18 307 +#define KEY_F19 308 +#define KEY_F20 309 +#define KEY_F21 310 +#define KEY_F22 311 +#define KEY_F23 312 +#define KEY_F24 313 +#define KEY_F25 314 +#define KEY_KP_0 320 +#define KEY_KP_1 321 +#define KEY_KP_2 322 +#define KEY_KP_3 323 +#define KEY_KP_4 324 +#define KEY_KP_5 325 +#define KEY_KP_6 326 +#define KEY_KP_7 327 +#define KEY_KP_8 328 +#define KEY_KP_9 329 +#define KEY_KP_DECIMAL 330 +#define KEY_KP_DIVIDE 331 +#define KEY_KP_MULTIPLY 332 +#define KEY_KP_SUBTRACT 333 +#define KEY_KP_ADD 334 +#define KEY_KP_ENTER 335 +#define KEY_KP_EQUAL 336 +#define KEY_LEFT_SHIFT 340 +#define KEY_LEFT_CONTROL 341 +#define KEY_LEFT_ALT 342 +#define KEY_LEFT_SUPER 343 +#define KEY_RIGHT_SHIFT 344 +#define KEY_RIGHT_CONTROL 345 +#define KEY_RIGHT_ALT 346 +#define KEY_RIGHT_SUPER 347 +#define KEY_MENU 348 + + +#endif /* CONSTANTS_H */ diff --git a/demo/main.lua b/demo/main.lua index ef8de9b..afd8258 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -4,8 +4,6 @@ local ball local ball1 local camera -local LEFT = 263 -local RIGHT = 262 local UP = 265 local DOWN = 264 local Q = 81 @@ -92,19 +90,19 @@ function update(delta) end function onkey(key, code, action, mod) - if key == LEFT and action == 1 then + if key == KEY_LEFT and action == 1 then vx = -1.0 - elseif key == RIGHT and action == 1 then + elseif key == KEY_RIGHT and action == 1 then vx = 1.0 - elseif (key == LEFT or key == RIGHT) and action == 0 then + elseif (key == KEY_LEFT or key == KEY_RIGHT) and action == 0 then vx = 0.0 end - if key == UP and action == 1 then + if key == couch.KEY_W and action == 1 then vz = 1.0 - elseif key == DOWN and action == 1 then + elseif key == couch.KEY_S and action == 1 then vz = -1.0 - elseif (key == DOWN or key == UP) and action == 0 then + elseif (key == couch.KEY_W or key == couch.KEY_S) and action == 0 then vz = 0.0 end diff --git a/scripting/couch.i b/scripting/couch.i index 0bc3f16..9a75359 100644 --- a/scripting/couch.i +++ b/scripting/couch.i @@ -7,6 +7,7 @@ %{ #include "types.h" +#include "constants.h" #include "Node.h" #include "Transform.h" #include "Spatial.h" @@ -37,6 +38,7 @@ public: %ignore "Vector3"; %include "types.h" +%include "constants.h" %include "Node.h" %include "Spatial.h" %include "Mesh.h"