Module input improvements
This commit is contained in:
@@ -7,8 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/math/modulevec2.h"
|
||||
#include "input/input.h"
|
||||
|
||||
static scriptproto_t MODULE_INPUT_PROTO;
|
||||
|
||||
// Static Methods
|
||||
moduleBaseFunction(moduleInputBind) {
|
||||
moduleBaseRequireArgs(2);
|
||||
moduleBaseRequireString(0);
|
||||
@@ -16,16 +21,18 @@ moduleBaseFunction(moduleInputBind) {
|
||||
|
||||
char_t strBtn[128];
|
||||
moduleBaseToString(args[0], strBtn, sizeof(strBtn));
|
||||
if(strBtn[0] == '\0') return moduleBaseThrow("inputBind: Button name cannot be empty");
|
||||
if(strBtn[0] == '\0') {
|
||||
return moduleBaseThrow("Input.bind: button name cannot be empty");
|
||||
}
|
||||
|
||||
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[1]);
|
||||
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("inputBind: Invalid action ID");
|
||||
if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.bind: invalid action");
|
||||
}
|
||||
|
||||
inputbutton_t btn = inputButtonGetByName(strBtn);
|
||||
if(btn.type == INPUT_BUTTON_TYPE_NONE) {
|
||||
return moduleBaseThrow("inputBind: Invalid button name");
|
||||
return moduleBaseThrow("Input.bind: invalid button name");
|
||||
}
|
||||
|
||||
inputBind(btn, action);
|
||||
@@ -33,77 +40,88 @@ moduleBaseFunction(moduleInputBind) {
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputIsDown) {
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireNumber(0);
|
||||
|
||||
moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
|
||||
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
|
||||
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("inputIsDown: Invalid action ID");
|
||||
if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.isDown: invalid action");
|
||||
}
|
||||
|
||||
return jerry_boolean(inputIsDown(action));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputPressed) {
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireNumber(0);
|
||||
|
||||
moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
|
||||
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
|
||||
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("inputPressed: Invalid action ID");
|
||||
if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.pressed: invalid action");
|
||||
}
|
||||
|
||||
return jerry_boolean(inputPressed(action));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputReleased) {
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireNumber(0);
|
||||
|
||||
moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
|
||||
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
|
||||
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("inputReleased: Invalid action ID");
|
||||
if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.released: invalid action");
|
||||
}
|
||||
|
||||
return jerry_boolean(inputReleased(action));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputGetValue) {
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireNumber(0);
|
||||
|
||||
moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
|
||||
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
|
||||
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("inputGetValue: Invalid action ID");
|
||||
if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.getValue: invalid action");
|
||||
}
|
||||
|
||||
return jerry_number(inputGetCurrentValue(action));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputAxis) {
|
||||
moduleBaseRequireArgs(2);
|
||||
moduleBaseRequireNumber(0);
|
||||
moduleBaseRequireNumber(1);
|
||||
|
||||
moduleBaseRequireNumber(0); moduleBaseRequireNumber(1);
|
||||
const inputaction_t neg = (inputaction_t)jerry_value_as_number(args[0]);
|
||||
const inputaction_t pos = (inputaction_t)jerry_value_as_number(args[1]);
|
||||
|
||||
if(neg < INPUT_ACTION_NULL || neg >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("inputAxis: Invalid negative action ID");
|
||||
if(neg <= INPUT_ACTION_NULL || neg >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.axis: invalid negative action");
|
||||
}
|
||||
if(pos < INPUT_ACTION_NULL || pos >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("inputAxis: Invalid positive action ID");
|
||||
if(pos <= INPUT_ACTION_NULL || pos >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.axis: invalid positive action");
|
||||
}
|
||||
|
||||
return jerry_number(inputAxis(neg, pos));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputAxis2D) {
|
||||
moduleBaseRequireArgs(4);
|
||||
moduleBaseRequireNumber(0); moduleBaseRequireNumber(1);
|
||||
moduleBaseRequireNumber(2); moduleBaseRequireNumber(3);
|
||||
const inputaction_t negX = (inputaction_t)jerry_value_as_number(args[0]);
|
||||
const inputaction_t posX = (inputaction_t)jerry_value_as_number(args[1]);
|
||||
const inputaction_t negY = (inputaction_t)jerry_value_as_number(args[2]);
|
||||
const inputaction_t posY = (inputaction_t)jerry_value_as_number(args[3]);
|
||||
if(negX <= INPUT_ACTION_NULL || negX >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.axis2D: invalid negX action");
|
||||
}
|
||||
if(posX <= INPUT_ACTION_NULL || posX >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.axis2D: invalid posX action");
|
||||
}
|
||||
if(negY <= INPUT_ACTION_NULL || negY >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.axis2D: invalid negY action");
|
||||
}
|
||||
if(posY <= INPUT_ACTION_NULL || posY >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.axis2D: invalid posY action");
|
||||
}
|
||||
vec2 result;
|
||||
inputAxis2D(negX, posX, negY, posY, result);
|
||||
return scriptProtoCreateValue(&MODULE_VEC2_PROTO, result);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputGetEventAction) {
|
||||
moduleBaseRequireArgs(1);
|
||||
|
||||
const inputevent_t *event = (const inputevent_t *)moduleBaseUnwrapPointer(args[0]);
|
||||
if(event == NULL) return moduleBaseThrow("inputGetEventAction: Expected input event object");
|
||||
|
||||
const inputevent_t *event = (const inputevent_t *)(
|
||||
moduleBaseUnwrapPointer(args[0])
|
||||
);
|
||||
if(event == NULL) {
|
||||
return moduleBaseThrow("Input.getEventAction: expected input event object");
|
||||
}
|
||||
return jerry_number(event->action);
|
||||
}
|
||||
|
||||
@@ -126,19 +144,19 @@ static void moduleInput(void) {
|
||||
#endif
|
||||
);
|
||||
|
||||
jerry_value_t evPressed = moduleBaseWrapPointer(&INPUT.eventPressed);
|
||||
moduleBaseSetValue("INPUT_EVENT_PRESSED", evPressed);
|
||||
jerry_value_free(evPressed);
|
||||
scriptProtoInit(&MODULE_INPUT_PROTO, "Input", sizeof(uint8_t), NULL);
|
||||
|
||||
jerry_value_t evReleased = moduleBaseWrapPointer(&INPUT.eventReleased);
|
||||
moduleBaseSetValue("INPUT_EVENT_RELEASED", evReleased);
|
||||
jerry_value_free(evReleased);
|
||||
|
||||
moduleBaseFunctionRegister("inputBind", moduleInputBind);
|
||||
moduleBaseFunctionRegister("inputIsDown", moduleInputIsDown);
|
||||
moduleBaseFunctionRegister("inputPressed", moduleInputPressed);
|
||||
moduleBaseFunctionRegister("inputReleased", moduleInputReleased);
|
||||
moduleBaseFunctionRegister("inputGetValue", moduleInputGetValue);
|
||||
moduleBaseFunctionRegister("inputAxis", moduleInputAxis);
|
||||
moduleBaseFunctionRegister("inputGetEventAction", moduleInputGetEventAction);
|
||||
#define X(name, method) \
|
||||
scriptProtoDefineStaticFunc( \
|
||||
&MODULE_INPUT_PROTO, #name, moduleInput##method \
|
||||
);
|
||||
X(bind, Bind);
|
||||
X(isDown, IsDown);
|
||||
X(pressed, Pressed);
|
||||
X(released, Released);
|
||||
X(getValue, GetValue);
|
||||
X(axis, Axis);
|
||||
X(axis2D, Axis2D);
|
||||
X(getEventAction, GetEventAction);
|
||||
#undef X
|
||||
}
|
||||
|
||||
@@ -7,23 +7,23 @@
|
||||
|
||||
#pragma once
|
||||
#include "script/module/script/modulescript.h"
|
||||
#include "script/module/math/modulemath.h"
|
||||
#include "script/module/entity/moduleentity.h"
|
||||
#include "script/module/input/moduleinput.h"
|
||||
#include "script/module/moduleplatform.h"
|
||||
#include "script/module/time/moduletime.h"
|
||||
#include "script/module/display/modulecolor.h"
|
||||
#include "script/module/math/modulemath.h"
|
||||
#include "script/module/display/modulescreen.h"
|
||||
#include "script/module/scene/modulescene.h"
|
||||
|
||||
static void moduleRegister(void) {
|
||||
moduleScript();
|
||||
moduleMath();
|
||||
moduleEntity();
|
||||
moduleInput();
|
||||
modulePlatform();
|
||||
moduleTime();
|
||||
moduleColor();
|
||||
moduleMath();
|
||||
moduleScreen();
|
||||
moduleScene();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user