Scripting quality of life adjustments

This commit is contained in:
Dane Johnson 2021-01-20 21:42:14 -06:00
parent 087a75eaab
commit cf39a40f29
6 changed files with 289 additions and 17 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

250
core/constants.h Normal file
View File

@ -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 */

View File

@ -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

View File

@ -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"