Module input improvements

This commit is contained in:
2026-04-29 23:40:01 -05:00
parent 010900fe21
commit 3d984e13c2
4 changed files with 128 additions and 111 deletions
+3 -4
View File
@@ -12,10 +12,9 @@ Object.assign(CubeEntity.prototype, Entity.prototype);
CubeEntity.prototype.update = function() { CubeEntity.prototype.update = function() {
var speed = 3.0; var speed = 3.0;
var dx = inputAxis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT); var move = Input.axis2D(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT, INPUT_ACTION_UP, INPUT_ACTION_DOWN);
var dz = inputAxis(INPUT_ACTION_UP, INPUT_ACTION_DOWN); this.position.x += move.x * speed * TIME.delta;
this.position.x += dx * speed * TIME.delta; this.position.z += move.y * speed * TIME.delta;
this.position.z += dz * speed * TIME.delta;
this.material.setColor(Color.rainbow()); this.material.setColor(Color.rainbow());
}; };
+50 -50
View File
@@ -1,75 +1,75 @@
// Default input bindings. // Default input bindings.
if (typeof PSP !== 'undefined') { if (typeof PSP !== 'undefined') {
inputBind("up", INPUT_ACTION_UP); Input.bind("up", INPUT_ACTION_UP);
inputBind("down", INPUT_ACTION_DOWN); Input.bind("down", INPUT_ACTION_DOWN);
inputBind("left", INPUT_ACTION_LEFT); Input.bind("left", INPUT_ACTION_LEFT);
inputBind("right", INPUT_ACTION_RIGHT); Input.bind("right", INPUT_ACTION_RIGHT);
inputBind("accept", INPUT_ACTION_ACCEPT); Input.bind("accept", INPUT_ACTION_ACCEPT);
inputBind("cancel", INPUT_ACTION_CANCEL); Input.bind("cancel", INPUT_ACTION_CANCEL);
inputBind("select", INPUT_ACTION_RAGEQUIT); Input.bind("select", INPUT_ACTION_RAGEQUIT);
inputBind("lstick_up", INPUT_ACTION_UP); Input.bind("lstick_up", INPUT_ACTION_UP);
inputBind("lstick_down", INPUT_ACTION_DOWN); Input.bind("lstick_down", INPUT_ACTION_DOWN);
inputBind("lstick_left", INPUT_ACTION_LEFT); Input.bind("lstick_left", INPUT_ACTION_LEFT);
inputBind("lstick_right", INPUT_ACTION_RIGHT); Input.bind("lstick_right", INPUT_ACTION_RIGHT);
inputBind("triangle", INPUT_ACTION_CONSOLE); Input.bind("triangle", INPUT_ACTION_CONSOLE);
} else if (typeof DOLPHIN !== 'undefined') { } else if (typeof DOLPHIN !== 'undefined') {
inputBind("up", INPUT_ACTION_UP); Input.bind("up", INPUT_ACTION_UP);
inputBind("down", INPUT_ACTION_DOWN); Input.bind("down", INPUT_ACTION_DOWN);
inputBind("left", INPUT_ACTION_LEFT); Input.bind("left", INPUT_ACTION_LEFT);
inputBind("right", INPUT_ACTION_RIGHT); Input.bind("right", INPUT_ACTION_RIGHT);
inputBind("b", INPUT_ACTION_CANCEL); Input.bind("b", INPUT_ACTION_CANCEL);
inputBind("a", INPUT_ACTION_ACCEPT); Input.bind("a", INPUT_ACTION_ACCEPT);
inputBind("z", INPUT_ACTION_CONSOLE); Input.bind("z", INPUT_ACTION_CONSOLE);
inputBind("lstick_up", INPUT_ACTION_UP); Input.bind("lstick_up", INPUT_ACTION_UP);
inputBind("lstick_down", INPUT_ACTION_DOWN); Input.bind("lstick_down", INPUT_ACTION_DOWN);
inputBind("lstick_left", INPUT_ACTION_LEFT); Input.bind("lstick_left", INPUT_ACTION_LEFT);
inputBind("lstick_right", INPUT_ACTION_RIGHT); Input.bind("lstick_right", INPUT_ACTION_RIGHT);
} else if (typeof LINUX !== 'undefined') { } else if (typeof LINUX !== 'undefined') {
if (typeof INPUT_KEYBOARD !== 'undefined') { if (typeof INPUT_KEYBOARD !== 'undefined') {
inputBind("w", INPUT_ACTION_UP); Input.bind("w", INPUT_ACTION_UP);
inputBind("s", INPUT_ACTION_DOWN); Input.bind("s", INPUT_ACTION_DOWN);
inputBind("a", INPUT_ACTION_LEFT); Input.bind("a", INPUT_ACTION_LEFT);
inputBind("d", INPUT_ACTION_RIGHT); Input.bind("d", INPUT_ACTION_RIGHT);
inputBind("left", INPUT_ACTION_LEFT); Input.bind("left", INPUT_ACTION_LEFT);
inputBind("right", INPUT_ACTION_RIGHT); Input.bind("right", INPUT_ACTION_RIGHT);
inputBind("up", INPUT_ACTION_UP); Input.bind("up", INPUT_ACTION_UP);
inputBind("down", INPUT_ACTION_DOWN); Input.bind("down", INPUT_ACTION_DOWN);
inputBind("enter", INPUT_ACTION_ACCEPT); Input.bind("enter", INPUT_ACTION_ACCEPT);
inputBind("e", INPUT_ACTION_ACCEPT); Input.bind("e", INPUT_ACTION_ACCEPT);
inputBind("q", INPUT_ACTION_CANCEL); Input.bind("q", INPUT_ACTION_CANCEL);
inputBind("escape", INPUT_ACTION_RAGEQUIT); Input.bind("escape", INPUT_ACTION_RAGEQUIT);
inputBind("`", INPUT_ACTION_CONSOLE); Input.bind("`", INPUT_ACTION_CONSOLE);
} }
if (typeof INPUT_GAMEPAD !== 'undefined') { if (typeof INPUT_GAMEPAD !== 'undefined') {
inputBind("gamepad_up", INPUT_ACTION_UP); Input.bind("gamepad_up", INPUT_ACTION_UP);
inputBind("gamepad_down", INPUT_ACTION_DOWN); Input.bind("gamepad_down", INPUT_ACTION_DOWN);
inputBind("gamepad_left", INPUT_ACTION_LEFT); Input.bind("gamepad_left", INPUT_ACTION_LEFT);
inputBind("gamepad_right", INPUT_ACTION_RIGHT); Input.bind("gamepad_right", INPUT_ACTION_RIGHT);
inputBind("gamepad_a", INPUT_ACTION_ACCEPT); Input.bind("gamepad_a", INPUT_ACTION_ACCEPT);
inputBind("gamepad_b", INPUT_ACTION_CANCEL); Input.bind("gamepad_b", INPUT_ACTION_CANCEL);
inputBind("gamepad_back", INPUT_ACTION_RAGEQUIT); Input.bind("gamepad_back", INPUT_ACTION_RAGEQUIT);
inputBind("gamepad_lstick_up", INPUT_ACTION_UP); Input.bind("gamepad_lstick_up", INPUT_ACTION_UP);
inputBind("gamepad_lstick_down", INPUT_ACTION_DOWN); Input.bind("gamepad_lstick_down", INPUT_ACTION_DOWN);
inputBind("gamepad_lstick_left", INPUT_ACTION_LEFT); Input.bind("gamepad_lstick_left", INPUT_ACTION_LEFT);
inputBind("gamepad_lstick_right", INPUT_ACTION_RIGHT); Input.bind("gamepad_lstick_right", INPUT_ACTION_RIGHT);
} }
if (typeof INPUT_POINTER !== 'undefined') { if (typeof INPUT_POINTER !== 'undefined') {
inputBind("mouse_x", INPUT_ACTION_POINTERX); Input.bind("mouse_x", INPUT_ACTION_POINTERX);
inputBind("mouse_y", INPUT_ACTION_POINTERY); Input.bind("mouse_y", INPUT_ACTION_POINTERY);
} }
} else { } else {
consolePrint("Unknown platform, no default input bindings set."); print("Unknown platform, no default input bindings set.");
} }
Scene.set('scenes/cube.js'); Scene.set('scenes/cube.js');
+73 -55
View File
@@ -7,8 +7,13 @@
#pragma once #pragma once
#include "script/module/modulebase.h" #include "script/module/modulebase.h"
#include "script/scriptproto.h"
#include "script/module/math/modulevec2.h"
#include "input/input.h" #include "input/input.h"
static scriptproto_t MODULE_INPUT_PROTO;
// Static Methods
moduleBaseFunction(moduleInputBind) { moduleBaseFunction(moduleInputBind) {
moduleBaseRequireArgs(2); moduleBaseRequireArgs(2);
moduleBaseRequireString(0); moduleBaseRequireString(0);
@@ -16,16 +21,18 @@ moduleBaseFunction(moduleInputBind) {
char_t strBtn[128]; char_t strBtn[128];
moduleBaseToString(args[0], strBtn, sizeof(strBtn)); 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]); const inputaction_t action = (inputaction_t)jerry_value_as_number(args[1]);
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) { if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
return moduleBaseThrow("inputBind: Invalid action ID"); return moduleBaseThrow("Input.bind: invalid action");
} }
inputbutton_t btn = inputButtonGetByName(strBtn); inputbutton_t btn = inputButtonGetByName(strBtn);
if(btn.type == INPUT_BUTTON_TYPE_NONE) { if(btn.type == INPUT_BUTTON_TYPE_NONE) {
return moduleBaseThrow("inputBind: Invalid button name"); return moduleBaseThrow("Input.bind: invalid button name");
} }
inputBind(btn, action); inputBind(btn, action);
@@ -33,77 +40,88 @@ moduleBaseFunction(moduleInputBind) {
} }
moduleBaseFunction(moduleInputIsDown) { moduleBaseFunction(moduleInputIsDown) {
moduleBaseRequireArgs(1); moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
moduleBaseRequireNumber(0);
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]); const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) { if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
return moduleBaseThrow("inputIsDown: Invalid action ID"); return moduleBaseThrow("Input.isDown: invalid action");
} }
return jerry_boolean(inputIsDown(action)); return jerry_boolean(inputIsDown(action));
} }
moduleBaseFunction(moduleInputPressed) { moduleBaseFunction(moduleInputPressed) {
moduleBaseRequireArgs(1); moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
moduleBaseRequireNumber(0);
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]); const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) { if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
return moduleBaseThrow("inputPressed: Invalid action ID"); return moduleBaseThrow("Input.pressed: invalid action");
} }
return jerry_boolean(inputPressed(action)); return jerry_boolean(inputPressed(action));
} }
moduleBaseFunction(moduleInputReleased) { moduleBaseFunction(moduleInputReleased) {
moduleBaseRequireArgs(1); moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
moduleBaseRequireNumber(0);
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]); const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) { if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
return moduleBaseThrow("inputReleased: Invalid action ID"); return moduleBaseThrow("Input.released: invalid action");
} }
return jerry_boolean(inputReleased(action)); return jerry_boolean(inputReleased(action));
} }
moduleBaseFunction(moduleInputGetValue) { moduleBaseFunction(moduleInputGetValue) {
moduleBaseRequireArgs(1); moduleBaseRequireArgs(1); moduleBaseRequireNumber(0);
moduleBaseRequireNumber(0);
const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]); const inputaction_t action = (inputaction_t)jerry_value_as_number(args[0]);
if(action < INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) { if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
return moduleBaseThrow("inputGetValue: Invalid action ID"); return moduleBaseThrow("Input.getValue: invalid action");
} }
return jerry_number(inputGetCurrentValue(action)); return jerry_number(inputGetCurrentValue(action));
} }
moduleBaseFunction(moduleInputAxis) { moduleBaseFunction(moduleInputAxis) {
moduleBaseRequireArgs(2); moduleBaseRequireArgs(2);
moduleBaseRequireNumber(0); moduleBaseRequireNumber(0); moduleBaseRequireNumber(1);
moduleBaseRequireNumber(1);
const inputaction_t neg = (inputaction_t)jerry_value_as_number(args[0]); const inputaction_t neg = (inputaction_t)jerry_value_as_number(args[0]);
const inputaction_t pos = (inputaction_t)jerry_value_as_number(args[1]); const inputaction_t pos = (inputaction_t)jerry_value_as_number(args[1]);
if(neg <= INPUT_ACTION_NULL || neg >= INPUT_ACTION_COUNT) {
if(neg < INPUT_ACTION_NULL || neg >= INPUT_ACTION_COUNT) { return moduleBaseThrow("Input.axis: invalid negative action");
return moduleBaseThrow("inputAxis: Invalid negative action ID");
} }
if(pos < INPUT_ACTION_NULL || pos >= INPUT_ACTION_COUNT) { if(pos <= INPUT_ACTION_NULL || pos >= INPUT_ACTION_COUNT) {
return moduleBaseThrow("inputAxis: Invalid positive action ID"); return moduleBaseThrow("Input.axis: invalid positive action");
} }
return jerry_number(inputAxis(neg, pos)); 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) { moduleBaseFunction(moduleInputGetEventAction) {
moduleBaseRequireArgs(1); moduleBaseRequireArgs(1);
const inputevent_t *event = (const inputevent_t *)(
const inputevent_t *event = (const inputevent_t *)moduleBaseUnwrapPointer(args[0]); moduleBaseUnwrapPointer(args[0])
if(event == NULL) return moduleBaseThrow("inputGetEventAction: Expected input event object"); );
if(event == NULL) {
return moduleBaseThrow("Input.getEventAction: expected input event object");
}
return jerry_number(event->action); return jerry_number(event->action);
} }
@@ -126,19 +144,19 @@ static void moduleInput(void) {
#endif #endif
); );
jerry_value_t evPressed = moduleBaseWrapPointer(&INPUT.eventPressed); scriptProtoInit(&MODULE_INPUT_PROTO, "Input", sizeof(uint8_t), NULL);
moduleBaseSetValue("INPUT_EVENT_PRESSED", evPressed);
jerry_value_free(evPressed);
jerry_value_t evReleased = moduleBaseWrapPointer(&INPUT.eventReleased); #define X(name, method) \
moduleBaseSetValue("INPUT_EVENT_RELEASED", evReleased); scriptProtoDefineStaticFunc( \
jerry_value_free(evReleased); &MODULE_INPUT_PROTO, #name, moduleInput##method \
);
moduleBaseFunctionRegister("inputBind", moduleInputBind); X(bind, Bind);
moduleBaseFunctionRegister("inputIsDown", moduleInputIsDown); X(isDown, IsDown);
moduleBaseFunctionRegister("inputPressed", moduleInputPressed); X(pressed, Pressed);
moduleBaseFunctionRegister("inputReleased", moduleInputReleased); X(released, Released);
moduleBaseFunctionRegister("inputGetValue", moduleInputGetValue); X(getValue, GetValue);
moduleBaseFunctionRegister("inputAxis", moduleInputAxis); X(axis, Axis);
moduleBaseFunctionRegister("inputGetEventAction", moduleInputGetEventAction); X(axis2D, Axis2D);
X(getEventAction, GetEventAction);
#undef X
} }
+2 -2
View File
@@ -7,23 +7,23 @@
#pragma once #pragma once
#include "script/module/script/modulescript.h" #include "script/module/script/modulescript.h"
#include "script/module/math/modulemath.h"
#include "script/module/entity/moduleentity.h" #include "script/module/entity/moduleentity.h"
#include "script/module/input/moduleinput.h" #include "script/module/input/moduleinput.h"
#include "script/module/moduleplatform.h" #include "script/module/moduleplatform.h"
#include "script/module/time/moduletime.h" #include "script/module/time/moduletime.h"
#include "script/module/display/modulecolor.h" #include "script/module/display/modulecolor.h"
#include "script/module/math/modulemath.h"
#include "script/module/display/modulescreen.h" #include "script/module/display/modulescreen.h"
#include "script/module/scene/modulescene.h" #include "script/module/scene/modulescene.h"
static void moduleRegister(void) { static void moduleRegister(void) {
moduleScript(); moduleScript();
moduleMath();
moduleEntity(); moduleEntity();
moduleInput(); moduleInput();
modulePlatform(); modulePlatform();
moduleTime(); moduleTime();
moduleColor(); moduleColor();
moduleMath();
moduleScreen(); moduleScreen();
moduleScene(); moduleScene();
} }