📜 ⬆️ ⬇️

Working with Android gestures using the Linderdaum Engine

Today we will talk about how to make the scene control multitouch and pinch-zoom gesture on Android using the Linderdaum Engine .

Linderdaum Engine logo




')
1. Preparation

We assume that the Linderdaum Engine SDK is already installed and configured. About configuring the SDK and how to compile the application, you can read in the post habrahabr.ru/blogs/gdev/121062 The only thing that we will need in addition is an Android device with multitouch support.

2. We write application

To write the most compact code, we will use the new feature - the class clPinchZoomHandler. For independent work with multitouch and writing handlers for other gestures there is a class clGestureHandler. If you want hardcore, you can intercept the L_EVENT_MOTION event, in which all the presses and releases of fingers on the screen will come (including a mouse on a PC, so that it is easier to debug).

Run the Project Wizard and select:



We throw out everything from the .cpp file that is accumulated, and write the following code there:

#include "Linderdaum.h" sEnvironment* Env = NULL; clScene* g_Scene = NULL; clPinchZoomHandler* g_PinchZoomHandler = NULL; LMatrix4 Position = LMatrix4::IdentityStatic(); LMatrix4 PositionDelta = LMatrix4::IdentityStatic(); LMatrix4 ZoomDelta = LMatrix4::IdentityStatic(); int g_PlaneID; //      ,     void Event_DRAW( LEvent Event, const LEventArgs& Args ) { //   g_Scene->RenderForward(); //     ,      if ( !g_PinchZoomHandler->IsGestureValid() ) return; if ( g_PinchZoomHandler->IsDraggingValid() ) { //    PositionDelta = g_PinchZoomHandler->GetTranslationMatrix(); } else { //       Position = Position * PositionDelta; PositionDelta = LMatrix4::Identity(); } float ZoomFactor = 1.0f; if ( g_PinchZoomHandler->IsPinchZoomValid() ) { //    ZoomFactor = g_PinchZoomHandler->GetPinchZoomFactor(); ZoomDelta = g_PinchZoomHandler->GetPinchZoomMatrix(); } else { //       Position = Position * ZoomDelta; ZoomDelta = LMatrix4::Identity(); } //      g_Scene->SetLocalTransform( g_PlaneID, Position * PositionDelta * ZoomDelta ); //            size_t NumContacts = g_PinchZoomHandler->GetMotionData()->GetNumTouchPoints(); clCanvas* C = Env->Renderer->GetCanvas(); LString Str1( "Zoom factor: " + LStr::ToStr( ZoomFactor ) ); LString Str2( "Active : " + LStr::ToStr( NumContacts ) ); C->TextStrFreeType( LRect( 0.0f, 0.0f ), Str1, 0.05f, LC_White, NULL, TextAlign_Left ); C->TextStrFreeType( LRect( 0.0f, 0.05f ), Str2, 0.05f, LC_White, NULL, TextAlign_Left ); } //  APPLICATION_ENTRY_POINT { Env = new sEnvironment(); Env->DeployDefaultEnvironment( "", "../../CommonMedia" ); Env->Connect( L_EVENT_DRAWOVERLAY, Utils::Bind( &Event_DRAW ) ); //  ,      clGeom* TestGeom1 = Env->Resources->CreatePlane( 0.1f, 0.2f, 0.9f, 0.8f, -0.1f, 10 ); //     g_Scene = Env->Linker->Instantiate( "clScene" ); g_Scene->SetCameraTransform( LMatrix4::GetTranslateMatrix( LVector3(0.0f) ) ); g_Scene->SetCameraProjection( Env->Renderer->GetCanvas()->GetOrthoMatrices()->GetProjectionMatrix() ); g_Scene->SetUseOffscreenBuffer( false, false ); //      g_PlaneID = g_Scene->AddGeom( TestGeom1 ); //     clMaterial* Mtl = Env->Resources->CreateMaterial(); Mtl->SetPropertyValue( "DiffuseMap", "banner.jpg" ); g_Scene->SetMtl( g_PlaneID, Mtl ); //    -        g_PinchZoomHandler = Env->Linker->Instantiate( "clPinchZoomHandler" ); Env->RunApplication( DEFAULT_CONSOLE_AUTOEXEC ); APPLICATION_EXIT_POINT( Env ); } //  APPLICATION_SHUTDOWN { delete( g_Scene ); delete( g_PinchZoomHandler ); } 

In the Data folder, save the file banner.jpg. Here is such (256x128):
image
Run cygwin and write:
ndk-build
ant copy-common-media debug

We get the .apk file for the device.

3. Result

This is how it looks on the device:
image
You can move the picture with one finger and scale with two.

In the SDK itself there is a slightly more complex example of working with pinch zoom.

Source: https://habr.com/ru/post/124688/


All Articles