Started on input, tired
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "lib_engine.h"
|
||||
#include "lib_render.h"
|
||||
#include "lib_input.h"
|
||||
#include "lib_standard.h"
|
||||
#include "repl_tools.h"
|
||||
|
||||
@@ -43,6 +44,7 @@ void initEngine() {
|
||||
initInterpreter(&engine.interpreter);
|
||||
injectNativeHook(&engine.interpreter, "engine", hookEngine);
|
||||
injectNativeHook(&engine.interpreter, "render", hookRender);
|
||||
injectNativeHook(&engine.interpreter, "input", hookInput);
|
||||
injectNativeHook(&engine.interpreter, "standard", hookStandard);
|
||||
|
||||
size_t size = 0;
|
||||
|
||||
@@ -198,8 +198,6 @@ static int nativeLoadNode(Interpreter* interpreter, LiteralArray* arguments) {
|
||||
|
||||
initEngineNode(node, &inner, tb, size);
|
||||
|
||||
//NOTE: initNode() must be called manually
|
||||
|
||||
// return the node
|
||||
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
|
||||
pushLiteralArray(&interpreter->stack, nodeLiteral);
|
||||
|
||||
71
core/lib_input.c
Normal file
71
core/lib_input.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "lib_input.h"
|
||||
|
||||
#include "memory.h"
|
||||
|
||||
//TODO: native input calls
|
||||
|
||||
//call the hook
|
||||
typedef struct Natives {
|
||||
char* name;
|
||||
NativeFn fn;
|
||||
} Natives;
|
||||
|
||||
int hookInput(Interpreter* interpreter, Literal identifier, Literal alias) {
|
||||
//build the natives list
|
||||
Natives natives[] = {
|
||||
// {"mapInputEventToKey", nativeMapInputEventToKey},
|
||||
// {"mapInputEventToMouse", nativeMapInputEventToMouse},
|
||||
// {"mapInputEventToSpecial", nativeMapInputEventToSpecial},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
//store the library in an aliased dictionary
|
||||
if (!IS_NULL(alias)) {
|
||||
//make sure the name isn't taken
|
||||
if (isDelcaredScopeVariable(interpreter->scope, alias)) {
|
||||
interpreter->errorOutput("Can't override an existing variable\n");
|
||||
freeLiteral(alias);
|
||||
return false;
|
||||
}
|
||||
|
||||
//create the dictionary to load up with functions
|
||||
LiteralDictionary* dictionary = ALLOCATE(LiteralDictionary, 1);
|
||||
initLiteralDictionary(dictionary);
|
||||
|
||||
//load the dict with functions
|
||||
for (int i = 0; natives[i].name; i++) {
|
||||
Literal name = TO_STRING_LITERAL(copyString(natives[i].name, strlen(natives[i].name)), strlen(natives[i].name));
|
||||
Literal func = TO_FUNCTION_LITERAL((void*)natives[i].fn, 0);
|
||||
func.type = LITERAL_FUNCTION_NATIVE;
|
||||
|
||||
setLiteralDictionary(dictionary, name, func);
|
||||
|
||||
freeLiteral(name);
|
||||
freeLiteral(func);
|
||||
}
|
||||
|
||||
//build the type
|
||||
Literal type = TO_TYPE_LITERAL(LITERAL_DICTIONARY, true);
|
||||
Literal strType = TO_TYPE_LITERAL(LITERAL_STRING, true);
|
||||
Literal fnType = TO_TYPE_LITERAL(LITERAL_FUNCTION_NATIVE, true);
|
||||
TYPE_PUSH_SUBTYPE(&type, strType);
|
||||
TYPE_PUSH_SUBTYPE(&type, fnType);
|
||||
|
||||
//set scope
|
||||
Literal dict = TO_DICTIONARY_LITERAL(dictionary);
|
||||
declareScopeVariable(interpreter->scope, alias, type);
|
||||
setScopeVariable(interpreter->scope, alias, dict, false);
|
||||
|
||||
//cleanup
|
||||
freeLiteral(dict);
|
||||
freeLiteral(type);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//default
|
||||
for (int i = 0; natives[i].name; i++) {
|
||||
injectNativeFn(interpreter, natives[i].name, natives[i].fn);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
6
core/lib_input.h
Normal file
6
core/lib_input.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "interpreter.h"
|
||||
|
||||
int hookInput(Interpreter* interpreter, Literal identifier, Literal alias);
|
||||
|
||||
@@ -58,8 +58,6 @@ static int nativeLoadRenderNode(Interpreter* interpreter, LiteralArray* argument
|
||||
|
||||
initRenderNode(node, &inner, tb, size);
|
||||
|
||||
//NOTE: initNode() must be called manually
|
||||
|
||||
// return the node
|
||||
Literal nodeLiteral = TO_OPAQUE_LITERAL(node, node->tag);
|
||||
pushLiteralArray(&interpreter->stack, nodeLiteral);
|
||||
@@ -196,10 +194,10 @@ static int nativeSetRectRenderNode(Interpreter* interpreter, LiteralArray* argum
|
||||
if (!IS_OPAQUE(nodeLiteral) || !IS_INTEGER(x) || !IS_INTEGER(y) || !IS_INTEGER(w) || !IS_INTEGER(h)) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to setRectRenderNode\n");
|
||||
freeLiteral(nodeLiteral);
|
||||
freeLiteral(xi);
|
||||
freeLiteral(yi);
|
||||
freeLiteral(wi);
|
||||
freeLiteral(hi);
|
||||
freeLiteral(x);
|
||||
freeLiteral(y);
|
||||
freeLiteral(w);
|
||||
freeLiteral(h);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -211,13 +209,15 @@ static int nativeSetRectRenderNode(Interpreter* interpreter, LiteralArray* argum
|
||||
|
||||
//cleanup
|
||||
freeLiteral(nodeLiteral);
|
||||
freeLiteral(xi);
|
||||
freeLiteral(yi);
|
||||
freeLiteral(wi);
|
||||
freeLiteral(hi);
|
||||
freeLiteral(x);
|
||||
freeLiteral(y);
|
||||
freeLiteral(w);
|
||||
freeLiteral(h);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//TODO: get x, y, w, h
|
||||
|
||||
static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* arguments) {
|
||||
if (arguments->count != 3 && arguments->count != 5) {
|
||||
interpreter->errorOutput("Incorrect number of arguments passed to drawRenderNode\n");
|
||||
@@ -264,10 +264,10 @@ static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* argument
|
||||
if (!IS_OPAQUE(nodeLiteral) || !IS_INTEGER(x) || !IS_INTEGER(y) || (!IS_INTEGER(w) && !IS_NULL(w)) || (!IS_INTEGER(h) && !IS_NULL(h))) {
|
||||
interpreter->errorOutput("Incorrect argument type passed to drawRenderNode\n");
|
||||
freeLiteral(nodeLiteral);
|
||||
freeLiteral(xi);
|
||||
freeLiteral(yi);
|
||||
freeLiteral(wi);
|
||||
freeLiteral(hi);
|
||||
freeLiteral(x);
|
||||
freeLiteral(y);
|
||||
freeLiteral(w);
|
||||
freeLiteral(h);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -288,10 +288,10 @@ static int nativeDrawRenderNode(Interpreter* interpreter, LiteralArray* argument
|
||||
|
||||
//cleanup
|
||||
freeLiteral(nodeLiteral);
|
||||
freeLiteral(xi);
|
||||
freeLiteral(yi);
|
||||
freeLiteral(wi);
|
||||
freeLiteral(hi);
|
||||
freeLiteral(x);
|
||||
freeLiteral(y);
|
||||
freeLiteral(w);
|
||||
freeLiteral(h);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user