Skip to content

Commit

Permalink
add event handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
wutipong committed Apr 14, 2024
1 parent e1bf4cd commit 2fbd6ee
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/DemoScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <ICameraController.h>
#include <IGraphics.h>
#include <IInput.h>
#include <IOperatingSystem.h>
#include <IResourceLoader.h>
#include <IUI.h>
Expand Down Expand Up @@ -168,6 +169,60 @@ bool DemoScene::Init(Renderer *pRenderer)
};
addSampler(pRenderer, &samplerDesc, &pSampler);

typedef bool (*CameraInputHandler)(InputActionContext *ctx, DefaultInputActions::DefaultInputAction action);
static CameraInputHandler onCameraInput =
[](InputActionContext *ctx, DefaultInputActions::DefaultInputAction action)
{
if (*(ctx->pCaptured))
{
float2 delta = uiIsFocused() ? float2(0.f, 0.f) : ctx->mFloat2;
switch (action)
{
case DefaultInputActions::ROTATE_CAMERA:
pCameraController->onRotate(delta);
break;
case DefaultInputActions::TRANSLATE_CAMERA:
pCameraController->onMove(delta);
break;
case DefaultInputActions::TRANSLATE_CAMERA_VERTICAL:
pCameraController->onMoveY(delta[0]);
break;
default:
break;
}
}
return true;
};
InputActionDesc actionDesc = {DefaultInputActions::CAPTURE_INPUT,
[](InputActionContext *ctx)
{
setEnableCaptureInput(!uiIsFocused() &&
INPUT_ACTION_PHASE_CANCELED != ctx->mPhase);
return true;
},
NULL};
addInputAction(&actionDesc);
actionDesc = {DefaultInputActions::ROTATE_CAMERA,
[](InputActionContext *ctx) { return onCameraInput(ctx, DefaultInputActions::ROTATE_CAMERA); }, NULL};
addInputAction(&actionDesc);
actionDesc = {DefaultInputActions::TRANSLATE_CAMERA,
[](InputActionContext *ctx) { return onCameraInput(ctx, DefaultInputActions::TRANSLATE_CAMERA); },
NULL};
addInputAction(&actionDesc);
actionDesc = {DefaultInputActions::TRANSLATE_CAMERA_VERTICAL,
[](InputActionContext *ctx)
{ return onCameraInput(ctx, DefaultInputActions::TRANSLATE_CAMERA_VERTICAL); },
NULL};
addInputAction(&actionDesc);
actionDesc = {DefaultInputActions::RESET_CAMERA,
[](InputActionContext *ctx)
{
if (!uiWantTextInput())
pCameraController->resetView();
return true;
}};
addInputAction(&actionDesc);

waitForToken(&token);

tf_free(sphereVertices);
Expand Down

0 comments on commit 2fbd6ee

Please sign in to comment.