From a2a22e5e4873111d45c0b9a09d482407d22a89ce Mon Sep 17 00:00:00 2001 From: Jett <30197659+JettMonstersGoBoom@users.noreply.github.com> Date: Sun, 28 Sep 2025 17:02:53 -0400 Subject: [PATCH] ADDED: example: `core_input_actions` (#5211) * adding core_input_actions action based input API vs direct input, allows remapping of buttons or keys to an action. * adjusted formatting. * updates for struct name and fixed a typo --- examples/Makefile | 1 + examples/core/core_input_actions.c | 174 +++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 examples/core/core_input_actions.c diff --git a/examples/Makefile b/examples/Makefile index ee17bb350..6d409b511 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -524,6 +524,7 @@ CORE = \ core/core_input_mouse_wheel \ core/core_input_multitouch \ core/core_input_virtual_controls \ + core/core_input_actions \ core/core_random_sequence \ core/core_random_values \ core/core_render_texture \ diff --git a/examples/core/core_input_actions.c b/examples/core/core_input_actions.c new file mode 100644 index 000000000..b8266a8f4 --- /dev/null +++ b/examples/core/core_input_actions.c @@ -0,0 +1,174 @@ + +/******************************************************************************************* +* +* raylib [core_inputactionInputs] example - presents a simple API for remapping input to actions +* +* Example complexity rating: [★☆☆☆] 1/4 +* +* Example originally created with raylib 5.5, last time updated with raylib 5.6 +* +* Example contributed by MonstersGoBoom and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2025 MonstersGoBoom +* +********************************************************************************************/ + +/* + Simple example for decoding input as actions, allowing remapping of input to different keys or gamepad buttons. + for example instead of + IsKeyDown(KEY_LEFT) + you'd use + IsActionDown(ACTION_LEFT) + which can be reassigned to e.g. KEY_A and also assigned to a gamepad button. the action will trigger with either gamepad or keys + */ + + +#include +#include +#include +#include "raylib.h" + +// add your own action types here + +typedef enum ActionType +{ + NO_ACTION, + ACTION_UP, + ACTION_DOWN, + ACTION_LEFT, + ACTION_RIGHT, + ACTION_FIRE, + MAX_ACTION +} ActionType; + +// struct for key and button inputs +typedef struct ActionInput +{ + int key; + int button; +} ActionInput; + +// gamepad index, change this if you have multiple gamepads. +int gamepadIndex = 0; +static ActionInput actionInputs[MAX_ACTION] = {0}; + +// combines IsKeyPressed and IsGameButtonPressed to one action +bool isActionPressed(int action) +{ + if (action