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() {
var speed = 3.0;
var dx = inputAxis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT);
var dz = inputAxis(INPUT_ACTION_UP, INPUT_ACTION_DOWN);
this.position.x += dx * speed * TIME.delta;
this.position.z += dz * speed * TIME.delta;
var move = Input.axis2D(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT, INPUT_ACTION_UP, INPUT_ACTION_DOWN);
this.position.x += move.x * speed * TIME.delta;
this.position.z += move.y * speed * TIME.delta;
this.material.setColor(Color.rainbow());
};
+50 -50
View File
@@ -1,75 +1,75 @@
// Default input bindings.
if (typeof PSP !== 'undefined') {
inputBind("up", INPUT_ACTION_UP);
inputBind("down", INPUT_ACTION_DOWN);
inputBind("left", INPUT_ACTION_LEFT);
inputBind("right", INPUT_ACTION_RIGHT);
inputBind("accept", INPUT_ACTION_ACCEPT);
inputBind("cancel", INPUT_ACTION_CANCEL);
inputBind("select", INPUT_ACTION_RAGEQUIT);
inputBind("lstick_up", INPUT_ACTION_UP);
inputBind("lstick_down", INPUT_ACTION_DOWN);
inputBind("lstick_left", INPUT_ACTION_LEFT);
inputBind("lstick_right", INPUT_ACTION_RIGHT);
inputBind("triangle", INPUT_ACTION_CONSOLE);
Input.bind("up", INPUT_ACTION_UP);
Input.bind("down", INPUT_ACTION_DOWN);
Input.bind("left", INPUT_ACTION_LEFT);
Input.bind("right", INPUT_ACTION_RIGHT);
Input.bind("accept", INPUT_ACTION_ACCEPT);
Input.bind("cancel", INPUT_ACTION_CANCEL);
Input.bind("select", INPUT_ACTION_RAGEQUIT);
Input.bind("lstick_up", INPUT_ACTION_UP);
Input.bind("lstick_down", INPUT_ACTION_DOWN);
Input.bind("lstick_left", INPUT_ACTION_LEFT);
Input.bind("lstick_right", INPUT_ACTION_RIGHT);
Input.bind("triangle", INPUT_ACTION_CONSOLE);
} else if (typeof DOLPHIN !== 'undefined') {
inputBind("up", INPUT_ACTION_UP);
inputBind("down", INPUT_ACTION_DOWN);
inputBind("left", INPUT_ACTION_LEFT);
inputBind("right", INPUT_ACTION_RIGHT);
inputBind("b", INPUT_ACTION_CANCEL);
inputBind("a", INPUT_ACTION_ACCEPT);
inputBind("z", INPUT_ACTION_CONSOLE);
inputBind("lstick_up", INPUT_ACTION_UP);
inputBind("lstick_down", INPUT_ACTION_DOWN);
inputBind("lstick_left", INPUT_ACTION_LEFT);
inputBind("lstick_right", INPUT_ACTION_RIGHT);
Input.bind("up", INPUT_ACTION_UP);
Input.bind("down", INPUT_ACTION_DOWN);
Input.bind("left", INPUT_ACTION_LEFT);
Input.bind("right", INPUT_ACTION_RIGHT);
Input.bind("b", INPUT_ACTION_CANCEL);
Input.bind("a", INPUT_ACTION_ACCEPT);
Input.bind("z", INPUT_ACTION_CONSOLE);
Input.bind("lstick_up", INPUT_ACTION_UP);
Input.bind("lstick_down", INPUT_ACTION_DOWN);
Input.bind("lstick_left", INPUT_ACTION_LEFT);
Input.bind("lstick_right", INPUT_ACTION_RIGHT);
} else if (typeof LINUX !== 'undefined') {
if (typeof INPUT_KEYBOARD !== 'undefined') {
inputBind("w", INPUT_ACTION_UP);
inputBind("s", INPUT_ACTION_DOWN);
inputBind("a", INPUT_ACTION_LEFT);
inputBind("d", INPUT_ACTION_RIGHT);
Input.bind("w", INPUT_ACTION_UP);
Input.bind("s", INPUT_ACTION_DOWN);
Input.bind("a", INPUT_ACTION_LEFT);
Input.bind("d", INPUT_ACTION_RIGHT);
inputBind("left", INPUT_ACTION_LEFT);
inputBind("right", INPUT_ACTION_RIGHT);
inputBind("up", INPUT_ACTION_UP);
inputBind("down", INPUT_ACTION_DOWN);
Input.bind("left", INPUT_ACTION_LEFT);
Input.bind("right", INPUT_ACTION_RIGHT);
Input.bind("up", INPUT_ACTION_UP);
Input.bind("down", INPUT_ACTION_DOWN);
inputBind("enter", INPUT_ACTION_ACCEPT);
inputBind("e", INPUT_ACTION_ACCEPT);
Input.bind("enter", 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);
inputBind("`", INPUT_ACTION_CONSOLE);
Input.bind("escape", INPUT_ACTION_RAGEQUIT);
Input.bind("`", INPUT_ACTION_CONSOLE);
}
if (typeof INPUT_GAMEPAD !== 'undefined') {
inputBind("gamepad_up", INPUT_ACTION_UP);
inputBind("gamepad_down", INPUT_ACTION_DOWN);
inputBind("gamepad_left", INPUT_ACTION_LEFT);
inputBind("gamepad_right", INPUT_ACTION_RIGHT);
Input.bind("gamepad_up", INPUT_ACTION_UP);
Input.bind("gamepad_down", INPUT_ACTION_DOWN);
Input.bind("gamepad_left", INPUT_ACTION_LEFT);
Input.bind("gamepad_right", INPUT_ACTION_RIGHT);
inputBind("gamepad_a", INPUT_ACTION_ACCEPT);
inputBind("gamepad_b", INPUT_ACTION_CANCEL);
inputBind("gamepad_back", INPUT_ACTION_RAGEQUIT);
Input.bind("gamepad_a", INPUT_ACTION_ACCEPT);
Input.bind("gamepad_b", INPUT_ACTION_CANCEL);
Input.bind("gamepad_back", INPUT_ACTION_RAGEQUIT);
inputBind("gamepad_lstick_up", INPUT_ACTION_UP);
inputBind("gamepad_lstick_down", INPUT_ACTION_DOWN);
inputBind("gamepad_lstick_left", INPUT_ACTION_LEFT);
inputBind("gamepad_lstick_right", INPUT_ACTION_RIGHT);
Input.bind("gamepad_lstick_up", INPUT_ACTION_UP);
Input.bind("gamepad_lstick_down", INPUT_ACTION_DOWN);
Input.bind("gamepad_lstick_left", INPUT_ACTION_LEFT);
Input.bind("gamepad_lstick_right", INPUT_ACTION_RIGHT);
}
if (typeof INPUT_POINTER !== 'undefined') {
inputBind("mouse_x", INPUT_ACTION_POINTERX);
inputBind("mouse_y", INPUT_ACTION_POINTERY);
Input.bind("mouse_x", INPUT_ACTION_POINTERX);
Input.bind("mouse_y", INPUT_ACTION_POINTERY);
}
} else {
consolePrint("Unknown platform, no default input bindings set.");
print("Unknown platform, no default input bindings set.");
}
Scene.set('scenes/cube.js');
+73 -55
View File
@@ -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
}
+2 -2
View File
@@ -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();
}