Skip to content

Commit

Permalink
feat(engine): remove bindings from plugins and add JSON bindings to s…
Browse files Browse the repository at this point in the history
…amples
  • Loading branch information
diogomsmiranda committed Feb 10, 2024
1 parent 0612d99 commit 372978c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 24 deletions.
41 changes: 41 additions & 0 deletions engine/samples/voxels/assets/sample.bind
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"actions": {
},
"axes": {
"free-horizontal-x": {
"pos": [
"w",
"Up"
],
"neg": [
"s",
"Down"
],
"gamepad": [
"LY"
]
},
"free-horizontal-z": {
"pos": [
"d",
"Right"
],
"neg": [
"a",
"Left"
],
"gamepad": [
"LX"
]
},
"free-vertical": {
"pos": [
"Space"
],
"neg": [
"S-Space"
],
"gamepad": []
}
}
}
3 changes: 3 additions & 0 deletions engine/samples/voxels/assets/sample.bind.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "bf49ba61-5103-41bc-92e0-8a442d7842d4"
}
15 changes: 15 additions & 0 deletions engine/samples/voxels/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cubos/engine/input/input.hpp>
#include <cubos/engine/input/plugin.hpp>
#include <cubos/engine/renderer/directional_light.hpp>
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>
Expand All @@ -12,6 +14,8 @@ static const Asset<VoxelGrid> CarAsset = AnyAsset("059c16e7-a439-44c7-9bdc-6e069
static const Asset<VoxelPalette> PaletteAsset = AnyAsset("1aa5e234-28cb-4386-99b4-39386b0fc215");
/// [Get handles to assets]

static const Asset<InputBindings> BindingsAsset = AnyAsset("bf49ba61-5103-41bc-92e0-8a442d7842d4");

int main(int argc, char** argv)
{
Cubos cubos{argc, argv};
Expand All @@ -21,11 +25,22 @@ int main(int argc, char** argv)
cubos.addPlugin(voxelsPlugin);
/// [Adding the plugin]
cubos.addPlugin(freeCameraControllerPlugin);
/// [Adding the plugin]
cubos.addPlugin(inputPlugin);

cubos.startupSystem("configure Assets").tagged("cubos.settings").call([](Settings& settings) {
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
});

/// [Loading the bindings]
cubos.startupSystem("load and set the Input Bindings")
.tagged("cubos.assets")
.call([](const Assets& assets, Input& input) {
auto bindings = assets.read<InputBindings>(BindingsAsset);
input.bind(*bindings);
CUBOS_INFO("Loaded bindings: {}", input.bindings().at(0));
});

cubos.startupSystem("create a camera").call([](Commands cmds, ActiveCameras& activeCameras) {
// Spawn the camera entity.
activeCameras.entities[0] = cmds.create()
Expand Down
16 changes: 0 additions & 16 deletions engine/src/utils/free_camera_controller/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ void cubos::engine::freeCameraControllerPlugin(Cubos& cubos)
}
});

cubos.startupSystem("create default InputBindings for FreeCameraController")
.tagged("cubos.input")
.call([](Input& input) {
InputBindings bindings;
bindings.axes()["free-horizontal-x"] = InputAxis{{{Key::W, Modifiers::None}, {Key::Up, Modifiers::None}},
{{Key::S, Modifiers::None}, {Key::Down, Modifiers::None}},
{}};
bindings.axes()["free-horizontal-z"] = InputAxis{{{Key::D, Modifiers::None}, {Key::Right, Modifiers::None}},
{{Key::A, Modifiers::None}, {Key::Left, Modifiers::None}},
{}};
bindings.axes()["free-vertical"] =
InputAxis{{{Key::Space, Modifiers::None}}, {{Key::LAlt, Modifiers::None}}, {}};

input.bind(bindings);
});

cubos.startupSystem("set initial FreeCameraController rotation")
.call([](Query<FreeCameraController&, const Rotation&> entities) {
for (auto [controller, rotation] : entities)
Expand Down
13 changes: 13 additions & 0 deletions tools/tesseratos/samples/main/assets/tesseratos.bind
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"actions": {
"free-toggle": {
"keys": [
"LControl"
],
"gamepad": [],
"mouse": []
}
},
"axes": {
}
}
3 changes: 3 additions & 0 deletions tools/tesseratos/samples/main/assets/tesseratos.bind.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "bf49ba61-5103-41bc-92e0-8a331d7842e5"
}
15 changes: 15 additions & 0 deletions tools/tesseratos/samples/main/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cubos/engine/input/input.hpp>
#include <cubos/engine/input/plugin.hpp>
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>
#include <cubos/engine/transform/plugin.hpp>
Expand All @@ -7,23 +9,36 @@
using cubos::core::ecs::Commands;

using cubos::engine::ActiveCameras;
using cubos::engine::Assets;
using cubos::engine::Camera;
using cubos::engine::Cubos;
using cubos::engine::Input;
using cubos::engine::LocalToWorld;
using cubos::engine::Position;
using cubos::engine::Rotation;
using cubos::engine::Settings;

static const Asset<InputBindings> BindingsAsset = AnyAsset("bf49ba61-5103-41bc-92e0-8a331d7842e5");

int main(int argc, char** argv)
{
Cubos cubos{argc, argv};
cubos.addPlugin(tesseratos::plugin);
cubos.addPlugin(cubos::engine::inputPlugin);

cubos.startupSystem("configure Assets plugin").tagged("cubos.settings").call([](Settings& settings) {
settings.setString("assets.io.path", SAMPLE_ASSETS_FOLDER);
settings.setBool("assets.io.readOnly", false);
});

cubos.startupSystem("load and set the Input Bindings")
.tagged("cubos.assets")
.call([](const Assets& assets, Input& input) {
auto bindings = assets.read<InputBindings>(BindingsAsset);
input.bind(*bindings);
CUBOS_INFO("Loaded bindings: {}", input.bindings().at(0));
});

cubos.startupSystem("create a Camera").call([](ActiveCameras& camera, Commands cmds) {
camera.entities[0] =
cmds.create()
Expand Down
10 changes: 2 additions & 8 deletions tools/tesseratos/src/tesseratos/debug_camera/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,15 @@ void tesseratos::debugCameraPlugin(Cubos& cubos)
cubos.addPlugin(cubos::engine::imguiPlugin);
cubos.addPlugin(toolboxPlugin);
cubos.addPlugin(cubos::engine::freeCameraControllerPlugin);
cubos.addPlugin(cubos::engine::inputPlugin);

cubos.addResource<DebugCameraInfo>();

cubos.startupSystem("create Debug Camera")
.tagged("cubos.input")
.after("cubos.fcc.init")
.call([](Input& input, Window& window, Query<Camera&, Position&, FreeCameraController&> entities,
Commands commands, DebugCameraInfo& debugCamera) {
.call([](Window& window, Query<Camera&, Position&, FreeCameraController&> entities, Commands commands,
DebugCameraInfo& debugCamera) {
debugCamera.ent = commands.create().add(Camera{}).add(Position{{}}).add(FreeCameraController{}).entity();

cubos::engine::InputBindings bindings = input.bindings().at(0);
bindings.actions()["free-toggle"] = cubos::engine::InputAction{{{Key::F, Modifiers::None}}, {}, {}};
input.bind(bindings);

for (auto [camera, position, controller] : entities)
{
window->mouseState(MouseState::Default);
Expand Down

0 comments on commit 372978c

Please sign in to comment.